| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class ActionScheduler_DBStoreMigrator |
| 5 |
* |
| 6 |
* A class for direct saving of actions to the table data store during migration. |
| 7 |
* |
| 8 |
* @since 3.0.0 |
| 9 |
*/ |
| 10 |
class ActionScheduler_DBStoreMigrator extends ActionScheduler_DBStore { |
| 11 |
|
| 12 |
/** |
| 13 |
* Save an action with optional last attempt date. |
| 14 |
* |
| 15 |
* Normally, saving an action sets its attempted date to 0000-00-00 00:00:00 because when an action is first saved, |
| 16 |
* it can't have been attempted yet, but migrated completed actions will have an attempted date, so we need to save |
| 17 |
* that when first saving the action. |
| 18 |
* |
| 19 |
* @param ActionScheduler_Action $action |
| 20 |
* @param \DateTime $scheduled_date Optional date of the first instance to store. |
| 21 |
* @param \DateTime $last_attempt_date Optional date the action was last attempted. |
| 22 |
* |
| 23 |
* @return string The action ID |
| 24 |
* @throws \RuntimeException When the action is not saved. |
| 25 |
*/ |
| 26 |
public function save_action( ActionScheduler_Action $action, \DateTime $scheduled_date = null, \DateTime $last_attempt_date = null ){ |
| 27 |
try { |
| 28 |
/** @var \wpdb $wpdb */ |
| 29 |
global $wpdb; |
| 30 |
|
| 31 |
$action_id = parent::save_action( $action, $scheduled_date ); |
| 32 |
|
| 33 |
if ( null !== $last_attempt_date ) { |
| 34 |
$data = [ |
| 35 |
'last_attempt_gmt' => $this->get_scheduled_date_string( $action, $last_attempt_date ), |
| 36 |
'last_attempt_local' => $this->get_scheduled_date_string_local( $action, $last_attempt_date ), |
| 37 |
]; |
| 38 |
|
| 39 |
$wpdb->update( $wpdb->actionscheduler_actions, $data, array( 'action_id' => $action_id ), array( '%s', '%s' ), array( '%d' ) ); |
| 40 |
} |
| 41 |
|
| 42 |
return $action_id; |
| 43 |
} catch ( \Exception $e ) { |
| 44 |
throw new \RuntimeException( sprintf( __( 'Error saving action: %s', 'action-scheduler' ), $e->getMessage() ), 0 ); |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
|