PluginProbe
Jetpack VaultPress Backup / 1.0.1
Jetpack VaultPress Backup v1.0.1
3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7.1 3.8 3.9 trunk 0.2.0 1.0.0 1.0.1 1.1.0 1.2.0 1.3.0 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5 1.6 1.7 All 37 releases
jetpack-backup / src / php / class-jetpack-backup.php

class-jetpack-backup.php in Jetpack VaultPress Backup 1.0.1, at src/php/class-jetpack-backup.php

249 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Primary class file for the Jetpack Backup plugin.
4 *
5 * @package automattic/jetpack-backup-plugin
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 use Automattic\Jetpack\Admin_UI\Admin_Menu;
13 use Automattic\Jetpack\Connection\Manager as Connection_Manager;
14 use Automattic\Jetpack\Connection\Rest_Authentication as Connection_Rest_Authentication;
15
16 /**
17 * Class Jetpack_Backup
18 */
19 class Jetpack_Backup {
20 /**
21 * Constructor.
22 */
23 public function __construct() {
24 // Set up the REST authentication hooks.
25 Connection_Rest_Authentication::init();
26
27 add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) );
28
29 $page_suffix = Admin_Menu::add_menu(
30 __( 'Jetpack Backup', 'jetpack-backup' ),
31 _x( 'Backup', 'The Jetpack Backup product name, without the Jetpack prefix', 'jetpack-backup' ),
32 'manage_options',
33 'jetpack-backup',
34 array( $this, 'plugin_settings_page' ),
35 99
36 );
37 add_action( 'load-' . $page_suffix, array( $this, 'admin_init' ) );
38
39 // Init Jetpack packages and ConnectionUI.
40 add_action(
41 'plugins_loaded',
42 function () {
43 $config = new Automattic\Jetpack\Config();
44 // Connection package.
45 $config->ensure(
46 'connection',
47 array(
48 'slug' => JETPACK_BACKUP_PLUGIN_SLUG,
49 'name' => JETPACK_BACKUP_PLUGIN_NAME,
50 'url_info' => JETPACK_BACKUP_PLUGIN_URI,
51 )
52 );
53 // Sync package.
54 $config->ensure( 'sync' );
55
56 // Identity crisis package.
57 $config->ensure( 'identity_crisis' );
58 },
59 1
60 );
61
62 // Add "Settings" link to plugins page.
63 add_filter(
64 'plugin_action_links_' . JETPACK_BACKUP_PLUGIN_FOLDER . '/jetpack-backup.php',
65 function ( $actions ) {
66 $settings_link = '<a href="' . esc_url( admin_url( 'admin.php?page=jetpack-backup' ) ) . '">' . __( 'Settings', 'jetpack-backup' ) . '</a>';
67 array_unshift( $actions, $settings_link );
68
69 return $actions;
70 }
71 );
72 }
73
74 /**
75 * Initialize the admin resources.
76 */
77 public function admin_init() {
78 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
79 }
80
81 /**
82 * Enqueue plugin admin scripts and styles.
83 */
84 public function enqueue_admin_scripts() {
85 $build_assets = require_once JETPACK_BACKUP_PLUGIN_DIR . '/build/index.asset.php';
86
87 // Main JS file.
88 wp_register_script(
89 'jetpack-backup-script',
90 plugins_url( 'build/index.js', JETPACK_BACKUP_PLUGIN_ROOT_FILE ),
91 $build_assets['dependencies'],
92 $build_assets['version'],
93 true
94 );
95 wp_enqueue_script( 'jetpack-backup-script' );
96 // Initial JS state including JP Connection data.
97 wp_add_inline_script( 'jetpack-backup-script', $this->get_initial_state(), 'before' );
98
99 // Translation assets.
100 wp_set_script_translations( 'jetpack-backup-script-translations', 'jetpack-backup' );
101
102 // Main CSS file.
103 wp_enqueue_style(
104 'jetpack-backup-style',
105 plugins_url( 'build/index.css', JETPACK_BACKUP_PLUGIN_ROOT_FILE ),
106 array( 'wp-components' ),
107 $build_assets['version']
108 );
109 // RTL CSS file.
110 wp_style_add_data(
111 'jetpack-backup-style',
112 'rtl',
113 plugins_url( 'build/index.rtl.css', JETPACK_BACKUP_PLUGIN_ROOT_FILE )
114 );
115 }
116
117 /**
118 * Main plugin settings page.
119 */
120 public function plugin_settings_page() {
121 ?>
122 <div id="jetpack-backup-root"></div>
123 <?php
124 }
125
126 /**
127 * Return the rendered initial state JavaScript code.
128 *
129 * @return string
130 */
131 private function get_initial_state() {
132 require_once JETPACK_BACKUP_PLUGIN_DIR . '/src//php/class-initial-state.php';
133 return ( new Initial_State() )->render();
134 }
135
136 /**
137 * Register REST API
138 */
139 public function register_rest_routes() {
140
141 // Get information on most recent 10 backups.
142 register_rest_route(
143 'jetpack/v4',
144 '/backups',
145 array(
146 'methods' => WP_REST_Server::READABLE,
147 'callback' => __CLASS__ . '::get_recent_backups',
148 'permission_callback' => __CLASS__ . '::backups_permissions_callback',
149 )
150 );
151
152 // Get site backup/scan/anti-spam capabilities.
153 register_rest_route(
154 'jetpack/v4',
155 '/backup-capabilities',
156 array(
157 'methods' => WP_REST_Server::READABLE,
158 'callback' => __CLASS__ . '::get_backup_capabilities',
159 'permission_callback' => __CLASS__ . '::backups_permissions_callback',
160 )
161 );
162
163 }
164
165 /**
166 * The backup calls should only occur from a signed in admin user
167 *
168 * @access public
169 * @static
170 *
171 * @return true|WP_Error
172 */
173 public static function backups_permissions_callback() {
174 return current_user_can( 'manage_options' );
175 }
176
177 /**
178 * Get information about recent backups
179 *
180 * @access public
181 * @static
182 *
183 * @return array An array of recent backups
184 */
185 public static function get_recent_backups() {
186 $blog_id = \Jetpack_Options::get_option( 'id' );
187
188 $response = Automattic\Jetpack\Connection\Client::wpcom_json_api_request_as_blog(
189 '/sites/' . $blog_id . '/rewind/backups',
190 'v2',
191 array(),
192 null,
193 'wpcom'
194 );
195
196 if ( 200 !== $response['response']['code'] ) {
197 return null;
198 }
199
200 return rest_ensure_response(
201 json_decode( $response['body'], true )
202 );
203 }
204
205 /**
206 * Get an array of backup/scan/anti-spam site capabilities
207 *
208 * @access public
209 * @static
210 *
211 * @return array An array of capabilities
212 */
213 public static function get_backup_capabilities() {
214 $blog_id = \Jetpack_Options::get_option( 'id' );
215
216 $response = Automattic\Jetpack\Connection\Client::wpcom_json_api_request_as_user(
217 '/sites/' . $blog_id . '/rewind/capabilities',
218 'v2',
219 array(),
220 null,
221 'wpcom'
222 );
223
224 if ( is_wp_error( $response ) ) {
225 return null;
226 }
227
228 if ( 200 !== $response['response']['code'] ) {
229 return null;
230 }
231
232 return rest_ensure_response(
233 json_decode( $response['body'], true )
234 );
235 }
236
237 /**
238 * Removes plugin from the connection manager
239 * If it's the last plugin using the connection, the site will be disconnected.
240 *
241 * @access public
242 * @static
243 */
244 public static function plugin_deactivation() {
245 $manager = new Connection_Manager( 'jetpack-backup' );
246 $manager->remove_connection();
247 }
248 }
249