PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.2.2
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.2.2
6.2.2 6.2.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 All 155 releases
mainwp / class / class-mainwp-demo-handle.php

class-mainwp-demo-handle.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 6.2.2, at class/class-mainwp-demo-handle.php

610 lines 19.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP Settings Handle
4 *
5 * This Class handles building/Managing the
6 * Settings MainWP DashboardPage & all SubPages.
7 *
8 * @package MainWP/Settings
9 */
10
11 namespace MainWP\Dashboard;
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 /**
19 * Class MainWP_Settings
20 *
21 * @package MainWP\Dashboard
22 */
23 class MainWP_Demo_Handle { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
24
25 //phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
26
27 /**
28 * Protected static variable to hold the single instance of the class.
29 *
30 * @var mixed Default null
31 */
32 protected static $instance = null;
33
34 /**
35 * Protected static variable to hold the single instance of the demo website ids.
36 *
37 * @var mixed Default array().
38 */
39 protected static $demo_website_ids = array();
40 /**
41 * Get Class Name
42 *
43 * @return __CLASS__
44 */
45 public static function get_class_name() {
46 return __CLASS__;
47 }
48
49 /**
50 * Return the single instance of the class.
51 *
52 * @return mixed $instance The single instance of the class.
53 */
54 public static function get_instance() {
55 if ( is_null( static::$instance ) ) {
56 static::$instance = new self();
57 }
58 if ( empty( static::$demo_website_ids ) ) {
59 $added_demo = get_option( 'mainwp_demo_mode_added_ids', array() );
60 if ( is_array( $added_demo ) && ! empty( $added_demo['sites_ids'] ) ) {
61 static::$demo_website_ids = $added_demo['sites_ids'];
62 }
63
64 if ( empty( static::$demo_website_ids ) || ! is_array( static::$demo_website_ids ) ) {
65 static::$demo_website_ids = array( -1 ); // for single assign.
66 }
67 }
68 return static::$instance;
69 }
70
71 /**
72 * Method init_data_demo()
73 *
74 * Handle init data mode.
75 */
76 public function init_data_demo() {
77 if ( isset( $_GET['enable_demo_mode'] ) && 'yes' === $_GET['enable_demo_mode'] ) { // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized.Recommended
78 $this->import_data_demo();
79 MainWP_Utility::update_option( 'mainwp_enable_guided_tours', 1 );
80 ?>
81 <script type="text/javascript">
82 jQuery( document ).ready( function( $ ) {
83 setTimeout(function () {
84 location.href = 'admin.php?page=mainwp_tab&message=enable-demo-mode';
85 }, 100);
86 });
87 </script>
88 <?php
89 }
90 }
91
92 /**
93 *
94 * Handle detect if it's instawp site.
95 */
96 public static function is_instawp_site() {
97 $urlparts = wp_parse_url( home_url() );
98 $domain = $urlparts['host'];
99 return false !== stripos( $domain, 'instawp.xyz' );
100 }
101
102 /**
103 * Method import_data_demo()
104 *
105 * Handle import data demo.
106 *
107 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_wp_file_system()
108 * @uses \MainWP\Dashboard\MainWP_Utility::starts_with()
109 */
110 public function import_data_demo() { //phpcs:ignore -- NOSONAR - complex method.
111
112 $demo_files = array(
113 'demo_mainwp_wp',
114 'demo_mainwp_wp_clients',
115 'demo_mainwp_wp_clients_contacts',
116 'demo_mainwp_wp_options',
117 'demo_mainwp_wp_actions',
118 );
119
120 $wp_data_rows = array();
121 $clients_data_rows = array();
122 $contacts_data_rows = array();
123 $wp_options_data_rows = array();
124 $wp_action_data_rows = array();
125
126 foreach ( $demo_files as $file ) {
127 $file_path = MAINWP_PLUGIN_DIR . 'assets' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'demo' . DIRECTORY_SEPARATOR . 'json' . DIRECTORY_SEPARATOR . $file . '.json';
128 $json_content = '';
129
130 if ( file_exists( $file_path ) ) {
131 ob_start();
132 include_once $file_path; // NOSONAR - WP compatible.
133 $json_content = ob_get_clean();
134 }
135
136 if ( ! empty( $json_content ) ) {
137 $json_content = json_decode( $json_content, true );
138 }
139
140 if ( ! is_array( $json_content ) || ! isset( $json_content['data'] ) ) {
141 continue;
142 }
143
144 if ( 'demo_mainwp_wp_clients' === $file ) {
145 $clients_data_rows = $json_content['data'];
146 } elseif ( 'demo_mainwp_wp' === $file ) {
147 $wp_data_rows = $json_content['data'];
148 } elseif ( 'demo_mainwp_wp_clients_contacts' === $file ) {
149 $contacts_data_rows = $json_content['data'];
150 } elseif ( 'demo_mainwp_wp_options' === $file ) {
151 $wp_options_data_rows = $json_content['data'];
152 } elseif ( 'demo_mainwp_wp_actions' === $file ) {
153 $wp_action_data_rows = $json_content['data'];
154 }
155 }
156
157 $count_inserted = 0;
158 $index = 0;
159 $demo_added_ids = array(
160 'sites_ids' => array(),
161 'clients_ids' => array(),
162 'contacts_ids' => array(),
163 );
164 if ( is_array( $wp_data_rows ) && ! empty( $wp_data_rows ) ) {
165 foreach ( $wp_data_rows as $row ) {
166 $websiteid = $this->add_demo_website( $row );
167 if ( $websiteid ) {
168 ++$count_inserted;
169 $client_id = 0;
170 if ( isset( $clients_data_rows[ $index ] ) ) {
171 $client_id = $this->add_demo_client( $clients_data_rows[ $index ], array( $websiteid ) );
172
173 if ( $client_id && isset( $contacts_data_rows[ $index ] ) ) {
174 $this->add_demo_contact( $contacts_data_rows[ $index ], $client_id );
175 }
176 }
177 $this->add_demo_wp_option( $wp_options_data_rows, $index, $websiteid );
178 $this->add_demo_none_mainwp_action( $wp_action_data_rows, $index, $websiteid );
179 // saved import ids to clear.
180 $demo_added_ids['sites_ids'][] = $websiteid;
181 if ( $client_id ) {
182 $demo_added_ids['clients_ids'][] = $client_id;
183 }
184 }
185 ++$index;
186 }
187 $data['count'] = $index;
188 $data['inserted'] = $count_inserted;
189 update_option( 'mainwp_demo_mode_added_ids', $demo_added_ids );
190 MainWP_Utility::update_option( 'mainwp_setting_demo_mode_enabled', 1 );
191 }
192
193 if ( ! $index ) {
194 $data['error'] = esc_html__( 'Invalid demo data.', 'mainwp' );
195 }
196
197 return $data;
198 }
199
200
201 /**
202 * Method add_demo_client()
203 *
204 * Handle to add demo Client.
205 *
206 * @param array $params Array of data.
207 * @param array $selected_sites client sites ids.
208 */
209 private function add_demo_client( $params, $selected_sites ) {
210
211 if ( isset( $params['client_id'] ) ) {
212 unset( $params['client_id'] );
213 }
214 $params['primary_contact_id'] = 0;
215
216 if ( empty( $params['created'] ) ) {
217 $params['created'] = time();
218 }
219
220 $clientid = 0;
221 try {
222 $inserted = MainWP_DB_Client::instance()->update_client( $params );
223 if ( $inserted ) {
224 $clientid = $inserted->client_id;
225 }
226 } catch ( \Exception $e ) {
227 $clientid = 0;
228 }
229
230 if ( $clientid && $selected_sites ) {
231 MainWP_DB_Client::instance()->update_selected_sites_for_client( $clientid, $selected_sites );
232 }
233 return $clientid;
234 }
235
236 /**
237 * Method add_demo_contact()
238 *
239 * Handle to add demo contact.
240 *
241 * @param array $params Array of data.
242 * @param int $client_id client id.
243 */
244 private function add_demo_contact( $params, $client_id ) {
245 if ( isset( $params['contact_id'] ) ) {
246 unset( $params['contact_id'] );
247 }
248 $params['contact_client_id'] = $client_id;
249 $contact_int = MainWP_DB_Client::instance()->update_client_contact( $params );
250 if ( $contact_int ) {
251 $update = array(
252 'client_id' => $client_id,
253 'primary_contact_id' => $contact_int->contact_id,
254 );
255 MainWP_DB_Client::instance()->update_client( $update );
256 return $contact_int->contact_id;
257 }
258 return false;
259 }
260
261
262
263 /**
264 * Method add_demo_website()
265 *
266 * Handle add demo website data.
267 *
268 * @param array $params Array of demo Site to add.
269 */
270 private function add_demo_website( $params ) {
271 global $wpdb;
272 $userid = get_current_user_id();
273
274 if ( MainWP_Utility::ctype_digit( $userid ) && is_array( $params ) && isset( $params['url'] ) && ! empty( $params['url'] ) && isset( $params['adminname'] ) && ! empty( $params['adminname'] ) ) {
275
276 $existed = MainWP_DB::instance()->get_websites_by_url( $params['url'] );
277
278 if ( ! empty( $existed ) ) {
279 return false;
280 }
281
282 if ( isset( $params['id'] ) ) {
283 unset( $params['id'] );
284 }
285
286 $url = $params['url'];
287
288 if ( '/' !== substr( $url, - 1 ) ) {
289 $url .= '/';
290 }
291
292 $params['url'] = $url;
293 $params['userid'] = $userid;
294 $params['client_id'] = 0; // set: 0.
295
296 $syncValues = array(
297 'dtsSync' => 0,
298 'dtsSyncStart' => 0,
299 'dtsAutomaticSync' => 0,
300 'dtsAutomaticSyncStart' => 0,
301 'totalsize' => 0,
302 'extauth' => '',
303 'sync_errors' => '',
304 );
305
306 if ( $wpdb->insert( MainWP_DB::instance()->get_table_name( 'wp' ), $params ) ) {
307 $websiteid = $wpdb->insert_id;
308 $syncValues['wpid'] = $websiteid;
309 $wpdb->insert( MainWP_DB::instance()->get_table_name( 'wp_sync' ), $syncValues );
310 $wpdb->insert(
311 MainWP_DB::instance()->get_table_name( 'wp_settings_backup' ),
312 array(
313 'wpid' => $websiteid,
314 'archiveFormat' => 'global',
315 )
316 );
317
318 $website = new \stdClass();
319 $website->id = $websiteid;
320
321 $added = time();
322 MainWP_DB::instance()->update_website_option( $website, 'added_timestamp', $added );
323
324 return $websiteid;
325 }
326 }
327
328 return false;
329 }
330
331 /**
332 * Method add_demo_wp_option()
333 *
334 * Handle add demo website options data.
335 *
336 * @param array $data_rows Array of Site options.
337 * @param index $index index of data.
338 * @param index $websiteid website id.
339 */
340 private function add_demo_wp_option( $data_rows, $index, $websiteid ) {
341 global $wpdb;
342 if ( ! is_array( $data_rows ) || empty( $websiteid ) ) {
343 return;
344 }
345
346 $opt_names = array(
347 'recent_posts',
348 'recent_pages',
349 );
350
351 foreach ( $opt_names as $opt_name ) {
352 if ( isset( $data_rows[ $opt_name ] ) && ! empty( $data_rows[ $opt_name ][ $index ] ) ) {
353 $row = $data_rows[ $opt_name ][ $index ];
354 if ( isset( $row['opt_id'] ) ) {
355 unset( $row['opt_id'] );
356 $row['wpid'] = $websiteid;
357 $wpdb->insert( MainWP_DB::instance()->get_table_name( 'wp_options' ), $row );
358 }
359 }
360 }
361 }
362
363 /**
364 * Method add_demo_none_mainwp_action()
365 *
366 * Handle add demo none mainwp actions.
367 *
368 * @param array $data_rows Array of actions.
369 * @param index $index index of data.
370 * @param index $websiteid website id.
371 */
372 private function add_demo_none_mainwp_action( $data_rows, $index, $websiteid ) {
373 global $wpdb;
374 }
375
376 /**
377 * Method delete_data_demo()
378 *
379 * Handle delete demo data .
380 */
381 public function delete_data_demo() {
382 global $wpdb;
383 $demo_data_ids = get_option( 'mainwp_demo_mode_added_ids' );
384
385 if ( is_array( $demo_data_ids ) ) {
386 if ( ! empty( $demo_data_ids['sites_ids'] ) ) {
387 foreach ( $demo_data_ids['sites_ids'] as $itemid ) {
388 MainWP_Manage_Sites_Handler::remove_website( $itemid );
389 }
390 }
391 // to fix issue: change clients of demo site.
392 if ( ! empty( $demo_data_ids['clients_ids'] ) ) {
393 foreach ( $demo_data_ids['clients_ids'] as $itemid ) {
394 MainWP_DB_Client::instance()->delete_client( $itemid );
395 }
396 }
397 }
398 MainWP_Utility::update_option( 'mainwp_setting_demo_mode_enabled', 0 );
399 $this->clear_session_cached_demo_data();
400 return array(
401 'success' => 1,
402 );
403 }
404
405 /**
406 * Method delete_data_demo()
407 *
408 * Handle delete demo data .
409 */
410 public function is_new_instance() {
411 global $wpdb;
412 $cache_key = 'mainwp_is_new_instance';
413 $cached = wp_cache_get( $cache_key );
414
415 if ( false !== $cached ) {
416 return $cached;
417 }
418
419 $table_wp = esc_sql( MainWP_DB::instance()->get_table_name( 'wp' ) );
420 $count = $wpdb->get_var( "SELECT count(id) FROM {$table_wp}" ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Table name cannot be parameterized.
421 if ( $count ) {
422 wp_cache_set( $cache_key, false );
423 return false;
424 }
425
426 $table_clients = esc_sql( MainWP_DB::instance()->get_table_name( 'wp_clients' ) );
427 $count = $wpdb->get_var( "SELECT count(client_id) FROM {$table_clients}" ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Table name cannot be parameterized.
428 if ( $count ) {
429 wp_cache_set( $cache_key, false );
430 return false;
431 }
432
433 $extensions_disabled = MainWP_Extensions_Handler::get_extensions_disabled();
434 $extensions = MainWP_Extensions_Handler::get_extensions();
435 if ( ! empty( $extensions ) && $extensions !== $extensions_disabled ) {
436 wp_cache_set( $cache_key, false );
437 return false;
438 }
439
440 wp_cache_set( $cache_key, true );
441 return true;
442 }
443
444 /**
445 * Method is_demo_website()
446 *
447 * Check if website is demo data.
448 *
449 * @param array $website Site to check.
450 */
451 public function is_demo_website( $website ) {
452 if ( is_numeric( $website ) ) {
453 if ( in_array( $website, static::$demo_website_ids ) ) {
454 return true;
455 }
456 } elseif ( is_object( $website ) && property_exists( $website, 'id' ) && in_array( $website->id, static::$demo_website_ids ) ) {
457 return true;
458 }
459 return false;
460 }
461
462
463 /**
464 * Method get_open_site_demo_url()
465 *
466 * Render open site demo url.
467 *
468 * @param mixed $website Site.
469 */
470 public function get_open_site_demo_url( $website ) {
471 $open_url = '';
472 if ( is_numeric( $website ) ) {
473 $website = MainWP_DB::instance()->get_website_by_id( $website );
474 }
475 if ( is_object( $website ) && property_exists( $website, 'url' ) ) {
476 $open_url = $website->url . 'wp-admin.html';
477 }
478 return $open_url;
479 }
480
481
482 /**
483 * Method is_demo_mode()
484 *
485 * Check if mainwp is demo mode.
486 *
487 * @return bool True if demo mode, else False.
488 */
489 public static function is_demo_mode() {
490 return defined( 'MAINWP_DEMO_MODE' ) && MAINWP_DEMO_MODE;
491 }
492
493 /**
494 * Method render_demo_disable_button()
495 *
496 * Handle render disable demo buttons.
497 *
498 * @param string $content html content.
499 * @param bool $echo_out to echo or return.
500 *
501 * @return mixed Button or echo.
502 */
503 public function render_demo_disable_button( $content, $echo_out = true ) {
504 $button = '<div style="display:inline-block;" data-tooltip="' . $this->get_demo_tooltip() . '" data-inverted="" data-variation="mini" data-position="top right">' . $content . '</div>';
505 if ( $echo_out ) {
506 echo $button; //phpcs:ignore WordPress.Security.EscapeOutput
507 }
508 return $button;
509 }
510
511 /**
512 * Method get_markup_str()
513 *
514 * Get demo markup string.
515 *
516 * @return string markup string.
517 */
518 public function get_markup_str() {
519 return '::[adminwebsitedemo]';
520 }
521
522 /**
523 * Method get_demo_notice()
524 *
525 * Get demo message to notice.
526 */
527 public function get_demo_tooltip() {
528 return esc_html__( 'This function does not work in the demo mode!', 'mainwp' );
529 }
530
531 /**
532 * Method get_demo_notice()
533 *
534 * Get demo message to notice.
535 */
536 public function get_demo_notice() {
537 return '[Demo Website]';
538 }
539
540 /**
541 * Method handle_action_demo()
542 *
543 * Handle action for demo website.
544 *
545 * @param object $pWebsite The demo site.
546 * @param string $what action.
547 */
548 public function handle_action_demo( $pWebsite, $what ) {
549 $output = array(
550 'error' => $this->get_demo_notice(),
551 );
552 if ( 'stats' === $what || 'sync_site' === $what ) {
553 $update_dm = array(
554 'dtsSync' => time(),
555 'sync_errors' => '',
556 );
557 MainWP_DB::instance()->update_website_sync_values( $pWebsite->id, $update_dm );
558 return true;
559 } elseif ( 'perform_install' === $what || 'perform_upload' === $what ) {
560 wp_die( wp_send_json( $output ) ); //phpcs:ignore WordPress.Security.EscapeOutput
561 }
562 return $output;
563 }
564
565 /**
566 * Method handle_fetch_urls_demo()
567 *
568 * Handle action for demo website.
569 *
570 * @param mixed $data action.
571 * @param object $website The demo site.
572 * @param mixed $output output.
573 */
574 public function handle_fetch_urls_demo( $data, $website, &$output ) {
575 unset( $data );
576 if ( $this->is_demo_website( $website ) ) {
577 $output->errors[ $website->id ] = $this->get_demo_notice();
578 }
579 }
580
581 /**
582 * Method clear_session_cached_demo_data()
583 *
584 * Handle clear session cached demo data.
585 */
586 public function clear_session_cached_demo_data() {
587 MainWP_Cache::init_session();
588 $clear = array(
589 'SNThemesAll',
590 'SNThemesAllStatus',
591 'MainWP_PluginsActiveStatus',
592 'MainWP_PluginsActive',
593 'MainWPPluginsSearchResult',
594 'MainWPPluginsSearchContext',
595 'MainWPPluginsSearch',
596 'MainWPThemesSearchResult',
597 'MainWPThemesSearchContext',
598 'MainWPThemesSearch',
599 'MainWPUsersSearchResult',
600 'MainWPUsersSearchContext',
601 'MainWPUsersSearch',
602 );
603 foreach ( $clear as $cl ) {
604 if ( isset( $_SESSION[ $cl ] ) ) {
605 unset( $_SESSION[ $cl ] );
606 }
607 }
608 }
609 }
610