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

345 lines 9.3 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 My_Jetpack_Initializer::init();
79 }
80
81 /**
82 * Initialize the admin resources.
83 */
84 public function admin_init() {
85 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
86 }
87
88 /**
89 * Enqueue plugin admin scripts and styles.
90 */
91 public function enqueue_admin_scripts() {
92 $status = new Status();
93 $manager = new Connection_Manager( 'jetpack-backup' );
94
95 Assets::register_script(
96 'jetpack-backup',
97 'build/index.js',
98 JETPACK_BACKUP_PLUGIN_ROOT_FILE,
99 array(
100 'in_footer' => true,
101 'textdomain' => 'jetpack-backup',
102 )
103 );
104 Assets::enqueue_script( 'jetpack-backup' );
105 // Initial JS state including JP Connection data.
106 wp_add_inline_script( 'jetpack-backup', $this->get_initial_state(), 'before' );
107 wp_add_inline_script( 'jetpack-backup', Connection_Initial_State::render(), 'before' );
108
109 // Load script for analytics.
110 if ( ! $status->is_offline_mode() && $manager->is_connected() ) {
111 wp_enqueue_script( 'jp-tracks', '//stats.wp.com/w.js', array(), gmdate( 'YW' ), true );
112 }
113 }
114
115 /**
116 * Main plugin settings page.
117 */
118 public function plugin_settings_page() {
119 ?>
120 <div id="jetpack-backup-root"></div>
121 <?php
122 }
123
124 /**
125 * Return the rendered initial state JavaScript code.
126 *
127 * @return string
128 */
129 private function get_initial_state() {
130 require_once JETPACK_BACKUP_PLUGIN_DIR . '/src//php/class-initial-state.php';
131 return ( new Initial_State() )->render();
132 }
133
134 /**
135 * Register REST API
136 */
137 public function register_rest_routes() {
138
139 // Get information on most recent 10 backups.
140 register_rest_route(
141 'jetpack/v4',
142 '/backups',
143 array(
144 'methods' => WP_REST_Server::READABLE,
145 'callback' => __CLASS__ . '::get_recent_backups',
146 'permission_callback' => __CLASS__ . '::backups_permissions_callback',
147 )
148 );
149
150 // Get site backup/scan/anti-spam capabilities.
151 register_rest_route(
152 'jetpack/v4',
153 '/backup-capabilities',
154 array(
155 'methods' => WP_REST_Server::READABLE,
156 'callback' => __CLASS__ . '::get_backup_capabilities',
157 'permission_callback' => __CLASS__ . '::backups_permissions_callback',
158 )
159 );
160
161 // Get information on site products.
162 // Backup plugin version of /site/purchases from JP plugin.
163 // Revert once this route and MyPlan component are extracted to a common package.
164 register_rest_route(
165 'jetpack/v4',
166 '/site/current-purchases',
167 array(
168 'methods' => WP_REST_Server::READABLE,
169 'callback' => __CLASS__ . '::get_site_current_purchases',
170 'permission_callback' => __CLASS__ . '::backups_permissions_callback',
171 )
172 );
173
174 // Get currently promoted product from the product's endpoint.
175 register_rest_route(
176 'jetpack/v4',
177 '/backup-promoted-product-info',
178 array(
179 'methods' => WP_REST_Server::READABLE,
180 'callback' => __CLASS__ . '::get_backup_promoted_product_info',
181 'permission_callback' => __CLASS__ . '::backups_permissions_callback',
182 )
183 );
184 }
185 /**
186 * The backup calls should only occur from a signed in admin user
187 *
188 * @access public
189 * @static
190 *
191 * @return true|WP_Error
192 */
193 public static function backups_permissions_callback() {
194 return current_user_can( 'manage_options' );
195 }
196
197 /**
198 * Get information about recent backups
199 *
200 * @access public
201 * @static
202 *
203 * @return array An array of recent backups
204 */
205 public static function get_recent_backups() {
206 $blog_id = \Jetpack_Options::get_option( 'id' );
207
208 $response = Automattic\Jetpack\Connection\Client::wpcom_json_api_request_as_blog(
209 '/sites/' . $blog_id . '/rewind/backups',
210 'v2',
211 array(),
212 null,
213 'wpcom'
214 );
215
216 if ( 200 !== $response['response']['code'] ) {
217 return null;
218 }
219
220 return rest_ensure_response(
221 json_decode( $response['body'], true )
222 );
223 }
224
225 /**
226 * Get an array of backup/scan/anti-spam site capabilities
227 *
228 * @access public
229 * @static
230 *
231 * @return array An array of capabilities
232 */
233 public static function get_backup_capabilities() {
234 $blog_id = \Jetpack_Options::get_option( 'id' );
235
236 $response = Automattic\Jetpack\Connection\Client::wpcom_json_api_request_as_user(
237 '/sites/' . $blog_id . '/rewind/capabilities',
238 'v2',
239 array(),
240 null,
241 'wpcom'
242 );
243
244 if ( is_wp_error( $response ) ) {
245 return null;
246 }
247
248 if ( 200 !== $response['response']['code'] ) {
249 return null;
250 }
251
252 return rest_ensure_response(
253 json_decode( $response['body'], true )
254 );
255 }
256
257 /**
258 * Gets information about the currently promoted backup product.
259 *
260 * @return string|WP_Error A JSON object of the current backup product being promoted if the request was successful, or a WP_Error otherwise.
261 */
262 public static function get_backup_promoted_product_info() {
263 $request_url = 'https://public-api.wordpress.com/rest/v1.1/products?locale=' . get_user_locale() . '&type=jetpack';
264 $wpcom_request = wp_remote_get( esc_url_raw( $request_url ) );
265 $response_code = wp_remote_retrieve_response_code( $wpcom_request );
266 if ( 200 === $response_code ) {
267 $products = json_decode( wp_remote_retrieve_body( $wpcom_request ) );
268 return $products->{JETPACK_BACKUP_PROMOTED_PRODUCT};
269 } else {
270 // Something went wrong so we'll just return the response without caching.
271 return new WP_Error(
272 'failed_to_fetch_data',
273 esc_html__( 'Unable to fetch the requested data.', 'jetpack-backup' ),
274 array(
275 'status' => $response_code,
276 'request' => $wpcom_request,
277 )
278 );
279 }
280 }
281
282 /**
283 * Redirects to plugin page when the plugin is activated
284 *
285 * @access public
286 * @static
287 *
288 * @param string $plugin Path to the plugin file relative to the plugins directory.
289 */
290 public static function plugin_activation( $plugin ) {
291 if ( JETPACK_BACKUP_PLUGIN_ROOT_FILE_RELATIVE_PATH === $plugin ) {
292 wp_safe_redirect( esc_url( admin_url( 'admin.php?page=jetpack-backup' ) ) );
293 exit;
294 }
295 }
296
297 /**
298 * Removes plugin from the connection manager
299 * If it's the last plugin using the connection, the site will be disconnected.
300 *
301 * @access public
302 * @static
303 */
304 public static function plugin_deactivation() {
305 $manager = new Connection_Manager( 'jetpack-backup' );
306 $manager->remove_connection();
307 }
308
309 /**
310 * Returns the result of `/sites/%d/purchases` endpoint call.
311 *
312 * @return array of site purchases.
313 */
314 public static function get_site_current_purchases() {
315
316 $request = sprintf( '/sites/%d/purchases', \Jetpack_Options::get_option( 'id' ) );
317 $response = Automattic\Jetpack\Connection\Client::wpcom_json_api_request_as_blog( $request, '1.1' );
318
319 // Bail if there was an error or malformed response.
320 if ( is_wp_error( $response ) || ! is_array( $response ) || ! isset( $response['body'] ) ) {
321 return self::get_failed_fetch_error();
322 }
323
324 if ( 200 !== (int) wp_remote_retrieve_response_code( $response ) ) {
325 return self::get_failed_fetch_error();
326 }
327
328 // Decode the results.
329 $results = json_decode( $response['body'], true );
330
331 // Bail if there were no results or purchase details returned.
332 if ( ! is_array( $results ) ) {
333 return self::get_failed_fetch_error();
334 }
335
336 return rest_ensure_response(
337 array(
338 'code' => 'success',
339 'message' => esc_html__( 'Site purchases correctly received.', 'jetpack-backup' ),
340 'data' => wp_remote_retrieve_body( $response ),
341 )
342 );
343 }
344 }
345