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

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