| 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 */ //phpcs:ignore Generic.Commenting.DocComment.MissingShort |
| 33 |
global $wpdb; |
| 34 |
$wpdb->insert( |
| 35 |
$wpdb->actionscheduler_logs, |
| 36 |
array( |
| 37 |
'action_id' => $action_id, |
| 38 |
'message' => $message, |
| 39 |
'log_date_gmt' => $date_gmt, |
| 40 |
'log_date_local' => $date_local, |
| 41 |
), |
| 42 |
array( '%d', '%s', '%s', '%s' ) |
| 43 |
); |
| 44 |
|
| 45 |
return $wpdb->insert_id; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Retrieve an action log entry. |
| 50 |
* |
| 51 |
* @param int $entry_id Log entry ID. |
| 52 |
* |
| 53 |
* @return ActionScheduler_LogEntry |
| 54 |
*/ |
| 55 |
public function get_entry( $entry_id ) { |
| 56 |
/** @var \wpdb $wpdb */ //phpcs:ignore Generic.Commenting.DocComment.MissingShort |
| 57 |
global $wpdb; |
| 58 |
$entry = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->actionscheduler_logs} WHERE log_id=%d", $entry_id ) ); |
| 59 |
|
| 60 |
return $this->create_entry_from_db_record( $entry ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Create an action log entry from a database record. |
| 65 |
* |
| 66 |
* @param object $record Log entry database record object. |
| 67 |
* |
| 68 |
* @return ActionScheduler_LogEntry |
| 69 |
*/ |
| 70 |
private function create_entry_from_db_record( $record ) { |
| 71 |
if ( empty( $record ) ) { |
| 72 |
return new ActionScheduler_NullLogEntry(); |
| 73 |
} |
| 74 |
|
| 75 |
if ( is_null( $record->log_date_gmt ) ) { |
| 76 |
$date = as_get_datetime_object( ActionScheduler_StoreSchema::DEFAULT_DATE ); |
| 77 |
} else { |
| 78 |
$date = as_get_datetime_object( $record->log_date_gmt ); |
| 79 |
} |
| 80 |
|
| 81 |
return new ActionScheduler_LogEntry( $record->action_id, $record->message, $date ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Retrieve the an action's log entries from the database. |
| 86 |
* |
| 87 |
* @param int $action_id Action ID. |
| 88 |
* |
| 89 |
* @return ActionScheduler_LogEntry[] |
| 90 |
*/ |
| 91 |
public function get_logs( $action_id ) { |
| 92 |
/** @var \wpdb $wpdb */ //phpcs:ignore Generic.Commenting.DocComment.MissingShort |
| 93 |
global $wpdb; |
| 94 |
|
| 95 |
$records = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->actionscheduler_logs} WHERE action_id=%d", $action_id ) ); |
| 96 |
|
| 97 |
return array_map( array( $this, 'create_entry_from_db_record' ), $records ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Initialize the data store. |
| 102 |
* |
| 103 |
* @codeCoverageIgnore |
| 104 |
*/ |
| 105 |
public function init() { |
| 106 |
$table_maker = new ActionScheduler_LoggerSchema(); |
| 107 |
$table_maker->init(); |
| 108 |
$table_maker->register_tables(); |
| 109 |
|
| 110 |
parent::init(); |
| 111 |
|
| 112 |
add_action( 'action_scheduler_deleted_action', array( $this, 'clear_deleted_action_logs' ), 10, 1 ); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Delete the action logs for an action. |
| 117 |
* |
| 118 |
* @param int $action_id Action ID. |
| 119 |
*/ |
| 120 |
public function clear_deleted_action_logs( $action_id ) { |
| 121 |
/** @var \wpdb $wpdb */ //phpcs:ignore Generic.Commenting.DocComment.MissingShort |
| 122 |
global $wpdb; |
| 123 |
$wpdb->delete( $wpdb->actionscheduler_logs, array( 'action_id' => $action_id ), array( '%d' ) ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Bulk add cancel action log entries. |
| 128 |
* |
| 129 |
* @param array $action_ids List of action ID. |
| 130 |
*/ |
| 131 |
public function bulk_log_cancel_actions( $action_ids ) { |
| 132 |
if ( empty( $action_ids ) ) { |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
/** @var \wpdb $wpdb */ //phpcs:ignore Generic.Commenting.DocComment.MissingShort |
| 137 |
global $wpdb; |
| 138 |
$date = as_get_datetime_object(); |
| 139 |
$date_gmt = $date->format( 'Y-m-d H:i:s' ); |
| 140 |
ActionScheduler_TimezoneHelper::set_local_timezone( $date ); |
| 141 |
$date_local = $date->format( 'Y-m-d H:i:s' ); |
| 142 |
$message = __( 'action canceled', 'action-scheduler' ); |
| 143 |
$format = '(%d, ' . $wpdb->prepare( '%s, %s, %s', $message, $date_gmt, $date_local ) . ')'; |
| 144 |
$sql_query = "INSERT {$wpdb->actionscheduler_logs} (action_id, message, log_date_gmt, log_date_local) VALUES "; |
| 145 |
$value_rows = array(); |
| 146 |
|
| 147 |
foreach ( $action_ids as $action_id ) { |
| 148 |
$value_rows[] = $wpdb->prepare( $format, $action_id ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 149 |
} |
| 150 |
$sql_query .= implode( ',', $value_rows ); |
| 151 |
|
| 152 |
$wpdb->query( $sql_query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 153 |
} |
| 154 |
} |
| 155 |
|