mailpoet
/
vendor
/
woocommerce
/
action-scheduler
/
classes
/
data-stores
/
ActionScheduler_DBLogger.php
ActionScheduler_DBLogger.php
1 year ago
ActionScheduler_DBStore.php
1 year ago
ActionScheduler_HybridStore.php
1 year ago
ActionScheduler_wpCommentLogger.php
1 year ago
ActionScheduler_wpPostStore.php
1 year ago
ActionScheduler_wpPostStore_PostStatusRegistrar.php
4 years ago
ActionScheduler_wpPostStore_PostTypeRegistrar.php
1 year ago
ActionScheduler_wpPostStore_TaxonomyRegistrar.php
1 year ago
index.php
3 years ago
ActionScheduler_DBLogger.php
83 lines
| 1 | <?php |
| 2 | if (!defined('ABSPATH')) exit; |
| 3 | class ActionScheduler_DBLogger extends ActionScheduler_Logger { |
| 4 | public function log( $action_id, $message, ?DateTime $date = null ) { |
| 5 | if ( empty( $date ) ) { |
| 6 | $date = as_get_datetime_object(); |
| 7 | } else { |
| 8 | $date = clone $date; |
| 9 | } |
| 10 | $date_gmt = $date->format( 'Y-m-d H:i:s' ); |
| 11 | ActionScheduler_TimezoneHelper::set_local_timezone( $date ); |
| 12 | $date_local = $date->format( 'Y-m-d H:i:s' ); |
| 13 | //phpcs:ignore Generic.Commenting.DocComment.MissingShort |
| 14 | global $wpdb; |
| 15 | $wpdb->insert( |
| 16 | $wpdb->actionscheduler_logs, |
| 17 | array( |
| 18 | 'action_id' => $action_id, |
| 19 | 'message' => $message, |
| 20 | 'log_date_gmt' => $date_gmt, |
| 21 | 'log_date_local' => $date_local, |
| 22 | ), |
| 23 | array( '%d', '%s', '%s', '%s' ) |
| 24 | ); |
| 25 | return $wpdb->insert_id; |
| 26 | } |
| 27 | public function get_entry( $entry_id ) { |
| 28 | //phpcs:ignore Generic.Commenting.DocComment.MissingShort |
| 29 | global $wpdb; |
| 30 | $entry = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->actionscheduler_logs} WHERE log_id=%d", $entry_id ) ); |
| 31 | return $this->create_entry_from_db_record( $entry ); |
| 32 | } |
| 33 | private function create_entry_from_db_record( $record ) { |
| 34 | if ( empty( $record ) ) { |
| 35 | return new ActionScheduler_NullLogEntry(); |
| 36 | } |
| 37 | if ( is_null( $record->log_date_gmt ) ) { |
| 38 | $date = as_get_datetime_object( ActionScheduler_StoreSchema::DEFAULT_DATE ); |
| 39 | } else { |
| 40 | $date = as_get_datetime_object( $record->log_date_gmt ); |
| 41 | } |
| 42 | return new ActionScheduler_LogEntry( $record->action_id, $record->message, $date ); |
| 43 | } |
| 44 | public function get_logs( $action_id ) { |
| 45 | //phpcs:ignore Generic.Commenting.DocComment.MissingShort |
| 46 | global $wpdb; |
| 47 | $records = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->actionscheduler_logs} WHERE action_id=%d", $action_id ) ); |
| 48 | return array_map( array( $this, 'create_entry_from_db_record' ), $records ); |
| 49 | } |
| 50 | public function init() { |
| 51 | $table_maker = new ActionScheduler_LoggerSchema(); |
| 52 | $table_maker->init(); |
| 53 | $table_maker->register_tables(); |
| 54 | parent::init(); |
| 55 | add_action( 'action_scheduler_deleted_action', array( $this, 'clear_deleted_action_logs' ), 10, 1 ); |
| 56 | } |
| 57 | public function clear_deleted_action_logs( $action_id ) { |
| 58 | //phpcs:ignore Generic.Commenting.DocComment.MissingShort |
| 59 | global $wpdb; |
| 60 | $wpdb->delete( $wpdb->actionscheduler_logs, array( 'action_id' => $action_id ), array( '%d' ) ); |
| 61 | } |
| 62 | public function bulk_log_cancel_actions( $action_ids ) { |
| 63 | if ( empty( $action_ids ) ) { |
| 64 | return; |
| 65 | } |
| 66 | //phpcs:ignore Generic.Commenting.DocComment.MissingShort |
| 67 | global $wpdb; |
| 68 | $date = as_get_datetime_object(); |
| 69 | $date_gmt = $date->format( 'Y-m-d H:i:s' ); |
| 70 | ActionScheduler_TimezoneHelper::set_local_timezone( $date ); |
| 71 | $date_local = $date->format( 'Y-m-d H:i:s' ); |
| 72 | $message = __( 'action canceled', 'action-scheduler' ); |
| 73 | $format = '(%d, ' . $wpdb->prepare( '%s, %s, %s', $message, $date_gmt, $date_local ) . ')'; |
| 74 | $sql_query = "INSERT {$wpdb->actionscheduler_logs} (action_id, message, log_date_gmt, log_date_local) VALUES "; |
| 75 | $value_rows = array(); |
| 76 | foreach ( $action_ids as $action_id ) { |
| 77 | $value_rows[] = $wpdb->prepare( $format, $action_id ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 78 | } |
| 79 | $sql_query .= implode( ',', $value_rows ); |
| 80 | $wpdb->query( $sql_query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 81 | } |
| 82 | } |
| 83 |