diff --git a/plugins/woocommerce-admin/client/header/activity-panel/panels/inbox.js b/plugins/woocommerce-admin/client/header/activity-panel/panels/inbox.js index d2d545c292c..4357c20af50 100644 --- a/plugins/woocommerce-admin/client/header/activity-panel/panels/inbox.js +++ b/plugins/woocommerce-admin/client/header/activity-panel/panels/inbox.js @@ -93,6 +93,7 @@ export default compose( const inboxQuery = { page: 1, per_page: QUERY_DEFAULTS.pageSize, + type: 'info,warning', }; const notes = getNotes( inboxQuery ); 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 c8b04aecd4a..8bdbcad91b0 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 @@ -109,6 +109,12 @@ class WC_Admin_REST_Admin_Notes_Controller extends WC_REST_CRUD_Controller { 'page' => $page, ); + $type = isset( $request['type'] ) ? $request['type'] : ''; + $type = sanitize_text_field( $type ); + if ( ! empty( $type ) ) { + $args['type'] = $type; + } + $notes = WC_Admin_Notes::get_notes( 'edit', $args ); $data = array(); 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 5995534ea01..14f79b72d5d 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 @@ -227,16 +227,37 @@ class WC_Admin_Notes_Data_Store extends WC_Data_Store_WP implements WC_Object_Da $page = 1; } - $offset = $per_page * ( $page - 1 ); - $pagination = sprintf( ' LIMIT %d, %d', $offset, $per_page ); + $offset = $per_page * ( $page - 1 ); - return $wpdb->get_results( - $wpdb->prepare( + $allowed_types = WC_Admin_Note::get_allowed_types(); + $where_type_array = array(); + if ( isset( $args['type'] ) ) { + $args_types = explode( ',', $args['type'] ); + foreach ( (array) $args_types as $args_type ) { + $args_type = trim( $args_type ); + if ( in_array( $args_type, $allowed_types, true ) ) { + $where_type_array[] = "'" . esc_sql( $args_type ) . "'"; + } + } + } + $escaped_where_types = implode( ',', $where_type_array ); + + if ( empty( $escaped_where_types ) ) { + $query = $wpdb->prepare( "SELECT note_id, title, content FROM {$wpdb->prefix}woocommerce_admin_notes ORDER BY note_id DESC LIMIT %d, %d", $offset, $per_page - ) - ); + ); + } else { + $query = $wpdb->prepare( + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared + "SELECT note_id, title, content FROM {$wpdb->prefix}woocommerce_admin_notes WHERE type IN ($escaped_where_types) ORDER BY note_id DESC LIMIT %d, %d", + $offset, + $per_page + ); + } + + return $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared } /** @@ -246,7 +267,6 @@ class WC_Admin_Notes_Data_Store extends WC_Data_Store_WP implements WC_Object_Da */ public function get_notes_count() { global $wpdb; - // phpcs:ignore WordPress.WP.PreparedSQL.NotPrepared return $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}woocommerce_admin_notes" ); } diff --git a/plugins/woocommerce-admin/tests/api/admin-notes.php b/plugins/woocommerce-admin/tests/api/admin-notes.php index acfab0db931..feb081211fb 100644 --- a/plugins/woocommerce-admin/tests/api/admin-notes.php +++ b/plugins/woocommerce-admin/tests/api/admin-notes.php @@ -104,6 +104,24 @@ class WC_Tests_API_Admin_Notes extends WC_REST_Unit_Test_Case { $this->assertEquals( 2, count( $notes ) ); } + /** + * Test getting notes of a certain type. + * + * @since 3.5.0 + */ + public function test_get_warning_notes() { + wp_set_current_user( $this->user ); + + $request = new WP_REST_Request( 'GET', $this->endpoint ); + $request->set_query_params( array( 'type' => 'warning' ) ); + $response = $this->server->dispatch( $request ); + $notes = $response->get_data(); + + $this->assertEquals( 200, $response->get_status() ); + $this->assertEquals( 1, count( $notes ) ); + $this->assertEquals( $notes[0]['title'], 'PHPUNIT_TEST_NOTE_2_TITLE' ); + } + /** * Test getting lots of notes without permission. It should fail. *