PluginProbe
Jetpack VaultPress Backup / 1.2.0
Jetpack VaultPress Backup v1.2.0
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.2.0, at src/php/class-jetpack-backup.php

359 lines 9.8 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\Assets;
14 use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State;
15 use Automattic\Jetpack\Connection\Manager as Connection_Manager;
16 use Automattic\Jetpack\Connection\Rest_Authentication as Connection_Rest_Authentication;
17 use Automattic\Jetpack\My_Jetpack\Initializer as My_Jetpack_Initializer;
18 use Automattic\Jetpack\Status;
19
20 /**
21 * Class Jetpack_Backup
22 */
23 class Jetpack_Backup {
24
25 /**
26 * Constructor.
27 */
28 public function __construct() {
29 // Set up the REST authentication hooks.
30 Connection_Rest_Authentication::init();
31
32 add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) );
33
34 $page_suffix = Admin_Menu::add_menu(
35 __( 'Jetpack Backup', 'jetpack-backup' ),
36 _x( 'Backup', 'The Jetpack Backup product name, without the Jetpack prefix', 'jetpack-backup' ),
37 'manage_options',
38 'jetpack-backup',
39 array( $this, 'plugin_settings_page' ),
40 99
41 );
42 add_action( 'load-' . $page_suffix, array( $this, 'admin_init' ) );
43
44 // Init Jetpack packages and ConnectionUI.
45 add_action(
46 'plugins_loaded',
47 function () {
48 $config = new Automattic\Jetpack\Config();
49 // Connection package.
50 $config->ensure(
51 'connection',
52 array(
53 'slug' => JETPACK_BACKUP_PLUGIN_SLUG,
54 'name' => JETPACK_BACKUP_PLUGIN_NAME,
55 'url_info' => JETPACK_BACKUP_PLUGIN_URI,
56 )
57 );
58 // Sync package.
59 $config->ensure( 'sync' );
60
61 // Identity crisis package.
62 $config->ensure( 'identity_crisis' );
63 },
64 1
65 );
66
67 // Add "Settings" link to plugins page.
68 add_filter(
69 'plugin_action_links_' . JETPACK_BACKUP_PLUGIN_FOLDER . '/jetpack-backup.php',
70 function ( $actions ) {
71 $settings_link = '<a href="' . esc_url( admin_url( 'admin.php?page=jetpack-backup' ) ) . '">' . __( 'Settings', 'jetpack-backup' ) . '</a>';
72 array_unshift( $actions, $settings_link );
73
74 return $actions;
75 }
76 );
77
78 add_action( 'plugins_loaded', array( $this, 'maybe_upgrade_db' ), 20 );
79
80 My_Jetpack_Initializer::init();
81 }
82
83 /**
84 * Initialize the admin resources.
85 */
86 public function admin_init() {
87 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
88 }
89
90 /**
91 * Checks current version against version in code and run upgrades if we are running a new version
92 */
93 public function maybe_upgrade_db() {
94 $current_db_version = get_option( 'jetpack_backup_db_version' );
95 if ( version_compare( $current_db_version, JETPACK_BACKUP_DB_VERSION, '<' ) ) {
96 update_option( 'jetpack_backup_db_version', JETPACK_BACKUP_DB_VERSION );
97 Jetpack_Backup_Upgrades::upgrade();
98 }
99 }
100
101 /**
102 * Enqueue plugin admin scripts and styles.
103 */
104 public function enqueue_admin_scripts() {
105 $status = new Status();
106 $manager = new Connection_Manager( 'jetpack-backup' );
107
108 Assets::register_script(
109 'jetpack-backup',
110 'build/index.js',
111 JETPACK_BACKUP_PLUGIN_ROOT_FILE,
112 array(
113 'in_footer' => true,
114 'textdomain' => 'jetpack-backup',
115 )
116 );
117 Assets::enqueue_script( 'jetpack-backup' );
118 // Initial JS state including JP Connection data.
119 wp_add_inline_script( 'jetpack-backup', $this->get_initial_state(), 'before' );
120 wp_add_inline_script( 'jetpack-backup', Connection_Initial_State::render(), 'before' );
121
122 // Load script for analytics.
123 if ( ! $status->is_offline_mode() && $manager->is_connected() ) {
124 wp_enqueue_script( 'jp-tracks', '//stats.wp.com/w.js', array(), gmdate( 'YW' ), true );
125 }
126 }
127
128 /**
129 * Main plugin settings page.
130 */
131 public function plugin_settings_page() {
132 ?>
133 <div id="jetpack-backup-root"></div>
134 <?php
135 }
136
137 /**
138 * Return the rendered initial state JavaScript code.
139 *
140 * @return string
141 */
142 private function get_initial_state() {
143 require_once JETPACK_BACKUP_PLUGIN_DIR . '/src//php/class-initial-state.php';
144 return ( new Initial_State() )->render();
145 }
146
147 /**
148 * Register REST API
149 */
150 public function register_rest_routes() {
151
152 // Get information on most recent 10 backups.
153 register_rest_route(
154 'jetpack/v4',
155 '/backups',
156 array(
157 'methods' => WP_REST_Server::READABLE,
158 'callback' => __CLASS__ . '::get_recent_backups',
159 'permission_callback' => __CLASS__ . '::backups_permissions_callback',
160 )
161 );
162
163 // Get site backup/scan/anti-spam capabilities.
164 register_rest_route(
165 'jetpack/v4',
166 '/backup-capabilities',
167 array(
168 'methods' => WP_REST_Server::READABLE,
169 'callback' => __CLASS__ . '::get_backup_capabilities',
170 'permission_callback' => __CLASS__ . '::backups_permissions_callback',
171 )
172 );
173
174 // Get information on site products.
175 // Backup plugin version of /site/purchases from JP plugin.
176 // Revert once this route and MyPlan component are extracted to a common package.
177 register_rest_route(
178 'jetpack/v4',
179 '/site/current-purchases',
180 array(
181 'methods' => WP_REST_Server::READABLE,
182 'callback' => __CLASS__ . '::get_site_current_purchases',
183 'permission_callback' => __CLASS__ . '::backups_permissions_callback',
184 )
185 );
186
187 // Get currently promoted product from the product's endpoint.
188 register_rest_route(
189 'jetpack/v4',
190 '/backup-promoted-product-info',
191 array(
192 'methods' => WP_REST_Server::READABLE,
193 'callback' => __CLASS__ . '::get_backup_promoted_product_info',
194 'permission_callback' => __CLASS__ . '::backups_permissions_callback',
195 )
196 );
197 }
198 /**
199 * The backup calls should only occur from a signed in admin user
200 *
201 * @access public
202 * @static
203 *
204 * @return true|WP_Error
205 */
206 public static function backups_permissions_callback() {
207 return current_user_can( 'manage_options' );
208 }
209
210 /**
211 * Get information about recent backups
212 *
213 * @access public
214 * @static
215 *
216 * @return array An array of recent backups
217 */
218 public static function get_recent_backups() {
219 $blog_id = \Jetpack_Options::get_option( 'id' );
220
221 $response = Automattic\Jetpack\Connection\Client::wpcom_json_api_request_as_blog(
222 '/sites/' . $blog_id . '/rewind/backups',
223 'v2',
224 array(),
225 null,
226 'wpcom'
227 );
228
229 if ( 200 !== $response['response']['code'] ) {
230 return null;
231 }
232
233 return rest_ensure_response(
234 json_decode( $response['body'], true )
235 );
236 }
237
238 /**
239 * Get an array of backup/scan/anti-spam site capabilities
240 *
241 * @access public
242 * @static
243 *
244 * @return array An array of capabilities
245 */
246 public static function get_backup_capabilities() {
247 $blog_id = \Jetpack_Options::get_option( 'id' );
248
249 $response = Automattic\Jetpack\Connection\Client::wpcom_json_api_request_as_user(
250 '/sites/' . $blog_id . '/rewind/capabilities',
251 'v2',
252 array(),
253 null,
254 'wpcom'
255 );
256
257 if ( is_wp_error( $response ) ) {
258 return null;
259 }
260
261 if ( 200 !== $response['response']['code'] ) {
262 return null;
263 }
264
265 return rest_ensure_response(
266 json_decode( $response['body'], true )
267 );
268 }
269
270 /**
271 * Gets information about the currently promoted backup product.
272 *
273 * @return string|WP_Error A JSON object of the current backup product being promoted if the request was successful, or a WP_Error otherwise.
274 */
275 public static function get_backup_promoted_product_info() {
276 $request_url = 'https://public-api.wordpress.com/rest/v1.1/products?locale=' . get_user_locale() . '&type=jetpack';
277 $wpcom_request = wp_remote_get( esc_url_raw( $request_url ) );
278 $response_code = wp_remote_retrieve_response_code( $wpcom_request );
279 if ( 200 === $response_code ) {
280 $products = json_decode( wp_remote_retrieve_body( $wpcom_request ) );
281 return $products->{JETPACK_BACKUP_PROMOTED_PRODUCT};
282 } else {
283 // Something went wrong so we'll just return the response without caching.
284 return new WP_Error(
285 'failed_to_fetch_data',
286 esc_html__( 'Unable to fetch the requested data.', 'jetpack-backup' ),
287 array(
288 'status' => $response_code,
289 'request' => $wpcom_request,
290 )
291 );
292 }
293 }
294
295 /**
296 * Redirects to plugin page when the plugin is activated
297 *
298 * @access public
299 * @static
300 *
301 * @param string $plugin Path to the plugin file relative to the plugins directory.
302 */
303 public static function plugin_activation( $plugin ) {
304 if ( JETPACK_BACKUP_PLUGIN_ROOT_FILE_RELATIVE_PATH === $plugin ) {
305 wp_safe_redirect( esc_url( admin_url( 'admin.php?page=jetpack-backup' ) ) );
306 exit;
307 }
308 }
309
310 /**
311 * Removes plugin from the connection manager
312 * If it's the last plugin using the connection, the site will be disconnected.
313 *
314 * @access public
315 * @static
316 */
317 public static function plugin_deactivation() {
318 $manager = new Connection_Manager( 'jetpack-backup' );
319 $manager->disconnect_site_wpcom();
320 $manager->delete_all_connection_tokens();
321 }
322
323 /**
324 * Returns the result of `/sites/%d/purchases` endpoint call.
325 *
326 * @return array of site purchases.
327 */
328 public static function get_site_current_purchases() {
329
330 $request = sprintf( '/sites/%d/purchases', \Jetpack_Options::get_option( 'id' ) );
331 $response = Automattic\Jetpack\Connection\Client::wpcom_json_api_request_as_blog( $request, '1.1' );
332
333 // Bail if there was an error or malformed response.
334 if ( is_wp_error( $response ) || ! is_array( $response ) || ! isset( $response['body'] ) ) {
335 return self::get_failed_fetch_error();
336 }
337
338 if ( 200 !== (int) wp_remote_retrieve_response_code( $response ) ) {
339 return self::get_failed_fetch_error();
340 }
341
342 // Decode the results.
343 $results = json_decode( $response['body'], true );
344
345 // Bail if there were no results or purchase details returned.
346 if ( ! is_array( $results ) ) {
347 return self::get_failed_fetch_error();
348 }
349
350 return rest_ensure_response(
351 array(
352 'code' => 'success',
353 'message' => esc_html__( 'Site purchases correctly received.', 'jetpack-backup' ),
354 'data' => wp_remote_retrieve_body( $response ),
355 )
356 );
357 }
358 }
359