PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.0.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.0.1
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
mainwp / modules / api-backups / classes / class-api-backups-admin.php

class-api-backups-admin.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 5.0.1, at modules/api-backups/classes/class-api-backups-admin.php

304 lines 8.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * =======================================
4 * MainWP API Backups Admin
5 * =======================================
6 *
7 * @package MainWP\Dashboard
8 * @version 5.0
9 */
10
11 namespace MainWP\Dashboard\Module\ApiBackups;
12
13 use MainWP\Dashboard\MainWP_Logger;
14 use MainWP\Dashboard\MainWP_UI;
15 use MainWP\Dashboard\MainWP_Menu;
16 use function MainWP\Dashboard\mainwp_current_user_have_right;
17
18
19 /**
20 * Class Api_Backups_Admin
21 */
22 class Api_Backups_Admin {
23
24 /**
25 * Version string number.
26 *
27 * @var string $version Version.
28 */
29 public $version = '4.2';
30
31 /**
32 * Update version string number.
33 *
34 * @var string $version Update version.
35 */
36 public $update_version = '4.1';
37
38 /**
39 * Current page.
40 *
41 * @static
42 * @var string $page Current page.
43 */
44 public static $page;
45
46 /**
47 * Variable to hold the Sub Pages.
48 *
49 * @static
50 * @var mixed Sub Page, default null.
51 */
52 public static $subPages = null;
53
54 /**
55 * Static variable to hold the single instance of the class.
56 *
57 * @static
58 *
59 * @var mixed Default null
60 */
61 public static $instance = null;
62
63 /**
64 * Get Instance
65 *
66 * Creates public static instance.
67 *
68 * @static
69 *
70 * @return Api_Backups_Admin
71 */
72 public static function get_instance() {
73 if ( null === self::$instance ) {
74 self::$instance = new self();
75 }
76 return self::$instance;
77 }
78
79 /**
80 * Method get_class_name()
81 *
82 * Get Class Name.
83 *
84 * @return string __CLASS__
85 */
86 public static function get_class_name() {
87 return __CLASS__;
88 }
89
90 /**
91 * Constructor
92 *
93 * Runs each time the class is called.
94 */
95 public function __construct() {
96 add_action( 'admin_init', array( &$this, 'admin_init' ) );
97 add_filter( 'mainwp_getprimarybackup_methods', array( &$this, 'hook_getprimarybackup_methods' ) );
98 add_filter( 'mainwp_managesites_getbackuplink', array( &$this, 'hook_managesites_getbackuplink' ), 10, 4 );
99
100 $this->update_check();
101 Api_Backups_Overview::get_instance();
102 Api_Backups_Settings::get_instance();
103 Api_Backups_Handler::get_instance();
104 Api_Backups_Hooks::get_instance();
105 }
106
107 /**
108 * Method admin_init() initiated by init()
109 *
110 * Enqueue scripts and styles.
111 */
112 public function admin_init() {
113 add_filter( 'mainwp_log_specific_actions', array( &$this, 'hook_mainwp_log_specific_actions' ), 10, 2 );
114 Api_Backups_3rd_Party::instance()->init_ajax_actions();
115 $module_url = Api_Backups_Manager::get_location_path( 'url' );
116
117 if ( isset( $_GET['page'] ) && ( 'ManageApiBackups' === $_GET['page'] || 'ManageSitesApiBackups' === $_GET['page'] || 'SettingsApiBackups' === $_GET['page'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
118 // Enqueue needed assets.
119 wp_enqueue_script( 'mainwp-module-api-backups', $module_url . 'assets/js/module-api-backups.js', array(), $this->version, true );
120 wp_enqueue_script( 'mainwp-module-api-backups-manager', $module_url . 'assets/js/module-api-backups-manager.js', array( 'mainwp' ), $this->version, true );
121 wp_enqueue_style( 'mainwp-module-api-backups', $module_url . 'assets/css/module-api-backups.css', array(), $this->version );
122 }
123 }
124
125
126 /**
127 * Method hook_getprimarybackup_methods().
128 *
129 * @param array $input Input values.
130 *
131 * @return array $input
132 */
133 public function hook_getprimarybackup_methods( $input ) {
134 $input[] = array(
135 'value' => 'module-api-backups',
136 'title' => esc_html__( 'API Backups', 'mainwp' ),
137 );
138 return $input;
139 }
140
141 /**
142 * Handle @filter hook_managesites_getbackuplink().
143 *
144 * @param array $input Input values.
145 * @param int $site_id Site id.
146 * @param int $last_primary_backup Last time primary backup.
147 * @param string $primary_backup Primary backup method.
148 *
149 * @return array $input
150 */
151 public function hook_managesites_getbackuplink( $input, $site_id, $last_primary_backup = 0, $primary_backup = '' ) {
152 if ( 'module-api-backups' !== $primary_backup ) {
153 return $input;
154 }
155
156 if ( $site_id ) {
157 if ( ! empty( $last_primary_backup ) ) {
158 $output = Api_Backups_Utility::format_timestamp( Api_Backups_Utility::get_timestamp( $last_primary_backup ) ) . '<br />';
159 } else {
160 $output = '<span class="mainwp-red">Never</span><br/>';
161 }
162
163 if ( mainwp_current_user_have_right( 'dashboard', 'execute_backups' ) ) {
164 $link = sprintf( '<a href="admin.php?page=ManageSitesApiBackups&id=%d">' . __( 'Backup Now', 'it-l10n-mainwp-backupbuddy' ) . '</a>', $site_id );
165 $output .= $link;
166 }
167 return $output;
168 } else {
169 return $input;
170 }
171 }
172
173
174 /**
175 * Method hook_mainwp_log_specific_actions().
176 *
177 * @param array $input Input values.
178 *
179 * @return array $input
180 */
181 public function hook_mainwp_log_specific_actions( $input ) {
182 $input[ MainWP_Logger::API_BACKUPS_LOG_PRIORITY ] = 'API Backups';
183 return $input;
184 }
185
186 /**
187 * Method update_check().
188 *
189 * Handle update db values if needed .
190 */
191 public function update_check() {
192
193 $current_update = get_option( 'mainwp_api_backups_update_version' );
194
195 if ( ( false === $current_update && version_compare( $this->version, '4.2', '<=' ) ) || version_compare( $current_update, '4.1', '<' ) ) {
196 $map_names = array(
197 'vultr' => 'mainwp_vultr_api_key',
198 'gridpane' => 'mainwp_gridpane_api_key',
199 'linode' => 'mainwp_linode_api_key',
200 'digitalocean' => 'mainwp_digitalocean_api_key',
201 'cloudways' => 'mainwp_cloudways_api_key',
202 );
203
204 foreach ( $map_names as $api_name => $old_name ) {
205 $data = get_option( $old_name );
206 if ( ! empty( $data ) ) {
207 $encrypted = Api_Backups_Utility::get_instance()->encrypt_api_keys( $data, false, false, $api_name );
208 if ( is_array( $encrypted ) && ! empty( $encrypted['file_key'] ) ) {
209 update_option( 'mainwp_api_backups_' . $api_name . '_api_key', $encrypted );
210 delete_option( $old_name );
211 }
212 }
213 }
214 update_option( 'mainwp_api_backups_update_version', $this->update_version );
215 }
216 }
217
218 /**
219 * Method init_menu()
220 *
221 * Add Insights Overview sub menu "Insights".
222 */
223 public static function init_menu() {
224 self::$page = add_submenu_page(
225 'mainwp_tab',
226 esc_html__( 'API Backups', 'mainwp' ),
227 '<div class="mainwp-hidden" id="mainwp-api-backups">' . esc_html__( 'API Backups', 'mainwp' ) . '</div>',
228 'read',
229 'ManageApiBackups',
230 array(
231 Api_Backups_Overview::get_instance(),
232 'render_backups_list',
233 )
234 );
235
236 /**
237 * This hook allows you to add extra sub pages to the client page via the 'mainwp_getsubpages_cost_tracker' filter.
238 */
239 self::$subPages = apply_filters( 'mainwp_getsubpages_api_backups', array() );
240
241 if ( isset( self::$subPages ) && is_array( self::$subPages ) ) {
242 foreach ( self::$subPages as $subPage ) {
243 if ( empty( $subPage['slug'] ) || empty( $subPage['callback'] ) ) {
244 continue;
245 }
246 if ( MainWP_Menu::is_disable_menu_item( 3, 'ManageApiBackups' . $subPage['slug'] ) ) {
247 continue;
248 }
249 add_submenu_page( 'mainwp_tab', $subPage['title'], '<div class="mainwp-hidden">' . esc_html( $subPage['title'] ) . '</div>', 'read', 'ManageApiBackups' . $subPage['slug'], $subPage['callback'] );
250 }
251 }
252
253 self::init_left_menu( self::$subPages );
254 }
255
256
257
258 /**
259 * Initiates API Backups menu.
260 *
261 * @param array $subPages API Backups sub pages.
262 */
263 public static function init_left_menu( $subPages ) {
264 MainWP_Menu::add_left_menu(
265 array(
266 'title' => esc_html__( 'API Backups', 'mainwp' ),
267 'parent_key' => 'managesites',
268 'slug' => 'ManageApiBackups',
269 'href' => 'admin.php?page=ManageApiBackups',
270 'icon' => '<i class="pie chart icon"></i>',
271 'desc' => 'API Backups Overview',
272 'nosubmenu' => true,
273 'leftsub_order' => 8,
274 ),
275 1
276 );
277
278 $init_sub_subleftmenu = array();
279
280 MainWP_Menu::init_subpages_left_menu( $subPages, $init_sub_subleftmenu, 'ManageApiBackups', 'ManageApiBackups' );
281
282 foreach ( $init_sub_subleftmenu as $item ) {
283 if ( MainWP_Menu::is_disable_menu_item( 3, $item['slug'] ) ) {
284 continue;
285 }
286 MainWP_Menu::add_left_menu( $item, 2 );
287 }
288 }
289
290 /**
291 * Method render_header()
292 *
293 * Render page header.
294 */
295 public static function render_header() {
296 $params = array(
297 'title' => esc_html__( 'API Backups - BETA', 'mainwp' ),
298 'which' => 'page_api_backups_overview',
299 'wrap_class' => 'mainwp-module-api-backups-content-wrapper',
300 );
301 MainWP_UI::render_top_header( $params );
302 }
303 }
304