* WooCommerce Admin (Dashboard) Notes.
* The WooCommerce admin notes class gets admin notes data from storage and checks validity.
namespace Automattic\WooCommerce\Admin\Notes;
defined( 'ABSPATH' ) || exit;
class Note extends \WC_Data {
const E_WC_ADMIN_NOTE_ERROR = 'error'; // used for presenting error conditions.
const E_WC_ADMIN_NOTE_WARNING = 'warning'; // used for presenting warning conditions.
const E_WC_ADMIN_NOTE_UPDATE = 'update'; // i.e. used when a new version is available.
const E_WC_ADMIN_NOTE_INFORMATIONAL = 'info'; // used for presenting informational messages.
const E_WC_ADMIN_NOTE_MARKETING = 'marketing'; // used for adding marketing messages.
const E_WC_ADMIN_NOTE_SURVEY = 'survey'; // used for adding survey messages.
const E_WC_ADMIN_NOTE_EMAIL = 'email'; // used for adding notes that will be sent by email.
const E_WC_ADMIN_NOTE_PENDING = 'pending'; // the note is pending - hidden but not actioned.
const E_WC_ADMIN_NOTE_UNACTIONED = 'unactioned'; // the note has not yet been actioned by a user.
const E_WC_ADMIN_NOTE_ACTIONED = 'actioned'; // the note has had its action completed by a user.
const E_WC_ADMIN_NOTE_SNOOZED = 'snoozed'; // the note has been snoozed by a user.
const E_WC_ADMIN_NOTE_SENT = 'sent'; // the note has been sent by email to the user.
* This is the name of this object type.
protected $object_type = 'admin-note';
protected $cache_group = 'admin-note';
* Note constructor. Loads note data.
* @param mixed $data Note data, object, or ID.
public function __construct( $data = '' ) {
// Set default data here to allow `content_data` to be an object.
'type' => self::E_WC_ADMIN_NOTE_INFORMATIONAL,
'content_data' => new \stdClass(),
'status' => self::E_WC_ADMIN_NOTE_UNACTIONED,
'source' => 'woocommerce',
'date_created' => '0000-00-00 00:00:00',
parent::__construct( $data );
if ( $data instanceof Note ) {
$this->set_id( absint( $data->get_id() ) );
} elseif ( is_numeric( $data ) ) {
} elseif ( is_object( $data ) && ! empty( $data->note_id ) ) {
$this->set_id( $data->note_id );
unset( $data->icon ); // Icons are deprecated.
$this->set_props( (array) $data );
$this->set_object_read( true );
$this->set_object_read( true );
$this->data_store = Notes::load_data_store();
if ( $this->get_id() > 0 ) {
$this->data_store->read( $this );
* Merge changes with data and clear.
public function apply_changes() {
$this->data = array_replace_recursive( $this->data, $this->changes ); // @codingStandardsIgnoreLine
// Note actions need to be replaced wholesale.
// Merging arrays doesn't allow for deleting note actions.
if ( isset( $this->changes['actions'] ) ) {
$this->data['actions'] = $this->changes['actions'];
$this->changes = array();
|--------------------------------------------------------------------------
|--------------------------------------------------------------------------
| Methods for getting allowed types, statuses.
public static function get_deprecated_types() {
self::E_WC_ADMIN_NOTE_EMAIL,
public static function get_allowed_types() {
self::E_WC_ADMIN_NOTE_ERROR,
self::E_WC_ADMIN_NOTE_WARNING,
self::E_WC_ADMIN_NOTE_UPDATE,
self::E_WC_ADMIN_NOTE_INFORMATIONAL,
self::E_WC_ADMIN_NOTE_MARKETING,
self::E_WC_ADMIN_NOTE_SURVEY,
return apply_filters( 'woocommerce_note_types', $allowed_types );
public static function get_allowed_statuses() {
$allowed_statuses = array(
self::E_WC_ADMIN_NOTE_PENDING,
self::E_WC_ADMIN_NOTE_ACTIONED,
self::E_WC_ADMIN_NOTE_UNACTIONED,
self::E_WC_ADMIN_NOTE_SNOOZED,
self::E_WC_ADMIN_NOTE_SENT,
return apply_filters( 'woocommerce_note_statuses', $allowed_statuses );
|--------------------------------------------------------------------------
|--------------------------------------------------------------------------
| Methods for getting data from the note object.
* Returns all data for this object.
* Override \WC_Data::get_data() to avoid errantly including meta data
* from ID collisions with the posts table.
public function get_data() {
return array_merge( array( 'id' => $this->get_id() ), $this->data );
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
public function get_name( $context = 'view' ) {
return $this->get_prop( 'name', $context );
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
public function get_type( $context = 'view' ) {
return $this->get_prop( 'type', $context );
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
public function get_locale( $context = 'view' ) {
return $this->get_prop( 'locale', $context );
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
public function get_title( $context = 'view' ) {
return $this->get_prop( 'title', $context );
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
public function get_content( $context = 'view' ) {
return $this->get_prop( 'content', $context );
* Get note content data (i.e. values that would be needed for re-localization)
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
public function get_content_data( $context = 'view' ) {
return $this->get_prop( 'content_data', $context );
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
public function get_status( $context = 'view' ) {
return $this->get_prop( 'status', $context );
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
public function get_source( $context = 'view' ) {
return $this->get_prop( 'source', $context );
* Get date note was created.
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return WC_DateTime|NULL object if the date is set or null if there is no date.
public function get_date_created( $context = 'view' ) {
return $this->get_prop( 'date_created', $context );
* Get date on which user should be reminded of the note (if any).
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return WC_DateTime|NULL object if the date is set or null if there is no date.
public function get_date_reminder( $context = 'view' ) {
return $this->get_prop( 'date_reminder', $context );
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return bool Whether or not the note can be snoozed.
public function get_is_snoozable( $context = 'view' ) {
return $this->get_prop( 'is_snoozable', $context );
* Get actions on the note (if any).
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
public function get_actions( $context = 'view' ) {
return $this->get_prop( 'actions', $context );
* Get action by action name on the note.
* @param string $action_name The action name.
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return object the action.
public function get_action( $action_name, $context = 'view' ) {
$actions = $this->get_prop( 'actions', $context );
foreach ( $actions as $i => $action ) {
if ( $action->name === $action_name ) {
$matching_action =& $actions[ $i ];
* Get note layout (the old notes won't have one).
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
public function get_layout( $context = 'view' ) {
return $this->get_prop( 'layout', $context );
* Get note image (if any).
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
public function get_image( $context = 'view' ) {
return $this->get_prop( 'image', $context );
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
public function get_is_deleted( $context = 'view' ) {
return $this->get_prop( 'is_deleted', $context );
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
public function get_is_read( $context = 'view' ) {
return $this->get_prop( 'is_read', $context );
|--------------------------------------------------------------------------
|--------------------------------------------------------------------------
| Methods for setting note data. These should not update anything in the
| database itself and should only change what is stored in the class
* @param string $name Note name.
public function set_name( $name ) {
// Don't allow empty names.
$this->error( 'admin_note_invalid_data', __( 'The admin note name prop cannot be empty.', 'woocommerce' ) );
$this->set_prop( 'name', $name );
* @param string $type Note type.
public function set_type( $type ) {
$this->error( 'admin_note_invalid_data', __( 'The admin note type prop cannot be empty.', 'woocommerce' ) );
if ( in_array( $type, self::get_deprecated_types(), true ) ) {
'admin_note_invalid_data',
__( 'The admin note type prop is deprecated.', 'woocommerce' )
if ( ! in_array( $type, self::get_allowed_types(), true ) ) {
'admin_note_invalid_data',
/* translators: %s: admin note type. */
__( 'The admin note type prop (%s) is not one of the supported types.', 'woocommerce' ),
$this->set_prop( 'type', $type );
* @param string $locale Note locale.
public function set_locale( $locale ) {
if ( empty( $locale ) ) {
$this->error( 'admin_note_invalid_data', __( 'The admin note locale prop cannot be empty.', 'woocommerce' ) );
$this->set_prop( 'locale', $locale );
* @param string $title Note title.
public function set_title( $title ) {
$this->error( 'admin_note_invalid_data', __( 'The admin note title prop cannot be empty.', 'woocommerce' ) );
$this->set_prop( 'title', $title );
* Set note icon (Deprecated).
* @param string $icon Note icon.
public function set_icon( $icon ) {
wc_deprecated_function( 'set_icon', '4.3' );
* @param string $content Note content.
public function set_content( $content ) {
$content = wp_kses( $content, $allowed_html );
if ( empty( $content ) ) {
$this->error( 'admin_note_invalid_data', __( 'The admin note content prop cannot be empty.', 'woocommerce' ) );
$this->set_prop( 'content', $content );
* Set note data for potential re-localization.
* @todo Set a default empty array? https://github.com/woocommerce/woocommerce-admin/pull/1763#pullrequestreview-212442921.
* @param object $content_data Note data.
public function set_content_data( $content_data ) {
// Make sure $content_data is stdClass Object or an array.
if ( ! ( $content_data instanceof \stdClass ) ) {