protected function __construct() { if (false !== get_transient('_my-wp-backup-activated')) { delete_transient('_my-wp-backup-activated'); wp_redirect(Admin::get_page_url('')); } self::$info = get_file_data(__FILE__, array('name' => 'Plugin Name', 'pluginUri' => 'Plugin URI', 'supportUri' => 'Support URI', 'version' => 'Version', 'description' => 'Description', 'author' => 'Author', 'authorUri' => 'Author URI', 'textDomain' => 'Text Domain', 'domainPath' => 'Domain Path', 'slug' => 'Slug', 'license' => 'License', 'licenseUri' => 'License URI')); Admin::get_instance(); $options = get_site_option('my-wp-backup-options', Admin::$options); self::$info['baseDir'] = plugin_dir_path(__FILE__); self::$info['baseDirUrl'] = plugin_dir_url(__FILE__); self::$info['backup_dir'] = trailingslashit(ABSPATH) . trailingslashit(ltrim($options['backup_dir'], '/')); self::$info['root_dir'] = trailingslashit(ABSPATH); if (defined('WP_CLI') && WP_CLI) { \WP_CLI::add_command('job', new Cli\Job()); \WP_CLI::add_command('backup', new Cli\Backup()); } add_action('wp_backup_run_job', array(Job::get_instance(), 'cron_run')); add_action('wp_backup_restore_backup', array(Backup::get_instance(), 'cron_run')); $version = get_site_option(self::KEY_VERSION); if (!$version || self::$info['version'] !== $version) { if ($this->update_options()) { update_site_option(self::KEY_VERSION, self::$info['version']); } } }
/** * Delete a job. * @synopsis <id> [--confirm] * * @param $args * @param $assoc * * @return bool */ public function delete($args, $assoc) { list($id) = $args; if (isset($assoc['confirm'])) { $job = \MyWPBackup\Admin\Job::get($id); if (!$job) { echo sprintf(__('No such job with ID %s.', 'my-wp-backup'), $id) . "\n"; return false; } \MyWPBackup\Admin\Job::delete($id); echo sprintf(__('Deleted job %s.', 'my-wp-backup'), $job['job_name']) . "\n"; } else { echo 'Please add --confirm to really delete the job.' . "\n"; } return true; }
public function init() { wp_cache_add_non_persistent_groups('my-wp-backup'); add_action('admin_post_MyWPBackup_settings', array($this, 'post_settings')); add_action('admin_post_MyWPBackup_import', array($this, 'post_import')); add_action(is_multisite() ? 'network_admin_notices' : 'admin_notices', array($this, 'admin_notice')); global $current_user; $user_id = $current_user->ID; if (is_multisite() && is_network_admin() || !is_multisite() && current_user_can('manage_options')) { if ('1' === filter_input(INPUT_GET, 'mwpb_notice_close', FILTER_SANITIZE_NUMBER_INT)) { add_user_meta($user_id, 'mwpb-notice-1', 'true', true); } } Job::get_instance(); Backup::get_instance(); }
public function post_run_job() { if (!isset($_POST['_wpnonce']) || !isset($_POST['id'])) { return; } $nonce = filter_input(INPUT_POST, '_wpnonce', FILTER_SANITIZE_STRING); if (!wp_verify_nonce($nonce, 'my-wp-backup-run-job')) { wp_die(esc_html__('Nope! Security check failed!', 'my-wp-backup')); } $id = absint($_POST['id']); //input var okay $job = Job::get($id); if (!$job) { wp_die(esc_html__('Nope! Security check failed!', 'my-wp-backup')); } if (false !== ($running_job = get_transient('my-wp-backup-running'))) { if ($id !== $running_job['id']) { add_settings_error('', '', sprintf(__('Job "%s" is currently running.', 'my-wp-backup'), $running_job['job_name'])); set_transient('settings_errors', get_settings_errors()); wp_safe_redirect($this->admin->get_page_url('jobs', array('settings-updated' => 1))); } } if ('manual' === $job['schedule_type']) { $uniqid = uniqid(); wp_schedule_single_event(time(), 'wp_backup_run_job', array(array($id, $uniqid))); wp_safe_redirect($this->admin->get_page_url('jobs', array('action' => 'view', 'uniqid' => $uniqid, 'id' => $id))); } else { add_settings_error('', '', sprintf(__('Cannot manually run job of schedule type "%s".', 'my-wp-backup'), $job['schedule_type'])); set_transient('settings_errors', get_settings_errors()); wp_safe_redirect($this->admin->get_page_url('jobs', array('settings-updated' => 1))); } }
<input type="hidden" name="my-wp-backup-jobs[id]" value="<?php echo esc_attr($id); ?> "> <input type="hidden" name="my-wp-backup-jobs[action]" value="<?php echo esc_attr($action); ?> "> <input type="hidden" name="tour" value="<?php echo esc_attr(isset($_GET['tour']) && 'yes' === $_GET['tour'] ? 'yes' : 'no'); ?> "> <?php if ('edit' === $action) { $job = Job::get($id); } else { $job = Job::$form_defaults; } ?> <div id="section-general" class="nav-tab-content nav-tab-content-active"> <table class="form-table"> <tbody> <tr> <th scope="row"> <label for="job-name"><?php esc_html_e('Job Name', 'my-wp-backup'); ?> </label> </th>
public function get_archives() { $id = $this['job']['id']; $uniqid = $this['uniqid']; return array_map(function ($filename) use($id, $uniqid) { return Admin\Job::get_instance()->get_basedir($id, $uniqid) . $filename; }, $this['archives']); }
public function download_googledrive($options) { $this->log(__('Downloading backup via google drive', 'my-wp-backup')); $client = Admin\Job::get_drive_client(); $client->setAccessToken(html_entity_decode($options['token_json'])); $service = new \Google_Service_Drive($client); $info = $this->backup['destinations']['googledrive']; foreach ($this->backup['archives'] as $archive) { $file = $info['files'][$archive]; $local_filename = $this->basedir . $archive; $this->log(sprintf(__('Downloading %s -> %s...', 'my-wp-backup'), $file['id'], $local_filename), 'debug'); $local = fopen($local_filename, 'wb'); $url = $service->files->get($file['id'])->getDownloadUrl(); $token = json_decode($client->getAccessToken(), true); if ($client->isAccessTokenExpired()) { $client->refreshToken($token['refresh_token']); } $token = json_decode($client->getAccessToken(), true); $httpclient = new \Guzzle\Http\Client(); $httpclient->setDefaultOption('headers/Authorization', 'Bearer ' . $token['access_token']); $httpclient->get($url)->setResponseBody($local)->send(); if (is_resource($local)) { fclose($local); } $this->log(__('Ok.', 'my-wp-backup'), 'debug'); } $this->log(__('Done google drive download', 'my-wp-backup')); }