namespace, '/segments', 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, '/segments/(?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' ), ), ) ); } /** * @return WP_REST_Response { items: [{ id, name, filter, count, … }] } */ public function get_items( $request ) { global $wpdb; $table = Mailpuff_Schema::table( Mailpuff_Schema::SEGMENTS ); $rows = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i ORDER BY name ASC', $table ) ); $items = array(); foreach ( (array) $rows as $row ) { $items[] = $this->shape( $row ); } return $this->success( array( 'items' => $items ) ); } /** * @return WP_REST_Response|WP_Error */ public function create_item( $request ) { global $wpdb; $name = sanitize_text_field( $request->get_param( 'name' ) ); if ( empty( $name ) ) { return $this->error( 'missing_name', __( 'Segment name is required.', 'mailpuff' ), 400 ); } $filter = Mailpuff_Contact_Filter::normalize( $request->get_param( 'filter' ) ); $bad = $this->field_error( $filter ); if ( $bad ) { return $bad; } $table = Mailpuff_Schema::table( Mailpuff_Schema::SEGMENTS ); $exists = $wpdb->get_var( $wpdb->prepare( 'SELECT id FROM %i WHERE name = %s', $table, $name ) ); if ( $exists ) { return $this->error( 'duplicate_name', __( 'A segment with that name already exists.', 'mailpuff' ), 409 ); } $now = current_time( 'mysql' ); $wpdb->insert( $table, array( 'name' => $name, 'filter' => wp_json_encode( $filter ), 'created_at' => $now, 'updated_at' => $now, ), array( '%s', '%s', '%s', '%s' ) ); $row = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE id = %d', $table, (int) $wpdb->insert_id ) ); return $this->success( $this->shape( $row ), 201 ); } /** * @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::SEGMENTS ); $row = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE id = %d', $table, $id ) ); if ( ! $row ) { return $this->error( 'not_found', __( 'Segment 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', __( 'Segment 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 segment with that name already exists.', 'mailpuff' ), 409 ); } $data['name'] = $name; $formats[] = '%s'; } if ( null !== $request->get_param( 'filter' ) ) { $filter = Mailpuff_Contact_Filter::normalize( $request->get_param( 'filter' ) ); $bad = $this->field_error( $filter ); if ( $bad ) { return $bad; } $data['filter'] = wp_json_encode( $filter ); $formats[] = '%s'; } if ( empty( $data ) ) { return $this->error( 'no_data', __( 'No data to update.', 'mailpuff' ), 400 ); } $data['updated_at'] = current_time( 'mysql' ); $formats[] = '%s'; $wpdb->update( $table, $data, array( 'id' => $id ), $formats, array( '%d' ) ); $row = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE id = %d', $table, $id ) ); return $this->success( $this->shape( $row ) ); } /** * @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::SEGMENTS ); $row = $wpdb->get_row( $wpdb->prepare( 'SELECT id FROM %i WHERE id = %d', $table, $id ) ); if ( ! $row ) { return $this->error( 'not_found', __( 'Segment not found.', 'mailpuff' ), 404 ); } $wpdb->delete( $table, array( 'id' => $id ), array( '%d' ) ); return $this->success( array( 'deleted' => true ) ); } /** * The first field condition a filter cannot keep (a field that does not * exist, a condition its type does not take, a missing value), as a 400. * * @param array $filter A normalized filter. * @return WP_Error|null */ private function field_error( array $filter ) { foreach ( (array) ( $filter['fields'] ?? array() ) as $condition ) { $message = Mailpuff_Fields::condition_error( $condition ); if ( '' !== $message ) { return $this->error( 'invalid_field_condition', $message, 400 ); } } return null; } /** * Decode a stored segment row for the API: parse the filter JSON and attach a * live matching-contact count via the shared filter builder. * * @param object $row Raw DB row. * @return array */ private function shape( $row ) { $filter = json_decode( isset( $row->filter ) ? (string) $row->filter : '', true ); $filter = is_array( $filter ) ? $filter : array(); return array( 'id' => (int) $row->id, 'name' => $row->name, 'filter' => $filter, 'count' => $this->count_for_filter( $filter ), // What a campaign to it reaches now, as Review counts it. 'subscribed_count' => Mailpuff_Audience::count( array( 'type' => 'segment', 'id' => (int) $row->id, ) ), 'campaigns_using' => Mailpuff_Audience::unsent_campaigns( 'segment', (int) $row->id ), 'created_at' => $row->created_at, 'updated_at' => $row->updated_at, ); } /** * Count contacts matching a filter (all statuses — the segment's raw size, * mirroring the Contacts list; the send path additionally excludes * unsubscribed / suppressed). * * @param array $filter Canonical filter. * @return int */ private function count_for_filter( $filter ) { global $wpdb; list( $clauses, $params ) = Mailpuff_Contact_Filter::build( $filter ); $table = Mailpuff_Schema::table( Mailpuff_Schema::SUBSCRIBERS ); $sql = 'SELECT COUNT(*) FROM %i c WHERE 1=1' . $clauses; // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- clauses from Mailpuff_Contact_Filter::build() are literal, values and tables bound. return (int) $wpdb->get_var( $wpdb->prepare( $sql, array_merge( array( $table ), $params ) ) ); } }