2018-09-20 23:56:35 +00:00
< ? php
/**
* WC_Admin_Note_Data_Store class file .
*
* @ package WooCommerce Admin / Classes
*/
defined ( 'ABSPATH' ) || exit ;
/**
* WC Admin Note Data Store ( Custom Tables )
*/
2018-09-26 17:38:25 +00:00
class WC_Admin_Notes_Data_Store extends WC_Data_Store_WP implements WC_Object_Data_Store_Interface {
2018-09-20 23:56:35 +00:00
/**
* Method to create a new note in the database .
*
2018-09-26 23:35:01 +00:00
* @ param WC_Admin_Note $note Admin note .
2018-09-20 23:56:35 +00:00
*/
public function create ( & $note ) {
2018-09-25 00:09:39 +00:00
$date_created = current_time ( 'timestamp' , 1 );
$note -> set_date_created ( $date_created );
2018-09-20 23:56:35 +00:00
global $wpdb ;
2018-09-21 23:44:04 +00:00
$note_to_be_inserted = array (
2019-03-16 00:43:06 +00:00
'name' => $note -> get_name (),
'type' => $note -> get_type (),
'locale' => $note -> get_locale (),
'title' => $note -> get_title (),
'content' => $note -> get_content (),
'icon' => $note -> get_icon (),
'status' => $note -> get_status (),
'source' => $note -> get_source (),
2019-07-08 16:48:38 +00:00
'is_snoozable' => ( int ) $note -> get_is_snoozable (),
2018-09-20 23:56:35 +00:00
);
2018-09-21 23:44:04 +00:00
2018-10-12 20:04:23 +00:00
$note_to_be_inserted [ 'content_data' ] = wp_json_encode ( $note -> get_content_data () );
2018-09-25 00:09:39 +00:00
$note_to_be_inserted [ 'date_created' ] = gmdate ( 'Y-m-d H:i:s' , $date_created );
$note_to_be_inserted [ 'date_reminder' ] = null ;
2019-02-01 19:23:07 +00:00
$wpdb -> insert ( $wpdb -> prefix . 'wc_admin_notes' , $note_to_be_inserted );
2018-09-21 23:44:04 +00:00
$note_id = $wpdb -> insert_id ;
$note -> set_id ( $note_id );
2018-09-20 23:56:35 +00:00
$note -> save_meta_data ();
2018-09-26 17:38:25 +00:00
$this -> save_actions ( $note );
2018-09-20 23:56:35 +00:00
$note -> apply_changes ();
2019-02-01 19:10:31 +00:00
/**
* Fires when an admin note is created .
*
* @ param int $note_id Note ID .
*/
2018-09-20 23:56:35 +00:00
do_action ( 'woocommerce_new_note' , $note_id );
}
/**
* Method to read a note .
*
2018-09-26 23:35:01 +00:00
* @ param WC_Admin_Note $note Admin note .
* @ throws Exception Throws exception when invalid data is found .
2018-09-20 23:56:35 +00:00
*/
public function read ( & $note ) {
global $wpdb ;
$note -> set_defaults ();
$note_row = false ;
$note_id = $note -> get_id ();
if ( 0 !== $note_id || '0' !== $note_id ) {
$note_row = $wpdb -> get_row (
$wpdb -> prepare (
2019-03-16 00:43:06 +00:00
" SELECT name, type, locale, title, content, icon, content_data, status, source, date_created, date_reminder, is_snoozable FROM { $wpdb -> prefix } wc_admin_notes WHERE note_id = %d LIMIT 1 " ,
2018-09-20 23:56:35 +00:00
$note -> get_id ()
)
);
}
if ( 0 === $note -> get_id () || '0' === $note -> get_id () ) {
$this -> read_actions ( $note );
$note -> read_meta_data ();
$note -> set_object_read ( true );
2019-02-01 19:10:31 +00:00
/**
* Fires when an admin note is loaded .
*
* @ param int $note_id Note ID .
*/
2018-09-20 23:56:35 +00:00
do_action ( 'woocommerce_admin_note_loaded' , $note );
} elseif ( $note_row ) {
2018-09-21 23:44:04 +00:00
$note -> set_name ( $note_row -> name );
$note -> set_type ( $note_row -> type );
$note -> set_locale ( $note_row -> locale );
$note -> set_title ( $note_row -> title );
$note -> set_content ( $note_row -> content );
$note -> set_icon ( $note_row -> icon );
2018-09-25 00:09:39 +00:00
$note -> set_content_data ( json_decode ( $note_row -> content_data ) );
2018-09-21 23:44:04 +00:00
$note -> set_status ( $note_row -> status );
$note -> set_source ( $note_row -> source );
$note -> set_date_created ( $note_row -> date_created );
$note -> set_date_reminder ( $note_row -> date_reminder );
2019-03-16 00:43:06 +00:00
$note -> set_is_snoozable ( $note_row -> is_snoozable );
2018-09-20 23:56:35 +00:00
$this -> read_actions ( $note );
2018-09-21 23:44:04 +00:00
$note -> read_meta_data ();
$note -> set_object_read ( true );
2019-02-01 19:10:31 +00:00
/**
* Fires when an admin note is loaded .
*
* @ param int $note_id Note ID .
*/
2018-09-21 23:44:04 +00:00
do_action ( 'woocommerce_admin_note_loaded' , $note );
2018-09-20 23:56:35 +00:00
} else {
2019-03-13 17:14:02 +00:00
throw new Exception ( __ ( 'Invalid data store for admin note.' , 'woocommerce-admin' ) );
2018-09-20 23:56:35 +00:00
}
}
/**
* Updates a note in the database .
*
2018-09-26 23:35:01 +00:00
* @ param WC_Admin_Note $note Admin note .
2018-09-20 23:56:35 +00:00
*/
public function update ( & $note ) {
global $wpdb ;
2018-09-25 00:09:39 +00:00
2018-09-27 17:48:25 +00:00
if ( $note -> get_id () ) {
2018-10-26 22:07:59 +00:00
$date_created = $note -> get_date_created ();
$date_created_timestamp = $date_created -> getTimestamp ();
$date_created_to_db = gmdate ( 'Y-m-d H:i:s' , $date_created_timestamp );
$date_reminder = $note -> get_date_reminder ();
if ( is_null ( $date_reminder ) ) {
$date_reminder_to_db = null ;
} else {
$date_reminder_timestamp = $date_reminder -> getTimestamp ();
$date_reminder_to_db = gmdate ( 'Y-m-d H:i:s' , $date_reminder_timestamp );
}
2018-09-20 23:56:35 +00:00
$wpdb -> update (
2019-02-01 19:23:07 +00:00
$wpdb -> prefix . 'wc_admin_notes' ,
2018-10-19 22:05:40 +00:00
array (
2018-09-21 23:44:04 +00:00
'name' => $note -> get_name (),
'type' => $note -> get_type (),
'locale' => $note -> get_locale (),
'title' => $note -> get_title (),
'content' => $note -> get_content (),
'icon' => $note -> get_icon (),
2018-10-12 20:04:23 +00:00
'content_data' => wp_json_encode ( $note -> get_content_data () ),
2018-09-20 23:56:35 +00:00
'status' => $note -> get_status (),
'source' => $note -> get_source (),
2018-10-26 22:07:59 +00:00
'date_created' => $date_created_to_db ,
'date_reminder' => $date_reminder_to_db ,
2019-03-16 00:43:06 +00:00
'is_snoozable' => $note -> get_is_snoozable (),
2018-10-19 22:05:40 +00:00
),
array ( 'note_id' => $note -> get_id () )
2018-09-20 23:56:35 +00:00
);
}
$note -> save_meta_data ();
2018-09-26 17:38:25 +00:00
$this -> save_actions ( $note );
2018-09-20 23:56:35 +00:00
$note -> apply_changes ();
2019-02-01 19:10:31 +00:00
/**
* Fires when an admin note is updated .
*
* @ param int $note_id Note ID .
*/
2018-09-20 23:56:35 +00:00
do_action ( 'woocommerce_update_note' , $note -> get_id () );
}
/**
* Deletes a note from the database .
*
2018-09-26 23:35:01 +00:00
* @ param WC_Admin_Note $note Admin note .
* @ param array $args Array of args to pass to the delete method ( not used ) .
2018-09-20 23:56:35 +00:00
*/
public function delete ( & $note , $args = array () ) {
$note_id = $note -> get_id ();
2018-10-18 22:52:55 +00:00
if ( $note_id ) {
2018-09-20 23:56:35 +00:00
global $wpdb ;
2019-02-01 19:23:07 +00:00
$wpdb -> delete ( $wpdb -> prefix . 'wc_admin_notes' , array ( 'note_id' => $note_id ) );
$wpdb -> delete ( $wpdb -> prefix . 'wc_admin_note_actions' , array ( 'note_id' => $note_id ) );
2018-09-20 23:56:35 +00:00
$note -> set_id ( null );
}
2019-02-01 19:08:43 +00:00
/**
* Fires when an admin note is updated .
*
* @ param int $note_id Note ID .
*/
do_action ( 'woocommerce_delete_note' , $note_id );
2018-09-20 23:56:35 +00:00
}
/**
* Read actions from the database .
*
2018-09-26 23:35:01 +00:00
* @ param WC_Admin_Note $note Admin note .
2018-09-20 23:56:35 +00:00
*/
private function read_actions ( & $note ) {
global $wpdb ;
2019-05-24 19:20:13 +00:00
$db_actions = $wpdb -> get_results (
2018-09-20 23:56:35 +00:00
$wpdb -> prepare (
2019-05-24 19:20:13 +00:00
" SELECT action_id, name, label, query, status, is_primary
FROM { $wpdb -> prefix } wc_admin_note_actions
WHERE note_id = % d " ,
2018-09-20 23:56:35 +00:00
$note -> get_id ()
)
);
2019-05-24 19:20:13 +00:00
$note_actions = array ();
if ( $db_actions ) {
foreach ( $db_actions as $action ) {
$note_actions [] = ( object ) array (
'id' => ( int ) $action -> action_id ,
'name' => $action -> name ,
'label' => $action -> label ,
'query' => $action -> query ,
'status' => $action -> status ,
'primary' => ( bool ) $action -> is_primary ,
);
2018-09-20 23:56:35 +00:00
}
}
2019-05-24 19:20:13 +00:00
$note -> set_actions ( $note_actions );
2018-09-20 23:56:35 +00:00
}
/**
* Save actions to the database .
* This function clears old actions , then re - inserts new if any changes are found .
*
* @ param WC_Admin_Note $note Note object .
*
* @ return bool | void
*/
private function save_actions ( & $note ) {
2019-05-24 19:20:13 +00:00
global $wpdb ;
2018-09-20 23:56:35 +00:00
$changed_props = array_keys ( $note -> get_changes () );
2019-05-24 19:20:13 +00:00
2018-09-26 17:38:25 +00:00
if ( ! in_array ( 'actions' , $changed_props , true ) ) {
2018-09-20 23:56:35 +00:00
return false ;
}
foreach ( $note -> get_actions ( 'edit' ) as $action ) {
2019-05-24 19:20:13 +00:00
$action_data = array (
'note_id' => $note -> get_id (),
'name' => $action -> name ,
'label' => $action -> label ,
'query' => $action -> query ,
'status' => $action -> status ,
'is_primary' => $action -> primary ,
);
$data_format = array (
'%d' ,
'%s' ,
'%s' ,
'%s' ,
'%s' ,
'%d' ,
);
if ( ! empty ( $action -> id ) ) {
$action_data [ 'action_id' ] = $action -> id ;
$data_format [] = '%d' ;
}
$wpdb -> replace (
2019-02-01 19:23:07 +00:00
$wpdb -> prefix . 'wc_admin_note_actions' ,
2019-05-24 19:20:13 +00:00
$action_data ,
$data_format
2018-09-20 23:56:35 +00:00
);
}
}
2018-09-21 23:44:04 +00:00
/**
* Return an ordered list of notes .
*
2018-10-11 19:25:09 +00:00
* @ param array $args Query arguments .
2018-09-21 23:44:04 +00:00
* @ return array An array of objects containing a note id .
*/
2018-10-11 19:25:09 +00:00
public function get_notes ( $args = array () ) {
2018-09-21 23:44:04 +00:00
global $wpdb ;
2018-10-11 19:25:09 +00:00
2019-04-01 02:53:34 +00:00
$defaults = array (
'per_page' => get_option ( 'posts_per_page' ),
'page' => 1 ,
'order' => 'DESC' ,
'orderby' => 'date_created' ,
);
$args = wp_parse_args ( $args , $defaults );
2018-10-11 19:25:09 +00:00
2019-04-01 02:53:34 +00:00
$offset = $args [ 'per_page' ] * ( $args [ 'page' ] - 1 );
2019-03-12 13:13:20 +00:00
$where_clauses = $this -> get_notes_where_clauses ( $args );
2018-12-11 01:00:57 +00:00
2019-03-12 13:13:20 +00:00
$query = $wpdb -> prepare (
2019-04-01 02:53:34 +00:00
" SELECT note_id, title, content FROM { $wpdb -> prefix } wc_admin_notes WHERE 1=1 { $where_clauses } ORDER BY { $args [ 'orderby' ] } { $args [ 'order' ] } LIMIT %d, %d " ,
2019-03-12 13:13:20 +00:00
$offset ,
2019-04-01 02:53:34 +00:00
$args [ 'per_page' ]
2019-03-12 13:13:20 +00:00
); // WPCS: unprepared SQL ok.
2018-12-11 01:00:57 +00:00
return $wpdb -> get_results ( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
2018-09-21 23:44:04 +00:00
}
2018-10-11 19:25:09 +00:00
/**
* Return a count 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 array An array of objects containing a note id .
*/
2019-04-01 02:53:34 +00:00
public function get_notes_count ( $type = array (), $status = array () ) {
2018-10-11 19:25:09 +00:00
global $wpdb ;
2019-03-08 15:17:24 +00:00
2019-03-12 13:13:20 +00:00
$where_clauses = $this -> get_notes_where_clauses (
array (
'type' => $type ,
'status' => $status ,
)
);
if ( ! empty ( $where_clauses ) ) {
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
return $wpdb -> get_var ( " SELECT COUNT(*) FROM { $wpdb -> prefix } wc_admin_notes WHERE 1=1 { $where_clauses } " );
}
return $wpdb -> get_var ( " SELECT COUNT(*) FROM { $wpdb -> prefix } wc_admin_notes " );
}
/**
* Return where clauses for getting notes by status and type . For use in both the count and listing queries .
*
* @ param array $args Array of args to pass .
* @ return string Where clauses for the query .
*/
public function get_notes_where_clauses ( $args = array () ) {
2019-03-08 15:17:24 +00:00
$allowed_types = WC_Admin_Note :: get_allowed_types ();
$where_type_array = array ();
2019-03-12 13:13:20 +00:00
if ( isset ( $args [ 'type' ] ) ) {
2019-04-01 02:53:34 +00:00
foreach ( $args [ 'type' ] as $args_type ) {
2019-03-08 15:17:24 +00:00
$args_type = trim ( $args_type );
if ( in_array ( $args_type , $allowed_types , true ) ) {
$where_type_array [] = " ' " . esc_sql ( $args_type ) . " ' " ;
}
}
}
2019-03-12 13:13:20 +00:00
$allowed_statuses = WC_Admin_Note :: get_allowed_statuses ();
$where_status_array = array ();
if ( isset ( $args [ 'status' ] ) ) {
2019-04-01 02:53:34 +00:00
foreach ( $args [ 'status' ] as $args_status ) {
2019-03-12 13:13:20 +00:00
$args_status = trim ( $args_status );
if ( in_array ( $args_status , $allowed_statuses , true ) ) {
$where_status_array [] = " ' " . esc_sql ( $args_status ) . " ' " ;
}
}
}
$escaped_where_types = implode ( ',' , $where_type_array );
$escaped_status_types = implode ( ',' , $where_status_array );
$where_clauses = '' ;
2019-03-08 15:17:24 +00:00
if ( ! empty ( $escaped_where_types ) ) {
2019-03-12 13:13:20 +00:00
$where_clauses .= " AND type IN ( $escaped_where_types ) " ;
2019-03-08 15:17:24 +00:00
}
2019-03-12 13:13:20 +00:00
if ( ! empty ( $escaped_status_types ) ) {
$where_clauses .= " AND status IN ( $escaped_status_types ) " ;
}
return $where_clauses ;
2018-10-11 19:25:09 +00:00
}
2018-10-18 22:52:55 +00:00
/**
* Find all the notes with a given name .
*
* @ param string $name Name to search for .
* @ return array An array of matching note ids .
*/
public function get_notes_with_name ( $name ) {
global $wpdb ;
return $wpdb -> get_col (
$wpdb -> prepare (
2019-02-01 19:23:07 +00:00
" SELECT note_id FROM { $wpdb -> prefix } wc_admin_notes WHERE name = %s ORDER BY note_id ASC " ,
2018-10-18 22:52:55 +00:00
$name
)
);
}
2018-09-20 23:56:35 +00:00
}