From e31db726071122e297bd272611f59b4d4824874f Mon Sep 17 00:00:00 2001 From: thanghv Date: Tue, 12 May 2020 20:19:58 +0700 Subject: [PATCH] Refactoring --- class/class-mainwp-child-callable.php | 1259 ++++++++++ class/class-mainwp-child-install.php | 150 +- class/class-mainwp-child-posts.php | 662 +++++ class/class-mainwp-child-stats.php | 701 ++++++ class/class-mainwp-child-updates.php | 44 +- class/class-mainwp-child-users.php | 470 ++++ class/class-mainwp-child.php | 3240 +------------------------ class/class-mainwp-debug.php | 8 +- class/class-mainwp-helper.php | 19 +- mainwp-child.php | 2 +- 10 files changed, 3412 insertions(+), 3143 deletions(-) create mode 100644 class/class-mainwp-child-callable.php create mode 100644 class/class-mainwp-child-posts.php create mode 100644 class/class-mainwp-child-stats.php create mode 100644 class/class-mainwp-child-users.php diff --git a/class/class-mainwp-child-callable.php b/class/class-mainwp-child-callable.php new file mode 100644 index 0000000..85e367a --- /dev/null +++ b/class/class-mainwp-child-callable.php @@ -0,0 +1,1259 @@ + 'get_site_stats', + 'upgrade' => 'upgrade_wp', + 'newpost' => 'new_post', + 'deactivate' => 'deactivate', + 'newuser' => 'new_user', + 'newadminpassword' => 'new_admin_password', + 'installplugintheme' => 'install_plugin_theme', + 'upgradeplugintheme' => 'upgrade_plugin_theme', + 'upgradetranslation' => 'upgrade_translation', + 'backup' => 'backup', + 'backup_checkpid' => 'backup_checkpid', + 'cloneinfo' => 'cloneinfo', + 'security' => 'get_security_stats', + 'securityFix' => 'do_security_fix', + 'securityUnFix' => 'do_security_un_fix', + 'post_action' => 'post_action', + 'get_all_posts' => 'get_all_posts', + 'comment_action' => 'comment_action', + 'comment_bulk_action' => 'comment_bulk_action', + 'get_all_comments' => 'get_all_comments', + 'get_all_themes' => 'get_all_themes', + 'theme_action' => 'theme_action', + 'get_all_plugins' => 'get_all_plugins', + 'plugin_action' => 'plugin_action', + 'get_all_pages' => 'get_all_pages', + 'get_all_users' => 'get_all_users', + 'user_action' => 'user_action', + 'search_users' => 'search_users', + 'insert_comment' => 'insert_comment', + 'cancel_scheduled_post' => 'cancel_scheduled_post', + 'serverInformation' => 'server_information', + 'maintenance_site' => 'maintenance_site', + 'keyword_links_action' => 'keyword_links_action', + 'branding_child_plugin' => 'branding_child_plugin', + 'code_snippet' => 'code_snippet', + 'uploader_action' => 'uploader_action', + 'wordpress_seo' => 'wordpress_seo', + 'client_report' => 'client_report', + 'createBackupPoll' => 'backup_poll', + 'page_speed' => 'page_speed', + 'woo_com_status' => 'woo_com_status', + 'links_checker' => 'links_checker', + 'wordfence' => 'wordfence', + 'delete_backup' => 'delete_backup', + 'update_values' => 'update_child_values', + 'ithemes' => 'ithemes', + 'updraftplus' => 'updraftplus', + 'backup_wp' => 'backup_wp', + 'backwpup' => 'backwpup', + 'wp_rocket' => 'wp_rocket', + 'settings_tools' => 'settings_tools', + 'skeleton_key' => 'skeleton_key', + 'custom_post_type' => 'custom_post_type', + 'backup_buddy' => 'backup_buddy', + 'get_site_icon' => 'get_site_icon', + 'vulner_checker' => 'vulner_checker', + 'wp_staging' => 'wp_staging', + 'disconnect' => 'disconnect', + 'time_capsule' => 'time_capsule', + 'extra_excution' => 'extra_execution', // deprecated! + 'extra_execution' => 'extra_execution', + 'wpvivid_backuprestore' => 'wpvivid_backuprestore', + ); + + + private $callableFunctionsNoAuth = array( + 'stats' => 'get_site_stats_no_auth', + ); + + + /** + * Method get_class_name() + * + * Get Class Name. + * + * @return object + */ + public static function get_class_name() { + return __CLASS__; + } + + public function __construct() { + + } + + public static function get_instance() { + if ( null === self::$instance ) { + self::$instance = new self(); + } + return self::$instance; + } + + public function is_callable_function( $func ) { + if ( isset( $this->callableFunctions[ $func ] ) ) + return true; + return false; + } + + public function is_callable_function_no_auth( $func ) { + if ( isset( $this->callableFunctionsNoAuth[ $func ] ) ) + return true; + return false; + } + + public function call_function( $func ) { + if ( $this->is_callable_function( $func )) { + call_user_func( array( $this, $this->callableFunctions[ $func ] ) ); + } + } + + public function call_function_no_auth( $func ) { + if ( $this->is_callable_function_no_auth( $func )) { + call_user_func( array( $this, $this->callableFunctionsNoAuth[ $func ] ) ); + } + } + + public function get_site_stats() { + MainWP_Child_Stats::get_instance()->get_site_stats(); + } + + public function get_site_stats_no_auth() { + MainWP_Child_Stats::get_instance()->get_site_stats_no_auth(); + } + + /** + * Functions to support core functionality + */ + public function install_plugin_theme() { + MainWP_Child_Install::get_instance()->install_plugin_theme(); + } + + public function upgrade_wp() { + MainWP_Child_Updates::get_instance()->upgrade_wp(); + } + + public function upgrade_translation() { + MainWP_Child_Updates::get_instance()->upgrade_translation(); + } + + public function upgrade_plugin_theme() { + MainWP_Child_Updates::get_instance()->upgrade_plugin_theme(); + } + + + public function insert_comment() { + $postId = $_POST['id']; + $comments = maybe_unserialize( base64_decode( $_POST['comments'] ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. + $ids = array(); + foreach ( $comments as $comment ) { + $ids[] = wp_insert_comment( + array( + 'comment_post_ID' => $postId, + 'comment_author' => $comment['author'], + 'comment_content' => $comment['content'], + 'comment_date' => $comment['date'], + ) + ); + } + mainwp_child_helper()->write( $ids ); + } + + public function cancel_scheduled_post() { + global $wpdb; + $postId = $_POST['post_id']; + $cancel_all = $_POST['cancel_all']; + $result = false; + $information = array(); + if ( $postId > 0 ) { + if ( 'yes' === get_post_meta( $postId, '_is_auto_generate_content', true ) ) { + $post = $wpdb->get_row( + $wpdb->prepare( + "SELECT * FROM $wpdb->posts WHERE ID = %d AND post_status = 'future'", + $postId + ) + ); + if ( $post ) { + $result = wp_trash_post( $postId ); + } else { + $result = true; + } + } + if ( ! $result ) { + $information['status'] = 'SUCCESS'; + } + } elseif ( $cancel_all ) { + $post_type = $_POST['post_type']; + $posts = $wpdb->get_results( $wpdb->prepare( "SELECT p.ID FROM $wpdb->posts p JOIN $wpdb->postmeta pm ON p.ID=pm.post_id WHERE p.post_status='future' AND p.post_type = %s AND pm.meta_key = '_is_auto_generate_content' AND pm.meta_value = 'yes' ", $post_type ) ); + $count = 0; + if ( is_array( $posts ) ) { + foreach ( $posts as $post ) { + if ( $post ) { + if ( false !== wp_trash_post( $post->ID ) ) { + $count ++; + + } + } + } + } else { + $posts = array(); + } + + $information['status'] = 'SUCCESS'; + $information['count'] = $count; + } + + mainwp_child_helper()->write( $information ); + } + + public function theme_action() { + MainWP_Child_Install::get_instance()->theme_action(); + } + + public function plugin_action() { + MainWP_Child_Install::get_instance()->plugin_action(); + } + + public function get_all_plugins() { + MainWP_Child_Install::get_instance()->get_all_plugins(); + } + + public function get_all_themes() { + MainWP_Child_Install::get_instance()->get_all_themes(); + } + + public function get_all_users() { + MainWP_Child_Users::get_instance()->get_all_users(); + } + + public function user_action() { + MainWP_Child_Users::get_instance()->user_action(); + } + + public function search_users() { + MainWP_Child_Users::get_instance()->search_users(); + } + + public function get_all_posts() { + MainWP_Child_Posts::get_instance()->get_all_posts(); + } + + public function get_all_pages() { + MainWP_Child_Posts::get_instance()->get_all_pages(); + } + + public function comment_action() { + MainWP_Child_Posts::get_instance()->comment_action(); + } + + public function get_all_comments() { + MainWP_Child_Posts::get_instance()->get_all_comments(); + } + + public function comment_bulk_action() { + MainWP_Child_Posts::get_instance()->comment_bulk_action(); + } + + public function server_information() { + ob_start(); + MainWP_Child_Server_Information::render(); + $output['information'] = ob_get_contents(); + ob_end_clean(); + ob_start(); + MainWP_Child_Server_Information::render_cron(); + $output['cron'] = ob_get_contents(); + ob_end_clean(); + ob_start(); + MainWP_Child_Server_Information::render_error_log_page(); + $output['error'] = ob_get_contents(); + ob_end_clean(); + ob_start(); + MainWP_Child_Server_Information::render_wp_config(); + $output['wpconfig'] = ob_get_contents(); + ob_end_clean(); + ob_start(); + MainWP_Child_Server_Information::renderhtaccess(); + $output['htaccess'] = ob_get_contents(); + ob_end_clean(); + + mainwp_child_helper()->write( $output ); + } + + public function maintenance_site() { + global $wpdb; + $information = array(); + if ( isset( $_POST['action'] ) ) { + if ( 'save_settings' === $_POST['action'] ) { + + if ( isset( $_POST['enable_alert'] ) && '1' === $_POST['enable_alert'] ) { + MainWP_Helper::update_option( 'mainwp_maintenance_opt_alert_404', 1, 'yes' ); + } else { + delete_option( 'mainwp_maintenance_opt_alert_404' ); + } + + if ( isset( $_POST['email'] ) && ! empty( $_POST['email'] ) ) { + MainWP_Helper::update_option( 'mainwp_maintenance_opt_alert_404_email', $_POST['email'], 'yes' ); + } else { + delete_option( 'mainwp_maintenance_opt_alert_404_email' ); + } + $information['result'] = 'SUCCESS'; + mainwp_child_helper()->write( $information ); + + return; + } elseif ( 'clear_settings' === $_POST['action'] ) { + delete_option( 'mainwp_maintenance_opt_alert_404' ); + delete_option( 'mainwp_maintenance_opt_alert_404_email' ); + $information['result'] = 'SUCCESS'; + mainwp_child_helper()->write( $information ); + } + mainwp_child_helper()->write( $information ); + } + + $maint_options = $_POST['options']; + $max_revisions = isset( $_POST['revisions'] ) ? intval( $_POST['revisions'] ) : 0; + + if ( ! is_array( $maint_options ) ) { + $information['status'] = 'FAIL'; + $maint_options = array(); + } + + $performed_what = array(); + + if ( in_array( 'revisions', $maint_options ) ) { + if ( empty( $max_revisions ) ) { + $sql_clean = "DELETE FROM $wpdb->posts WHERE post_type = 'revision'"; + $wpdb->query( $sql_clean ); // phpcs:ignore -- safe sql. + // to fix issue of meta_value short length. + $performed_what[] = 'revisions'; // 'Posts revisions deleted'. + } else { + $results = MainWP_Helper::get_revisions( $max_revisions ); + $count_deleted = MainWP_Helper::delete_revisions( $results, $max_revisions ); + $performed_what[] = 'revisions_max'; // 'Posts revisions deleted'. + } + } + + if ( in_array( 'autodraft', $maint_options ) ) { + $sql_clean = "DELETE FROM $wpdb->posts WHERE post_status = 'auto-draft'"; + $wpdb->query( $sql_clean ); // phpcs:ignore -- safe sql. + $performed_what[] = 'autodraft'; // 'Auto draft posts deleted'. + } + + if ( in_array( 'trashpost', $maint_options ) ) { + $sql_clean = "DELETE FROM $wpdb->posts WHERE post_status = 'trash'"; + $wpdb->query( $sql_clean ); // phpcs:ignore -- safe sql. + $performed_what[] = 'trashpost'; // 'Trash posts deleted'. + } + + if ( in_array( 'spam', $maint_options ) ) { + $sql_clean = "DELETE FROM $wpdb->comments WHERE comment_approved = 'spam'"; + $wpdb->query( $sql_clean ); // phpcs:ignore -- safe sql. + $performed_what[] = 'spam'; // 'Spam comments deleted'. + } + + if ( in_array( 'pending', $maint_options ) ) { + $sql_clean = "DELETE FROM $wpdb->comments WHERE comment_approved = '0'"; + $wpdb->query( $sql_clean ); // phpcs:ignore -- safe sql. + $performed_what[] = 'pending'; // 'Pending comments deleted'. + } + + if ( in_array( 'trashcomment', $maint_options ) ) { + $sql_clean = "DELETE FROM $wpdb->comments WHERE comment_approved = 'trash'"; + $wpdb->query( $sql_clean ); // phpcs:ignore -- safe sql. + $performed_what[] = 'trashcomment'; // 'Trash comments deleted'. + } + + if ( in_array( 'tags', $maint_options ) ) { + $post_tags = get_terms( 'post_tag', array( 'hide_empty' => false ) ); + if ( is_array( $post_tags ) ) { + foreach ( $post_tags as $tag ) { + if ( 0 === $tag->count ) { + wp_delete_term( $tag->term_id, 'post_tag' ); + } + } + } + $performed_what[] = 'tags'; // 'Tags with 0 posts associated deleted'. + } + + if ( in_array( 'categories', $maint_options ) ) { + $post_cats = get_terms( 'category', array( 'hide_empty' => false ) ); + if ( is_array( $post_cats ) ) { + foreach ( $post_cats as $cat ) { + if ( 0 === $cat->count ) { + wp_delete_term( $cat->term_id, 'category' ); + } + } + } + $performed_what[] = 'categories'; // 'Categories with 0 posts associated deleted'. + } + + if ( in_array( 'optimize', $maint_options ) ) { + $this->maintenance_optimize(); + $performed_what[] = 'optimize'; // 'Database optimized'. + } + if ( ! isset( $information['status'] ) ) { + $information['status'] = 'SUCCESS'; + } + + if ( ! empty( $performed_what ) && has_action( 'mainwp_reports_maintenance' ) ) { + $details = implode( ',', $performed_what ); + $log_time = time(); + $message = 'Maintenance Performed'; + $result = 'Maintenance Performed'; + do_action( 'mainwp_reports_maintenance', $message, $log_time, $details, $result, $max_revisions ); + } + + mainwp_child_helper()->write( $information ); + } + + public function maintenance_optimize() { + global $wpdb, $table_prefix; + $sql = 'SHOW TABLE STATUS FROM `' . DB_NAME . '`'; + $result = MainWP_Child_DB::to_query( $sql, $wpdb->dbh ); + if ( MainWP_Child_DB::num_rows( $result ) && MainWP_Child_DB::is_result( $result ) ) { + while ( $row = MainWP_Child_DB::fetch_array( $result ) ) { + if ( strpos( $row['Name'], $table_prefix ) !== false ) { + $sql = 'OPTIMIZE TABLE ' . $row['Name']; + MainWP_Child_DB::to_query( $sql, $wpdb->dbh ); + } + } + } + } + + + public function new_post() { + MainWP_Child_Posts::get_instance()->new_post(); + } + + public function post_action() { + MainWP_Child_Posts::get_instance()->post_action(); + } + + public function new_admin_password() { + MainWP_Child_Users::get_instance()->new_admin_password(); + } + + public function new_user() { + MainWP_Child_Users::get_instance()->new_user(); + } + + public function cloneinfo() { + global $table_prefix; + $information['dbCharset'] = DB_CHARSET; + $information['dbCollate'] = DB_COLLATE; + $information['table_prefix'] = $table_prefix; + $information['site_url'] = get_option( 'site_url' ); + $information['home'] = get_option( 'home' ); + + mainwp_child_helper()->write( $information ); + } + + public function backup_poll() { + $fileNameUID = ( isset( $_POST['fileNameUID'] ) ? $_POST['fileNameUID'] : '' ); + $fileName = ( isset( $_POST['fileName'] ) ? $_POST['fileName'] : '' ); + + if ( 'full' === $_POST['type'] ) { + if ( '' !== $fileName ) { + $backupFile = $fileName; + } else { + $backupFile = 'backup-' . $fileNameUID . '-'; + } + + $dirs = MainWP_Helper::get_mainwp_dir( 'backup' ); + $backupdir = $dirs[0]; + $result = glob( $backupdir . $backupFile . '*' ); + $archiveFile = false; + foreach ( $result as $file ) { + if ( MainWP_Helper::is_archive( $file, $backupFile, '(.*)' ) ) { + $archiveFile = $file; + break; + } + } + if ( false === $archiveFile ) { + mainwp_child_helper()->write( array() ); + } + + mainwp_child_helper()->write( array( 'size' => filesize( $archiveFile ) ) ); + } else { + $backupFile = 'dbBackup-' . $fileNameUID . '-*.sql'; + + $dirs = MainWP_Helper::get_mainwp_dir( 'backup' ); + $backupdir = $dirs[0]; + $result = glob( $backupdir . $backupFile . '*' ); + if ( 0 === count( $result ) ) { + mainwp_child_helper()->write( array() ); + } + + $size = 0; + foreach ( $result as $f ) { + $size += filesize( $f ); + } + mainwp_child_helper()->write( array( 'size' => $size ) ); + exit(); + } + } + + public function backup_checkpid() { + $pid = $_POST['pid']; + + $dirs = MainWP_Helper::get_mainwp_dir( 'backup' ); + $backupdir = $dirs[0]; + + $information = array(); + + /** @var $wp_filesystem WP_Filesystem_Base */ + global $wp_filesystem; + + MainWP_Helper::get_wp_filesystem(); + + $pidFile = trailingslashit( $backupdir ) . 'backup-' . $pid . '.pid'; + $doneFile = trailingslashit( $backupdir ) . 'backup-' . $pid . '.done'; + if ( $wp_filesystem->is_file( $pidFile ) ) { + $time = $wp_filesystem->mtime( $pidFile ); + + $minutes = date( 'i', time() ); // phpcs:ignore -- local time. + $seconds = date( 's', time() ); // phpcs:ignore -- local time. + + $file_minutes = date( 'i', $time ); // phpcs:ignore -- local time. + $file_seconds = date( 's', $time ); // phpcs:ignore -- local time. + + $minuteDiff = $minutes - $file_minutes; + if ( 59 === $minuteDiff ) { + $minuteDiff = 1; + } + $secondsdiff = ( $minuteDiff * 60 ) + $seconds - $file_seconds; + + $file = $wp_filesystem->get_contents( $pidFile ); + $information['file'] = basename( $file ); + if ( $secondsdiff < 80 ) { + $information['status'] = 'busy'; + } else { + $information['status'] = 'stalled'; + } + } elseif ( $wp_filesystem->is_file( $doneFile ) ) { + $file = $wp_filesystem->get_contents( $doneFile ); + $information['status'] = 'done'; + $information['file'] = basename( $file ); + $information['size'] = filesize( $file ); + } else { + $information['status'] = 'invalid'; + } + + mainwp_child_helper()->write( $information ); + } + + public function backup( $pWrite = true ) { + + $timeout = 20 * 60 * 60; + set_time_limit( $timeout ); + ini_set( 'max_execution_time', $timeout ); // phpcs:ignore + MainWP_Helper::end_session(); + + // Cleanup pid files! + $dirs = MainWP_Helper::get_mainwp_dir( 'backup' ); + $backupdir = trailingslashit( $dirs[0] ); + + /** @var $wp_filesystem WP_Filesystem_Base */ + global $wp_filesystem; + + MainWP_Helper::get_wp_filesystem(); + + $files = glob( $backupdir . '*' ); + foreach ( $files as $file ) { + if ( MainWP_Helper::ends_with( $file, '/index.php' ) | MainWP_Helper::ends_with( $file, '/.htaccess' ) ) { + continue; + } + + if ( ( time() - filemtime( $file ) ) > ( 60 * 60 * 3 ) ) { + unlink( $file ); + } + } + + $fileName = ( isset( $_POST['fileUID'] ) ? $_POST['fileUID'] : '' ); + if ( 'full' === $_POST['type'] ) { + $excludes = ( isset( $_POST['exclude'] ) ? explode( ',', $_POST['exclude'] ) : array() ); + $excludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/uploads/mainwp'; + $uploadDir = MainWP_Helper::get_mainwp_dir(); + $uploadDir = $uploadDir[0]; + $excludes[] = str_replace( ABSPATH, '', $uploadDir ); + $excludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/object-cache.php'; + + if ( function_exists( 'posix_uname' ) ) { + $uname = posix_uname(); + if ( is_array( $uname ) && isset( $uname['nodename'] ) ) { + if ( stristr( $uname['nodename'], 'hostgator' ) ) { + if ( ! isset( $_POST['file_descriptors'] ) || '0' == $_POST['file_descriptors'] || $_POST['file_descriptors'] > 1000 ) { + $_POST['file_descriptors'] = 1000; + } + $_POST['file_descriptors_auto'] = 0; + $_POST['loadFilesBeforeZip'] = false; + } + } + } + + $file_descriptors = ( isset( $_POST['file_descriptors'] ) ? $_POST['file_descriptors'] : 0 ); + $file_descriptors_auto = ( isset( $_POST['file_descriptors_auto'] ) ? $_POST['file_descriptors_auto'] : 0 ); + if ( 1 === (int) $file_descriptors_auto ) { + if ( function_exists( 'posix_getrlimit' ) ) { + $result = posix_getrlimit(); + if ( isset( $result['soft openfiles'] ) ) { + $file_descriptors = $result['soft openfiles']; + } + } + } + + $loadFilesBeforeZip = ( isset( $_POST['loadFilesBeforeZip'] ) ? $_POST['loadFilesBeforeZip'] : true ); + + $newExcludes = array(); + foreach ( $excludes as $exclude ) { + $newExcludes[] = rtrim( $exclude, '/' ); + } + + $excludebackup = ( isset( $_POST['excludebackup'] ) && '1' == $_POST['excludebackup'] ); + $excludecache = ( isset( $_POST['excludecache'] ) && '1' == $_POST['excludecache'] ); + $excludezip = ( isset( $_POST['excludezip'] ) && '1' == $_POST['excludezip'] ); + $excludenonwp = ( isset( $_POST['excludenonwp'] ) && '1' == $_POST['excludenonwp'] ); + + if ( $excludebackup ) { + $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/uploads/backupbuddy_backups'; + $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/uploads/backupbuddy_temp'; + $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/uploads/pb_backupbuddy'; + $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/managewp'; + $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/infinitewp'; + $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/backups'; + $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/backups'; + $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/uploads/backwpup*'; + $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/plugins/wp-complete-backup/storage'; + $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/backups'; + $newExcludes[] = '/administrator/backups'; + } + + if ( $excludecache ) { + $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/w3tc-cache'; + $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/w3tc'; + $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/cache/config'; + $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/cache/minify'; + $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/cache/page_enhanced'; + $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/cache/tmp'; + $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/cache/supercache'; + $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/cache/quick-cache'; + $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/hyper-cache/cache'; + $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/cache/all'; + $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/cache/wp-rocket'; + } + + $file = false; + if ( isset( $_POST['f'] ) ) { + $file = $_POST['f']; + } elseif ( isset( $_POST['file'] ) ) { + $file = $_POST['file']; + } + + $ext = 'zip'; + if ( isset( $_POST['ext'] ) ) { + $ext = $_POST['ext']; + } + + $pid = false; + if ( isset( $_POST['pid'] ) ) { + $pid = $_POST['pid']; + } + + $append = ( isset( $_POST['append'] ) && ( '1' == $_POST['append'] ) ); + + $res = MainWP_Backup::get()->create_full_backup( $newExcludes, $fileName, true, true, $file_descriptors, $file, $excludezip, $excludenonwp, $loadFilesBeforeZip, $ext, $pid, $append ); + if ( ! $res ) { + $information['full'] = false; + } else { + $information['full'] = $res['file']; + $information['size'] = $res['filesize']; + } + $information['db'] = false; + } elseif ( 'db' == $_POST['type'] ) { + $ext = 'zip'; + if ( isset( $_POST['ext'] ) ) { + $ext = $_POST['ext']; + } + + $res = $this->backup_db( $fileName, $ext ); + if ( ! $res ) { + $information['db'] = false; + } else { + $information['db'] = $res['file']; + $information['size'] = $res['filesize']; + } + $information['full'] = false; + } else { + $information['full'] = false; + $information['db'] = false; + } + + if ( $pWrite ) { + mainwp_child_helper()->write( $information ); + } + + return $information; + } + + protected function backup_db( $fileName = '', $ext = 'zip' ) { + $dirs = MainWP_Helper::get_mainwp_dir( 'backup' ); + $dir = $dirs[0]; + $timestamp = time(); + + if ( '' !== $fileName ) { + $fileName .= '-'; + } + + $filepath_prefix = $dir . 'dbBackup-' . $fileName . $timestamp; + + $dh = opendir( $dir ); + + if ( $dh ) { + while ( ( $file = readdir( $dh ) ) !== false ) { + if ( '.' !== $file && '..' !== $file && ( preg_match( '/dbBackup-(.*).sql(\.zip|\.tar|\.tar\.gz|\.tar\.bz2|\.tmp)?$/', $file ) ) ) { + unlink( $dir . $file ); + } + } + closedir( $dh ); + } + + $result = MainWP_Backup::get()->create_backup_db( $filepath_prefix, $ext ); + + MainWP_Helper::update_option( 'mainwp_child_last_db_backup_size', filesize( $result['filepath'] ) ); + + return ( ! $result ) ? false : array( + 'timestamp' => $timestamp, + 'file' => basename( $result['filepath'] ), + 'filesize' => filesize( $result['filepath'] ), + ); + } + + public function get_site_icon() { + $information = array(); + $url = $this->get_favicon( true ); + if ( ! empty( $url ) ) { + $information['faviIconUrl'] = $url; + } + mainwp_child_helper()->write( $information ); + } + + public function get_favicon( $parse_page = false ) { + + $favi_url = ''; + $favi = ''; + $site_url = get_option( 'siteurl' ); + if ( substr( $site_url, - 1 ) != '/' ) { + $site_url .= '/'; + } + + if ( function_exists( 'get_site_icon_url' ) && has_site_icon() ) { + $favi = get_site_icon_url(); + $favi_url = $favi; + } + + if ( empty( $favi ) ) { + if ( file_exists( ABSPATH . 'favicon.ico' ) ) { + $favi = 'favicon.ico'; + } elseif ( file_exists( ABSPATH . 'favicon.png' ) ) { + $favi = 'favicon.png'; + } + + if ( ! empty( $favi ) ) { + $favi_url = $site_url . $favi; + } + } + + if ( $parse_page ) { + // try to parse page. + if ( empty( $favi_url ) ) { + $request = wp_remote_get( $site_url, array( 'timeout' => 50 ) ); + $favi = ''; + if ( is_array( $request ) && isset( $request['body'] ) ) { + $preg_str1 = '/(]*)(?:rel="shortcut\s+icon"\s*)(?:[^>]*)?href="([^"]+)"(?:[^>]*)?>)/is'; + $preg_str2 = '/(]*)(?:rel="(?:shortcut\s+)?icon"\s*)(?:[^>]*)?href="([^"]+)"(?:[^>]*)?>)/is'; + + if ( preg_match( $preg_str1, $request['body'], $matches ) ) { + $favi = $matches[2]; + } elseif ( preg_match( $preg_str2, $request['body'], $matches ) ) { + $favi = $matches[2]; + } + } + + if ( ! empty( $favi ) ) { + if ( false === strpos( $favi, 'http' ) ) { + if ( 0 === strpos( $favi, '//' ) ) { + if ( 0 === strpos( $site_url, 'https' ) ) { + $favi_url = 'https:' . $favi; + } else { + $favi_url = 'http:' . $favi; + } + } else { + $favi_url = $site_url . $favi; + } + } else { + $favi_url = $favi; + } + } + } + + if ( ! empty( $favi_url ) ) { + return $favi_url; + } else { + return false; + } + } else { + return $favi_url; + } + } + + public function get_security_stats() { + $information = array(); + + $information['listing'] = ( ! MainWP_Security::prevent_listing_ok() ? 'N' : 'Y' ); + $information['wp_version'] = ( ! MainWP_Security::remove_wp_version_ok() ? 'N' : 'Y' ); + $information['rsd'] = ( ! MainWP_Security::remove_rsd_ok() ? 'N' : 'Y' ); + $information['wlw'] = ( ! MainWP_Security::remove_wlw_ok() ? 'N' : 'Y' ); + $information['db_reporting'] = ( ! MainWP_Security::remove_database_reporting_ok() ? 'N' : 'Y' ); + $information['php_reporting'] = ( ! MainWP_Security::remove_php_reporting_ok() ? 'N' : 'Y' ); + $information['versions'] = ( ! MainWP_Security::remove_scripts_version_ok() || ! MainWP_Security::remove_styles_version_ok() || ! MainWP_Security::remove_generator_version_ok() ? 'N' : 'Y' ); + $information['registered_versions'] = ( MainWP_Security::remove_registered_versions_ok() ? 'Y' : 'N' ); + $information['admin'] = ( MainWP_Security::admin_user_ok() ? 'Y' : 'N' ); + $information['readme'] = ( MainWP_Security::remove_readme_ok() ? 'Y' : 'N' ); + + mainwp_child_helper()->write( $information ); + } + + + public function do_security_fix() { + $sync = false; + if ( 'all' === $_POST['feature'] ) { + $sync = true; + } + + $information = array(); + $security = get_option( 'mainwp_security' ); + if ( ! is_array( $security ) ) { + $security = array(); + } + + if ( 'all' === $_POST['feature'] || 'listing' === $_POST['feature'] ) { + MainWP_Security::prevent_listing(); + $information['listing'] = ( ! MainWP_Security::prevent_listing_ok() ? 'N' : 'Y' ); + } + + if ( 'all' === $_POST['feature'] || 'wp_version' === $_POST['feature'] ) { + $security['wp_version'] = true; + MainWP_Security::remove_wp_version( true ); + $information['wp_version'] = ( ! MainWP_Security::remove_wp_version_ok() ? 'N' : 'Y' ); + } + + if ( 'all' === $_POST['feature'] || 'rsd' === $_POST['feature'] ) { + $security['rsd'] = true; + MainWP_Security::remove_rsd( true ); + $information['rsd'] = ( ! MainWP_Security::remove_rsd_ok() ? 'N' : 'Y' ); + } + + if ( 'all' === $_POST['feature'] || 'wlw' === $_POST['feature'] ) { + $security['wlw'] = true; + MainWP_Security::remove_wlw( true ); + $information['wlw'] = ( ! MainWP_Security::remove_wlw_ok() ? 'N' : 'Y' ); + } + + if ( 'all' === $_POST['feature'] || 'db_reporting' === $_POST['feature'] ) { + MainWP_Security::remove_database_reporting(); + $information['db_reporting'] = ( ! MainWP_Security::remove_database_reporting_ok() ? 'N' : 'Y' ); + } + + if ( 'all' === $_POST['feature'] || 'php_reporting' === $_POST['feature'] ) { + $security['php_reporting'] = true; + MainWP_Security::remove_php_reporting( true ); + $information['php_reporting'] = ( ! MainWP_Security::remove_php_reporting_ok() ? 'N' : 'Y' ); + } + + if ( 'all' === $_POST['feature'] || 'versions' === $_POST['feature'] ) { + $security['scripts_version'] = true; + $security['styles_version'] = true; + $security['generator_version'] = true; + MainWP_Security::remove_generator_version( true ); + $information['versions'] = 'Y'; + } + + if ( 'all' === $_POST['feature'] || 'registered_versions' === $_POST['feature'] ) { + $security['registered_versions'] = true; + $information['registered_versions'] = 'Y'; + } + + if ( 'all' === $_POST['feature'] || 'admin' === $_POST['feature'] ) { + $information['admin'] = ( ! MainWP_Security::admin_user_ok() ? 'N' : 'Y' ); + } + + if ( 'all' === $_POST['feature'] || 'readme' === $_POST['feature'] ) { + $security['readme'] = true; + MainWP_Security::remove_readme( true ); + $information['readme'] = ( MainWP_Security::remove_readme_ok() ? 'Y' : 'N' ); + } + + MainWP_Helper::update_option( 'mainwp_security', $security, 'yes' ); + + if ( $sync ) { + $information['sync'] = MainWP_Child_Stats::get_instance()->get_site_stats( array(), false ); + } + mainwp_child_helper()->write( $information ); + } + + public function do_security_un_fix() { + $information = array(); + + $sync = false; + if ( 'all' === $_POST['feature'] ) { + $sync = true; + } + + $security = get_option( 'mainwp_security' ); + + if ( 'all' === $_POST['feature'] || 'wp_version' === $_POST['feature'] ) { + $security['wp_version'] = false; + $information['wp_version'] = 'N'; + } + + if ( 'all' === $_POST['feature'] || 'rsd' === $_POST['feature'] ) { + $security['rsd'] = false; + $information['rsd'] = 'N'; + } + + if ( 'all' === $_POST['feature'] || 'wlw' === $_POST['feature'] ) { + $security['wlw'] = false; + $information['wlw'] = 'N'; + } + + if ( 'all' === $_POST['feature'] || 'php_reporting' === $_POST['feature'] ) { + $security['php_reporting'] = false; + $information['php_reporting'] = 'N'; + } + + if ( 'all' === $_POST['feature'] || 'versions' === $_POST['feature'] ) { + $security['scripts_version'] = false; + $security['styles_version'] = false; + $security['generator_version'] = false; + $information['versions'] = 'N'; + } + + if ( 'all' === $_POST['feature'] || 'registered_versions' === $_POST['feature'] ) { + $security['registered_versions'] = false; + $information['registered_versions'] = 'N'; + } + if ( 'all' === $_POST['feature'] || 'readme' === $_POST['feature'] ) { + $security['readme'] = false; + $information['readme'] = MainWP_Security::remove_readme_ok(); + } + + MainWP_Helper::update_option( 'mainwp_security', $security, 'yes' ); + + if ( $sync ) { + $information['sync'] = MainWP_Child_Stats::get_instance()->get_site_stats( array(), false ); + } + + mainwp_child_helper()->write( $information ); + } + + public function settings_tools() { + if ( isset( $_POST['action'] ) ) { + switch ( $_POST['action'] ) { + case 'force_destroy_sessions': + if ( 0 === get_current_user_id() ) { + mainwp_child_helper()->write( array( 'error' => __( 'Cannot get user_id', 'mainwp-child' ) ) ); + } + + wp_destroy_all_sessions(); + + $sessions = wp_get_all_sessions(); + + if ( empty( $sessions ) ) { + mainwp_child_helper()->write( array( 'success' => 1 ) ); + } else { + mainwp_child_helper()->write( array( 'error' => __( 'Cannot destroy sessions', 'mainwp-child' ) ) ); + } + break; + + default: + mainwp_child_helper()->write( array( 'error' => __( 'Invalid action', 'mainwp-child' ) ) ); + } + } else { + mainwp_child_helper()->write( array( 'error' => __( 'Missing action', 'mainwp-child' ) ) ); + } + } + + public function skeleton_key() { + MainWP_Child_Skeleton_Key::instance()->action(); + } + + public function custom_post_type() { + MainWP_Custom_Post_Type::instance()->action(); + } + + public function backup_buddy() { + \MainWP_Child_Back_Up_Buddy::instance()->action(); + } + + public function vulner_checker() { + MainWP_Child_Vulnerability_Checker::instance()->action(); + } + + public function time_capsule() { + \MainWP_Child_Timecapsule::instance()->action(); + } + + public function wp_staging() { + MainWP_Child_Staging::instance()->action(); + } + + public function extra_execution() { + $post = $_POST; + $information = array(); + $information = apply_filters( 'mainwp_child_extra_execution', $information, $post ); + mainwp_child_helper()->write( $information ); + } + + + public function uploader_action() { + $file_url = base64_decode( $_POST['url'] ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. + $path = $_POST['path']; + $filename = $_POST['filename']; + $information = array(); + + if ( empty( $file_url ) || empty( $path ) ) { + mainwp_child_helper()->write( $information ); + + return; + } + + if ( strpos( $path, 'wp-content' ) === 0 ) { + $path = basename( WP_CONTENT_DIR ) . substr( $path, 10 ); + } elseif ( strpos( $path, 'wp-includes' ) === 0 ) { + $path = WPINC . substr( $path, 11 ); + } + + if ( '/' === $path ) { + $dir = ABSPATH; + } else { + $path = str_replace( ' ', '-', $path ); + $path = str_replace( '.', '-', $path ); + $dir = ABSPATH . $path; + } + + if ( ! file_exists( $dir ) ) { + if ( false === mkdir( $dir, 0777, true ) ) { + $information['error'] = 'ERRORCREATEDIR'; + mainwp_child_helper()->write( $information ); + + return; + } + } + + try { + $upload = MainWP_Helper::upload_file( $file_url, $dir, $filename ); + if ( null !== $upload ) { + $information['success'] = true; + } + } catch ( \Exception $e ) { + $information['error'] = $e->getMessage(); + } + mainwp_child_helper()->write( $information ); + } + + public function wordpress_seo() { + \MainWP_WordPress_SEO::instance()->action(); + } + + public function client_report() { + MainWP_Client_Report::instance()->action(); + } + + public function page_speed() { + \MainWP_Child_Pagespeed::instance()->action(); + } + + public function woo_com_status() { + \MainWP_Child_WooCommerce_Status::instance()->action(); + } + + public function links_checker() { + \MainWP_Child_Links_Checker::instance()->action(); + } + + public function wordfence() { + \MainWP_Child_Wordfence::instance()->action(); + } + + public function ithemes() { + \MainWP_Child_IThemes_Security::instance()->action(); + } + + + public function updraftplus() { + \MainWP_Child_Updraft_Plus_Backups::instance()->action(); + } + + public function wpvivid_backuprestore() { + \MainWP_Child_WPvivid_BackupRestore::instance()->action(); + } + + public function backup_wp() { + if ( ! version_compare( phpversion(), '5.3', '>=' ) ) { + $error = sprintf( __( 'PHP Version %s is unsupported.', 'mainwp-child' ), phpversion() ); + mainwp_child_helper()->write( array( 'error' => $error ) ); + } + \MainWP_Child_Back_Up_WordPress::instance()->action(); + } + + public function wp_rocket() { + \MainWP_Child_WP_Rocket::instance()->action(); + } + + public function backwpup() { + \MainWP_Child_Back_WP_Up::instance()->action(); + } + + + public function delete_backup() { + $dirs = MainWP_Helper::get_mainwp_dir( 'backup' ); + $backupdir = $dirs[0]; + + $file = $_REQUEST['del']; + + if ( file_exists( $backupdir . $file ) ) { + unlink( $backupdir . $file ); + } + + mainwp_child_helper()->write( array( 'result' => 'ok' ) ); + } + + + public function update_child_values() { + $uniId = isset( $_POST['uniqueId'] ) ? $_POST['uniqueId'] : ''; + MainWP_Helper::update_option( 'mainwp_child_uniqueId', $uniId ); + mainwp_child_helper()->write( array( 'result' => 'ok' ) ); + } + + + + public function keyword_links_action() { + MainWP_Keyword_Links::instance()->action(); + } + + public function branding_child_plugin() { + MainWP_Child_Branding::instance()->action(); + } + + public function code_snippet() { + $action = $_POST['action']; + $information = array( 'status' => 'FAIL' ); + if ( 'run_snippet' === $action || 'save_snippet' === $action ) { + if ( ! isset( $_POST['code'] ) ) { + mainwp_child_helper()->write( $information ); + } + } + $code = stripslashes( $_POST['code'] ); + if ( 'run_snippet' === $action ) { + $information = MainWP_Helper::execute_snippet( $code ); + } elseif ( 'save_snippet' === $action ) { + $type = $_POST['type']; + $slug = $_POST['slug']; + $snippets = get_option( 'mainwp_ext_code_snippets' ); + + if ( ! is_array( $snippets ) ) { + $snippets = array(); + } + + if ( 'C' === $type ) { // save into wp-config file. + if ( false !== $this->snippet_update_wp_config( 'save', $slug, $code ) ) { + $information['status'] = 'SUCCESS'; + } + } else { + $snippets[ $slug ] = $code; + if ( MainWP_Helper::update_option( 'mainwp_ext_code_snippets', $snippets ) ) { + $information['status'] = 'SUCCESS'; + } + } + MainWP_Helper::update_option( 'mainwp_ext_snippets_enabled', true, 'yes' ); + } elseif ( 'delete_snippet' === $action ) { + $type = $_POST['type']; + $slug = $_POST['slug']; + $snippets = get_option( 'mainwp_ext_code_snippets' ); + + if ( ! is_array( $snippets ) ) { + $snippets = array(); + } + if ( 'C' === $type ) { // delete in wp-config file. + if ( false !== $this->snippet_update_wp_config( 'delete', $slug ) ) { + $information['status'] = 'SUCCESS'; + } + } else { + if ( isset( $snippets[ $slug ] ) ) { + unset( $snippets[ $slug ] ); + if ( MainWP_Helper::update_option( 'mainwp_ext_code_snippets', $snippets ) ) { + $information['status'] = 'SUCCESS'; + } + } else { + $information['status'] = 'SUCCESS'; + } + } + } + mainwp_child_helper()->write( $information ); + } + + public function snippet_update_wp_config( $action, $slug, $code = '' ) { + + $config_file = ''; + if ( file_exists( ABSPATH . 'wp-config.php' ) ) { + // The config file resides in ABSPATH. + $config_file = ABSPATH . 'wp-config.php'; + } elseif ( file_exists( dirname( ABSPATH ) . '/wp-config.php' ) && ! file_exists( dirname( ABSPATH ) . '/wp-settings.php' ) ) { + // The config file resides one level above ABSPATH but is not part of another install. + $config_file = dirname( ABSPATH ) . '/wp-config.php'; + } + + if ( ! empty( $config_file ) ) { + $wpConfig = file_get_contents( $config_file ); + + if ( 'delete' === $action ) { + $wpConfig = preg_replace( '/' . PHP_EOL . '{1,2}\/\*\*\*snippet_' . $slug . '\*\*\*\/(.*)\/\*\*\*end_' . $slug . '\*\*\*\/' . PHP_EOL . '/is', '', $wpConfig ); + } elseif ( 'save' === $action ) { + $wpConfig = preg_replace( '/(\$table_prefix *= *[\'"][^\'|^"]*[\'"] *;)/is', '${1}' . PHP_EOL . PHP_EOL . '/***snippet_' . $slug . '***/' . PHP_EOL . $code . PHP_EOL . '/***end_' . $slug . '***/' . PHP_EOL, $wpConfig ); + } + file_put_contents( $config_file, $wpConfig ); + + return true; + } + return false; + } + + public function disconnect() { + global $mainWPChild; + $mainWPChild->deactivation( false ); + mainwp_child_helper()->write( array( 'result' => 'success' ) ); + } + + + // Deactivating child plugin. + public function deactivate() { + global $mainWPChild; + include_once ABSPATH . 'wp-admin/includes/plugin.php'; + deactivate_plugins( $mainWPChild->plugin_slug, true ); + $information = array(); + if ( is_plugin_active( $mainWPChild->plugin_slug ) ) { + MainWP_Helper::error( 'Plugin still active' ); + } + $information['deactivated'] = true; + mainwp_child_helper()->write( $information ); + } + +} diff --git a/class/class-mainwp-child-install.php b/class/class-mainwp-child-install.php index 6a17f62..3656439 100644 --- a/class/class-mainwp-child-install.php +++ b/class/class-mainwp-child-install.php @@ -28,6 +28,154 @@ class MainWP_Child_Install { return self::$instance; } + + public function plugin_action() { + + global $mainWPChild; + + $action = $_POST['action']; + $plugins = explode( '||', $_POST['plugin'] ); + + if ( 'activate' === $action ) { + include_once ABSPATH . '/wp-admin/includes/plugin.php'; + + foreach ( $plugins as $idx => $plugin ) { + if ( $plugin !== $mainWPChild->plugin_slug ) { + $thePlugin = get_plugin_data( $plugin ); + if ( null !== $thePlugin && '' !== $thePlugin ) { + if ( 'quotes-collection/quotes-collection.php' == $plugin ) { + activate_plugin( $plugin, '', false, true ); + } else { + activate_plugin( $plugin ); + } + } + } + } + } elseif ( 'deactivate' === $action ) { + include_once ABSPATH . '/wp-admin/includes/plugin.php'; + + foreach ( $plugins as $idx => $plugin ) { + if ( $plugin !== $mainWPChild->plugin_slug ) { + $thePlugin = get_plugin_data( $plugin ); + if ( null !== $thePlugin && '' !== $thePlugin ) { + deactivate_plugins( $plugin ); + } + } + } + } elseif ( 'delete' === $action ) { + include_once ABSPATH . '/wp-admin/includes/plugin.php'; + if ( file_exists( ABSPATH . '/wp-admin/includes/screen.php' ) ) { + include_once ABSPATH . '/wp-admin/includes/screen.php'; + } + include_once ABSPATH . '/wp-admin/includes/file.php'; + include_once ABSPATH . '/wp-admin/includes/template.php'; + include_once ABSPATH . '/wp-admin/includes/misc.php'; + include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader.php'; + include_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-base.php'; + include_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-direct.php'; + + MainWP_Helper::check_wp_filesystem(); + + $pluginUpgrader = new Plugin_Upgrader(); + + $all_plugins = get_plugins(); + foreach ( $plugins as $idx => $plugin ) { + if ( $plugin !== $mainWPChild->plugin_slug ) { + if ( isset( $all_plugins[ $plugin ] ) ) { + if ( is_plugin_active( $plugin ) ) { + $thePlugin = get_plugin_data( $plugin ); + if ( null !== $thePlugin && '' !== $thePlugin ) { + deactivate_plugins( $plugin ); + } + } + $tmp['plugin'] = $plugin; + if ( true === $pluginUpgrader->delete_old_plugin( null, null, null, $tmp ) ) { + $args = array( + 'action' => 'delete', + 'Name' => $all_plugins[ $plugin ]['Name'], + ); + do_action( 'mainwp_child_plugin_action', $args ); + } + } + } + } + } else { + $information['status'] = 'FAIL'; + } + + if ( ! isset( $information['status'] ) ) { + $information['status'] = 'SUCCESS'; + } + $information['sync'] = MainWP_Child_Stats::get_instance()->get_site_stats( array(), false ); + mainwp_child_helper()->write( $information ); + } + + public function theme_action() { + + $action = $_POST['action']; + $theme = $_POST['theme']; + + if ( 'activate' === $action ) { + include_once ABSPATH . '/wp-admin/includes/theme.php'; + $theTheme = wp_get_theme( $theme ); + if ( null !== $theTheme && '' !== $theTheme ) { + switch_theme( $theTheme['Template'], $theTheme['Stylesheet'] ); + } + } elseif ( 'delete' === $action ) { + include_once ABSPATH . '/wp-admin/includes/theme.php'; + if ( file_exists( ABSPATH . '/wp-admin/includes/screen.php' ) ) { + include_once ABSPATH . '/wp-admin/includes/screen.php'; + } + include_once ABSPATH . '/wp-admin/includes/file.php'; + include_once ABSPATH . '/wp-admin/includes/template.php'; + include_once ABSPATH . '/wp-admin/includes/misc.php'; + include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader.php'; + include_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-base.php'; + include_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-direct.php'; + + MainWP_Helper::check_wp_filesystem(); + + $themeUpgrader = new Theme_Upgrader(); + + $theme_name = wp_get_theme()->get( 'Name' ); + $themes = explode( '||', $theme ); + + if ( count( $themes ) == 1 ) { + $themeToDelete = current( $themes ); + if ( $themeToDelete == $theme_name ) { + $information['error'] = 'IsActivatedTheme'; + mainwp_child_helper()->write( $information ); + return; + } + } + + foreach ( $themes as $idx => $themeToDelete ) { + if ( $themeToDelete !== $theme_name ) { + $theTheme = wp_get_theme( $themeToDelete ); + if ( null !== $theTheme && '' !== $theTheme ) { + $tmp['theme'] = $theTheme['Template']; + if ( true === $themeUpgrader->delete_old_theme( null, null, null, $tmp ) ) { + $args = array( + 'action' => 'delete', + 'Name' => $theTheme['Name'], + ); + do_action( 'mainwp_child_theme_action', $args ); + } + } + } + } + } else { + $information['status'] = 'FAIL'; + } + + if ( ! isset( $information['status'] ) ) { + $information['status'] = 'SUCCESS'; + } + + $information['sync'] = MainWP_Child_Stats::get_instance()->get_site_stats( array(), false ); + mainwp_child_helper()->write( $information ); + } + /** * Functions to support core functionality @@ -155,6 +303,6 @@ class MainWP_Child_Install { $information['installation'] = 'SUCCESS'; $information['destination_name'] = $result['destination_name']; mainwp_child_helper()->write( $information ); - } + } } diff --git a/class/class-mainwp-child-posts.php b/class/class-mainwp-child-posts.php new file mode 100644 index 0000000..344ce24 --- /dev/null +++ b/class/class-mainwp-child-posts.php @@ -0,0 +1,662 @@ +comments_and_clauses = ''; + $this->posts_where_suffix = ''; + } + + public static function get_instance() { + if ( null === self::$instance ) { + self::$instance = new self(); + } + + return self::$instance; + } + + + + public function get_recent_posts( $pAllowedStatuses, $pCount, $type = 'post', $extra = null ) { + $allPosts = array(); + if ( null !== $pAllowedStatuses ) { + foreach ( $pAllowedStatuses as $status ) { + $this->get_recent_posts_int( $status, $pCount, $type, $allPosts, $extra ); + } + } else { + $this->get_recent_posts_int( 'any', $pCount, $type, $allPosts, $extra ); + } + + return $allPosts; + } + + public function get_recent_posts_int( $status, $pCount, $type = 'post', &$allPosts, $extra = null ) { + $args = array( + 'post_status' => $status, + 'suppress_filters' => false, + 'post_type' => $type, + ); + + $tokens = array(); + if ( is_array( $extra ) && isset( $extra['tokens'] ) ) { + $tokens = $extra['tokens']; + if ( 1 == $extra['extract_post_type'] ) { + $args['post_type'] = 'post'; + } elseif ( 2 == $extra['extract_post_type'] ) { + $args['post_type'] = 'page'; + } elseif ( 3 == $extra['extract_post_type'] ) { + $args['post_type'] = array( 'post', 'page' ); + } + } + $tokens = array_flip( $tokens ); + + if ( 0 !== $pCount ) { + $args['numberposts'] = $pCount; + } + + /* + * + * Credits + * + * Plugin-Name: Yoast SEO + * Plugin URI: https://yoast.com/wordpress/plugins/seo/#utm_source=wpadmin&utm_medium=plugin&utm_campaign=wpseoplugin + * Author: Team Yoast + * Author URI: https://yoast.com/ + * Licence: GPL v3 + * + * The code is used for the MainWP WordPress SEO Extension + * Extension URL: https://mainwp.com/extension/wordpress-seo/ + * + */ + + $wp_seo_enabled = false; + if ( isset( $_POST['WPSEOEnabled'] ) && $_POST['WPSEOEnabled'] ) { + if ( is_plugin_active( 'wordpress-seo/wp-seo.php' ) && class_exists( 'WPSEO_Link_Column_Count' ) && class_exists( 'WPSEO_Meta' ) ) { + $wp_seo_enabled = true; + } + } + + $posts = get_posts( $args ); + if ( is_array( $posts ) ) { + if ( $wp_seo_enabled ) { + $post_ids = array(); + foreach ( $posts as $post ) { + $post_ids[] = $post->ID; + } + $link_count = new WPSEO_Link_Column_Count(); + $link_count->set( $post_ids ); + } + foreach ( $posts as $post ) { + $outPost = array(); + $outPost['id'] = $post->ID; + $outPost['post_type'] = $post->post_type; + $outPost['status'] = $post->post_status; + $outPost['title'] = $post->post_title; + $outPost['comment_count'] = $post->comment_count; + if ( isset( $extra['where_post_date'] ) && ! empty( $extra['where_post_date'] ) ) { + $outPost['dts'] = strtotime( $post->post_date_gmt ); + } else { + $outPost['dts'] = strtotime( $post->post_modified_gmt ); + } + + if ( 'future' == $post->post_status ) { + $outPost['dts'] = strtotime( $post->post_date_gmt ); + } + + $usr = get_user_by( 'id', $post->post_author ); + $outPost['author'] = ! empty( $usr ) ? $usr->user_nicename : 'removed'; + $categoryObjects = get_the_category( $post->ID ); + $categories = ''; + foreach ( $categoryObjects as $cat ) { + if ( '' !== $categories ) { + $categories .= ', '; + } + $categories .= $cat->name; + } + $outPost['categories'] = $categories; + + $tagObjects = get_the_tags( $post->ID ); + $tags = ''; + if ( is_array( $tagObjects ) ) { + foreach ( $tagObjects as $tag ) { + if ( '' !== $tags ) { + $tags .= ', '; + } + $tags .= $tag->name; + } + } + $outPost['tags'] = $tags; + + if ( is_array( $tokens ) ) { + if ( isset( $tokens['[post.url]'] ) ) { + $outPost['[post.url]'] = get_permalink( $post->ID ); + } + if ( isset( $tokens['[post.website.url]'] ) ) { + $outPost['[post.website.url]'] = get_site_url(); + } + if ( isset( $tokens['[post.website.name]'] ) ) { + $outPost['[post.website.name]'] = get_bloginfo( 'name' ); + } + } + + if ( $wp_seo_enabled ) { + $post_id = $post->ID; + $outPost['seo_data'] = array( + 'count_seo_links' => $link_count->get( $post_id, 'internal_link_count' ), + 'count_seo_linked' => $link_count->get( $post_id, 'incoming_link_count' ), + 'seo_score' => \MainWP_WordPress_SEO::instance()->parse_column_score( $post_id ), + 'readability_score' => \MainWP_WordPress_SEO::instance()->parse_column_score_readability( $post_id ), + ); + } + + $allPosts[] = $outPost; + } + } + } + + + public function get_all_posts() { + $post_type = ( isset( $_POST['post_type'] ) ? $_POST['post_type'] : 'post' ); + $this->get_all_posts_by_type( $post_type ); + } + + public function get_all_pages() { + $this->get_all_posts_by_type( 'page' ); + } + + public function posts_where( $where ) { + if ( $this->posts_where_suffix ) { + $where .= ' ' . $this->posts_where_suffix; + } + + return $where; + } + + public function get_all_posts_by_type( $type ) { + global $wpdb; + + add_filter( 'posts_where', array( &$this, 'posts_where' ) ); + $where_post_date = isset( $_POST['where_post_date'] ) && ! empty( $_POST['where_post_date'] ) ? true : false; + if ( isset( $_POST['postId'] ) ) { + $this->posts_where_suffix .= " AND $wpdb->posts.ID = " . $_POST['postId']; + } elseif ( isset( $_POST['userId'] ) ) { + $this->posts_where_suffix .= " AND $wpdb->posts.post_author = " . $_POST['userId']; + } else { + if ( isset( $_POST['keyword'] ) ) { + $search_on = isset( $_POST['search_on'] ) ? $_POST['search_on'] : ''; + if ( 'title' == $search_on ) { + $this->posts_where_suffix .= " AND ( $wpdb->posts.post_title LIKE '%" . $_POST['keyword'] . "%' )"; + } elseif ( 'content' == $search_on ) { + $this->posts_where_suffix .= " AND ($wpdb->posts.post_content LIKE '%" . $_POST['keyword'] . "%' )"; + } else { + $this->posts_where_suffix .= " AND ($wpdb->posts.post_content LIKE '%" . $_POST['keyword'] . "%' OR $wpdb->posts.post_title LIKE '%" . $_POST['keyword'] . "%' )"; + } + } + if ( isset( $_POST['dtsstart'] ) && '' !== $_POST['dtsstart'] ) { + if ( $where_post_date ) { + $this->posts_where_suffix .= " AND $wpdb->posts.post_date > '" . $_POST['dtsstart'] . "'"; + } else { + $this->posts_where_suffix .= " AND $wpdb->posts.post_modified > '" . $_POST['dtsstart'] . "'"; + } + } + if ( isset( $_POST['dtsstop'] ) && '' !== $_POST['dtsstop'] ) { + if ( $where_post_date ) { + $this->posts_where_suffix .= " AND $wpdb->posts.post_date < '" . $_POST['dtsstop'] . "'"; + } else { + $this->posts_where_suffix .= " AND $wpdb->posts.post_modified < '" . $_POST['dtsstop'] . "'"; + } + } + + if ( isset( $_POST['exclude_page_type'] ) && $_POST['exclude_page_type'] ) { + $this->posts_where_suffix .= " AND $wpdb->posts.post_type NOT IN ('page')"; + } + } + + $maxPages = 50; + if ( defined( 'MAINWP_CHILD_NR_OF_PAGES' ) ) { + $maxPages = MAINWP_CHILD_NR_OF_PAGES; + } + + if ( isset( $_POST['maxRecords'] ) ) { + $maxPages = $_POST['maxRecords']; + } + if ( 0 === $maxPages ) { + $maxPages = 99999; + } + + $extra = array(); + if ( isset( $_POST['extract_tokens'] ) ) { + $extra['tokens'] = maybe_unserialize( base64_decode( $_POST['extract_tokens'] ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. + $extra['extract_post_type'] = $_POST['extract_post_type']; + } + + $extra['where_post_date'] = $where_post_date; + $rslt = $this->get_recent_posts( explode( ',', $_POST['status'] ), $maxPages, $type, $extra ); + $this->posts_where_suffix = ''; + + mainwp_child_helper()->write( $rslt ); + } + + public function new_post() { + $new_post = maybe_unserialize( base64_decode( $_POST['new_post'] ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. + $post_custom = maybe_unserialize( base64_decode( $_POST['post_custom'] ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. + $post_category = rawurldecode( isset( $_POST['post_category'] ) ? base64_decode( $_POST['post_category'] ) : null ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. + $post_tags = rawurldecode( isset( $new_post['post_tags'] ) ? $new_post['post_tags'] : null ); + $post_featured_image = base64_decode( $_POST['post_featured_image'] ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. + $upload_dir = maybe_unserialize( base64_decode( $_POST['mainwp_upload_dir'] ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. + + if ( isset( $_POST['_ezin_post_category'] ) ) { + $new_post['_ezin_post_category'] = maybe_unserialize( base64_decode( $_POST['_ezin_post_category'] ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. + } + + $others = array(); + if ( isset( $_POST['featured_image_data'] ) && ! empty( $_POST['featured_image_data'] ) ) { + $others['featured_image_data'] = unserialize( base64_decode( $_POST['featured_image_data'] ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. + } + + $res = MainWP_Helper::create_post( $new_post, $post_custom, $post_category, $post_featured_image, $upload_dir, $post_tags, $others ); + + if ( is_array( $res ) && isset( $res['error'] ) ) { + MainWP_Helper::error( $res['error'] ); + } + + $created = $res['success']; + if ( true !== $created ) { + MainWP_Helper::error( 'Undefined error' ); + } + + $information['added'] = true; + $information['added_id'] = $res['added_id']; + $information['link'] = $res['link']; + + do_action( 'mainwp_child_after_newpost', $res ); + + mainwp_child_helper()->write( $information ); + } + + public function post_action() { + $action = $_POST['action']; + $postId = $_POST['id']; + $my_post = array(); + + if ( 'publish' === $action ) { + $post_current = get_post( $postId ); + if ( empty( $post_current ) ) { + $information['status'] = 'FAIL'; + } else { + if ( 'future' == $post_current->post_status ) { + wp_publish_post( $postId ); + wp_update_post( + array( + 'ID' => $postId, + 'post_date' => current_time( 'mysql', false ), + 'post_date_gmt' => current_time( 'mysql', true ), + ) + ); + } else { + wp_update_post( + array( + 'ID' => $postId, + 'post_status' => 'publish', + ) + ); + } + } + } elseif ( 'update' === $action ) { + $postData = $_POST['post_data']; + $my_post = is_array( $postData ) ? $postData : array(); + wp_update_post( $my_post ); + } elseif ( 'unpublish' === $action ) { + $my_post['ID'] = $postId; + $my_post['post_status'] = 'draft'; + wp_update_post( $my_post ); + } elseif ( 'trash' === $action ) { + add_action( 'trash_post', array( '\MainWP_Child_Links_Checker', 'hook_post_deleted' ) ); + wp_trash_post( $postId ); + } elseif ( 'delete' === $action ) { + add_action( 'delete_post', array( '\MainWP_Child_Links_Checker', 'hook_post_deleted' ) ); + wp_delete_post( $postId, true ); + } elseif ( 'restore' === $action ) { + wp_untrash_post( $postId ); + } elseif ( 'update_meta' === $action ) { + $values = maybe_unserialize( base64_decode( $_POST['values'] ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. + $meta_key = $values['meta_key']; + $meta_value = $values['meta_value']; + $check_prev = $values['check_prev']; + + foreach ( $meta_key as $i => $key ) { + if ( 1 === intval( $check_prev[ $i ] ) ) { + update_post_meta( $postId, $key, get_post_meta( $postId, $key, true ) ? get_post_meta( $postId, $key, true ) : $meta_value[ $i ] ); + } else { + update_post_meta( $postId, $key, $meta_value[ $i ] ); + } + } + } elseif ( 'get_edit' === $action ) { + $postId = $_POST['id']; + $post_type = $_POST['post_type']; + if ( 'post' == $post_type ) { + $my_post = $this->get_post_edit( $postId ); + } else { + $my_post = $this->get_page_edit( $postId ); + } + } else { + $information['status'] = 'FAIL'; + } + + if ( ! isset( $information['status'] ) ) { + $information['status'] = 'SUCCESS'; + } + $information['my_post'] = $my_post; + mainwp_child_helper()->write( $information ); + } + + public function get_post_edit( $id ) { + $post = get_post( $id ); + if ( $post ) { + $categoryObjects = get_the_category( $post->ID ); + $categories = ''; + foreach ( $categoryObjects as $cat ) { + if ( '' !== $categories ) { + $categories .= ', '; + } + $categories .= $cat->name; + } + $post_category = $categories; + + $tagObjects = get_the_tags( $post->ID ); + $tags = ''; + if ( is_array( $tagObjects ) ) { + foreach ( $tagObjects as $tag ) { + if ( '' !== $tags ) { + $tags .= ', '; + } + $tags .= $tag->name; + } + } + $post_tags = $tags; + + $post_custom = get_post_custom( $id ); + + $galleries = get_post_gallery( $id, false ); + $post_gallery_images = array(); + + if ( is_array( $galleries ) && isset( $galleries['ids'] ) ) { + $attached_images = explode( ',', $galleries['ids'] ); + foreach ( $attached_images as $attachment_id ) { + $attachment = get_post( $attachment_id ); + if ( $attachment ) { + $post_gallery_images[] = array( + 'id' => $attachment_id, + 'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ), + 'caption' => $attachment->post_excerpt, + 'description' => $attachment->post_content, + 'src' => $attachment->guid, + 'title' => $attachment->post_title, + ); + } + } + } + + include_once ABSPATH . 'wp-includes' . DIRECTORY_SEPARATOR . 'post-thumbnail-template.php'; + $post_featured_image = get_post_thumbnail_id( $id ); + $child_upload_dir = wp_upload_dir(); + $new_post = array( + 'edit_id' => $id, + 'is_sticky' => is_sticky( $id ) ? 1 : 0, + 'post_title' => $post->post_title, + 'post_content' => $post->post_content, + 'post_status' => $post->post_status, + 'post_date' => $post->post_date, + 'post_date_gmt' => $post->post_date_gmt, + 'post_tags' => $post_tags, + 'post_name' => $post->post_name, + 'post_excerpt' => $post->post_excerpt, + 'comment_status' => $post->comment_status, + 'ping_status' => $post->ping_status, + ); + + if ( null != $post_featured_image ) { // Featured image is set, retrieve URL. + $img = wp_get_attachment_image_src( $post_featured_image, 'full' ); + $post_featured_image = $img[0]; + } + + require_once ABSPATH . 'wp-admin/includes/post.php'; + wp_set_post_lock( $id ); + + $post_data = array( + 'new_post' => base64_encode( serialize( $new_post ) ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. + 'post_custom' => base64_encode( serialize( $post_custom ) ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. + 'post_category' => base64_encode( $post_category ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. + 'post_featured_image' => base64_encode( $post_featured_image ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. + 'post_gallery_images' => base64_encode( serialize( $post_gallery_images ) ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. + 'child_upload_dir' => base64_encode( serialize( $child_upload_dir ) ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. + ); + return $post_data; + + } + return false; + } + + public function get_page_edit( $id ) { + $post = get_post( $id ); + if ( $post ) { + $post_custom = get_post_custom( $id ); + include_once ABSPATH . 'wp-includes' . DIRECTORY_SEPARATOR . 'post-thumbnail-template.php'; + $post_featured_image = get_post_thumbnail_id( $id ); + $child_upload_dir = wp_upload_dir(); + + $new_post = array( + 'edit_id' => $id, + 'post_title' => $post->post_title, + 'post_content' => $post->post_content, + 'post_status' => $post->post_status, + 'post_date' => $post->post_date, + 'post_date_gmt' => $post->post_date_gmt, + 'post_type' => 'page', + 'post_name' => $post->post_name, + 'post_excerpt' => $post->post_excerpt, + 'comment_status' => $post->comment_status, + 'ping_status' => $post->ping_status, + ); + + if ( null != $post_featured_image ) { + $img = wp_get_attachment_image_src( $post_featured_image, 'full' ); + $post_featured_image = $img[0]; + } + + $galleries = get_post_gallery( $id, false ); + $post_gallery_images = array(); + + if ( is_array( $galleries ) && isset( $galleries['ids'] ) ) { + $attached_images = explode( ',', $galleries['ids'] ); + foreach ( $attached_images as $attachment_id ) { + $attachment = get_post( $attachment_id ); + if ( $attachment ) { + $post_gallery_images[] = array( + 'id' => $attachment_id, + 'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ), + 'caption' => $attachment->post_excerpt, + 'description' => $attachment->post_content, + 'src' => $attachment->guid, + 'title' => $attachment->post_title, + ); + } + } + } + + require_once ABSPATH . 'wp-admin/includes/post.php'; + wp_set_post_lock( $id ); + + $post_data = array( + 'new_post' => base64_encode( serialize( $new_post ) ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. + 'post_custom' => base64_encode( serialize( $post_custom ) ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. + 'post_featured_image' => base64_encode( $post_featured_image ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. + 'post_gallery_images' => base64_encode( serialize( $post_gallery_images ) ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. + 'child_upload_dir' => base64_encode( serialize( $child_upload_dir ) ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. + ); + return $post_data; + } + return false; + } + + public function comment_action() { + $action = $_POST['action']; + $commentId = $_POST['id']; + + if ( 'approve' === $action ) { + wp_set_comment_status( $commentId, 'approve' ); + } elseif ( 'unapprove' === $action ) { + wp_set_comment_status( $commentId, 'hold' ); + } elseif ( 'spam' === $action ) { + wp_spam_comment( $commentId ); + } elseif ( 'unspam' === $action ) { + wp_unspam_comment( $commentId ); + } elseif ( 'trash' === $action ) { + add_action( 'trashed_comment', array( '\MainWP_Child_Links_Checker', 'hook_trashed_comment' ), 10, 1 ); + wp_trash_comment( $commentId ); + } elseif ( 'restore' === $action ) { + wp_untrash_comment( $commentId ); + } elseif ( 'delete' === $action ) { + wp_delete_comment( $commentId, true ); + } else { + $information['status'] = 'FAIL'; + } + + if ( ! isset( $information['status'] ) ) { + $information['status'] = 'SUCCESS'; + } + mainwp_child_helper()->write( $information ); + } + + public function comment_bulk_action() { + $action = $_POST['action']; + $commentIds = explode( ',', $_POST['ids'] ); + $information['success'] = 0; + foreach ( $commentIds as $commentId ) { + if ( $commentId ) { + $information['success'] ++; + if ( 'approve' === $action ) { + wp_set_comment_status( $commentId, 'approve' ); + } elseif ( 'unapprove' === $action ) { + wp_set_comment_status( $commentId, 'hold' ); + } elseif ( 'spam' === $action ) { + wp_spam_comment( $commentId ); + } elseif ( 'unspam' === $action ) { + wp_unspam_comment( $commentId ); + } elseif ( 'trash' === $action ) { + wp_trash_comment( $commentId ); + } elseif ( 'restore' === $action ) { + wp_untrash_comment( $commentId ); + } elseif ( 'delete' === $action ) { + wp_delete_comment( $commentId, true ); + } else { + $information['success']--; + } + } + } + mainwp_child_helper()->write( $information ); + } + + + public function comments_clauses( $clauses ) { + if ( $this->comments_and_clauses ) { + $clauses['where'] .= ' ' . $this->comments_and_clauses; + } + + return $clauses; + } + + public function get_all_comments() { + global $wpdb; + + add_filter( 'comments_clauses', array( &$this, 'comments_clauses' ) ); + + if ( isset( $_POST['postId'] ) ) { + $this->comments_and_clauses .= " AND $wpdb->comments.comment_post_ID = " . $_POST['postId']; + } else { + if ( isset( $_POST['keyword'] ) ) { + $this->comments_and_clauses .= " AND $wpdb->comments.comment_content LIKE '%" . $_POST['keyword'] . "%'"; + } + if ( isset( $_POST['dtsstart'] ) && '' !== $_POST['dtsstart'] ) { + $this->comments_and_clauses .= " AND $wpdb->comments.comment_date > '" . $_POST['dtsstart'] . "'"; + } + if ( isset( $_POST['dtsstop'] ) && '' !== $_POST['dtsstop'] ) { + $this->comments_and_clauses .= " AND $wpdb->comments.comment_date < '" . $_POST['dtsstop'] . "'"; + } + } + + $maxComments = 50; + if ( defined( 'MAINWP_CHILD_NR_OF_COMMENTS' ) ) { + $maxComments = MAINWP_CHILD_NR_OF_COMMENTS; // to compatible. + } + + if ( isset( $_POST['maxRecords'] ) ) { + $maxComments = $_POST['maxRecords']; + } + + if ( 0 === $maxComments ) { + $maxComments = 99999; + } + + $rslt = $this->get_recent_comments( explode( ',', $_POST['status'] ), $maxComments ); + $this->comments_and_clauses = ''; + + mainwp_child_helper()->write( $rslt ); + } + + public function get_recent_comments( $pAllowedStatuses, $pCount ) { + if ( ! function_exists( 'get_comment_author_url' ) ) { + include_once WPINC . '/comment-template.php'; + } + $allComments = array(); + + foreach ( $pAllowedStatuses as $status ) { + $params = array( 'status' => $status ); + if ( 0 !== $pCount ) { + $params['number'] = $pCount; + } + $comments = get_comments( $params ); + if ( is_array( $comments ) ) { + foreach ( $comments as $comment ) { + $post = get_post( $comment->comment_post_ID ); + $email = apply_filters( 'comment_email', $comment->comment_author_email ); + $outComment = array(); + $outComment['id'] = $comment->comment_ID; + $outComment['status'] = wp_get_comment_status( $comment->comment_ID ); + $outComment['author'] = $comment->comment_author; + $outComment['author_url'] = get_comment_author_url( $comment->comment_ID ); + $outComment['author_ip'] = get_comment_author_IP( $comment->comment_ID ); + $outComment['author_email'] = apply_filters( 'comment_email', $comment->comment_author_email ); + $outComment['postId'] = $comment->comment_post_ID; + $outComment['postName'] = $post->post_title; + $outComment['comment_count'] = $post->comment_count; + $outComment['content'] = $comment->comment_content; + $outComment['dts'] = strtotime( $comment->comment_date_gmt ); + $allComments[] = $outComment; + } + } + } + + return $allComments; + } + +} diff --git a/class/class-mainwp-child-stats.php b/class/class-mainwp-child-stats.php new file mode 100644 index 0000000..21340f0 --- /dev/null +++ b/class/class-mainwp-child-stats.php @@ -0,0 +1,701 @@ +filterFunction = function( $a ) { + if ( null == $a ) { + return false; } + if ( is_object( $a ) && property_exists( $a, 'last_checked' ) && ! property_exists( $a, 'checked' ) ) { + return false; + } + return $a; + }; + } + + public static function get_instance() { + if ( null === self::$instance ) { + self::$instance = new self(); + } + + return self::$instance; + } + + // Show stats without login - only allowed while no account is added yet. + public function get_site_stats_no_auth( $information = array() ) { + if ( get_option( 'mainwp_child_pubkey' ) ) { + $hint = '
' . __( 'Hint: Go to the child site, deactivate and reactivate the MainWP Child plugin and try again.', 'mainwp-child' ); + MainWP_Helper::error( __( 'This site already contains a link. Please deactivate and reactivate the MainWP plugin.', 'mainwp-child' ) . $hint ); + } + + global $wp_version; + $information['version'] = MainWP_Child::$version; + $information['wpversion'] = $wp_version; + $information['wpe'] = MainWP_Helper::is_wp_engine() ? 1 : 0; + mainwp_child_helper()->write( $information ); + } + + + public function default_option_active_plugins( $default ) { + if ( ! is_array( $default ) ) { + $default = array(); + } + if ( ! in_array( 'managewp/init.php', $default ) ) { + $default[] = 'managewp/init.php'; + } + + return $default; + } + + // Show stats. + public function get_site_stats( $information = array(), $exit = true ) { + global $wp_version; + + if ( $exit ) { + $this->update_external_settings(); + } + + MainWP_Child_Branding::instance()->save_branding_options( 'branding_disconnected', '' ); + if ( isset( $_POST['server'] ) ) { + MainWP_Helper::update_option( 'mainwp_child_server', $_POST['server'] ); + } + + MainWP_Child_Plugins_Check::may_outdate_number_change(); + + $information['version'] = MainWP_Child::$version; + $information['wpversion'] = $wp_version; + $information['siteurl'] = get_option( 'siteurl' ); + $information['wpe'] = MainWP_Helper::is_wp_engine() ? 1 : 0; + $theme_name = wp_get_theme()->get( 'Name' ); + $information['site_info'] = array( + 'wpversion' => $wp_version, + 'debug_mode' => ( defined( 'WP_DEBUG' ) && true === WP_DEBUG ) ? true : false, + 'phpversion' => phpversion(), + 'child_version' => MainWP_Child::$version, + 'memory_limit' => MainWP_Child_Server_Information::get_php_memory_limit(), + 'mysql_version' => MainWP_Child_Server_Information::get_my_sql_version(), + 'themeactivated' => $theme_name, + 'ip' => $_SERVER['SERVER_ADDR'], + ); + + // Try to switch to SSL if SSL is enabled in between! + $pubkey = get_option( 'mainwp_child_pubkey' ); + $nossl = get_option( 'mainwp_child_nossl' ); + if ( 1 == $nossl ) { + if ( isset( $pubkey ) && MainWP_Helper::is_ssl_enabled() ) { + MainWP_Helper::update_option( 'mainwp_child_nossl', 0, 'yes' ); + $nossl = 0; + } + } + $information['nossl'] = ( 1 == $nossl ? 1 : 0 ); + + include_once ABSPATH . '/wp-admin/includes/update.php'; + + $timeout = 3 * 60 * 60; + set_time_limit( $timeout ); + ini_set( 'max_execution_time', $timeout ); //phpcs:ignore -- to custom + + // Check for new versions. + if ( null !== $this->filterFunction ) { + add_filter( 'pre_site_transient_update_core', $this->filterFunction, 99 ); + } + if ( null !== $this->filterFunction ) { + add_filter( 'pre_transient_update_core', $this->filterFunction, 99 ); + } + wp_version_check(); + $core_updates = get_core_updates(); + if ( is_array( $core_updates ) && count( $core_updates ) > 0 ) { + foreach ( $core_updates as $core_update ) { + if ( 'latest' === $core_update->response ) { + break; + } + if ( 'upgrade' === $core_update->response && version_compare( $wp_version, $core_update->current, '<=' ) ) { + $information['wp_updates'] = $core_update->current; + } + } + } + if ( ! isset( $information['wp_updates'] ) ) { + $information['wp_updates'] = null; + } + if ( null !== $this->filterFunction ) { + remove_filter( 'pre_site_transient_update_core', $this->filterFunction, 99 ); + } + if ( null !== $this->filterFunction ) { + remove_filter( 'pre_transient_update_core', $this->filterFunction, 99 ); + } + + add_filter( 'default_option_active_plugins', array( &$this, 'default_option_active_plugins' ) ); + add_filter( 'option_active_plugins', array( &$this, 'default_option_active_plugins' ) ); + + // First check for new premium updates. + $update_check = apply_filters( 'mwp_premium_update_check', array() ); + if ( ! empty( $update_check ) ) { + foreach ( $update_check as $updateFeedback ) { + if ( is_array( $updateFeedback['callback'] ) && isset( $updateFeedback['callback'][0] ) && isset( $updateFeedback['callback'][1] ) ) { + call_user_func( array( $updateFeedback['callback'][0], $updateFeedback['callback'][1] ) ); + } elseif ( is_string( $updateFeedback['callback'] ) ) { + call_user_func( $updateFeedback['callback'] ); + } + } + } + + $informationPremiumUpdates = apply_filters( 'mwp_premium_update_notification', array() ); + $premiumPlugins = array(); + $premiumThemes = array(); + if ( is_array( $informationPremiumUpdates ) ) { + $premiumUpdates = array(); + $information['premium_updates'] = array(); + $informationPremiumUpdatesLength = count( $informationPremiumUpdates ); + for ( $i = 0; $i < $informationPremiumUpdatesLength; $i ++ ) { + if ( ! isset( $informationPremiumUpdates[ $i ]['new_version'] ) ) { + continue; + } + $slug = ( isset( $informationPremiumUpdates[ $i ]['slug'] ) ? $informationPremiumUpdates[ $i ]['slug'] : $informationPremiumUpdates[ $i ]['Name'] ); + + if ( 'plugin' === $informationPremiumUpdates[ $i ]['type'] ) { + $premiumPlugins[] = $slug; + } elseif ( 'theme' === $informationPremiumUpdates[ $i ]['type'] ) { + $premiumThemes[] = $slug; + } + + $new_version = $informationPremiumUpdates[ $i ]['new_version']; + + unset( $informationPremiumUpdates[ $i ]['old_version'] ); + unset( $informationPremiumUpdates[ $i ]['new_version'] ); + + $information['premium_updates'][ $slug ] = $informationPremiumUpdates[ $i ]; + $information['premium_updates'][ $slug ]['update'] = (object) array( + 'new_version' => $new_version, + 'premium' => true, + 'slug' => $slug, + ); + if ( ! in_array( $slug, $premiumUpdates ) ) { + $premiumUpdates[] = $slug; + } + } + MainWP_Helper::update_option( 'mainwp_premium_updates', $premiumUpdates ); + } + + remove_filter( 'default_option_active_plugins', array( &$this, 'default_option_active_plugins' ) ); + remove_filter( 'option_active_plugins', array( &$this, 'default_option_active_plugins' ) ); + + if ( null !== $this->filterFunction ) { + add_filter( 'pre_site_transient_update_plugins', $this->filterFunction, 99 ); + } + + global $wp_current_filter; + $wp_current_filter[] = 'load-plugins.php'; // phpcs:ignore -- to custom plugin installation. + + wp_update_plugins(); + include_once ABSPATH . '/wp-admin/includes/plugin.php'; + + $plugin_updates = get_plugin_updates(); + if ( is_array( $plugin_updates ) ) { + $information['plugin_updates'] = array(); + + foreach ( $plugin_updates as $slug => $plugin_update ) { + if ( in_array( $plugin_update->Name, $premiumPlugins ) ) { + continue; + } + + // to fix incorrect info. + if ( ! property_exists( $plugin_update, 'update' ) || ! property_exists( $plugin_update->update, 'new_version' ) || empty( $plugin_update->update->new_version ) ) { + continue; + } + + $information['plugin_updates'][ $slug ] = $plugin_update; + } + } + + if ( null !== $this->filterFunction ) { + remove_filter( 'pre_site_transient_update_plugins', $this->filterFunction, 99 ); + } + + // to fix premium plugs update. + $cached_plugins_update = get_site_transient( 'mainwp_update_plugins_cached' ); + if ( is_array( $cached_plugins_update ) && ( count( $cached_plugins_update ) > 0 ) ) { + if ( ! isset( $information['plugin_updates'] ) ) { + $information['plugin_updates'] = array(); + } + foreach ( $cached_plugins_update as $slug => $plugin_update ) { + + // to fix incorrect info. + if ( ! property_exists( $plugin_update, 'new_version' ) || empty( $plugin_update->new_version ) ) { // may do not need to check this? + // to fix for some premiums update info. + if ( property_exists( $plugin_update, 'update' ) ) { + if ( ! property_exists( $plugin_update->update, 'new_version' ) || empty( $plugin_update->update->new_version ) ) { + continue; + } + } else { + continue; + } + } + + if ( ! isset( $information['plugin_updates'][ $slug ] ) ) { + $information['plugin_updates'][ $slug ] = $plugin_update; + } + } + } + + if ( null !== $this->filterFunction ) { + add_filter( 'pre_site_transient_update_themes', $this->filterFunction, 99 ); + } + wp_update_themes(); + include_once ABSPATH . '/wp-admin/includes/theme.php'; + $theme_updates = MainWP_Child_Updates::get_instance()->upgrade_get_theme_updates(); + if ( is_array( $theme_updates ) ) { + $information['theme_updates'] = array(); + + foreach ( $theme_updates as $slug => $theme_update ) { + $name = ( is_array( $theme_update ) ? $theme_update['Name'] : $theme_update->Name ); + if ( in_array( $name, $premiumThemes ) ) { + continue; + } + + $information['theme_updates'][ $slug ] = $theme_update; + } + } + if ( null !== $this->filterFunction ) { + remove_filter( 'pre_site_transient_update_themes', $this->filterFunction, 99 ); + } + + // to fix premium themes update. + $cached_themes_update = get_site_transient( 'mainwp_update_themes_cached' ); + if ( is_array( $cached_themes_update ) && ( count( $cached_themes_update ) > 0 ) ) { + if ( ! isset( $information['theme_updates'] ) ) { + $information['theme_updates'] = array(); + } + + foreach ( $cached_themes_update as $slug => $theme_update ) { + $name = ( is_array( $theme_update ) ? $theme_update['Name'] : $theme_update->Name ); + if ( in_array( $name, $premiumThemes ) ) { + continue; + } + if ( isset( $information['theme_updates'][ $slug ] ) ) { + continue; + } + $information['theme_updates'][ $slug ] = $theme_update; + } + } + + $translation_updates = wp_get_translation_updates(); + if ( ! empty( $translation_updates ) ) { + $information['translation_updates'] = array(); + foreach ( $translation_updates as $translation_update ) { + $new_translation_update = array( + 'type' => $translation_update->type, + 'slug' => $translation_update->slug, + 'language' => $translation_update->language, + 'version' => $translation_update->version, + ); + if ( 'plugin' === $translation_update->type ) { + $all_plugins = get_plugins(); + foreach ( $all_plugins as $file => $plugin ) { + $path = dirname( $file ); + if ( $path == $translation_update->slug ) { + $new_translation_update['name'] = $plugin['Name']; + break; + } + } + } elseif ( 'theme' === $translation_update->type ) { + $theme = wp_get_theme( $translation_update->slug ); + $new_translation_update['name'] = $theme->name; + } elseif ( ( 'core' === $translation_update->type ) && ( 'default' === $translation_update->slug ) ) { + $new_translation_update['name'] = 'WordPress core'; + } + $information['translation_updates'][] = $new_translation_update; + } + } + + $information['recent_comments'] = MainWP_Child_Posts::get_instance()->get_recent_comments( array( 'approve', 'hold' ), 5 ); + + $recent_number = 5; + + if ( isset( $_POST ) && isset( $_POST['recent_number'] ) ) { + $recent_number = $_POST['recent_number']; + if ( get_option( 'mainwp_child_recent_number', 5 ) != $recent_number ) { + update_option( 'mainwp_child_recent_number', $recent_number ); + } + } else { + $recent_number = get_option( 'mainwp_child_recent_number', 5 ); + } + + if ( $recent_number <= 0 || $recent_number > 30 ) { + $recent_number = 5; + } + + $information['recent_posts'] = MainWP_Child_Posts::get_instance()->get_recent_posts( array( 'publish', 'draft', 'pending', 'trash', 'future' ), $recent_number ); + $information['recent_pages'] = MainWP_Child_Posts::get_instance()->get_recent_posts( array( 'publish', 'draft', 'pending', 'trash', 'future' ), $recent_number, 'page' ); + $information['securityIssues'] = MainWP_Security::get_stats_security(); + + // Directory listings! + $information['directories'] = $this->scan_dir( ABSPATH, 3 ); + $cats = get_categories( + array( + 'hide_empty' => 0, + 'hierarchical' => true, + 'number' => 300, + ) + ); + $categories = array(); + foreach ( $cats as $cat ) { + $categories[] = $cat->name; + } + $information['categories'] = $categories; + + + $get_file_size = apply_filters_deprecated( 'mainwp-child-get-total-size', array( true ), '4.0.7.1', 'mainwp_child_get_total_size' ); + $get_file_size = apply_filters( 'mainwp_child_get_total_size', $get_file_size ); + + if ( $get_file_size && isset( $_POST['cloneSites'] ) && ( '0' !== $_POST['cloneSites'] ) ) { + $max_exe = ini_get( 'max_execution_time' ); + if ( $max_exe > 20 ) { + $information['totalsize'] = $this->get_total_file_size(); + } + } + $information['dbsize'] = MainWP_Child_DB::get_size(); + + global $mainWPChild; + $max_his = $mainWPChild->get_max_history(); + + + $auths = get_option( 'mainwp_child_auth' ); + $information['extauth'] = ( $auths && isset( $auths[ $max_his ] ) ? $auths[ $max_his ] : null ); + + $plugins = $this->get_all_plugins_int( false ); + $themes = $this->get_all_themes_int( false ); + $information['plugins'] = $plugins; + $information['themes'] = $themes; + + if ( isset( $_POST['optimize'] ) && ( '1' === $_POST['optimize'] ) ) { + $information['users'] = MainWP_Child_Users::get_instance()->get_all_users_int( 500 ); + } + + if ( isset( $_POST['primaryBackup'] ) && ! empty( $_POST['primaryBackup'] ) ) { + $primary_bk = $_POST['primaryBackup']; + $information['primaryLasttimeBackup'] = MainWP_Helper::get_lasttime_backup( $primary_bk ); + } + + $last_post = wp_get_recent_posts( array( 'numberposts' => absint( '1' ) ) ); + if ( isset( $last_post[0] ) ) { + $last_post = $last_post[0]; + } + if ( isset( $last_post ) && isset( $last_post['post_modified_gmt'] ) ) { + $information['last_post_gmt'] = strtotime( $last_post['post_modified_gmt'] ); + } + $information['mainwpdir'] = ( MainWP_Helper::validate_mainwp_dir() ? 1 : - 1 ); + $information['uniqueId'] = get_option( 'mainwp_child_uniqueId', '' ); + $information['plugins_outdate_info'] = MainWP_Child_Plugins_Check::instance()->get_plugins_outdate_info(); + $information['themes_outdate_info'] = MainWP_Child_Themes_Check::instance()->get_themes_outdate_info(); + + if ( isset( $_POST['user'] ) ) { + $user = get_user_by( 'login', $_POST['user'] ); + if ( $user && property_exists( $user, 'ID' ) && $user->ID ) { + $information['admin_nicename'] = $user->data->user_nicename; + $information['admin_useremail'] = $user->data->user_email; + } + } + + try { + do_action( 'mainwp_child_site_stats' ); + } catch ( \Exception $e ) { + MainWP_Helper::log_debug( $e->getMessage() ); + } + + if ( isset( $_POST['othersData'] ) ) { + $othersData = json_decode( stripslashes( $_POST['othersData'] ), true ); + if ( ! is_array( $othersData ) ) { + $othersData = array(); + } + + if ( isset( $othersData['wpvulndbToken'] ) ) { + $wpvulndb_token = get_option( 'mainwp_child_wpvulndb_token', '' ); + if ( $wpvulndb_token != $othersData['wpvulndbToken'] ) { + MainWP_Helper::update_option( 'mainwp_child_wpvulndb_token', $othersData['wpvulndbToken'] ); + } + } + + try { + $information = apply_filters_deprecated( 'mainwp-site-sync-others-data', array( $information, $othersData ), '4.0.7.1', 'mainwp_site_sync_others_data' ); + $information = apply_filters( 'mainwp_site_sync_others_data', $information, $othersData ); + + } catch ( \Exception $e ) { + MainWP_Helper::log_debug( $e->getMessage() ); + } + } + + if ( $exit ) { + mainwp_child_helper()->write( $information ); + } + + return $information; + } + + public function update_external_settings() { + $update_htaccess = false; + + if ( isset( $_POST['cloneSites'] ) ) { + if ( '0' !== $_POST['cloneSites'] ) { + $arr = json_decode( urldecode( $_POST['cloneSites'] ), 1 ); + MainWP_Helper::update_option( 'mainwp_child_clone_sites', ( ! is_array( $arr ) ? array() : $arr ) ); + } else { + MainWP_Helper::update_option( 'mainwp_child_clone_sites', '0' ); + } + } + + if ( isset( $_POST['siteId'] ) ) { + MainWP_Helper::update_option( 'mainwp_child_siteid', intval( $_POST['siteId'] ) ); + } + + if ( isset( $_POST['pluginDir'] ) ) { + if ( get_option( 'mainwp_child_pluginDir' ) !== $_POST['pluginDir'] ) { + MainWP_Helper::update_option( 'mainwp_child_pluginDir', $_POST['pluginDir'], 'yes' ); + $update_htaccess = true; + } + } elseif ( false !== get_option( 'mainwp_child_pluginDir' ) ) { + MainWP_Helper::update_option( 'mainwp_child_pluginDir', false, 'yes' ); + $update_htaccess = true; + } + + if ( $update_htaccess ) { + global $mainWPChild; + $mainWPChild->update_htaccess( true ); + } + } + + public function get_total_file_size( $directory = WP_CONTENT_DIR ) { + try { + if ( MainWP_Helper::function_exists( 'popen' ) ) { + $uploadDir = MainWP_Helper::get_mainwp_dir(); + $uploadDir = $uploadDir[0]; + $popenHandle = popen( 'du -s ' . $directory . ' --exclude "' . str_replace( ABSPATH, '', $uploadDir ) . '"', 'r' ); // phpcs:ignore -- run if enabled. + if ( 'resource' === gettype( $popenHandle ) ) { + $size = fread( $popenHandle, 1024 ); + pclose( $popenHandle ); + $size = substr( $size, 0, strpos( $size, "\t" ) ); + if ( $size && MainWP_Helper::ctype_digit( $size ) ) { + return $size / 1024; + } + } + } + + if ( MainWP_Helper::function_exists( 'shell_exec' ) ) { + $uploadDir = MainWP_Helper::get_mainwp_dir(); + $uploadDir = $uploadDir[0]; + $size = shell_exec( 'du -s ' . $directory . ' --exclude "' . str_replace( ABSPATH, '', $uploadDir ) . '"' ); // phpcs:ignore -- run if enabled. + if ( null !== $size ) { + $size = substr( $size, 0, strpos( $size, "\t" ) ); + if ( $size && MainWP_Helper::ctype_digit( $size ) ) { + return $size / 1024; + } + } + } + if ( class_exists( 'COM' ) ) { + $obj = new COM( 'scripting.filesystemobject' ); + + if ( is_object( $obj ) ) { + $ref = $obj->getfolder( $directory ); + + $size = $ref->size; + + $obj = null; + if ( MainWP_Helper::ctype_digit( $size ) ) { + return $size / 1024; + } + } + } + // to fix for window host, performance not good? + if ( class_exists( 'RecursiveIteratorIterator' ) ) { + $size = 0; + foreach ( new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $directory ) ) as $file ) { + $size += $file->getSize(); + } + if ( $size && MainWP_Helper::ctype_digit( $size ) ) { + return $size / 1024 / 1024; + } + } + return 0; + } catch ( \Exception $e ) { + return 0; + } + } + + public function scan_dir( $pDir, $pLvl ) { + $output = array(); + if ( file_exists( $pDir ) && is_dir( $pDir ) ) { + if ( 'logs' === basename( $pDir ) ) { + return empty( $output ) ? null : $output; + } + if ( 0 === $pLvl ) { + return empty( $output ) ? null : $output; + } + $files = $this->int_scan_dir( $pDir ); + if ( $files ) { + foreach ( $files as $file ) { + if ( ( '.' === $file ) || ( '..' === $file ) ) { + continue; + } + $newDir = $pDir . $file . DIRECTORY_SEPARATOR; + if ( is_dir( $newDir ) ) { + $output[ $file ] = $this->scan_dir( $newDir, $pLvl - 1, false ); + } + } + + unset( $files ); + $files = null; + } + } + + return empty( $output ) ? null : $output; + } + + public function int_scan_dir( $dir ) { + $dh = opendir( $dir ); + if ( is_dir( $dir ) && $dh ) { + $cnt = 0; + $out = array(); + $file = readdir( $dh ); + while ( false !== $file ) { + $newDir = $dir . $file . DIRECTORY_SEPARATOR; + if ( ! is_dir( $newDir ) ) { + continue; + } + + $out[] = $file; + if ( $cnt ++ > 10 ) { + return $out; + } + } + closedir( $dh ); + + return $out; + } + + return false; + } + + public function get_all_themes() { + $keyword = $_POST['keyword']; + $status = $_POST['status']; + $filter = isset( $_POST['filter'] ) ? $_POST['filter'] : true; + $rslt = $this->get_all_themes_int( $filter, $keyword, $status ); + + mainwp_child_helper()->write( $rslt ); + } + + public function get_all_themes_int( $filter, $keyword = '', $status = '' ) { + $rslt = array(); + $themes = wp_get_themes(); + + if ( is_array( $themes ) ) { + $theme_name = wp_get_theme()->get( 'Name' ); + + /** @var $theme WP_Theme */ + foreach ( $themes as $theme ) { + $out = array(); + $out['name'] = $theme->get( 'Name' ); + $out['title'] = $theme->display( 'Name', true, false ); + $out['description'] = $theme->display( 'Description', true, false ); + $out['version'] = $theme->display( 'Version', true, false ); + $out['active'] = ( $theme->get( 'Name' ) === $theme_name ) ? 1 : 0; + $out['slug'] = $theme->get_stylesheet(); + if ( ! $filter ) { + if ( '' == $keyword || stristr( $out['title'], $keyword ) ) { + $rslt[] = $out; + } + } elseif ( ( ( 'active' === $status ) ? 1 : 0 ) === $out['active'] ) { + if ( '' == $keyword || stristr( $out['title'], $keyword ) ) { + $rslt[] = $out; + } + } + } + } + + return $rslt; + } + + + public function get_all_plugins() { + $keyword = $_POST['keyword']; + $status = $_POST['status']; + $filter = isset( $_POST['filter'] ) ? $_POST['filter'] : true; + $rslt = $this->get_all_plugins_int( $filter, $keyword, $status ); + + mainwp_child_helper()->write( $rslt ); + } + + public function get_all_plugins_int( $filter, $keyword = '', $status = '' ) { + if ( ! function_exists( 'get_plugins' ) ) { + include_once ABSPATH . 'wp-admin/includes/plugin.php'; + } + global $mainWPChild; + $rslt = array(); + $plugins = get_plugins(); + if ( is_array( $plugins ) ) { + $active_plugins = get_option( 'active_plugins' ); + + foreach ( $plugins as $pluginslug => $plugin ) { + $out = array(); + $out['mainwp'] = ( $pluginslug == $mainWPChild->plugin_slug ? 'T' : 'F' ); + $out['name'] = $plugin['Name']; + $out['slug'] = $pluginslug; + $out['description'] = $plugin['Description']; + $out['version'] = $plugin['Version']; + $out['active'] = is_plugin_active( $pluginslug ) ? 1 : 0; + if ( ! $filter ) { + if ( '' == $keyword || stristr( $out['name'], $keyword ) ) { + $rslt[] = $out; + } + } elseif ( ( ( 'active' == $status ) ? 1 : 0 ) == $out['active'] ) { + if ( '' == $keyword || stristr( $out['name'], $keyword ) ) { + $rslt[] = $out; + } + } + } + } + + $muplugins = get_mu_plugins(); + if ( is_array( $muplugins ) ) { + foreach ( $muplugins as $pluginslug => $plugin ) { + $out = array(); + $out['mainwp'] = ( $pluginslug == $mainWPChild->plugin_slug ? 'T' : 'F' ); + $out['name'] = $plugin['Name']; + $out['slug'] = $pluginslug; + $out['description'] = $plugin['Description']; + $out['version'] = $plugin['Version']; + $out['active'] = 1; + $out['mu'] = 1; + if ( ! $filter ) { + if ( '' == $keyword || stristr( $out['name'], $keyword ) ) { + $rslt[] = $out; + } + } elseif ( ( ( 'active' == $status ) ? 1 : 0 ) == $out['active'] ) { + if ( '' == $keyword || stristr( $out['name'], $keyword ) ) { + $rslt[] = $out; + } + } + } + } + + return $rslt; + } + + +} diff --git a/class/class-mainwp-child-updates.php b/class/class-mainwp-child-updates.php index 082ada2..5e3b734 100644 --- a/class/class-mainwp-child-updates.php +++ b/class/class-mainwp-child-updates.php @@ -350,12 +350,27 @@ class MainWP_Child_Updates { $information['upgrades'][ $slug ] = false; } } - } - $information['sync'] = $this->get_site_stats( array(), false ); + } + $information['sync'] = MainWP_Child_Stats::get_instance()->get_site_stats( array(), false ); mainwp_child_helper()->write( $information ); } + public function upgrade_get_theme_updates() { + $themeUpdates = get_theme_updates(); + $newThemeUpdates = array(); + if ( is_array( $themeUpdates ) ) { + foreach ( $themeUpdates as $slug => $themeUpdate ) { + $newThemeUpdate = array(); + $newThemeUpdate['update'] = $themeUpdate->update; + $newThemeUpdate['Name'] = MainWP_Helper::search( $themeUpdate, 'Name' ); + $newThemeUpdate['Version'] = MainWP_Helper::search( $themeUpdate, 'Version' ); + $newThemeUpdates[ $slug ] = $newThemeUpdate; + } + } + return $newThemeUpdates; + } + public function hook_fix_optimize_press_theme_update( $transient ) { if ( ! defined( 'OP_FUNC' ) ) { return $transient; @@ -464,18 +479,15 @@ class MainWP_Child_Updates { if ( 'plugin' == $type || 'theme' == $type ) { $list = isset( $_GET['list'] ) ? $_GET['list'] : ''; - if ( ! empty( $list ) ) { - // to call function upgrade_plugin_theme(). + + if ( ! empty( $list ) ) { $_POST['type'] = $type; $_POST['list'] = $list; - - global $mainWPChild; - $callable = $mainWPChild->get_callable_functions(); - - $function = 'upgradeplugintheme'; - if ( isset( $callable [ $function ] ) ) { - call_user_func( array( $this, $callable [ $function ] ) ); - } + + $function = 'upgradeplugintheme'; // to call function upgrade_plugin_theme(). + if ( MainWP_Child_Callable::get_instance()->is_callable_function( $function ) ) { + MainWP_Child_Callable::get_instance()->call_function( $function ); + } } } } @@ -745,9 +757,9 @@ class MainWP_Child_Updates { } } else { $information['upgrades'] = array(); // to fix error message when translations updated. - } - - $information['sync'] = $this->get_site_stats( array(), false ); + } + $information['sync'] = MainWP_Child_Stats::get_instance()->get_site_stats( array(), false ); mainwp_child_helper()->write( $information ); - } + } + } diff --git a/class/class-mainwp-child-users.php b/class/class-mainwp-child-users.php new file mode 100644 index 0000000..9c59596 --- /dev/null +++ b/class/class-mainwp-child-users.php @@ -0,0 +1,470 @@ +ID ) ) ? $current_user->ID : 0; + include_once ABSPATH . '/wp-admin/includes/user.php'; + + if ( 'delete' === $action ) { + wp_delete_user( $userId, $reassign ); + } elseif ( 'changeRole' === $action ) { + $my_user = array(); + $my_user['ID'] = $userId; + $my_user['role'] = $extra; + wp_update_user( $my_user ); + } elseif ( 'update_password' === $action ) { + $my_user = array(); + $my_user['ID'] = $userId; + $my_user['user_pass'] = $user_pass; + wp_update_user( $my_user ); + } elseif ( 'edit' === $action ) { + $user_data = $this->get_user_to_edit( $userId ); + if ( ! empty( $user_data ) ) { + $information['user_data'] = $user_data; + } else { + $failed = true; + } + } elseif ( 'update_user' === $action ) { + $my_user = $_POST['extra']; + if ( is_array( $my_user ) ) { + foreach ( $my_user as $idx => $val ) { + if ( 'donotupdate' === $val || ( empty( $val ) && 'role' !== $idx ) ) { + unset( $my_user[ $idx ] ); + } + } + $result = $this->edit_user( $userId, $my_user ); + if ( is_array( $result ) && isset( $result['error'] ) ) { + $information['error'] = $result['error']; + } + } else { + $failed = true; + } + } else { + $failed = true; + } + + if ( $failed ) { + $information['status'] = 'FAIL'; + } + + if ( ! isset( $information['status'] ) && ! isset( $information['error'] ) ) { + $information['status'] = 'SUCCESS'; + if ( 'update_user' === $action && isset( $_POST['optimize'] ) && ! empty( $_POST['optimize'] ) ) { + $information['users'] = $this->get_all_users_int( 500 ); + } + } + mainwp_child_helper()->write( $information ); + } + + + public function get_all_users_int( $number = false ) { + $allusers = array(); + + $params = array(); + if ( $number ) { + $params['number'] = $number; + } + + $new_users = get_users( $params ); + if ( is_array( $new_users ) ) { + foreach ( $new_users as $new_user ) { + $usr = array(); + $usr['id'] = $new_user->ID; + $usr['login'] = $new_user->user_login; + $usr['nicename'] = $new_user->user_nicename; + $usr['email'] = $new_user->user_email; + $usr['registered'] = $new_user->user_registered; + $usr['status'] = $new_user->user_status; + $usr['display_name'] = $new_user->display_name; + $userdata = get_userdata( $new_user->ID ); + $user_roles = $userdata->roles; + $user_role = array_shift( $user_roles ); + $usr['role'] = $user_role; + $usr['post_count'] = count_user_posts( $new_user->ID ); + $allusers[] = $usr; + } + } + + return $allusers; + } + + + public function get_all_users( $return = false ) { + $roles = explode( ',', $_POST['role'] ); + $allusers = array(); + if ( is_array( $roles ) ) { + foreach ( $roles as $role ) { + $new_users = get_users( 'role=' . $role ); + foreach ( $new_users as $new_user ) { + $usr = array(); + $usr['id'] = $new_user->ID; + $usr['login'] = $new_user->user_login; + $usr['nicename'] = $new_user->user_nicename; + $usr['email'] = $new_user->user_email; + $usr['registered'] = $new_user->user_registered; + $usr['status'] = $new_user->user_status; + $usr['display_name'] = $new_user->display_name; + $usr['role'] = $role; + $usr['post_count'] = count_user_posts( $new_user->ID ); + $usr['avatar'] = get_avatar( $new_user->ID, 32 ); + $allusers[] = $usr; + } + } + } + if ( $return ) { + return $allusers; + } + mainwp_child_helper()->write( $allusers ); + } + + + public function search_users() { + + $search_user_role = array(); + $check_users_role = false; + + if ( isset( $_POST['role'] ) && ! empty( $_POST['role'] ) ) { + $check_users_role = true; + $all_users_role = $this->get_all_users( true ); + foreach ( $all_users_role as $user ) { + $search_user_role[] = $user['id']; + } + unset( $all_users_role ); + } + + $columns = explode( ',', $_POST['search_columns'] ); + $allusers = array(); + $exclude = array(); + + foreach ( $columns as $col ) { + if ( empty( $col ) ) { + continue; + } + + $user_query = new WP_User_Query( + array( + 'search' => $_POST['search'], + 'fields' => 'all_with_meta', + 'search_columns' => array( $col ), + 'query_orderby' => array( $col ), + 'exclude' => $exclude, + ) + ); + if ( ! empty( $user_query->results ) ) { + foreach ( $user_query->results as $new_user ) { + if ( $check_users_role ) { + if ( ! in_array( $new_user->ID, $search_user_role ) ) { + continue; + } + } + $exclude[] = $new_user->ID; + $usr = array(); + $usr['id'] = $new_user->ID; + $usr['login'] = $new_user->user_login; + $usr['nicename'] = $new_user->user_nicename; + $usr['email'] = $new_user->user_email; + $usr['registered'] = $new_user->user_registered; + $usr['status'] = $new_user->user_status; + $usr['display_name'] = $new_user->display_name; + $userdata = get_userdata( $new_user->ID ); + $user_roles = $userdata->roles; + $user_role = array_shift( $user_roles ); + $usr['role'] = $user_role; + $usr['post_count'] = count_user_posts( $new_user->ID ); + $usr['avatar'] = get_avatar( $new_user->ID, 32 ); + $allusers[] = $usr; + } + } + } + + mainwp_child_helper()->write( $allusers ); + } + + + public function edit_user( $user_id, $data ) { + $wp_roles = wp_roles(); + $user = new stdClass(); + + $update = true; + + if ( $user_id ) { + $user->ID = (int) $user_id; + $userdata = get_userdata( $user_id ); + $user->user_login = wp_slash( $userdata->user_login ); + } else { + return array( 'error' => 'ERROR: Empty user id.' ); + } + + $pass1 = ''; + $pass2 = ''; + + if ( isset( $data['pass1'] ) ) { + $pass1 = $data['pass1']; + } + + if ( isset( $data['pass2'] ) ) { + $pass2 = $data['pass2']; + } + + if ( isset( $data['role'] ) && current_user_can( 'edit_users' ) ) { + $new_role = sanitize_text_field( $data['role'] ); + $potential_role = isset( $wp_roles->role_objects[ $new_role ] ) ? $wp_roles->role_objects[ $new_role ] : false; + // Don't let anyone with 'edit_users' (admins) edit their own role to something without it. + // Multisite super admins can freely edit their blog roles -- they possess all caps. + if ( ( is_multisite() && current_user_can( 'manage_sites' ) ) || get_current_user_id() != $user_id || ( $potential_role && $potential_role->has_cap( 'edit_users' ) ) ) { + $user->role = $new_role; + } + // If the new role isn't editable by the logged-in user die with error. + $editable_roles = get_editable_roles(); + if ( ! empty( $new_role ) && empty( $editable_roles[ $new_role ] ) ) { + return array( 'error' => 'You can’t give users that role.' ); + } + } + + $email = ''; + if ( isset( $data['email'] ) ) { + $email = trim( $data['email'] ); + } + + if ( ! empty( $email ) ) { + $user->user_email = sanitize_text_field( wp_unslash( $email ) ); + } else { + $user->user_email = $userdata->user_email; + } + + if ( isset( $data['url'] ) ) { + if ( empty( $data['url'] ) || 'http://' == $data['url'] ) { + $user->user_url = ''; + } else { + $user->user_url = esc_url_raw( $data['url'] ); + $protocols = implode( '|', array_map( 'preg_quote', wp_allowed_protocols() ) ); + $user->user_url = preg_match( '/^(' . $protocols . '):/is', $user->user_url ) ? $user->user_url : 'http://' . $user->user_url; + } + } + + if ( isset( $data['first_name'] ) ) { + $user->first_name = sanitize_text_field( $data['first_name'] ); + } + if ( isset( $data['last_name'] ) ) { + $user->last_name = sanitize_text_field( $data['last_name'] ); + } + if ( isset( $data['nickname'] ) && ! empty( $data['nickname'] ) ) { + $user->nickname = sanitize_text_field( $data['nickname'] ); + } + if ( isset( $data['display_name'] ) ) { + $user->display_name = sanitize_text_field( $data['display_name'] ); + } + if ( isset( $data['description'] ) ) { + $user->description = trim( $data['description'] ); + } + + $errors = new \WP_Error(); + + // checking that username has been typed. + if ( '' == $user->user_login ) { + $errors->add( 'user_login', __( 'ERROR: Please enter a username.' ) ); + } + + do_action_ref_array( 'check_passwords', array( $user->user_login, &$pass1, &$pass2 ) ); + + if ( ! empty( $pass1 ) || ! empty( $pass2 ) ) { + // Check for blank password when adding a user. + if ( ! $update && empty( $pass1 ) ) { + $errors->add( 'pass', __( 'ERROR: Please enter a password.' ), array( 'form-field' => 'pass1' ) ); + } + // Check for "\" in password. + if ( false !== strpos( wp_unslash( $pass1 ), '\\' ) ) { + $errors->add( 'pass', __( 'ERROR: Passwords may not contain the character "\\".' ), array( 'form-field' => 'pass1' ) ); + } + // Checking the password has been typed twice the same. + if ( ( $update || ! empty( $pass1 ) ) && $pass1 != $pass2 ) { + $errors->add( 'pass', __( 'ERROR: Please enter the same password in both password fields.' ), array( 'form-field' => 'pass1' ) ); + } + + if ( ! empty( $pass1 ) ) { + $user->user_pass = $pass1; + } + } else { + $user->user_pass = $userdata->user_pass; + } + + $illegal_logins = (array) apply_filters( 'illegal_user_logins', array() ); + + if ( in_array( strtolower( $user->user_login ), array_map( 'strtolower', $illegal_logins ) ) ) { + $errors->add( 'invalid_username', __( 'ERROR: Sorry, that username is not allowed.' ) ); + } + + $owner_id = email_exists( $user->user_email ); + + if ( empty( $user->user_email ) ) { + $errors->add( 'empty_email', __( 'ERROR: Please enter an email address.' ), array( 'form-field' => 'email' ) ); + } elseif ( ! is_email( $user->user_email ) ) { + $errors->add( 'invalid_email', __( 'ERROR: The email address isn’t correct.' ), array( 'form-field' => 'email' ) ); + } elseif ( ( $owner_id ) && ( ! $update || ( $owner_id != $user->ID ) ) ) { + $errors->add( 'email_exists', __( 'ERROR: This email is already registered, please choose another one.' ), array( 'form-field' => 'email' ) ); + } + + do_action_ref_array( 'user_profile_update_errors', array( &$errors, $update, &$user ) ); + + if ( $errors->get_error_codes() ) { + $error_str = ''; + foreach ( $errors->get_error_messages() as $message ) { + if ( is_string( $message ) ) { + $error_str .= ' ' . esc_html( wp_strip_all_tags( $message ) ); + } + } + return array( 'error' => $error_str ); + } + + $user_id = wp_update_user( $user ); + + return $user_id; + } + + public function get_user_to_edit( $user_id ) { + require_once ABSPATH . 'wp-admin/includes/user.php'; + $profileuser = get_user_to_edit( $user_id ); + + $edit_data = array(); + if ( is_object( $profileuser ) ) { + $user_roles = array_intersect( array_values( $profileuser->roles ), array_keys( get_editable_roles() ) ); + $user_role = reset( $user_roles ); + $edit_data['role'] = $user_role; + $edit_data['first_name'] = $profileuser->first_name; + $edit_data['last_name'] = $profileuser->last_name; + $edit_data['nickname'] = $profileuser->nickname; + + $public_display = array(); + $public_display['display_nickname'] = $profileuser->nickname; + $public_display['display_username'] = $profileuser->user_login; + + if ( ! empty( $profileuser->first_name ) ) { + $public_display['display_firstname'] = $profileuser->first_name; + } + + if ( ! empty( $profileuser->last_name ) ) { + $public_display['display_lastname'] = $profileuser->last_name; + } + + if ( ! empty( $profileuser->first_name ) && ! empty( $profileuser->last_name ) ) { + $public_display['display_firstlast'] = $profileuser->first_name . ' ' . $profileuser->last_name; + $public_display['display_lastfirst'] = $profileuser->last_name . ' ' . $profileuser->first_name; + } + + if ( ! in_array( $profileuser->display_name, $public_display ) ) { // Only add this if it isn't duplicated elsewhere! + $public_display = array( 'display_displayname' => $profileuser->display_name ) + $public_display; + } + + $public_display = array_map( 'trim', $public_display ); + $public_display = array_unique( $public_display ); + + $edit_data['public_display'] = $public_display; + $edit_data['display_name'] = $profileuser->display_name; + $edit_data['user_email'] = $profileuser->user_email; + $edit_data['user_url'] = $profileuser->user_url; + foreach ( wp_get_user_contact_methods( $profileuser ) as $name => $desc ) { + $edit_data['contact_methods'][ $name ] = $profileuser->$name; + } + $edit_data['description'] = $profileuser->description; + } + return $edit_data; + } + + + public function new_admin_password() { + $new_password = maybe_unserialize( base64_decode( $_POST['new_password'] ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. + $user = get_user_by( 'login', $_POST['user'] ); + require_once ABSPATH . WPINC . '/registration.php'; + + $id = wp_update_user( + array( + 'ID' => $user->ID, + 'user_pass' => $new_password['user_pass'], + ) + ); + if ( $id !== $user->ID ) { + if ( is_wp_error( $id ) ) { + MainWP_Helper::error( $id->get_error_message() ); + } else { + MainWP_Helper::error( __( 'Administrator password could not be changed.', 'mainwp-child' ) ); + } + } + + $information['added'] = true; + mainwp_child_helper()->write( $information ); + } + + public function new_user() { + $new_user = maybe_unserialize( base64_decode( $_POST['new_user'] ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. + $send_password = $_POST['send_password']; + if ( isset( $new_user['role'] ) ) { + if ( ! get_role( $new_user['role'] ) ) { + $new_user['role'] = 'subscriber'; + } + } + + $new_user_id = wp_insert_user( $new_user ); + + if ( is_wp_error( $new_user_id ) ) { + MainWP_Helper::error( $new_user_id->get_error_message() ); + } + if ( 0 === $new_user_id ) { + MainWP_Helper::error( __( 'Undefined error!', 'mainwp-child' ) ); + } + + if ( $send_password ) { + $user = new WP_User( $new_user_id ); + + $user_login = stripslashes( $user->user_login ); + $user_email = stripslashes( $user->user_email ); + + // The blogname option is escaped with esc_html on the way into the database in sanitize_option + // we want to reverse this for the plain text arena of emails. + $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); + + $message = sprintf( __( 'Username: %s' ), $user_login ) . "\r\n"; + $message .= sprintf( __( 'Password: %s' ), $new_user['user_pass'] ) . "\r\n"; + $message .= wp_login_url() . "\r\n"; + + wp_mail( $user_email, sprintf( __( '[%s] Your username and password' ), $blogname ), $message, '' ); + } + $information['added'] = true; + mainwp_child_helper()->write( $information ); + } + + +} diff --git a/class/class-mainwp-child.php b/class/class-mainwp-child.php index 029aea8..8286657 100644 --- a/class/class-mainwp-child.php +++ b/class/class-mainwp-child.php @@ -24,85 +24,10 @@ class MainWP_Child { public static $version = '4.0.7.1'; private $update_version = '1.5'; - private $callableFunctions = array( - 'stats' => 'get_site_stats', - 'upgrade' => 'upgrade_wp', - 'newpost' => 'new_post', - 'deactivate' => 'deactivate', - 'newuser' => 'new_user', - 'newadminpassword' => 'new_admin_password', - 'installplugintheme' => 'install_plugin_theme', - 'upgradeplugintheme' => 'upgrade_plugin_theme', - 'upgradetranslation' => 'upgrade_translation', - 'backup' => 'backup', - 'backup_checkpid' => 'backup_checkpid', - 'cloneinfo' => 'cloneinfo', - 'security' => 'get_security_stats', - 'securityFix' => 'do_security_fix', - 'securityUnFix' => 'do_security_un_fix', - 'post_action' => 'post_action', - 'get_all_posts' => 'get_all_posts', - 'comment_action' => 'comment_action', - 'comment_bulk_action' => 'comment_bulk_action', - 'get_all_comments' => 'get_all_comments', - 'get_all_themes' => 'get_all_themes', - 'theme_action' => 'theme_action', - 'get_all_plugins' => 'get_all_plugins', - 'plugin_action' => 'plugin_action', - 'get_all_pages' => 'get_all_pages', - 'get_all_users' => 'get_all_users', - 'user_action' => 'user_action', - 'search_users' => 'search_users', - 'insert_comment' => 'insert_comment', - 'cancel_scheduled_post' => 'cancel_scheduled_post', - 'serverInformation' => 'server_information', - 'maintenance_site' => 'maintenance_site', - 'keyword_links_action' => 'keyword_links_action', - 'branding_child_plugin' => 'branding_child_plugin', - 'code_snippet' => 'code_snippet', - 'uploader_action' => 'uploader_action', - 'wordpress_seo' => 'wordpress_seo', - 'client_report' => 'client_report', - 'createBackupPoll' => 'backup_poll', - 'page_speed' => 'page_speed', - 'woo_com_status' => 'woo_com_status', - 'links_checker' => 'links_checker', - 'wordfence' => 'wordfence', - 'delete_backup' => 'delete_backup', - 'update_values' => 'update_values', - 'ithemes' => 'ithemes', - 'updraftplus' => 'updraftplus', - 'backup_wp' => 'backup_wp', - 'backwpup' => 'backwpup', - 'wp_rocket' => 'wp_rocket', - 'settings_tools' => 'settings_tools', - 'skeleton_key' => 'skeleton_key', - 'custom_post_type' => 'custom_post_type', - 'backup_buddy' => 'backup_buddy', - 'get_site_icon' => 'get_site_icon', - 'vulner_checker' => 'vulner_checker', - 'wp_staging' => 'wp_staging', - 'disconnect' => 'disconnect', - 'time_capsule' => 'time_capsule', - 'extra_excution' => 'extra_execution', // deprecated! - 'extra_execution' => 'extra_execution', - 'wpvivid_backuprestore' => 'wpvivid_backuprestore', - ); - - private $FTP_ERROR = 'Failed! Please, add FTP details for automatic updates.'; - - private $callableFunctionsNoAuth = array( - 'stats' => 'get_site_stats_no_auth', - ); - - private $posts_where_suffix; - private $comments_and_clauses; - private $plugin_slug; - private $plugin_dir; - private $slug; + public $plugin_slug; + private $plugin_dir; private $maxHistory = 5; - private $filterFunction = null; public static $brandingTitle = null; public static $subPages; @@ -111,21 +36,10 @@ class MainWP_Child { public function __construct( $plugin_file ) { $this->update(); $this->load_all_options(); - $this->filterFunction = function( $a ) { - if ( null == $a ) { - return false; } - if ( is_object( $a ) && property_exists( $a, 'last_checked' ) && ! property_exists( $a, 'checked' ) ) { - return false; - } - return $a; - }; + $this->plugin_dir = dirname( $plugin_file ); $this->plugin_slug = plugin_basename( $plugin_file ); - list ( $t1, $t2 ) = explode( '/', $this->plugin_slug ); - $this->slug = str_replace( '.php', '', $t2 ); - - $this->posts_where_suffix = ''; - $this->comments_and_clauses = ''; + add_action( 'template_redirect', array( $this, 'template_redirect' ) ); add_action( 'init', array( &$this, 'check_login' ), 1 ); add_action( 'init', array( &$this, 'parse_init' ), 9999 ); @@ -483,10 +397,6 @@ class MainWP_Child { return apply_filters( 'mainwp_child_plugin_row_meta', $plugin_meta, $plugin_file, $this->plugin_slug ); } - public function get_callable_functions() { - return $this->callableFunctions; - } - public function admin_menu() { $branding_opts = MainWP_Child_Branding::instance()->get_branding_options(); $is_hide = isset( $branding_opts['hide'] ) ? $branding_opts['hide'] : ''; @@ -629,7 +539,7 @@ class MainWP_Child { ?>
> - settings(); ?> + render_settings(); ?>
@@ -863,7 +773,7 @@ class MainWP_Child { maxHistory; + } + public function mod_rewrite_rules( $pRules ) { $home_root = wp_parse_url( home_url() ); @@ -967,14 +881,10 @@ class MainWP_Child { $auth = $this->auth( isset( $_POST['mainwpsignature'] ) ? rawurldecode( $_POST['mainwpsignature'] ) : '', isset( $_POST['function'] ) ? $_POST['function'] : rawurldecode( ( isset( $_REQUEST['where'] ) ? $_REQUEST['where'] : $file ) ), isset( $_POST['nonce'] ) ? $_POST['nonce'] : '', isset( $_POST['nossl'] ) ? $_POST['nossl'] : 0 ); - if ( ! $auth && isset( $_POST['mainwpsignature'] ) ) { + if ( ! $auth ) { MainWP_Helper::error( __( 'Authentication failed! Please deactivate and re-activate the MainWP Child plugin on this site.', 'mainwp-child' ) ); } - - if ( ! $auth && isset( $_POST['function'] ) && isset( $this->callableFunctions[ $_POST['function'] ] ) && ! isset( $this->callableFunctionsNoAuth[ $_POST['function'] ] ) ) { - MainWP_Helper::error( __( 'Authentication failed! Please deactivate and re-activate the MainWP Child plugin on this site.', 'mainwp-child' ) ); - } - + $auth_user = false; if ( $auth ) { // disable duo auth for mainwp. @@ -1299,8 +1209,14 @@ class MainWP_Child { MainWP_Helper::error( __( 'Authentication failed! Please deactivate & re-activate the MainWP Child plugin on this site and try again.', 'mainwp-child' ) ); } - if ( ! $auth && isset( $_POST['function'] ) && isset( $this->callableFunctions[ $_POST['function'] ] ) && ! isset( $this->callableFunctionsNoAuth[ $_POST['function'] ] ) ) { - MainWP_Helper::error( __( 'Authentication failed! Please deactivate & re-activate the MainWP Child plugin on this site and try again.', 'mainwp-child' ) ); + if ( ! $auth && isset( $_POST['function'] ) ) { + $func = $_POST['function']; + $callable = MainWP_Child_Callable::get_instance()->is_callable_function( $func ); + $callable_no_auth = MainWP_Child_Callable::get_instance()->is_callable_function_no_auth( $func ); + + if ( $callable && ! $callable_no_auth ) { + MainWP_Helper::error( __( 'Authentication failed! Please deactivate & re-activate the MainWP Child plugin on this site and try again.', 'mainwp-child' ) ); + } } $auth_user = false; @@ -1376,18 +1292,41 @@ class MainWP_Child { $_wp_submenu_nopriv = array(); // phpcs:ignore -- to fix warning. } + $callable = false; + $func_auth = false; + + $callable_no_auth = false; + $func_no_auth = false; + + if ( isset( $_POST['function'] ) ) { + + $func = $_POST['function']; + + $callable = MainWP_Child_Callable::get_instance()->is_callable_function( $func ); + if ( $callable ) { + $func_auth = $func; + } + + if ( ! $callable ) { + $callable_no_auth = MainWP_Child_Callable::get_instance()->is_callable_function_no_auth( $func ); + if ( $callable_no_auth ) { + $func_no_auth = $func; + } + } + } + + // Call the function required. - if ( $auth && isset( $_POST['function'] ) && isset( $this->callableFunctions[ $_POST['function'] ] ) ) { + if ( $auth && isset( $_POST['function'] ) && $callable ) { define( 'DOING_CRON', true ); - MainWP_Helper::handle_fatal_error(); - self::fix_for_custom_themes(); - call_user_func( array( $this, $this->callableFunctions[ $_POST['function'] ] ) ); - } elseif ( isset( $_POST['function'] ) && isset( $this->callableFunctionsNoAuth[ $_POST['function'] ] ) ) { + self::fix_for_custom_themes(); + MainWP_Child_Callable::get_instance()->call_function( $func_auth ); + } elseif ( isset( $_POST['function'] ) && $callable_no_auth ) { define( 'DOING_CRON', true ); self::fix_for_custom_themes(); - call_user_func( array( $this, $this->callableFunctionsNoAuth[ $_POST['function'] ] ) ); - } elseif ( isset( $_POST['function'] ) && isset( $_POST['mainwpsignature'] ) && ! isset( $this->callableFunctions[ $_POST['function'] ] ) && ! isset( $this->callableFunctionsNoAuth[ $_POST['function'] ] ) ) { + MainWP_Child_Callable::get_instance()->call_function_no_auth( $func_no_auth ); + } elseif ( isset( $_POST['function'] ) && isset( $_POST['mainwpsignature'] ) && ! $callable && ! $callable_no_auth ) { MainWP_Helper::error( __( 'Required version has not been detected. Please, make sure that you are using the latest version of the MainWP Child plugin on your site.', 'mainwp-child' ) ); } @@ -1428,17 +1367,6 @@ class MainWP_Child { return false; } - public function default_option_active_plugins( $default ) { - if ( ! is_array( $default ) ) { - $default = array(); - } - if ( ! in_array( 'managewp/init.php', $default ) ) { - $default[] = 'managewp/init.php'; - } - - return $default; - } - public function auth( $signature, $func, $nonce, $pNossl ) { if ( empty( $signature ) || ! isset( $func ) || ( ! get_option( 'mainwp_child_pubkey' ) && ! get_option( 'mainwp_child_nossl_key' ) ) ) { $auth = false; @@ -1495,25 +1423,6 @@ class MainWP_Child { } - /** - * Functions to support core functionality - */ - public function install_plugin_theme() { - MainWP_Child_Install::get_instance()->install_plugin_theme(); - } - - public function upgrade_wp() { - MainWP_Child_Updates::get_instance()->upgrade_wp(); - } - - public function upgrade_translation() { - MainWP_Child_Updates::get_instance()->upgrade_translation(); - } - - public function upgrade_plugin_theme() { - MainWP_Child_Updates::get_instance()->upgrade_plugin_theme(); - } - // This will register the current wp - thus generating the public key etc. public function register_site() { global $current_user; @@ -1524,13 +1433,6 @@ class MainWP_Child { MainWP_Helper::error( __( 'Invalid request!', 'mainwp-child' ) ); } - $hint_miss_user = __( 'That administrator username was not found on this child site. Please verify that it is an existing administrator.', 'mainwp-child' ) . '
' . __( 'Hint: Check if the administrator user exists on the child site, if not, you need to use an existing administrator.', 'mainwp-child' ); - - $user = get_user_by( 'login', $_POST['user'] ); - if ( empty( $user ) ) { - MainWP_Helper::error( $hint_miss_user ); - } - // Already added - can't readd. Deactivate plugin. if ( get_option( 'mainwp_child_pubkey' ) ) { // set disconnect status to yes here, it will empty after reconnected. @@ -1553,7 +1455,8 @@ class MainWP_Child { // Login. if ( isset( $_POST['user'] ) ) { - if ( ! $this->login( $_POST['user'] ) ) { + if ( ! $this->login( $_POST['user'] ) ) { + $hint_miss_user = __( 'That administrator username was not found on this child site. Please verify that it is an existing administrator.', 'mainwp-child' ) . '
' . __( 'Hint: Check if the administrator user exists on the child site, if not, you need to use an existing administrator.', 'mainwp-child' ); MainWP_Helper::error( $hint_miss_user ); } @@ -1580,2691 +1483,8 @@ class MainWP_Child { $information['register'] = 'OK'; $information['uniqueId'] = get_option( 'mainwp_child_uniqueId', '' ); $information['user'] = $_POST['user']; - - $this->get_site_stats( $information ); - } - - public function new_post() { - $new_post = maybe_unserialize( base64_decode( $_POST['new_post'] ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. - $post_custom = maybe_unserialize( base64_decode( $_POST['post_custom'] ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. - $post_category = rawurldecode( isset( $_POST['post_category'] ) ? base64_decode( $_POST['post_category'] ) : null ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. - $post_tags = rawurldecode( isset( $new_post['post_tags'] ) ? $new_post['post_tags'] : null ); - $post_featured_image = base64_decode( $_POST['post_featured_image'] ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. - $upload_dir = maybe_unserialize( base64_decode( $_POST['mainwp_upload_dir'] ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. - - if ( isset( $_POST['_ezin_post_category'] ) ) { - $new_post['_ezin_post_category'] = maybe_unserialize( base64_decode( $_POST['_ezin_post_category'] ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. - } - - $others = array(); - if ( isset( $_POST['featured_image_data'] ) && ! empty( $_POST['featured_image_data'] ) ) { - $others['featured_image_data'] = unserialize( base64_decode( $_POST['featured_image_data'] ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. - } - - $res = MainWP_Helper::create_post( $new_post, $post_custom, $post_category, $post_featured_image, $upload_dir, $post_tags, $others ); - - if ( is_array( $res ) && isset( $res['error'] ) ) { - MainWP_Helper::error( $res['error'] ); - } - - $created = $res['success']; - if ( true !== $created ) { - MainWP_Helper::error( 'Undefined error' ); - } - - $information['added'] = true; - $information['added_id'] = $res['added_id']; - $information['link'] = $res['link']; - - do_action( 'mainwp_child_after_newpost', $res ); - - mainwp_child_helper()->write( $information ); - } - - public function post_action() { - $action = $_POST['action']; - $postId = $_POST['id']; - $my_post = array(); - - if ( 'publish' === $action ) { - $post_current = get_post( $postId ); - if ( empty( $post_current ) ) { - $information['status'] = 'FAIL'; - } else { - if ( 'future' == $post_current->post_status ) { - wp_publish_post( $postId ); - wp_update_post( - array( - 'ID' => $postId, - 'post_date' => current_time( 'mysql', false ), - 'post_date_gmt' => current_time( 'mysql', true ), - ) - ); - } else { - wp_update_post( - array( - 'ID' => $postId, - 'post_status' => 'publish', - ) - ); - } - } - } elseif ( 'update' === $action ) { - $postData = $_POST['post_data']; - $my_post = is_array( $postData ) ? $postData : array(); - wp_update_post( $my_post ); - } elseif ( 'unpublish' === $action ) { - $my_post['ID'] = $postId; - $my_post['post_status'] = 'draft'; - wp_update_post( $my_post ); - } elseif ( 'trash' === $action ) { - add_action( 'trash_post', array( '\MainWP_Child_Links_Checker', 'hook_post_deleted' ) ); - wp_trash_post( $postId ); - } elseif ( 'delete' === $action ) { - add_action( 'delete_post', array( '\MainWP_Child_Links_Checker', 'hook_post_deleted' ) ); - wp_delete_post( $postId, true ); - } elseif ( 'restore' === $action ) { - wp_untrash_post( $postId ); - } elseif ( 'update_meta' === $action ) { - $values = maybe_unserialize( base64_decode( $_POST['values'] ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. - $meta_key = $values['meta_key']; - $meta_value = $values['meta_value']; - $check_prev = $values['check_prev']; - - foreach ( $meta_key as $i => $key ) { - if ( 1 === intval( $check_prev[ $i ] ) ) { - update_post_meta( $postId, $key, get_post_meta( $postId, $key, true ) ? get_post_meta( $postId, $key, true ) : $meta_value[ $i ] ); - } else { - update_post_meta( $postId, $key, $meta_value[ $i ] ); - } - } - } elseif ( 'get_edit' === $action ) { - $postId = $_POST['id']; - $post_type = $_POST['post_type']; - if ( 'post' == $post_type ) { - $my_post = $this->get_post_edit( $postId ); - } else { - $my_post = $this->get_page_edit( $postId ); - } - } else { - $information['status'] = 'FAIL'; - } - - if ( ! isset( $information['status'] ) ) { - $information['status'] = 'SUCCESS'; - } - $information['my_post'] = $my_post; - mainwp_child_helper()->write( $information ); - } - - public function get_post_edit( $id ) { - $post = get_post( $id ); - if ( $post ) { - $categoryObjects = get_the_category( $post->ID ); - $categories = ''; - foreach ( $categoryObjects as $cat ) { - if ( '' !== $categories ) { - $categories .= ', '; - } - $categories .= $cat->name; - } - $post_category = $categories; - - $tagObjects = get_the_tags( $post->ID ); - $tags = ''; - if ( is_array( $tagObjects ) ) { - foreach ( $tagObjects as $tag ) { - if ( '' !== $tags ) { - $tags .= ', '; - } - $tags .= $tag->name; - } - } - $post_tags = $tags; - - $post_custom = get_post_custom( $id ); - - $galleries = get_post_gallery( $id, false ); - $post_gallery_images = array(); - - if ( is_array( $galleries ) && isset( $galleries['ids'] ) ) { - $attached_images = explode( ',', $galleries['ids'] ); - foreach ( $attached_images as $attachment_id ) { - $attachment = get_post( $attachment_id ); - if ( $attachment ) { - $post_gallery_images[] = array( - 'id' => $attachment_id, - 'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ), - 'caption' => $attachment->post_excerpt, - 'description' => $attachment->post_content, - 'src' => $attachment->guid, - 'title' => $attachment->post_title, - ); - } - } - } - - include_once ABSPATH . 'wp-includes' . DIRECTORY_SEPARATOR . 'post-thumbnail-template.php'; - $post_featured_image = get_post_thumbnail_id( $id ); - $child_upload_dir = wp_upload_dir(); - $new_post = array( - 'edit_id' => $id, - 'is_sticky' => is_sticky( $id ) ? 1 : 0, - 'post_title' => $post->post_title, - 'post_content' => $post->post_content, - 'post_status' => $post->post_status, - 'post_date' => $post->post_date, - 'post_date_gmt' => $post->post_date_gmt, - 'post_tags' => $post_tags, - 'post_name' => $post->post_name, - 'post_excerpt' => $post->post_excerpt, - 'comment_status' => $post->comment_status, - 'ping_status' => $post->ping_status, - ); - - if ( null != $post_featured_image ) { // Featured image is set, retrieve URL. - $img = wp_get_attachment_image_src( $post_featured_image, 'full' ); - $post_featured_image = $img[0]; - } - - require_once ABSPATH . 'wp-admin/includes/post.php'; - wp_set_post_lock( $id ); - - $post_data = array( - 'new_post' => base64_encode( serialize( $new_post ) ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. - 'post_custom' => base64_encode( serialize( $post_custom ) ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. - 'post_category' => base64_encode( $post_category ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. - 'post_featured_image' => base64_encode( $post_featured_image ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. - 'post_gallery_images' => base64_encode( serialize( $post_gallery_images ) ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. - 'child_upload_dir' => base64_encode( serialize( $child_upload_dir ) ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. - ); - return $post_data; - - } - return false; - } - - public function get_page_edit( $id ) { - $post = get_post( $id ); - if ( $post ) { - $post_custom = get_post_custom( $id ); - include_once ABSPATH . 'wp-includes' . DIRECTORY_SEPARATOR . 'post-thumbnail-template.php'; - $post_featured_image = get_post_thumbnail_id( $id ); - $child_upload_dir = wp_upload_dir(); - - $new_post = array( - 'edit_id' => $id, - 'post_title' => $post->post_title, - 'post_content' => $post->post_content, - 'post_status' => $post->post_status, - 'post_date' => $post->post_date, - 'post_date_gmt' => $post->post_date_gmt, - 'post_type' => 'page', - 'post_name' => $post->post_name, - 'post_excerpt' => $post->post_excerpt, - 'comment_status' => $post->comment_status, - 'ping_status' => $post->ping_status, - ); - - if ( null != $post_featured_image ) { - $img = wp_get_attachment_image_src( $post_featured_image, 'full' ); - $post_featured_image = $img[0]; - } - - $galleries = get_post_gallery( $id, false ); - $post_gallery_images = array(); - - if ( is_array( $galleries ) && isset( $galleries['ids'] ) ) { - $attached_images = explode( ',', $galleries['ids'] ); - foreach ( $attached_images as $attachment_id ) { - $attachment = get_post( $attachment_id ); - if ( $attachment ) { - $post_gallery_images[] = array( - 'id' => $attachment_id, - 'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ), - 'caption' => $attachment->post_excerpt, - 'description' => $attachment->post_content, - 'src' => $attachment->guid, - 'title' => $attachment->post_title, - ); - } - } - } - - require_once ABSPATH . 'wp-admin/includes/post.php'; - wp_set_post_lock( $id ); - - $post_data = array( - 'new_post' => base64_encode( serialize( $new_post ) ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. - 'post_custom' => base64_encode( serialize( $post_custom ) ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. - 'post_featured_image' => base64_encode( $post_featured_image ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. - 'post_gallery_images' => base64_encode( serialize( $post_gallery_images ) ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. - 'child_upload_dir' => base64_encode( serialize( $child_upload_dir ) ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. - ); - return $post_data; - } - return false; - } - - public function user_action() { - $action = $_POST['action']; - $extra = $_POST['extra']; - $userId = $_POST['id']; - $user_pass = $_POST['user_pass']; - $failed = false; - - global $current_user; - $reassign = ( isset( $current_user ) && isset( $current_user->ID ) ) ? $current_user->ID : 0; - include_once ABSPATH . '/wp-admin/includes/user.php'; - - if ( 'delete' === $action ) { - wp_delete_user( $userId, $reassign ); - } elseif ( 'changeRole' === $action ) { - $my_user = array(); - $my_user['ID'] = $userId; - $my_user['role'] = $extra; - wp_update_user( $my_user ); - } elseif ( 'update_password' === $action ) { - $my_user = array(); - $my_user['ID'] = $userId; - $my_user['user_pass'] = $user_pass; - wp_update_user( $my_user ); - } elseif ( 'edit' === $action ) { - $user_data = $this->get_user_to_edit( $userId ); - if ( ! empty( $user_data ) ) { - $information['user_data'] = $user_data; - } else { - $failed = true; - } - } elseif ( 'update_user' === $action ) { - $my_user = $_POST['extra']; - if ( is_array( $my_user ) ) { - foreach ( $my_user as $idx => $val ) { - if ( 'donotupdate' === $val || ( empty( $val ) && 'role' !== $idx ) ) { - unset( $my_user[ $idx ] ); - } - } - $result = $this->edit_user( $userId, $my_user ); - if ( is_array( $result ) && isset( $result['error'] ) ) { - $information['error'] = $result['error']; - } - } else { - $failed = true; - } - } else { - $failed = true; - } - - if ( $failed ) { - $information['status'] = 'FAIL'; - } - - if ( ! isset( $information['status'] ) && ! isset( $information['error'] ) ) { - $information['status'] = 'SUCCESS'; - if ( 'update_user' === $action && isset( $_POST['optimize'] ) && ! empty( $_POST['optimize'] ) ) { - $information['users'] = $this->get_all_users_int( 500 ); - } - } - mainwp_child_helper()->write( $information ); - } - - public function edit_user( $user_id, $data ) { - $wp_roles = wp_roles(); - $user = new stdClass(); - - $update = true; - - if ( $user_id ) { - $user->ID = (int) $user_id; - $userdata = get_userdata( $user_id ); - $user->user_login = wp_slash( $userdata->user_login ); - } else { - return array( 'error' => 'ERROR: Empty user id.' ); - } - - $pass1 = ''; - $pass2 = ''; - - if ( isset( $data['pass1'] ) ) { - $pass1 = $data['pass1']; - } - - if ( isset( $data['pass2'] ) ) { - $pass2 = $data['pass2']; - } - - if ( isset( $data['role'] ) && current_user_can( 'edit_users' ) ) { - $new_role = sanitize_text_field( $data['role'] ); - $potential_role = isset( $wp_roles->role_objects[ $new_role ] ) ? $wp_roles->role_objects[ $new_role ] : false; - // Don't let anyone with 'edit_users' (admins) edit their own role to something without it. - // Multisite super admins can freely edit their blog roles -- they possess all caps. - if ( ( is_multisite() && current_user_can( 'manage_sites' ) ) || get_current_user_id() != $user_id || ( $potential_role && $potential_role->has_cap( 'edit_users' ) ) ) { - $user->role = $new_role; - } - // If the new role isn't editable by the logged-in user die with error. - $editable_roles = get_editable_roles(); - if ( ! empty( $new_role ) && empty( $editable_roles[ $new_role ] ) ) { - return array( 'error' => 'You can’t give users that role.' ); - } - } - - $email = ''; - if ( isset( $data['email'] ) ) { - $email = trim( $data['email'] ); - } - - if ( ! empty( $email ) ) { - $user->user_email = sanitize_text_field( wp_unslash( $email ) ); - } else { - $user->user_email = $userdata->user_email; - } - - if ( isset( $data['url'] ) ) { - if ( empty( $data['url'] ) || 'http://' == $data['url'] ) { - $user->user_url = ''; - } else { - $user->user_url = esc_url_raw( $data['url'] ); - $protocols = implode( '|', array_map( 'preg_quote', wp_allowed_protocols() ) ); - $user->user_url = preg_match( '/^(' . $protocols . '):/is', $user->user_url ) ? $user->user_url : 'http://' . $user->user_url; - } - } - - if ( isset( $data['first_name'] ) ) { - $user->first_name = sanitize_text_field( $data['first_name'] ); - } - if ( isset( $data['last_name'] ) ) { - $user->last_name = sanitize_text_field( $data['last_name'] ); - } - if ( isset( $data['nickname'] ) && ! empty( $data['nickname'] ) ) { - $user->nickname = sanitize_text_field( $data['nickname'] ); - } - if ( isset( $data['display_name'] ) ) { - $user->display_name = sanitize_text_field( $data['display_name'] ); - } - if ( isset( $data['description'] ) ) { - $user->description = trim( $data['description'] ); - } - - $errors = new \WP_Error(); - - // checking that username has been typed. - if ( '' == $user->user_login ) { - $errors->add( 'user_login', __( 'ERROR: Please enter a username.' ) ); - } - - do_action_ref_array( 'check_passwords', array( $user->user_login, &$pass1, &$pass2 ) ); - - if ( ! empty( $pass1 ) || ! empty( $pass2 ) ) { - // Check for blank password when adding a user. - if ( ! $update && empty( $pass1 ) ) { - $errors->add( 'pass', __( 'ERROR: Please enter a password.' ), array( 'form-field' => 'pass1' ) ); - } - // Check for "\" in password. - if ( false !== strpos( wp_unslash( $pass1 ), '\\' ) ) { - $errors->add( 'pass', __( 'ERROR: Passwords may not contain the character "\\".' ), array( 'form-field' => 'pass1' ) ); - } - // Checking the password has been typed twice the same. - if ( ( $update || ! empty( $pass1 ) ) && $pass1 != $pass2 ) { - $errors->add( 'pass', __( 'ERROR: Please enter the same password in both password fields.' ), array( 'form-field' => 'pass1' ) ); - } - - if ( ! empty( $pass1 ) ) { - $user->user_pass = $pass1; - } - } else { - $user->user_pass = $userdata->user_pass; - } - - $illegal_logins = (array) apply_filters( 'illegal_user_logins', array() ); - - if ( in_array( strtolower( $user->user_login ), array_map( 'strtolower', $illegal_logins ) ) ) { - $errors->add( 'invalid_username', __( 'ERROR: Sorry, that username is not allowed.' ) ); - } - - $owner_id = email_exists( $user->user_email ); - - if ( empty( $user->user_email ) ) { - $errors->add( 'empty_email', __( 'ERROR: Please enter an email address.' ), array( 'form-field' => 'email' ) ); - } elseif ( ! is_email( $user->user_email ) ) { - $errors->add( 'invalid_email', __( 'ERROR: The email address isn’t correct.' ), array( 'form-field' => 'email' ) ); - } elseif ( ( $owner_id ) && ( ! $update || ( $owner_id != $user->ID ) ) ) { - $errors->add( 'email_exists', __( 'ERROR: This email is already registered, please choose another one.' ), array( 'form-field' => 'email' ) ); - } - - do_action_ref_array( 'user_profile_update_errors', array( &$errors, $update, &$user ) ); - - if ( $errors->get_error_codes() ) { - $error_str = ''; - foreach ( $errors->get_error_messages() as $message ) { - if ( is_string( $message ) ) { - $error_str .= ' ' . esc_html( wp_strip_all_tags( $message ) ); - } - } - return array( 'error' => $error_str ); - } - - $user_id = wp_update_user( $user ); - - return $user_id; - } - - public function get_user_to_edit( $user_id ) { - require_once ABSPATH . 'wp-admin/includes/user.php'; - $profileuser = get_user_to_edit( $user_id ); - - $edit_data = array(); - if ( is_object( $profileuser ) ) { - $user_roles = array_intersect( array_values( $profileuser->roles ), array_keys( get_editable_roles() ) ); - $user_role = reset( $user_roles ); - $edit_data['role'] = $user_role; - $edit_data['first_name'] = $profileuser->first_name; - $edit_data['last_name'] = $profileuser->last_name; - $edit_data['nickname'] = $profileuser->nickname; - - $public_display = array(); - $public_display['display_nickname'] = $profileuser->nickname; - $public_display['display_username'] = $profileuser->user_login; - - if ( ! empty( $profileuser->first_name ) ) { - $public_display['display_firstname'] = $profileuser->first_name; - } - - if ( ! empty( $profileuser->last_name ) ) { - $public_display['display_lastname'] = $profileuser->last_name; - } - - if ( ! empty( $profileuser->first_name ) && ! empty( $profileuser->last_name ) ) { - $public_display['display_firstlast'] = $profileuser->first_name . ' ' . $profileuser->last_name; - $public_display['display_lastfirst'] = $profileuser->last_name . ' ' . $profileuser->first_name; - } - - if ( ! in_array( $profileuser->display_name, $public_display ) ) { // Only add this if it isn't duplicated elsewhere! - $public_display = array( 'display_displayname' => $profileuser->display_name ) + $public_display; - } - - $public_display = array_map( 'trim', $public_display ); - $public_display = array_unique( $public_display ); - - $edit_data['public_display'] = $public_display; - $edit_data['display_name'] = $profileuser->display_name; - $edit_data['user_email'] = $profileuser->user_email; - $edit_data['user_url'] = $profileuser->user_url; - foreach ( wp_get_user_contact_methods( $profileuser ) as $name => $desc ) { - $edit_data['contact_methods'][ $name ] = $profileuser->$name; - } - $edit_data['description'] = $profileuser->description; - } - return $edit_data; - } - - public function comment_action() { - $action = $_POST['action']; - $commentId = $_POST['id']; - - if ( 'approve' === $action ) { - wp_set_comment_status( $commentId, 'approve' ); - } elseif ( 'unapprove' === $action ) { - wp_set_comment_status( $commentId, 'hold' ); - } elseif ( 'spam' === $action ) { - wp_spam_comment( $commentId ); - } elseif ( 'unspam' === $action ) { - wp_unspam_comment( $commentId ); - } elseif ( 'trash' === $action ) { - add_action( 'trashed_comment', array( '\MainWP_Child_Links_Checker', 'hook_trashed_comment' ), 10, 1 ); - wp_trash_comment( $commentId ); - } elseif ( 'restore' === $action ) { - wp_untrash_comment( $commentId ); - } elseif ( 'delete' === $action ) { - wp_delete_comment( $commentId, true ); - } else { - $information['status'] = 'FAIL'; - } - - if ( ! isset( $information['status'] ) ) { - $information['status'] = 'SUCCESS'; - } - mainwp_child_helper()->write( $information ); - } - - public function comment_bulk_action() { - $action = $_POST['action']; - $commentIds = explode( ',', $_POST['ids'] ); - $information['success'] = 0; - foreach ( $commentIds as $commentId ) { - if ( $commentId ) { - $information['success'] ++; - if ( 'approve' === $action ) { - wp_set_comment_status( $commentId, 'approve' ); - } elseif ( 'unapprove' === $action ) { - wp_set_comment_status( $commentId, 'hold' ); - } elseif ( 'spam' === $action ) { - wp_spam_comment( $commentId ); - } elseif ( 'unspam' === $action ) { - wp_unspam_comment( $commentId ); - } elseif ( 'trash' === $action ) { - wp_trash_comment( $commentId ); - } elseif ( 'restore' === $action ) { - wp_untrash_comment( $commentId ); - } elseif ( 'delete' === $action ) { - wp_delete_comment( $commentId, true ); - } else { - $information['success']--; - } - } - } - mainwp_child_helper()->write( $information ); - } - - - public function new_admin_password() { - $new_password = maybe_unserialize( base64_decode( $_POST['new_password'] ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. - $user = get_user_by( 'login', $_POST['user'] ); - require_once ABSPATH . WPINC . '/registration.php'; - - $id = wp_update_user( - array( - 'ID' => $user->ID, - 'user_pass' => $new_password['user_pass'], - ) - ); - if ( $id !== $user->ID ) { - if ( is_wp_error( $id ) ) { - MainWP_Helper::error( $id->get_error_message() ); - } else { - MainWP_Helper::error( __( 'Administrator password could not be changed.', 'mainwp-child' ) ); - } - } - - $information['added'] = true; - mainwp_child_helper()->write( $information ); - } - - public function new_user() { - $new_user = maybe_unserialize( base64_decode( $_POST['new_user'] ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. - $send_password = $_POST['send_password']; - if ( isset( $new_user['role'] ) ) { - if ( ! get_role( $new_user['role'] ) ) { - $new_user['role'] = 'subscriber'; - } - } - - $new_user_id = wp_insert_user( $new_user ); - - if ( is_wp_error( $new_user_id ) ) { - MainWP_Helper::error( $new_user_id->get_error_message() ); - } - if ( 0 === $new_user_id ) { - MainWP_Helper::error( __( 'Undefined error!', 'mainwp-child' ) ); - } - - if ( $send_password ) { - $user = new WP_User( $new_user_id ); - - $user_login = stripslashes( $user->user_login ); - $user_email = stripslashes( $user->user_email ); - - // The blogname option is escaped with esc_html on the way into the database in sanitize_option - // we want to reverse this for the plain text arena of emails. - $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); - - $message = sprintf( __( 'Username: %s' ), $user_login ) . "\r\n"; - $message .= sprintf( __( 'Password: %s' ), $new_user['user_pass'] ) . "\r\n"; - $message .= wp_login_url() . "\r\n"; - - wp_mail( $user_email, sprintf( __( '[%s] Your username and password' ), $blogname ), $message, '' ); - } - $information['added'] = true; - mainwp_child_helper()->write( $information ); - } - - public function cloneinfo() { - global $table_prefix; - $information['dbCharset'] = DB_CHARSET; - $information['dbCollate'] = DB_COLLATE; - $information['table_prefix'] = $table_prefix; - $information['site_url'] = get_option( 'site_url' ); - $information['home'] = get_option( 'home' ); - - mainwp_child_helper()->write( $information ); - } - - public function backup_poll() { - $fileNameUID = ( isset( $_POST['fileNameUID'] ) ? $_POST['fileNameUID'] : '' ); - $fileName = ( isset( $_POST['fileName'] ) ? $_POST['fileName'] : '' ); - - if ( 'full' === $_POST['type'] ) { - if ( '' !== $fileName ) { - $backupFile = $fileName; - } else { - $backupFile = 'backup-' . $fileNameUID . '-'; - } - - $dirs = MainWP_Helper::get_mainwp_dir( 'backup' ); - $backupdir = $dirs[0]; - $result = glob( $backupdir . $backupFile . '*' ); - $archiveFile = false; - foreach ( $result as $file ) { - if ( MainWP_Helper::is_archive( $file, $backupFile, '(.*)' ) ) { - $archiveFile = $file; - break; - } - } - if ( false === $archiveFile ) { - mainwp_child_helper()->write( array() ); - } - - mainwp_child_helper()->write( array( 'size' => filesize( $archiveFile ) ) ); - } else { - $backupFile = 'dbBackup-' . $fileNameUID . '-*.sql'; - - $dirs = MainWP_Helper::get_mainwp_dir( 'backup' ); - $backupdir = $dirs[0]; - $result = glob( $backupdir . $backupFile . '*' ); - if ( 0 === count( $result ) ) { - mainwp_child_helper()->write( array() ); - } - - $size = 0; - foreach ( $result as $f ) { - $size += filesize( $f ); - } - mainwp_child_helper()->write( array( 'size' => $size ) ); - exit(); - } - } - - public function backup_checkpid() { - $pid = $_POST['pid']; - - $dirs = MainWP_Helper::get_mainwp_dir( 'backup' ); - $backupdir = $dirs[0]; - - $information = array(); - - /** @var $wp_filesystem WP_Filesystem_Base */ - global $wp_filesystem; - - MainWP_Helper::get_wp_filesystem(); - - $pidFile = trailingslashit( $backupdir ) . 'backup-' . $pid . '.pid'; - $doneFile = trailingslashit( $backupdir ) . 'backup-' . $pid . '.done'; - if ( $wp_filesystem->is_file( $pidFile ) ) { - $time = $wp_filesystem->mtime( $pidFile ); - - $minutes = date( 'i', time() ); // phpcs:ignore -- local time. - $seconds = date( 's', time() ); // phpcs:ignore -- local time. - - $file_minutes = date( 'i', $time ); // phpcs:ignore -- local time. - $file_seconds = date( 's', $time ); // phpcs:ignore -- local time. - - $minuteDiff = $minutes - $file_minutes; - if ( 59 === $minuteDiff ) { - $minuteDiff = 1; - } - $secondsdiff = ( $minuteDiff * 60 ) + $seconds - $file_seconds; - - $file = $wp_filesystem->get_contents( $pidFile ); - $information['file'] = basename( $file ); - if ( $secondsdiff < 80 ) { - $information['status'] = 'busy'; - } else { - $information['status'] = 'stalled'; - } - } elseif ( $wp_filesystem->is_file( $doneFile ) ) { - $file = $wp_filesystem->get_contents( $doneFile ); - $information['status'] = 'done'; - $information['file'] = basename( $file ); - $information['size'] = filesize( $file ); - } else { - $information['status'] = 'invalid'; - } - - mainwp_child_helper()->write( $information ); - } - - public function backup( $pWrite = true ) { - - $timeout = 20 * 60 * 60; - set_time_limit( $timeout ); - ini_set( 'max_execution_time', $timeout ); // phpcs:ignore - MainWP_Helper::end_session(); - - // Cleanup pid files! - $dirs = MainWP_Helper::get_mainwp_dir( 'backup' ); - $backupdir = trailingslashit( $dirs[0] ); - - /** @var $wp_filesystem WP_Filesystem_Base */ - global $wp_filesystem; - - MainWP_Helper::get_wp_filesystem(); - - $files = glob( $backupdir . '*' ); - foreach ( $files as $file ) { - if ( MainWP_Helper::ends_with( $file, '/index.php' ) | MainWP_Helper::ends_with( $file, '/.htaccess' ) ) { - continue; - } - - if ( ( time() - filemtime( $file ) ) > ( 60 * 60 * 3 ) ) { - unlink( $file ); - } - } - - $fileName = ( isset( $_POST['fileUID'] ) ? $_POST['fileUID'] : '' ); - if ( 'full' === $_POST['type'] ) { - $excludes = ( isset( $_POST['exclude'] ) ? explode( ',', $_POST['exclude'] ) : array() ); - $excludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/uploads/mainwp'; - $uploadDir = MainWP_Helper::get_mainwp_dir(); - $uploadDir = $uploadDir[0]; - $excludes[] = str_replace( ABSPATH, '', $uploadDir ); - $excludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/object-cache.php'; - - if ( function_exists( 'posix_uname' ) ) { - $uname = posix_uname(); - if ( is_array( $uname ) && isset( $uname['nodename'] ) ) { - if ( stristr( $uname['nodename'], 'hostgator' ) ) { - if ( ! isset( $_POST['file_descriptors'] ) || '0' == $_POST['file_descriptors'] || $_POST['file_descriptors'] > 1000 ) { - $_POST['file_descriptors'] = 1000; - } - $_POST['file_descriptors_auto'] = 0; - $_POST['loadFilesBeforeZip'] = false; - } - } - } - - $file_descriptors = ( isset( $_POST['file_descriptors'] ) ? $_POST['file_descriptors'] : 0 ); - $file_descriptors_auto = ( isset( $_POST['file_descriptors_auto'] ) ? $_POST['file_descriptors_auto'] : 0 ); - if ( 1 === (int) $file_descriptors_auto ) { - if ( function_exists( 'posix_getrlimit' ) ) { - $result = posix_getrlimit(); - if ( isset( $result['soft openfiles'] ) ) { - $file_descriptors = $result['soft openfiles']; - } - } - } - - $loadFilesBeforeZip = ( isset( $_POST['loadFilesBeforeZip'] ) ? $_POST['loadFilesBeforeZip'] : true ); - - $newExcludes = array(); - foreach ( $excludes as $exclude ) { - $newExcludes[] = rtrim( $exclude, '/' ); - } - - $excludebackup = ( isset( $_POST['excludebackup'] ) && '1' == $_POST['excludebackup'] ); - $excludecache = ( isset( $_POST['excludecache'] ) && '1' == $_POST['excludecache'] ); - $excludezip = ( isset( $_POST['excludezip'] ) && '1' == $_POST['excludezip'] ); - $excludenonwp = ( isset( $_POST['excludenonwp'] ) && '1' == $_POST['excludenonwp'] ); - - if ( $excludebackup ) { - $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/uploads/backupbuddy_backups'; - $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/uploads/backupbuddy_temp'; - $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/uploads/pb_backupbuddy'; - $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/managewp'; - $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/infinitewp'; - $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/backups'; - $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/backups'; - $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/uploads/backwpup*'; - $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/plugins/wp-complete-backup/storage'; - $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/backups'; - $newExcludes[] = '/administrator/backups'; - } - - if ( $excludecache ) { - $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/w3tc-cache'; - $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/w3tc'; - $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/cache/config'; - $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/cache/minify'; - $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/cache/page_enhanced'; - $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/cache/tmp'; - $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/cache/supercache'; - $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/cache/quick-cache'; - $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/hyper-cache/cache'; - $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/cache/all'; - $newExcludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/cache/wp-rocket'; - } - - $file = false; - if ( isset( $_POST['f'] ) ) { - $file = $_POST['f']; - } elseif ( isset( $_POST['file'] ) ) { - $file = $_POST['file']; - } - - $ext = 'zip'; - if ( isset( $_POST['ext'] ) ) { - $ext = $_POST['ext']; - } - - $pid = false; - if ( isset( $_POST['pid'] ) ) { - $pid = $_POST['pid']; - } - - $append = ( isset( $_POST['append'] ) && ( '1' == $_POST['append'] ) ); - - $res = MainWP_Backup::get()->create_full_backup( $newExcludes, $fileName, true, true, $file_descriptors, $file, $excludezip, $excludenonwp, $loadFilesBeforeZip, $ext, $pid, $append ); - if ( ! $res ) { - $information['full'] = false; - } else { - $information['full'] = $res['file']; - $information['size'] = $res['filesize']; - } - $information['db'] = false; - } elseif ( 'db' == $_POST['type'] ) { - $ext = 'zip'; - if ( isset( $_POST['ext'] ) ) { - $ext = $_POST['ext']; - } - - $res = $this->backup_db( $fileName, $ext ); - if ( ! $res ) { - $information['db'] = false; - } else { - $information['db'] = $res['file']; - $information['size'] = $res['filesize']; - } - $information['full'] = false; - } else { - $information['full'] = false; - $information['db'] = false; - } - - if ( $pWrite ) { - mainwp_child_helper()->write( $information ); - } - - return $information; - } - - protected function backup_db( $fileName = '', $ext = 'zip' ) { - $dirs = MainWP_Helper::get_mainwp_dir( 'backup' ); - $dir = $dirs[0]; - $timestamp = time(); - - if ( '' !== $fileName ) { - $fileName .= '-'; - } - - $filepath_prefix = $dir . 'dbBackup-' . $fileName . $timestamp; - - $dh = opendir( $dir ); - - if ( $dh ) { - while ( ( $file = readdir( $dh ) ) !== false ) { - if ( '.' !== $file && '..' !== $file && ( preg_match( '/dbBackup-(.*).sql(\.zip|\.tar|\.tar\.gz|\.tar\.bz2|\.tmp)?$/', $file ) ) ) { - unlink( $dir . $file ); - } - } - closedir( $dh ); - } - - $result = MainWP_Backup::get()->create_backup_db( $filepath_prefix, $ext ); - - MainWP_Helper::update_option( 'mainwp_child_last_db_backup_size', filesize( $result['filepath'] ) ); - - return ( ! $result ) ? false : array( - 'timestamp' => $timestamp, - 'file' => basename( $result['filepath'] ), - 'filesize' => filesize( $result['filepath'] ), - ); - } - - public function do_security_fix() { - $sync = false; - if ( 'all' === $_POST['feature'] ) { - $sync = true; - } - - $information = array(); - $security = get_option( 'mainwp_security' ); - if ( ! is_array( $security ) ) { - $security = array(); - } - - if ( 'all' === $_POST['feature'] || 'listing' === $_POST['feature'] ) { - MainWP_Security::prevent_listing(); - $information['listing'] = ( ! MainWP_Security::prevent_listing_ok() ? 'N' : 'Y' ); - } - - if ( 'all' === $_POST['feature'] || 'wp_version' === $_POST['feature'] ) { - $security['wp_version'] = true; - MainWP_Security::remove_wp_version( true ); - $information['wp_version'] = ( ! MainWP_Security::remove_wp_version_ok() ? 'N' : 'Y' ); - } - - if ( 'all' === $_POST['feature'] || 'rsd' === $_POST['feature'] ) { - $security['rsd'] = true; - MainWP_Security::remove_rsd( true ); - $information['rsd'] = ( ! MainWP_Security::remove_rsd_ok() ? 'N' : 'Y' ); - } - - if ( 'all' === $_POST['feature'] || 'wlw' === $_POST['feature'] ) { - $security['wlw'] = true; - MainWP_Security::remove_wlw( true ); - $information['wlw'] = ( ! MainWP_Security::remove_wlw_ok() ? 'N' : 'Y' ); - } - - if ( 'all' === $_POST['feature'] || 'db_reporting' === $_POST['feature'] ) { - MainWP_Security::remove_database_reporting(); - $information['db_reporting'] = ( ! MainWP_Security::remove_database_reporting_ok() ? 'N' : 'Y' ); - } - - if ( 'all' === $_POST['feature'] || 'php_reporting' === $_POST['feature'] ) { - $security['php_reporting'] = true; - MainWP_Security::remove_php_reporting( true ); - $information['php_reporting'] = ( ! MainWP_Security::remove_php_reporting_ok() ? 'N' : 'Y' ); - } - - if ( 'all' === $_POST['feature'] || 'versions' === $_POST['feature'] ) { - $security['scripts_version'] = true; - $security['styles_version'] = true; - $security['generator_version'] = true; - MainWP_Security::remove_generator_version( true ); - $information['versions'] = 'Y'; - } - - if ( 'all' === $_POST['feature'] || 'registered_versions' === $_POST['feature'] ) { - $security['registered_versions'] = true; - $information['registered_versions'] = 'Y'; - } - - if ( 'all' === $_POST['feature'] || 'admin' === $_POST['feature'] ) { - $information['admin'] = ( ! MainWP_Security::admin_user_ok() ? 'N' : 'Y' ); - } - - if ( 'all' === $_POST['feature'] || 'readme' === $_POST['feature'] ) { - $security['readme'] = true; - MainWP_Security::remove_readme( true ); - $information['readme'] = ( MainWP_Security::remove_readme_ok() ? 'Y' : 'N' ); - } - - MainWP_Helper::update_option( 'mainwp_security', $security, 'yes' ); - - if ( $sync ) { - $information['sync'] = $this->get_site_stats( array(), false ); - } - mainwp_child_helper()->write( $information ); - } - - public function do_security_un_fix() { - $information = array(); - - $sync = false; - if ( 'all' === $_POST['feature'] ) { - $sync = true; - } - - $security = get_option( 'mainwp_security' ); - - if ( 'all' === $_POST['feature'] || 'wp_version' === $_POST['feature'] ) { - $security['wp_version'] = false; - $information['wp_version'] = 'N'; - } - - if ( 'all' === $_POST['feature'] || 'rsd' === $_POST['feature'] ) { - $security['rsd'] = false; - $information['rsd'] = 'N'; - } - - if ( 'all' === $_POST['feature'] || 'wlw' === $_POST['feature'] ) { - $security['wlw'] = false; - $information['wlw'] = 'N'; - } - - if ( 'all' === $_POST['feature'] || 'php_reporting' === $_POST['feature'] ) { - $security['php_reporting'] = false; - $information['php_reporting'] = 'N'; - } - - if ( 'all' === $_POST['feature'] || 'versions' === $_POST['feature'] ) { - $security['scripts_version'] = false; - $security['styles_version'] = false; - $security['generator_version'] = false; - $information['versions'] = 'N'; - } - - if ( 'all' === $_POST['feature'] || 'registered_versions' === $_POST['feature'] ) { - $security['registered_versions'] = false; - $information['registered_versions'] = 'N'; - } - if ( 'all' === $_POST['feature'] || 'readme' === $_POST['feature'] ) { - $security['readme'] = false; - $information['readme'] = MainWP_Security::remove_readme_ok(); - } - - MainWP_Helper::update_option( 'mainwp_security', $security, 'yes' ); - - if ( $sync ) { - $information['sync'] = $this->get_site_stats( array(), false ); - } - - mainwp_child_helper()->write( $information ); - } - - public function get_security_stats() { - $information = array(); - - $information['listing'] = ( ! MainWP_Security::prevent_listing_ok() ? 'N' : 'Y' ); - $information['wp_version'] = ( ! MainWP_Security::remove_wp_version_ok() ? 'N' : 'Y' ); - $information['rsd'] = ( ! MainWP_Security::remove_rsd_ok() ? 'N' : 'Y' ); - $information['wlw'] = ( ! MainWP_Security::remove_wlw_ok() ? 'N' : 'Y' ); - $information['db_reporting'] = ( ! MainWP_Security::remove_database_reporting_ok() ? 'N' : 'Y' ); - $information['php_reporting'] = ( ! MainWP_Security::remove_php_reporting_ok() ? 'N' : 'Y' ); - $information['versions'] = ( ! MainWP_Security::remove_scripts_version_ok() || ! MainWP_Security::remove_styles_version_ok() || ! MainWP_Security::remove_generator_version_ok() ? 'N' : 'Y' ); - $information['registered_versions'] = ( MainWP_Security::remove_registered_versions_ok() ? 'Y' : 'N' ); - $information['admin'] = ( MainWP_Security::admin_user_ok() ? 'Y' : 'N' ); - $information['readme'] = ( MainWP_Security::remove_readme_ok() ? 'Y' : 'N' ); - - mainwp_child_helper()->write( $information ); - } - - public function update_external_settings() { - $update_htaccess = false; - - if ( isset( $_POST['cloneSites'] ) ) { - if ( '0' !== $_POST['cloneSites'] ) { - $arr = json_decode( urldecode( $_POST['cloneSites'] ), 1 ); - MainWP_Helper::update_option( 'mainwp_child_clone_sites', ( ! is_array( $arr ) ? array() : $arr ) ); - } else { - MainWP_Helper::update_option( 'mainwp_child_clone_sites', '0' ); - } - } - - if ( isset( $_POST['siteId'] ) ) { - MainWP_Helper::update_option( 'mainwp_child_siteid', intval( $_POST['siteId'] ) ); - } - - if ( isset( $_POST['pluginDir'] ) ) { - if ( get_option( 'mainwp_child_pluginDir' ) !== $_POST['pluginDir'] ) { - MainWP_Helper::update_option( 'mainwp_child_pluginDir', $_POST['pluginDir'], 'yes' ); - $update_htaccess = true; - } - } elseif ( false !== get_option( 'mainwp_child_pluginDir' ) ) { - MainWP_Helper::update_option( 'mainwp_child_pluginDir', false, 'yes' ); - $update_htaccess = true; - } - - if ( $update_htaccess ) { - $this->update_htaccess( true ); - } - } - - // Show stats. - public function get_site_stats( $information = array(), $exit = true ) { - global $wp_version; - - if ( $exit ) { - $this->update_external_settings(); - } - - MainWP_Child_Branding::instance()->save_branding_options( 'branding_disconnected', '' ); - if ( isset( $_POST['server'] ) ) { - MainWP_Helper::update_option( 'mainwp_child_server', $_POST['server'] ); - } - - MainWP_Child_Plugins_Check::may_outdate_number_change(); - - $information['version'] = self::$version; - $information['wpversion'] = $wp_version; - $information['siteurl'] = get_option( 'siteurl' ); - $information['wpe'] = MainWP_Helper::is_wp_engine() ? 1 : 0; - $theme_name = wp_get_theme()->get( 'Name' ); - $information['site_info'] = array( - 'wpversion' => $wp_version, - 'debug_mode' => ( defined( 'WP_DEBUG' ) && true === WP_DEBUG ) ? true : false, - 'phpversion' => phpversion(), - 'child_version' => self::$version, - 'memory_limit' => MainWP_Child_Server_Information::get_php_memory_limit(), - 'mysql_version' => MainWP_Child_Server_Information::get_my_sql_version(), - 'themeactivated' => $theme_name, - 'ip' => $_SERVER['SERVER_ADDR'], - ); - - // Try to switch to SSL if SSL is enabled in between! - $pubkey = get_option( 'mainwp_child_pubkey' ); - $nossl = get_option( 'mainwp_child_nossl' ); - if ( 1 == $nossl ) { - if ( isset( $pubkey ) && MainWP_Helper::is_ssl_enabled() ) { - MainWP_Helper::update_option( 'mainwp_child_nossl', 0, 'yes' ); - $nossl = 0; - } - } - $information['nossl'] = ( 1 == $nossl ? 1 : 0 ); - - include_once ABSPATH . '/wp-admin/includes/update.php'; - - $timeout = 3 * 60 * 60; - set_time_limit( $timeout ); - ini_set( 'max_execution_time', $timeout ); //phpcs:ignore -- to custom - - // Check for new versions. - if ( null !== $this->filterFunction ) { - add_filter( 'pre_site_transient_update_core', $this->filterFunction, 99 ); - } - if ( null !== $this->filterFunction ) { - add_filter( 'pre_transient_update_core', $this->filterFunction, 99 ); - } - wp_version_check(); - $core_updates = get_core_updates(); - if ( is_array( $core_updates ) && count( $core_updates ) > 0 ) { - foreach ( $core_updates as $core_update ) { - if ( 'latest' === $core_update->response ) { - break; - } - if ( 'upgrade' === $core_update->response && version_compare( $wp_version, $core_update->current, '<=' ) ) { - $information['wp_updates'] = $core_update->current; - } - } - } - if ( ! isset( $information['wp_updates'] ) ) { - $information['wp_updates'] = null; - } - if ( null !== $this->filterFunction ) { - remove_filter( 'pre_site_transient_update_core', $this->filterFunction, 99 ); - } - if ( null !== $this->filterFunction ) { - remove_filter( 'pre_transient_update_core', $this->filterFunction, 99 ); - } - - add_filter( 'default_option_active_plugins', array( &$this, 'default_option_active_plugins' ) ); - add_filter( 'option_active_plugins', array( &$this, 'default_option_active_plugins' ) ); - - // First check for new premium updates. - $update_check = apply_filters( 'mwp_premium_update_check', array() ); - if ( ! empty( $update_check ) ) { - foreach ( $update_check as $updateFeedback ) { - if ( is_array( $updateFeedback['callback'] ) && isset( $updateFeedback['callback'][0] ) && isset( $updateFeedback['callback'][1] ) ) { - call_user_func( array( $updateFeedback['callback'][0], $updateFeedback['callback'][1] ) ); - } elseif ( is_string( $updateFeedback['callback'] ) ) { - call_user_func( $updateFeedback['callback'] ); - } - } - } - - $informationPremiumUpdates = apply_filters( 'mwp_premium_update_notification', array() ); - $premiumPlugins = array(); - $premiumThemes = array(); - if ( is_array( $informationPremiumUpdates ) ) { - $premiumUpdates = array(); - $information['premium_updates'] = array(); - $informationPremiumUpdatesLength = count( $informationPremiumUpdates ); - for ( $i = 0; $i < $informationPremiumUpdatesLength; $i ++ ) { - if ( ! isset( $informationPremiumUpdates[ $i ]['new_version'] ) ) { - continue; - } - $slug = ( isset( $informationPremiumUpdates[ $i ]['slug'] ) ? $informationPremiumUpdates[ $i ]['slug'] : $informationPremiumUpdates[ $i ]['Name'] ); - - if ( 'plugin' === $informationPremiumUpdates[ $i ]['type'] ) { - $premiumPlugins[] = $slug; - } elseif ( 'theme' === $informationPremiumUpdates[ $i ]['type'] ) { - $premiumThemes[] = $slug; - } - - $new_version = $informationPremiumUpdates[ $i ]['new_version']; - - unset( $informationPremiumUpdates[ $i ]['old_version'] ); - unset( $informationPremiumUpdates[ $i ]['new_version'] ); - - $information['premium_updates'][ $slug ] = $informationPremiumUpdates[ $i ]; - $information['premium_updates'][ $slug ]['update'] = (object) array( - 'new_version' => $new_version, - 'premium' => true, - 'slug' => $slug, - ); - if ( ! in_array( $slug, $premiumUpdates ) ) { - $premiumUpdates[] = $slug; - } - } - MainWP_Helper::update_option( 'mainwp_premium_updates', $premiumUpdates ); - } - - remove_filter( 'default_option_active_plugins', array( &$this, 'default_option_active_plugins' ) ); - remove_filter( 'option_active_plugins', array( &$this, 'default_option_active_plugins' ) ); - - if ( null !== $this->filterFunction ) { - add_filter( 'pre_site_transient_update_plugins', $this->filterFunction, 99 ); - } - - global $wp_current_filter; - $wp_current_filter[] = 'load-plugins.php'; // phpcs:ignore -- to custom plugin installation. - - wp_update_plugins(); - include_once ABSPATH . '/wp-admin/includes/plugin.php'; - - $plugin_updates = get_plugin_updates(); - if ( is_array( $plugin_updates ) ) { - $information['plugin_updates'] = array(); - - foreach ( $plugin_updates as $slug => $plugin_update ) { - if ( in_array( $plugin_update->Name, $premiumPlugins ) ) { - continue; - } - - // to fix incorrect info. - if ( ! property_exists( $plugin_update, 'update' ) || ! property_exists( $plugin_update->update, 'new_version' ) || empty( $plugin_update->update->new_version ) ) { - continue; - } - - $information['plugin_updates'][ $slug ] = $plugin_update; - } - } - - if ( null !== $this->filterFunction ) { - remove_filter( 'pre_site_transient_update_plugins', $this->filterFunction, 99 ); - } - - // to fix premium plugs update. - $cached_plugins_update = get_site_transient( 'mainwp_update_plugins_cached' ); - if ( is_array( $cached_plugins_update ) && ( count( $cached_plugins_update ) > 0 ) ) { - if ( ! isset( $information['plugin_updates'] ) ) { - $information['plugin_updates'] = array(); - } - foreach ( $cached_plugins_update as $slug => $plugin_update ) { - - // to fix incorrect info. - if ( ! property_exists( $plugin_update, 'new_version' ) || empty( $plugin_update->new_version ) ) { // may do not need to check this? - // to fix for some premiums update info. - if ( property_exists( $plugin_update, 'update' ) ) { - if ( ! property_exists( $plugin_update->update, 'new_version' ) || empty( $plugin_update->update->new_version ) ) { - continue; - } - } else { - continue; - } - } - - if ( ! isset( $information['plugin_updates'][ $slug ] ) ) { - $information['plugin_updates'][ $slug ] = $plugin_update; - } - } - } - - if ( null !== $this->filterFunction ) { - add_filter( 'pre_site_transient_update_themes', $this->filterFunction, 99 ); - } - wp_update_themes(); - include_once ABSPATH . '/wp-admin/includes/theme.php'; - $theme_updates = $this->upgrade_get_theme_updates(); - if ( is_array( $theme_updates ) ) { - $information['theme_updates'] = array(); - - foreach ( $theme_updates as $slug => $theme_update ) { - $name = ( is_array( $theme_update ) ? $theme_update['Name'] : $theme_update->Name ); - if ( in_array( $name, $premiumThemes ) ) { - continue; - } - - $information['theme_updates'][ $slug ] = $theme_update; - } - } - if ( null !== $this->filterFunction ) { - remove_filter( 'pre_site_transient_update_themes', $this->filterFunction, 99 ); - } - - // to fix premium themes update. - $cached_themes_update = get_site_transient( 'mainwp_update_themes_cached' ); - if ( is_array( $cached_themes_update ) && ( count( $cached_themes_update ) > 0 ) ) { - if ( ! isset( $information['theme_updates'] ) ) { - $information['theme_updates'] = array(); - } - - foreach ( $cached_themes_update as $slug => $theme_update ) { - $name = ( is_array( $theme_update ) ? $theme_update['Name'] : $theme_update->Name ); - if ( in_array( $name, $premiumThemes ) ) { - continue; - } - if ( isset( $information['theme_updates'][ $slug ] ) ) { - continue; - } - $information['theme_updates'][ $slug ] = $theme_update; - } - } - - $translation_updates = wp_get_translation_updates(); - if ( ! empty( $translation_updates ) ) { - $information['translation_updates'] = array(); - foreach ( $translation_updates as $translation_update ) { - $new_translation_update = array( - 'type' => $translation_update->type, - 'slug' => $translation_update->slug, - 'language' => $translation_update->language, - 'version' => $translation_update->version, - ); - if ( 'plugin' === $translation_update->type ) { - $all_plugins = get_plugins(); - foreach ( $all_plugins as $file => $plugin ) { - $path = dirname( $file ); - if ( $path == $translation_update->slug ) { - $new_translation_update['name'] = $plugin['Name']; - break; - } - } - } elseif ( 'theme' === $translation_update->type ) { - $theme = wp_get_theme( $translation_update->slug ); - $new_translation_update['name'] = $theme->name; - } elseif ( ( 'core' === $translation_update->type ) && ( 'default' === $translation_update->slug ) ) { - $new_translation_update['name'] = 'WordPress core'; - } - $information['translation_updates'][] = $new_translation_update; - } - } - - $information['recent_comments'] = $this->get_recent_comments( array( 'approve', 'hold' ), 5 ); - - $recent_number = 5; - - if ( isset( $_POST ) && isset( $_POST['recent_number'] ) ) { - $recent_number = $_POST['recent_number']; - if ( get_option( 'mainwp_child_recent_number', 5 ) != $recent_number ) { - update_option( 'mainwp_child_recent_number', $recent_number ); - } - } else { - $recent_number = get_option( 'mainwp_child_recent_number', 5 ); - } - - if ( $recent_number <= 0 || $recent_number > 30 ) { - $recent_number = 5; - } - - $information['recent_posts'] = $this->get_recent_posts( array( 'publish', 'draft', 'pending', 'trash', 'future' ), $recent_number ); - $information['recent_pages'] = $this->get_recent_posts( array( 'publish', 'draft', 'pending', 'trash', 'future' ), $recent_number, 'page' ); - $information['securityIssues'] = MainWP_Security::get_stats_security(); - - // Directory listings! - $information['directories'] = $this->scan_dir( ABSPATH, 3 ); - $cats = get_categories( - array( - 'hide_empty' => 0, - 'hierarchical' => true, - 'number' => 300, - ) - ); - $categories = array(); - foreach ( $cats as $cat ) { - $categories[] = $cat->name; - } - $information['categories'] = $categories; - - $get_file_size = apply_filters_deprecated( 'mainwp-child-get-total-size', array( true ), '4.0.7.1', 'mainwp_child_get_total_size' ); - $get_file_size = apply_filters( 'mainwp_child_get_total_size', $get_file_size ); - - if ( $get_file_size && isset( $_POST['cloneSites'] ) && ( '0' !== $_POST['cloneSites'] ) ) { - $max_exe = ini_get( 'max_execution_time' ); - if ( $max_exe > 20 ) { - $information['totalsize'] = $this->get_total_file_size(); - } - } - $information['dbsize'] = MainWP_Child_DB::get_size(); - - $auths = get_option( 'mainwp_child_auth' ); - $information['extauth'] = ( $auths && isset( $auths[ $this->maxHistory ] ) ? $auths[ $this->maxHistory ] : null ); - - $plugins = $this->get_all_plugins_int( false ); - $themes = $this->get_all_themes_int( false ); - $information['plugins'] = $plugins; - $information['themes'] = $themes; - - if ( isset( $_POST['optimize'] ) && ( '1' === $_POST['optimize'] ) ) { - $information['users'] = $this->get_all_users_int( 500 ); - } - - if ( isset( $_POST['primaryBackup'] ) && ! empty( $_POST['primaryBackup'] ) ) { - $primary_bk = $_POST['primaryBackup']; - $information['primaryLasttimeBackup'] = MainWP_Helper::get_lasttime_backup( $primary_bk ); - } - - $last_post = wp_get_recent_posts( array( 'numberposts' => absint( '1' ) ) ); - if ( isset( $last_post[0] ) ) { - $last_post = $last_post[0]; - } - if ( isset( $last_post ) && isset( $last_post['post_modified_gmt'] ) ) { - $information['last_post_gmt'] = strtotime( $last_post['post_modified_gmt'] ); - } - $information['mainwpdir'] = ( MainWP_Helper::validate_mainwp_dir() ? 1 : - 1 ); - $information['uniqueId'] = get_option( 'mainwp_child_uniqueId', '' ); - $information['plugins_outdate_info'] = MainWP_Child_Plugins_Check::instance()->get_plugins_outdate_info(); - $information['themes_outdate_info'] = MainWP_Child_Themes_Check::instance()->get_themes_outdate_info(); - - if ( isset( $_POST['user'] ) ) { - $user = get_user_by( 'login', $_POST['user'] ); - if ( $user && property_exists( $user, 'ID' ) && $user->ID ) { - $information['admin_nicename'] = $user->data->user_nicename; - $information['admin_useremail'] = $user->data->user_email; - } - } - - try { - do_action( 'mainwp_child_site_stats' ); - } catch ( \Exception $e ) { - // ok. - } - - if ( isset( $_POST['othersData'] ) ) { - $othersData = json_decode( stripslashes( $_POST['othersData'] ), true ); - if ( ! is_array( $othersData ) ) { - $othersData = array(); - } - - if ( isset( $othersData['wpvulndbToken'] ) ) { - $wpvulndb_token = get_option( 'mainwp_child_wpvulndb_token', '' ); - if ( $wpvulndb_token != $othersData['wpvulndbToken'] ) { - MainWP_Helper::update_option( 'mainwp_child_wpvulndb_token', $othersData['wpvulndbToken'] ); - } - } - - try { - $information = apply_filters_deprecated( 'mainwp-site-sync-others-data', array( $information, $othersData ), '4.0.7.1', 'mainwp_site_sync_others_data' ); - $information = apply_filters( 'mainwp_site_sync_others_data', $information, $othersData ); - - } catch ( \Exception $e ) { - // ok! - } - } - - if ( $exit ) { - mainwp_child_helper()->write( $information ); - } - - return $information; - } - - public function get_site_icon() { - $information = array(); - $url = $this->get_favicon( true ); - if ( ! empty( $url ) ) { - $information['faviIconUrl'] = $url; - } - mainwp_child_helper()->write( $information ); - } - - public function get_favicon( $parse_page = false ) { - - $favi_url = ''; - $favi = ''; - $site_url = get_option( 'siteurl' ); - if ( substr( $site_url, - 1 ) != '/' ) { - $site_url .= '/'; - } - - if ( function_exists( 'get_site_icon_url' ) && has_site_icon() ) { - $favi = get_site_icon_url(); - $favi_url = $favi; - } - - if ( empty( $favi ) ) { - if ( file_exists( ABSPATH . 'favicon.ico' ) ) { - $favi = 'favicon.ico'; - } elseif ( file_exists( ABSPATH . 'favicon.png' ) ) { - $favi = 'favicon.png'; - } - - if ( ! empty( $favi ) ) { - $favi_url = $site_url . $favi; - } - } - - if ( $parse_page ) { - // try to parse page. - if ( empty( $favi_url ) ) { - $request = wp_remote_get( $site_url, array( 'timeout' => 50 ) ); - $favi = ''; - if ( is_array( $request ) && isset( $request['body'] ) ) { - $preg_str1 = '/(]*)(?:rel="shortcut\s+icon"\s*)(?:[^>]*)?href="([^"]+)"(?:[^>]*)?>)/is'; - $preg_str2 = '/(]*)(?:rel="(?:shortcut\s+)?icon"\s*)(?:[^>]*)?href="([^"]+)"(?:[^>]*)?>)/is'; - - if ( preg_match( $preg_str1, $request['body'], $matches ) ) { - $favi = $matches[2]; - } elseif ( preg_match( $preg_str2, $request['body'], $matches ) ) { - $favi = $matches[2]; - } - } - - if ( ! empty( $favi ) ) { - if ( false === strpos( $favi, 'http' ) ) { - if ( 0 === strpos( $favi, '//' ) ) { - if ( 0 === strpos( $site_url, 'https' ) ) { - $favi_url = 'https:' . $favi; - } else { - $favi_url = 'http:' . $favi; - } - } else { - $favi_url = $site_url . $favi; - } - } else { - $favi_url = $favi; - } - } - } - - if ( ! empty( $favi_url ) ) { - return $favi_url; - } else { - return false; - } - } else { - return $favi_url; - } - } - - public function scan_dir( $pDir, $pLvl ) { - $output = array(); - if ( file_exists( $pDir ) && is_dir( $pDir ) ) { - if ( 'logs' === basename( $pDir ) ) { - return empty( $output ) ? null : $output; - } - if ( 0 === $pLvl ) { - return empty( $output ) ? null : $output; - } - $files = $this->int_scan_dir( $pDir ); - if ( $files ) { - foreach ( $files as $file ) { - if ( ( '.' === $file ) || ( '..' === $file ) ) { - continue; - } - $newDir = $pDir . $file . DIRECTORY_SEPARATOR; - if ( is_dir( $newDir ) ) { - $output[ $file ] = $this->scan_dir( $newDir, $pLvl - 1, false ); - } - } - - unset( $files ); - $files = null; - } - } - - return empty( $output ) ? null : $output; - } - - public function int_scan_dir( $dir ) { - $dh = opendir( $dir ); - if ( is_dir( $dir ) && $dh ) { - $cnt = 0; - $out = array(); - $file = readdir( $dh ); - while ( false !== $file ) { - $newDir = $dir . $file . DIRECTORY_SEPARATOR; - if ( ! is_dir( $newDir ) ) { - continue; - } - - $out[] = $file; - if ( $cnt ++ > 10 ) { - return $out; - } - } - closedir( $dh ); - - return $out; - } - - return false; - } - - public function upgrade_get_theme_updates() { - $themeUpdates = get_theme_updates(); - $newThemeUpdates = array(); - if ( is_array( $themeUpdates ) ) { - foreach ( $themeUpdates as $slug => $themeUpdate ) { - $newThemeUpdate = array(); - $newThemeUpdate['update'] = $themeUpdate->update; - $newThemeUpdate['Name'] = MainWP_Helper::search( $themeUpdate, 'Name' ); - $newThemeUpdate['Version'] = MainWP_Helper::search( $themeUpdate, 'Version' ); - $newThemeUpdates[ $slug ] = $newThemeUpdate; - } - } - - return $newThemeUpdates; - } - - public function get_recent_posts( $pAllowedStatuses, $pCount, $type = 'post', $extra = null ) { - $allPosts = array(); - if ( null !== $pAllowedStatuses ) { - foreach ( $pAllowedStatuses as $status ) { - $this->get_recent_posts_int( $status, $pCount, $type, $allPosts, $extra ); - } - } else { - $this->get_recent_posts_int( 'any', $pCount, $type, $allPosts, $extra ); - } - - return $allPosts; - } - - public function get_recent_posts_int( $status, $pCount, $type = 'post', &$allPosts, $extra = null ) { - $args = array( - 'post_status' => $status, - 'suppress_filters' => false, - 'post_type' => $type, - ); - - $tokens = array(); - if ( is_array( $extra ) && isset( $extra['tokens'] ) ) { - $tokens = $extra['tokens']; - if ( 1 == $extra['extract_post_type'] ) { - $args['post_type'] = 'post'; - } elseif ( 2 == $extra['extract_post_type'] ) { - $args['post_type'] = 'page'; - } elseif ( 3 == $extra['extract_post_type'] ) { - $args['post_type'] = array( 'post', 'page' ); - } - } - $tokens = array_flip( $tokens ); - - if ( 0 !== $pCount ) { - $args['numberposts'] = $pCount; - } - - /* - * - * Credits - * - * Plugin-Name: Yoast SEO - * Plugin URI: https://yoast.com/wordpress/plugins/seo/#utm_source=wpadmin&utm_medium=plugin&utm_campaign=wpseoplugin - * Author: Team Yoast - * Author URI: https://yoast.com/ - * Licence: GPL v3 - * - * The code is used for the MainWP WordPress SEO Extension - * Extension URL: https://mainwp.com/extension/wordpress-seo/ - * - */ - - $wp_seo_enabled = false; - if ( isset( $_POST['WPSEOEnabled'] ) && $_POST['WPSEOEnabled'] ) { - if ( is_plugin_active( 'wordpress-seo/wp-seo.php' ) && class_exists( 'WPSEO_Link_Column_Count' ) && class_exists( 'WPSEO_Meta' ) ) { - $wp_seo_enabled = true; - } - } - - $posts = get_posts( $args ); - if ( is_array( $posts ) ) { - if ( $wp_seo_enabled ) { - $post_ids = array(); - foreach ( $posts as $post ) { - $post_ids[] = $post->ID; - } - $link_count = new WPSEO_Link_Column_Count(); - $link_count->set( $post_ids ); - } - foreach ( $posts as $post ) { - $outPost = array(); - $outPost['id'] = $post->ID; - $outPost['post_type'] = $post->post_type; - $outPost['status'] = $post->post_status; - $outPost['title'] = $post->post_title; - $outPost['comment_count'] = $post->comment_count; - if ( isset( $extra['where_post_date'] ) && ! empty( $extra['where_post_date'] ) ) { - $outPost['dts'] = strtotime( $post->post_date_gmt ); - } else { - $outPost['dts'] = strtotime( $post->post_modified_gmt ); - } - - if ( 'future' == $post->post_status ) { - $outPost['dts'] = strtotime( $post->post_date_gmt ); - } - - $usr = get_user_by( 'id', $post->post_author ); - $outPost['author'] = ! empty( $usr ) ? $usr->user_nicename : 'removed'; - $categoryObjects = get_the_category( $post->ID ); - $categories = ''; - foreach ( $categoryObjects as $cat ) { - if ( '' !== $categories ) { - $categories .= ', '; - } - $categories .= $cat->name; - } - $outPost['categories'] = $categories; - - $tagObjects = get_the_tags( $post->ID ); - $tags = ''; - if ( is_array( $tagObjects ) ) { - foreach ( $tagObjects as $tag ) { - if ( '' !== $tags ) { - $tags .= ', '; - } - $tags .= $tag->name; - } - } - $outPost['tags'] = $tags; - - if ( is_array( $tokens ) ) { - if ( isset( $tokens['[post.url]'] ) ) { - $outPost['[post.url]'] = get_permalink( $post->ID ); - } - if ( isset( $tokens['[post.website.url]'] ) ) { - $outPost['[post.website.url]'] = get_site_url(); - } - if ( isset( $tokens['[post.website.name]'] ) ) { - $outPost['[post.website.name]'] = get_bloginfo( 'name' ); - } - } - - if ( $wp_seo_enabled ) { - $post_id = $post->ID; - $outPost['seo_data'] = array( - 'count_seo_links' => $link_count->get( $post_id, 'internal_link_count' ), - 'count_seo_linked' => $link_count->get( $post_id, 'incoming_link_count' ), - 'seo_score' => \MainWP_WordPress_SEO::instance()->parse_column_score( $post_id ), - 'readability_score' => \MainWP_WordPress_SEO::instance()->parse_column_score_readability( $post_id ), - ); - } - - $allPosts[] = $outPost; - } - } - } - - public function posts_where( $where ) { - if ( $this->posts_where_suffix ) { - $where .= ' ' . $this->posts_where_suffix; - } - - return $where; - } - - public function get_all_posts() { - $post_type = ( isset( $_POST['post_type'] ) ? $_POST['post_type'] : 'post' ); - $this->get_all_posts_by_type( $post_type ); - } - - public function insert_comment() { - $postId = $_POST['id']; - $comments = maybe_unserialize( base64_decode( $_POST['comments'] ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. - $ids = array(); - foreach ( $comments as $comment ) { - $ids[] = wp_insert_comment( - array( - 'comment_post_ID' => $postId, - 'comment_author' => $comment['author'], - 'comment_content' => $comment['content'], - 'comment_date' => $comment['date'], - ) - ); - } - mainwp_child_helper()->write( $ids ); - } - - public function cancel_scheduled_post() { - global $wpdb; - $postId = $_POST['post_id']; - $cancel_all = $_POST['cancel_all']; - $result = false; - $information = array(); - if ( $postId > 0 ) { - if ( 'yes' === get_post_meta( $postId, '_is_auto_generate_content', true ) ) { - $post = $wpdb->get_row( - $wpdb->prepare( - "SELECT * FROM $wpdb->posts WHERE ID = %d AND post_status = 'future'", - $postId - ) - ); - if ( $post ) { - $result = wp_trash_post( $postId ); - } else { - $result = true; - } - } - if ( ! $result ) { - $information['status'] = 'SUCCESS'; - } - } elseif ( $cancel_all ) { - $post_type = $_POST['post_type']; - $posts = $wpdb->get_results( $wpdb->prepare( "SELECT p.ID FROM $wpdb->posts p JOIN $wpdb->postmeta pm ON p.ID=pm.post_id WHERE p.post_status='future' AND p.post_type = %s AND pm.meta_key = '_is_auto_generate_content' AND pm.meta_value = 'yes' ", $post_type ) ); - $count = 0; - if ( is_array( $posts ) ) { - foreach ( $posts as $post ) { - if ( $post ) { - if ( false !== wp_trash_post( $post->ID ) ) { - $count ++; - - } - } - } - } else { - $posts = array(); - } - - $information['status'] = 'SUCCESS'; - $information['count'] = $count; - } - - mainwp_child_helper()->write( $information ); - } - - public function get_all_pages() { - $this->get_all_posts_by_type( 'page' ); - } - - public function get_all_pages_int() { - $rslt = $this->get_recent_posts( null, - 1, 'page' ); - - return $rslt; - } - - public function get_all_posts_by_type( $type ) { - global $wpdb; - - add_filter( 'posts_where', array( &$this, 'posts_where' ) ); - $where_post_date = isset( $_POST['where_post_date'] ) && ! empty( $_POST['where_post_date'] ) ? true : false; - if ( isset( $_POST['postId'] ) ) { - $this->posts_where_suffix .= " AND $wpdb->posts.ID = " . $_POST['postId']; - } elseif ( isset( $_POST['userId'] ) ) { - $this->posts_where_suffix .= " AND $wpdb->posts.post_author = " . $_POST['userId']; - } else { - if ( isset( $_POST['keyword'] ) ) { - $search_on = isset( $_POST['search_on'] ) ? $_POST['search_on'] : ''; - if ( 'title' == $search_on ) { - $this->posts_where_suffix .= " AND ( $wpdb->posts.post_title LIKE '%" . $_POST['keyword'] . "%' )"; - } elseif ( 'content' == $search_on ) { - $this->posts_where_suffix .= " AND ($wpdb->posts.post_content LIKE '%" . $_POST['keyword'] . "%' )"; - } else { - $this->posts_where_suffix .= " AND ($wpdb->posts.post_content LIKE '%" . $_POST['keyword'] . "%' OR $wpdb->posts.post_title LIKE '%" . $_POST['keyword'] . "%' )"; - } - } - if ( isset( $_POST['dtsstart'] ) && '' !== $_POST['dtsstart'] ) { - if ( $where_post_date ) { - $this->posts_where_suffix .= " AND $wpdb->posts.post_date > '" . $_POST['dtsstart'] . "'"; - } else { - $this->posts_where_suffix .= " AND $wpdb->posts.post_modified > '" . $_POST['dtsstart'] . "'"; - } - } - if ( isset( $_POST['dtsstop'] ) && '' !== $_POST['dtsstop'] ) { - if ( $where_post_date ) { - $this->posts_where_suffix .= " AND $wpdb->posts.post_date < '" . $_POST['dtsstop'] . "'"; - } else { - $this->posts_where_suffix .= " AND $wpdb->posts.post_modified < '" . $_POST['dtsstop'] . "'"; - } - } - - if ( isset( $_POST['exclude_page_type'] ) && $_POST['exclude_page_type'] ) { - $this->posts_where_suffix .= " AND $wpdb->posts.post_type NOT IN ('page')"; - } - } - - $maxPages = 50; - if ( defined( 'MAINWP_CHILD_NR_OF_PAGES' ) ) { - $maxPages = MAINWP_CHILD_NR_OF_PAGES; - } - - if ( isset( $_POST['maxRecords'] ) ) { - $maxPages = $_POST['maxRecords']; - } - if ( 0 === $maxPages ) { - $maxPages = 99999; - } - - $extra = array(); - if ( isset( $_POST['extract_tokens'] ) ) { - $extra['tokens'] = maybe_unserialize( base64_decode( $_POST['extract_tokens'] ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. - $extra['extract_post_type'] = $_POST['extract_post_type']; - } - - $extra['where_post_date'] = $where_post_date; - $rslt = $this->get_recent_posts( explode( ',', $_POST['status'] ), $maxPages, $type, $extra ); - $this->posts_where_suffix = ''; - - mainwp_child_helper()->write( $rslt ); - } - - public function comments_clauses( $clauses ) { - if ( $this->comments_and_clauses ) { - $clauses['where'] .= ' ' . $this->comments_and_clauses; - } - - return $clauses; - } - - public function get_all_comments() { - global $wpdb; - - add_filter( 'comments_clauses', array( &$this, 'comments_clauses' ) ); - - if ( isset( $_POST['postId'] ) ) { - $this->comments_and_clauses .= " AND $wpdb->comments.comment_post_ID = " . $_POST['postId']; - } else { - if ( isset( $_POST['keyword'] ) ) { - $this->comments_and_clauses .= " AND $wpdb->comments.comment_content LIKE '%" . $_POST['keyword'] . "%'"; - } - if ( isset( $_POST['dtsstart'] ) && '' !== $_POST['dtsstart'] ) { - $this->comments_and_clauses .= " AND $wpdb->comments.comment_date > '" . $_POST['dtsstart'] . "'"; - } - if ( isset( $_POST['dtsstop'] ) && '' !== $_POST['dtsstop'] ) { - $this->comments_and_clauses .= " AND $wpdb->comments.comment_date < '" . $_POST['dtsstop'] . "'"; - } - } - - $maxComments = 50; - if ( defined( 'MAINWP_CHILD_NR_OF_COMMENTS' ) ) { - $maxComments = MAINWP_CHILD_NR_OF_COMMENTS; // to compatible. - } - - if ( isset( $_POST['maxRecords'] ) ) { - $maxComments = $_POST['maxRecords']; - } - - if ( 0 === $maxComments ) { - $maxComments = 99999; - } - - $rslt = $this->get_recent_comments( explode( ',', $_POST['status'] ), $maxComments ); - $this->comments_and_clauses = ''; - - mainwp_child_helper()->write( $rslt ); - } - - public function get_recent_comments( $pAllowedStatuses, $pCount ) { - if ( ! function_exists( 'get_comment_author_url' ) ) { - include_once WPINC . '/comment-template.php'; - } - $allComments = array(); - - foreach ( $pAllowedStatuses as $status ) { - $params = array( 'status' => $status ); - if ( 0 !== $pCount ) { - $params['number'] = $pCount; - } - $comments = get_comments( $params ); - if ( is_array( $comments ) ) { - foreach ( $comments as $comment ) { - $post = get_post( $comment->comment_post_ID ); - $email = apply_filters( 'comment_email', $comment->comment_author_email ); - $outComment = array(); - $outComment['id'] = $comment->comment_ID; - $outComment['status'] = wp_get_comment_status( $comment->comment_ID ); - $outComment['author'] = $comment->comment_author; - $outComment['author_url'] = get_comment_author_url( $comment->comment_ID ); - $outComment['author_ip'] = get_comment_author_IP( $comment->comment_ID ); - $outComment['author_email'] = apply_filters( 'comment_email', $comment->comment_author_email ); - $outComment['postId'] = $comment->comment_post_ID; - $outComment['postName'] = $post->post_title; - $outComment['comment_count'] = $post->comment_count; - $outComment['content'] = $comment->comment_content; - $outComment['dts'] = strtotime( $comment->comment_date_gmt ); - $allComments[] = $outComment; - } - } - } - - return $allComments; - } - - public function theme_action() { - $action = $_POST['action']; - $theme = $_POST['theme']; - - if ( 'activate' === $action ) { - include_once ABSPATH . '/wp-admin/includes/theme.php'; - $theTheme = wp_get_theme( $theme ); - if ( null !== $theTheme && '' !== $theTheme ) { - switch_theme( $theTheme['Template'], $theTheme['Stylesheet'] ); - } - } elseif ( 'delete' === $action ) { - include_once ABSPATH . '/wp-admin/includes/theme.php'; - if ( file_exists( ABSPATH . '/wp-admin/includes/screen.php' ) ) { - include_once ABSPATH . '/wp-admin/includes/screen.php'; - } - include_once ABSPATH . '/wp-admin/includes/file.php'; - include_once ABSPATH . '/wp-admin/includes/template.php'; - include_once ABSPATH . '/wp-admin/includes/misc.php'; - include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader.php'; - include_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-base.php'; - include_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-direct.php'; - - global $wp_filesystem; - - MainWP_Helper::check_wp_filesystem(); - - if ( empty( $wp_filesystem ) ) { - $wp_filesystem = new WP_Filesystem_Direct( null ); - } - $themeUpgrader = new Theme_Upgrader(); - - $theme_name = wp_get_theme()->get( 'Name' ); - $themes = explode( '||', $theme ); - - if ( count( $themes ) == 1 ) { - $themeToDelete = current( $themes ); - if ( $themeToDelete == $theme_name ) { - $information['error'] = 'IsActivatedTheme'; - mainwp_child_helper()->write( $information ); - return; - } - } - - foreach ( $themes as $idx => $themeToDelete ) { - if ( $themeToDelete !== $theme_name ) { - $theTheme = wp_get_theme( $themeToDelete ); - if ( null !== $theTheme && '' !== $theTheme ) { - $tmp['theme'] = $theTheme['Template']; - if ( true === $themeUpgrader->delete_old_theme( null, null, null, $tmp ) ) { - $args = array( - 'action' => 'delete', - 'Name' => $theTheme['Name'], - ); - do_action( 'mainwp_child_theme_action', $args ); - } - } - } - } - } else { - $information['status'] = 'FAIL'; - } - - if ( ! isset( $information['status'] ) ) { - $information['status'] = 'SUCCESS'; - } - $information['sync'] = $this->get_site_stats( array(), false ); - mainwp_child_helper()->write( $information ); - } - - public function get_all_themes() { - $keyword = $_POST['keyword']; - $status = $_POST['status']; - $filter = isset( $_POST['filter'] ) ? $_POST['filter'] : true; - $rslt = $this->get_all_themes_int( $filter, $keyword, $status ); - - mainwp_child_helper()->write( $rslt ); - } - - public function get_all_themes_int( $filter, $keyword = '', $status = '' ) { - $rslt = array(); - $themes = wp_get_themes(); - - if ( is_array( $themes ) ) { - $theme_name = wp_get_theme()->get( 'Name' ); - - /** @var $theme WP_Theme */ - foreach ( $themes as $theme ) { - $out = array(); - $out['name'] = $theme->get( 'Name' ); - $out['title'] = $theme->display( 'Name', true, false ); - $out['description'] = $theme->display( 'Description', true, false ); - $out['version'] = $theme->display( 'Version', true, false ); - $out['active'] = ( $theme->get( 'Name' ) === $theme_name ) ? 1 : 0; - $out['slug'] = $theme->get_stylesheet(); - if ( ! $filter ) { - if ( '' == $keyword || stristr( $out['title'], $keyword ) ) { - $rslt[] = $out; - } - } elseif ( ( ( 'active' === $status ) ? 1 : 0 ) === $out['active'] ) { - if ( '' == $keyword || stristr( $out['title'], $keyword ) ) { - $rslt[] = $out; - } - } - } - } - - return $rslt; - } - - public function plugin_action() { - $action = $_POST['action']; - $plugins = explode( '||', $_POST['plugin'] ); - - if ( 'activate' === $action ) { - include_once ABSPATH . '/wp-admin/includes/plugin.php'; - - foreach ( $plugins as $idx => $plugin ) { - if ( $plugin !== $this->plugin_slug ) { - $thePlugin = get_plugin_data( $plugin ); - if ( null !== $thePlugin && '' !== $thePlugin ) { - if ( 'quotes-collection/quotes-collection.php' == $plugin ) { - activate_plugin( $plugin, '', false, true ); - } else { - activate_plugin( $plugin ); - } - } - } - } - } elseif ( 'deactivate' === $action ) { - include_once ABSPATH . '/wp-admin/includes/plugin.php'; - - foreach ( $plugins as $idx => $plugin ) { - if ( $plugin !== $this->plugin_slug ) { - $thePlugin = get_plugin_data( $plugin ); - if ( null !== $thePlugin && '' !== $thePlugin ) { - deactivate_plugins( $plugin ); - } - } - } - } elseif ( 'delete' === $action ) { - include_once ABSPATH . '/wp-admin/includes/plugin.php'; - if ( file_exists( ABSPATH . '/wp-admin/includes/screen.php' ) ) { - include_once ABSPATH . '/wp-admin/includes/screen.php'; - } - include_once ABSPATH . '/wp-admin/includes/file.php'; - include_once ABSPATH . '/wp-admin/includes/template.php'; - include_once ABSPATH . '/wp-admin/includes/misc.php'; - include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader.php'; - include_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-base.php'; - include_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-direct.php'; - - global $wp_filesystem; - MainWP_Helper::check_wp_filesystem(); - - if ( null === $wp_filesystem ) { - $wp_filesystem = new WP_Filesystem_Direct( null ); - } - $pluginUpgrader = new Plugin_Upgrader(); - - $all_plugins = get_plugins(); - foreach ( $plugins as $idx => $plugin ) { - if ( $plugin !== $this->plugin_slug ) { - if ( isset( $all_plugins[ $plugin ] ) ) { - if ( is_plugin_active( $plugin ) ) { - $thePlugin = get_plugin_data( $plugin ); - if ( null !== $thePlugin && '' !== $thePlugin ) { - deactivate_plugins( $plugin ); - } - } - $tmp['plugin'] = $plugin; - if ( true === $pluginUpgrader->delete_old_plugin( null, null, null, $tmp ) ) { - $args = array( - 'action' => 'delete', - 'Name' => $all_plugins[ $plugin ]['Name'], - ); - do_action( 'mainwp_child_plugin_action', $args ); - } - } - } - } - } else { - $information['status'] = 'FAIL'; - } - - if ( ! isset( $information['status'] ) ) { - $information['status'] = 'SUCCESS'; - } - $information['sync'] = $this->get_site_stats( array(), false ); - mainwp_child_helper()->write( $information ); - } - - public function get_all_plugins() { - $keyword = $_POST['keyword']; - $status = $_POST['status']; - $filter = isset( $_POST['filter'] ) ? $_POST['filter'] : true; - $rslt = $this->get_all_plugins_int( $filter, $keyword, $status ); - - mainwp_child_helper()->write( $rslt ); - } - - public function get_all_plugins_int( $filter, $keyword = '', $status = '' ) { - if ( ! function_exists( 'get_plugins' ) ) { - include_once ABSPATH . 'wp-admin/includes/plugin.php'; - } - $rslt = array(); - $plugins = get_plugins(); - if ( is_array( $plugins ) ) { - $active_plugins = get_option( 'active_plugins' ); - - foreach ( $plugins as $pluginslug => $plugin ) { - $out = array(); - $out['mainwp'] = ( $pluginslug == $this->plugin_slug ? 'T' : 'F' ); - $out['name'] = $plugin['Name']; - $out['slug'] = $pluginslug; - $out['description'] = $plugin['Description']; - $out['version'] = $plugin['Version']; - $out['active'] = is_plugin_active( $pluginslug ) ? 1 : 0; - if ( ! $filter ) { - if ( '' == $keyword || stristr( $out['name'], $keyword ) ) { - $rslt[] = $out; - } - } elseif ( ( ( 'active' == $status ) ? 1 : 0 ) == $out['active'] ) { - if ( '' == $keyword || stristr( $out['name'], $keyword ) ) { - $rslt[] = $out; - } - } - } - } - - $muplugins = get_mu_plugins(); - if ( is_array( $muplugins ) ) { - foreach ( $muplugins as $pluginslug => $plugin ) { - $out = array(); - $out['mainwp'] = ( $pluginslug == $this->plugin_slug ? 'T' : 'F' ); - $out['name'] = $plugin['Name']; - $out['slug'] = $pluginslug; - $out['description'] = $plugin['Description']; - $out['version'] = $plugin['Version']; - $out['active'] = 1; - $out['mu'] = 1; - if ( ! $filter ) { - if ( '' == $keyword || stristr( $out['name'], $keyword ) ) { - $rslt[] = $out; - } - } elseif ( ( ( 'active' == $status ) ? 1 : 0 ) == $out['active'] ) { - if ( '' == $keyword || stristr( $out['name'], $keyword ) ) { - $rslt[] = $out; - } - } - } - } - - return $rslt; - } - - public function get_all_users( $return = false ) { - $roles = explode( ',', $_POST['role'] ); - $allusers = array(); - if ( is_array( $roles ) ) { - foreach ( $roles as $role ) { - $new_users = get_users( 'role=' . $role ); - foreach ( $new_users as $new_user ) { - $usr = array(); - $usr['id'] = $new_user->ID; - $usr['login'] = $new_user->user_login; - $usr['nicename'] = $new_user->user_nicename; - $usr['email'] = $new_user->user_email; - $usr['registered'] = $new_user->user_registered; - $usr['status'] = $new_user->user_status; - $usr['display_name'] = $new_user->display_name; - $usr['role'] = $role; - $usr['post_count'] = count_user_posts( $new_user->ID ); - $usr['avatar'] = get_avatar( $new_user->ID, 32 ); - $allusers[] = $usr; - } - } - } - if ( $return ) { - return $allusers; - } - mainwp_child_helper()->write( $allusers ); - } - - public function get_all_users_int( $number = false ) { - $allusers = array(); - - $params = array(); - if ( $number ) { - $params['number'] = $number; - } - - $new_users = get_users( $params ); - if ( is_array( $new_users ) ) { - foreach ( $new_users as $new_user ) { - $usr = array(); - $usr['id'] = $new_user->ID; - $usr['login'] = $new_user->user_login; - $usr['nicename'] = $new_user->user_nicename; - $usr['email'] = $new_user->user_email; - $usr['registered'] = $new_user->user_registered; - $usr['status'] = $new_user->user_status; - $usr['display_name'] = $new_user->display_name; - $userdata = get_userdata( $new_user->ID ); - $user_roles = $userdata->roles; - $user_role = array_shift( $user_roles ); - $usr['role'] = $user_role; - $usr['post_count'] = count_user_posts( $new_user->ID ); - $allusers[] = $usr; - } - } - - return $allusers; - } - - public function search_users() { - - $search_user_role = array(); - $check_users_role = false; - - if ( isset( $_POST['role'] ) && ! empty( $_POST['role'] ) ) { - $check_users_role = true; - $all_users_role = $this->get_all_users( true ); - foreach ( $all_users_role as $user ) { - $search_user_role[] = $user['id']; - } - unset( $all_users_role ); - } - - $columns = explode( ',', $_POST['search_columns'] ); - $allusers = array(); - $exclude = array(); - - foreach ( $columns as $col ) { - if ( empty( $col ) ) { - continue; - } - - $user_query = new WP_User_Query( - array( - 'search' => $_POST['search'], - 'fields' => 'all_with_meta', - 'search_columns' => array( $col ), - 'query_orderby' => array( $col ), - 'exclude' => $exclude, - ) - ); - if ( ! empty( $user_query->results ) ) { - foreach ( $user_query->results as $new_user ) { - if ( $check_users_role ) { - if ( ! in_array( $new_user->ID, $search_user_role ) ) { - continue; - } - } - $exclude[] = $new_user->ID; - $usr = array(); - $usr['id'] = $new_user->ID; - $usr['login'] = $new_user->user_login; - $usr['nicename'] = $new_user->user_nicename; - $usr['email'] = $new_user->user_email; - $usr['registered'] = $new_user->user_registered; - $usr['status'] = $new_user->user_status; - $usr['display_name'] = $new_user->display_name; - $userdata = get_userdata( $new_user->ID ); - $user_roles = $userdata->roles; - $user_role = array_shift( $user_roles ); - $usr['role'] = $user_role; - $usr['post_count'] = count_user_posts( $new_user->ID ); - $usr['avatar'] = get_avatar( $new_user->ID, 32 ); - $allusers[] = $usr; - } - } - } - - mainwp_child_helper()->write( $allusers ); - } - - // Show stats without login - only allowed while no account is added yet. - public function get_site_stats_no_auth( $information = array() ) { - if ( get_option( 'mainwp_child_pubkey' ) ) { - $hint = '
' . __( 'Hint: Go to the child site, deactivate and reactivate the MainWP Child plugin and try again.', 'mainwp-child' ); - MainWP_Helper::error( __( 'This site already contains a link. Please deactivate and reactivate the MainWP plugin.', 'mainwp-child' ) . $hint ); - } - - global $wp_version; - $information['version'] = self::$version; - $information['wpversion'] = $wp_version; - $information['wpe'] = MainWP_Helper::is_wp_engine() ? 1 : 0; - mainwp_child_helper()->write( $information ); - } - - // Deactivating the plugin. - public function deactivate() { - include_once ABSPATH . 'wp-admin/includes/plugin.php'; - deactivate_plugins( $this->plugin_slug, true ); - $information = array(); - if ( is_plugin_active( $this->plugin_slug ) ) { - MainWP_Helper::error( 'Plugin still active' ); - } - $information['deactivated'] = true; - mainwp_child_helper()->write( $information ); - } - - public function activation() { - $mu_plugin_enabled = apply_filters( 'mainwp_child_mu_plugin_enabled', false ); - if ( $mu_plugin_enabled ) { - return; - } - - $to_delete = array( - 'mainwp_child_pubkey', - 'mainwp_child_nonce', - 'mainwp_child_nossl', - 'mainwp_child_nossl_key', - ); - foreach ( $to_delete as $delete ) { - if ( get_option( $delete ) ) { - delete_option( $delete ); - } - } - - MainWP_Helper::update_option( 'mainwp_child_activated_once', true ); - - // delete bad data if existed. - $to_delete = array( 'mainwp_ext_snippets_enabled', 'mainwp_ext_code_snippets' ); - foreach ( $to_delete as $delete ) { - delete_option( $delete ); - } - } - - public function deactivation( $deact = true ) { - - $mu_plugin_enabled = apply_filters( 'mainwp_child_mu_plugin_enabled', false ); - if ( $mu_plugin_enabled ) { - return; - } - - $to_delete = array( - 'mainwp_child_pubkey', - 'mainwp_child_nonce', - 'mainwp_child_nossl', - 'mainwp_child_nossl_key', - 'mainwp_security', - 'mainwp_child_server', - ); - $to_delete[] = 'mainwp_ext_snippets_enabled'; - $to_delete[] = 'mainwp_ext_code_snippets'; - - foreach ( $to_delete as $delete ) { - if ( get_option( $delete ) ) { - delete_option( $delete ); - wp_cache_delete( $delete, 'options' ); - } - } - - if ( $deact ) { - do_action( 'mainwp_child_deactivation' ); - } - } - - public function get_total_file_size( $directory = WP_CONTENT_DIR ) { - try { - if ( MainWP_Helper::function_exists( 'popen' ) ) { - $uploadDir = MainWP_Helper::get_mainwp_dir(); - $uploadDir = $uploadDir[0]; - $popenHandle = popen( 'du -s ' . $directory . ' --exclude "' . str_replace( ABSPATH, '', $uploadDir ) . '"', 'r' ); // phpcs:ignore -- run if enabled. - if ( 'resource' === gettype( $popenHandle ) ) { - $size = fread( $popenHandle, 1024 ); - pclose( $popenHandle ); - $size = substr( $size, 0, strpos( $size, "\t" ) ); - if ( $size && MainWP_Helper::ctype_digit( $size ) ) { - return $size / 1024; - } - } - } - - if ( MainWP_Helper::function_exists( 'shell_exec' ) ) { - $uploadDir = MainWP_Helper::get_mainwp_dir(); - $uploadDir = $uploadDir[0]; - $size = shell_exec( 'du -s ' . $directory . ' --exclude "' . str_replace( ABSPATH, '', $uploadDir ) . '"' ); // phpcs:ignore -- run if enabled. - if ( null !== $size ) { - $size = substr( $size, 0, strpos( $size, "\t" ) ); - if ( $size && MainWP_Helper::ctype_digit( $size ) ) { - return $size / 1024; - } - } - } - if ( class_exists( 'COM' ) ) { - $obj = new COM( 'scripting.filesystemobject' ); - - if ( is_object( $obj ) ) { - $ref = $obj->getfolder( $directory ); - - $size = $ref->size; - - $obj = null; - if ( MainWP_Helper::ctype_digit( $size ) ) { - return $size / 1024; - } - } - } - // to fix for window host, performance not good? - if ( class_exists( 'RecursiveIteratorIterator' ) ) { - $size = 0; - foreach ( new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $directory ) ) as $file ) { - $size += $file->getSize(); - } - if ( $size && MainWP_Helper::ctype_digit( $size ) ) { - return $size / 1024 / 1024; - } - } - return 0; - } catch ( \Exception $e ) { - return 0; - } - } - - public function server_information() { - ob_start(); - MainWP_Child_Server_Information::render(); - $output['information'] = ob_get_contents(); - ob_end_clean(); - ob_start(); - MainWP_Child_Server_Information::render_cron(); - $output['cron'] = ob_get_contents(); - ob_end_clean(); - ob_start(); - MainWP_Child_Server_Information::render_error_log_page(); - $output['error'] = ob_get_contents(); - ob_end_clean(); - ob_start(); - MainWP_Child_Server_Information::render_wp_config(); - $output['wpconfig'] = ob_get_contents(); - ob_end_clean(); - ob_start(); - MainWP_Child_Server_Information::renderhtaccess(); - $output['htaccess'] = ob_get_contents(); - ob_end_clean(); - - mainwp_child_helper()->write( $output ); - } - - public function maintenance_site() { - global $wpdb; - $information = array(); - if ( isset( $_POST['action'] ) ) { - if ( 'save_settings' === $_POST['action'] ) { - - if ( isset( $_POST['enable_alert'] ) && '1' === $_POST['enable_alert'] ) { - MainWP_Helper::update_option( 'mainwp_maintenance_opt_alert_404', 1, 'yes' ); - } else { - delete_option( 'mainwp_maintenance_opt_alert_404' ); - } - - if ( isset( $_POST['email'] ) && ! empty( $_POST['email'] ) ) { - MainWP_Helper::update_option( 'mainwp_maintenance_opt_alert_404_email', $_POST['email'], 'yes' ); - } else { - delete_option( 'mainwp_maintenance_opt_alert_404_email' ); - } - $information['result'] = 'SUCCESS'; - mainwp_child_helper()->write( $information ); - - return; - } elseif ( 'clear_settings' === $_POST['action'] ) { - delete_option( 'mainwp_maintenance_opt_alert_404' ); - delete_option( 'mainwp_maintenance_opt_alert_404_email' ); - $information['result'] = 'SUCCESS'; - mainwp_child_helper()->write( $information ); - } - mainwp_child_helper()->write( $information ); - } - - $maint_options = $_POST['options']; - $max_revisions = isset( $_POST['revisions'] ) ? intval( $_POST['revisions'] ) : 0; - - if ( ! is_array( $maint_options ) ) { - $information['status'] = 'FAIL'; - $maint_options = array(); - } - - $performed_what = array(); - - if ( in_array( 'revisions', $maint_options ) ) { - if ( empty( $max_revisions ) ) { - $sql_clean = "DELETE FROM $wpdb->posts WHERE post_type = 'revision'"; - $wpdb->query( $sql_clean ); // phpcs:ignore -- safe sql. - // to fix issue of meta_value short length. - $performed_what[] = 'revisions'; // 'Posts revisions deleted'. - } else { - $results = MainWP_Helper::get_revisions( $max_revisions ); - $count_deleted = MainWP_Helper::delete_revisions( $results, $max_revisions ); - $performed_what[] = 'revisions_max'; // 'Posts revisions deleted'. - } - } - - if ( in_array( 'autodraft', $maint_options ) ) { - $sql_clean = "DELETE FROM $wpdb->posts WHERE post_status = 'auto-draft'"; - $wpdb->query( $sql_clean ); // phpcs:ignore -- safe sql. - $performed_what[] = 'autodraft'; // 'Auto draft posts deleted'. - } - - if ( in_array( 'trashpost', $maint_options ) ) { - $sql_clean = "DELETE FROM $wpdb->posts WHERE post_status = 'trash'"; - $wpdb->query( $sql_clean ); // phpcs:ignore -- safe sql. - $performed_what[] = 'trashpost'; // 'Trash posts deleted'. - } - - if ( in_array( 'spam', $maint_options ) ) { - $sql_clean = "DELETE FROM $wpdb->comments WHERE comment_approved = 'spam'"; - $wpdb->query( $sql_clean ); // phpcs:ignore -- safe sql. - $performed_what[] = 'spam'; // 'Spam comments deleted'. - } - - if ( in_array( 'pending', $maint_options ) ) { - $sql_clean = "DELETE FROM $wpdb->comments WHERE comment_approved = '0'"; - $wpdb->query( $sql_clean ); // phpcs:ignore -- safe sql. - $performed_what[] = 'pending'; // 'Pending comments deleted'. - } - - if ( in_array( 'trashcomment', $maint_options ) ) { - $sql_clean = "DELETE FROM $wpdb->comments WHERE comment_approved = 'trash'"; - $wpdb->query( $sql_clean ); // phpcs:ignore -- safe sql. - $performed_what[] = 'trashcomment'; // 'Trash comments deleted'. - } - - if ( in_array( 'tags', $maint_options ) ) { - $post_tags = get_terms( 'post_tag', array( 'hide_empty' => false ) ); - if ( is_array( $post_tags ) ) { - foreach ( $post_tags as $tag ) { - if ( 0 === $tag->count ) { - wp_delete_term( $tag->term_id, 'post_tag' ); - } - } - } - $performed_what[] = 'tags'; // 'Tags with 0 posts associated deleted'. - } - - if ( in_array( 'categories', $maint_options ) ) { - $post_cats = get_terms( 'category', array( 'hide_empty' => false ) ); - if ( is_array( $post_cats ) ) { - foreach ( $post_cats as $cat ) { - if ( 0 === $cat->count ) { - wp_delete_term( $cat->term_id, 'category' ); - } - } - } - $performed_what[] = 'categories'; // 'Categories with 0 posts associated deleted'. - } - - if ( in_array( 'optimize', $maint_options ) ) { - $this->maintenance_optimize(); - $performed_what[] = 'optimize'; // 'Database optimized'. - } - if ( ! isset( $information['status'] ) ) { - $information['status'] = 'SUCCESS'; - } - - if ( ! empty( $performed_what ) && has_action( 'mainwp_reports_maintenance' ) ) { - $details = implode( ',', $performed_what ); - $log_time = time(); - $message = 'Maintenance Performed'; - $result = 'Maintenance Performed'; - do_action( 'mainwp_reports_maintenance', $message, $log_time, $details, $result, $max_revisions ); - } - - mainwp_child_helper()->write( $information ); - } - - public function maintenance_optimize() { - global $wpdb, $table_prefix; - $sql = 'SHOW TABLE STATUS FROM `' . DB_NAME . '`'; - $result = MainWP_Child_DB::to_query( $sql, $wpdb->dbh ); - if ( MainWP_Child_DB::num_rows( $result ) && MainWP_Child_DB::is_result( $result ) ) { - while ( $row = MainWP_Child_DB::fetch_array( $result ) ) { - if ( strpos( $row['Name'], $table_prefix ) !== false ) { - $sql = 'OPTIMIZE TABLE ' . $row['Name']; - MainWP_Child_DB::to_query( $sql, $wpdb->dbh ); - } - } - } + + MainWP_Child_Stats::get_instance()->get_site_stats( $information ); } public function maintenance_alert_404() { @@ -4350,237 +1570,6 @@ class MainWP_Child { ); } - public function keyword_links_action() { - MainWP_Keyword_Links::instance()->action(); - } - - public function branding_child_plugin() { - MainWP_Child_Branding::instance()->action(); - } - - public function code_snippet() { - $action = $_POST['action']; - $information = array( 'status' => 'FAIL' ); - if ( 'run_snippet' === $action || 'save_snippet' === $action ) { - if ( ! isset( $_POST['code'] ) ) { - mainwp_child_helper()->write( $information ); - } - } - $code = stripslashes( $_POST['code'] ); - if ( 'run_snippet' === $action ) { - $information = MainWP_Helper::execute_snippet( $code ); - } elseif ( 'save_snippet' === $action ) { - $type = $_POST['type']; - $slug = $_POST['slug']; - $snippets = get_option( 'mainwp_ext_code_snippets' ); - - if ( ! is_array( $snippets ) ) { - $snippets = array(); - } - - if ( 'C' === $type ) { // save into wp-config file. - if ( false !== $this->snippet_update_wp_config( 'save', $slug, $code ) ) { - $information['status'] = 'SUCCESS'; - } - } else { - $snippets[ $slug ] = $code; - if ( MainWP_Helper::update_option( 'mainwp_ext_code_snippets', $snippets ) ) { - $information['status'] = 'SUCCESS'; - } - } - MainWP_Helper::update_option( 'mainwp_ext_snippets_enabled', true, 'yes' ); - } elseif ( 'delete_snippet' === $action ) { - $type = $_POST['type']; - $slug = $_POST['slug']; - $snippets = get_option( 'mainwp_ext_code_snippets' ); - - if ( ! is_array( $snippets ) ) { - $snippets = array(); - } - if ( 'C' === $type ) { // delete in wp-config file. - if ( false !== $this->snippet_update_wp_config( 'delete', $slug ) ) { - $information['status'] = 'SUCCESS'; - } - } else { - if ( isset( $snippets[ $slug ] ) ) { - unset( $snippets[ $slug ] ); - if ( MainWP_Helper::update_option( 'mainwp_ext_code_snippets', $snippets ) ) { - $information['status'] = 'SUCCESS'; - } - } else { - $information['status'] = 'SUCCESS'; - } - } - } - mainwp_child_helper()->write( $information ); - } - - public function snippet_update_wp_config( $action, $slug, $code = '' ) { - - $config_file = ''; - if ( file_exists( ABSPATH . 'wp-config.php' ) ) { - // The config file resides in ABSPATH. - $config_file = ABSPATH . 'wp-config.php'; - } elseif ( file_exists( dirname( ABSPATH ) . '/wp-config.php' ) && ! file_exists( dirname( ABSPATH ) . '/wp-settings.php' ) ) { - // The config file resides one level above ABSPATH but is not part of another install. - $config_file = dirname( ABSPATH ) . '/wp-config.php'; - } - - if ( ! empty( $config_file ) ) { - $wpConfig = file_get_contents( $config_file ); - - if ( 'delete' === $action ) { - $wpConfig = preg_replace( '/' . PHP_EOL . '{1,2}\/\*\*\*snippet_' . $slug . '\*\*\*\/(.*)\/\*\*\*end_' . $slug . '\*\*\*\/' . PHP_EOL . '/is', '', $wpConfig ); - } elseif ( 'save' === $action ) { - $wpConfig = preg_replace( '/(\$table_prefix *= *[\'"][^\'|^"]*[\'"] *;)/is', '${1}' . PHP_EOL . PHP_EOL . '/***snippet_' . $slug . '***/' . PHP_EOL . $code . PHP_EOL . '/***end_' . $slug . '***/' . PHP_EOL, $wpConfig ); - } - file_put_contents( $config_file, $wpConfig ); - - return true; - } - return false; - } - - public function run_saved_snippets() { - $action = null; - if ( isset( $_POST['action'] ) ) { - $action = $_POST['action']; - } - - if ( 'run_snippet' === $action || 'save_snippet' === $action || 'delete_snippet' === $action ) { - return; - } // do not run saved snippets if in do action snippet - - if ( get_option( 'mainwp_ext_snippets_enabled' ) ) { - $snippets = get_option( 'mainwp_ext_code_snippets' ); - if ( is_array( $snippets ) && count( $snippets ) > 0 ) { - foreach ( $snippets as $code ) { - MainWP_Helper::execute_snippet( $code ); - } - } - } - } - - - public function uploader_action() { - $file_url = base64_decode( $_POST['url'] ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. - $path = $_POST['path']; - $filename = $_POST['filename']; - $information = array(); - - if ( empty( $file_url ) || empty( $path ) ) { - mainwp_child_helper()->write( $information ); - - return; - } - - if ( strpos( $path, 'wp-content' ) === 0 ) { - $path = basename( WP_CONTENT_DIR ) . substr( $path, 10 ); - } elseif ( strpos( $path, 'wp-includes' ) === 0 ) { - $path = WPINC . substr( $path, 11 ); - } - - if ( '/' === $path ) { - $dir = ABSPATH; - } else { - $path = str_replace( ' ', '-', $path ); - $path = str_replace( '.', '-', $path ); - $dir = ABSPATH . $path; - } - - if ( ! file_exists( $dir ) ) { - if ( false === mkdir( $dir, 0777, true ) ) { - $information['error'] = 'ERRORCREATEDIR'; - mainwp_child_helper()->write( $information ); - - return; - } - } - - try { - $upload = MainWP_Helper::upload_file( $file_url, $dir, $filename ); - if ( null !== $upload ) { - $information['success'] = true; - } - } catch ( \Exception $e ) { - $information['error'] = $e->getMessage(); - } - mainwp_child_helper()->write( $information ); - } - - public function wordpress_seo() { - \MainWP_WordPress_SEO::instance()->action(); - } - - public function client_report() { - MainWP_Client_Report::instance()->action(); - } - - public function page_speed() { - \MainWP_Child_Pagespeed::instance()->action(); - } - - public function woo_com_status() { - \MainWP_Child_WooCommerce_Status::instance()->action(); - } - - public function links_checker() { - \MainWP_Child_Links_Checker::instance()->action(); - } - - public function wordfence() { - \MainWP_Child_Wordfence::instance()->action(); - } - - public function ithemes() { - \MainWP_Child_IThemes_Security::instance()->action(); - } - - - public function updraftplus() { - \MainWP_Child_Updraft_Plus_Backups::instance()->action(); - } - - public function wpvivid_backuprestore() { - \MainWP_Child_WPvivid_BackupRestore::instance()->action(); - } - - public function backup_wp() { - if ( ! version_compare( phpversion(), '5.3', '>=' ) ) { - $error = sprintf( __( 'PHP Version %s is unsupported.', 'mainwp-child' ), phpversion() ); - mainwp_child_helper()->write( array( 'error' => $error ) ); - } - \MainWP_Child_Back_Up_WordPress::instance()->action(); - } - - public function wp_rocket() { - \MainWP_Child_WP_Rocket::instance()->action(); - } - - public function backwpup() { - \MainWP_Child_Back_WP_Up::instance()->action(); - } - - - public function delete_backup() { - $dirs = MainWP_Helper::get_mainwp_dir( 'backup' ); - $backupdir = $dirs[0]; - - $file = $_REQUEST['del']; - - if ( file_exists( $backupdir . $file ) ) { - unlink( $backupdir . $file ); - } - - mainwp_child_helper()->write( array( 'result' => 'ok' ) ); - } - - public function update_values() { - $uniId = isset( $_POST['uniqueId'] ) ? $_POST['uniqueId'] : ''; - MainWP_Helper::update_option( 'mainwp_child_uniqueId', $uniId ); - mainwp_child_helper()->write( array( 'result' => 'ok' ) ); - } - public function upload_file( $file, $offset = 0 ) { $dirs = MainWP_Helper::get_mainwp_dir( 'backup' ); $backupdir = $dirs[0]; @@ -4623,70 +1612,91 @@ class MainWP_Child { return fclose( $handle ); } - - public function settings_tools() { + + public function run_saved_snippets() { + $action = null; if ( isset( $_POST['action'] ) ) { - switch ( $_POST['action'] ) { - case 'force_destroy_sessions': - if ( 0 === get_current_user_id() ) { - mainwp_child_helper()->write( array( 'error' => __( 'Cannot get user_id', 'mainwp-child' ) ) ); - } + $action = $_POST['action']; + } - wp_destroy_all_sessions(); + if ( 'run_snippet' === $action || 'save_snippet' === $action || 'delete_snippet' === $action ) { + return; + } // do not run saved snippets if in do action snippet - $sessions = wp_get_all_sessions(); - - if ( empty( $sessions ) ) { - mainwp_child_helper()->write( array( 'success' => 1 ) ); - } else { - mainwp_child_helper()->write( array( 'error' => __( 'Cannot destroy sessions', 'mainwp-child' ) ) ); - } - break; - - default: - mainwp_child_helper()->write( array( 'error' => __( 'Invalid action', 'mainwp-child' ) ) ); + if ( get_option( 'mainwp_ext_snippets_enabled' ) ) { + $snippets = get_option( 'mainwp_ext_code_snippets' ); + if ( is_array( $snippets ) && count( $snippets ) > 0 ) { + foreach ( $snippets as $code ) { + MainWP_Helper::execute_snippet( $code ); + } } - } else { - mainwp_child_helper()->write( array( 'error' => __( 'Missing action', 'mainwp-child' ) ) ); } } - public function skeleton_key() { - MainWP_Child_Skeleton_Key::instance()->action(); - } + + /* + * hook to deactivation child plugin action + */ + public function deactivation( $deact = true ) { - public function custom_post_type() { - MainWP_Custom_Post_Type::instance()->action(); - } + $mu_plugin_enabled = apply_filters( 'mainwp_child_mu_plugin_enabled', false ); + if ( $mu_plugin_enabled ) { + return; + } - public function backup_buddy() { - \MainWP_Child_Back_Up_Buddy::instance()->action(); - } + $to_delete = array( + 'mainwp_child_pubkey', + 'mainwp_child_nonce', + 'mainwp_child_nossl', + 'mainwp_child_nossl_key', + 'mainwp_security', + 'mainwp_child_server', + ); + $to_delete[] = 'mainwp_ext_snippets_enabled'; + $to_delete[] = 'mainwp_ext_code_snippets'; - public function vulner_checker() { - MainWP_Child_Vulnerability_Checker::instance()->action(); - } + foreach ( $to_delete as $delete ) { + if ( get_option( $delete ) ) { + delete_option( $delete ); + wp_cache_delete( $delete, 'options' ); + } + } - public function time_capsule() { - \MainWP_Child_Timecapsule::instance()->action(); + if ( $deact ) { + do_action( 'mainwp_child_deactivation' ); + } } + + /* + * hook to activation child plugin action + */ + public function activation() { + $mu_plugin_enabled = apply_filters( 'mainwp_child_mu_plugin_enabled', false ); + if ( $mu_plugin_enabled ) { + return; + } - public function wp_staging() { - MainWP_Child_Staging::instance()->action(); + $to_delete = array( + 'mainwp_child_pubkey', + 'mainwp_child_nonce', + 'mainwp_child_nossl', + 'mainwp_child_nossl_key', + ); + foreach ( $to_delete as $delete ) { + if ( get_option( $delete ) ) { + delete_option( $delete ); + } + } + + MainWP_Helper::update_option( 'mainwp_child_activated_once', true ); + + // delete bad data if existed. + $to_delete = array( 'mainwp_ext_snippets_enabled', 'mainwp_ext_code_snippets' ); + foreach ( $to_delete as $delete ) { + delete_option( $delete ); + } } - - public function extra_execution() { - $post = $_POST; - $information = array(); - $information = apply_filters( 'mainwp_child_extra_execution', $information, $post ); - mainwp_child_helper()->write( $information ); - } - - public function disconnect() { - $this->deactivation( false ); - mainwp_child_helper()->write( array( 'result' => 'success' ) ); - } - + public static function fix_for_custom_themes() { if ( file_exists( ABSPATH . '/wp-admin/includes/screen.php' ) ) { include_once ABSPATH . '/wp-admin/includes/screen.php'; diff --git a/class/class-mainwp-debug.php b/class/class-mainwp-debug.php index 895fd45..c23fda8 100644 --- a/class/class-mainwp-debug.php +++ b/class/class-mainwp-debug.php @@ -4,9 +4,9 @@ namespace MainWP\Child; class MainWP_Debug { /** - * @param $mainwpChild MainWP_Child + * @param $mainWPChild MainWP_Child */ - public static function process( &$mainwpChild ) { + public static function process( &$mainWPChild ) { if ( ! isset( $_GET['mainwpdebug'] ) || ! defined( 'MAINWP_DEBUG' ) || ( MAINWP_DEBUG !== true ) ) { return; } @@ -21,11 +21,11 @@ class MainWP_Debug { $_POST['excludezip'] = '1'; $_POST['excludenonwp'] = '1'; $_POST['ext'] = 'tar.gz'; - print_r( $mainwpChild->backup( false ) ); // phpcs:ignore -- debug feature. + print_r( $mainWPChild->backup( false ) ); // phpcs:ignore -- debug feature. } elseif ( 'test' == $_GET['mainwpdebug'] ) { print_r( get_included_files() ); // phpcs:ignore -- debug feature. } else { - print_r( $mainwpChild->get_site_stats( array(), false ) ); // phpcs:ignore -- debug feature. + print_r( MainWP_Child_Stats::get_instance()->get_site_stats( array(), false ) ); // phpcs:ignore -- debug feature. } $stop = microtime( true ); diff --git a/class/class-mainwp-helper.php b/class/class-mainwp-helper.php index 29edf70..4c6aa7e 100644 --- a/class/class-mainwp-helper.php +++ b/class/class-mainwp-helper.php @@ -125,7 +125,7 @@ class MainWP_Helper { if ( null !== $code ) { $information['error_code'] = $code; } - self::write( $information ); + self::instance()->write( $information ); } /** @@ -519,7 +519,7 @@ class MainWP_Helper { $new_post['post_content'] = str_replace( $lnkToReplace, $linkToReplaceWith, $new_post['post_content'] ); } } catch ( \Exception $e ) { - error_log( $e->getMessage() ); // phpcs:ignore -- debug mode only. + MainWP_Helper::log_debug( $e->getMessage() ); } } } @@ -1004,19 +1004,21 @@ class MainWP_Helper { public static function check_wp_filesystem() { - + + $FTP_ERROR = 'Failed! Please, add FTP details for automatic updates.'; + self::get_wp_filesystem(); + global $wp_filesystem; if ( empty( $wp_filesystem ) ) { - self::error( $this->FTP_ERROR ); + self::error( $FTP_ERROR ); } elseif ( is_wp_error( $wp_filesystem->errors ) ) { $errorCodes = $wp_filesystem->errors->get_error_codes(); if ( ! empty( $errorCodes ) ) { self::error( __( 'WordPress Filesystem error: ', 'mainwp-child' ) . $wp_filesystem->errors->get_error_message() ); } } - return $wp_filesystem; } @@ -1751,7 +1753,7 @@ class MainWP_Helper { // handle fatal errors and compile errors. $error = error_get_last(); if ( isset( $error['type'] ) && isset( $error['message'] ) && ( E_ERROR === $error['type'] || E_COMPILE_ERROR === $error['type'] ) ) { - self::write( array( 'error' => 'MainWP_Child fatal error : ' . $error['message'] . ' Line: ' . $error['line'] . ' File: ' . $error['file'] ) ); + self::instance()->write( array( 'error' => 'MainWP_Child fatal error : ' . $error['message'] . ' Line: ' . $error['line'] . ' File: ' . $error['file'] ) ); } } @@ -1786,4 +1788,9 @@ class MainWP_Helper { return $return; } + public static function log_debug( $msg ) { + if ( defined( 'MAINWP_DEBUG' ) && MAINWP_DEBUG ) { + error_log( $msg ); // phpcs:ignore -- debug mode only. + } + } } diff --git a/mainwp-child.php b/mainwp-child.php index 212d3d9..693d53b 100644 --- a/mainwp-child.php +++ b/mainwp-child.php @@ -10,7 +10,7 @@ */ require_once ABSPATH . 'wp-includes' . DIRECTORY_SEPARATOR . 'version.php'; // Version information from WordPress. -define( 'MAINWP_DEBUG', false ); +define( 'MAINWP_DEBUG', true ); if ( ! defined( 'MAINWP_CHILD_FILE' ) ) { define( 'MAINWP_CHILD_FILE', __FILE__ );