From 64931ea48a7b8e9d4b2db881b0f1baa1336eed66 Mon Sep 17 00:00:00 2001 From: thanghv Date: Fri, 22 May 2020 00:03:42 +0700 Subject: [PATCH] Refactoring --- class/class-mainwp-backup.php | 4 +- class/class-mainwp-child-callable.php | 8 ++-- class/class-mainwp-child-install.php | 4 +- class/class-mainwp-child-posts.php | 39 ++++++++++--------- .../class-mainwp-child-server-information.php | 6 +-- class/class-mainwp-child-stats.php | 8 ++-- class/class-mainwp-child-updates.php | 6 +-- class/class-mainwp-clone.php | 4 +- class/class-mainwp-helper.php | 27 ------------- phpcs.xml | 14 +++++-- 10 files changed, 51 insertions(+), 69 deletions(-) diff --git a/class/class-mainwp-backup.php b/class/class-mainwp-backup.php index 75e268c..461450c 100644 --- a/class/class-mainwp-backup.php +++ b/class/class-mainwp-backup.php @@ -153,7 +153,7 @@ class MainWP_Backup { exit(); } } - + public function backup_checkpid() { $pid = $_POST['pid']; @@ -202,7 +202,7 @@ class MainWP_Backup { MainWP_Helper::write( $information ); } - + public function backup( $pWrite = true ) { $timeout = 20 * 60 * 60; diff --git a/class/class-mainwp-child-callable.php b/class/class-mainwp-child-callable.php index 22ce817..bff8e05 100644 --- a/class/class-mainwp-child-callable.php +++ b/class/class-mainwp-child-callable.php @@ -334,7 +334,7 @@ class MainWP_Child_Callable { global $wpdb; return $wpdb->get_results( $wpdb->prepare( " SELECT `post_parent`, COUNT(*) cnt FROM $wpdb->posts WHERE `post_type` = 'revision' GROUP BY `post_parent` HAVING COUNT(*) > %d ", $max_revisions ) ); } - + private function maintenance_delete_revisions( $results, $max_revisions ) { global $wpdb; @@ -433,7 +433,7 @@ class MainWP_Child_Callable { } public function backup_poll() { - MainWP_Backup::get()->backup_poll(); + MainWP_Backup::get()->backup_poll(); } public function backup_checkpid() { @@ -441,7 +441,7 @@ class MainWP_Child_Callable { } public function backup( $pWrite = true ) { - return MainWP_Backup::get()->backup( $pWrite ); + return MainWP_Backup::get()->backup( $pWrite ); } protected function backup_full( $fileName ) { @@ -449,7 +449,7 @@ class MainWP_Child_Callable { } protected function backup_db( $fileName = '', $ext = 'zip' ) { - return MainWP_Backup::get()->backup_db( $fileName, $ext ); + return MainWP_Backup::get()->backup_db( $fileName, $ext ); } public function get_site_icon() { diff --git a/class/class-mainwp-child-install.php b/class/class-mainwp-child-install.php index 9692f5b..cef42d3 100644 --- a/class/class-mainwp-child-install.php +++ b/class/class-mainwp-child-install.php @@ -276,7 +276,7 @@ class MainWP_Child_Install { $information['destination_name'] = $result['destination_name']; MainWP_Helper::write( $information ); } - + /** * Hook to set ssl verify. */ @@ -284,7 +284,7 @@ class MainWP_Child_Install { $r['sslverify'] = false; return $r; } - + /** * Include necessary files. */ diff --git a/class/class-mainwp-child-posts.php b/class/class-mainwp-child-posts.php index 23f7011..9c30779 100644 --- a/class/class-mainwp-child-posts.php +++ b/class/class-mainwp-child-posts.php @@ -76,7 +76,7 @@ class MainWP_Child_Posts { $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' ) ) { + if ( is_plugin_active( 'wordpress-seo/wp-seo.php' ) && class_exists( '\WPSEO_Link_Column_Count' ) && class_exists( '\WPSEO_Meta' ) ) { $wp_seo_enabled = true; } } @@ -685,7 +685,7 @@ class MainWP_Child_Posts { } } $check_image_existed = $edit_post_id ? true : false; // if editing post then will check if image existed. - $this->create_found_images( $new_post, $upload_dir, $check_image_existed ); + $this->update_found_images( $new_post, $upload_dir, $check_image_existed ); $this->create_has_shortcode_gallery( $new_post ); if ( $is_post_plus ) { $this->create_post_plus( $new_post, $post_custom ); @@ -736,7 +736,7 @@ class MainWP_Child_Posts { global $current_user; - $this->create_wp_rocket( $post_custom ); + $this->update_wp_rocket_custom_post( $post_custom ); // current user may be connected admin or alternative admin. $current_uid = $current_user->ID; @@ -777,13 +777,13 @@ class MainWP_Child_Posts { private function update_post_data( $new_post_id, $post_custom, $post_category, $post_featured_image, $check_image_existed, $is_post_plus ) { $seo_ext_activated = false; - if ( class_exists( 'WPSEO_Meta' ) && class_exists( 'WPSEO_Admin' ) ) { + if ( class_exists( '\WPSEO_Meta' ) && class_exists( '\WPSEO_Admin' ) ) { $seo_ext_activated = true; } $post_to_only_existing_categories = false; - $this->create_set_custom_fields( $new_post_id, $post_custom, $seo_ext_activated, $post_to_only_existing_categories ); + $this->set_custom_post_fields( $new_post_id, $post_custom, $seo_ext_activated, $post_to_only_existing_categories ); // yoast seo plugin activated. if ( $seo_ext_activated ) { @@ -791,12 +791,12 @@ class MainWP_Child_Posts { } $this->create_set_categories( $new_post_id, $post_category, $post_to_only_existing_categories ); - $this->create_featured_image( $new_post_id, $post_featured_image, $check_image_existed ); // post plus extension process. if ( $is_post_plus ) { - $this->create_post_plus_categories( $new_post_id, $post_custom ); + $this->post_plus_update_author( $new_post_id, $post_custom ); + $this->post_plus_update_categories( $new_post_id, $post_custom ); } // to support custom post author. @@ -811,7 +811,7 @@ class MainWP_Child_Posts { } } - private function create_wp_rocket( &$post_custom ) { + private function update_wp_rocket_custom_post( &$post_custom ) { // Options fields. $wprocket_fields = array( 'lazyload', @@ -846,7 +846,7 @@ class MainWP_Child_Posts { } } - private function create_found_images( &$new_post, $upload_dir, $check_image_existed ) { + private function update_found_images( &$new_post, $upload_dir, $check_image_existed ) { // Search for all the images added to the new post. Some images have a href tag to click to navigate to the image.. we need to replace this too. $foundMatches = preg_match_all( '/(]+href=\"(.*?)\"[^>]*>)?(\/]*src=\"((.*?)(png|gif|jpg|jpeg))\")/ix', $new_post['post_content'], $matches, PREG_SET_ORDER ); @@ -970,12 +970,11 @@ class MainWP_Child_Posts { } } - private function create_post_plus_categories( $new_post_id, $post_custom ) { - - $random_privelege = isset( $post_custom['_saved_draft_random_privelege'] ) ? $post_custom['_saved_draft_random_privelege'] : null; - $random_privelege = is_array( $random_privelege ) ? current( $random_privelege ) : null; - $random_privelege_base = base64_decode( $random_privelege ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. - $random_privelege = maybe_unserialize( $random_privelege_base ); + private function post_plus_update_author( $new_post_id, $post_custom ) { + $random_privelege = isset( $post_custom['_saved_draft_random_privelege'] ) ? $post_custom['_saved_draft_random_privelege'] : null; + $random_privelege = is_array( $random_privelege ) ? current( $random_privelege ) : null; + $random_privelege_base = base64_decode( $random_privelege ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. + $random_privelege = maybe_unserialize( $random_privelege_base ); if ( is_array( $random_privelege ) && count( $random_privelege ) > 0 ) { $random_post_authors = array(); @@ -996,9 +995,11 @@ class MainWP_Child_Posts { ); } } - - $random_category = isset( $post_custom['_saved_draft_random_category'] ) ? $post_custom['_saved_draft_random_category'] : false; - $random_category = is_array( $random_category ) ? current( $random_category ) : null; + } + + private function post_plus_update_categories( $new_post_id, $post_custom ) { + $random_category = isset( $post_custom['_saved_draft_random_category'] ) ? $post_custom['_saved_draft_random_category'] : false; + $random_category = is_array( $random_category ) ? current( $random_category ) : null; if ( ! empty( $random_category ) ) { $cats = get_categories( array( @@ -1045,7 +1046,7 @@ class MainWP_Child_Posts { } } - private function create_set_custom_fields( $new_post_id, $post_custom, $seo_ext_activated, &$post_to_only ) { + private function set_custom_post_fields( $new_post_id, $post_custom, $seo_ext_activated, &$post_to_only ) { // Set custom fields. $not_allowed = array( diff --git a/class/class-mainwp-child-server-information.php b/class/class-mainwp-child-server-information.php index 8b72d4e..2b8ba93 100644 --- a/class/class-mainwp-child-server-information.php +++ b/class/class-mainwp-child-server-information.php @@ -15,7 +15,7 @@ class MainWP_Child_Server_Information extends MainWP_Child_Server_Information_Ba public static function get_class_name() { return __CLASS__; } - + public static function init() { add_action( 'wp_ajax_mainwp-child_dismiss_warnings', @@ -890,7 +890,7 @@ class MainWP_Child_Server_Information extends MainWP_Child_Server_Information_Ba Pass' : ' Warning' ); ?> - diff --git a/class/class-mainwp-child-stats.php b/class/class-mainwp-child-stats.php index afd8bd2..4a6b9cd 100644 --- a/class/class-mainwp-child-stats.php +++ b/class/class-mainwp-child-stats.php @@ -563,8 +563,8 @@ class MainWP_Child_Stats { } } } - if ( class_exists( 'COM' ) ) { - $obj = new COM( 'scripting.filesystemobject' ); + if ( class_exists( '\COM' ) ) { + $obj = new \COM( 'scripting.filesystemobject' ); if ( is_object( $obj ) ) { $ref = $obj->getfolder( $directory ); @@ -578,9 +578,9 @@ class MainWP_Child_Stats { } } // to fix for window host, performance not good? - if ( class_exists( 'RecursiveIteratorIterator' ) ) { + if ( class_exists( '\RecursiveIteratorIterator' ) ) { $size = 0; - foreach ( new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $directory ) ) as $file ) { + foreach ( new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator( $directory ) ) as $file ) { $size += $file->getSize(); } if ( $size && MainWP_Helper::ctype_digit( $size ) ) { diff --git a/class/class-mainwp-child-updates.php b/class/class-mainwp-child-updates.php index 643a239..c67c9a1 100644 --- a/class/class-mainwp-child-updates.php +++ b/class/class-mainwp-child-updates.php @@ -371,11 +371,11 @@ class MainWP_Child_Updates { if ( in_array( 'backupbuddy/backupbuddy.php', $plugins ) ) { if ( isset( $GLOBALS['ithemes_updater_path'] ) ) { - if ( ! class_exists( 'Ithemes_Updater_Settings' ) ) { + if ( ! class_exists( '\Ithemes_Updater_Settings' ) ) { require $GLOBALS['ithemes_updater_path'] . '/settings.php'; } - if ( class_exists( 'Ithemes_Updater_Settings' ) ) { - $ithemes_updater = new Ithemes_Updater_Settings(); + if ( class_exists( '\Ithemes_Updater_Settings' ) ) { + $ithemes_updater = new \Ithemes_Updater_Settings(); $ithemes_updater->update(); } } diff --git a/class/class-mainwp-clone.php b/class/class-mainwp-clone.php index 54e326b..5d07561 100644 --- a/class/class-mainwp-clone.php +++ b/class/class-mainwp-clone.php @@ -121,7 +121,7 @@ class MainWP_Clone { } $branding_opts = MainWP_Child_Branding::instance()->get_branding_options(); - $hide_restore = isset( $branding_opts['remove_restore'] ) && $branding_opts['remove_restore'] ? true : false; + $hide_restore = isset( $branding_opts['remove_restore'] ) && $branding_opts['remove_restore'] ? true : false; if ( ! $hide_restore ) { if ( '' == session_id() ) { session_start(); @@ -1464,7 +1464,7 @@ class MainWP_Clone { } return $file; } - + public static function is_archive( $pFileName, $pPrefix = '', $pSuffix = '' ) { return preg_match( '/' . $pPrefix . '(.*).(zip|tar|tar.gz|tar.bz2)' . $pSuffix . '$/', $pFileName ); } diff --git a/class/class-mainwp-helper.php b/class/class-mainwp-helper.php index 6d8a34b..8d9373a 100644 --- a/class/class-mainwp-helper.php +++ b/class/class-mainwp-helper.php @@ -121,7 +121,6 @@ class MainWP_Helper { } } } - return null; } @@ -151,7 +150,6 @@ class MainWP_Helper { } else { $init = true; } - return $init; } @@ -192,7 +190,6 @@ class MainWP_Helper { if ( 0 == $length ) { return true; } - return ( substr( $haystack, - $length ) == $needle ); } @@ -220,7 +217,6 @@ class MainWP_Helper { } else { $url = $url . '/'; } - return $url; } @@ -235,7 +231,6 @@ class MainWP_Helper { while ( $length -- ) { $str .= $charset[ mt_rand( 0, $count - 1 ) ]; // phpcs:ignore } - return $str; } @@ -255,7 +250,6 @@ class MainWP_Helper { $val *= 1024; break; } - return $val; } @@ -270,7 +264,6 @@ class MainWP_Helper { if ( ! is_readable( $dir ) ) { return null; } - return ( 2 === count( scandir( $dir ) ) ); } @@ -304,13 +297,11 @@ class MainWP_Helper { return ( function_exists( $func ) && ! array_search( $func, $suhosin ) ); } } - return true; } public static function get_timestamp( $timestamp ) { $gmtOffset = get_option( 'gmt_offset' ); - return ( $gmtOffset ? ( $gmtOffset * HOUR_IN_SECONDS ) + $timestamp : $timestamp ); } @@ -328,11 +319,9 @@ class MainWP_Helper { public static function update_option( $option_name, $option_value, $autoload = 'no' ) { $success = add_option( $option_name, $option_value, '', $autoload ); - if ( ! $success ) { $success = update_option( $option_name, $option_value ); } - return $success; } @@ -340,7 +329,6 @@ class MainWP_Helper { if ( empty( $value ) ) { return false; } - if ( null != $excludes ) { foreach ( $excludes as $exclude ) { if ( self::ends_with( $exclude, '*' ) ) { @@ -354,7 +342,6 @@ class MainWP_Helper { } } } - return false; } @@ -362,14 +349,12 @@ class MainWP_Helper { if ( ! function_exists( 'mb_ereg_replace' ) ) { return sanitize_file_name( $filename ); } - // Remove anything which isn't a word, whitespace, number or any of the following caracters -_~,;:[](). // If you don't need to handle multi-byte characters you can use preg_replace rather than mb_ereg_replace. // Thanks @�?ukasz Rysiak! $filename = mb_ereg_replace( '([^\w\s\d\-_~,;:\[\]\(\).])', '', $filename ); // Remove any runs of periods (thanks falstro!). $filename = mb_ereg_replace( '([\.]{2,})', '', $filename ); - return $filename; } @@ -382,11 +367,9 @@ class MainWP_Helper { if ( 0 == $current_user->ID ) { return false; } - if ( 10 == $current_user->wp_user_level || ( isset( $current_user->user_level ) && 10 == $current_user->user_level ) || current_user_can( 'level_10' ) ) { return true; } - return false; } @@ -398,11 +381,9 @@ class MainWP_Helper { } public static function is_updates_screen() { - if ( ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) { return false; } - if ( function_exists( 'get_current_screen' ) ) { $screen = get_current_screen(); if ( $screen ) { @@ -431,7 +412,6 @@ class MainWP_Helper { $missing[] = $files; } } - if ( ! empty( $missing ) ) { $message = 'Missing file(s): ' . implode( ',', $missing ); if ( $return ) { @@ -456,7 +436,6 @@ class MainWP_Helper { $missing[] = $classes; } } - if ( ! empty( $missing ) ) { $message = 'Missing classes: ' . implode( ',', $missing ); if ( $return ) { @@ -482,7 +461,6 @@ class MainWP_Helper { $missing[] = $methods; } } - if ( ! empty( $missing ) ) { $message = 'Missing method: ' . implode( ',', $missing ); if ( $return ) { @@ -491,7 +469,6 @@ class MainWP_Helper { throw new \Exception( $message ); } } - return true; } @@ -508,7 +485,6 @@ class MainWP_Helper { $missing[] = $properties; } } - if ( ! empty( $missing ) ) { $message = 'Missing properties: ' . implode( ',', $missing ); if ( $return ) { @@ -517,7 +493,6 @@ class MainWP_Helper { throw new \Exception( $message ); } } - return true; } @@ -534,7 +509,6 @@ class MainWP_Helper { $missing[] = $funcs; } } - if ( ! empty( $missing ) ) { $message = 'Missing functions: ' . implode( ',', $missing ); if ( $return ) { @@ -543,7 +517,6 @@ class MainWP_Helper { throw new \Exception( $message ); } } - return true; } diff --git a/phpcs.xml b/phpcs.xml index 3675ce8..9b20596 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -28,11 +28,19 @@ - - + + - + + + + + + + + +