Activity Panel: Inbox: Filter update and error notes out of the inbox (https://github.com/woocommerce/woocommerce-admin/pull/893)

* Filter update and error notes out of the inbox

* Remove redundant trim().

* Fix test for admin notes API type parameter.
This commit is contained in:
Allen Snook 2018-12-10 17:00:57 -08:00 committed by Joshua T Flowers
parent 76f3f8605f
commit de105648be
4 changed files with 52 additions and 7 deletions

View File

@ -93,6 +93,7 @@ export default compose(
const inboxQuery = {
page: 1,
per_page: QUERY_DEFAULTS.pageSize,
type: 'info,warning',
};
const notes = getNotes( inboxQuery );

View File

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

View File

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

View File

@ -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.
*