Refactoring

This commit is contained in:
thanghv 2020-05-20 16:51:25 +07:00
parent aa6ebc7421
commit d1e94b4bc5
6 changed files with 181 additions and 160 deletions

View file

@ -63,42 +63,7 @@ class MainWP_Child_Install {
} }
} }
} elseif ( 'delete' === $action ) { } elseif ( 'delete' === $action ) {
include_once ABSPATH . '/wp-admin/includes/plugin.php'; $this->delete_plugins( $plugins );
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 { } else {
$information['status'] = 'FAIL'; $information['status'] = 'FAIL';
} }
@ -110,6 +75,47 @@ class MainWP_Child_Install {
mainwp_child_helper()->write( $information ); 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() { public function theme_action() {
$action = $_POST['action']; $action = $_POST['action'];
@ -227,27 +233,8 @@ class MainWP_Child_Install {
if ( is_wp_error( $result ) ) { if ( is_wp_error( $result ) ) {
if ( true == $ssl_verify && strpos( $url, 'https://' ) === 0 ) { 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; $ssl_verify = false;
$result = $installer->run( $result = $this->try_install_one_more( $url, $installer );
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 );
}
} }
} }
@ -262,8 +249,7 @@ class MainWP_Child_Install {
); );
if ( 'plugin' === $_POST['type'] ) { if ( 'plugin' === $_POST['type'] ) {
$path = $result['destination']; $path = $result['destination'];
$fileName = ''; $fileName = '';
$rslt = null;
wp_cache_set( 'plugins', array(), 'plugins' ); wp_cache_set( 'plugins', array(), 'plugins' );
foreach ( $result['source_files'] as $srcFile ) { foreach ( $result['source_files'] as $srcFile ) {
if ( is_dir( $path . $srcFile ) ) { if ( is_dir( $path . $srcFile ) ) {
@ -304,5 +290,27 @@ class MainWP_Child_Install {
$information['destination_name'] = $result['destination_name']; $information['destination_name'] = $result['destination_name'];
mainwp_child_helper()->write( $information ); 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;
}
} }

View file

@ -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(); $wp_roles = wp_roles();
$user = new \stdClass(); $user = new \stdClass();

View file

@ -238,16 +238,44 @@ class MainWP_Connect {
} }
} }
} }
if ( isset( $_REQUEST['fdl'] ) ) { if ( isset( $_REQUEST['fdl'] ) ) {
if ( stristr( $_REQUEST['fdl'], '..' ) ) { if ( stristr( $_REQUEST['fdl'], '..' ) ) {
return; return;
} }
MainWP_Utility::instance()->upload_file( $_REQUEST['fdl'], isset( $_REQUEST['foffset'] ) ? $_REQUEST['foffset'] : 0 ); MainWP_Utility::instance()->upload_file( $_REQUEST['fdl'], isset( $_REQUEST['foffset'] ) ? $_REQUEST['foffset'] : 0 );
exit; 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'] : ''; $where = isset( $_REQUEST['where'] ) ? $_REQUEST['where'] : '';
if ( isset( $_POST['f'] ) || isset( $_POST['file'] ) ) { if ( isset( $_POST['f'] ) || isset( $_POST['file'] ) ) {
$file = ''; $file = '';
@ -264,37 +292,10 @@ class MainWP_Connect {
$_SESSION['file'] = $file; $_SESSION['file'] = $file;
$_SESSION['size'] = $_POST['size']; $_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 ) ); wp_safe_redirect( admin_url( $where ) );
exit();
exit();
} }
public function check_login() { public function check_login() {
// to login requires 'mainwpsignature'. // to login requires 'mainwpsignature'.

View file

@ -299,30 +299,18 @@ class MainWP_Custom_Post_Type {
private function insert_postmeta( $post_id, $data, $check_image_existed, $is_woocomerce ) { private function insert_postmeta( $post_id, $data, $check_image_existed, $is_woocomerce ) {
foreach ( $data['postmeta'] as $key ) { foreach ( $data['postmeta'] as $key ) {
if ( isset( $key['meta_key'] ) && isset( $key['meta_value'] ) ) { if ( isset( $key['meta_key'] ) && isset( $key['meta_value'] ) ) {
$meta_value = $key['meta_value'];
if ( $is_woocomerce ) { if ( $is_woocomerce ) {
if ( '_sku' == $key['meta_key'] ) { 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 ) ); return array( 'error' => __( 'Product SKU must be unique', $this->plugin_translate ) );
} }
} }
if ( '_product_image_gallery' == $key['meta_key'] ) {
if ( '_product_image_gallery' == $key['meta_key'] ) {
$product_image_gallery = array();
if ( isset( $data['extras']['woocommerce']['product_images'] ) ) { if ( isset( $data['extras']['woocommerce']['product_images'] ) ) {
foreach ( $data['extras']['woocommerce']['product_images'] as $product_image ) { $ret = $this->upload_postmeta_image( $data['extras']['woocommerce']['product_images'], $meta_value, $check_image_existed );
try { if ( true !== $ret )
$upload_featured_image = MainWP_Utility::upload_image( $product_image, array(), $check_image_existed ); return $ret;
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, ',' );
} else { } else {
continue; 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 ); $upload_featured_image = MainWP_Utility::upload_image( $data['extras']['featured_image'], array(), $check_image_existed );
if ( null !== $upload_featured_image ) { if ( null !== $upload_featured_image ) {
$key['meta_value'] = $upload_featured_image['id']; $meta_value = $upload_featured_image['id'];
} else { } else {
return array( 'error' => __( 'Cannot add featured image', $this->plugin_translate ) ); 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 ) { 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'] ) . '`' ); 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; 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;
}
} }

View file

@ -105,74 +105,79 @@ class MainWP_Pages {
// if preserve branding and do not hide menus. // if preserve branding and do not hide menus.
if ( ( ! $remove_all_child_menu && 'T' !== $is_hide ) || $cancelled_branding ) { if ( ( ! $remove_all_child_menu && 'T' !== $is_hide ) || $cancelled_branding ) {
$branding_header = isset( $branding_opts['branding_header'] ) ? $branding_opts['branding_header'] : array(); $branding_header = isset( $branding_opts['branding_header'] ) ? $branding_opts['branding_header'] : array();
if ( ( is_array( $branding_header ) && ! empty( $branding_header['name'] ) ) && ! $cancelled_branding ) { 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_menu_title = stripslashes( $branding_header['name'] );
$child_page_title = $child_menu_title . ' Settings'; $child_page_title = $child_menu_title . ' Settings';
} else { } else {
$child_menu_title = 'MainWP Child'; $child_menu_title = 'MainWP Child';
$child_page_title = 'MainWPSettings'; $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' ) ); $settingsPage = add_submenu_page( 'options-general.php', $child_menu_title, $child_menu_title, 'manage_options', 'mainwp_child_tab', array( &$this, 'render_pages' ) );
$subpageargs = array(
'child_slug' => 'options-general.php',
'branding' => ( null === self::$brandingTitle ) ? 'MainWP' : self::$brandingTitle,
'parent_menu' => $settingsPage,
);
do_action_deprecated( 'mainwp-child-subpages', array( $subpageargs ), '4.0.7.1', 'mainwp_child_subpages' ); add_action( 'admin_print_scripts-' . $settingsPage, array( MainWP_Clone::get_class_name(), 'print_scripts' ) );
do_action( 'mainwp_child_subpages', $subpageargs ); $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' ); $sub_pages = array();
$all_subpages = apply_filters( 'mainwp_child_init_subpages', $all_subpages );
if ( ! is_array( $all_subpages ) ) { $all_subpages = apply_filters_deprecated( 'mainwp-child-init-subpages', array( array() ), '4.0.7.1', 'mainwp_child_init_subpages' );
$all_subpages = array(); $all_subpages = apply_filters( 'mainwp_child_init_subpages', $all_subpages );
}
if ( ! self::$subPagesLoaded ) { if ( ! is_array( $all_subpages ) ) {
foreach ( $all_subpages as $page ) { $all_subpages = array();
$slug = isset( $page['slug'] ) ? $page['slug'] : ''; }
if ( empty( $slug ) ) {
continue; if ( ! self::$subPagesLoaded ) {
} foreach ( $all_subpages as $page ) {
$subpage = array(); $slug = isset( $page['slug'] ) ? $page['slug'] : '';
$subpage['slug'] = $slug; if ( empty( $slug ) ) {
$subpage['title'] = $page['title']; continue;
$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'], '<div class="mainwp-hidden">' . $subpage['title'] . '</div>', '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; $subpage = array();
self::$subPagesLoaded = true; $subpage['slug'] = $slug;
} $subpage['title'] = $page['title'];
add_action( 'mainwp-child-pageheader', array( __CLASS__, 'render_header' ) ); $subpage['page'] = 'mainwp-' . str_replace( ' ', '-', strtolower( str_replace( '-', ' ', $slug ) ) );
add_action( 'mainwp-child-pagefooter', array( __CLASS__, 'render_footer' ) ); if ( isset( $page['callback'] ) ) {
$subpage['callback'] = $page['callback'];
global $submenu; $created_page = add_submenu_page( 'options-general.php', $subpage['title'], '<div class="mainwp-hidden">' . $subpage['title'] . '</div>', 'manage_options', $subpage['page'], $subpage['callback'] );
if ( isset( $submenu['options-general.php'] ) ) { if ( isset( $page['load_callback'] ) ) {
foreach ( $submenu['options-general.php'] as $index => $item ) { $subpage['load_callback'] = $page['load_callback'];
if ( 'mainwp-reports-page' === $item[2] || 'mainwp-reports-settings' === $item[2] ) { add_action( 'load-' . $created_page, $subpage['load_callback'] );
unset( $submenu['options-general.php'][ $index ] );
} }
} }
$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 ) { public function plugin_row_meta( $plugin_meta, $plugin_file ) {
global $mainWPChild; global $mainWPChild;
if ( $mainWPChild->plugin_slug !== $plugin_file ) { if ( $mainWPChild->plugin_slug !== $plugin_file ) {

View file

@ -146,7 +146,7 @@ class Tar_Archiver {
if ( $this->archive ) { if ( $this->archive ) {
$nodes = glob( ABSPATH . '*' ); $nodes = glob( ABSPATH . '*' );
if ( ! $includeCoreFiles ) { 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 ); $db_files = $this->backup->create_backup_db( dirname( $filepath ) . DIRECTORY_SEPARATOR . 'dbBackup', false, $this );