PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.3
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.3
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 / class / class-mainwp-post-handler.php

class-mainwp-post-handler.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 5.3, at class/class-mainwp-post-handler.php

1,865 lines 76.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Post Handler.
4 *
5 * @package MainWP/Dashboard
6 */
7
8 namespace MainWP\Dashboard;
9
10 // phpcs:disable Generic.Metrics.CyclomaticComplexity -- complexity.
11
12 /**
13 * Class MainWP_Post_Handler
14 *
15 * @package MainWP\Dashboard
16 *
17 * @uses \MainWP\Dashboard\MainWP_Post_Base_Handler
18 */
19 class MainWP_Post_Handler extends MainWP_Post_Base_Handler { // phpcs:ignore -- NOSONAR - Complex.
20
21 /**
22 * Private static variable to hold the single instance of the class.
23 *
24 * @static
25 *
26 * @var mixed Default null
27 */
28 private static $instance = null;
29
30 /**
31 * Method instance()
32 *
33 * Create a public static instance.
34 *
35 * @static
36 * @return MainWP_Post_Handler
37 */
38 public static function instance() {
39 if ( null === static::$instance ) {
40 static::$instance = new self();
41 }
42 return static::$instance;
43 }
44
45 /**
46 * Initiate all actions.
47 *
48 * @uses \MainWP\Dashboard\MainWP_Post_Page_Handler::get_class_name()
49 */
50 public function init() {
51 // Page: ManageSites.
52 $this->add_action( 'mainwp_notes_save', array( &$this, 'mainwp_notes_save' ) );
53
54 // Page: BulkAddUser.
55 $this->add_action( 'mainwp_bulkadduser', array( &$this, 'mainwp_bulkadduser' ) );
56 $this->add_action( 'mainwp_importuser', array( &$this, 'mainwp_importuser' ) );
57
58 // Widget: RightNow.
59 $this->add_action( 'mainwp_syncerrors_dismiss', array( &$this, 'mainwp_syncerrors_dismiss' ) );
60
61 if ( \mainwp_current_user_can( 'dashboard', 'manage_security_issues' ) ) {
62 // Page: SecurityIssues.
63 $this->add_action( 'mainwp_security_issues_request', array( &$this, 'mainwp_security_issues_request' ) );
64 $this->add_action( 'mainwp_security_issues_fix', array( &$this, 'mainwp_security_issues_fix' ) );
65 $this->add_action( 'mainwp_security_issues_unfix', array( &$this, 'mainwp_security_issues_unfix' ) );
66 }
67
68 $this->add_action( 'mainwp_notice_status_update', array( &$this, 'mainwp_notice_status_update' ) );
69 $this->add_action( 'mainwp_dismiss_activate_notice', array( &$this, 'dismiss_activate_notice' ) );
70 $this->add_action( 'mainwp_status_saving', array( &$this, 'mainwp_status_saving' ) );
71 $this->add_action( 'mainwp_leftmenu_filter_group', array( &$this, 'mainwp_leftmenu_filter_group' ) );
72 $this->add_action( 'mainwp_widgets_order', array( &$this, 'ajax_widgets_order' ) );
73 $this->add_action( 'mainwp_save_settings', array( &$this, 'ajax_mainwp_save_settings' ) );
74 $this->add_action( 'mainwp_guided_tours_option_update', array( &$this, 'ajax_guided_tours_option_update' ) );
75
76 // Page: Recent Posts.
77 if ( \mainwp_current_user_can( 'dashboard', 'manage_posts' ) ) {
78 $this->add_action( 'mainwp_post_unpublish', array( &$this, 'mainwp_post_unpublish' ) );
79 $this->add_action( 'mainwp_post_publish', array( &$this, 'mainwp_post_publish' ) );
80 $this->add_action( 'mainwp_post_trash', array( &$this, 'mainwp_post_trash' ) );
81 $this->add_action( 'mainwp_post_delete', array( &$this, 'mainwp_post_delete' ) );
82 $this->add_action( 'mainwp_post_restore', array( &$this, 'mainwp_post_restore' ) );
83 $this->add_action( 'mainwp_post_approve', array( &$this, 'mainwp_post_approve' ) );
84 }
85 $this->add_action( 'mainwp_post_addmeta', array( MainWP_Post_Page_Handler::get_class_name(), 'ajax_add_meta' ) );
86 // Page: Pages.
87 if ( \mainwp_current_user_can( 'dashboard', 'manage_pages' ) ) {
88 $this->add_action( 'mainwp_page_unpublish', array( &$this, 'mainwp_page_unpublish' ) );
89 $this->add_action( 'mainwp_page_publish', array( &$this, 'mainwp_page_publish' ) );
90 $this->add_action( 'mainwp_page_trash', array( &$this, 'mainwp_page_trash' ) );
91 $this->add_action( 'mainwp_page_delete', array( &$this, 'mainwp_page_delete' ) );
92 $this->add_action( 'mainwp_page_restore', array( &$this, 'mainwp_page_restore' ) );
93 }
94 // Page: Users.
95 $this->add_action( 'mainwp_user_delete', array( &$this, 'mainwp_user_delete' ) );
96 $this->add_action( 'mainwp_user_edit', array( &$this, 'mainwp_user_edit' ) );
97 $this->add_action( 'mainwp_user_update_password', array( &$this, 'mainwp_user_update_password' ) );
98 $this->add_action( 'mainwp_user_update_user', array( &$this, 'mainwp_user_update_user' ) );
99
100 // Page: Posts.
101 $this->add_action( 'mainwp_posts_search', array( &$this, 'mainwp_posts_search' ) );
102 $this->add_action( 'mainwp_get_categories', array( &$this, 'mainwp_get_categories' ) );
103 $this->add_action( 'mainwp_post_get_edit', array( &$this, 'mainwp_post_get_edit' ) );
104 $this->add_action( 'mainwp_post_postingbulk', array( MainWP_Post_Page_Handler::get_class_name(), 'ajax_posting_posts' ) );
105 $this->add_action( 'mainwp_get_sites_of_groups', array( MainWP_Post_Page_Handler::get_class_name(), 'ajax_get_sites_of_groups' ) );
106
107 // Page: Pages.
108 $this->add_action( 'mainwp_pages_search', array( &$this, 'mainwp_pages_search' ) );
109 // Page: User.
110 $this->add_action( 'mainwp_users_search', array( &$this, 'mainwp_users_search' ) );
111
112 $this->add_action( 'mainwp_events_notice_hide', array( &$this, 'mainwp_events_notice_hide' ) );
113 $this->add_action( 'mainwp_showhide_sections', array( &$this, 'mainwp_showhide_sections' ) );
114 $this->add_action( 'mainwp_saving_status', array( &$this, 'mainwp_saving_status' ) );
115 $this->add_action( 'mainwp_autoupdate_and_trust_child', array( &$this, 'mainwp_autoupdate_and_trust_child' ) );
116 $this->add_action( 'mainwp_installation_warning_hide', array( &$this, 'mainwp_installation_warning_hide' ) );
117 $this->add_action( 'mainwp_force_destroy_sessions', array( &$this, 'mainwp_force_destroy_sessions' ) );
118 $this->add_action( 'mainwp_recheck_http', array( &$this, 'ajax_recheck_http' ) );
119 $this->add_action( 'mainwp_ignore_http_response', array( &$this, 'mainwp_ignore_http_response' ) );
120 $this->add_action( 'mainwp_disconnect_site', array( &$this, 'ajax_disconnect_site' ) );
121 $this->add_action( 'mainwp_manage_sites_display_rows', array( &$this, 'ajax_sites_display_rows' ) );
122 $this->add_action( 'mainwp_monitoring_sites_display_rows', array( &$this, 'ajax_monitoring_display_rows' ) );
123
124 $this->add_action_nonce( 'mainwp-common-nonce' );
125
126 // Page: Clients.
127 $this->add_action( 'mainwp_clients_add_client', array( &$this, 'mainwp_clients_add_client' ) );
128 $this->add_action( 'mainwp_clients_delete_client', array( &$this, 'mainwp_clients_delete_client' ) );
129
130 $this->add_action( 'mainwp_clients_save_field', array( &$this, 'mainwp_clients_save_field' ) );
131 $this->add_action( 'mainwp_clients_delete_general_field', array( &$this, 'mainwp_clients_delete_general_field' ) );
132 $this->add_action( 'mainwp_clients_delete_field', array( &$this, 'mainwp_clients_delete_field' ) );
133 $this->add_action( 'mainwp_clients_notes_save', array( &$this, 'mainwp_clients_notes_save' ) );
134 $this->add_action( 'mainwp_clients_suspend_client', array( &$this, 'mainwp_clients_suspend_client' ) );
135 $this->add_action( 'mainwp_refresh_icon', array( &$this, 'ajax_refresh_icon' ) );
136 $this->add_action( 'mainwp_upload_custom_icon', array( &$this, 'ajax_upload_custom_icon' ) );
137 $this->add_action( 'mainwp_select_custom_theme', array( &$this, 'ajax_select_custom_theme' ) );
138 $this->add_action( 'mainwp_site_actions_dismiss', array( &$this, 'ajax_site_actions_dismiss' ) );
139 $this->add_action( 'mainwp_delete_non_mainwp_actions', array( &$this, 'ajax_delete_non_mainwp_actions' ) );
140 $this->add_action( 'mainwp_import_demo_data', array( &$this, 'ajax_import_demo_data' ) );
141 $this->add_action( 'mainwp_delete_demo_data', array( &$this, 'ajax_delete_demo_data' ) );
142 $this->add_action( 'mainwp_prepare_renew_connections', array( MainWP_Connect_Helper::instance(), 'ajax_prepare_renew_connections' ) );
143 $this->add_action( 'mainwp_renew_connections', array( MainWP_Connect_Helper::instance(), 'ajax_renew_connections' ) );
144
145 // Page: managesites.
146 $this->add_action( 'mainwp_save_temp_import_website', array( &$this, 'ajax_save_temp_import_website' ) );
147 $this->add_action( 'mainwp_delete_temp_import_website', array( &$this, 'ajax_delete_temp_import_website' ) );
148 $this->add_action( 'mainwp_import_website_add_client', array( &$this, 'ajax_import_website_add_client' ) );
149 $this->add_action( 'mainwp_import_website_add_client_no_site', array( &$this, 'ajax_import_website_add_client_no_site' ) );
150
151 // Page:: mainwp-setup.
152 $this->add_action( 'mainwp_clients_add_multi_client', array( &$this, 'ajax_clients_add_multi_client' ) );
153 $this->add_action( 'mainwp_increase_connection_security', array( &$this, 'ajax_increase_connection_security' ) );
154 }
155
156 /**
157 * Method add_post_action()
158 *
159 * Add ajax action.
160 *
161 * @param string $action Action to perform.
162 * @param string $callback Callback to perform.
163 */
164 public function add_post_action( $action, $callback ) {
165 $this->add_action( $action, $callback );
166 }
167
168 /**
169 * Method mainwp_installation_warning_hide()
170 *
171 * Hide the installation warning.
172 */
173 public function mainwp_installation_warning_hide() {
174 $this->secure_request( 'mainwp_installation_warning_hide' );
175
176 update_option( 'mainwp_installation_warning_hide_the_notice', 'yes' );
177 die( 'ok' );
178 }
179
180 /**
181 * Method mainwp_users_search()
182 *
183 * Search Post handler,
184 * Page: User.
185 *
186 * @uses \MainWP\Dashboard\MainWP_Cache::init_session()
187 * @uses \MainWP\Dashboard\MainWP_User::render_table()
188 */
189 public function mainwp_users_search() {
190 $this->secure_request( 'mainwp_users_search' );
191 MainWP_Cache::init_session();
192
193 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
194 $role = isset( $_POST['urole'] ) ? sanitize_text_field( wp_unslash( $_POST['urole'] ) ) : '';
195 $groups = isset( $_POST['groups'] ) && is_array( $_POST['groups'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['groups'] ) ) : '';
196 $sites = isset( $_POST['sites'] ) && is_array( $_POST['sites'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['sites'] ) ) : '';
197 $clients = isset( $_POST['clients'] ) && is_array( $_POST['clients'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['clients'] ) ) : '';
198 $search = isset( $_POST['search'] ) ? sanitize_text_field( wp_unslash( $_POST['search'] ) ) : '';
199 // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
200 MainWP_User::render_table( false, $role, $groups, $sites, $search, $clients );
201 die();
202 }
203
204 /**
205 * Method mainwp_posts_search()
206 *
207 * Search Post handler,
208 * Page: Posts.
209 *
210 * @uses \MainWP\Dashboard\MainWP_Cache::init_session()
211 * @uses \MainWP\Dashboard\MainWP_Post::render_table()
212 * @uses \MainWP\Dashboard\MainWP_Utility::update_option()
213 */
214 public function mainwp_posts_search() { // phpcs:ignore -- NOSONAR - complex.
215 $this->secure_request( 'mainwp_posts_search' );
216 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
217 $post_type = ( isset( $_POST['post_type'] ) && 0 < strlen( sanitize_text_field( wp_unslash( $_POST['post_type'] ) ) ) ? sanitize_text_field( wp_unslash( $_POST['post_type'] ) ) : 'post' );
218 if ( isset( $_POST['maximum'] ) ) {
219 MainWP_Utility::update_option( 'mainwp_maximumPosts', isset( $_POST['maximum'] ) ? intval( $_POST['maximum'] ) : 50 );
220 }
221 $keyword = isset( $_POST['keyword'] ) ? sanitize_text_field( wp_unslash( $_POST['keyword'] ) ) : '';
222 $dtsstart = isset( $_POST['dtsstart'] ) ? sanitize_text_field( trim( wp_unslash( $_POST['dtsstart'] ) ) ) : '';
223 $dtsstop = isset( $_POST['dtsstop'] ) ? sanitize_text_field( trim( wp_unslash( $_POST['dtsstop'] ) ) ) : '';
224 $status = isset( $_POST['status'] ) ? sanitize_text_field( wp_unslash( $_POST['status'] ) ) : '';
225 $groups = isset( $_POST['groups'] ) && is_array( $_POST['groups'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['groups'] ) ) : '';
226 $sites = isset( $_POST['sites'] ) && is_array( $_POST['sites'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['sites'] ) ) : '';
227 $clients = isset( $_POST['clients'] ) && is_array( $_POST['clients'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['clients'] ) ) : '';
228 $postId = isset( $_POST['postId'] ) ? sanitize_text_field( wp_unslash( $_POST['postId'] ) ) : '';
229 $userId = isset( $_POST['userId'] ) ? sanitize_text_field( wp_unslash( $_POST['userId'] ) ) : '';
230 $search_on = isset( $_POST['search_on'] ) ? sanitize_text_field( wp_unslash( $_POST['search_on'] ) ) : '';
231 $table_content_only = isset( $_POST['table_content'] ) && wp_unslash( $_POST['table_content'] ) ? true : false;
232 // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
233
234 MainWP_Cache::init_session();
235 if ( $table_content_only ) {
236 MainWP_Post::render_table_body( $keyword, $dtsstart, $dtsstop, $status, $groups, $sites, $postId, $userId, $post_type, $search_on, true, $clients );
237 } else {
238 $params = array(
239 'groups' => $groups,
240 'sites' => $sites,
241 'postId' => $postId,
242 'userId' => $userId,
243 'post_type' => $post_type,
244 'search_on' => $search_on,
245 'clients' => $clients,
246 );
247 MainWP_Post::render_table( false, $keyword, $dtsstart, $dtsstop, $status, $params );
248 }
249 die();
250 }
251 /**
252 * Method mainwp_pages_search()
253 *
254 * Search Post handler,
255 * Page: Pages.
256 *
257 * @uses \MainWP\Dashboard\MainWP_Cache::init_session()
258 * @uses \MainWP\Dashboard\MainWP_Page::render_table()
259 *
260 * @uses \MainWP\Dashboard\MainWP_Utility::update_option()
261 */
262 public function mainwp_pages_search() {
263 $this->secure_request( 'mainwp_pages_search' );
264 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
265 if ( isset( $_POST['maximum'] ) ) {
266 MainWP_Utility::update_option( 'mainwp_maximumPages', intval( $_POST['maximum'] ) ? intval( $_POST['maximum'] ) : 50 );
267 }
268 $keyword = isset( $_POST['keyword'] ) ? sanitize_text_field( wp_unslash( $_POST['keyword'] ) ) : '';
269 $dtsstart = isset( $_POST['dtsstart'] ) ? sanitize_text_field( wp_unslash( $_POST['dtsstart'] ) ) : '';
270 $dtsstop = isset( $_POST['dtsstop'] ) ? sanitize_text_field( wp_unslash( $_POST['dtsstop'] ) ) : '';
271 $status = isset( $_POST['status'] ) ? sanitize_text_field( wp_unslash( $_POST['status'] ) ) : '';
272 $groups = isset( $_POST['groups'] ) && is_array( $_POST['groups'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['groups'] ) ) : '';
273 $sites = isset( $_POST['sites'] ) && is_array( $_POST['sites'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['sites'] ) ) : '';
274 $clients = isset( $_POST['clients'] ) && is_array( $_POST['clients'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['clients'] ) ) : '';
275 $search_on = isset( $_POST['search_on'] ) ? sanitize_text_field( wp_unslash( $_POST['search_on'] ) ) : '';
276 // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
277 MainWP_Cache::init_session();
278
279 $params = array(
280 'groups' => $groups,
281 'sites' => $sites,
282 'search_on' => $search_on,
283 'clients' => $clients,
284 );
285
286 MainWP_Page::render_table( false, $keyword, $dtsstart, $dtsstop, $status, $params );
287 die();
288 }
289
290 /**
291 * Method mainwp_get_categories()
292 *
293 * Get post/page categories.
294 *
295 * @uses \MainWP\Dashboard\MainWP_Post_Page_Handler::get_categories()
296 */
297 public function mainwp_get_categories() {
298 $this->secure_request( 'mainwp_get_categories' );
299 MainWP_Post_Page_Handler::ajax_handle_get_categories();
300 die();
301 }
302
303 /**
304 * Method mainwp_post_get_edit()
305 *
306 * Get post to edit.
307 *
308 * @uses \MainWP\Dashboard\MainWP_Post_Page_Handler::get_post()
309 */
310 public function mainwp_post_get_edit() {
311 $this->secure_request( 'mainwp_post_get_edit' );
312 MainWP_Post_Page_Handler::get_post();
313 die();
314 }
315
316 /**
317 * Method mainwp_user_delete()
318 *
319 * Delete User from Child Site,
320 * Page: Users.
321 *
322 * @uses \MainWP\Dashboard\MainWP_User::delete()
323 */
324 public function mainwp_user_delete() {
325 $this->secure_request( 'mainwp_user_delete' );
326 MainWP_User::delete();
327 }
328
329 /**
330 * Method mainwp_user_edit()
331 *
332 * Edit User from Child Site,
333 * Page: Users.
334 *
335 * @uses \MainWP\Dashboard\MainWP_User::edit()
336 */
337 public function mainwp_user_edit() {
338 $this->secure_request( 'mainwp_user_edit' );
339 MainWP_User::edit();
340 }
341
342 /**
343 * Method mainwp_user_update_password(
344 *
345 * Update User password from Child Site,
346 * Page: Users.
347 *
348 * @uses \MainWP\Dashboard\MainWP_User::update_password()
349 */
350 public function mainwp_user_update_password() {
351 $this->secure_request( 'mainwp_user_update_password' );
352 MainWP_User::update_password();
353 }
354
355 /**
356 * Method mainwp_user_update_user()
357 *
358 * Update User from Child Site,
359 * Page: Users.
360 *
361 * @uses \MainWP\Dashboard\MainWP_User::update_user()
362 */
363 public function mainwp_user_update_user() {
364 $this->secure_request( 'mainwp_user_update_user' );
365 MainWP_User::update_user();
366 }
367
368 /**
369 * Method mainwp_post_unpublish()
370 *
371 * Unpublish post from Child Site,
372 * Page: Recent Posts.
373 *
374 * @uses \MainWP\Dashboard\MainWP_Recent_Posts::unpublish()
375 */
376 public function mainwp_post_unpublish() {
377 $this->secure_request( 'mainwp_post_unpublish' );
378 MainWP_Recent_Posts::unpublish();
379 }
380
381 /**
382 * Method mainwp_post_publish()
383 *
384 * Publish post on Child Site,
385 * Page: Recent Posts.
386 *
387 * @uses \MainWP\Dashboard\MainWP_Recent_Posts::publish()
388 */
389 public function mainwp_post_publish() {
390 $this->secure_request( 'mainwp_post_publish' );
391 MainWP_Recent_Posts::publish();
392 }
393
394 /**
395 * Method mainwp_post_approve()
396 *
397 * Approve post on Child Site,
398 * Page: Recent Posts.
399 *
400 * @uses \MainWP\Dashboard\MainWP_Recent_Posts::approve()
401 */
402 public function mainwp_post_approve() {
403 $this->secure_request( 'mainwp_post_approve' );
404 MainWP_Recent_Posts::approve();
405 }
406
407 /**
408 * Method mainwp_post_trash()
409 *
410 * Trash post on Child Site,
411 * Page: Recent Posts.
412 *
413 * @uses \MainWP\Dashboard\MainWP_Recent_Posts::trash()
414 */
415 public function mainwp_post_trash() {
416 $this->secure_request( 'mainwp_post_trash' );
417
418 MainWP_Recent_Posts::trash();
419 }
420
421 /**
422 * Method mainwp_post_delete()
423 *
424 * Delete post on Child Site,
425 * Page: Recent Posts.
426 *
427 * @uses \MainWP\Dashboard\MainWP_Recent_Posts::delete()
428 */
429 public function mainwp_post_delete() {
430 $this->secure_request( 'mainwp_post_delete' );
431
432 MainWP_Recent_Posts::delete();
433 }
434
435 /**
436 * Method mainwp_post_restore()
437 *
438 * Restore post,
439 * Page: Recent Posts.
440 *
441 * @uses \MainWP\Dashboard\MainWP_Recent_Posts::restore()
442 */
443 public function mainwp_post_restore() {
444 $this->secure_request( 'mainwp_post_restore' );
445
446 MainWP_Recent_Posts::restore();
447 }
448
449 /**
450 * Method mainwp_page_unpublish()
451 *
452 * Unpublish page,
453 * Page: Recent Pages.
454 *
455 * @uses \MainWP\Dashboard\MainWP_Page::unpublish()
456 */
457 public function mainwp_page_unpublish() {
458 $this->secure_request( 'mainwp_page_unpublish' );
459 MainWP_Page::unpublish();
460 }
461
462 /**
463 * Method mainwp_page_publish()
464 *
465 * Publish page,
466 * Page: Recent Pages.
467 *
468 * @uses \MainWP\Dashboard\MainWP_Page::publish()
469 */
470 public function mainwp_page_publish() {
471 $this->secure_request( 'mainwp_page_publish' );
472 MainWP_Page::publish();
473 }
474
475 /**
476 * Method mainwp_page_trash()
477 *
478 * Trash page,
479 * Page: Recent Pages.
480 *
481 * @uses \MainWP\Dashboard\MainWP_Page::trash()
482 */
483 public function mainwp_page_trash() {
484 $this->secure_request( 'mainwp_page_trash' );
485 MainWP_Page::trash();
486 }
487
488 /**
489 * Method mainwp_page_delete()
490 *
491 * Delete page,
492 * Page: Recent Pages.
493 *
494 * @uses \MainWP\Dashboard\MainWP_Page::delete()
495 */
496 public function mainwp_page_delete() {
497 $this->secure_request( 'mainwp_page_delete' );
498 MainWP_Page::delete();
499 }
500
501 /**
502 * Method mainwp_page_restor()
503 *
504 * Restore page,
505 * Page: Recent Pages.
506 *
507 * @uses \MainWP\Dashboard\MainWP_Page::restore()
508 */
509 public function mainwp_page_restore() {
510 $this->secure_request( 'mainwp_page_restore' );
511 MainWP_Page::restore();
512 }
513
514 /**
515 * Method mainwp_notice_status_update()
516 *
517 * Hide after installation notices,
518 * (PHP version, Trust MainWP Child, Multisite Warning and OpenSSL warning).
519 *
520 * @uses \MainWP\Dashboard\MainWP_Utility::update_option()
521 */
522 public function mainwp_notice_status_update() {
523 $this->secure_request( 'mainwp_notice_status_update' );
524
525 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
526 $no_id = isset( $_POST['notice_id'] ) ? sanitize_text_field( wp_unslash( $_POST['notice_id'] ) ) : false;
527 if ( 'mail_failed' === $no_id ) {
528 MainWP_Utility::update_option( 'mainwp_notice_wp_mail_failed', 'hide' );
529 die( 'ok' );
530 }
531 $time_set = isset( $_POST['time_set'] ) && 1 === intval( $_POST['time_set'] ) ? true : false;
532 $this->hide_dashboard_notice_status( $no_id, $time_set );
533 // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
534 die( 1 );
535 }
536
537 /**
538 * Method hide dashboard notice status.
539 *
540 * @param mixed $no_id notice id.
541 * @param mixed $time_set time.
542 * @param bool $hide_noti hide notice.
543 * @return void
544 */
545 public function hide_dashboard_notice_status( $no_id, $time_set = false, $hide_noti = true ) { //phpcs:ignore -- NOSONAR - complexity.
546 /**
547 * Current user global.
548 *
549 * @global string
550 */
551 global $current_user;
552 $user_id = $current_user->ID;
553 if ( $user_id ) {
554 $status = get_user_option( 'mainwp_notice_saved_status' );
555 if ( ! is_array( $status ) ) {
556 $status = array();
557 }
558 if ( ! empty( $no_id ) ) {
559 if ( 'mainwp_secure_priv_key_notice' === $no_id ) {
560 MainWP_Utility::update_option( 'mainwp_not_start_encrypt_keys', 1 ); // to show the button.
561 }
562 if ( $hide_noti ) {
563 if ( $time_set ) {
564 $status[ $no_id ] = time();
565 } else {
566 $status[ $no_id ] = 1;
567 }
568 } elseif ( ! empty( $status[ $no_id ] ) ) {
569 unset( $status[ $no_id ] );
570 }
571 update_user_option( $user_id, 'mainwp_notice_saved_status', $status );
572 }
573 }
574 }
575
576 /**
577 * Method mainwp_status_saving()
578 *
579 * Save last_sync_sites time().
580 */
581 public function mainwp_status_saving() { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR - complexity.
582 $this->secure_request( 'mainwp_status_saving' );
583 $values = get_option( 'mainwp_status_saved_values' );
584
585 if ( ! is_array( $values ) ) {
586 $values = array();
587 }
588
589 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
590 if ( ! isset( $_POST['status'] ) ) {
591 die( -1 );
592 }
593
594 if ( 'last_sync_sites' === $_POST['status'] ) {
595 if ( isset( $_POST['isGlobalSync'] ) && ! empty( $_POST['isGlobalSync'] ) ) {
596 update_option( 'mainwp_last_synced_all_sites', time() );
597
598 /**
599 * Action: mainwp_synced_all_sites
600 *
601 * Fires upon successfull synchronization process.
602 *
603 * @since 3.5.1
604 */
605 do_action( 'mainwp_synced_all_sites' );
606 }
607 die( 'ok' );
608 }
609
610 $status = isset( $_POST['status'] ) ? sanitize_text_field( wp_unslash( $_POST['status'] ) ) : '';
611 $value = isset( $_POST['value'] ) ? sanitize_text_field( wp_unslash( $_POST['value'] ) ) : '';
612 // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
613
614 if ( ! empty( $status ) ) {
615 if ( empty( $value ) ) {
616 if ( isset( $values[ $status ] ) ) {
617 unset( $values[ $status ] );
618 }
619 } else {
620 $values[ $status ] = $value;
621 }
622 update_option( 'mainwp_status_saved_values', $values );
623 }
624
625 die( 'ok' );
626 }
627
628 /**
629 * Method ajax_widget_order()
630 *
631 * Update saved widget order.
632 */
633 public function ajax_widgets_order() { //phpcs:ignore -- NOSONAR - complex.
634
635 $this->secure_request( 'mainwp_widgets_order' );
636 $user = wp_get_current_user();
637 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
638 if ( $user && ! empty( $_POST['page'] ) ) {
639 $page = isset( $_POST['page'] ) ? sanitize_text_field( wp_unslash( $_POST['page'] ) ) : '';
640 $order = isset( $_POST['order'] ) ? sanitize_text_field( wp_unslash( $_POST['order'] ) ) : '';
641 $wgids = isset( $_POST['wgids'] ) ? sanitize_text_field( wp_unslash( $_POST['wgids'] ) ) : '';
642
643 $wgs_orders = array();
644
645 if ( ! empty( $wgids ) ) {
646 $wgids = json_decode( $wgids, true );
647 $order = json_decode( $order, true );
648 if ( is_array( $wgids ) && is_array( $order ) ) {
649 foreach ( $wgids as $idx => $wgid ) {
650 if ( isset( $order[ $idx ] ) ) {
651 $pre = 'widget-';
652 if ( 0 === strpos( $wgid, $pre ) ) {
653 $wgid = substr( $wgid, strlen( $pre ) );
654 }
655 $wgs_orders[ $wgid ] = $order[ $idx ];
656 }
657 }
658 }
659 }
660
661 if ( 'mainwp_page_manageclients' === $page ) {
662 $item_id = isset( $_POST['item_id'] ) ? intval( $_POST['item_id'] ) : 0;
663 $sorted_array = get_user_option( 'mainwp_widgets_sorted_' . strtolower( $page ) );
664 if ( ! empty( $sorted_array ) ) {
665 $sorted_array = json_decode( $sorted_array, true );
666 }
667 if ( ! is_array( $sorted_array ) ) {
668 $sorted_array = array();
669 }
670 $sorted_array[ $item_id ] = $wgs_orders;
671 update_user_option( $user->ID, 'mainwp_widgets_sorted_' . $page, wp_json_encode( $sorted_array ), true );
672 } else {
673 update_user_option( $user->ID, 'mainwp_widgets_sorted_' . $page, wp_json_encode( $wgs_orders ), true );
674 }
675 die( 'ok' );
676 }
677 // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
678 die( -1 );
679 }
680
681 /**
682 * Method ajax_mainwp_save_settings()
683 *
684 * Update saved MainWP Settings.
685 *
686 * @uses \MainWP\Dashboard\MainWP_Utility::update_option()
687 */
688 public function ajax_mainwp_save_settings() {
689 $this->secure_request( 'mainwp_save_settings' );
690 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
691 $name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : '';
692 if ( ! empty( $name ) ) {
693 $option_name = 'mainwp_' . $name;
694 $val = isset( $_POST['value'] ) ? sanitize_text_field( wp_unslash( $_POST['value'] ) ) : '';
695 MainWP_Utility::update_option( $option_name, $val );
696 }
697 // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
698 die( 'ok' );
699 }
700
701 /**
702 * Method ajax_guided_tours_option_update()
703 *
704 * Update saved MainWP Settings.
705 *
706 * @uses \MainWP\Dashboard\MainWP_Utility::update_option()
707 */
708 public function ajax_guided_tours_option_update() {
709 $this->secure_request( 'mainwp_guided_tours_option_update' );
710 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
711 $enable = isset( $_POST['enable'] ) ? intval( $_POST['enable'] ) : 0;
712 // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
713 MainWP_Utility::update_option( 'mainwp_enable_guided_tours', $enable );
714 die( 'ok ' . intval( $enable ) );
715 }
716
717 /**
718 * Method mainwp_leftmenu_filter_group()
719 *
720 * MainWP left menu filter by group.
721 *
722 * @uses \MainWP\Dashboard\MainWP_DB::query()
723 * @uses \MainWP\Dashboard\MainWP_DB::get_websites_by_group_id()
724 * @uses \MainWP\Dashboard\MainWP_DB::fetch_object()
725 * @uses \MainWP\Dashboard\MainWP_DB::free_result()
726 */
727 public function mainwp_leftmenu_filter_group() {
728 $this->secure_request( 'mainwp_leftmenu_filter_group' );
729
730 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
731 $gid = isset( $_POST['group_id'] ) ? intval( $_POST['group_id'] ) : false;
732 // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
733
734 if ( ! empty( $gid ) ) {
735 $ids = '';
736 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_by_group_id( $gid, true ) );
737 while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
738 $ids .= $website->id . ',';
739 }
740 MainWP_DB::free_result( $websites );
741 $ids = rtrim( $ids, ',' );
742 die( $ids ); // phpcs:ignore WordPress.Security.EscapeOutput
743 }
744 die( '' );
745 }
746
747 /**
748 * Method dismiss_activate_notice()
749 *
750 * Dismiss activate notice.
751 */
752 public function dismiss_activate_notice() {
753 $this->secure_request( 'mainwp_dismiss_activate_notice' );
754
755 /**
756 * Current user global.
757 *
758 * @global string
759 */
760 global $current_user;
761 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
762 $user_id = $current_user->ID;
763 $slug = isset( $_POST['slug'] ) ? sanitize_text_field( wp_unslash( $_POST['slug'] ) ) : '';
764 if ( $user_id && ! empty( $slug ) ) {
765 $activate_notices = get_user_option( 'mainwp_hide_activate_notices' );
766 if ( ! is_array( $activate_notices ) ) {
767 $activate_notices = array();
768 }
769
770 $activate_notices[ $slug ] = time();
771 update_user_option( $user_id, 'mainwp_hide_activate_notices', $activate_notices );
772 }
773 // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
774 die( 1 );
775 }
776
777
778
779 /**
780 * Method mainwp_security_issues_request()
781 *
782 * Post handler for,
783 * Page: SecurityIssues.
784 *
785 * @uses \MainWP\Dashboard\MainWP_Exception
786 * @uses \MainWP\Dashboard\MainWP_Security_Issues::fetch_security_issues()
787 */
788 public function mainwp_security_issues_request() {
789 $this->secure_request( 'mainwp_security_issues_request' );
790
791 try {
792 wp_send_json( array( 'result' => MainWP_Security_Issues::fetch_security_issues() ) );
793 } catch ( MainWP_Exception $e ) {
794 die(
795 wp_json_encode(
796 array(
797 'error' => array(
798 'message' => $e->getMessage(),
799 'extra' => $e->get_message_extra(),
800 ),
801 )
802 )
803 );
804 }
805 }
806
807 /**
808 * Method mainwp_security_issues_fix()
809 *
810 * Post handler for 'fix issues',
811 * Page: SecurityIssues.
812 *
813 * @uses \MainWP\Dashboard\MainWP_Exception
814 * @uses \MainWP\Dashboard\MainWP_Security_Issues::fix_security_issue()
815 */
816 public function mainwp_security_issues_fix() {
817 $this->secure_request( 'mainwp_security_issues_fix' );
818
819 try {
820 wp_send_json( array( 'result' => MainWP_Security_Issues::fix_security_issue() ) );
821 } catch ( MainWP_Exception $e ) {
822 die(
823 wp_json_encode(
824 array(
825 'error' => array(
826 'message' => $e->getMessage(),
827 'extra' => $e->get_message_extra(),
828 ),
829 )
830 )
831 );
832 }
833 }
834
835 /**
836 * Method mainwp_security_issues_unfix()
837 *
838 * Post handler for 'unfix issues',
839 * Page: SecurityIssues.
840 *
841 * @uses \MainWP\Dashboard\MainWP_Exception
842 * @uses \MainWP\Dashboard\MainWP_Security_Issues::unfix_security_issue()
843 */
844 public function mainwp_security_issues_unfix() {
845 $this->secure_request( 'mainwp_security_issues_unfix' );
846
847 try {
848 wp_send_json( array( 'result' => MainWP_Security_Issues::unfix_security_issue() ) );
849 } catch ( MainWP_Exception $e ) {
850 die(
851 wp_json_encode(
852 array(
853 'error' => array(
854 'message' => $e->getMessage(),
855 'extra' => $e->get_message_extra(),
856 ),
857 )
858 )
859 );
860 }
861 }
862
863 /**
864 * Method ajax_disconnect_site()
865 *
866 * Disconnect Child Site.
867 *
868 * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed()
869 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
870 */
871 public function ajax_disconnect_site() {
872 $this->secure_request( 'mainwp_disconnect_site' );
873 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
874 $siteid = isset( $_POST['wp_id'] ) ? intval( $_POST['wp_id'] ) : 0;
875 // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
876
877 if ( empty( $siteid ) ) {
878 die( wp_json_encode( array( 'error' => 'Error: site id empty' ) ) );
879 }
880
881 $website = MainWP_DB::instance()->get_website_by_id( $siteid );
882
883 if ( ! $website ) {
884 die( wp_json_encode( array( 'error' => 'Not found site' ) ) );
885 }
886
887 try {
888 $information = MainWP_Connect::fetch_url_authed( $website, 'disconnect' );
889 } catch ( \Exception $e ) {
890 $information = array( 'error' => esc_html__( 'fetch_url_authed exception', 'mainwp' ) );
891 }
892
893 wp_send_json( $information );
894 }
895
896 /**
897 * Method ajax_sites_display_rows()
898 *
899 * Display rows via ajax,
900 * Page: Manage Sites.
901 *
902 * @uses \MainWP\Dashboard\MainWP_Manage_Sites::ajax_optimize_display_rows()
903 */
904 public function ajax_sites_display_rows() {
905 $this->secure_request( 'mainwp_manage_sites_display_rows' );
906 MainWP_Manage_Sites::ajax_optimize_display_rows();
907 }
908
909 /**
910 * Method ajax_monitoring_display_rows()
911 *
912 * Display rows via ajax,
913 * Page: Monitoring Sites.
914 *
915 * @uses \MainWP\Dashboard\MainWP_Monitoring::ajax_optimize_display_rows()
916 */
917 public function ajax_monitoring_display_rows() {
918 $this->secure_request( 'mainwp_monitoring_sites_display_rows' );
919 MainWP_Monitoring::ajax_optimize_display_rows();
920 }
921
922
923 /**
924 * Method mainwp_bulkadduser()
925 *
926 * Bulk Add User for,
927 * Page: BulkAddUser.
928 *
929 * @uses \MainWP\Dashboard\MainWP_User::do_bulk_add()
930 */
931 public function mainwp_bulkadduser() {
932 $this->check_security( 'mainwp_bulkadduser' );
933 MainWP_User::do_bulk_add();
934 die();
935 }
936
937 /**
938 * Method mainwp_importuser()
939 *
940 * Import user.
941 *
942 * @uses \MainWP\Dashboard\MainWP_User::do_import()
943 */
944 public function mainwp_importuser() {
945 $this->secure_request( 'mainwp_importuser' );
946 MainWP_User::do_import();
947 }
948
949 /**
950 * Method mainwp_clients_add_client()
951 *
952 * Add Client for,
953 * Page: BulkAddClient.
954 */
955 public function mainwp_clients_add_client() {
956 $this->check_security( 'mainwp_clients_add_client' );
957 MainWP_Client::add_client();
958 die();
959 }
960
961 /**
962 * Method mainwp_clients_delete_client()
963 *
964 * Add Client for,
965 * Page: BulkAddClient.
966 */
967 public function mainwp_clients_delete_client() {
968 $this->check_security( 'mainwp_clients_delete_client' );
969 $ret = array( 'success' => false );
970 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
971 $client_id = isset( $_POST['clientid'] ) ? intval( $_POST['clientid'] ) : 0;
972 // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
973 if ( $client_id ) {
974 MainWP_DB_Client::instance()->delete_client( $client_id );
975 $ret['success'] = 'SUCCESS';
976 $ret['result'] = esc_html__( 'Client removed successfully.', 'mainwp' );
977 } else {
978 $ret['result'] = esc_html__( 'Client ID empty.', 'mainwp' );
979 }
980
981 echo wp_json_encode( $ret );
982 exit;
983 }
984
985 /**
986 * Method mainwp_clients_save_field()
987 *
988 * Save client custom fields.
989 */
990 public function mainwp_clients_save_field() {
991 $this->check_security( 'mainwp_clients_save_field' );
992 MainWP_Client::save_client_field();
993 die();
994 }
995
996
997 /**
998 * Method mainwp_clients_delete_general_field()
999 *
1000 * Delete client general fields.
1001 */
1002 public function mainwp_clients_delete_general_field() {
1003 $this->check_security( 'mainwp_clients_delete_general_field' );
1004 $ret = array( 'success' => false );
1005 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1006 $field_id = isset( $_POST['field_id'] ) ? intval( $_POST['field_id'] ) : 0;
1007 $client_id = 0; // 0 general fields.
1008 if ( MainWP_DB_Client::instance()->delete_client_field_by( 'field_id', $field_id, $client_id ) ) {
1009 $ret['success'] = true;
1010 }
1011 // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1012 echo wp_json_encode( $ret );
1013 exit;
1014 }
1015
1016 /**
1017 * Method mainwp_clients_delete_field()
1018 *
1019 * Delete client custom fields.
1020 */
1021 public function mainwp_clients_delete_field() {
1022 $this->check_security( 'mainwp_clients_delete_field' );
1023 $ret = array( 'success' => false );
1024 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1025 $field_id = isset( $_POST['field_id'] ) ? intval( $_POST['field_id'] ) : 0;
1026 $client_id = isset( $_POST['client_id'] ) ? intval( $_POST['client_id'] ) : 0; // $client_id > 0, individual token.
1027 // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1028 if ( $client_id && MainWP_DB_Client::instance()->delete_client_field_by( 'field_id', $field_id, $client_id ) ) {
1029 $ret['success'] = true;
1030 }
1031 echo wp_json_encode( $ret );
1032 exit;
1033 }
1034
1035 /**
1036 * Method mainwp_notes_save()
1037 *
1038 * Post handler for save notes on,
1039 * Page: Manage Sites.
1040 *
1041 * @uses \MainWP\Dashboard\MainWP_Manage_Sites_Handler::save_note()
1042 */
1043 public function mainwp_notes_save() {
1044 $this->secure_request( 'mainwp_notes_save' );
1045 MainWP_Manage_Sites_Handler::save_note();
1046 }
1047
1048 /**
1049 * Method mainwp_clients_notes_save()
1050 *
1051 * Post handler for save notes on,
1052 * Page: Manage Sites.
1053 *
1054 * @uses \MainWP\Dashboard\MainWP_Manage_Sites_Handler::save_note()
1055 */
1056 public function mainwp_clients_notes_save() {
1057 $this->secure_request( 'mainwp_clients_notes_save' );
1058 MainWP_Client::save_note();
1059 }
1060
1061 /**
1062 * Method mainwp_clients_suspend_client()
1063 *
1064 * Post handler for suspend client,
1065 * Page: Manage Sites.
1066 *
1067 * @uses \MainWP\Dashboard\MainWP_Manage_Sites_Handler::save_note()
1068 */
1069 public function mainwp_clients_suspend_client() {
1070 $this->secure_request( 'mainwp_clients_suspend_client' );
1071 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1072 $clientid = isset( $_POST['clientid'] ) ? intval( $_POST['clientid'] ) : 0;
1073 $suspended = isset( $_POST['suspend_status'] ) && ! empty( $_POST['suspend_status'] ) ? 1 : 0;
1074 // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1075
1076 if ( empty( $clientid ) ) {
1077 wp_die( 'Error - empty client id!' );
1078 }
1079
1080 $params = array(
1081 'client_id' => $clientid,
1082 'suspended' => $suspended,
1083 );
1084
1085 MainWP_DB_Client::instance()->update_client( $params );
1086
1087 $client = MainWP_DB_Client::instance()->get_wp_client_by( 'client_id', $clientid );
1088
1089 /**
1090 * Fires immediately after update client suspend/unsuspend.
1091 *
1092 * @since 4.5.1.1
1093 *
1094 * @param object $client client data.
1095 * @param bool $suspended true|false.
1096 */
1097 do_action( 'mainwp_client_suspend', $client, $suspended );
1098
1099 MainWP_DB_Client::instance()->suspend_unsuspend_websites_by_client_id( $clientid, $suspended );
1100
1101 wp_die( 'success' );
1102 }
1103
1104 /**
1105 * Method mainwp_syncerrors_dismiss()
1106 *
1107 * Dismiss Sync errors for,
1108 * Widget: RightNow.
1109 *
1110 * @uses \MainWP\Dashboard\MainWP_Updates_Overview::dismiss_sync_errors()
1111 */
1112 public function mainwp_syncerrors_dismiss() {
1113
1114 $this->secure_request( 'mainwp_syncerrors_dismiss' );
1115
1116 try {
1117 die( wp_json_encode( array( 'result' => MainWP_Updates_Overview::dismiss_sync_errors() ) ) );
1118 } catch ( \Exception $e ) {
1119 die( wp_json_encode( array( 'error' => sanitize_text_field( $e->getMessage() ) ) ) );
1120 }
1121 }
1122
1123 /**
1124 * Method mainwp_events_notice_hide()
1125 *
1126 * Hide events notice.
1127 */
1128 public function mainwp_events_notice_hide() {
1129 $this->secure_request( 'mainwp_events_notice_hide' );
1130 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1131 if ( isset( $_POST['notice'] ) ) {
1132 $current_options = get_option( 'mainwp_showhide_events_notice' );
1133 if ( ! is_array( $current_options ) ) {
1134 $current_options = array();
1135 }
1136 if ( 'first_site' === $_POST['notice'] ) {
1137 update_option( 'mainwp_first_site_events_notice', '' );
1138 } elseif ( 'request_reviews1' === $_POST['notice'] ) {
1139 $current_options['request_reviews1'] = 15;
1140 $current_options['request_reviews1_starttime'] = time();
1141 } elseif ( 'request_reviews1_forever' === $_POST['notice'] || 'request_reviews2_forever' === $_POST['notice'] ) {
1142 $current_options['request_reviews1'] = 'forever';
1143 $current_options['request_reviews2'] = 'forever';
1144 } elseif ( 'request_reviews2' === $_POST['notice'] ) {
1145 $current_options['request_reviews2'] = 15;
1146 $current_options['request_reviews2_starttime'] = time();
1147 } elseif ( 'trust_child' === $_POST['notice'] ) {
1148 $current_options['trust_child'] = 1;
1149 } elseif ( 'multi_site' === $_POST['notice'] ) {
1150 $current_options['hide_multi_site_notice'] = 1;
1151 }
1152 update_option( 'mainwp_showhide_events_notice', $current_options );
1153 }
1154 // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1155 die( 'ok' );
1156 }
1157
1158 /**
1159 * Method mainwp_showhide_sections()
1160 *
1161 * Show/Hide sections.
1162 */
1163 public function mainwp_showhide_sections() {
1164 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1165 if ( isset( $_POST['sec'] ) && isset( $_POST['status'] ) ) {
1166 $opts = get_option( 'mainwp_opts_showhide_sections' );
1167 if ( ! is_array( $opts ) ) {
1168 $opts = array();
1169 }
1170 $opts[ sanitize_text_field( wp_unslash( $_POST['sec'] ) ) ] = sanitize_text_field( wp_unslash( $_POST['status'] ) );
1171 update_option( 'mainwp_opts_showhide_sections', $opts );
1172 die( 'ok' );
1173 }
1174 // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1175 die( 'failed' );
1176 }
1177
1178 /**
1179 * Method mainwp_saving_status()
1180 *
1181 * MainWP Saving Status.
1182 */
1183 public function mainwp_saving_status() {
1184 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1185 if ( ! isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( sanitize_key( $_REQUEST['nonce'] ), 'mainwp_ajax' ) ) {
1186 die( esc_html__( 'WP nonce could not be verified. Please reload the page and try again.', 'mainwp' ) );
1187 }
1188 $saving_status = isset( $_POST['saving_status'] ) ? sanitize_text_field( wp_unslash( $_POST['saving_status'] ) ) : false;
1189 if ( ! empty( $saving_status ) ) {
1190 $current_options = get_option( 'mainwp_opts_saving_status' );
1191 if ( ! is_array( $current_options ) ) {
1192 $current_options = array();
1193 }
1194 if ( isset( $_POST['value'] ) ) {
1195 $current_options[ $saving_status ] = sanitize_text_field( wp_unslash( $_POST['value'] ) );
1196 }
1197 update_option( 'mainwp_opts_saving_status', $current_options );
1198 }
1199 // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1200 die( 'ok' );
1201 }
1202
1203 /**
1204 * Method ajax_recheck_http()
1205 *
1206 * Recheck Child Site http status code & message.
1207 *
1208 * @uses \MainWP\Dashboard\MainWP_Connect::check_ignored_http_code()
1209 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
1210 * @uses \MainWP\Dashboard\MainWP_Monitoring_Handler::handle_check_website()
1211 */
1212 public function ajax_recheck_http() {
1213 $this->check_security( 'mainwp_recheck_http' );
1214 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1215 if ( ! isset( $_POST['websiteid'] ) || empty( $_POST['websiteid'] ) ) {
1216 die( -1 );
1217 }
1218
1219 $website = MainWP_DB::instance()->get_website_by_id( intval( $_POST['websiteid'] ) );
1220 if ( empty( $website ) ) {
1221 die( -1 );
1222 }
1223 // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1224
1225 $result = MainWP_Monitoring_Handler::handle_check_website( $website );
1226 $http_code = ( is_array( $result ) && isset( $result['httpCode'] ) ) ? $result['httpCode'] : 0;
1227 $check_result = MainWP_Connect::check_ignored_http_code( $http_code, $website );
1228
1229 if ( is_array( $result ) && isset( $result['new_uptime_status'] ) ) {
1230 $check_result = $result['new_uptime_status'];
1231 }
1232
1233 die(
1234 wp_json_encode(
1235 array(
1236 'httpcode' => esc_html( $http_code ),
1237 'status' => $check_result ? 1 : 0,
1238 )
1239 )
1240 );
1241 }
1242
1243 /**
1244 * Method mainwp_ignore_http_response()
1245 *
1246 * Ignore Child Site https response.
1247 *
1248 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
1249 * @uses \MainWP\Dashboard\MainWP_DB::update_website_values()
1250 */
1251 public function mainwp_ignore_http_response() {
1252 $this->check_security( 'mainwp_ignore_http_response' );
1253 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1254 $siteid = isset( $_POST['websiteid'] ) ? intval( $_POST['websiteid'] ) : false;
1255 // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1256 if ( empty( $siteid ) ) {
1257 die( -1 );
1258 }
1259
1260 $website = MainWP_DB::instance()->get_website_by_id( $siteid );
1261 if ( empty( $website ) ) {
1262 die( -1 );
1263 }
1264
1265 MainWP_DB::instance()->update_website_values( $website->id, array( 'http_response_code' => '-1' ) );
1266 die( wp_json_encode( array( 'ok' => 1 ) ) );
1267 }
1268
1269 /**
1270 * Method mainwp_autoupdate_and_trust_child()
1271 *
1272 * Set MainWP Child Plugin to Trusted & AutoUpdate.
1273 *
1274 * @uses \MainWP\Dashboard\MainWP_Plugins_Handler::trust_plugin()
1275 */
1276 public function mainwp_autoupdate_and_trust_child() {
1277 $this->secure_request( 'mainwp_autoupdate_and_trust_child' );
1278 if ( 1 !== (int) get_option( 'mainwp_pluginAutomaticDailyUpdate' ) ) {
1279 update_option( 'mainwp_pluginAutomaticDailyUpdate', 1 );
1280 }
1281 MainWP_Plugins_Handler::trust_plugin( 'mainwp-child/mainwp-child.php' );
1282 die( 'ok' );
1283 }
1284
1285 /**
1286 * Method mainwp_force_destroy_sessions()
1287 *
1288 * Force destroy sessions.
1289 *
1290 * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed()
1291 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
1292 *
1293 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
1294 */
1295 public function mainwp_force_destroy_sessions() {
1296 $this->secure_request( 'mainwp_force_destroy_sessions' );
1297
1298 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1299 $website_id = ( isset( $_POST['website_id'] ) ? (int) $_POST['website_id'] : 0 );
1300 // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1301 if ( ! MainWP_DB::instance()->get_website_by_id( $website_id ) ) {
1302 die( wp_json_encode( array( 'error' => array( 'message' => esc_html__( 'This website does not exist.', 'mainwp' ) ) ) ) );
1303 }
1304
1305 $website = MainWP_DB::instance()->get_website_by_id( $website_id );
1306 if ( ! MainWP_System_Utility::can_edit_website( $website ) ) {
1307 die( wp_json_encode( array( 'error' => array( 'message' => esc_html__( 'You cannot edit this website.', 'mainwp' ) ) ) ) );
1308 }
1309
1310 try {
1311 $information = MainWP_Connect::fetch_url_authed(
1312 $website,
1313 'settings_tools',
1314 array(
1315 'action' => 'force_destroy_sessions',
1316 )
1317 );
1318 } catch ( \Exception $e ) {
1319 $information = array( 'error' => esc_html__( 'fetch_url_authed exception', 'mainwp' ) );
1320 }
1321
1322 wp_send_json( $information );
1323 }
1324
1325 /**
1326 * Method ajax_refresh_icon()
1327 */
1328 public function ajax_refresh_icon() {
1329 $this->secure_request( 'mainwp_refresh_icon' );
1330 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1331 $slug = isset( $_POST['slug'] ) ? sanitize_text_field( wp_unslash( $_POST['slug'] ) ) : '';
1332 $type = isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : '';
1333 // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1334
1335 if ( empty( $slug ) ) {
1336 wp_die( 'failed' );
1337 }
1338
1339 $fet_icon = MainWP_System_Utility::handle_get_icon( $slug, $type );
1340
1341 if ( ! empty( $fet_icon ) ) {
1342 wp_die( 'success' );
1343 } else {
1344 wp_die( 'failed' );
1345 }
1346 }
1347
1348 /**
1349 * Method ajax_upload_custom_icon()
1350 */
1351 public function ajax_upload_custom_icon() {
1352 $this->secure_request( 'mainwp_upload_custom_icon' );
1353 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1354 $slug = isset( $_POST['slug'] ) ? sanitize_text_field( wp_unslash( $_POST['slug'] ) ) : '';
1355 $type = isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : '';
1356 $delete = isset( $_POST['delete'] ) ? intval( $_POST['delete'] ) : 0;
1357
1358 if ( empty( $slug ) || ( 'plugin' !== $type && 'theme' !== $type ) ) {
1359 wp_die( 'invalid data!' );
1360 }
1361
1362 $sub_folder = '';
1363
1364 if ( 'plugin' === $type ) {
1365 $sub_folder = 'plugin-icons';
1366 } elseif ( 'theme' === $type ) {
1367 $sub_folder = 'theme-icons';
1368 } else {
1369 wp_die( wp_json_encode( array( 'result' => 'invalid' ) ) );
1370 }
1371
1372 if ( $delete ) {
1373 MainWP_System_Utility::update_cached_icons( '', $slug, $type, true );
1374 wp_die( wp_json_encode( array( 'result' => 'success' ) ) );
1375 }
1376
1377 $output = isset( $_FILES['mainwp_upload_icon_uploader'] ) ? MainWP_System_Utility::handle_upload_image( $sub_folder, $_FILES['mainwp_upload_icon_uploader'], 0 ) : null;
1378 // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1379
1380 $uploaded_icon = 'NOTCHANGE';
1381 if ( is_array( $output ) && isset( $output['filename'] ) && ! empty( $output['filename'] ) ) {
1382 $uploaded_icon = $output['filename'];
1383 }
1384
1385 if ( 'NOTCHANGE' !== $uploaded_icon ) {
1386 MainWP_System_Utility::update_cached_icons( $uploaded_icon, $slug, $type, true );
1387 wp_die( wp_json_encode( array( 'result' => 'success' ) ) );
1388 } else {
1389 wp_die( wp_json_encode( array( 'result' => 'failed' ) ) );
1390 }
1391 }
1392
1393 /**
1394 * Method ajax_select_custom_theme()
1395 */
1396 public function ajax_select_custom_theme() {
1397 $this->secure_request( 'mainwp_select_custom_theme' );
1398 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1399 $theme = isset( $_POST['theme'] ) ? sanitize_text_field( wp_unslash( $_POST['theme'] ) ) : '';
1400 // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1401 if ( empty( $theme ) ) {
1402 wp_die( 'failed' );
1403 }
1404
1405 $user = wp_get_current_user();
1406 if ( empty( $user ) || empty( $user->ID ) ) {
1407 wp_die( 'failed' );
1408 }
1409
1410 update_user_option( $user->ID, 'mainwp_selected_theme', $theme );
1411 wp_die( 'success' );
1412 }
1413
1414
1415 /**
1416 * Method ajax_site_actions_dismiss()
1417 */
1418 public function ajax_site_actions_dismiss() {
1419 $this->secure_request( 'mainwp_site_actions_dismiss' );
1420 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1421 $action_id = isset( $_POST['action_id'] ) ? intval( $_POST['action_id'] ) : 0;
1422 // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1423
1424 if ( empty( $action_id ) ) {
1425 wp_die( 'failed' );
1426 }
1427 $update = array(
1428 'dismiss' => 1,
1429 );
1430 MainWP_DB_Site_Actions::instance()->update_action_by_id( $action_id, $update );
1431 wp_die( 'success' );
1432 }
1433
1434 /**
1435 * Method ajax_site_actions_dismiss()
1436 */
1437 public function ajax_delete_non_mainwp_actions() {
1438 $this->secure_request( 'mainwp_delete_non_mainwp_actions' );
1439 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1440 $siteid = isset( $_POST['wp_id'] ) ? intval( $_POST['wp_id'] ) : 0;
1441 if ( empty( $siteid ) ) {
1442 wp_die( wp_json_encode( array( 'error' => 'Empty site ID' ) ) );
1443 }
1444 // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1445 $website = MainWP_DB::instance()->get_website_by_id( $siteid );
1446 $success = false;
1447 $error = '';
1448 try {
1449 $response = MainWP_Connect::fetch_url_authed( $website, 'delete_actions', array( 'del' => 'act' ) );
1450 if ( is_array( $response ) ) {
1451 if ( isset( $response['success'] ) ) {
1452 $success = true;
1453 } elseif ( isset( $response['error'] ) ) {
1454 $error = $response['error'];
1455 }
1456 }
1457 } catch ( \Exception $e ) {
1458 // ok!
1459 }
1460 if ( $success ) {
1461 MainWP_DB_Site_Actions::instance()->delete_action_by( 'wpid', $siteid );
1462 wp_die( wp_json_encode( array( 'success' => 'ok' ) ) );
1463 }
1464
1465 if ( empty( $error ) ) {
1466 $error = esc_html__( 'Undefined error. Please try again.', 'mainwp' );
1467 }
1468 wp_die( wp_json_encode( array( 'error' => $error ) ) );
1469 }
1470
1471 /**
1472 * Method ajax_import_demo_data().
1473 */
1474 public function ajax_import_demo_data() {
1475 $this->secure_request( 'mainwp_import_demo_data' );
1476 $data = MainWP_Demo_Handle::get_instance()->import_data_demo();
1477 MainWP_Utility::update_option( 'mainwp_enable_guided_tours', 1 );
1478 wp_die( wp_json_encode( $data ) );
1479 }
1480
1481 /**
1482 * Method ajax_delete_demo_data().
1483 */
1484 public function ajax_delete_demo_data() {
1485 $this->secure_request( 'mainwp_delete_demo_data' );
1486 $data = MainWP_Demo_Handle::get_instance()->delete_data_demo();
1487 MainWP_Utility::update_option( 'mainwp_enable_guided_tours', 0 );
1488 wp_die( wp_json_encode( $data ) );
1489 }
1490
1491 /**
1492 * Method ajax_save_temp_import_website()
1493 *
1494 * Save temp import website.
1495 *
1496 * @uses MainWP_DB::instance()->get_general_option()
1497 * @uses MainWP_DB::instance()->update_general_option(()
1498 */
1499 public function ajax_save_temp_import_website() {
1500 $this->secure_request( 'mainwp_save_temp_import_website' ); // Check secure.
1501 $error_msg = esc_html__( 'Undefined error. Please try again.', 'mainwp' );
1502 // Get previously saved temporary data (if any).
1503 $values = MainWP_DB::instance()->get_general_option( 'temp_import_sites', 'array' );
1504 $status = $this->mainwp_get_sanitized_post( 'status' );
1505 $index = $this->mainwp_get_sanitized_post( 'row_index', 'intval' ) ?? 1;
1506 if ( empty( $status ) || '' === $index ) {
1507 wp_die( wp_send_json_error( $error_msg ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1508 }
1509
1510 $is_update = false;
1511 if ( 'delete_temp' === $status ) { // Handling remove temp data.
1512 $is_update = $this->mainwp_handle_delete_temp_import_website( $values, $index );
1513 } elseif ( 'save_temp' === $status ) { // Handling save row data.
1514 // Update row data into array.
1515 $row_item = array(
1516 'site_url' => $this->mainwp_get_sanitized_post( 'site_url' ),
1517 'admin_name' => $this->mainwp_get_sanitized_post( 'admin_name' ),
1518 'admin_password' => $this->mainwp_get_sanitized_post( 'admin_password' ),
1519 'site_name' => $this->mainwp_get_sanitized_post( 'site_name' ),
1520 'tag' => $this->mainwp_get_sanitized_post( 'tag' ),
1521 'security_id' => $this->mainwp_get_sanitized_post( 'security_id' ),
1522 'http_username' => $this->mainwp_get_sanitized_post( 'http_username' ),
1523 'http_password' => $this->mainwp_get_sanitized_post( 'http_password' ),
1524 'verify_certificate' => $this->mainwp_get_sanitized_post( 'verify_certificate', 'intval' ) ?? 1,
1525 'ssl_version' => $this->mainwp_get_sanitized_post( 'ssl_version' ) ?? 'auto',
1526 );
1527
1528 $is_update = $this->mainwp_handle_save_temp_import_website( $values, $index, $row_item );
1529 }
1530 // Save data to options table.
1531 if ( $is_update ) {
1532 MainWP_DB::instance()->update_general_option( 'temp_import_sites', $values, 'array' );
1533 wp_die( wp_send_json_success( esc_html( __( 'Row data saved successfully.', 'mainwp' ) ) ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1534 }
1535
1536 // In case of no updates.
1537 wp_die( wp_send_json_error( esc_html( $error_msg ) ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1538 }
1539
1540 /**
1541 * Method mainwp_handle_delete_temp_import_website()
1542 *
1543 * Handling remove temp data.
1544 *
1545 * @param mixed $values temp import sites.
1546 * @param int $index row key.
1547 *
1548 * @return bool true|false.
1549 */
1550 private function mainwp_handle_delete_temp_import_website( &$values, $index ) {
1551 // Check if row data stream exists.
1552 if ( ! empty( $values ) && is_array( $values ) && array_key_exists( $index, $values ) ) {
1553 unset( $values[ $index ] ); // Remove row data.
1554 return true;
1555 }
1556 return false;
1557 }
1558
1559 /**
1560 * Method mainwp_handle_save_temp_import_website()
1561 *
1562 * Create value if empty or array key exists.
1563 *
1564 * @param mixed $values temp import sites.
1565 * @param int $index row key.
1566 * @param array $row_item new row.
1567 *
1568 * @return bool true.
1569 */
1570 private function mainwp_handle_save_temp_import_website( &$values, $index, $row_item ) {
1571 if ( empty( $values ) || ! array_key_exists( $index, $values ) ) {
1572 $index = 0 !== $index ? $index : 0;
1573 $values[ $index ] = $row_item;
1574 } else {
1575 $values[ $index ] = $row_item;
1576 }
1577 return true;
1578 }
1579
1580 /**
1581 * Method ajax_delete_temp_import_website().
1582 *
1583 * Delete data temp if has been added website.
1584 *
1585 * @uses MainWP_DB::instance()->get_general_option().
1586 * @uses MainWP_DB::instance()->update_general_option().
1587 */
1588 public function ajax_delete_temp_import_website() {
1589 $this->secure_request( 'mainwp_delete_temp_import_website' ); // Check secure.
1590 $error_msg = esc_html__( 'Undefined error. Please try again.', 'mainwp' );
1591 // Get option temp import sites data.
1592 $temp_sites = MainWP_DB::instance()->get_general_option( 'temp_import_sites', 'array' );
1593 if ( empty( $temp_sites ) || ! is_array( $temp_sites ) ) { // Check if temp_sites data exists and is an array.
1594 wp_die( wp_send_json_error( esc_html( $error_msg ) ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1595 }
1596
1597 // Get wpurl, wpadmin from POST.
1598 $wp_url = $this->mainwp_get_sanitized_post( 'managesites_add_wpurl' );
1599 $wp_admin = $this->mainwp_get_sanitized_post( 'managesites_add_wpadmin' );
1600 if ( empty( $wp_url ) || empty( $wp_admin ) ) { // Check wpurl, wpadmin has data or empty.
1601 wp_die( wp_send_json_error( esc_html( $error_msg ) ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1602 }
1603 $is_delete = false;
1604
1605 if ( ! empty( $temp_sites ) && is_array( $temp_sites ) ) {
1606 foreach ( $temp_sites as $k_site => $val_site ) { // Loop through data to remove added website.
1607 // Check if row data stream exists.
1608 if ( rtrim( $wp_url, '/' ) === $val_site['site_url'] && $wp_admin === $val_site['admin_name'] ) {
1609 // Remove row data.
1610 unset( $temp_sites[ $k_site ] );
1611 $is_delete = true;
1612 break;
1613 }
1614 }
1615 }
1616
1617 // Save data to options table.
1618 if ( $is_delete ) {
1619 MainWP_DB::instance()->update_general_option( 'temp_import_sites', $temp_sites, 'array' );
1620 wp_die( wp_send_json_success( esc_html__( 'Delete import row successfully.', 'mainwp' ) ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1621 }
1622
1623 // In case of no delete.
1624 wp_die( wp_send_json_error( $error_msg ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1625 }
1626
1627 /**
1628 * Method ajax_import_website_add_client()
1629 *
1630 * Create client.
1631 */
1632 public function ajax_import_website_add_client() {
1633 $this->secure_request( 'mainwp_import_website_add_client' ); // Check secure.
1634 $error_msg = esc_html__( 'Undefined error. Please try again.', 'mainwp' );
1635 try {
1636 // Retrieve client data.
1637 $data = $this->mainwp_get_sanitized_post( 'client' );
1638 $site_id = $this->mainwp_get_sanitized_post( 'site_id' );
1639
1640 if ( empty( $data ) ) {
1641 wp_die( wp_send_json_error( esc_html( $error_msg ) ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1642 }
1643
1644 $client_data = json_decode( $data, true );
1645 // Create client.
1646 $client = $this->mainwp_handle_create_client( $client_data );
1647 if ( $client ) {
1648 // Add groups website and client.
1649 if ( ! empty( $site_id ) ) {
1650 $this->mainwp_handle_selected_sites_for_client( $client, $site_id );
1651 }
1652
1653 wp_die( wp_send_json_success( esc_html__( 'Created client successfully.', 'mainwp' ) ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1654 }
1655 } catch ( \Exception $e ) {
1656 wp_die( wp_send_json_error( sanitize_text_field( $e->getMessage() ) ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1657 }
1658 // In case of no created.
1659 wp_die( wp_send_json_error( esc_html( $error_msg ) ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1660 }
1661
1662 /**
1663 * Method ajax_import_website_add_client_no_site()
1664 *
1665 * Create client dont has a website.
1666 */
1667 public function ajax_import_website_add_client_no_site() {
1668 $this->secure_request( 'mainwp_import_website_add_client_no_site' ); // Check secure.
1669 $error_msg = esc_html__( 'Undefined error. Please try again.', 'mainwp' );
1670 try {
1671 $data = ! empty( $_POST['client'] ) ? rest_sanitize_array( $_POST['client'] ) : array(); //phpcs:ignore -- NonceVerification.
1672
1673 if ( empty( $data ) || ! is_array( $data ) ) {
1674 wp_die( wp_send_json_error( esc_html( $error_msg ) ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1675 }
1676
1677 foreach ( $data as $val_data ) {
1678 $client_data = json_decode( stripslashes( $val_data ), true ); // Decode client data.
1679 $this->mainwp_handle_create_client( $client_data ); // Update or instert new client.
1680 }
1681 } catch ( \Exception $e ) {
1682 wp_die( wp_send_json_error( sanitize_text_field( $e->getMessage() ) ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1683 }
1684 // In case of no created.
1685 wp_die( wp_send_json_error( esc_html( $error_msg ) ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1686 }
1687
1688 /**
1689 * Method ajax_clients_add_multi_client
1690 *
1691 * Create Multi Client form QSW.
1692 */
1693 public function ajax_clients_add_multi_client() {
1694 $error_msg = esc_html__( 'Undefined error. Please try again.', 'mainwp' );
1695 $this->check_security( 'mainwp_clients_add_multi_client' );
1696 $dataes = isset( $_POST['data'] ) ? $_POST['data'] : array(); //phpcs:ignore -- NonceVerification.
1697 try {
1698 if ( ! empty( $dataes ) ) {
1699 foreach ( $dataes as $val_data ) {
1700 $client_data = array(
1701 'image' => '',
1702 'name' => $val_data['client_name'] ?? '',
1703 'address_1' => '',
1704 'address_2' => '',
1705 'city' => '',
1706 'zip' => '',
1707 'state' => '',
1708 'country' => '',
1709 'note' => '',
1710 'selected_icon_info' => 'selected:wordpress;color:#34424d',
1711 'client_email' => $val_data['client_email'] ?? '',
1712 'client_phone' => '',
1713 'client_facebook' => '',
1714 'client_twitter' => '',
1715 'client_instagram' => '',
1716 'client_linkedin' => '',
1717 'suspended' => 0,
1718 'primary_contact_id' => 0,
1719 );
1720
1721 // Create client.
1722 $client = $this->mainwp_handle_create_client( $client_data );
1723
1724 // Create client successfy.
1725 if ( ! empty( $client ) && ! empty( $val_data['website_id'] ) ) {
1726 // Add groups website and client.
1727 $this->mainwp_handle_selected_sites_for_client( $client, $val_data['website_id'] );
1728 }
1729 // Create client contact.
1730 if ( ! empty( $val_data['contacts_field'] ) ) {
1731 $this->mainwp_handle_create_contact_for_client( $client, $val_data['contacts_field'] );
1732 }
1733 }
1734 wp_die( wp_send_json_success( esc_html__( 'Created client successfully.', 'mainwp' ) ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1735 }
1736 } catch ( \Exception $e ) {
1737 wp_die( wp_send_json_error( sanitize_text_field( $e->getMessage() ) ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1738 }
1739
1740 wp_die( wp_send_json_error( esc_html( $error_msg ) ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1741 }
1742
1743 /**
1744 * Method ajax_increase_connection_security
1745 */
1746 public function ajax_increase_connection_security() {
1747 $this->check_security( 'mainwp_increase_connection_security' );
1748 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
1749 while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
1750 // try to decrypt priv key.
1751 $de_privkey = MainWP_Encrypt_Data_Lib::instance()->decrypt_privkey( base64_decode( $website->privkey ), $website->id ); // phpcs:ignore -- NOSONAR - base64_encode trust.
1752 if ( empty( $de_privkey ) ) { // key is not encrypted.
1753 $en_privkey = MainWP_Encrypt_Data_Lib::instance()->encrypt_privkey( base64_decode( $website->privkey ), $website->id, true ); //phpcs:ignore -- NOSONAR - trust - create encrypt priv key for the site.
1754 if ( ! empty( $en_privkey['en_data'] ) ) {
1755 MainWP_DB::instance()->update_website_values(
1756 $website->id,
1757 array(
1758 'privkey' => base64_encode( $en_privkey['en_data'] ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- NOSONAR - base64_encode trust.
1759 )
1760 );
1761 }
1762 }
1763 }
1764 MainWP_DB::free_result( $websites );
1765 $this->hide_dashboard_notice_status( 'mainwp_secure_priv_key_notice' );
1766 delete_option( 'mainwp_not_start_encrypt_keys' );
1767 wp_die( wp_json_encode( array( 'success' => 1 ) ) );
1768 }
1769
1770
1771
1772 /**
1773 * Method mainwp_get_sanitized_post()
1774 *
1775 * Sanitized post field.
1776 *
1777 * @param string $key key to get from POST.
1778 * @param string $callback cleaning method.
1779 *
1780 * @return mixed data value.
1781 */
1782 public function mainwp_get_sanitized_post( $key, $callback = 'sanitize_text_field' ) {
1783 return isset( $_POST[ $key ] ) ? $callback( wp_unslash( $_POST[ $key ] ) ) : ''; //phpcs:ignore -- NonceVerification.
1784 }
1785
1786 /**
1787 * Method mainwp_handle_create_client()
1788 *
1789 * Create new client or return client data.
1790 *
1791 * @uses MainWP_DB_Client::instance()->get_wp_client_by()
1792 * @uses MainWP_DB_Client::instance()->update_client()
1793 *
1794 * @param array $client_data new client creation data.
1795 *
1796 * @return object client data.
1797 */
1798 public function mainwp_handle_create_client( $client_data ) {
1799 // Check if client does not exist then create new one.
1800 $client = MainWP_DB_Client::instance()->get_wp_client_by( 'client_email', $client_data['client_email'], OBJECT );
1801 if ( empty( $client ) ) {
1802 $client = MainWP_DB_Client::instance()->update_client( $client_data ); // Create client.
1803 }
1804
1805 return $client;
1806 }
1807
1808 /**
1809 * Method mainwp_handle_selected_sites_for_client()
1810 *
1811 * Update selected sites for client.
1812 *
1813 * @uses MainWP_DB_Client::instance()->get_websites_by_client_ids()
1814 * @uses MainWP_DB_Client::instance()->update_selected_sites_for_client()
1815 *
1816 * @param object $client client data.
1817 * @param int $website_id id.
1818 */
1819 public function mainwp_handle_selected_sites_for_client( $client, $website_id ) {
1820 $sites_ids = MainWP_DB_Client::instance()->get_websites_by_client_ids( $client->client_id );
1821 $ids = isset( $sites_ids ) && ! empty( $sites_ids ) ? array_column( $sites_ids, 'id' ) : array();
1822 $ids[] = $website_id;
1823 MainWP_DB_Client::instance()->update_selected_sites_for_client( $client->client_id, $ids );
1824 }
1825
1826 /**
1827 * Method mainwp_handle_create_contact_for_client()
1828 *
1829 * Handle create contact for client.
1830 *
1831 * @uses MainWP_DB_Client::instance()->update_client_contact()
1832 * @uses MMainWP_DB_Client::instance()->update_client()
1833 *
1834 * @param object $client client data.
1835 * @param array $contacts_data contacts Data.
1836 */
1837 public function mainwp_handle_create_contact_for_client( $client, $contacts_data ) {
1838 $contacts = array_filter( $contacts_data );
1839 // Check required fields.
1840 if ( ( isset( $contacts['contact_name'] ) && '' !== $contacts['contact_name'] ) && ( isset( $contacts['contact_email'] ) && $contacts['contact_email'] ) ) {
1841 $contact_data = array(
1842 'contact_name' => $contacts['contact_name'] ?? '',
1843 'contact_email' => $contacts['contact_email'] ?? '',
1844 'contact_role' => $contacts['contact_role'] ?? '',
1845 'contact_client_id' => $client->client_id,
1846 'contact_phone' => '',
1847 'facebook' => '',
1848 'twitter' => '',
1849 'instagram' => '',
1850 'linkedin' => '',
1851 'contact_icon_info' => '',
1852 );
1853 $inserted = MainWP_DB_Client::instance()->update_client_contact( $contact_data ); // Create or update contact of client.
1854 // If the update is successful, the client will be updated again.
1855 if ( $inserted ) {
1856 $update = array(
1857 'client_id' => $client->client_id,
1858 'primary_contact_id' => $inserted->contact_id,
1859 );
1860 MainWP_DB_Client::instance()->update_client( $update );
1861 }
1862 }
1863 }
1864 }
1865