2018-09-20 23:56:35 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2018-09-21 23:44:04 +00:00
|
|
|
* Handles storage and retrieval of admin notes
|
2018-09-20 23:56:35 +00:00
|
|
|
*
|
2018-09-26 23:35:01 +00:00
|
|
|
* @package WooCommerce Admin/Classes
|
2018-09-20 23:56:35 +00:00
|
|
|
*/
|
|
|
|
|
2018-09-21 23:44:04 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Admin Notes class.
|
|
|
|
*/
|
|
|
|
class WC_Admin_Notes {
|
2019-03-18 23:02:02 +00:00
|
|
|
/**
|
|
|
|
* Hook used for recurring "unsnooze" action.
|
|
|
|
*/
|
|
|
|
const UNSNOOZE_HOOK = 'wc_admin_unsnooze_admin_notes';
|
|
|
|
|
2019-05-13 02:30:07 +00:00
|
|
|
/**
|
|
|
|
* Action scheduler group.
|
|
|
|
*/
|
|
|
|
const QUEUE_GROUP = 'wc-admin-notes';
|
|
|
|
|
2019-06-12 16:16:08 +00:00
|
|
|
/**
|
|
|
|
* Queue instance.
|
|
|
|
*
|
|
|
|
* @var WC_Queue_Interface
|
|
|
|
*/
|
|
|
|
protected static $queue = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get queue instance.
|
|
|
|
*
|
|
|
|
* @return WC_Queue_Interface
|
|
|
|
*/
|
|
|
|
public static function queue() {
|
|
|
|
if ( is_null( self::$queue ) ) {
|
|
|
|
self::$queue = WC()->queue();
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$queue;
|
|
|
|
}
|
|
|
|
|
2019-03-18 23:02:02 +00:00
|
|
|
/**
|
2019-04-01 14:31:28 +00:00
|
|
|
* Hook appropriate actions.
|
2019-03-18 23:02:02 +00:00
|
|
|
*/
|
|
|
|
public static function init() {
|
2019-04-01 14:31:28 +00:00
|
|
|
add_action( 'admin_init', array( __CLASS__, 'schedule_unsnooze_notes' ) );
|
2019-03-18 23:02:02 +00:00
|
|
|
add_action( self::UNSNOOZE_HOOK, array( __CLASS__, 'unsnooze_notes' ) );
|
|
|
|
}
|
|
|
|
|
2018-09-21 23:44:04 +00:00
|
|
|
/**
|
|
|
|
* Get notes from the database.
|
|
|
|
*
|
|
|
|
* @param string $context Getting notes for what context. Valid values: view, edit.
|
2018-10-11 19:25:09 +00:00
|
|
|
* @param array $args Arguments to pass to the query( e.g. per_page and page).
|
2018-09-21 23:44:04 +00:00
|
|
|
* @return array Array of arrays.
|
|
|
|
*/
|
2018-10-11 19:40:19 +00:00
|
|
|
public static function get_notes( $context = 'edit', $args = array() ) {
|
2018-09-24 19:19:57 +00:00
|
|
|
$data_store = WC_Data_Store::load( 'admin-note' );
|
2018-10-11 19:25:09 +00:00
|
|
|
$raw_notes = $data_store->get_notes( $args );
|
2018-09-21 23:44:04 +00:00
|
|
|
$notes = array();
|
|
|
|
foreach ( (array) $raw_notes as $raw_note ) {
|
|
|
|
$note = new WC_Admin_Note( $raw_note );
|
|
|
|
$note_id = $note->get_id();
|
|
|
|
$notes[ $note_id ] = $note->get_data();
|
|
|
|
$notes[ $note_id ]['name'] = $note->get_name( $context );
|
|
|
|
$notes[ $note_id ]['type'] = $note->get_type( $context );
|
|
|
|
$notes[ $note_id ]['locale'] = $note->get_locale( $context );
|
|
|
|
$notes[ $note_id ]['title'] = $note->get_title( $context );
|
|
|
|
$notes[ $note_id ]['content'] = $note->get_content( $context );
|
|
|
|
$notes[ $note_id ]['icon'] = $note->get_icon( $context );
|
|
|
|
$notes[ $note_id ]['content_data'] = $note->get_content_data( $context );
|
|
|
|
$notes[ $note_id ]['status'] = $note->get_status( $context );
|
|
|
|
$notes[ $note_id ]['source'] = $note->get_source( $context );
|
|
|
|
$notes[ $note_id ]['date_created'] = $note->get_date_created( $context );
|
|
|
|
$notes[ $note_id ]['date_reminder'] = $note->get_date_reminder( $context );
|
|
|
|
$notes[ $note_id ]['actions'] = $note->get_actions( $context );
|
2018-09-20 23:56:35 +00:00
|
|
|
}
|
2018-09-21 23:44:04 +00:00
|
|
|
return $notes;
|
2018-09-20 23:56:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-09-21 23:44:04 +00:00
|
|
|
* Get admin note using it's ID
|
2018-09-20 23:56:35 +00:00
|
|
|
*
|
2018-09-21 23:44:04 +00:00
|
|
|
* @param int $note_id Note ID.
|
|
|
|
* @return WC_Admin_Note|bool
|
2018-09-20 23:56:35 +00:00
|
|
|
*/
|
2018-09-21 23:44:04 +00:00
|
|
|
public static function get_note( $note_id ) {
|
|
|
|
if ( false !== $note_id ) {
|
|
|
|
try {
|
|
|
|
return new WC_Admin_Note( $note_id );
|
|
|
|
} catch ( Exception $e ) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-20 23:56:35 +00:00
|
|
|
}
|
2018-09-21 23:44:04 +00:00
|
|
|
return false;
|
2018-09-20 23:56:35 +00:00
|
|
|
}
|
2018-10-11 19:25:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the total number of notes
|
|
|
|
*
|
2019-03-08 15:17:24 +00:00
|
|
|
* @param string $type Comma separated list of note types.
|
2019-03-12 13:13:20 +00:00
|
|
|
* @param string $status Comma separated list of statuses.
|
2018-10-11 19:25:09 +00:00
|
|
|
* @return int
|
|
|
|
*/
|
2019-04-01 02:53:34 +00:00
|
|
|
public static function get_notes_count( $type = array(), $status = array() ) {
|
2018-10-11 19:25:09 +00:00
|
|
|
$data_store = WC_Data_Store::load( 'admin-note' );
|
2019-03-12 13:13:20 +00:00
|
|
|
return $data_store->get_notes_count( $type, $status );
|
2018-10-11 19:25:09 +00:00
|
|
|
}
|
2018-10-18 22:52:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes admin notes with a given name.
|
|
|
|
*
|
|
|
|
* @param string $name Name to search for.
|
|
|
|
*/
|
|
|
|
public static function delete_notes_with_name( $name ) {
|
|
|
|
$data_store = WC_Data_Store::load( 'admin-note' );
|
|
|
|
$note_ids = $data_store->get_notes_with_name( $name );
|
|
|
|
foreach ( (array) $note_ids as $note_id ) {
|
|
|
|
$note = new WC_Admin_Note( $note_id );
|
|
|
|
$note->delete();
|
|
|
|
}
|
|
|
|
}
|
2019-03-18 23:02:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear note snooze status if the reminder date has been reached.
|
|
|
|
*/
|
|
|
|
public static function unsnooze_notes() {
|
|
|
|
$data_store = WC_Data_Store::load( 'admin-note' );
|
|
|
|
$raw_notes = $data_store->get_notes(
|
|
|
|
array(
|
2019-04-01 02:53:34 +00:00
|
|
|
'status' => array( WC_Admin_Note::E_WC_ADMIN_NOTE_SNOOZED ),
|
2019-03-18 23:02:02 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$now = new DateTime();
|
|
|
|
|
|
|
|
foreach ( $raw_notes as $raw_note ) {
|
|
|
|
$note = new WC_Admin_Note( $raw_note );
|
|
|
|
$date_reminder = $note->get_date_reminder( 'edit' );
|
|
|
|
|
|
|
|
if ( $date_reminder < $now ) {
|
|
|
|
$note->set_status( WC_Admin_Note::E_WC_ADMIN_NOTE_UNACTIONED );
|
|
|
|
$note->set_date_reminder( null );
|
|
|
|
$note->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-01 14:31:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Schedule unsnooze notes event.
|
|
|
|
*/
|
|
|
|
public static function schedule_unsnooze_notes() {
|
|
|
|
|
2019-07-05 02:32:24 +00:00
|
|
|
$snooze_checked_transient_key = sprintf( '%s_checked', self::UNSNOOZE_HOOK );
|
|
|
|
|
|
|
|
if ( 'yes' !== get_transient( $snooze_checked_transient_key ) ) {
|
|
|
|
$queue = WC()->queue();
|
|
|
|
$next = $queue->get_next( self::UNSNOOZE_HOOK );
|
|
|
|
|
|
|
|
if ( ! $next ) {
|
|
|
|
$queue->schedule_recurring( time(), HOUR_IN_SECONDS, self::UNSNOOZE_HOOK, array(), self::QUEUE_GROUP );
|
|
|
|
}
|
|
|
|
set_transient( $snooze_checked_transient_key, 'yes', HOUR_IN_SECONDS );
|
2019-05-13 02:30:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears all queued actions.
|
|
|
|
*/
|
|
|
|
public static function clear_queued_actions() {
|
|
|
|
$store = ActionScheduler::store();
|
|
|
|
|
|
|
|
if ( is_a( $store, 'WC_Admin_ActionScheduler_WPPostStore' ) ) {
|
|
|
|
// If we're using our data store, call our bespoke deletion method.
|
|
|
|
$action_types = array( self::UNSNOOZE_HOOK );
|
|
|
|
$store->clear_pending_wcadmin_actions( $action_types );
|
|
|
|
} else {
|
|
|
|
self::queue()->cancel_all( null, array(), self::QUEUE_GROUP );
|
2019-04-01 14:31:28 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-20 23:56:35 +00:00
|
|
|
}
|
2019-03-18 23:02:02 +00:00
|
|
|
|
|
|
|
WC_Admin_Notes::init();
|