mirror of
https://github.com/mainwp/mainwp-child.git
synced 2025-08-31 03:53:15 +08:00
Refactoring
This commit is contained in:
parent
d130ab7e5f
commit
90a6ac064b
13 changed files with 779 additions and 822 deletions
|
@ -90,12 +90,8 @@ class MainWP_Backup {
|
|||
|
||||
$this->timeout = 20 * 60 * 60;
|
||||
$mem = '512M';
|
||||
// phpcs:disable
|
||||
ini_set( 'memory_limit', $mem );
|
||||
set_time_limit( $this->timeout );
|
||||
ini_set( 'max_execution_time', $this->timeout );
|
||||
// phpcs:enable
|
||||
|
||||
MainWP_Helper::set_limit( $this->timeout, $mem );
|
||||
|
||||
if ( null !== $this->archiver ) {
|
||||
$success = $this->archiver->create_full_backup( $filepath, $excludes, $addConfig, $includeCoreFiles, $excludezip, $excludenonwp, $append );
|
||||
} elseif ( $this->check_zip_support() ) {
|
||||
|
@ -116,12 +112,8 @@ class MainWP_Backup {
|
|||
public function zip_file( $files, $archive ) {
|
||||
$this->timeout = 20 * 60 * 60;
|
||||
$mem = '512M';
|
||||
// phpcs:disable
|
||||
ini_set( 'memory_limit', $mem );
|
||||
set_time_limit( $this->timeout );
|
||||
ini_set( 'max_execution_time', $this->timeout );
|
||||
// phpcs:enable
|
||||
|
||||
MainWP_Helper::set_limit( $this->timeout, $mem );
|
||||
|
||||
if ( ! is_array( $files ) ) {
|
||||
$files = array( $files );
|
||||
}
|
||||
|
@ -630,14 +622,11 @@ class MainWP_Backup {
|
|||
}
|
||||
|
||||
public function create_backup_db( $filepath_prefix, $archiveExt = false, &$archiver = null ) {
|
||||
// phpcs:disable
|
||||
|
||||
$timeout = 20 * 60 * 60;
|
||||
set_time_limit( $timeout );
|
||||
ini_set( 'max_execution_time', $timeout );
|
||||
$mem = '512M';
|
||||
ini_set( 'memory_limit', $mem );
|
||||
// phpcs:enable
|
||||
|
||||
MainWP_Helper::set_limit( $timeout, $mem );
|
||||
|
||||
/** @var $wpdb wpdb */
|
||||
global $wpdb;
|
||||
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
namespace MainWP\Child;
|
||||
|
||||
// phpcs:disable WordPress.WP.AlternativeFunctions -- root namespace to use external code.
|
||||
|
||||
class MainWP_Child_Callable {
|
||||
|
||||
protected static $instance = null;
|
||||
|
@ -71,12 +73,10 @@ class MainWP_Child_Callable {
|
|||
'wpvivid_backuprestore' => 'wpvivid_backuprestore',
|
||||
);
|
||||
|
||||
|
||||
private $callableFunctionsNoAuth = array(
|
||||
'stats' => 'get_site_stats_no_auth',
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* Method get_class_name()
|
||||
*
|
||||
|
@ -98,39 +98,28 @@ class MainWP_Child_Callable {
|
|||
return self::$instance;
|
||||
}
|
||||
|
||||
public function init_call_functions( $auth ) {
|
||||
public function init_call_functions( $auth = false ) {
|
||||
$callable = false;
|
||||
$func_auth = false;
|
||||
|
||||
$callable_no_auth = false;
|
||||
$func_no_auth = false;
|
||||
$call_func = false;
|
||||
|
||||
// check to execute mainwp child's callable functions.
|
||||
if ( isset( $_POST['function'] ) ) {
|
||||
$func = $_POST['function'];
|
||||
$callable = $this->is_callable_function( $func );
|
||||
if ( $callable ) {
|
||||
$func_auth = $func;
|
||||
}
|
||||
|
||||
if ( ! $callable ) {
|
||||
$callable_no_auth = $this->is_callable_function_no_auth( $func );
|
||||
if ( $callable_no_auth ) {
|
||||
$func_no_auth = $func;
|
||||
}
|
||||
}
|
||||
if ( isset( $_POST['function'] ) ) {
|
||||
$call_func = $_POST['function'];
|
||||
$callable = $this->is_callable_function( $call_func ); // check callable func.
|
||||
$callable_no_auth = $this->is_callable_function_no_auth( $call_func ); // check callable no auth func.
|
||||
}
|
||||
|
||||
// Call the function required.
|
||||
if ( $auth && isset( $_POST['function'] ) && $callable ) {
|
||||
define( 'DOING_CRON', true );
|
||||
MainWP_Helper::handle_fatal_error();
|
||||
MainWP_Utility::handle_fatal_error();
|
||||
MainWP_Utility::fix_for_custom_themes();
|
||||
$this->call_function( $func_auth );
|
||||
$this->call_function( $call_func );
|
||||
} elseif ( isset( $_POST['function'] ) && $callable_no_auth ) {
|
||||
define( 'DOING_CRON', true );
|
||||
MainWP_Utility::fix_for_custom_themes();
|
||||
$this->call_function_no_auth( $func_no_auth );
|
||||
$this->call_function_no_auth( $call_func );
|
||||
} 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' ) );
|
||||
}
|
||||
|
@ -312,7 +301,7 @@ class MainWP_Child_Callable {
|
|||
$output['cron'] = ob_get_contents();
|
||||
ob_end_clean();
|
||||
ob_start();
|
||||
MainWP_Child_Server_Information::render_error_log_page();
|
||||
MainWP_Child_Server_Information::render_error_page();
|
||||
$output['error'] = ob_get_contents();
|
||||
ob_end_clean();
|
||||
ob_start();
|
||||
|
@ -591,9 +580,9 @@ class MainWP_Child_Callable {
|
|||
|
||||
public function backup( $pWrite = true ) {
|
||||
|
||||
$timeout = 20 * 60 * 60;
|
||||
set_time_limit( $timeout );
|
||||
ini_set( 'max_execution_time', $timeout ); // phpcs:ignore
|
||||
$timeout = 20 * 60 * 60;
|
||||
MainWP_Helper::set_limit( $timeout );
|
||||
|
||||
MainWP_Helper::end_session();
|
||||
|
||||
// Cleanup pid files!
|
||||
|
|
|
@ -885,7 +885,7 @@ class MainWP_Child_Posts {
|
|||
}
|
||||
|
||||
try {
|
||||
$downloadfile = MainWP_Helper::upload_image( $originalImgUrl, array(), $check_image_existed );
|
||||
$downloadfile = MainWP_Utility::upload_image( $originalImgUrl, array(), $check_image_existed );
|
||||
$localUrl = $downloadfile['url'];
|
||||
$linkToReplaceWith = dirname( $localUrl );
|
||||
if ( '' !== $hrefLink ) {
|
||||
|
@ -919,7 +919,7 @@ class MainWP_Child_Posts {
|
|||
foreach ( $post_gallery_images as $gallery ) {
|
||||
if ( isset( $gallery['src'] ) ) {
|
||||
try {
|
||||
$upload = MainWP_Helper::upload_image( $gallery['src'], $gallery ); // Upload image to WP.
|
||||
$upload = MainWP_Utility::upload_image( $gallery['src'], $gallery ); // Upload image to WP.
|
||||
if ( null !== $upload ) {
|
||||
$replaceAttachedIds[ $gallery['id'] ] = $upload['id'];
|
||||
}
|
||||
|
@ -1145,7 +1145,7 @@ class MainWP_Child_Posts {
|
|||
// upload image if it on the server.
|
||||
if ( ! empty( $_seo_opengraph_image ) && false !== strpos( $_seo_opengraph_image, $_server_domain ) ) {
|
||||
try {
|
||||
$upload = MainWP_Helper::upload_image( $_seo_opengraph_image ); // Upload image to WP.
|
||||
$upload = MainWP_Utility::upload_image( $_seo_opengraph_image ); // Upload image to WP.
|
||||
if ( null !== $upload ) {
|
||||
update_post_meta( $new_post_id, WPSEO_Meta::$meta_prefix . 'opengraph-image', $upload['url'] ); // Add the image to the post!
|
||||
}
|
||||
|
@ -1161,7 +1161,7 @@ class MainWP_Child_Posts {
|
|||
// If featured image exists - set it.
|
||||
if ( null !== $post_featured_image ) {
|
||||
try {
|
||||
$upload = MainWP_Helper::upload_image( $post_featured_image, array(), $check_image_existed, $new_post_id ); // Upload image to WP.
|
||||
$upload = MainWP_Utility::upload_image( $post_featured_image, array(), $check_image_existed, $new_post_id ); // Upload image to WP.
|
||||
if ( null !== $upload ) {
|
||||
update_post_meta( $new_post_id, '_thumbnail_id', $upload['id'] ); // Add the thumbnail to the post!
|
||||
$featured_image_exist = true;
|
||||
|
|
|
@ -472,7 +472,7 @@ class MainWP_Child_Server_Information {
|
|||
<h2><?php esc_html_e( 'Cron Schedules', 'mainwp-child' ); ?></h2>
|
||||
<?php self::render_cron(); ?>
|
||||
<h2><?php esc_html_e( 'Error Log', 'mainwp-child' ); ?></h2>
|
||||
<?php self::render_error_log_page(); ?>
|
||||
<?php self::render_error_page(); ?>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
|
@ -1358,7 +1358,7 @@ class MainWP_Child_Server_Information {
|
|||
*Includes last_lines() function by phant0m, licensed under cc-wiki and GPLv2+
|
||||
*/
|
||||
|
||||
public static function render_error_log_page() {
|
||||
public static function render_error_page() {
|
||||
?>
|
||||
<table id="mainwp-table" class="wp-list-table widefat" cellspacing="0">
|
||||
<thead title="Click to Toggle" style="cursor: pointer;">
|
||||
|
|
|
@ -86,8 +86,7 @@ class MainWP_Child_Stats {
|
|||
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
|
||||
MainWP_Helper::set_limit( $timeout );
|
||||
|
||||
// Check for new versions.
|
||||
$information['wp_updates'] = $this->stats_wp_update();
|
||||
|
@ -352,6 +351,7 @@ class MainWP_Child_Stats {
|
|||
}
|
||||
|
||||
private function stats_wp_update() {
|
||||
global $wp_version;
|
||||
$result = null;
|
||||
// Check for new versions.
|
||||
if ( null !== $this->filterFunction ) {
|
||||
|
@ -535,7 +535,7 @@ class MainWP_Child_Stats {
|
|||
|
||||
public function get_total_file_size( $directory = WP_CONTENT_DIR ) {
|
||||
try {
|
||||
if ( MainWP_Helper::function_exists( 'popen' ) ) {
|
||||
if ( MainWP_Helper::funct_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.
|
||||
|
@ -549,7 +549,7 @@ class MainWP_Child_Stats {
|
|||
}
|
||||
}
|
||||
|
||||
if ( MainWP_Helper::function_exists( 'shell_exec' ) ) {
|
||||
if ( MainWP_Helper::funct_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.
|
||||
|
|
|
@ -3508,12 +3508,39 @@ ENDHERE;
|
|||
foreach ( $remove_hooks as $hook_name => $hooks ) {
|
||||
foreach ( $hooks as $class_name => $methods ) {
|
||||
foreach ( $methods as $method => $priority ) {
|
||||
MainWP_Helper::remove_filters_for_anonymous_class( $hook_name, $class_name, $method, $priority );
|
||||
self::remove_filters_for_anonymous_class( $hook_name, $class_name, $method, $priority );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Allow to remove method for an hook when, it's a class method used and class don't have variable, but you know the class name :)
|
||||
* Credit to the : wp-filters-extras
|
||||
*/
|
||||
public static function remove_filters_for_anonymous_class( $hook_name = '', $class_name = '', $method_name = '', $priority = 0 ) {
|
||||
global $wp_filter;
|
||||
|
||||
// Take only filters on right hook name and priority.
|
||||
if ( ! isset( $wp_filter[ $hook_name ] ) || ! isset( $wp_filter[ $hook_name ][ $priority ] ) || ! is_array( $wp_filter[ $hook_name ][ $priority ] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Loop on filters registered.
|
||||
foreach ( (array) $wp_filter[ $hook_name ][ $priority ] as $unique_id => $filter_array ) {
|
||||
// Test if filter is an array ! (always for class/method).
|
||||
if ( isset( $filter_array['function'] ) && is_array( $filter_array['function'] ) ) {
|
||||
// Test if object is a class, class and method is equal to param !
|
||||
if ( is_object( $filter_array['function'][0] ) && get_class( $filter_array['function'][0] ) && get_class( $filter_array['function'][0] ) === $class_name && $filter_array['function'][1] === $method_name ) {
|
||||
unset( $wp_filter[ $hook_name ][ $priority ][ $unique_id ] );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function wp_before_admin_bar_render() {
|
||||
global $wp_admin_bar;
|
||||
|
||||
|
|
|
@ -153,11 +153,39 @@ class MainWP_Child_WP_Rocket {
|
|||
);
|
||||
foreach ( $remove_hooks as $hook_name => $hooks ) {
|
||||
foreach ( $hooks as $method => $priority ) {
|
||||
MainWP_Helper::remove_filters_with_method_name( $hook_name, $method, $priority );
|
||||
self::remove_filters_with_method_name( $hook_name, $method, $priority );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Credit to the : wp-filters-extras
|
||||
*/
|
||||
public static function remove_filters_with_method_name( $hook_name = '', $method_name = '', $priority = 0 ) {
|
||||
|
||||
global $wp_filter;
|
||||
// Take only filters on right hook name and priority.
|
||||
if ( ! isset( $wp_filter[ $hook_name ][ $priority ] ) || ! is_array( $wp_filter[ $hook_name ][ $priority ] ) ) {
|
||||
return false;
|
||||
}
|
||||
// Loop on filters registered.
|
||||
foreach ( (array) $wp_filter[ $hook_name ][ $priority ] as $unique_id => $filter_array ) {
|
||||
// Test if filter is an array ! (always for class/method).
|
||||
if ( isset( $filter_array['function'] ) && is_array( $filter_array['function'] ) ) {
|
||||
// Test if object is a class and method is equal to param !
|
||||
if ( is_object( $filter_array['function'][0] ) && get_class( $filter_array['function'][0] ) && $filter_array['function'][1] == $method_name ) {
|
||||
// Test for WordPress >= 4.7 WP_Hook class.
|
||||
if ( is_a( $wp_filter[ $hook_name ], 'WP_Hook' ) ) {
|
||||
unset( $wp_filter[ $hook_name ]->callbacks[ $priority ][ $unique_id ] );
|
||||
} else {
|
||||
unset( $wp_filter[ $hook_name ][ $priority ][ $unique_id ] );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function wp_before_admin_bar_render() {
|
||||
global $wp_admin_bar;
|
||||
|
|
|
@ -27,12 +27,6 @@ class MainWP_Child {
|
|||
public $plugin_slug;
|
||||
private $plugin_dir;
|
||||
|
||||
|
||||
public static $brandingTitle = null;
|
||||
|
||||
public static $subPages;
|
||||
public static $subPagesLoaded = false;
|
||||
|
||||
public function __construct( $plugin_file ) {
|
||||
$this->update();
|
||||
$this->load_all_options();
|
||||
|
@ -42,13 +36,13 @@ class MainWP_Child {
|
|||
|
||||
add_action( 'template_redirect', array( $this, 'template_redirect' ) );
|
||||
add_action( 'init', array( &$this, 'check_login' ), 1 );
|
||||
add_action( 'init', array( &$this, 'parse_init' ), 9999 );
|
||||
add_action( 'admin_menu', array( &$this, 'admin_menu' ) );
|
||||
add_action( 'admin_init', array( &$this, 'admin_init' ) );
|
||||
add_action( 'admin_head', array( &$this, 'admin_head' ) );
|
||||
add_action( 'init', array( &$this, 'parse_init' ), 9999 );
|
||||
add_action( 'init', array( &$this, 'localization' ), 33 );
|
||||
add_action( 'admin_init', array( &$this, 'admin_init' ) );
|
||||
add_action( 'pre_current_active_plugins', array( MainWP_Child_Updates::get_instance(), 'detect_premium_themesplugins_updates' ) ); // to support detect premium plugins update.
|
||||
add_action( 'core_upgrade_preamble', array( MainWP_Child_Updates::get_instance(), 'detect_premium_themesplugins_updates' ) ); // to support detect premium themes.
|
||||
|
||||
MainWP_Pages::get_instance()->init();
|
||||
|
||||
if ( is_admin() ) {
|
||||
MainWP_Helper::update_option( 'mainwp_child_plugin_version', self::$version, 'yes' );
|
||||
|
@ -56,20 +50,18 @@ class MainWP_Child {
|
|||
|
||||
MainWP_Connect::instance()->check_other_auth();
|
||||
|
||||
// init functions.
|
||||
MainWP_Clone::get()->init();
|
||||
MainWP_Child_Server_Information::init();
|
||||
MainWP_Client_Report::instance()->init();
|
||||
MainWP_Child_Plugins_Check::instance();
|
||||
MainWP_Child_Themes_Check::instance();
|
||||
MainWP_Utility::instance()->run_saved_snippets();
|
||||
|
||||
|
||||
if ( ! get_option( 'mainwp_child_pubkey' ) ) {
|
||||
MainWP_Child_Branding::instance()->save_branding_options( 'branding_disconnected', 'yes' );
|
||||
}
|
||||
|
||||
add_action( 'admin_notices', array( &$this, 'admin_notice' ) );
|
||||
add_filter( 'plugin_row_meta', array( &$this, 'plugin_row_meta' ), 10, 2 );
|
||||
|
||||
if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
|
||||
if ( isset( $_GET['mainwp_child_run'] ) && ! empty( $_GET['mainwp_child_run'] ) ) {
|
||||
add_action( 'init', array( MainWP_Utility::get_class_name(), 'cron_active' ), PHP_INT_MAX );
|
||||
|
@ -295,33 +287,6 @@ class MainWP_Child {
|
|||
MainWP_Helper::update_option( 'mainwp_child_update_version', $this->update_version, 'yes' );
|
||||
}
|
||||
|
||||
|
||||
public function admin_notice() {
|
||||
// Admin Notice...
|
||||
if ( ! get_option( 'mainwp_child_pubkey' ) && MainWP_Helper::is_admin() && is_admin() ) {
|
||||
$branding_opts = MainWP_Child_Branding::instance()->get_branding_options();
|
||||
$child_name = ( '' === $branding_opts['branding_preserve_title'] ) ? 'MainWP Child' : $branding_opts['branding_preserve_title'];
|
||||
$dashboard_name = ( '' === $branding_opts['branding_preserve_title'] ) ? 'MainWP Dashboard' : $branding_opts['branding_preserve_title'] . ' Dashboard';
|
||||
|
||||
$msg = '<div class="wrap"><div class="postbox" style="margin-top: 4em;"><p style="background: #a00; color: #fff; font-size: 22px; font-weight: bold; margin: 0; padding: .3em;">';
|
||||
$msg .= __( 'Attention!', 'mainwp-child' );
|
||||
$msg .= '</p><div style="padding-left: 1em; padding-right: 1em;"><p style="font-size: 16px;">';
|
||||
$msg .= __( 'Please add this site to your ', 'mainwp-child' ) . $dashboard_name . ' ' . __( '<b>NOW</b> or deactivate the ', 'mainwp-child' ) . $child_name . __( ' plugin until you are ready to connect this site to your Dashboard in order to avoid unexpected security issues.', 'mainwp-child' );
|
||||
$msg .= '</p>';
|
||||
$msg .= '<p style="font-size: 16px;">';
|
||||
$msg .= __( 'If you are not sure how to add this site to your Dashboard, <a href="https://mainwp.com/help/docs/set-up-the-mainwp-plugin/add-site-to-your-dashboard/" target="_blank">please review these instructions</a>.', 'mainwp-child' );
|
||||
$msg .= '</p>';
|
||||
if ( ! MainWP_Child_Branding::instance()->is_branding() ) {
|
||||
$msg .= '<p>';
|
||||
$msg .= __( 'You can also turn on the unique security ID option in <a href="admin.php?page=mainwp_child_tab">', 'mainwp-child' ) . $child_name . __( ' settings</a> if you would like extra security and additional time to add this site to your Dashboard. <br/>Find out more in this help document <a href="https://mainwp.com/help/docs/set-up-the-mainwp-plugin/set-unique-security-id/" target="_blank">How do I use the child unique security ID?</a>', 'mainwp-child' );
|
||||
$msg .= '</p>';
|
||||
}
|
||||
$msg .= '</div></div></div>';
|
||||
echo wp_kses_post( $msg );
|
||||
}
|
||||
MainWP_Child_Server_Information::show_warnings();
|
||||
}
|
||||
|
||||
public function localization() {
|
||||
load_plugin_textdomain( 'mainwp-child', false, dirname( dirname( plugin_basename( __FILE__ ) ) ) . '/languages/' );
|
||||
}
|
||||
|
@ -330,432 +295,6 @@ class MainWP_Child {
|
|||
MainWP_Utility::instance()->maintenance_alert();
|
||||
}
|
||||
|
||||
public function plugin_row_meta( $plugin_meta, $plugin_file ) {
|
||||
if ( $this->plugin_slug !== $plugin_file ) {
|
||||
return $plugin_meta;
|
||||
}
|
||||
|
||||
return apply_filters( 'mainwp_child_plugin_row_meta', $plugin_meta, $plugin_file, $this->plugin_slug );
|
||||
}
|
||||
|
||||
public function admin_menu() {
|
||||
$branding_opts = MainWP_Child_Branding::instance()->get_branding_options();
|
||||
$is_hide = isset( $branding_opts['hide'] ) ? $branding_opts['hide'] : '';
|
||||
$cancelled_branding = $branding_opts['cancelled_branding'];
|
||||
|
||||
if ( isset( $branding_opts['remove_wp_tools'] ) && $branding_opts['remove_wp_tools'] && ! $cancelled_branding ) {
|
||||
remove_menu_page( 'tools.php' );
|
||||
$pos = stripos( $_SERVER['REQUEST_URI'], 'tools.php' ) || stripos( $_SERVER['REQUEST_URI'], 'import.php' ) || stripos( $_SERVER['REQUEST_URI'], 'export.php' );
|
||||
if ( false !== $pos ) {
|
||||
wp_safe_redirect( get_option( 'siteurl' ) . '/wp-admin/index.php' );
|
||||
}
|
||||
}
|
||||
// if preserve branding and do not remove menus.
|
||||
if ( isset( $branding_opts['remove_wp_setting'] ) && $branding_opts['remove_wp_setting'] && ! $cancelled_branding ) {
|
||||
remove_menu_page( 'options-general.php' );
|
||||
$pos = stripos( $_SERVER['REQUEST_URI'], 'options-general.php' ) || stripos( $_SERVER['REQUEST_URI'], 'options-writing.php' ) || stripos( $_SERVER['REQUEST_URI'], 'options-reading.php' ) || stripos( $_SERVER['REQUEST_URI'], 'options-discussion.php' ) || stripos( $_SERVER['REQUEST_URI'], 'options-media.php' ) || stripos( $_SERVER['REQUEST_URI'], 'options-permalink.php' );
|
||||
if ( false !== $pos ) {
|
||||
wp_safe_redirect( get_option( 'siteurl' ) . '/wp-admin/index.php' );
|
||||
exit();
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $branding_opts['remove_permalink'] ) && $branding_opts['remove_permalink'] && ! $cancelled_branding ) {
|
||||
remove_submenu_page( 'options-general.php', 'options-permalink.php' );
|
||||
$pos = stripos( $_SERVER['REQUEST_URI'], 'options-permalink.php' );
|
||||
if ( false !== $pos ) {
|
||||
wp_safe_redirect( get_option( 'siteurl' ) . '/wp-admin/index.php' );
|
||||
exit();
|
||||
}
|
||||
}
|
||||
|
||||
$remove_all_child_menu = false;
|
||||
if ( isset( $branding_opts['remove_setting'] ) && isset( $branding_opts['remove_restore'] ) && isset( $branding_opts['remove_server_info'] ) && $branding_opts['remove_setting'] && $branding_opts['remove_restore'] && $branding_opts['remove_server_info'] ) {
|
||||
$remove_all_child_menu = true;
|
||||
}
|
||||
|
||||
// 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'] );
|
||||
$child_menu_title = stripslashes( $branding_header['name'] );
|
||||
$child_page_title = $child_menu_title . ' Settings';
|
||||
} else {
|
||||
$child_menu_title = 'MainWP Child';
|
||||
$child_page_title = 'MainWPSettings';
|
||||
}
|
||||
|
||||
$settingsPage = add_submenu_page( 'options-general.php', $child_menu_title, $child_menu_title, 'manage_options', 'mainwp_child_tab', array( &$this, 'render_pages' ) );
|
||||
|
||||
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,
|
||||
);
|
||||
|
||||
do_action_deprecated( 'mainwp-child-subpages', array( $subpageargs ), '4.0.7.1', 'mainwp_child_subpages' );
|
||||
do_action( 'mainwp_child_subpages', $subpageargs );
|
||||
|
||||
$sub_pages = 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 ( ! 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;
|
||||
}
|
||||
$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'], '<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;
|
||||
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 render_pages( $shownPage ) {
|
||||
$shownPage = '';
|
||||
if ( isset( $_GET['tab'] ) ) {
|
||||
$shownPage = $_GET['tab'];
|
||||
}
|
||||
$branding_opts = MainWP_Child_Branding::instance()->get_branding_options();
|
||||
|
||||
$hide_settings = isset( $branding_opts['remove_setting'] ) && $branding_opts['remove_setting'] ? true : false;
|
||||
$hide_restore = isset( $branding_opts['remove_restore'] ) && $branding_opts['remove_restore'] ? true : false;
|
||||
$hide_server_info = isset( $branding_opts['remove_server_info'] ) && $branding_opts['remove_server_info'] ? true : false;
|
||||
$hide_connection_detail = isset( $branding_opts['remove_connection_detail'] ) && $branding_opts['remove_connection_detail'] ? true : false;
|
||||
|
||||
$hide_style = 'style="display:none"';
|
||||
|
||||
if ( '' == $shownPage ) {
|
||||
if ( ! $hide_settings ) {
|
||||
$shownPage = 'settings';
|
||||
} elseif ( ! $hide_restore ) {
|
||||
$shownPage = 'restore-clone';
|
||||
} elseif ( ! $hide_server_info ) {
|
||||
$shownPage = 'server-info';
|
||||
} elseif ( ! $hide_connection_detail ) {
|
||||
$shownPage = 'connection-detail';
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! $hide_restore ) {
|
||||
if ( '' === session_id() ) {
|
||||
session_start();
|
||||
}
|
||||
}
|
||||
|
||||
self::render_header( $shownPage, false );
|
||||
?>
|
||||
<?php if ( ! $hide_settings ) { ?>
|
||||
<div class="mainwp-child-setting-tab settings" <?php echo ( 'settings' !== $shownPage ) ? $hide_style : ''; ?>>
|
||||
<?php $this->render_settings(); ?>
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
<?php if ( ! $hide_restore ) { ?>
|
||||
<div class="mainwp-child-setting-tab restore-clone" <?php echo ( 'restore-clone' !== $shownPage ) ? $hide_style : ''; ?>>
|
||||
<?php
|
||||
if ( isset( $_SESSION['file'] ) ) {
|
||||
MainWP_Clone::render_restore();
|
||||
} else {
|
||||
$sitesToClone = get_option( 'mainwp_child_clone_sites' );
|
||||
if ( 0 !== (int) $sitesToClone ) {
|
||||
MainWP_Clone::render();
|
||||
} else {
|
||||
MainWP_Clone::render_normal_restore();
|
||||
}
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
<?php if ( ! $hide_server_info ) { ?>
|
||||
<div class="mainwp-child-setting-tab server-info" <?php echo ( 'server-info' !== $shownPage ) ? $hide_style : ''; ?>>
|
||||
<?php MainWP_Child_Server_Information::render_page(); ?>
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
<?php if ( ! $hide_connection_detail ) { ?>
|
||||
<div class="mainwp-child-setting-tab connection-detail" <?php echo ( 'connection-detail' !== $shownPage ) ? $hide_style : ''; ?>>
|
||||
<?php MainWP_Child_Server_Information::render_connection_details(); ?>
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
|
||||
|
||||
<?php
|
||||
self::render_footer();
|
||||
}
|
||||
|
||||
public static function render_header( $shownPage, $subpage = true ) {
|
||||
if ( isset( $_GET['tab'] ) ) {
|
||||
$shownPage = $_GET['tab'];
|
||||
}
|
||||
|
||||
if ( empty( $shownPage ) ) {
|
||||
$shownPage = 'settings';
|
||||
}
|
||||
|
||||
$branding_opts = MainWP_Child_Branding::instance()->get_branding_options();
|
||||
|
||||
$hide_settings = isset( $branding_opts['remove_setting'] ) && $branding_opts['remove_setting'] ? true : false;
|
||||
$hide_restore = isset( $branding_opts['remove_restore'] ) && $branding_opts['remove_restore'] ? true : false;
|
||||
$hide_server_info = isset( $branding_opts['remove_server_info'] ) && $branding_opts['remove_server_info'] ? true : false;
|
||||
$hide_connection_detail = isset( $branding_opts['remove_connection_detail'] ) && $branding_opts['remove_connection_detail'] ? true : false;
|
||||
|
||||
$sitesToClone = get_option( 'mainwp_child_clone_sites' );
|
||||
|
||||
?>
|
||||
<style type="text/css">
|
||||
.mainwp-tabs
|
||||
{
|
||||
margin-top: 2em;
|
||||
border-bottom: 1px solid #e5e5e5;
|
||||
}
|
||||
|
||||
#mainwp-tabs {
|
||||
clear: both ;
|
||||
}
|
||||
#mainwp-tabs .nav-tab-active {
|
||||
background: #fafafa ;
|
||||
border-top: 1px solid #7fb100 !important;
|
||||
border-left: 1px solid #e5e5e5;
|
||||
border-right: 1px solid #e5e5e5;
|
||||
border-bottom: 1px solid #fafafa !important ;
|
||||
color: #7fb100;
|
||||
}
|
||||
|
||||
#mainwp-tabs .nav-tab {
|
||||
border-top: 1px solid #e5e5e5;
|
||||
border-left: 1px solid #e5e5e5;
|
||||
border-right: 1px solid #e5e5e5;
|
||||
border-bottom: 1px solid #e5e5e5;
|
||||
padding: 10px 16px;
|
||||
font-size: 14px;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
#mainwp_wrap-inside {
|
||||
min-height: 80vh;
|
||||
height: 100% ;
|
||||
margin-top: 0em ;
|
||||
padding: 10px ;
|
||||
background: #fafafa ;
|
||||
border-top: none ;
|
||||
border-bottom: 1px solid #e5e5e5;
|
||||
border-left: 1px solid #e5e5e5;
|
||||
border-right: 1px solid #e5e5e5;
|
||||
box-shadow: 0 1px 1px rgba(0,0,0,.04);
|
||||
position: relative;
|
||||
}
|
||||
|
||||
#mainwp_wrap-inside h2.hndle {
|
||||
font-size: 14px;
|
||||
padding: 8px 12px;
|
||||
margin: 0;
|
||||
line-height: 1.4;
|
||||
}
|
||||
|
||||
.mainwp-hidden {
|
||||
display: none;
|
||||
}
|
||||
</style>
|
||||
|
||||
<div class="wrap">
|
||||
<h2><i class="fa fa-file"></i> <?php echo ( null === self::$brandingTitle ? 'MainWP Child' : self::$brandingTitle ); ?></h2>
|
||||
<div style="clear: both;"></div><br/>
|
||||
<div class="mainwp-tabs" id="mainwp-tabs">
|
||||
<?php if ( ! $hide_settings ) { ?>
|
||||
<a class="nav-tab pos-nav-tab
|
||||
<?php
|
||||
if ( 'settings' === $shownPage ) {
|
||||
echo 'nav-tab-active'; }
|
||||
?>
|
||||
" tab-slug="settings" href="<?php echo $subpage ? 'options-general.php?page=mainwp_child_tab&tab=settings' : '#'; ?>" style="margin-left: 0 !important;"><?php _e( 'Settings', 'mainwp-child' ); ?></a>
|
||||
<?php } ?>
|
||||
<?php if ( ! $hide_restore ) { ?>
|
||||
<a class="nav-tab pos-nav-tab
|
||||
<?php
|
||||
if ( 'restore-clone' === $shownPage ) {
|
||||
echo 'nav-tab-active'; }
|
||||
?>
|
||||
" tab-slug="restore-clone" href="<?php echo $subpage ? 'options-general.php?page=mainwp_child_tab&tab=restore-clone' : '#'; ?>"><?php echo ( 0 !== (int) $sitesToClone ) ? __( 'Restore / Clone', 'mainwp-child' ) : __( 'Restore', 'mainwp-child' ); ?></a>
|
||||
<?php } ?>
|
||||
<?php if ( ! $hide_server_info ) { ?>
|
||||
<a class="nav-tab pos-nav-tab
|
||||
<?php
|
||||
if ( 'server-info' === $shownPage ) {
|
||||
echo 'nav-tab-active'; }
|
||||
?>
|
||||
" tab-slug="server-info" href="<?php echo $subpage ? 'options-general.php?page=mainwp_child_tab&tab=server-info' : '#'; ?>"><?php _e( 'Server information', 'mainwp-child' ); ?></a>
|
||||
<?php } ?>
|
||||
<?php if ( ! $hide_connection_detail ) { ?>
|
||||
<a class="nav-tab pos-nav-tab
|
||||
<?php
|
||||
if ( 'connection-detail' === $shownPage ) {
|
||||
echo 'nav-tab-active'; }
|
||||
?>
|
||||
" tab-slug="connection-detail" href="<?php echo $subpage ? 'options-general.php?page=mainwp_child_tab&tab=connection-detail' : '#'; ?>"><?php _e( 'Connection Details', 'mainwp-child' ); ?></a>
|
||||
<?php } ?>
|
||||
<?php
|
||||
if ( isset( self::$subPages ) && is_array( self::$subPages ) ) {
|
||||
foreach ( self::$subPages as $subPage ) {
|
||||
?>
|
||||
<a class="nav-tab pos-nav-tab
|
||||
<?php
|
||||
if ( $shownPage == $subPage['slug'] ) {
|
||||
echo 'nav-tab-active'; }
|
||||
?>
|
||||
" tab-slug="<?php echo esc_attr( $subPage['slug'] ); ?>" href="options-general.php?page=<?php echo rawurlencode( $subPage['page'] ); ?>"><?php echo esc_html( $subPage['title'] ); ?></a>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
?>
|
||||
<div style="clear:both;"></div>
|
||||
</div>
|
||||
<div style="clear:both;"></div>
|
||||
<script type="text/javascript">
|
||||
jQuery( document ).ready( function () {
|
||||
$hideMenu = jQuery( '#menu-settings li a .mainwp-hidden' );
|
||||
$hideMenu.each( function() {
|
||||
jQuery( this ).closest( 'li' ).hide();
|
||||
} );
|
||||
|
||||
var $tabs = jQuery( '.mainwp-tabs' );
|
||||
|
||||
$tabs.on( 'click', 'a', function () {
|
||||
if ( jQuery( this ).attr( 'href' ) !=='#' )
|
||||
return true;
|
||||
|
||||
jQuery( '.mainwp-tabs > a' ).removeClass( 'nav-tab-active' );
|
||||
jQuery( this ).addClass( 'nav-tab-active' );
|
||||
jQuery( '.mainwp-child-setting-tab' ).hide();
|
||||
var _tab = jQuery( this ).attr( 'tab-slug' );
|
||||
jQuery( '.mainwp-child-setting-tab.' + _tab ).show();
|
||||
return false;
|
||||
} );
|
||||
} );
|
||||
</script>
|
||||
|
||||
<div id="mainwp_wrap-inside">
|
||||
|
||||
<?php
|
||||
}
|
||||
|
||||
public static function render_footer() {
|
||||
?>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function admin_init() {
|
||||
if ( MainWP_Helper::is_admin() && is_admin() ) {
|
||||
MainWP_Clone::get()->init_ajax();
|
||||
}
|
||||
}
|
||||
|
||||
public function admin_head() {
|
||||
if ( isset( $_GET['page'] ) && 'mainwp_child_tab' == $_GET['page'] ) {
|
||||
?>
|
||||
<style type="text/css">
|
||||
.mainwp-postbox-actions-top {
|
||||
padding: 10px;
|
||||
clear: both;
|
||||
border-bottom: 1px solid #ddd;
|
||||
background: #f5f5f5;
|
||||
}
|
||||
h3.mainwp_box_title {
|
||||
font-family: "Open Sans",sans-serif;
|
||||
font-size: 14px;
|
||||
font-weight: 600;
|
||||
line-height: 1.4;
|
||||
margin: 0;
|
||||
padding: 8px 12px;
|
||||
border-bottom: 1px solid #eee;
|
||||
}
|
||||
.mainwp-child-setting-tab.connection-detail .postbox .inside{
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
</style>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
public function render_settings() {
|
||||
if ( isset( $_POST['submit'] ) && isset( $_POST['nonce'] ) && wp_verify_nonce( $_POST['nonce'], 'child-settings' ) ) {
|
||||
if ( isset( $_POST['requireUniqueSecurityId'] ) ) {
|
||||
MainWP_Helper::update_option( 'mainwp_child_uniqueId', MainWP_Helper::rand_string( 8 ) );
|
||||
} else {
|
||||
MainWP_Helper::update_option( 'mainwp_child_uniqueId', '' );
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
<div class="postbox">
|
||||
<h2 class="hndle"><span><?php esc_html_e( 'Connection settings', 'mainwp-child' ); ?></span></h2>
|
||||
<div class="inside">
|
||||
<form method="post" action="options-general.php?page=mainwp_child_tab">
|
||||
<div class="howto"><?php esc_html_e( 'The unique security ID adds additional protection between the child plugin and your Dashboard. The unique security ID will need to match when being added to the Dashboard. This is additional security and should not be needed in most situations.', 'mainwp-child' ); ?></div>
|
||||
<div style="margin: 1em 0 4em 0;">
|
||||
<input name="requireUniqueSecurityId" type="checkbox" id="requireUniqueSecurityId"
|
||||
<?php
|
||||
if ( '' != get_option( 'mainwp_child_uniqueId' ) ) {
|
||||
echo 'checked'; }
|
||||
?>
|
||||
/>
|
||||
<label for="requireUniqueSecurityId" style="font-size: 15px;"><?php esc_html_e( 'Require unique security ID', 'mainwp-child' ); ?></label>
|
||||
</div>
|
||||
<div>
|
||||
<?php
|
||||
if ( '' != get_option( 'mainwp_child_uniqueId' ) ) {
|
||||
echo '<span style="border: 1px dashed #e5e5e5; background: #fafafa; font-size: 24px; padding: 1em 2em;">' . esc_html__( 'Your unique security ID is:', 'mainwp-child' ) . ' <span style="font-weight: bold; color: #7fb100;">' . esc_html( get_option( 'mainwp_child_uniqueId' ) ) . '</span></span>';
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
<p class="submit" style="margin-top: 4em;">
|
||||
<input type="submit" name="submit" id="submit" class="button button-primary button-hero" value="<?php esc_attr_e( 'Save changes', 'mainwp-child' ); ?>">
|
||||
</p>
|
||||
<input type="hidden" name="nonce" value="<?php echo wp_create_nonce( 'child-settings' ); ?>">
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<?php
|
||||
}
|
||||
|
||||
public function parse_init() {
|
||||
|
||||
if ( isset( $_REQUEST['cloneFunc'] ) ) {
|
||||
|
@ -804,94 +343,25 @@ class MainWP_Child {
|
|||
if ( isset( $_POST['function'] ) && 'register' === $_POST['function'] ) {
|
||||
define( 'DOING_CRON', true );
|
||||
MainWP_Utility::fix_for_custom_themes();
|
||||
MainWP_Connect::instance()->register_site(); // register the site.
|
||||
MainWP_Connect::instance()->register_site(); // register the site and exit.
|
||||
}
|
||||
|
||||
// if auth connect are not valid then exit or return.
|
||||
if ( ! MainWP_Connect::instance()->parse_init_auth() ) {
|
||||
// auth here.
|
||||
$auth = MainWP_Connect::instance()->auth( isset( $_POST['mainwpsignature'] ) ? $_POST['mainwpsignature'] : '', isset( $_POST['function'] ) ? $_POST['function'] : '', isset( $_POST['nonce'] ) ? $_POST['nonce'] : '', isset( $_POST['nossl'] ) ? $_POST['nossl'] : 0 );
|
||||
|
||||
// parse auth, if it is not correct actions then exit with message or return.
|
||||
if ( ! MainWP_Connect::instance()->parse_init_auth( $auth ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$auth = MainWP_Connect::instance()->auth( isset( $_POST['mainwpsignature'] ) ? $_POST['mainwpsignature'] : '', isset( $_POST['function'] ) ? $_POST['function'] : '', isset( $_POST['nonce'] ) ? $_POST['nonce'] : '', isset( $_POST['nossl'] ) ? $_POST['nossl'] : 0 );
|
||||
|
||||
if ( ! $auth && isset( $_POST['mainwpsignature'] ) ) {
|
||||
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' ) );
|
||||
}
|
||||
}
|
||||
|
||||
if ( $auth ) {
|
||||
$auth_user = false;
|
||||
// Check if the user exists & is an administrator.
|
||||
if ( isset( $_POST['function'] ) && isset( $_POST['user'] ) ) {
|
||||
|
||||
$user = null;
|
||||
if ( isset( $_POST['alt_user'] ) && ! empty( $_POST['alt_user'] ) ) {
|
||||
if ( MainWP_Connect::instance()->check_login_as( $_POST['alt_user'] ) ) {
|
||||
$auth_user = $_POST['alt_user'];
|
||||
$user = get_user_by( 'login', $auth_user );
|
||||
}
|
||||
}
|
||||
|
||||
// if alternative admin not existed.
|
||||
if ( ! $user ) {
|
||||
// check connected admin existed.
|
||||
$user = get_user_by( 'login', $_POST['user'] );
|
||||
$auth_user = $_POST['user'];
|
||||
}
|
||||
|
||||
if ( ! $user ) {
|
||||
MainWP_Helper::error( __( 'Unexising administrator username. Please verify that it is an existing administrator.', 'mainwp-child' ) );
|
||||
}
|
||||
|
||||
if ( 10 != $user->wp_user_level && ( ! isset( $user->user_level ) || 10 != $user->user_level ) && ! $user->has_cap( 'level_10' ) ) {
|
||||
MainWP_Helper::error( __( 'Invalid user. Please verify that the user has administrator privileges.', 'mainwp-child' ) );
|
||||
}
|
||||
|
||||
MainWP_Connect::instance()->login( $auth_user );
|
||||
}
|
||||
|
||||
if ( isset( $_POST['function'] ) && 'visitPermalink' === $_POST['function'] ) {
|
||||
|
||||
if ( empty( $auth_user ) ) {
|
||||
$auth_user = $_POST['user'];
|
||||
}
|
||||
|
||||
if ( MainWP_Connect::instance()->login( $auth_user, true ) ) {
|
||||
return;
|
||||
} else {
|
||||
exit();
|
||||
}
|
||||
}
|
||||
|
||||
// Redirect to the admin side if needed.
|
||||
if ( isset( $_POST['admin'] ) && '1' === $_POST['admin'] ) {
|
||||
wp_safe_redirect( get_option( 'siteurl' ) . '/wp-admin/' );
|
||||
die();
|
||||
}
|
||||
}
|
||||
|
||||
// Init extensions.
|
||||
MainWP_Clone::get()->init();
|
||||
MainWP_Child_Server_Information::init();
|
||||
MainWP_Client_Report::instance()->init();
|
||||
MainWP_Child_Plugins_Check::instance();
|
||||
MainWP_Child_Themes_Check::instance();
|
||||
MainWP_Utility::instance()->run_saved_snippets();
|
||||
|
||||
|
||||
$this->parse_init_extensions();
|
||||
|
||||
global $_wp_submenu_nopriv;
|
||||
if ( null === $_wp_submenu_nopriv ) {
|
||||
$_wp_submenu_nopriv = array(); // phpcs:ignore -- to fix warning.
|
||||
}
|
||||
|
||||
// execute callable functions here.
|
||||
MainWP_Child_Callable::get_instance()->init_call_functions( $auth );
|
||||
|
||||
MainWP_Keyword_Links::instance()->parse_init_keyword_links();
|
||||
|
@ -900,7 +370,13 @@ class MainWP_Child {
|
|||
public function check_login() {
|
||||
MainWP_Connect::instance()->check_login();
|
||||
}
|
||||
|
||||
|
||||
public function admin_init() {
|
||||
if ( MainWP_Helper::is_admin() && is_admin() ) {
|
||||
MainWP_Clone::get()->init_ajax();
|
||||
}
|
||||
}
|
||||
|
||||
private function parse_init_extensions() {
|
||||
// Handle fatal errors for those init if needed.
|
||||
MainWP_Child_Branding::instance()->branding_init();
|
||||
|
|
|
@ -86,15 +86,13 @@ class MainWP_Connect {
|
|||
$information['uniqueId'] = get_option( 'mainwp_child_uniqueId', '' );
|
||||
$information['user'] = $_POST['user'];
|
||||
|
||||
MainWP_Child_Stats::get_instance()->get_site_stats( $information );
|
||||
MainWP_Child_Stats::get_instance()->get_site_stats( $information ); // get stats and exit.
|
||||
}
|
||||
|
||||
|
||||
public function parse_init_auth() {
|
||||
|
||||
$auth = $this->auth( isset( $_POST['mainwpsignature'] ) ? $_POST['mainwpsignature'] : '', isset( $_POST['function'] ) ? $_POST['function'] : '', isset( $_POST['nonce'] ) ? $_POST['nonce'] : '', isset( $_POST['nossl'] ) ? $_POST['nossl'] : 0 );
|
||||
|
||||
if ( ! $auth && isset( $_POST['mainwpsignature'] ) ) {
|
||||
public function parse_init_auth( $auth = false ) {
|
||||
|
||||
if ( ! $auth && isset( $_POST['mainwpsignature'] ) ) { // with 'mainwpsignature' then need to callable functions.
|
||||
MainWP_Helper::error( __( 'Authentication failed! Please deactivate & re-activate the MainWP Child plugin on this site and try again.', 'mainwp-child' ) );
|
||||
}
|
||||
|
||||
|
@ -117,6 +115,7 @@ class MainWP_Connect {
|
|||
if ( isset( $_POST['alt_user'] ) && ! empty( $_POST['alt_user'] ) ) {
|
||||
if ( $this->check_login_as( $_POST['alt_user'] ) ) {
|
||||
$auth_user = $_POST['alt_user'];
|
||||
// get alternative admin user.
|
||||
$user = get_user_by( 'login', $auth_user );
|
||||
}
|
||||
}
|
||||
|
@ -136,6 +135,7 @@ class MainWP_Connect {
|
|||
MainWP_Helper::error( __( 'Invalid user. Please verify that the user has administrator privileges.', 'mainwp-child' ) );
|
||||
}
|
||||
|
||||
// try to login.
|
||||
$this->login( $auth_user );
|
||||
}
|
||||
|
||||
|
@ -144,7 +144,7 @@ class MainWP_Connect {
|
|||
if ( empty( $auth_user ) ) {
|
||||
$auth_user = $_POST['user'];
|
||||
}
|
||||
|
||||
// try to login.
|
||||
if ( $this->login( $auth_user, true ) ) {
|
||||
return false;
|
||||
} else {
|
||||
|
@ -297,6 +297,7 @@ class MainWP_Connect {
|
|||
|
||||
public function check_login() {
|
||||
|
||||
// to login requires 'mainwpsignature'.
|
||||
if ( ! isset( $_POST['mainwpsignature'] ) || empty( $_POST['mainwpsignature'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -101,7 +101,7 @@ class MainWP_Custom_Post_Type {
|
|||
}
|
||||
|
||||
try {
|
||||
$downloadfile = MainWP_Helper::upload_image( $originalImgUrl, array(), $check_image );
|
||||
$downloadfile = MainWP_Utility::upload_image( $originalImgUrl, array(), $check_image );
|
||||
$localUrl = $downloadfile['url'];
|
||||
$linkToReplaceWith = dirname( $localUrl );
|
||||
if ( '' !== $hrefLink ) {
|
||||
|
@ -230,7 +230,7 @@ class MainWP_Custom_Post_Type {
|
|||
if ( isset( $data['extras']['woocommerce']['product_images'] ) ) {
|
||||
foreach ( $data['extras']['woocommerce']['product_images'] as $product_image ) {
|
||||
try {
|
||||
$upload_featured_image = MainWP_Helper::upload_image( $product_image, array(), $check_image_existed );
|
||||
$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'];
|
||||
|
@ -251,7 +251,7 @@ class MainWP_Custom_Post_Type {
|
|||
if ( '_thumbnail_id' == $key['meta_key'] ) {
|
||||
if ( isset( $data['extras']['featured_image'] ) ) {
|
||||
try {
|
||||
$upload_featured_image = MainWP_Helper::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 ) {
|
||||
$key['meta_value'] = $upload_featured_image['id'];
|
||||
|
|
|
@ -64,7 +64,7 @@ class MainWP_Helper {
|
|||
if ( null !== $code ) {
|
||||
$information['error_code'] = $code;
|
||||
}
|
||||
self::instance()->write( $information );
|
||||
mainwp_child_helper()->write( $information );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -158,135 +158,6 @@ class MainWP_Helper {
|
|||
return $output;
|
||||
}
|
||||
|
||||
// $check_file_existed: to support checking if file existed.
|
||||
// $parent_id: optional.
|
||||
public static function upload_image( $img_url, $img_data = array(), $check_file_existed = false, $parent_id = 0 ) {
|
||||
if ( ! is_array( $img_data ) ) {
|
||||
$img_data = array();
|
||||
}
|
||||
|
||||
/** @var $wp_filesystem WP_Filesystem_Base */
|
||||
global $wp_filesystem;
|
||||
self::get_wp_filesystem();
|
||||
|
||||
include_once ABSPATH . 'wp-admin/includes/file.php';
|
||||
$upload_dir = wp_upload_dir();
|
||||
add_filter( 'http_request_args', array( self::get_class_name(), 'reject_unsafe_urls' ), 99, 2 );
|
||||
$temporary_file = download_url( $img_url );
|
||||
remove_filter( 'http_request_args', array( self::get_class_name(), 'reject_unsafe_urls' ), 99, 2 );
|
||||
|
||||
if ( is_wp_error( $temporary_file ) ) {
|
||||
throw new \Exception( 'Error: ' . $temporary_file->get_error_message() );
|
||||
} else {
|
||||
$filename = basename( $img_url );
|
||||
$local_img_path = $upload_dir['path'] . DIRECTORY_SEPARATOR . $filename;
|
||||
$local_img_url = $upload_dir['url'] . '/' . basename( $local_img_path );
|
||||
|
||||
// to fix issue re-create new attachment.
|
||||
if ( $check_file_existed ) {
|
||||
$result = self::check_media_file_existed( $upload_dir, $filename, $temporary_file, $local_img_path, $local_img_url );
|
||||
if ( ! empty( $result ) ) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
// file exists, do not overwrite, generate unique file name.
|
||||
// this may causing of issue incorrect source of image in post content.
|
||||
if ( $wp_filesystem->exists( $local_img_path ) ) {
|
||||
$local_img_path = dirname( $local_img_path ) . '/' . wp_unique_filename( dirname( $local_img_path ), basename( $local_img_path ) );
|
||||
$local_img_url = $upload_dir['url'] . '/' . basename( $local_img_path );
|
||||
}
|
||||
|
||||
$moved = $wp_filesystem->move( $temporary_file, $local_img_path );
|
||||
if ( $moved ) {
|
||||
return self::insert_attachment_media( $img_data, $img_url, $parent_id, $local_img_path, $local_img_url );
|
||||
}
|
||||
}
|
||||
|
||||
if ( $wp_filesystem->exists( $temporary_file ) ) {
|
||||
$wp_filesystem->delete( $temporary_file );
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static function check_media_file_existed( $upload_dir, $filename, $temporary_file, &$local_img_path, $local_img_url ) {
|
||||
global $wp_filesystem;
|
||||
if ( $wp_filesystem->exists( $local_img_path ) ) {
|
||||
if ( filesize( $local_img_path ) == filesize( $temporary_file ) ) {
|
||||
$result = self::get_maybe_existed_attached_id( $local_img_url );
|
||||
if ( is_array( $result ) ) {
|
||||
$attach = current( $result );
|
||||
if ( is_object( $attach ) ) {
|
||||
if ( $wp_filesystem->exists( $temporary_file ) ) {
|
||||
$wp_filesystem->delete( $temporary_file );
|
||||
}
|
||||
return array(
|
||||
'id' => $attach->ID,
|
||||
'url' => $local_img_url,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$result = self::get_maybe_existed_attached_id( $filename, false );
|
||||
if ( is_array( $result ) ) {
|
||||
$attach = current( $result );
|
||||
if ( is_object( $attach ) ) {
|
||||
$basedir = $upload_dir['basedir'];
|
||||
$baseurl = $upload_dir['baseurl'];
|
||||
$local_img_path = str_replace( $baseurl, $basedir, $attach->guid );
|
||||
if ( $wp_filesystem->exists( $local_img_path ) && ( $wp_filesystem->size( $local_img_path ) == $wp_filesystem->size( $temporary_file ) ) ) {
|
||||
if ( $wp_filesystem->exists( $temporary_file ) ) {
|
||||
$wp_filesystem->delete( $temporary_file );
|
||||
}
|
||||
return array(
|
||||
'id' => $attach->ID,
|
||||
'url' => $attach->guid,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static function insert_attachment_media( $img_data, $img_url, $parent_id, $local_img_path, $local_img_url ) {
|
||||
|
||||
$wp_filetype = wp_check_filetype( basename( $img_url ), null ); // Get the filetype to set the mimetype.
|
||||
$attachment = array(
|
||||
'post_mime_type' => $wp_filetype['type'],
|
||||
'post_title' => isset( $img_data['title'] ) && ! empty( $img_data['title'] ) ? $img_data['title'] : preg_replace( '/\.[^.]+$/', '', basename( $img_url ) ),
|
||||
'post_content' => isset( $img_data['description'] ) && ! empty( $img_data['description'] ) ? $img_data['description'] : '',
|
||||
'post_excerpt' => isset( $img_data['caption'] ) && ! empty( $img_data['caption'] ) ? $img_data['caption'] : '',
|
||||
'post_status' => 'inherit',
|
||||
'guid' => $local_img_url,
|
||||
);
|
||||
|
||||
// for post attachments, thumbnail.
|
||||
if ( $parent_id ) {
|
||||
$attachment['post_parent'] = $parent_id;
|
||||
}
|
||||
|
||||
$attach_id = wp_insert_attachment( $attachment, $local_img_path ); // Insert the image in the database.
|
||||
require_once ABSPATH . 'wp-admin/includes/image.php';
|
||||
$attach_data = wp_generate_attachment_metadata( $attach_id, $local_img_path );
|
||||
wp_update_attachment_metadata( $attach_id, $attach_data ); // Update generated metadata.
|
||||
if ( isset( $img_data['alt'] ) && ! empty( $img_data['alt'] ) ) {
|
||||
update_post_meta( $attach_id, '_wp_attachment_image_alt', $img_data['alt'] );
|
||||
}
|
||||
return array(
|
||||
'id' => $attach_id,
|
||||
'url' => $local_img_url,
|
||||
);
|
||||
}
|
||||
|
||||
public static function get_maybe_existed_attached_id( $filename, $full_guid = true ) {
|
||||
global $wpdb;
|
||||
if ( $full_guid ) {
|
||||
return $wpdb->get_results( $wpdb->prepare( "SELECT ID,guid FROM $wpdb->posts WHERE post_type = 'attachment' AND guid = %s", $filename ) );
|
||||
}
|
||||
return $wpdb->get_results( $wpdb->prepare( "SELECT ID,guid FROM $wpdb->posts WHERE post_type = 'attachment' AND guid LIKE %s", '%/' . $wpdb->esc_like( $filename ) ) );
|
||||
}
|
||||
|
||||
public static function get_mainwp_dir( $what = null, $dieOnError = true ) {
|
||||
/** @var $wp_filesystem WP_Filesystem_Base */
|
||||
global $wp_filesystem;
|
||||
|
@ -633,7 +504,7 @@ class MainWP_Helper {
|
|||
rmdir( $dir );
|
||||
}
|
||||
|
||||
public static function function_exists( $func ) {
|
||||
public static function funct_exists( $func ) {
|
||||
if ( ! function_exists( $func ) ) {
|
||||
return false;
|
||||
}
|
||||
|
@ -884,62 +755,6 @@ class MainWP_Helper {
|
|||
return $arr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allow to remove method for an hook when, it's a class method used and class don't have variable, but you know the class name :)
|
||||
* Credit to the : wp-filters-extras
|
||||
*/
|
||||
|
||||
public static function remove_filters_for_anonymous_class( $hook_name = '', $class_name = '', $method_name = '', $priority = 0 ) {
|
||||
global $wp_filter;
|
||||
|
||||
// Take only filters on right hook name and priority.
|
||||
if ( ! isset( $wp_filter[ $hook_name ] ) || ! isset( $wp_filter[ $hook_name ][ $priority ] ) || ! is_array( $wp_filter[ $hook_name ][ $priority ] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Loop on filters registered.
|
||||
foreach ( (array) $wp_filter[ $hook_name ][ $priority ] as $unique_id => $filter_array ) {
|
||||
// Test if filter is an array ! (always for class/method).
|
||||
if ( isset( $filter_array['function'] ) && is_array( $filter_array['function'] ) ) {
|
||||
// Test if object is a class, class and method is equal to param !
|
||||
if ( is_object( $filter_array['function'][0] ) && get_class( $filter_array['function'][0] ) && get_class( $filter_array['function'][0] ) === $class_name && $filter_array['function'][1] === $method_name ) {
|
||||
unset( $wp_filter[ $hook_name ][ $priority ][ $unique_id ] );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Credit to the : wp-filters-extras
|
||||
*/
|
||||
|
||||
public static function remove_filters_with_method_name( $hook_name = '', $method_name = '', $priority = 0 ) {
|
||||
|
||||
global $wp_filter;
|
||||
// Take only filters on right hook name and priority.
|
||||
if ( ! isset( $wp_filter[ $hook_name ][ $priority ] ) || ! is_array( $wp_filter[ $hook_name ][ $priority ] ) ) {
|
||||
return false;
|
||||
}
|
||||
// Loop on filters registered.
|
||||
foreach ( (array) $wp_filter[ $hook_name ][ $priority ] as $unique_id => $filter_array ) {
|
||||
// Test if filter is an array ! (always for class/method).
|
||||
if ( isset( $filter_array['function'] ) && is_array( $filter_array['function'] ) ) {
|
||||
// Test if object is a class and method is equal to param !
|
||||
if ( is_object( $filter_array['function'][0] ) && get_class( $filter_array['function'][0] ) && $filter_array['function'][1] == $method_name ) {
|
||||
// Test for WordPress >= 4.7 WP_Hook class.
|
||||
if ( is_a( $wp_filter[ $hook_name ], 'WP_Hook' ) ) {
|
||||
unset( $wp_filter[ $hook_name ]->callbacks[ $priority ][ $unique_id ] );
|
||||
} else {
|
||||
unset( $wp_filter[ $hook_name ][ $priority ][ $unique_id ] );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function sanitize_filename( $filename ) {
|
||||
if ( ! function_exists( 'mb_ereg_replace' ) ) {
|
||||
return sanitize_file_name( $filename );
|
||||
|
@ -1168,28 +983,6 @@ class MainWP_Helper {
|
|||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Handle fatal error for requests from the dashboard
|
||||
* mwp_action requests
|
||||
* wordpress_seo requests
|
||||
* This will do not handle fatal error for sync request from the dashboard
|
||||
*/
|
||||
public static function handle_fatal_error() {
|
||||
|
||||
function handle_shutdown() {
|
||||
// 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::instance()->write( array( 'error' => 'MainWP_Child fatal error : ' . $error['message'] . ' Line: ' . $error['line'] . ' File: ' . $error['file'] ) );
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $_POST['function'] ) && isset( $_POST['mainwpsignature'] ) && ( isset( $_POST['mwp_action'] ) || 'wordpress_seo' == $_POST['function'] ) ) {
|
||||
register_shutdown_function( 'handle_shutdown' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method execute_snippet()
|
||||
*
|
||||
|
@ -1221,4 +1014,14 @@ class MainWP_Helper {
|
|||
error_log( $msg ); // phpcs:ignore -- debug mode only.
|
||||
}
|
||||
}
|
||||
|
||||
public static function set_limit( $timeout, $mem = false ){
|
||||
// phpcs:disable
|
||||
if ( ! empty( $mem ) ) {
|
||||
ini_set( 'memory_limit', $mem );
|
||||
}
|
||||
set_time_limit( $timeout );
|
||||
ini_set( 'max_execution_time', $timeout );
|
||||
// phpcs:enable
|
||||
}
|
||||
}
|
||||
|
|
493
class/class-mainwp-pages.php
Normal file
493
class/class-mainwp-pages.php
Normal file
|
@ -0,0 +1,493 @@
|
|||
<?php
|
||||
|
||||
namespace MainWP\Child;
|
||||
|
||||
class MainWP_Pages {
|
||||
|
||||
protected static $instance = null;
|
||||
|
||||
public static $subPages;
|
||||
public static $subPagesLoaded = false;
|
||||
|
||||
public static $brandingTitle = null;
|
||||
|
||||
/**
|
||||
* 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 init(){
|
||||
add_action( 'admin_menu', array( &$this, 'admin_menu' ) );
|
||||
add_action( 'admin_head', array( &$this, 'admin_head' ) );
|
||||
add_action( 'admin_notices', array( &$this, 'admin_notice' ) );
|
||||
add_filter( 'plugin_row_meta', array( &$this, 'plugin_row_meta' ), 10, 2 );
|
||||
}
|
||||
|
||||
|
||||
public function admin_notice() {
|
||||
// Admin Notice...
|
||||
if ( ! get_option( 'mainwp_child_pubkey' ) && MainWP_Helper::is_admin() && is_admin() ) {
|
||||
$branding_opts = MainWP_Child_Branding::instance()->get_branding_options();
|
||||
$child_name = ( '' === $branding_opts['branding_preserve_title'] ) ? 'MainWP Child' : $branding_opts['branding_preserve_title'];
|
||||
$dashboard_name = ( '' === $branding_opts['branding_preserve_title'] ) ? 'MainWP Dashboard' : $branding_opts['branding_preserve_title'] . ' Dashboard';
|
||||
|
||||
$msg = '<div class="wrap"><div class="postbox" style="margin-top: 4em;"><p style="background: #a00; color: #fff; font-size: 22px; font-weight: bold; margin: 0; padding: .3em;">';
|
||||
$msg .= __( 'Attention!', 'mainwp-child' );
|
||||
$msg .= '</p><div style="padding-left: 1em; padding-right: 1em;"><p style="font-size: 16px;">';
|
||||
$msg .= __( 'Please add this site to your ', 'mainwp-child' ) . $dashboard_name . ' ' . __( '<b>NOW</b> or deactivate the ', 'mainwp-child' ) . $child_name . __( ' plugin until you are ready to connect this site to your Dashboard in order to avoid unexpected security issues.', 'mainwp-child' );
|
||||
$msg .= '</p>';
|
||||
$msg .= '<p style="font-size: 16px;">';
|
||||
$msg .= __( 'If you are not sure how to add this site to your Dashboard, <a href="https://mainwp.com/help/docs/set-up-the-mainwp-plugin/add-site-to-your-dashboard/" target="_blank">please review these instructions</a>.', 'mainwp-child' );
|
||||
$msg .= '</p>';
|
||||
if ( ! MainWP_Child_Branding::instance()->is_branding() ) {
|
||||
$msg .= '<p>';
|
||||
$msg .= __( 'You can also turn on the unique security ID option in <a href="admin.php?page=mainwp_child_tab">', 'mainwp-child' ) . $child_name . __( ' settings</a> if you would like extra security and additional time to add this site to your Dashboard. <br/>Find out more in this help document <a href="https://mainwp.com/help/docs/set-up-the-mainwp-plugin/set-unique-security-id/" target="_blank">How do I use the child unique security ID?</a>', 'mainwp-child' );
|
||||
$msg .= '</p>';
|
||||
}
|
||||
$msg .= '</div></div></div>';
|
||||
echo wp_kses_post( $msg );
|
||||
}
|
||||
MainWP_Child_Server_Information::show_warnings();
|
||||
}
|
||||
|
||||
public function admin_menu() {
|
||||
$branding_opts = MainWP_Child_Branding::instance()->get_branding_options();
|
||||
$is_hide = isset( $branding_opts['hide'] ) ? $branding_opts['hide'] : '';
|
||||
$cancelled_branding = $branding_opts['cancelled_branding'];
|
||||
|
||||
if ( isset( $branding_opts['remove_wp_tools'] ) && $branding_opts['remove_wp_tools'] && ! $cancelled_branding ) {
|
||||
remove_menu_page( 'tools.php' );
|
||||
$pos = stripos( $_SERVER['REQUEST_URI'], 'tools.php' ) || stripos( $_SERVER['REQUEST_URI'], 'import.php' ) || stripos( $_SERVER['REQUEST_URI'], 'export.php' );
|
||||
if ( false !== $pos ) {
|
||||
wp_safe_redirect( get_option( 'siteurl' ) . '/wp-admin/index.php' );
|
||||
}
|
||||
}
|
||||
// if preserve branding and do not remove menus.
|
||||
if ( isset( $branding_opts['remove_wp_setting'] ) && $branding_opts['remove_wp_setting'] && ! $cancelled_branding ) {
|
||||
remove_menu_page( 'options-general.php' );
|
||||
$pos = stripos( $_SERVER['REQUEST_URI'], 'options-general.php' ) || stripos( $_SERVER['REQUEST_URI'], 'options-writing.php' ) || stripos( $_SERVER['REQUEST_URI'], 'options-reading.php' ) || stripos( $_SERVER['REQUEST_URI'], 'options-discussion.php' ) || stripos( $_SERVER['REQUEST_URI'], 'options-media.php' ) || stripos( $_SERVER['REQUEST_URI'], 'options-permalink.php' );
|
||||
if ( false !== $pos ) {
|
||||
wp_safe_redirect( get_option( 'siteurl' ) . '/wp-admin/index.php' );
|
||||
exit();
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $branding_opts['remove_permalink'] ) && $branding_opts['remove_permalink'] && ! $cancelled_branding ) {
|
||||
remove_submenu_page( 'options-general.php', 'options-permalink.php' );
|
||||
$pos = stripos( $_SERVER['REQUEST_URI'], 'options-permalink.php' );
|
||||
if ( false !== $pos ) {
|
||||
wp_safe_redirect( get_option( 'siteurl' ) . '/wp-admin/index.php' );
|
||||
exit();
|
||||
}
|
||||
}
|
||||
|
||||
$remove_all_child_menu = false;
|
||||
if ( isset( $branding_opts['remove_setting'] ) && isset( $branding_opts['remove_restore'] ) && isset( $branding_opts['remove_server_info'] ) && $branding_opts['remove_setting'] && $branding_opts['remove_restore'] && $branding_opts['remove_server_info'] ) {
|
||||
$remove_all_child_menu = true;
|
||||
}
|
||||
|
||||
// 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'] );
|
||||
$child_menu_title = stripslashes( $branding_header['name'] );
|
||||
$child_page_title = $child_menu_title . ' Settings';
|
||||
} else {
|
||||
$child_menu_title = 'MainWP Child';
|
||||
$child_page_title = 'MainWPSettings';
|
||||
}
|
||||
|
||||
$settingsPage = add_submenu_page( 'options-general.php', $child_menu_title, $child_menu_title, 'manage_options', 'mainwp_child_tab', array( &$this, 'render_pages' ) );
|
||||
|
||||
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,
|
||||
);
|
||||
|
||||
do_action_deprecated( 'mainwp-child-subpages', array( $subpageargs ), '4.0.7.1', 'mainwp_child_subpages' );
|
||||
do_action( 'mainwp_child_subpages', $subpageargs );
|
||||
|
||||
$sub_pages = 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 ( ! 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;
|
||||
}
|
||||
$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'], '<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;
|
||||
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 ) {
|
||||
return $plugin_meta;
|
||||
}
|
||||
return apply_filters( 'mainwp_child_plugin_row_meta', $plugin_meta, $plugin_file, $mainWPChild->plugin_slug );
|
||||
}
|
||||
|
||||
public function render_pages( $shownPage ) {
|
||||
$shownPage = '';
|
||||
if ( isset( $_GET['tab'] ) ) {
|
||||
$shownPage = $_GET['tab'];
|
||||
}
|
||||
$branding_opts = MainWP_Child_Branding::instance()->get_branding_options();
|
||||
|
||||
$hide_settings = isset( $branding_opts['remove_setting'] ) && $branding_opts['remove_setting'] ? true : false;
|
||||
$hide_restore = isset( $branding_opts['remove_restore'] ) && $branding_opts['remove_restore'] ? true : false;
|
||||
$hide_server_info = isset( $branding_opts['remove_server_info'] ) && $branding_opts['remove_server_info'] ? true : false;
|
||||
$hide_connection_detail = isset( $branding_opts['remove_connection_detail'] ) && $branding_opts['remove_connection_detail'] ? true : false;
|
||||
|
||||
$hide_style = 'style="display:none"';
|
||||
|
||||
if ( '' == $shownPage ) {
|
||||
if ( ! $hide_settings ) {
|
||||
$shownPage = 'settings';
|
||||
} elseif ( ! $hide_restore ) {
|
||||
$shownPage = 'restore-clone';
|
||||
} elseif ( ! $hide_server_info ) {
|
||||
$shownPage = 'server-info';
|
||||
} elseif ( ! $hide_connection_detail ) {
|
||||
$shownPage = 'connection-detail';
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! $hide_restore ) {
|
||||
if ( '' === session_id() ) {
|
||||
session_start();
|
||||
}
|
||||
}
|
||||
|
||||
self::render_header( $shownPage, false );
|
||||
?>
|
||||
<?php if ( ! $hide_settings ) { ?>
|
||||
<div class="mainwp-child-setting-tab settings" <?php echo ( 'settings' !== $shownPage ) ? $hide_style : ''; ?>>
|
||||
<?php $this->render_settings(); ?>
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
<?php if ( ! $hide_restore ) { ?>
|
||||
<div class="mainwp-child-setting-tab restore-clone" <?php echo ( 'restore-clone' !== $shownPage ) ? $hide_style : ''; ?>>
|
||||
<?php
|
||||
if ( isset( $_SESSION['file'] ) ) {
|
||||
MainWP_Clone::render_restore();
|
||||
} else {
|
||||
$sitesToClone = get_option( 'mainwp_child_clone_sites' );
|
||||
if ( 0 !== (int) $sitesToClone ) {
|
||||
MainWP_Clone::render();
|
||||
} else {
|
||||
MainWP_Clone::render_normal_restore();
|
||||
}
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
<?php if ( ! $hide_server_info ) { ?>
|
||||
<div class="mainwp-child-setting-tab server-info" <?php echo ( 'server-info' !== $shownPage ) ? $hide_style : ''; ?>>
|
||||
<?php MainWP_Child_Server_Information::render_page(); ?>
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
<?php if ( ! $hide_connection_detail ) { ?>
|
||||
<div class="mainwp-child-setting-tab connection-detail" <?php echo ( 'connection-detail' !== $shownPage ) ? $hide_style : ''; ?>>
|
||||
<?php MainWP_Child_Server_Information::render_connection_details(); ?>
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
|
||||
|
||||
<?php
|
||||
self::render_footer();
|
||||
}
|
||||
|
||||
public static function render_header( $shownPage, $subpage = true ) {
|
||||
if ( isset( $_GET['tab'] ) ) {
|
||||
$shownPage = $_GET['tab'];
|
||||
}
|
||||
|
||||
if ( empty( $shownPage ) ) {
|
||||
$shownPage = 'settings';
|
||||
}
|
||||
|
||||
$branding_opts = MainWP_Child_Branding::instance()->get_branding_options();
|
||||
|
||||
$hide_settings = isset( $branding_opts['remove_setting'] ) && $branding_opts['remove_setting'] ? true : false;
|
||||
$hide_restore = isset( $branding_opts['remove_restore'] ) && $branding_opts['remove_restore'] ? true : false;
|
||||
$hide_server_info = isset( $branding_opts['remove_server_info'] ) && $branding_opts['remove_server_info'] ? true : false;
|
||||
$hide_connection_detail = isset( $branding_opts['remove_connection_detail'] ) && $branding_opts['remove_connection_detail'] ? true : false;
|
||||
|
||||
$sitesToClone = get_option( 'mainwp_child_clone_sites' );
|
||||
|
||||
?>
|
||||
<style type="text/css">
|
||||
.mainwp-tabs
|
||||
{
|
||||
margin-top: 2em;
|
||||
border-bottom: 1px solid #e5e5e5;
|
||||
}
|
||||
|
||||
#mainwp-tabs {
|
||||
clear: both ;
|
||||
}
|
||||
#mainwp-tabs .nav-tab-active {
|
||||
background: #fafafa ;
|
||||
border-top: 1px solid #7fb100 !important;
|
||||
border-left: 1px solid #e5e5e5;
|
||||
border-right: 1px solid #e5e5e5;
|
||||
border-bottom: 1px solid #fafafa !important ;
|
||||
color: #7fb100;
|
||||
}
|
||||
|
||||
#mainwp-tabs .nav-tab {
|
||||
border-top: 1px solid #e5e5e5;
|
||||
border-left: 1px solid #e5e5e5;
|
||||
border-right: 1px solid #e5e5e5;
|
||||
border-bottom: 1px solid #e5e5e5;
|
||||
padding: 10px 16px;
|
||||
font-size: 14px;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
#mainwp_wrap-inside {
|
||||
min-height: 80vh;
|
||||
height: 100% ;
|
||||
margin-top: 0em ;
|
||||
padding: 10px ;
|
||||
background: #fafafa ;
|
||||
border-top: none ;
|
||||
border-bottom: 1px solid #e5e5e5;
|
||||
border-left: 1px solid #e5e5e5;
|
||||
border-right: 1px solid #e5e5e5;
|
||||
box-shadow: 0 1px 1px rgba(0,0,0,.04);
|
||||
position: relative;
|
||||
}
|
||||
|
||||
#mainwp_wrap-inside h2.hndle {
|
||||
font-size: 14px;
|
||||
padding: 8px 12px;
|
||||
margin: 0;
|
||||
line-height: 1.4;
|
||||
}
|
||||
|
||||
.mainwp-hidden {
|
||||
display: none;
|
||||
}
|
||||
</style>
|
||||
|
||||
<div class="wrap">
|
||||
<h2><i class="fa fa-file"></i> <?php echo ( null === self::$brandingTitle ? 'MainWP Child' : self::$brandingTitle ); ?></h2>
|
||||
<div style="clear: both;"></div><br/>
|
||||
<div class="mainwp-tabs" id="mainwp-tabs">
|
||||
<?php if ( ! $hide_settings ) { ?>
|
||||
<a class="nav-tab pos-nav-tab
|
||||
<?php
|
||||
if ( 'settings' === $shownPage ) {
|
||||
echo 'nav-tab-active'; }
|
||||
?>
|
||||
" tab-slug="settings" href="<?php echo $subpage ? 'options-general.php?page=mainwp_child_tab&tab=settings' : '#'; ?>" style="margin-left: 0 !important;"><?php _e( 'Settings', 'mainwp-child' ); ?></a>
|
||||
<?php } ?>
|
||||
<?php if ( ! $hide_restore ) { ?>
|
||||
<a class="nav-tab pos-nav-tab
|
||||
<?php
|
||||
if ( 'restore-clone' === $shownPage ) {
|
||||
echo 'nav-tab-active'; }
|
||||
?>
|
||||
" tab-slug="restore-clone" href="<?php echo $subpage ? 'options-general.php?page=mainwp_child_tab&tab=restore-clone' : '#'; ?>"><?php echo ( 0 !== (int) $sitesToClone ) ? __( 'Restore / Clone', 'mainwp-child' ) : __( 'Restore', 'mainwp-child' ); ?></a>
|
||||
<?php } ?>
|
||||
<?php if ( ! $hide_server_info ) { ?>
|
||||
<a class="nav-tab pos-nav-tab
|
||||
<?php
|
||||
if ( 'server-info' === $shownPage ) {
|
||||
echo 'nav-tab-active'; }
|
||||
?>
|
||||
" tab-slug="server-info" href="<?php echo $subpage ? 'options-general.php?page=mainwp_child_tab&tab=server-info' : '#'; ?>"><?php _e( 'Server information', 'mainwp-child' ); ?></a>
|
||||
<?php } ?>
|
||||
<?php if ( ! $hide_connection_detail ) { ?>
|
||||
<a class="nav-tab pos-nav-tab
|
||||
<?php
|
||||
if ( 'connection-detail' === $shownPage ) {
|
||||
echo 'nav-tab-active'; }
|
||||
?>
|
||||
" tab-slug="connection-detail" href="<?php echo $subpage ? 'options-general.php?page=mainwp_child_tab&tab=connection-detail' : '#'; ?>"><?php _e( 'Connection Details', 'mainwp-child' ); ?></a>
|
||||
<?php } ?>
|
||||
<?php
|
||||
if ( isset( self::$subPages ) && is_array( self::$subPages ) ) {
|
||||
foreach ( self::$subPages as $subPage ) {
|
||||
?>
|
||||
<a class="nav-tab pos-nav-tab
|
||||
<?php
|
||||
if ( $shownPage == $subPage['slug'] ) {
|
||||
echo 'nav-tab-active'; }
|
||||
?>
|
||||
" tab-slug="<?php echo esc_attr( $subPage['slug'] ); ?>" href="options-general.php?page=<?php echo rawurlencode( $subPage['page'] ); ?>"><?php echo esc_html( $subPage['title'] ); ?></a>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
?>
|
||||
<div style="clear:both;"></div>
|
||||
</div>
|
||||
<div style="clear:both;"></div>
|
||||
<script type="text/javascript">
|
||||
jQuery( document ).ready( function () {
|
||||
$hideMenu = jQuery( '#menu-settings li a .mainwp-hidden' );
|
||||
$hideMenu.each( function() {
|
||||
jQuery( this ).closest( 'li' ).hide();
|
||||
} );
|
||||
|
||||
var $tabs = jQuery( '.mainwp-tabs' );
|
||||
|
||||
$tabs.on( 'click', 'a', function () {
|
||||
if ( jQuery( this ).attr( 'href' ) !=='#' )
|
||||
return true;
|
||||
|
||||
jQuery( '.mainwp-tabs > a' ).removeClass( 'nav-tab-active' );
|
||||
jQuery( this ).addClass( 'nav-tab-active' );
|
||||
jQuery( '.mainwp-child-setting-tab' ).hide();
|
||||
var _tab = jQuery( this ).attr( 'tab-slug' );
|
||||
jQuery( '.mainwp-child-setting-tab.' + _tab ).show();
|
||||
return false;
|
||||
} );
|
||||
} );
|
||||
</script>
|
||||
|
||||
<div id="mainwp_wrap-inside">
|
||||
|
||||
<?php
|
||||
}
|
||||
|
||||
public static function render_footer() {
|
||||
?>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
|
||||
public function admin_head() {
|
||||
if ( isset( $_GET['page'] ) && 'mainwp_child_tab' == $_GET['page'] ) {
|
||||
?>
|
||||
<style type="text/css">
|
||||
.mainwp-postbox-actions-top {
|
||||
padding: 10px;
|
||||
clear: both;
|
||||
border-bottom: 1px solid #ddd;
|
||||
background: #f5f5f5;
|
||||
}
|
||||
h3.mainwp_box_title {
|
||||
font-family: "Open Sans",sans-serif;
|
||||
font-size: 14px;
|
||||
font-weight: 600;
|
||||
line-height: 1.4;
|
||||
margin: 0;
|
||||
padding: 8px 12px;
|
||||
border-bottom: 1px solid #eee;
|
||||
}
|
||||
.mainwp-child-setting-tab.connection-detail .postbox .inside{
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
</style>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
public function render_settings() {
|
||||
if ( isset( $_POST['submit'] ) && isset( $_POST['nonce'] ) && wp_verify_nonce( $_POST['nonce'], 'child-settings' ) ) {
|
||||
if ( isset( $_POST['requireUniqueSecurityId'] ) ) {
|
||||
MainWP_Helper::update_option( 'mainwp_child_uniqueId', MainWP_Helper::rand_string( 8 ) );
|
||||
} else {
|
||||
MainWP_Helper::update_option( 'mainwp_child_uniqueId', '' );
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
<div class="postbox">
|
||||
<h2 class="hndle"><span><?php esc_html_e( 'Connection settings', 'mainwp-child' ); ?></span></h2>
|
||||
<div class="inside">
|
||||
<form method="post" action="options-general.php?page=mainwp_child_tab">
|
||||
<div class="howto"><?php esc_html_e( 'The unique security ID adds additional protection between the child plugin and your Dashboard. The unique security ID will need to match when being added to the Dashboard. This is additional security and should not be needed in most situations.', 'mainwp-child' ); ?></div>
|
||||
<div style="margin: 1em 0 4em 0;">
|
||||
<input name="requireUniqueSecurityId" type="checkbox" id="requireUniqueSecurityId"
|
||||
<?php
|
||||
if ( '' != get_option( 'mainwp_child_uniqueId' ) ) {
|
||||
echo 'checked'; }
|
||||
?>
|
||||
/>
|
||||
<label for="requireUniqueSecurityId" style="font-size: 15px;"><?php esc_html_e( 'Require unique security ID', 'mainwp-child' ); ?></label>
|
||||
</div>
|
||||
<div>
|
||||
<?php
|
||||
if ( '' != get_option( 'mainwp_child_uniqueId' ) ) {
|
||||
echo '<span style="border: 1px dashed #e5e5e5; background: #fafafa; font-size: 24px; padding: 1em 2em;">' . esc_html__( 'Your unique security ID is:', 'mainwp-child' ) . ' <span style="font-weight: bold; color: #7fb100;">' . esc_html( get_option( 'mainwp_child_uniqueId' ) ) . '</span></span>';
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
<p class="submit" style="margin-top: 4em;">
|
||||
<input type="submit" name="submit" id="submit" class="button button-primary button-hero" value="<?php esc_attr_e( 'Save changes', 'mainwp-child' ); ?>">
|
||||
</p>
|
||||
<input type="hidden" name="nonce" value="<?php echo wp_create_nonce( 'child-settings' ); ?>">
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<?php
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -142,6 +142,28 @@ class MainWP_Utility {
|
|||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Handle fatal error for requests from the dashboard
|
||||
* mwp_action requests
|
||||
* wordpress_seo requests
|
||||
* This will do not handle fatal error for sync request from the dashboard
|
||||
*/
|
||||
public static function handle_fatal_error() {
|
||||
|
||||
function handle_shutdown() {
|
||||
// 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'] ) ) {
|
||||
mainwp_child_helper()->write( array( 'error' => 'MainWP_Child fatal error : ' . $error['message'] . ' Line: ' . $error['line'] . ' File: ' . $error['file'] ) );
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $_POST['function'] ) && isset( $_POST['mainwpsignature'] ) && ( isset( $_POST['mwp_action'] ) || 'wordpress_seo' == $_POST['function'] ) ) {
|
||||
register_shutdown_function( 'handle_shutdown' );
|
||||
}
|
||||
}
|
||||
|
||||
public function cron_active() {
|
||||
if ( ! defined( 'DOING_CRON' ) || ! DOING_CRON ) {
|
||||
return;
|
||||
|
@ -207,5 +229,134 @@ class MainWP_Utility {
|
|||
|
||||
return fclose( $handle );
|
||||
}
|
||||
|
||||
// $check_file_existed: to support checking if file existed.
|
||||
// $parent_id: optional.
|
||||
public static function upload_image( $img_url, $img_data = array(), $check_file_existed = false, $parent_id = 0 ) {
|
||||
if ( ! is_array( $img_data ) ) {
|
||||
$img_data = array();
|
||||
}
|
||||
|
||||
/** @var $wp_filesystem WP_Filesystem_Base */
|
||||
global $wp_filesystem;
|
||||
MainWP_Helper::get_wp_filesystem();
|
||||
|
||||
include_once ABSPATH . 'wp-admin/includes/file.php';
|
||||
$upload_dir = wp_upload_dir();
|
||||
add_filter( 'http_request_args', array( MainWP_Helper::get_class_name(), 'reject_unsafe_urls' ), 99, 2 );
|
||||
$temporary_file = download_url( $img_url );
|
||||
remove_filter( 'http_request_args', array( MainWP_Helper::get_class_name(), 'reject_unsafe_urls' ), 99, 2 );
|
||||
|
||||
if ( is_wp_error( $temporary_file ) ) {
|
||||
throw new \Exception( 'Error: ' . $temporary_file->get_error_message() );
|
||||
} else {
|
||||
$filename = basename( $img_url );
|
||||
$local_img_path = $upload_dir['path'] . DIRECTORY_SEPARATOR . $filename;
|
||||
$local_img_url = $upload_dir['url'] . '/' . basename( $local_img_path );
|
||||
|
||||
// to fix issue re-create new attachment.
|
||||
if ( $check_file_existed ) {
|
||||
$result = self::check_media_file_existed( $upload_dir, $filename, $temporary_file, $local_img_path, $local_img_url );
|
||||
if ( ! empty( $result ) ) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
// file exists, do not overwrite, generate unique file name.
|
||||
// this may causing of issue incorrect source of image in post content.
|
||||
if ( $wp_filesystem->exists( $local_img_path ) ) {
|
||||
$local_img_path = dirname( $local_img_path ) . '/' . wp_unique_filename( dirname( $local_img_path ), basename( $local_img_path ) );
|
||||
$local_img_url = $upload_dir['url'] . '/' . basename( $local_img_path );
|
||||
}
|
||||
|
||||
$moved = $wp_filesystem->move( $temporary_file, $local_img_path );
|
||||
if ( $moved ) {
|
||||
return self::insert_attachment_media( $img_data, $img_url, $parent_id, $local_img_path, $local_img_url );
|
||||
}
|
||||
}
|
||||
|
||||
if ( $wp_filesystem->exists( $temporary_file ) ) {
|
||||
$wp_filesystem->delete( $temporary_file );
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static function check_media_file_existed( $upload_dir, $filename, $temporary_file, &$local_img_path, $local_img_url ) {
|
||||
global $wp_filesystem;
|
||||
if ( $wp_filesystem->exists( $local_img_path ) ) {
|
||||
if ( filesize( $local_img_path ) == filesize( $temporary_file ) ) {
|
||||
$result = self::get_maybe_existed_attached_id( $local_img_url );
|
||||
if ( is_array( $result ) ) {
|
||||
$attach = current( $result );
|
||||
if ( is_object( $attach ) ) {
|
||||
if ( $wp_filesystem->exists( $temporary_file ) ) {
|
||||
$wp_filesystem->delete( $temporary_file );
|
||||
}
|
||||
return array(
|
||||
'id' => $attach->ID,
|
||||
'url' => $local_img_url,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$result = self::get_maybe_existed_attached_id( $filename, false );
|
||||
if ( is_array( $result ) ) {
|
||||
$attach = current( $result );
|
||||
if ( is_object( $attach ) ) {
|
||||
$basedir = $upload_dir['basedir'];
|
||||
$baseurl = $upload_dir['baseurl'];
|
||||
$local_img_path = str_replace( $baseurl, $basedir, $attach->guid );
|
||||
if ( $wp_filesystem->exists( $local_img_path ) && ( $wp_filesystem->size( $local_img_path ) == $wp_filesystem->size( $temporary_file ) ) ) {
|
||||
if ( $wp_filesystem->exists( $temporary_file ) ) {
|
||||
$wp_filesystem->delete( $temporary_file );
|
||||
}
|
||||
return array(
|
||||
'id' => $attach->ID,
|
||||
'url' => $attach->guid,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static function insert_attachment_media( $img_data, $img_url, $parent_id, $local_img_path, $local_img_url ) {
|
||||
|
||||
$wp_filetype = wp_check_filetype( basename( $img_url ), null ); // Get the filetype to set the mimetype.
|
||||
$attachment = array(
|
||||
'post_mime_type' => $wp_filetype['type'],
|
||||
'post_title' => isset( $img_data['title'] ) && ! empty( $img_data['title'] ) ? $img_data['title'] : preg_replace( '/\.[^.]+$/', '', basename( $img_url ) ),
|
||||
'post_content' => isset( $img_data['description'] ) && ! empty( $img_data['description'] ) ? $img_data['description'] : '',
|
||||
'post_excerpt' => isset( $img_data['caption'] ) && ! empty( $img_data['caption'] ) ? $img_data['caption'] : '',
|
||||
'post_status' => 'inherit',
|
||||
'guid' => $local_img_url,
|
||||
);
|
||||
|
||||
// for post attachments, thumbnail.
|
||||
if ( $parent_id ) {
|
||||
$attachment['post_parent'] = $parent_id;
|
||||
}
|
||||
|
||||
$attach_id = wp_insert_attachment( $attachment, $local_img_path ); // Insert the image in the database.
|
||||
require_once ABSPATH . 'wp-admin/includes/image.php';
|
||||
$attach_data = wp_generate_attachment_metadata( $attach_id, $local_img_path );
|
||||
wp_update_attachment_metadata( $attach_id, $attach_data ); // Update generated metadata.
|
||||
if ( isset( $img_data['alt'] ) && ! empty( $img_data['alt'] ) ) {
|
||||
update_post_meta( $attach_id, '_wp_attachment_image_alt', $img_data['alt'] );
|
||||
}
|
||||
return array(
|
||||
'id' => $attach_id,
|
||||
'url' => $local_img_url,
|
||||
);
|
||||
}
|
||||
|
||||
public static function get_maybe_existed_attached_id( $filename, $full_guid = true ) {
|
||||
global $wpdb;
|
||||
if ( $full_guid ) {
|
||||
return $wpdb->get_results( $wpdb->prepare( "SELECT ID,guid FROM $wpdb->posts WHERE post_type = 'attachment' AND guid = %s", $filename ) );
|
||||
}
|
||||
return $wpdb->get_results( $wpdb->prepare( "SELECT ID,guid FROM $wpdb->posts WHERE post_type = 'attachment' AND guid LIKE %s", '%/' . $wpdb->esc_like( $filename ) ) );
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue