wp-mail-smtp
/
vendor
/
woocommerce
/
action-scheduler
/
classes
/
data-stores
/
ActionScheduler_DBLogger.php
ActionScheduler_DBLogger.php
6 years ago
ActionScheduler_DBStore.php
6 years ago
ActionScheduler_HybridStore.php
6 years ago
ActionScheduler_wpCommentLogger.php
6 years ago
ActionScheduler_wpPostStore.php
6 years ago
ActionScheduler_wpPostStore_PostStatusRegistrar.php
6 years ago
ActionScheduler_wpPostStore_PostTypeRegistrar.php
6 years ago
ActionScheduler_wpPostStore_TaxonomyRegistrar.php
6 years ago
ActionScheduler_DBLogger.php
147 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class ActionScheduler_DBLogger |
| 5 | * |
| 6 | * Action logs data table data store. |
| 7 | * |
| 8 | * @since 3.0.0 |
| 9 | */ |
| 10 | class ActionScheduler_DBLogger extends ActionScheduler_Logger { |
| 11 | |
| 12 | /** |
| 13 | * Add a record to an action log. |
| 14 | * |
| 15 | * @param int $action_id Action ID. |
| 16 | * @param string $message Message to be saved in the log entry. |
| 17 | * @param DateTime $date Timestamp of the log entry. |
| 18 | * |
| 19 | * @return int The log entry ID. |
| 20 | */ |
| 21 | public function log( $action_id, $message, DateTime $date = null ) { |
| 22 | if ( empty( $date ) ) { |
| 23 | $date = as_get_datetime_object(); |
| 24 | } else { |
| 25 | $date = clone $date; |
| 26 | } |
| 27 | |
| 28 | $date_gmt = $date->format( 'Y-m-d H:i:s' ); |
| 29 | ActionScheduler_TimezoneHelper::set_local_timezone( $date ); |
| 30 | $date_local = $date->format( 'Y-m-d H:i:s' ); |
| 31 | |
| 32 | /** @var \wpdb $wpdb */ |
| 33 | global $wpdb; |
| 34 | $wpdb->insert( $wpdb->actionscheduler_logs, [ |
| 35 | 'action_id' => $action_id, |
| 36 | 'message' => $message, |
| 37 | 'log_date_gmt' => $date_gmt, |
| 38 | 'log_date_local' => $date_local, |
| 39 | ], [ '%d', '%s', '%s', '%s' ] ); |
| 40 | |
| 41 | return $wpdb->insert_id; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Retrieve an action log entry. |
| 46 | * |
| 47 | * @param int $entry_id Log entry ID. |
| 48 | * |
| 49 | * @return ActionScheduler_LogEntry |
| 50 | */ |
| 51 | public function get_entry( $entry_id ) { |
| 52 | /** @var \wpdb $wpdb */ |
| 53 | global $wpdb; |
| 54 | $entry = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->actionscheduler_logs} WHERE log_id=%d", $entry_id ) ); |
| 55 | |
| 56 | return $this->create_entry_from_db_record( $entry ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Create an action log entry from a database record. |
| 61 | * |
| 62 | * @param object $record Log entry database record object. |
| 63 | * |
| 64 | * @return ActionScheduler_LogEntry |
| 65 | */ |
| 66 | private function create_entry_from_db_record( $record ) { |
| 67 | if ( empty( $record ) ) { |
| 68 | return new ActionScheduler_NullLogEntry(); |
| 69 | } |
| 70 | |
| 71 | $date = as_get_datetime_object( $record->log_date_gmt ); |
| 72 | |
| 73 | return new ActionScheduler_LogEntry( $record->action_id, $record->message, $date ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Retrieve the an action's log entries from the database. |
| 78 | * |
| 79 | * @param int $action_id Action ID. |
| 80 | * |
| 81 | * @return ActionScheduler_LogEntry[] |
| 82 | */ |
| 83 | public function get_logs( $action_id ) { |
| 84 | /** @var \wpdb $wpdb */ |
| 85 | global $wpdb; |
| 86 | |
| 87 | $records = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->actionscheduler_logs} WHERE action_id=%d", $action_id ) ); |
| 88 | |
| 89 | return array_map( [ $this, 'create_entry_from_db_record' ], $records ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Initialize the data store. |
| 94 | * |
| 95 | * @codeCoverageIgnore |
| 96 | */ |
| 97 | public function init() { |
| 98 | |
| 99 | $table_maker = new ActionScheduler_LoggerSchema(); |
| 100 | $table_maker->register_tables(); |
| 101 | |
| 102 | parent::init(); |
| 103 | |
| 104 | add_action( 'action_scheduler_deleted_action', [ $this, 'clear_deleted_action_logs' ], 10, 1 ); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Delete the action logs for an action. |
| 109 | * |
| 110 | * @param int $action_id Action ID. |
| 111 | */ |
| 112 | public function clear_deleted_action_logs( $action_id ) { |
| 113 | /** @var \wpdb $wpdb */ |
| 114 | global $wpdb; |
| 115 | $wpdb->delete( $wpdb->actionscheduler_logs, [ 'action_id' => $action_id, ], [ '%d' ] ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Bulk add cancel action log entries. |
| 120 | * |
| 121 | * @param array $action_ids List of action ID. |
| 122 | */ |
| 123 | public function bulk_log_cancel_actions( $action_ids ) { |
| 124 | if ( empty( $action_ids ) ) { |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | /** @var \wpdb $wpdb */ |
| 129 | global $wpdb; |
| 130 | $date = as_get_datetime_object(); |
| 131 | $date_gmt = $date->format( 'Y-m-d H:i:s' ); |
| 132 | ActionScheduler_TimezoneHelper::set_local_timezone( $date ); |
| 133 | $date_local = $date->format( 'Y-m-d H:i:s' ); |
| 134 | $message = __( 'action canceled', 'action-scheduler' ); |
| 135 | $format = '(%d, ' . $wpdb->prepare( '%s, %s, %s', $message, $date_gmt, $date_local ) . ')'; |
| 136 | $sql_query = "INSERT {$wpdb->actionscheduler_logs} (action_id, message, log_date_gmt, log_date_local) VALUES "; |
| 137 | $value_rows = []; |
| 138 | |
| 139 | foreach ( $action_ids as $action_id ) { |
| 140 | $value_rows[] = $wpdb->prepare( $format, $action_id ); |
| 141 | } |
| 142 | $sql_query .= implode( ',', $value_rows ); |
| 143 | |
| 144 | $wpdb->query( $sql_query ); |
| 145 | } |
| 146 | } |
| 147 |