if (!defined('FW')) die('Forbidden'); /** * @since 2.0.0 */ final class FW_Ext_Backups_Task_Collection { /** * @var string * @since 2.0.0 */ private $id; /** * @var string * @since 2.0.0 */ private $title; /** * @var FW_Ext_Backups_Task[] * @since 2.0.0 */ private $tasks = array(); /** * @param string $id */ public function __construct($id = null) { $this->id = (string)(is_null($id) ? fw_rand_md5() : $id); } /** * @param FW_Ext_Backups_Task $task * * @return bool * @since 2.0.0 */ public function add_task(FW_Ext_Backups_Task $task) { if (in_array($task, $this->tasks, true)) { return false; } else { foreach ($this->tasks as $_task) { if ($_task->get_id() === $task->get_id()) { return false; } } $this->tasks[] = $task; return true; } } /** * @return string * @since 2.0.0 */ public function get_id() { return $this->id; } public function get_title() { if (is_null($this->title)) { return fw_id_to_title($this->id); } else { return $this->title; } } public function set_title($title) { $this->title = $title; return $this; } /** * @return FW_Ext_Backups_Task[] * @since 2.0.0 */ public function get_tasks() { return $this->tasks; } /** * Empty out the collection internal state and return the deleted tasks * * @return FW_Ext_Backups_Task[] * @since 2.0.17 */ public function empty_collection() { $tmp = $this->tasks; $this->tasks = array(); return $tmp; } /** * @param string $id * * @return FW_Ext_Backups_Task|null * @since 2.0.0 * * Note: The returned instance will be changed by reference https://3v4l.org/Ps5hs (use `clone $instance`) */ public function get_task($id) { if (empty($this->tasks)) { return null; } foreach ($this->tasks as $task) { if ($task->get_id() === $id) { return $task; } } return null; } /** * If the collection is in a state that can be Cancelled/Aborted * @return bool */ public function is_cancelable() { $tasks = $this->get_tasks(); if ( ($first_task = reset($tasks)) && !$first_task->get_last_execution_start_time() ) { return true; // The execution haven't started } foreach ($tasks as $task) { if ( ! $task->get_last_execution_start_time() ) { return false; // We reached a pending task } elseif ( ! $task->get_last_execution_end_time() ) { /** @var FW_Extension_Backups $ext */ $ext = fw_ext('backups'); if (($task->get_last_execution_start_time() + $ext->get_task_step_execution_threshold()) < time()) { return true; // The task is execution for a too long time } else { return false; } } } return false; } /** * @return array * @since 2.0.0 */ public function to_array() { $tasks = array(); foreach ($this->get_tasks() as $task) { $tasks[] = $task->to_array(); } return array( 'id' => $this->get_id(), 'title' => $this->title, 'tasks' => $tasks, ); } /** * @param array $c * * @return FW_Ext_Backups_Task_Collection * @since 2.0.0 */ public static function from_array(array $c) { if (empty($c)) { return null; } $collection = new self($c['id']); if (isset($c['title'])) { $collection->set_title($c['title']); } foreach ($c['tasks'] as $t) { $collection->add_task( FW_Ext_Backups_Task::from_array($t) ); } return $collection; } } if (!defined('FW')) die('Forbidden'); /** * @internal */ class _FW_Ext_Backups_Log { /** * @var FW_Access_Key|null */ private static $access_key; /** * @return FW_Access_Key|null */ private static function get_access_key() { if (empty(self::$access_key)) { self::$access_key = new FW_Access_Key('fw:ext:backups:log'); } return self::$access_key; } /** * @return FW_Extension_Backups */ private static function backups() { return fw_ext('backups'); } private static $wp_option = 'fw:ext:backups:log'; private static $log_limit = 30; public function __construct() { add_action('fw:ext:backups:task:fail', array($this, '_action_task_fail')); add_action('fw:ext:backups:tasks:success', array($this, '_action_tasks_success')); add_action('fw_ext_backups_page_footer', array($this, '_action_page_footer')); add_action('fw:ext:backups:enqueue_scripts', array($this, '_action_enqueue_scripts')); add_filter( 'fw_ext_backups_db_export_exclude_option', array($this, '_filter_fw_ext_backups_db_export_exclude_option'), 10, 3 ); add_filter( 'fw_ext_backups_db_restore_keep_options', array($this, '_filter_fw_ext_backups_db_restore_keep_options') ); add_filter( 'fw_ext_backups_ajax_status_extra_response', array($this, '_filter_fw_ext_backups_ajax_status_extra_response') ); } private function get_log() { return get_option(self::$wp_option, array()); } private function set_log($log) { while (count($log) > self::$log_limit) { array_pop($log); } return update_option(self::$wp_option, $log, false); } private function add_log($type, $title, array $data = array()) { if (!in_array($type, array('success', 'info', 'warning', 'error'))) { trigger_error('Invalid log type: '. $type, E_USER_WARNING); } $log = $this->get_log(); array_unshift($log, array( 'type' => $type, 'title' => $title, 'data' => $data, 'time' => time(), )); $this->set_log($log); } private function render_log() { return fw_render_view(dirname(__FILE__) .'/view.php', array( 'log' => $this->get_log() )); } /** * @param FW_Ext_Backups_Task $task * @internal */ public function _action_task_fail(FW_Ext_Backups_Task $task) { $this->add_log( 'error', self::backups()->tasks()->get_task_type_title($task->get_type()) . (is_wp_error($task->get_result()) ? ': '. $task->get_result()->get_error_message() : ''), is_wp_error($task->get_result()) ? (array)$task->get_result()->get_error_data() : array() ); } /** * @param FW_Ext_Backups_Task_Collection $tasks * @internal */ public function _action_tasks_success(FW_Ext_Backups_Task_Collection $tasks) { $this->add_log( 'success', $tasks->get_title() ); } /** * @param array $options {option_name: true} * @return array */ public function _filter_fw_ext_backups_db_restore_keep_options($options) { $options[ self::$wp_option] = true; return $options; } /** * @param bool $exclude * @param string $option_name * @param bool $is_full_backup * @return bool */ public function _filter_fw_ext_backups_db_export_exclude_option($exclude, $option_name, $is_full_backup) { if (!$is_full_backup && $option_name === self::$wp_option) { return true; } return $exclude; } /** * @internal */ public function _action_page_footer() { echo '
'; } /** * @param $data * @return mixed * @internal */ public function _filter_fw_ext_backups_ajax_status_extra_response($data) { $data['log'] = array( 'html' => $this->render_log(), ); return $data; } /** * @internal */ public function _action_enqueue_scripts() { wp_enqueue_style( 'fw-ext-backups-log', self::backups()->get_uri('/includes/log/styles.css'), array(), self::backups()->manifest->get_version() ); wp_enqueue_script( 'fw-ext-backups-log', self::backups()->get_uri('/includes/log/scripts.js'), array('fw'), self::backups()->manifest->get_version() ); } } new _FW_Ext_Backups_Log();

Prepárate ... Algo realmente genial llegará pronto