Accept page and per page params in REST query, return total count of notes in header

This commit is contained in:
Allen Snook 2018-10-11 12:25:09 -07:00
parent 68b799f796
commit 054fbb4ee3
3 changed files with 78 additions and 8 deletions

View File

@ -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;
}
/**

View File

@ -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();
}
}

View File

@ -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
}
}