From 054fbb4ee3def3d504e946807ba43f08f87069aa Mon Sep 17 00:00:00 2001 From: Allen Snook Date: Thu, 11 Oct 2018 12:25:09 -0700 Subject: [PATCH] Accept page and per page params in REST query, return total count of notes in header --- ...s-wc-admin-rest-admin-notes-controller.php | 23 ++++++++- .../includes/class-wc-admin-notes.php | 15 +++++- .../class-wc-admin-notes-data-store.php | 48 +++++++++++++++++-- 3 files changed, 78 insertions(+), 8 deletions(-) diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-admin-notes-controller.php b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-admin-notes-controller.php index 3dbe46ef077..aaad9e77529 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-admin-notes-controller.php +++ b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-admin-notes-controller.php @@ -90,7 +90,22 @@ class WC_Admin_REST_Admin_Notes_Controller extends WC_REST_CRUD_Controller { * @return WP_REST_Response */ public function get_items( $request ) { - $notes = WC_Admin_Notes::get_notes(); + $per_page = isset( $request['per_page'] ) ? intval( $request['per_page'] ) : 10; + if ( $per_page <= 0 ) { + $per_page = 1; + } + + $page = isset( $request['page'] ) ? intval( $request['page'] ) : 1; + if ( $page <= 0 ) { + $page = 1; + } + + $args = array( + 'per_page' => $per_page, + 'page' => $page, + ); + + $notes = WC_Admin_Notes::get_notes( 'edit', $args ); $data = array(); foreach ( (array) $notes as $note_obj ) { @@ -98,7 +113,11 @@ class WC_Admin_REST_Admin_Notes_Controller extends WC_REST_CRUD_Controller { $note = $this->prepare_response_for_collection( $note ); $data[] = $note; } - return rest_ensure_response( $data ); + + $response = rest_ensure_response( $data ); + $response->header( 'X-WP-Total', WC_Admin_Notes::get_notes_count() ); + + return $response; } /** diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-notes.php b/plugins/woocommerce-admin/includes/class-wc-admin-notes.php index 550a448eec0..db2f4ce639a 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-notes.php +++ b/plugins/woocommerce-admin/includes/class-wc-admin-notes.php @@ -16,11 +16,12 @@ class WC_Admin_Notes { * Get notes from the database. * * @param string $context Getting notes for what context. Valid values: view, edit. + * @param array $args Arguments to pass to the query( e.g. per_page and page). * @return array Array of arrays. */ - public static function get_notes( $context = 'admin' ) { + public static function get_notes( $context = 'admin', $args = array() ) { $data_store = WC_Data_Store::load( 'admin-note' ); - $raw_notes = $data_store->get_notes(); + $raw_notes = $data_store->get_notes( $args ); $notes = array(); foreach ( (array) $raw_notes as $raw_note ) { $note = new WC_Admin_Note( $raw_note ); @@ -58,4 +59,14 @@ class WC_Admin_Notes { } return false; } + + /** + * Get the total number of notes + * + * @return int + */ + public static function get_notes_count() { + $data_store = WC_Data_Store::load( 'admin-note' ); + return $data_store->get_notes_count(); + } } diff --git a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-notes-data-store.php b/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-notes-data-store.php index 000efcd9df3..f3d2e63de47 100644 --- a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-notes-data-store.php +++ b/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-notes-data-store.php @@ -33,7 +33,7 @@ class WC_Admin_Notes_Data_Store extends WC_Data_Store_WP implements WC_Object_Da 'source' => $note->get_source(), ); - $encoding_options = defined( 'JSON_FORCE_OBJECT' ) ? JSON_FORCE_OBJECT : 0; + $encoding_options = defined( 'JSON_FORCE_OBJECT' ) ? JSON_FORCE_OBJECT : 0; // phpcs:ignore PHPCompatibility.PHP.NewConstants $note_to_be_inserted['content_data'] = wp_json_encode( $note->get_content_data(), $encoding_options ); $note_to_be_inserted['date_created'] = gmdate( 'Y-m-d H:i:s', $date_created ); @@ -104,7 +104,7 @@ class WC_Admin_Notes_Data_Store extends WC_Data_Store_WP implements WC_Object_Da */ public function update( &$note ) { global $wpdb; - $encoding_options = defined( 'JSON_FORCE_OBJECT' ) ? JSON_FORCE_OBJECT : 0; + $encoding_options = defined( 'JSON_FORCE_OBJECT' ) ? JSON_FORCE_OBJECT : 0; // phpcs:ignore PHPCompatibility.PHP.NewConstants if ( $note->get_id() ) { $wpdb->update( @@ -199,10 +199,50 @@ class WC_Admin_Notes_Data_Store extends WC_Data_Store_WP implements WC_Object_Da /** * Return an ordered list of notes. * + * @param array $args Query arguments. * @return array An array of objects containing a note id. */ - public function get_notes() { + public function get_notes( $args = array() ) { global $wpdb; - return $wpdb->get_results( "SELECT note_id, title, content FROM {$wpdb->prefix}woocommerce_admin_notes order by note_id ASC;" ); + + // Build the query. + $query = " + SELECT note_id, title, content + FROM {$wpdb->prefix}woocommerce_admin_notes + ORDER BY note_id DESC + "; + + $per_page = isset( $args['per_page'] ) ? intval( $args['per_page'] ) : 10; + if ( $per_page <= 0 ) { + $per_page = 1; + } + + $page = isset( $args['page'] ) ? intval( $args['page'] ) : 1; + if ( $page <= 0 ) { + $page = 1; + } + + $offset = $per_page * ( $page - 1 ); + $pagination = sprintf( ' LIMIT %d, %d', $offset, $per_page ); + + return $wpdb->get_results( $query . $pagination ); // phpcs:ignore WordPress.WP.PreparedSQL.NotPrepared } + + /** + * Return a count of notes. + * + * @return array An array of objects containing a note id. + */ + public function get_notes_count() { + global $wpdb; + + // Build the query. + $query = " + SELECT COUNT(*) + FROM {$wpdb->prefix}woocommerce_admin_notes + "; + + return $wpdb->get_var( $query ); // phpcs:ignore WordPress.WP.PreparedSQL.NotPrepared + } + }