namespace, '/groups', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_items' ), 'permission_callback' => array( $this, 'check_permission' ), ), array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'create_item' ), 'permission_callback' => array( $this, 'check_permission' ), ), ) ); register_rest_route( $this->namespace, '/groups/(?P\d+)', array( array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_item' ), 'permission_callback' => array( $this, 'check_permission' ), ), array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array( $this, 'delete_item' ), 'permission_callback' => array( $this, 'check_permission' ), ), ) ); } /** * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public function get_items( $request ) { global $wpdb; $lists_table = Mailpuff_Schema::table( Mailpuff_Schema::LISTS ); $subscriber_lists_table = Mailpuff_Schema::table( Mailpuff_Schema::SUBSCRIBER_LISTS ); $rows = $wpdb->get_results( $wpdb->prepare( 'SELECT g.*, COUNT( cg.contact_id ) AS contact_count FROM %i g LEFT JOIN %i cg ON cg.group_id = g.id GROUP BY g.id ORDER BY g.name ASC', $lists_table, $subscriber_lists_table ) ); foreach ( (array) $rows as $row ) { $id = (int) $row->id; // What a campaign to it reaches now, as Review counts it. $row->subscribed_count = Mailpuff_Audience::count( array( 'type' => 'group', 'id' => $id, ) ); $row->campaigns_using = Mailpuff_Audience::unsent_campaigns( 'group', $id ); $row->segments_using = Mailpuff_Audience::segments_with_group( $id ); } return $this->success( array( 'items' => $rows ) ); } /** * @param WP_REST_Request $request Request object. * @return WP_REST_Response|WP_Error */ public function create_item( $request ) { global $wpdb; $name = sanitize_text_field( $request->get_param( 'name' ) ); $description = sanitize_textarea_field( $request->get_param( 'description' ) ); if ( empty( $name ) ) { return $this->error( 'missing_name', __( 'Tag name is required.', 'mailpuff' ), 400 ); } $table = Mailpuff_Schema::table( Mailpuff_Schema::LISTS ); $exists = $wpdb->get_var( $wpdb->prepare( 'SELECT id FROM %i WHERE name = %s', $table, $name ) ); if ( $exists ) { return $this->error( 'duplicate_name', __( 'A tag with that name already exists.', 'mailpuff' ), 409 ); } $wpdb->insert( $table, array( 'name' => $name, 'description' => $description, 'visibility' => Mailpuff_Group::visibility( $request->get_param( 'visibility' ) ), 'created_at' => current_time( 'mysql' ), ), array( '%s', '%s', '%s', '%s' ) ); $group_id = (int) $wpdb->insert_id; $group = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE id = %d', $table, $group_id ) ); return $this->success( $group, 201 ); } /** * @param WP_REST_Request $request Request object. * @return WP_REST_Response|WP_Error */ public function update_item( $request ) { global $wpdb; $id = absint( $request->get_param( 'id' ) ); $table = Mailpuff_Schema::table( Mailpuff_Schema::LISTS ); $group = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE id = %d', $table, $id ) ); if ( ! $group ) { return $this->error( 'not_found', __( 'Tag not found.', 'mailpuff' ), 404 ); } $data = array(); $formats = array(); $name = $request->get_param( 'name' ); if ( null !== $name ) { $name = sanitize_text_field( $name ); if ( empty( $name ) ) { return $this->error( 'missing_name', __( 'Tag name cannot be empty.', 'mailpuff' ), 400 ); } $exists = $wpdb->get_var( $wpdb->prepare( 'SELECT id FROM %i WHERE name = %s AND id != %d', $table, $name, $id ) ); if ( $exists ) { return $this->error( 'duplicate_name', __( 'A tag with that name already exists.', 'mailpuff' ), 409 ); } $data['name'] = $name; $formats[] = '%s'; } $description = $request->get_param( 'description' ); if ( null !== $description ) { $data['description'] = sanitize_textarea_field( $description ); $formats[] = '%s'; } $visibility = $request->get_param( 'visibility' ); if ( null !== $visibility ) { $data['visibility'] = Mailpuff_Group::visibility( $visibility ); $formats[] = '%s'; } if ( empty( $data ) ) { return $this->error( 'no_data', __( 'No data to update.', 'mailpuff' ), 400 ); } $wpdb->update( $table, $data, array( 'id' => $id ), $formats, array( '%d' ) ); $group = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE id = %d', $table, $id ) ); return $this->success( $group ); } /** * @param WP_REST_Request $request Request object. * @return WP_REST_Response|WP_Error */ public function delete_item( $request ) { global $wpdb; $id = absint( $request->get_param( 'id' ) ); $table = Mailpuff_Schema::table( Mailpuff_Schema::LISTS ); $group = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE id = %d', $table, $id ) ); if ( ! $group ) { return $this->error( 'not_found', __( 'Tag not found.', 'mailpuff' ), 404 ); } // Each member leaves through the service, so the group-left hook fires. $subscriber_lists_table = Mailpuff_Schema::table( Mailpuff_Schema::SUBSCRIBER_LISTS ); foreach ( (array) $wpdb->get_col( $wpdb->prepare( 'SELECT contact_id FROM %i WHERE group_id = %d', $subscriber_lists_table, $id ) ) as $contact_id ) { Mailpuff_Contact_Service::remove_from_group( (int) $contact_id, $id ); } $wpdb->delete( $table, array( 'id' => $id ), array( '%d' ) ); // The tag's own welcome email goes with it. Mailpuff_Tag_Emails::forget( $id ); return $this->success( array( 'deleted' => true ) ); } }