From d1e94b4bc5a8de4b085449b767542781bafdb011 Mon Sep 17 00:00:00 2001 From: thanghv Date: Wed, 20 May 2020 16:51:25 +0700 Subject: [PATCH] Refactoring --- class/class-mainwp-child-install.php | 126 +++++++++++++----------- class/class-mainwp-child-users.php | 2 +- class/class-mainwp-connect.php | 63 ++++++------ class/class-mainwp-custom-post-type.php | 47 +++++---- class/class-mainwp-pages.php | 101 ++++++++++--------- class/class-tar-archiver.php | 2 +- 6 files changed, 181 insertions(+), 160 deletions(-) diff --git a/class/class-mainwp-child-install.php b/class/class-mainwp-child-install.php index df0c8f5..aaf1d7b 100644 --- a/class/class-mainwp-child-install.php +++ b/class/class-mainwp-child-install.php @@ -63,42 +63,7 @@ class MainWP_Child_Install { } } } 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 ); - } - } - } - } + $this->delete_plugins( $plugins ); } else { $information['status'] = 'FAIL'; } @@ -110,6 +75,47 @@ class MainWP_Child_Install { mainwp_child_helper()->write( $information ); } + private function delete_plugins( $plugins ) { + global $mainWPChild; + 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 ); + } + } + } + } + + } + public function theme_action() { $action = $_POST['action']; @@ -227,27 +233,8 @@ class MainWP_Child_Install { if ( is_wp_error( $result ) ) { if ( true == $ssl_verify && strpos( $url, 'https://' ) === 0 ) { - add_filter( 'http_request_args', array( MainWP_Helper::get_class_name(), 'no_ssl_filter_function' ), 99, 2 ); $ssl_verify = false; - $result = $installer->run( - array( - 'package' => $url, - 'destination' => ( 'plugin' === $_POST['type'] ? WP_PLUGIN_DIR : WP_CONTENT_DIR . '/themes' ), - 'clear_destination' => ( isset( $_POST['overwrite'] ) && $_POST['overwrite'] ), - 'clear_working' => true, - 'hook_extra' => array(), - ) - ); - } - - if ( is_wp_error( $result ) ) { - $err_code = $result->get_error_code(); - if ( $result->get_error_data() && is_string( $result->get_error_data() ) ) { - $error = $result->get_error_data(); - MainWP_Helper::error( $error, $err_code ); - } else { - MainWP_Helper::error( implode( ', ', $error ), $err_code ); - } + $result = $this->try_install_one_more( $url, $installer ); } } @@ -262,8 +249,7 @@ class MainWP_Child_Install { ); if ( 'plugin' === $_POST['type'] ) { $path = $result['destination']; - $fileName = ''; - $rslt = null; + $fileName = ''; wp_cache_set( 'plugins', array(), 'plugins' ); foreach ( $result['source_files'] as $srcFile ) { if ( is_dir( $path . $srcFile ) ) { @@ -304,5 +290,27 @@ class MainWP_Child_Install { $information['destination_name'] = $result['destination_name']; mainwp_child_helper()->write( $information ); } - + + private function try_install_one_more( $url, $installer ) { + add_filter( 'http_request_args', array( MainWP_Helper::get_class_name(), 'no_ssl_filter_function' ), 99, 2 ); + $result = $installer->run( + array( + 'package' => $url, + 'destination' => ( 'plugin' === $_POST['type'] ? WP_PLUGIN_DIR : WP_CONTENT_DIR . '/themes' ), + 'clear_destination' => ( isset( $_POST['overwrite'] ) && $_POST['overwrite'] ), + 'clear_working' => true, + 'hook_extra' => array(), + ) + ); + if ( is_wp_error( $result ) ) { + $err_code = $result->get_error_code(); + if ( $result->get_error_data() && is_string( $result->get_error_data() ) ) { + $error = $result->get_error_data(); + MainWP_Helper::error( $error, $err_code ); + } else { + MainWP_Helper::error( implode( ', ', $error ), $err_code ); + } + } + return $result; + } } diff --git a/class/class-mainwp-child-users.php b/class/class-mainwp-child-users.php index 076d9c9..2b9228c 100644 --- a/class/class-mainwp-child-users.php +++ b/class/class-mainwp-child-users.php @@ -214,7 +214,7 @@ class MainWP_Child_Users { } - public function edit_user( $user_id, $data ) { + public function edit_user( $user_id, $data ) { // phpcs:ignore -- ignore complex method notice, see detail at: function edit_user() in the wp/wp-admin/includes/user.php. $wp_roles = wp_roles(); $user = new \stdClass(); diff --git a/class/class-mainwp-connect.php b/class/class-mainwp-connect.php index f467612..61d8cdd 100644 --- a/class/class-mainwp-connect.php +++ b/class/class-mainwp-connect.php @@ -238,16 +238,44 @@ class MainWP_Connect { } } } - if ( isset( $_REQUEST['fdl'] ) ) { if ( stristr( $_REQUEST['fdl'], '..' ) ) { return; } - MainWP_Utility::instance()->upload_file( $_REQUEST['fdl'], isset( $_REQUEST['foffset'] ) ? $_REQUEST['foffset'] : 0 ); exit; } + // to support open not wp-admin url. + if ( isset( $_REQUEST['open_location'] ) ) { + $open_location = base64_decode( $_REQUEST['open_location'] ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. + $this->open_location_redirect( $open_location ); + } + $this->where_redirect(); + } + private function open_location_redirect( $open_location ) { + $_vars = MainWP_Helper::parse_query( $open_location ); + $_path = wp_parse_url( $open_location, PHP_URL_PATH ); + if ( isset( $_vars['_mwpNoneName'] ) && isset( $_vars['_mwpNoneValue'] ) ) { + $_vars[ $_vars['_mwpNoneName'] ] = wp_create_nonce( $_vars['_mwpNoneValue'] ); + unset( $_vars['_mwpNoneName'] ); + unset( $_vars['_mwpNoneValue'] ); + $open_url = ''; + foreach ( $_vars as $key => $value ) { + $open_url .= $key . '=' . $value . '&'; + } + $open_url = rtrim( $open_url, '&' ); + $open_location = '/wp-admin/' . $_path . '?' . $open_url; + } else { + if ( strpos( $open_location, 'nonce=child_temp_nonce' ) !== false ) { + $open_location = str_replace( 'nonce=child_temp_nonce', 'nonce=' . wp_create_nonce( 'wp-ajax' ), $open_location ); + } + } + wp_safe_redirect( site_url() . $open_location ); + exit(); + } + + private function where_redirect() { $where = isset( $_REQUEST['where'] ) ? $_REQUEST['where'] : ''; if ( isset( $_POST['f'] ) || isset( $_POST['file'] ) ) { $file = ''; @@ -264,37 +292,10 @@ class MainWP_Connect { $_SESSION['file'] = $file; $_SESSION['size'] = $_POST['size']; } - - // to support open not wp-admin url. - $open_location = isset( $_REQUEST['open_location'] ) ? $_REQUEST['open_location'] : ''; - if ( ! empty( $open_location ) ) { - $open_location = base64_decode( $open_location ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons. - $_vars = MainWP_Helper::parse_query( $open_location ); - $_path = wp_parse_url( $open_location, PHP_URL_PATH ); - if ( isset( $_vars['_mwpNoneName'] ) && isset( $_vars['_mwpNoneValue'] ) ) { - $_vars[ $_vars['_mwpNoneName'] ] = wp_create_nonce( $_vars['_mwpNoneValue'] ); - unset( $_vars['_mwpNoneName'] ); - unset( $_vars['_mwpNoneValue'] ); - $open_url = ''; - foreach ( $_vars as $key => $value ) { - $open_url .= $key . '=' . $value . '&'; - } - $open_url = rtrim( $open_url, '&' ); - $open_location = '/wp-admin/' . $_path . '?' . $open_url; - } else { - if ( strpos( $open_location, 'nonce=child_temp_nonce' ) !== false ) { - $open_location = str_replace( 'nonce=child_temp_nonce', 'nonce=' . wp_create_nonce( 'wp-ajax' ), $open_location ); - } - } - wp_safe_redirect( site_url() . $open_location ); - exit(); - } - wp_safe_redirect( admin_url( $where ) ); - - exit(); + exit(); } - + public function check_login() { // to login requires 'mainwpsignature'. diff --git a/class/class-mainwp-custom-post-type.php b/class/class-mainwp-custom-post-type.php index 9edb37e..f88e3b6 100755 --- a/class/class-mainwp-custom-post-type.php +++ b/class/class-mainwp-custom-post-type.php @@ -299,30 +299,18 @@ class MainWP_Custom_Post_Type { private function insert_postmeta( $post_id, $data, $check_image_existed, $is_woocomerce ) { foreach ( $data['postmeta'] as $key ) { if ( isset( $key['meta_key'] ) && isset( $key['meta_value'] ) ) { + $meta_value = $key['meta_value']; if ( $is_woocomerce ) { if ( '_sku' == $key['meta_key'] ) { - if ( ! wc_product_has_unique_sku( $post_id, $key['meta_value'] ) ) { + if ( ! wc_product_has_unique_sku( $post_id, $meta_value ) ) { return array( 'error' => __( 'Product SKU must be unique', $this->plugin_translate ) ); } } - - if ( '_product_image_gallery' == $key['meta_key'] ) { - $product_image_gallery = array(); + if ( '_product_image_gallery' == $key['meta_key'] ) { if ( isset( $data['extras']['woocommerce']['product_images'] ) ) { - foreach ( $data['extras']['woocommerce']['product_images'] as $product_image ) { - try { - $upload_featured_image = MainWP_Utility::upload_image( $product_image, array(), $check_image_existed ); - - if ( null !== $upload_featured_image ) { - $product_image_gallery[] = $upload_featured_image['id']; - } else { - return array( 'error' => __( 'Cannot add product image', $this->plugin_translate ) ); - } - } catch ( \Exception $e ) { - continue; - } - } - $key['meta_value'] = implode( $product_image_gallery, ',' ); + $ret = $this->upload_postmeta_image( $data['extras']['woocommerce']['product_images'], $meta_value, $check_image_existed ); + if ( true !== $ret ) + return $ret; } else { continue; } @@ -335,7 +323,7 @@ class MainWP_Custom_Post_Type { $upload_featured_image = MainWP_Utility::upload_image( $data['extras']['featured_image'], array(), $check_image_existed ); if ( null !== $upload_featured_image ) { - $key['meta_value'] = $upload_featured_image['id']; + $meta_value = $upload_featured_image['id']; } else { return array( 'error' => __( 'Cannot add featured image', $this->plugin_translate ) ); } @@ -347,7 +335,7 @@ class MainWP_Custom_Post_Type { } } - $meta_value = maybe_unserialize( $key['meta_value'] ); + $meta_value = maybe_unserialize( $meta_value ); if ( add_post_meta( $post_id, $key['meta_key'], $meta_value ) === false ) { return array( 'error' => __( 'Error when adding post meta', $this->plugin_translate ) . ' `' . esc_html( $key['meta_key'] ) . '`' ); } @@ -355,4 +343,23 @@ class MainWP_Custom_Post_Type { } return true; } + + private function upload_postmeta_image( $product_images, &$meta_value, $check_image_existed ){ + $product_image_gallery = array(); + foreach ( $product_images as $product_image ) { + try { + $upload_featured_image = MainWP_Utility::upload_image( $product_image, array(), $check_image_existed ); + + if ( null !== $upload_featured_image ) { + $product_image_gallery[] = $upload_featured_image['id']; + } else { + return array( 'error' => __( 'Cannot add product image', $this->plugin_translate ) ); + } + } catch ( \Exception $e ) { + continue; + } + } + $meta_value = implode( $product_image_gallery, ',' ); + return true; + } } diff --git a/class/class-mainwp-pages.php b/class/class-mainwp-pages.php index 4caa34f..b5f4f7a 100644 --- a/class/class-mainwp-pages.php +++ b/class/class-mainwp-pages.php @@ -105,74 +105,79 @@ class MainWP_Pages { // if preserve branding and do not hide menus. if ( ( ! $remove_all_child_menu && 'T' !== $is_hide ) || $cancelled_branding ) { + $branding_header = isset( $branding_opts['branding_header'] ) ? $branding_opts['branding_header'] : array(); if ( ( is_array( $branding_header ) && ! empty( $branding_header['name'] ) ) && ! $cancelled_branding ) { - self::$brandingTitle = stripslashes( $branding_header['name'] ); + self::$brandingTitle = stripslashes( $branding_header['name'] ); $child_menu_title = stripslashes( $branding_header['name'] ); $child_page_title = $child_menu_title . ' Settings'; } else { $child_menu_title = 'MainWP Child'; $child_page_title = 'MainWPSettings'; - } + } + $this->init_pages( $branding_header ); + } + } - $settingsPage = add_submenu_page( 'options-general.php', $child_menu_title, $child_menu_title, 'manage_options', 'mainwp_child_tab', array( &$this, 'render_pages' ) ); + private function init_pages( $child_menu_title ) { - add_action( 'admin_print_scripts-' . $settingsPage, array( MainWP_Clone::get_class_name(), 'print_scripts' ) ); - $subpageargs = array( - 'child_slug' => 'options-general.php', - 'branding' => ( null === self::$brandingTitle ) ? 'MainWP' : self::$brandingTitle, - 'parent_menu' => $settingsPage, - ); + $settingsPage = add_submenu_page( 'options-general.php', $child_menu_title, $child_menu_title, 'manage_options', 'mainwp_child_tab', array( &$this, 'render_pages' ) ); - do_action_deprecated( 'mainwp-child-subpages', array( $subpageargs ), '4.0.7.1', 'mainwp_child_subpages' ); - do_action( 'mainwp_child_subpages', $subpageargs ); + add_action( 'admin_print_scripts-' . $settingsPage, array( MainWP_Clone::get_class_name(), 'print_scripts' ) ); + $subpageargs = array( + 'child_slug' => 'options-general.php', + 'branding' => ( null === self::$brandingTitle ) ? 'MainWP' : self::$brandingTitle, + 'parent_menu' => $settingsPage, + ); - $sub_pages = array(); + do_action_deprecated( 'mainwp-child-subpages', array( $subpageargs ), '4.0.7.1', 'mainwp_child_subpages' ); + do_action( 'mainwp_child_subpages', $subpageargs ); - $all_subpages = apply_filters_deprecated( 'mainwp-child-init-subpages', array( array() ), '4.0.7.1', 'mainwp_child_init_subpages' ); - $all_subpages = apply_filters( 'mainwp_child_init_subpages', $all_subpages ); + $sub_pages = array(); - if ( ! is_array( $all_subpages ) ) { - $all_subpages = array(); - } + $all_subpages = apply_filters_deprecated( 'mainwp-child-init-subpages', array( array() ), '4.0.7.1', 'mainwp_child_init_subpages' ); + $all_subpages = apply_filters( 'mainwp_child_init_subpages', $all_subpages ); - if ( ! self::$subPagesLoaded ) { - foreach ( $all_subpages as $page ) { - $slug = isset( $page['slug'] ) ? $page['slug'] : ''; - if ( empty( $slug ) ) { - continue; - } - $subpage = array(); - $subpage['slug'] = $slug; - $subpage['title'] = $page['title']; - $subpage['page'] = 'mainwp-' . str_replace( ' ', '-', strtolower( str_replace( '-', ' ', $slug ) ) ); - if ( isset( $page['callback'] ) ) { - $subpage['callback'] = $page['callback']; - $created_page = add_submenu_page( 'options-general.php', $subpage['title'], '
' . $subpage['title'] . '
', 'manage_options', $subpage['page'], $subpage['callback'] ); - if ( isset( $page['load_callback'] ) ) { - $subpage['load_callback'] = $page['load_callback']; - add_action( 'load-' . $created_page, $subpage['load_callback'] ); - } - } - $sub_pages[] = $subpage; + if ( ! is_array( $all_subpages ) ) { + $all_subpages = array(); + } + + if ( ! self::$subPagesLoaded ) { + foreach ( $all_subpages as $page ) { + $slug = isset( $page['slug'] ) ? $page['slug'] : ''; + if ( empty( $slug ) ) { + continue; } - self::$subPages = $sub_pages; - self::$subPagesLoaded = true; - } - add_action( 'mainwp-child-pageheader', array( __CLASS__, 'render_header' ) ); - add_action( 'mainwp-child-pagefooter', array( __CLASS__, 'render_footer' ) ); - - global $submenu; - if ( isset( $submenu['options-general.php'] ) ) { - foreach ( $submenu['options-general.php'] as $index => $item ) { - if ( 'mainwp-reports-page' === $item[2] || 'mainwp-reports-settings' === $item[2] ) { - unset( $submenu['options-general.php'][ $index ] ); + $subpage = array(); + $subpage['slug'] = $slug; + $subpage['title'] = $page['title']; + $subpage['page'] = 'mainwp-' . str_replace( ' ', '-', strtolower( str_replace( '-', ' ', $slug ) ) ); + if ( isset( $page['callback'] ) ) { + $subpage['callback'] = $page['callback']; + $created_page = add_submenu_page( 'options-general.php', $subpage['title'], '
' . $subpage['title'] . '
', 'manage_options', $subpage['page'], $subpage['callback'] ); + if ( isset( $page['load_callback'] ) ) { + $subpage['load_callback'] = $page['load_callback']; + add_action( 'load-' . $created_page, $subpage['load_callback'] ); } } + $sub_pages[] = $subpage; + } + self::$subPages = $sub_pages; + self::$subPagesLoaded = true; + } + add_action( 'mainwp-child-pageheader', array( __CLASS__, 'render_header' ) ); + add_action( 'mainwp-child-pagefooter', array( __CLASS__, 'render_footer' ) ); + + global $submenu; + if ( isset( $submenu['options-general.php'] ) ) { + foreach ( $submenu['options-general.php'] as $index => $item ) { + if ( 'mainwp-reports-page' === $item[2] || 'mainwp-reports-settings' === $item[2] ) { + unset( $submenu['options-general.php'][ $index ] ); + } } } } - + public function plugin_row_meta( $plugin_meta, $plugin_file ) { global $mainWPChild; if ( $mainWPChild->plugin_slug !== $plugin_file ) { diff --git a/class/class-tar-archiver.php b/class/class-tar-archiver.php index d927729..b8acca3 100644 --- a/class/class-tar-archiver.php +++ b/class/class-tar-archiver.php @@ -146,7 +146,7 @@ class Tar_Archiver { if ( $this->archive ) { $nodes = glob( ABSPATH . '*' ); if ( ! $includeCoreFiles ) { - $this->include_core_files( &$nodes ); + $this->include_core_files( $nodes ); } $db_files = $this->backup->create_backup_db( dirname( $filepath ) . DIRECTORY_SEPARATOR . 'dbBackup', false, $this );