Refactoring

This commit is contained in:
thanghv 2020-05-22 00:03:42 +07:00
parent f5acc21b32
commit 64931ea48a
10 changed files with 51 additions and 69 deletions

View file

@ -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( '/(<a[^>]+href=\"(.*?)\"[^>]*>)?(<img[^>\/]*src=\"((.*?)(png|gif|jpg|jpeg))\")/ix', $new_post['post_content'], $matches, PREG_SET_ORDER );
@ -970,8 +970,7 @@ class MainWP_Child_Posts {
}
}
private function create_post_plus_categories( $new_post_id, $post_custom ) {
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.
@ -996,7 +995,9 @@ class MainWP_Child_Posts {
);
}
}
}
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 ) ) {
@ -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(

View file

@ -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 ) ) {

View file

@ -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();
}
}

View file

@ -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 @<40>?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;
}

View file

@ -35,6 +35,14 @@
<exclude name="WordPress.WP.I18n"/>
</rule>
<!-- Checks the cyclomatic complexity for functions. -->
<rule ref="Generic.Metrics.CyclomaticComplexity">
<properties>
<property name="complexity" value="15" />
<property name="absoluteComplexity" value="30" />
</properties>
</rule>
<!-- Enforce PSR1 compatible namespaces. -->
<rule ref="PSR1.Classes.ClassDeclaration"/>