Add note status remote inbox notifications rule processor (https://github.com/woocommerce/woocommerce-admin/pull/5207)

* Add note status remote inbox notifications rule processor

* Notes class rename

Co-authored-by: Rebecca Scott <me@becdetat.com>
This commit is contained in:
Bec Scott 2020-10-06 07:51:59 +10:00 committed by GitHub
parent a1967f194c
commit 9906c6c1b3
4 changed files with 94 additions and 0 deletions

View File

@ -282,4 +282,23 @@ class Notes {
}
}
}
/**
* Get the status of a given note by name.
*
* @param string $note_name Name of the note.
* @return string|bool The note status.
*/
public static function get_note_status( $note_name ) {
$data_store = \WC_Data_Store::load( 'admin-note' );
$note_ids = $data_store->get_notes_with_name( $note_name );
if ( empty( $note_ids ) ) {
return false;
}
$note = self::get_note( $note_ids[0] );
return $note->get_status();
}
}

View File

@ -52,6 +52,8 @@ class GetRuleProcessor {
return new BaseLocationCountryRuleProcessor();
case 'base_location_state':
return new BaseLocationStateRuleProcessor();
case 'note_status':
return new NoteStatusRuleProcessor();
}
return new FailRuleProcessor();

View File

@ -0,0 +1,61 @@
<?php
/**
* Rule processor that compares against the status of another note. For
* example, this could be used to conditionally create a note only if another
* note has not been actioned.
*/
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Notes\Notes;
/**
* Rule processor that compares against the status of another note.
*/
class NoteStatusRuleProcessor implements RuleProcessorInterface {
/**
* Compare against the status of another note.
*
* @param object $rule The rule being processed by this rule processor.
* @param object $stored_state Stored state.
*
* @return bool The result of the operation.
*/
public function process( $rule, $stored_state ) {
$status = Notes::get_note_status( $rule->note_name );
if ( ! $status ) {
return false;
}
return ComparisonOperation::compare(
$status,
$rule->status,
$rule->operation
);
}
/**
* Validates the rule.
*
* @param object $rule The rule to validate.
*
* @return bool Pass/fail.
*/
public function validate( $rule ) {
if ( ! isset( $rule->note_name ) ) {
return false;
}
if ( ! isset( $rule->status ) ) {
return false;
}
if ( ! isset( $rule->operation ) ) {
return false;
}
return true;
}
}

View File

@ -368,4 +368,16 @@ This passes when the store is located in the specified state.
`value` and `operation` are both required.
### Note status
This passes when the status of the specified note matches the specified status.
The below example passes when the `wc-admin-mobile-app` note has not been
actioned.
```
{
"type": "note_status",
"note_name": "wc-admin-mobile-app",
"status": "actioned",
"operation": "!="
}
```