Add support for “primary” admin notice actions.

This commit is contained in:
Jeff Stieler 2019-05-21 13:01:55 -06:00
parent a382fd6143
commit db5a1b7348
5 changed files with 29 additions and 21 deletions

View File

@ -73,6 +73,7 @@ class StoreAlerts extends Component {
<Button <Button
key={ action.name } key={ action.name }
isDefault isDefault
isPrimary={ action.primary }
href={ action.url } href={ action.url }
onClick={ '' === action.status ? noop : markStatus } onClick={ '' === action.status ? noop : markStatus }
> >

View File

@ -170,6 +170,7 @@ class WC_Admin_Install {
label varchar(255) NOT NULL, label varchar(255) NOT NULL,
query longtext NOT NULL, query longtext NOT NULL,
status varchar(255) NOT NULL, status varchar(255) NOT NULL,
is_primary boolean DEFAULT 0 NOT NULL,
PRIMARY KEY (action_id), PRIMARY KEY (action_id),
KEY note_id (note_id) KEY note_id (note_id)
) $collate; ) $collate;

View File

@ -200,13 +200,13 @@ class WC_Admin_Notes_Data_Store extends WC_Data_Store_WP implements WC_Object_Da
$actions = $wpdb->get_results( $actions = $wpdb->get_results(
$wpdb->prepare( $wpdb->prepare(
"SELECT name, label, query, status FROM {$wpdb->prefix}wc_admin_note_actions WHERE note_id = %d", "SELECT name, label, query, status, is_primary FROM {$wpdb->prefix}wc_admin_note_actions WHERE note_id = %d",
$note->get_id() $note->get_id()
) )
); );
if ( $actions ) { if ( $actions ) {
foreach ( $actions as $action ) { foreach ( $actions as $action ) {
$note->add_action( $action->name, $action->label, $action->query, $action->status ); $note->add_action( $action->name, $action->label, $action->query, $action->status, $action->is_primary );
} }
} }
} }
@ -231,11 +231,12 @@ class WC_Admin_Notes_Data_Store extends WC_Data_Store_WP implements WC_Object_Da
$wpdb->insert( $wpdb->insert(
$wpdb->prefix . 'wc_admin_note_actions', $wpdb->prefix . 'wc_admin_note_actions',
array( array(
'note_id' => $note->get_id(), 'note_id' => $note->get_id(),
'name' => $action->name, 'name' => $action->name,
'label' => $action->label, 'label' => $action->label,
'query' => $action->query, 'query' => $action->query,
'status' => $action->status, 'status' => $action->status,
'is_primary' => $action->primary,
) )
); );
} }

View File

@ -466,16 +466,18 @@ class WC_Admin_Note extends WC_Data {
/** /**
* Add an action to the note * Add an action to the note
* *
* @param string $name Label name (not presented to user). * @param string $name Label name (not presented to user).
* @param string $label Note label (e.g. presented as button label). * @param string $label Note label (e.g. presented as button label).
* @param string $query Note query (for redirect). * @param string $query Note query (for redirect).
* @param string $status The status to set for the action should on click. * @param string $status The status to set for the action should on click.
* @param boolean $primary Whether or not this is the primary action. Defaults to false.
*/ */
public function add_action( $name, $label, $query, $status = '' ) { public function add_action( $name, $label, $query, $status = '', $primary = false ) {
$name = wc_clean( $name ); $name = wc_clean( $name );
$label = wc_clean( $label ); $label = wc_clean( $label );
$query = wc_clean( $query ); $query = wc_clean( $query );
$status = wc_clean( $status ); $status = wc_clean( $status );
$primary = (bool) $primary;
if ( empty( $name ) ) { if ( empty( $name ) ) {
$this->error( 'admin_note_invalid_data', __( 'The admin note action name prop cannot be empty.', 'woocommerce-admin' ) ); $this->error( 'admin_note_invalid_data', __( 'The admin note action name prop cannot be empty.', 'woocommerce-admin' ) );
@ -485,15 +487,17 @@ class WC_Admin_Note extends WC_Data {
$this->error( 'admin_note_invalid_data', __( 'The admin note action label prop cannot be empty.', 'woocommerce-admin' ) ); $this->error( 'admin_note_invalid_data', __( 'The admin note action label prop cannot be empty.', 'woocommerce-admin' ) );
} }
// @todo - maybe allow empty queries? for dismissals that don't need navigation.
if ( empty( $query ) ) { if ( empty( $query ) ) {
$this->error( 'admin_note_invalid_data', __( 'The admin note action query prop cannot be empty.', 'woocommerce-admin' ) ); $this->error( 'admin_note_invalid_data', __( 'The admin note action query prop cannot be empty.', 'woocommerce-admin' ) );
} }
$action = array( $action = array(
'name' => $name, 'name' => $name,
'label' => $label, 'label' => $label,
'query' => $query, 'query' => $query,
'status' => $status, 'status' => $status,
'primary' => $primary,
); );
$note_actions = $this->get_prop( 'actions', 'edit' ); $note_actions = $this->get_prop( 'actions', 'edit' );

View File

@ -49,7 +49,8 @@ class WC_Admin_Notes_Historical_Data {
'get-started', 'get-started',
__( 'Get Started', 'woocommerce-admin' ), __( 'Get Started', 'woocommerce-admin' ),
'?page=wc-admin#/analytics/settings', '?page=wc-admin#/analytics/settings',
'actioned' 'actioned',
true
); );
$note->save(); $note->save();