Refactoring

This commit is contained in:
thanghv 2020-05-13 01:18:02 +07:00
parent 08bc34414f
commit 89e6525a49
13 changed files with 1427 additions and 1248 deletions

View file

@ -307,7 +307,7 @@ class MainWP_Backup {
closedir( $fh );
// phpcs:enable
if ( defined( 'MAINWP_DEBUG' ) && MAINWP_DEBUG ) {
if ( defined( 'MAINWP_CHILD_DEBUG' ) && MAINWP_CHILD_DEBUG ) {
$string = wp_json_encode(
array(
'siteurl' => get_option( 'siteurl' ),
@ -447,7 +447,7 @@ class MainWP_Backup {
if ( $addConfig ) {
global $wpdb;
if ( defined( 'MAINWP_DEBUG' ) && MAINWP_DEBUG ) {
if ( defined( 'MAINWP_CHILD_DEBUG' ) && MAINWP_CHILD_DEBUG ) {
$string = wp_json_encode(
array(
'siteurl' => get_option( 'siteurl' ),

View file

@ -264,16 +264,12 @@ class MainWP_Child_Posts {
$post_featured_image = base64_decode( $_POST['post_featured_image'] ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons.
$upload_dir = maybe_unserialize( base64_decode( $_POST['mainwp_upload_dir'] ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons.
if ( isset( $_POST['_ezin_post_category'] ) ) {
$new_post['_ezin_post_category'] = maybe_unserialize( base64_decode( $_POST['_ezin_post_category'] ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons.
}
$others = array();
if ( isset( $_POST['featured_image_data'] ) && ! empty( $_POST['featured_image_data'] ) ) {
$others['featured_image_data'] = unserialize( base64_decode( $_POST['featured_image_data'] ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons.
}
$res = MainWP_Helper::create_post( $new_post, $post_custom, $post_category, $post_featured_image, $upload_dir, $post_tags, $others );
$res = $this->create_post( $new_post, $post_custom, $post_category, $post_featured_image, $upload_dir, $post_tags, $others );
if ( is_array( $res ) && isset( $res['error'] ) ) {
MainWP_Helper::error( $res['error'] );
@ -659,4 +655,539 @@ class MainWP_Child_Posts {
return $allComments;
}
private function create_post( $new_post, $post_custom, $post_category, $post_featured_image, $upload_dir, $post_tags, $others = array() ) {
global $current_user;
/**
* Hook: `mainwp_before_post_update`
*
* Runs before creating or updating a post via MainWP dashboard.
*
* @param array $new_post Post data array.
* @param array $post_custom Post custom meta data.
* @param string $post_category Post categories.
* @param string $post_tags Post tags.
*/
do_action( 'mainwp_before_post_update', $new_post, $post_custom, $post_category, $post_tags );
$this->create_wp_rocket( $post_custom );
// current user may be connected admin or alternative admin.
$current_uid = $current_user->ID;
// Set up a new post (adding addition information).
$post_author = isset( $new_post['post_author'] ) ? $new_post['post_author'] : $current_uid;
if ( isset( $new_post['custom_post_author'] ) && ! empty( $new_post['custom_post_author'] ) ) {
$_author = get_user_by( 'login', $new_post['custom_post_author'] );
if ( ! empty( $_author ) ) {
$new_post['post_author'] = $_author->ID;
} else {
$new_post['post_author'] = $current_uid;
}
unset( $new_post['custom_post_author'] );
}
$post_author = ! empty( $post_author ) ? $post_author : $current_uid;
$new_post['post_author'] = $post_author;
unset( $new_post['_ezin_post_category'] );
// post plus extension process.
$is_post_plus = isset( $post_custom['_mainwp_post_plus'] ) ? true : false;
$wp_error = null;
if ( $is_post_plus ) {
if ( isset( $new_post['post_date_gmt'] ) && ! empty( $new_post['post_date_gmt'] ) && '0000-00-00 00:00:00' != $new_post['post_date_gmt'] ) {
$post_date_timestamp = strtotime( $new_post['post_date_gmt'] ) + get_option( 'gmt_offset' ) * 60 * 60;
$new_post['post_date'] = date( 'Y-m-d H:i:s', $post_date_timestamp ); // phpcs:ignore -- local time.
}
}
$edit_post_id = 0;
if ( isset( $post_custom['_mainwp_edit_post_id'] ) && $post_custom['_mainwp_edit_post_id'] ) {
$edit_post_id = current( $post_custom['_mainwp_edit_post_id'] );
} elseif ( isset( $new_post['ID'] ) && $new_post['ID'] ) {
$edit_post_id = $new_post['ID'];
}
require_once ABSPATH . 'wp-admin/includes/post.php';
if ( $edit_post_id ) {
$user_id = wp_check_post_lock( $edit_post_id );
if ( $user_id ) {
$user = get_userdata( $user_id );
$error = sprintf( __( 'This content is currently locked. %s is currently editing.', 'mainwp-child' ), $user->display_name );
return array( 'error' => $error );
}
}
$check_image_existed = false;
if ( $edit_post_id ) {
$check_image_existed = true; // if editing post then will check if image existed.
}
$this->create_found_images( $new_post, $upload_dir, $check_image_existed );
$this->create_has_shortcode_gallery( $new_post );
if ( $is_post_plus ) {
$this->create_post_plus( $new_post, $post_custom );
}
if ( isset( $post_tags ) && '' !== $post_tags ) {
$new_post['tags_input'] = $post_tags;
}
// Save the post to the WP.
remove_filter( 'content_save_pre', 'wp_filter_post_kses' ); // to fix brake scripts or html.
$post_status = $new_post['post_status'];
$new_post['post_status'] = 'auto-draft'; // child reports: to logging as created post.
// update post.
if ( $edit_post_id ) {
// check if post existed.
$current_post = get_post( $edit_post_id );
if ( $current_post && ( ( ! isset( $new_post['post_type'] ) && 'post' == $current_post->post_type ) || ( isset( $new_post['post_type'] ) && $new_post['post_type'] == $current_post->post_type ) ) ) {
$new_post['ID'] = $edit_post_id;
}
$new_post['post_status'] = $post_status; // child reports: to logging as update post.
}
$new_post_id = wp_insert_post( $new_post, $wp_error );
// Show errors if something went wrong.
if ( is_wp_error( $wp_error ) ) {
return $wp_error->get_error_message();
}
if ( empty( $new_post_id ) ) {
return array( 'error' => 'Empty post id' );
}
if ( ! $edit_post_id ) {
wp_update_post(
array(
'ID' => $new_post_id,
'post_status' => $post_status,
)
);
}
$permalink = get_permalink( $new_post_id );
$seo_ext_activated = false;
if ( class_exists( 'WPSEO_Meta' ) && class_exists( 'WPSEO_Admin' ) ) {
$seo_ext_activated = true;
}
$post_to_only_existing_categories = false;
$this->create_set_custom_fields( $new_post_id, $post_custom, $seo_ext_activated, $post_to_only_existing_categories );
// yoast seo plugin activated.
if ( $seo_ext_activated ) {
$this->create_seo_extension_activated( $new_post_id, $post_custom );
}
$this->create_set_categories( $new_post_id, $post_category, $post_to_only_existing_categories );
$this->create_featured_image( $new_post_id, $post_featured_image, $check_image_existed );
// post plus extension process.
if ( $is_post_plus ) {
$this->create_post_plus_categories( $new_post_id, $post_custom );
}
// to support custom post author.
$custom_post_author = apply_filters( 'mainwp_create_post_custom_author', false, $new_post_id );
if ( ! empty( $custom_post_author ) ) {
wp_update_post(
array(
'ID' => $new_post_id,
'post_author' => $custom_post_author,
)
);
}
// unlock if edit post.
if ( $edit_post_id ) {
update_post_meta( $edit_post_id, '_edit_lock', '' );
}
$ret['success'] = true;
$ret['link'] = $permalink;
$ret['added_id'] = $new_post_id;
return $ret;
}
private function create_wp_rocket( &$post_custom ) {
// Options fields.
$wprocket_fields = array(
'lazyload',
'lazyload_iframes',
'minify_html',
'minify_css',
'minify_js',
'cdn',
'async_css',
'defer_all_js',
);
$wprocket_activated = false;
if ( \MainWP_Child_WP_Rocket::instance()->is_activated() ) {
if ( function_exists( 'get_rocket_option' ) ) {
$wprocket_activated = true;
foreach ( $wprocket_fields as $field ) {
if ( ! isset( $post_custom[ '_rocket_exclude_' . $field ] ) ) {
if ( ! get_rocket_option( $field ) ) {
$post_custom[ '_rocket_exclude_' . $field ] = array( true );
}
}
}
}
}
if ( ! $wprocket_activated ) {
foreach ( $wprocket_fields as $field ) {
if ( isset( $post_custom[ '_rocket_exclude_' . $field ] ) ) {
unset( $post_custom[ '_rocket_exclude_' . $field ] );
}
}
}
}
private function create_found_images( &$new_post, $upload_dir, $check_image_existed ) {
// Search for all the images added to the new post. Some images have a href tag to click to navigate to the image.. we need to replace this too.
$foundMatches = preg_match_all( '/(<a[^>]+href=\"(.*?)\"[^>]*>)?(<img[^>\/]*src=\"((.*?)(png|gif|jpg|jpeg))\")/ix', $new_post['post_content'], $matches, PREG_SET_ORDER );
if ( $foundMatches > 0 ) {
// We found images, now to download them so we can start balbal.
foreach ( $matches as $match ) {
$hrefLink = $match[2];
$imgUrl = $match[4];
if ( ! isset( $upload_dir['baseurl'] ) || ( 0 !== strripos( $imgUrl, $upload_dir['baseurl'] ) ) ) {
continue;
}
if ( preg_match( '/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', $imgUrl, $imgMatches ) ) {
$search = $imgMatches[0];
$replace = '.' . $match[6];
$originalImgUrl = str_replace( $search, $replace, $imgUrl );
} else {
$originalImgUrl = $imgUrl;
}
try {
$downloadfile = MainWP_Helper::upload_image( $originalImgUrl, array(), $check_image_existed );
$localUrl = $downloadfile['url'];
$linkToReplaceWith = dirname( $localUrl );
if ( '' !== $hrefLink ) {
$server = get_option( 'mainwp_child_server' );
$serverHost = wp_parse_url( $server, PHP_URL_HOST );
if ( ! empty( $serverHost ) && strpos( $hrefLink, $serverHost ) !== false ) {
$serverHref = 'href="' . $serverHost;
$replaceServerHref = 'href="' . wp_parse_url( $localUrl, PHP_URL_SCHEME ) . '://' . wp_parse_url( $localUrl, PHP_URL_HOST );
$new_post['post_content'] = str_replace( $serverHref, $replaceServerHref, $new_post['post_content'] );
}
}
$lnkToReplace = dirname( $imgUrl );
if ( 'http:' !== $lnkToReplace && 'https:' !== $lnkToReplace ) {
$new_post['post_content'] = str_replace( $lnkToReplace, $linkToReplaceWith, $new_post['post_content'] );
}
} catch ( \Exception $e ) {
MainWP_Helper::log_debug( $e->getMessage() );
}
}
}
}
private function create_has_shortcode_gallery( &$new_post ) {
if ( has_shortcode( $new_post['post_content'], 'gallery' ) ) {
if ( preg_match_all( '/\[gallery[^\]]+ids=\"(.*?)\"[^\]]*\]/ix', $new_post['post_content'], $matches, PREG_SET_ORDER ) ) {
$replaceAttachedIds = array();
if ( isset( $_POST['post_gallery_images'] ) ) {
$post_gallery_images = unserialize( base64_decode( $_POST['post_gallery_images'] ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons.
if ( is_array( $post_gallery_images ) ) {
foreach ( $post_gallery_images as $gallery ) {
if ( isset( $gallery['src'] ) ) {
try {
$upload = MainWP_Helper::upload_image( $gallery['src'], $gallery ); // Upload image to WP.
if ( null !== $upload ) {
$replaceAttachedIds[ $gallery['id'] ] = $upload['id'];
}
} catch ( \Exception $e ) {
// ok!
}
}
}
}
}
if ( count( $replaceAttachedIds ) > 0 ) {
foreach ( $matches as $match ) {
$idsToReplace = $match[1];
$idsToReplaceWith = '';
$originalIds = explode( ',', $idsToReplace );
foreach ( $originalIds as $attached_id ) {
if ( ! empty( $originalIds ) && isset( $replaceAttachedIds[ $attached_id ] ) ) {
$idsToReplaceWith .= $replaceAttachedIds[ $attached_id ] . ',';
}
}
$idsToReplaceWith = rtrim( $idsToReplaceWith, ',' );
if ( ! empty( $idsToReplaceWith ) ) {
$new_post['post_content'] = str_replace( '"' . $idsToReplace . '"', '"' . $idsToReplaceWith . '"', $new_post['post_content'] );
}
}
}
}
}
}
private function create_post_plus( &$new_post, $post_custom ) {
$random_publish_date = isset( $post_custom['_saved_draft_random_publish_date'] ) ? $post_custom['_saved_draft_random_publish_date'] : false;
$random_publish_date = is_array( $random_publish_date ) ? current( $random_publish_date ) : null;
if ( ! empty( $random_publish_date ) ) {
$random_date_from = isset( $post_custom['_saved_draft_publish_date_from'] ) ? $post_custom['_saved_draft_publish_date_from'] : 0;
$random_date_from = is_array( $random_date_from ) ? current( $random_date_from ) : 0;
$random_date_to = isset( $post_custom['_saved_draft_publish_date_to'] ) ? $post_custom['_saved_draft_publish_date_to'] : 0;
$random_date_to = is_array( $random_date_to ) ? current( $random_date_to ) : 0;
$now = time();
if ( empty( $random_date_from ) ) {
$random_date_from = $now;
}
if ( empty( $random_date_to ) ) {
$random_date_to = $now;
}
if ( $random_date_from === $now && $random_date_from === $random_date_to ) {
$random_date_to = $now + 7 * 24 * 3600;
}
if ( $random_date_from > $random_date_to ) {
$tmp = $random_date_from;
$random_date_from = $random_date_to;
$random_date_to = $tmp;
}
$random_timestamp = wp_rand( $random_date_from, $random_date_to );
$new_post['post_date'] = date( 'Y-m-d H:i:s', $random_timestamp ); // phpcs:ignore -- local time.
}
}
private function create_post_plus_categories( $new_post_id, $post_custom ) {
$random_privelege = isset( $post_custom['_saved_draft_random_privelege'] ) ? $post_custom['_saved_draft_random_privelege'] : null;
$random_privelege = is_array( $random_privelege ) ? current( $random_privelege ) : null;
$random_privelege_base = base64_decode( $random_privelege ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons.
$random_privelege = maybe_unserialize( $random_privelege_base );
if ( is_array( $random_privelege ) && count( $random_privelege ) > 0 ) {
$random_post_authors = array();
foreach ( $random_privelege as $role ) {
$users = get_users( array( 'role' => $role ) );
foreach ( $users as $user ) {
$random_post_authors[] = $user->ID;
}
}
if ( count( $random_post_authors ) > 0 ) {
shuffle( $random_post_authors );
$key = array_rand( $random_post_authors );
wp_update_post(
array(
'ID' => $new_post_id,
'post_author' => $random_post_authors[ $key ],
)
);
}
}
$random_category = isset( $post_custom['_saved_draft_random_category'] ) ? $post_custom['_saved_draft_random_category'] : false;
$random_category = is_array( $random_category ) ? current( $random_category ) : null;
if ( ! empty( $random_category ) ) {
$cats = get_categories(
array(
'type' => 'post',
'hide_empty' => 0,
)
);
$random_cats = array();
if ( is_array( $cats ) ) {
foreach ( $cats as $cat ) {
$random_cats[] = $cat->term_id;
}
}
if ( count( $random_cats ) > 0 ) {
shuffle( $random_cats );
$key = array_rand( $random_cats );
wp_set_post_categories( $new_post_id, array( $random_cats[ $key ] ), false );
}
}
}
private function create_set_categories ( $new_post_id, $post_category, $post_to_only ) {
// If categories exist, create them (second parameter of wp_create_categories adds the categories to the post).
include_once ABSPATH . 'wp-admin/includes/taxonomy.php'; // Contains wp_create_categories.
if ( isset( $post_category ) && '' !== $post_category ) {
$categories = explode( ',', $post_category );
if ( count( $categories ) > 0 ) {
if ( ! $post_to_only ) {
$post_category = wp_create_categories( $categories, $new_post_id );
} else {
$cat_ids = array();
foreach ( $categories as $cat ) {
$id = category_exists( $cat );
if ( $id ) {
$cat_ids[] = $id;
}
}
if ( count( $cat_ids ) > 0 ) {
wp_set_post_categories( $new_post_id, $cat_ids );
}
}
}
}
}
private function create_set_custom_fields( $new_post_id, $post_custom, $seo_ext_activated, &$post_to_only ) {
// Set custom fields.
$not_allowed = array(
'_slug',
'_tags',
'_edit_lock',
'_selected_sites',
'_selected_groups',
'_selected_by',
'_categories',
'_edit_last',
'_sticky',
'_mainwp_post_dripper',
'_bulkpost_do_not_del',
'_mainwp_spin_me',
);
$not_allowed[] = '_mainwp_boilerplate_sites_posts';
$not_allowed[] = '_ezine_post_keyword';
$not_allowed[] = '_ezine_post_display_sig';
$not_allowed[] = '_ezine_post_remove_link';
$not_allowed[] = '_ezine_post_grab_image';
$not_allowed[] = '_ezine_post_grab_image_placement';
$not_allowed[] = '_ezine_post_template_id';
$not_allowed[] = '_mainwp_post_plus';
$not_allowed[] = '_saved_as_draft';
$not_allowed[] = '_saved_draft_categories';
$not_allowed[] = '_saved_draft_tags';
$not_allowed[] = '_saved_draft_random_privelege';
$not_allowed[] = '_saved_draft_random_category';
$not_allowed[] = '_saved_draft_random_publish_date';
$not_allowed[] = '_saved_draft_publish_date_from';
$not_allowed[] = '_saved_draft_publish_date_to';
$not_allowed[] = '_post_to_only_existing_categories';
$not_allowed[] = '_mainwp_edit_post_site_id';
$not_allowed[] = '_mainwp_edit_post_id';
$not_allowed[] = '_edit_post_status';
if ( is_array( $post_custom ) ) {
foreach ( $post_custom as $meta_key => $meta_values ) {
if ( ! in_array( $meta_key, $not_allowed ) ) {
foreach ( $meta_values as $meta_value ) {
if ( 0 === strpos( $meta_key, '_mainwp_spinner_' ) ) {
continue;
}
if ( ! $seo_ext_activated ) {
// if WordPress SEO plugin is not activated do not save yoast post meta.
if ( false === strpos( $meta_key, '_yoast_wpseo_' ) ) {
update_post_meta( $new_post_id, $meta_key, $meta_value );
}
} else {
update_post_meta( $new_post_id, $meta_key, $meta_value );
}
}
} elseif ( '_sticky' === $meta_key ) {
foreach ( $meta_values as $meta_value ) {
if ( 'sticky' === base64_decode( $meta_value ) ) { // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons.
stick_post( $new_post_id );
}
}
} elseif ( '_post_to_only_existing_categories' === $meta_key ) {
if ( isset( $meta_values[0] ) && $meta_values[0] ) {
$post_to_only = true;
}
}
}
}
}
private function create_seo_extension_activated( $new_post_id, $post_custom ) {
$_seo_opengraph_image = isset( $post_custom[ WPSEO_Meta::$meta_prefix . 'opengraph-image' ] ) ? $post_custom[ WPSEO_Meta::$meta_prefix . 'opengraph-image' ] : array();
$_seo_opengraph_image = current( $_seo_opengraph_image );
$_server_domain = '';
$_server = get_option( 'mainwp_child_server' );
if ( preg_match( '/(https?:\/\/[^\/]+\/).+/', $_server, $matchs ) ) {
$_server_domain = isset( $matchs[1] ) ? $matchs[1] : '';
}
// 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.
if ( null !== $upload ) {
update_post_meta( $new_post_id, WPSEO_Meta::$meta_prefix . 'opengraph-image', $upload['url'] ); // Add the image to the post!
}
} catch ( \Exception $e ) {
// ok!
}
}
}
private function create_featured_image( $new_post_id, $post_featured_image, $check_image_existed ){
$featured_image_exist = false;
// 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.
if ( null !== $upload ) {
update_post_meta( $new_post_id, '_thumbnail_id', $upload['id'] ); // Add the thumbnail to the post!
$featured_image_exist = true;
if ( isset( $others['featured_image_data'] ) ) {
$_image_data = $others['featured_image_data'];
update_post_meta( $upload['id'], '_wp_attachment_image_alt', $_image_data['alt'] );
wp_update_post(
array(
'ID' => $upload['id'],
'post_excerpt' => $_image_data['caption'],
'post_content' => $_image_data['description'],
'post_title' => $_image_data['title'],
)
);
}
}
} catch ( \Exception $e ) {
// ok!
}
}
if ( ! $featured_image_exist ) {
delete_post_meta( $new_post_id, '_thumbnail_id' );
}
}
}

View file

@ -66,8 +66,7 @@ class MainWP_Child_Stats {
// Show stats.
public function get_site_stats( $information = array(), $exit = true ) {
global $wp_version;
if ( $exit ) {
$this->update_external_settings();
}
@ -78,33 +77,8 @@ class MainWP_Child_Stats {
}
MainWP_Child_Plugins_Check::may_outdate_number_change();
$information['version'] = MainWP_Child::$version;
$information['wpversion'] = $wp_version;
$information['siteurl'] = get_option( 'siteurl' );
$information['wpe'] = MainWP_Helper::is_wp_engine() ? 1 : 0;
$theme_name = wp_get_theme()->get( 'Name' );
$information['site_info'] = array(
'wpversion' => $wp_version,
'debug_mode' => ( defined( 'WP_DEBUG' ) && true === WP_DEBUG ) ? true : false,
'phpversion' => phpversion(),
'child_version' => MainWP_Child::$version,
'memory_limit' => MainWP_Child_Server_Information::get_php_memory_limit(),
'mysql_version' => MainWP_Child_Server_Information::get_my_sql_version(),
'themeactivated' => $theme_name,
'ip' => $_SERVER['SERVER_ADDR'],
);
// Try to switch to SSL if SSL is enabled in between!
$pubkey = get_option( 'mainwp_child_pubkey' );
$nossl = get_option( 'mainwp_child_nossl' );
if ( 1 == $nossl ) {
if ( isset( $pubkey ) && MainWP_Helper::is_ssl_enabled() ) {
MainWP_Helper::update_option( 'mainwp_child_nossl', 0, 'yes' );
$nossl = 0;
}
}
$information['nossl'] = ( 1 == $nossl ? 1 : 0 );
$this->stats_get_info( $information );
include_once ABSPATH . '/wp-admin/includes/update.php';
@ -113,55 +87,19 @@ class MainWP_Child_Stats {
ini_set( 'max_execution_time', $timeout ); //phpcs:ignore -- to custom
// Check for new versions.
if ( null !== $this->filterFunction ) {
add_filter( 'pre_site_transient_update_core', $this->filterFunction, 99 );
}
if ( null !== $this->filterFunction ) {
add_filter( 'pre_transient_update_core', $this->filterFunction, 99 );
}
wp_version_check();
$core_updates = get_core_updates();
if ( is_array( $core_updates ) && count( $core_updates ) > 0 ) {
foreach ( $core_updates as $core_update ) {
if ( 'latest' === $core_update->response ) {
break;
}
if ( 'upgrade' === $core_update->response && version_compare( $wp_version, $core_update->current, '<=' ) ) {
$information['wp_updates'] = $core_update->current;
}
}
}
if ( ! isset( $information['wp_updates'] ) ) {
$information['wp_updates'] = null;
}
if ( null !== $this->filterFunction ) {
remove_filter( 'pre_site_transient_update_core', $this->filterFunction, 99 );
}
if ( null !== $this->filterFunction ) {
remove_filter( 'pre_transient_update_core', $this->filterFunction, 99 );
}
$information['wp_updates'] = $this->stats_wp_update();
add_filter( 'default_option_active_plugins', array( &$this, 'default_option_active_plugins' ) );
add_filter( 'option_active_plugins', array( &$this, 'default_option_active_plugins' ) );
// First check for new premium updates.
$update_check = apply_filters( 'mwp_premium_update_check', array() );
if ( ! empty( $update_check ) ) {
foreach ( $update_check as $updateFeedback ) {
if ( is_array( $updateFeedback['callback'] ) && isset( $updateFeedback['callback'][0] ) && isset( $updateFeedback['callback'][1] ) ) {
call_user_func( array( $updateFeedback['callback'][0], $updateFeedback['callback'][1] ) );
} elseif ( is_string( $updateFeedback['callback'] ) ) {
call_user_func( $updateFeedback['callback'] );
}
}
}
$this->check_premium_updates();
$informationPremiumUpdates = apply_filters( 'mwp_premium_update_notification', array() );
$premiumPlugins = array();
$premiumThemes = array();
if ( is_array( $informationPremiumUpdates ) ) {
$premiumUpdates = array();
$information['premium_updates'] = array();
$premiumUpdates = array();
$informationPremiumUpdatesLength = count( $informationPremiumUpdates );
for ( $i = 0; $i < $informationPremiumUpdatesLength; $i ++ ) {
if ( ! isset( $informationPremiumUpdates[ $i ]['new_version'] ) ) {
@ -179,7 +117,11 @@ class MainWP_Child_Stats {
unset( $informationPremiumUpdates[ $i ]['old_version'] );
unset( $informationPremiumUpdates[ $i ]['new_version'] );
if ( ! isset( $information['premium_updates'] ) ) {
$information['premium_updates'] = array();
}
$information['premium_updates'][ $slug ] = $informationPremiumUpdates[ $i ];
$information['premium_updates'][ $slug ]['update'] = (object) array(
'new_version' => $new_version,
@ -192,183 +134,33 @@ class MainWP_Child_Stats {
}
MainWP_Helper::update_option( 'mainwp_premium_updates', $premiumUpdates );
}
remove_filter( 'default_option_active_plugins', array( &$this, 'default_option_active_plugins' ) );
remove_filter( 'option_active_plugins', array( &$this, 'default_option_active_plugins' ) );
if ( null !== $this->filterFunction ) {
add_filter( 'pre_site_transient_update_plugins', $this->filterFunction, 99 );
}
global $wp_current_filter;
$wp_current_filter[] = 'load-plugins.php'; // phpcs:ignore -- to custom plugin installation.
wp_update_plugins();
include_once ABSPATH . '/wp-admin/includes/plugin.php';
$plugin_updates = get_plugin_updates();
if ( is_array( $plugin_updates ) ) {
$information['plugin_updates'] = array();
foreach ( $plugin_updates as $slug => $plugin_update ) {
if ( in_array( $plugin_update->Name, $premiumPlugins ) ) {
continue;
}
// to fix incorrect info.
if ( ! property_exists( $plugin_update, 'update' ) || ! property_exists( $plugin_update->update, 'new_version' ) || empty( $plugin_update->update->new_version ) ) {
continue;
}
$information['plugin_updates'][ $slug ] = $plugin_update;
}
}
if ( null !== $this->filterFunction ) {
remove_filter( 'pre_site_transient_update_plugins', $this->filterFunction, 99 );
}
// to fix premium plugs update.
$cached_plugins_update = get_site_transient( 'mainwp_update_plugins_cached' );
if ( is_array( $cached_plugins_update ) && ( count( $cached_plugins_update ) > 0 ) ) {
if ( ! isset( $information['plugin_updates'] ) ) {
$information['plugin_updates'] = array();
}
foreach ( $cached_plugins_update as $slug => $plugin_update ) {
// to fix incorrect info.
if ( ! property_exists( $plugin_update, 'new_version' ) || empty( $plugin_update->new_version ) ) { // may do not need to check this?
// to fix for some premiums update info.
if ( property_exists( $plugin_update, 'update' ) ) {
if ( ! property_exists( $plugin_update->update, 'new_version' ) || empty( $plugin_update->update->new_version ) ) {
continue;
}
} else {
continue;
}
}
if ( ! isset( $information['plugin_updates'][ $slug ] ) ) {
$information['plugin_updates'][ $slug ] = $plugin_update;
}
}
}
if ( null !== $this->filterFunction ) {
add_filter( 'pre_site_transient_update_themes', $this->filterFunction, 99 );
}
wp_update_themes();
include_once ABSPATH . '/wp-admin/includes/theme.php';
$theme_updates = MainWP_Child_Updates::get_instance()->upgrade_get_theme_updates();
if ( is_array( $theme_updates ) ) {
$information['theme_updates'] = array();
foreach ( $theme_updates as $slug => $theme_update ) {
$name = ( is_array( $theme_update ) ? $theme_update['Name'] : $theme_update->Name );
if ( in_array( $name, $premiumThemes ) ) {
continue;
}
$information['theme_updates'][ $slug ] = $theme_update;
}
}
if ( null !== $this->filterFunction ) {
remove_filter( 'pre_site_transient_update_themes', $this->filterFunction, 99 );
}
// to fix premium themes update.
$cached_themes_update = get_site_transient( 'mainwp_update_themes_cached' );
if ( is_array( $cached_themes_update ) && ( count( $cached_themes_update ) > 0 ) ) {
if ( ! isset( $information['theme_updates'] ) ) {
$information['theme_updates'] = array();
}
foreach ( $cached_themes_update as $slug => $theme_update ) {
$name = ( is_array( $theme_update ) ? $theme_update['Name'] : $theme_update->Name );
if ( in_array( $name, $premiumThemes ) ) {
continue;
}
if ( isset( $information['theme_updates'][ $slug ] ) ) {
continue;
}
$information['theme_updates'][ $slug ] = $theme_update;
}
}
$translation_updates = wp_get_translation_updates();
if ( ! empty( $translation_updates ) ) {
$information['translation_updates'] = array();
foreach ( $translation_updates as $translation_update ) {
$new_translation_update = array(
'type' => $translation_update->type,
'slug' => $translation_update->slug,
'language' => $translation_update->language,
'version' => $translation_update->version,
);
if ( 'plugin' === $translation_update->type ) {
$all_plugins = get_plugins();
foreach ( $all_plugins as $file => $plugin ) {
$path = dirname( $file );
if ( $path == $translation_update->slug ) {
$new_translation_update['name'] = $plugin['Name'];
break;
}
}
} elseif ( 'theme' === $translation_update->type ) {
$theme = wp_get_theme( $translation_update->slug );
$new_translation_update['name'] = $theme->name;
} elseif ( ( 'core' === $translation_update->type ) && ( 'default' === $translation_update->slug ) ) {
$new_translation_update['name'] = 'WordPress core';
}
$information['translation_updates'][] = $new_translation_update;
}
}
$information['plugin_updates'] = $this->stats_plugin_update( $premiumPlugins );
$information['theme_updates'] = $this->stats_theme_update( $premiumThemes );
$information['translation_updates'] = $this->stats_translation_updates();
$information['recent_comments'] = MainWP_Child_Posts::get_instance()->get_recent_comments( array( 'approve', 'hold' ), 5 );
$recent_number = 5;
if ( isset( $_POST ) && isset( $_POST['recent_number'] ) ) {
$recent_number = $_POST['recent_number'];
if ( get_option( 'mainwp_child_recent_number', 5 ) != $recent_number ) {
update_option( 'mainwp_child_recent_number', $recent_number );
}
} else {
$recent_number = get_option( 'mainwp_child_recent_number', 5 );
}
if ( $recent_number <= 0 || $recent_number > 30 ) {
$recent_number = 5;
}
$recent_number = $this->get_recent_number();
$information['recent_posts'] = MainWP_Child_Posts::get_instance()->get_recent_posts( array( 'publish', 'draft', 'pending', 'trash', 'future' ), $recent_number );
$information['recent_pages'] = MainWP_Child_Posts::get_instance()->get_recent_posts( array( 'publish', 'draft', 'pending', 'trash', 'future' ), $recent_number, 'page' );
$information['securityIssues'] = MainWP_Security::get_stats_security();
// Directory listings!
$information['directories'] = $this->scan_dir( ABSPATH, 3 );
$cats = get_categories(
array(
'hide_empty' => 0,
'hierarchical' => true,
'number' => 300,
)
);
$categories = array();
foreach ( $cats as $cat ) {
$categories[] = $cat->name;
}
$information['categories'] = $categories;
$information['directories'] = $this->scan_dir( ABSPATH, 3 );
$information['categories'] = $this->stats_get_categories();
$get_file_size = apply_filters_deprecated( 'mainwp-child-get-total-size', array( true ), '4.0.7.1', 'mainwp_child_get_total_size' );
$get_file_size = apply_filters( 'mainwp_child_get_total_size', $get_file_size );
if ( $get_file_size && isset( $_POST['cloneSites'] ) && ( '0' !== $_POST['cloneSites'] ) ) {
$max_exe = ini_get( 'max_execution_time' );
if ( $max_exe > 20 ) {
$information['totalsize'] = $this->get_total_file_size();
}
$totalsize = $this->stats_get_total_size();
if ( ! empty( $totalsize ) ) {
$information['totalsize'] = $totalsize;
}
$information['dbsize'] = MainWP_Child_DB::get_size();
global $mainWPChild;
@ -377,10 +169,8 @@ class MainWP_Child_Stats {
$auths = get_option( 'mainwp_child_auth' );
$information['extauth'] = ( $auths && isset( $auths[ $max_his ] ) ? $auths[ $max_his ] : null );
$plugins = $this->get_all_plugins_int( false );
$themes = $this->get_all_themes_int( false );
$information['plugins'] = $plugins;
$information['themes'] = $themes;
$information['plugins'] = $this->get_all_plugins_int( false );
$information['themes'] = $this->get_all_themes_int( false );
if ( isset( $_POST['optimize'] ) && ( '1' === $_POST['optimize'] ) ) {
$information['users'] = MainWP_Child_Users::get_instance()->get_all_users_int( 500 );
@ -418,25 +208,7 @@ class MainWP_Child_Stats {
}
if ( isset( $_POST['othersData'] ) ) {
$othersData = json_decode( stripslashes( $_POST['othersData'] ), true );
if ( ! is_array( $othersData ) ) {
$othersData = array();
}
if ( isset( $othersData['wpvulndbToken'] ) ) {
$wpvulndb_token = get_option( 'mainwp_child_wpvulndb_token', '' );
if ( $wpvulndb_token != $othersData['wpvulndbToken'] ) {
MainWP_Helper::update_option( 'mainwp_child_wpvulndb_token', $othersData['wpvulndbToken'] );
}
}
try {
$information = apply_filters_deprecated( 'mainwp-site-sync-others-data', array( $information, $othersData ), '4.0.7.1', 'mainwp_site_sync_others_data' );
$information = apply_filters( 'mainwp_site_sync_others_data', $information, $othersData );
} catch ( \Exception $e ) {
MainWP_Helper::log_debug( $e->getMessage() );
}
$this->stats_others_data( $information );
}
if ( $exit ) {
@ -446,6 +218,298 @@ class MainWP_Child_Stats {
return $information;
}
private function stats_others_data( &$information ){
$othersData = json_decode( stripslashes( $_POST['othersData'] ), true );
if ( ! is_array( $othersData ) ) {
$othersData = array();
}
if ( isset( $othersData['wpvulndbToken'] ) ) {
$wpvulndb_token = get_option( 'mainwp_child_wpvulndb_token', '' );
if ( $wpvulndb_token != $othersData['wpvulndbToken'] ) {
MainWP_Helper::update_option( 'mainwp_child_wpvulndb_token', $othersData['wpvulndbToken'] );
}
}
try {
$information = apply_filters_deprecated( 'mainwp-site-sync-others-data', array( $information, $othersData ), '4.0.7.1', 'mainwp_site_sync_others_data' );
$information = apply_filters( 'mainwp_site_sync_others_data', $information, $othersData );
} catch ( \Exception $e ) {
MainWP_Helper::log_debug( $e->getMessage() );
}
}
private function stats_translation_updates() {
$results = array();
$translation_updates = wp_get_translation_updates();
if ( ! empty( $translation_updates ) ) {
foreach ( $translation_updates as $translation_update ) {
$new_translation_update = array(
'type' => $translation_update->type,
'slug' => $translation_update->slug,
'language' => $translation_update->language,
'version' => $translation_update->version,
);
if ( 'plugin' === $translation_update->type ) {
$all_plugins = get_plugins();
foreach ( $all_plugins as $file => $plugin ) {
$path = dirname( $file );
if ( $path == $translation_update->slug ) {
$new_translation_update['name'] = $plugin['Name'];
break;
}
}
} elseif ( 'theme' === $translation_update->type ) {
$theme = wp_get_theme( $translation_update->slug );
$new_translation_update['name'] = $theme->name;
} elseif ( ( 'core' === $translation_update->type ) && ( 'default' === $translation_update->slug ) ) {
$new_translation_update['name'] = 'WordPress core';
}
$results[] = $new_translation_update;
}
}
return $results;
}
private function stats_theme_update( $premiumThemes ) {
$results = array();
if ( null !== $this->filterFunction ) {
add_filter( 'pre_site_transient_update_themes', $this->filterFunction, 99 );
}
wp_update_themes();
include_once ABSPATH . '/wp-admin/includes/theme.php';
$theme_updates = MainWP_Child_Updates::get_instance()->upgrade_get_theme_updates();
if ( is_array( $theme_updates ) ) {
foreach ( $theme_updates as $slug => $theme_update ) {
$name = ( is_array( $theme_update ) ? $theme_update['Name'] : $theme_update->Name );
if ( in_array( $name, $premiumThemes ) ) {
continue;
}
$results[ $slug ] = $theme_update;
}
}
if ( null !== $this->filterFunction ) {
remove_filter( 'pre_site_transient_update_themes', $this->filterFunction, 99 );
}
// to fix premium themes update.
$cached_themes_update = get_site_transient( 'mainwp_update_themes_cached' );
if ( is_array( $cached_themes_update ) && ( count( $cached_themes_update ) > 0 ) ) {
foreach ( $cached_themes_update as $slug => $theme_update ) {
$name = ( is_array( $theme_update ) ? $theme_update['Name'] : $theme_update->Name );
if ( in_array( $name, $premiumThemes ) ) {
continue;
}
if ( isset( $results[ $slug ] ) ) {
continue;
}
$results[ $slug ] = $theme_update;
}
}
return $results;
}
private function stats_get_info( &$information ) {
global $wp_version;
$information['version'] = MainWP_Child::$version;
$information['wpversion'] = $wp_version;
$information['siteurl'] = get_option( 'siteurl' );
$information['wpe'] = MainWP_Helper::is_wp_engine() ? 1 : 0;
$theme_name = wp_get_theme()->get( 'Name' );
$information['site_info'] = array(
'wpversion' => $wp_version,
'debug_mode' => ( defined( 'WP_DEBUG' ) && true === WP_DEBUG ) ? true : false,
'phpversion' => phpversion(),
'child_version' => MainWP_Child::$version,
'memory_limit' => MainWP_Child_Server_Information::get_php_memory_limit(),
'mysql_version' => MainWP_Child_Server_Information::get_my_sql_version(),
'themeactivated' => $theme_name,
'ip' => $_SERVER['SERVER_ADDR'],
);
// Try to switch to SSL if SSL is enabled in between!
$pubkey = get_option( 'mainwp_child_pubkey' );
$nossl = get_option( 'mainwp_child_nossl' );
if ( 1 == $nossl ) {
if ( isset( $pubkey ) && MainWP_Helper::is_ssl_enabled() ) {
MainWP_Helper::update_option( 'mainwp_child_nossl', 0, 'yes' );
$nossl = 0;
}
}
$information['nossl'] = ( 1 == $nossl ? 1 : 0 );
}
private function stats_wp_update() {
$result = null;
// Check for new versions.
if ( null !== $this->filterFunction ) {
add_filter( 'pre_site_transient_update_core', $this->filterFunction, 99 );
}
if ( null !== $this->filterFunction ) {
add_filter( 'pre_transient_update_core', $this->filterFunction, 99 );
}
wp_version_check();
$core_updates = get_core_updates();
if ( is_array( $core_updates ) && count( $core_updates ) > 0 ) {
foreach ( $core_updates as $core_update ) {
if ( 'latest' === $core_update->response ) {
break;
}
if ( 'upgrade' === $core_update->response && version_compare( $wp_version, $core_update->current, '<=' ) ) {
$result = $core_update->current;
}
}
}
if ( null !== $this->filterFunction ) {
remove_filter( 'pre_site_transient_update_core', $this->filterFunction, 99 );
}
if ( null !== $this->filterFunction ) {
remove_filter( 'pre_transient_update_core', $this->filterFunction, 99 );
}
}
private function check_premium_updates() {
// First check for new premium updates.
$update_check = apply_filters( 'mwp_premium_update_check', array() );
if ( ! empty( $update_check ) ) {
foreach ( $update_check as $updateFeedback ) {
if ( is_array( $updateFeedback['callback'] ) && isset( $updateFeedback['callback'][0] ) && isset( $updateFeedback['callback'][1] ) ) {
call_user_func( array( $updateFeedback['callback'][0], $updateFeedback['callback'][1] ) );
} elseif ( is_string( $updateFeedback['callback'] ) ) {
call_user_func( $updateFeedback['callback'] );
}
}
}
}
private function stats_plugin_update( $premiumPlugins ) {
$results = array();
if ( null !== $this->filterFunction ) {
add_filter( 'pre_site_transient_update_plugins', $this->filterFunction, 99 );
}
global $wp_current_filter;
$wp_current_filter[] = 'load-plugins.php'; // phpcs:ignore -- to custom plugin installation.
wp_update_plugins();
include_once ABSPATH . '/wp-admin/includes/plugin.php';
$plugin_updates = get_plugin_updates();
if ( is_array( $plugin_updates ) ) {
foreach ( $plugin_updates as $slug => $plugin_update ) {
if ( in_array( $plugin_update->Name, $premiumPlugins ) ) {
continue;
}
// to fix incorrect info.
if ( ! property_exists( $plugin_update, 'update' ) || ! property_exists( $plugin_update->update, 'new_version' ) || empty( $plugin_update->update->new_version ) ) {
continue;
}
$results[ $slug ] = $plugin_update;
}
}
if ( null !== $this->filterFunction ) {
remove_filter( 'pre_site_transient_update_plugins', $this->filterFunction, 99 );
}
// to fix premium plugs update.
$cached_plugins_update = get_site_transient( 'mainwp_update_plugins_cached' );
if ( is_array( $cached_plugins_update ) && ( count( $cached_plugins_update ) > 0 ) ) {
foreach ( $cached_plugins_update as $slug => $plugin_update ) {
// to fix incorrect info.
if ( ! property_exists( $plugin_update, 'new_version' ) || empty( $plugin_update->new_version ) ) { // may do not need to check this?
// to fix for some premiums update info.
if ( property_exists( $plugin_update, 'update' ) ) {
if ( ! property_exists( $plugin_update->update, 'new_version' ) || empty( $plugin_update->update->new_version ) ) {
continue;
}
} else {
continue;
}
}
if ( ! isset( $results[ $slug ] ) ) {
$results[ $slug ] = $plugin_update;
}
}
}
return $results;
}
private function stats_get_categories() {
$cats = get_categories(
array(
'hide_empty' => 0,
'hierarchical' => true,
'number' => 300,
)
);
$categories = array();
foreach ( $cats as $cat ) {
$categories[] = $cat->name;
}
return $categories;
}
private function stats_get_total_size() {
$total = null;
$get_file_size = apply_filters_deprecated( 'mainwp-child-get-total-size', array( true ), '4.0.7.1', 'mainwp_child_get_total_size' );
$get_file_size = apply_filters( 'mainwp_child_get_total_size', $get_file_size );
if ( $get_file_size && isset( $_POST['cloneSites'] ) && ( '0' !== $_POST['cloneSites'] ) ) {
$max_exe = ini_get( 'max_execution_time' );
if ( $max_exe > 20 ) {
$total = $this->get_total_file_size();
}
}
return $total;
}
private function get_recent_number() {
$recent_number = 5;
if ( isset( $_POST ) && isset( $_POST['recent_number'] ) ) {
$recent_number = $_POST['recent_number'];
if ( get_option( 'mainwp_child_recent_number', 5 ) != $recent_number ) {
update_option( 'mainwp_child_recent_number', $recent_number );
}
} else {
$recent_number = get_option( 'mainwp_child_recent_number', 5 );
}
if ( $recent_number <= 0 || $recent_number > 30 ) {
$recent_number = 5;
}
return $recent_number;
}
public function update_external_settings() {
$update_htaccess = false;
@ -545,7 +609,7 @@ class MainWP_Child_Stats {
return empty( $output ) ? null : $output;
}
$files = $this->int_scan_dir( $pDir );
if ( $files ) {
if ( $files ) {
foreach ( $files as $file ) {
if ( ( '.' === $file ) || ( '..' === $file ) ) {
continue;
@ -573,13 +637,16 @@ class MainWP_Child_Stats {
while ( false !== $file ) {
$newDir = $dir . $file . DIRECTORY_SEPARATOR;
if ( ! is_dir( $newDir ) ) {
$file = readdir( $dh );
continue;
}
$out[] = $file;
$file = readdir( $dh );
if ( $cnt ++ > 10 ) {
return $out;
}
break;
}
}
closedir( $dh );
@ -588,7 +655,7 @@ class MainWP_Child_Stats {
return false;
}
public function get_all_themes() {
$keyword = $_POST['keyword'];
$status = $_POST['status'];

View file

@ -66,7 +66,74 @@ class MainWP_Child_Updates {
$information['upgrades'] = array();
$mwp_premium_updates_todo = array();
$mwp_premium_updates_todo_slugs = array();
if ( isset( $_POST['type'] ) && 'plugin' === $_POST['type'] ) {
if ( isset( $_POST['type'] ) && 'plugin' === $_POST['type'] ) {
$this->upgrade_plugin( $information, $mwp_premium_updates_todo, $mwp_premium_updates_todo_slugs );
} elseif ( isset( $_POST['type'] ) && 'theme' === $_POST['type'] ) {
$this->upgrade_theme( $information, $mwp_premium_updates_todo, $mwp_premium_updates_todo_slugs );
} else {
MainWP_Helper::error( __( 'Invalid request!', 'mainwp-child' ) );
}
if ( count( $mwp_premium_updates_todo ) > 0 ) {
// Upgrade via WP.
// @see wp-admin/update.php.
$result = $premiumUpgrader->bulk_upgrade( $mwp_premium_updates_todo_slugs );
if ( ! empty( $result ) ) {
foreach ( $result as $plugin => $info ) {
if ( ! empty( $info ) ) {
$information['upgrades'][ $plugin ] = true;
foreach ( $mwp_premium_updates_todo as $key => $update ) {
$slug = ( isset( $update['slug'] ) ? $update['slug'] : $update['Name'] );
}
}
}
}
// Upgrade via callback.
foreach ( $mwp_premium_updates_todo as $update ) {
$slug = ( isset( $update['slug'] ) ? $update['slug'] : $update['Name'] );
if ( isset( $update['url'] ) ) {
$installer = new WP_Upgrader();
$result = $installer->run(
array(
'package' => $update['url'],
'destination' => ( 'plugin' === $update['type'] ? WP_PLUGIN_DIR : WP_CONTENT_DIR . '/themes' ),
'clear_destination' => true,
'clear_working' => true,
'hook_extra' => array(),
)
);
$information['upgrades'][ $slug ] = ( ! is_wp_error( $result ) && ! empty( $result ) );
} elseif ( isset( $update['callback'] ) ) {
if ( is_array( $update['callback'] ) && isset( $update['callback'][0] ) && isset( $update['callback'][1] ) ) {
$update_result = call_user_func(
array(
$update['callback'][0],
$update['callback'][1],
)
);
$information['upgrades'][ $slug ] = $update_result && true;
} elseif ( is_string( $update['callback'] ) ) {
$update_result = call_user_func( $update['callback'] );
$information['upgrades'][ $slug ] = $update_result && true;
} else {
$information['upgrades'][ $slug ] = false;
}
} else {
$information['upgrades'][ $slug ] = false;
}
}
}
$information['sync'] = MainWP_Child_Stats::get_instance()->get_site_stats( array(), false );
mainwp_child_helper()->write( $information );
}
private function upgrade_plugin( &$information, &$mwp_premium_updates_todo, &$mwp_premium_updates_todo_slugs ) {
include_once ABSPATH . '/wp-admin/includes/update.php';
if ( null !== $this->filterFunction ) {
add_filter( 'pre_site_transient_update_plugins', $this->filterFunction, 99 );
@ -185,8 +252,11 @@ class MainWP_Child_Updates {
if ( null !== $this->filterFunction ) {
remove_filter( 'pre_site_transient_update_plugins', $this->filterFunction, 99 );
}
} elseif ( isset( $_POST['type'] ) && 'theme' === $_POST['type'] ) {
}
private function upgrade_theme( &$information, &$mwp_premium_updates_todo, &$mwp_premium_updates_todo_slugs ) {
$last_update = get_site_transient( 'update_themes' );
include_once ABSPATH . '/wp-admin/includes/update.php';
@ -295,66 +365,8 @@ class MainWP_Child_Updates {
if ( null !== $this->filterFunction ) {
remove_filter( 'pre_site_transient_update_themes', $this->filterFunction, 99 );
}
} else {
MainWP_Helper::error( __( 'Invalid request!', 'mainwp-child' ) );
}
if ( count( $mwp_premium_updates_todo ) > 0 ) {
// Upgrade via WP.
// @see wp-admin/update.php.
$result = $premiumUpgrader->bulk_upgrade( $mwp_premium_updates_todo_slugs );
if ( ! empty( $result ) ) {
foreach ( $result as $plugin => $info ) {
if ( ! empty( $info ) ) {
$information['upgrades'][ $plugin ] = true;
foreach ( $mwp_premium_updates_todo as $key => $update ) {
$slug = ( isset( $update['slug'] ) ? $update['slug'] : $update['Name'] );
}
}
}
}
// Upgrade via callback.
foreach ( $mwp_premium_updates_todo as $update ) {
$slug = ( isset( $update['slug'] ) ? $update['slug'] : $update['Name'] );
if ( isset( $update['url'] ) ) {
$installer = new WP_Upgrader();
$result = $installer->run(
array(
'package' => $update['url'],
'destination' => ( 'plugin' === $update['type'] ? WP_PLUGIN_DIR : WP_CONTENT_DIR . '/themes' ),
'clear_destination' => true,
'clear_working' => true,
'hook_extra' => array(),
)
);
$information['upgrades'][ $slug ] = ( ! is_wp_error( $result ) && ! empty( $result ) );
} elseif ( isset( $update['callback'] ) ) {
if ( is_array( $update['callback'] ) && isset( $update['callback'][0] ) && isset( $update['callback'][1] ) ) {
$update_result = call_user_func(
array(
$update['callback'][0],
$update['callback'][1],
)
);
$information['upgrades'][ $slug ] = $update_result && true;
} elseif ( is_string( $update['callback'] ) ) {
$update_result = call_user_func( $update['callback'] );
$information['upgrades'][ $slug ] = $update_result && true;
} else {
$information['upgrades'][ $slug ] = false;
}
} else {
$information['upgrades'][ $slug ] = false;
}
}
}
$information['sync'] = MainWP_Child_Stats::get_instance()->get_site_stats( array(), false );
mainwp_child_helper()->write( $information );
}
public function upgrade_get_theme_updates() {
$themeUpdates = get_theme_updates();
$newThemeUpdates = array();

View file

@ -2099,7 +2099,7 @@ class MainWP_Child_Updraft_Plus_Backups {
}
public function analyse_db_file( $timestamp, $res, $db_file = false, $header_only = false ) {
public function analyse_db_file( $timestamp, $res, $db_file = false, $header_only = false ) { // phpcs:ignore -- third party credit.
global $updraftplus;
$mess = array();

View file

@ -201,7 +201,7 @@ class MainWP_Child_Wordfence {
}
}
public function action() {
public function action() { // phpcs:ignore -- not quite complex method
$information = array();
if ( ! $this->is_wordfence_installed ) {
mainwp_child_helper()->write( array( 'error' => __( 'Please install the Wordfence plugin on the child site.', $this->plugin_translate ) ) );

View file

@ -5,7 +5,7 @@
namespace MainWP\Child;
// phpcs:disable
if ( defined( 'MAINWP_DEBUG' ) && MAINWP_DEBUG === true ) {
if ( defined( 'MAINWP_CHILD_DEBUG' ) && MAINWP_CHILD_DEBUG === true ) {
error_reporting( E_ALL );
ini_set( 'display_errors', true );
ini_set( 'display_startup_errors', true );
@ -21,6 +21,7 @@ require_once ABSPATH . '/wp-admin/includes/file.php';
require_once ABSPATH . '/wp-admin/includes/plugin.php';
class MainWP_Child {
public static $version = '4.0.7.1';
private $update_version = '1.5';
@ -942,124 +943,16 @@ class MainWP_Child {
}
public function parse_init() {
if ( isset( $_REQUEST['cloneFunc'] ) ) {
if ( ! isset( $_REQUEST['key'] ) ) {
// if not valid result then return.
$valid_clone = MainWP_Clone_Install::get()->request_clone_funct();
// not valid clone.
if ( ! $valid_clone ) {
return;
}
if ( ! isset( $_REQUEST['f'] ) || ( '' === $_REQUEST['f'] ) ) {
return;
}
if ( ! $this->is_valid_auth( $_REQUEST['key'] ) ) {
return;
}
if ( 'dl' === $_REQUEST['cloneFunc'] ) {
$this->upload_file( $_REQUEST['f'] );
exit;
} elseif ( 'deleteCloneBackup' === $_POST['cloneFunc'] ) {
$dirs = MainWP_Helper::get_mainwp_dir( 'backup' );
$backupdir = $dirs[0];
$result = glob( $backupdir . $_POST['f'] );
if ( 0 === count( $result ) ) {
return;
}
unlink( $result[0] );
mainwp_child_helper()->write( array( 'result' => 'ok' ) );
} elseif ( 'createCloneBackupPoll' === $_POST['cloneFunc'] ) {
$dirs = MainWP_Helper::get_mainwp_dir( 'backup' );
$backupdir = $dirs[0];
$result = glob( $backupdir . 'backup-' . $_POST['f'] . '-*' );
$archiveFile = false;
foreach ( $result as $file ) {
if ( MainWP_Helper::is_archive( $file, 'backup-' . $_POST['f'] . '-' ) ) {
$archiveFile = $file;
break;
}
}
if ( false === $archiveFile ) {
return;
}
mainwp_child_helper()->write( array( 'size' => filesize( $archiveFile ) ) );
} elseif ( 'createCloneBackup' === $_POST['cloneFunc'] ) {
MainWP_Helper::end_session();
$files = glob( WP_CONTENT_DIR . '/dbBackup*.sql' );
foreach ( $files as $file ) {
unlink( $file );
}
if ( file_exists( ABSPATH . 'clone/config.txt' ) ) {
unlink( ABSPATH . 'clone/config.txt' );
}
if ( MainWP_Helper::is_dir_empty( ABSPATH . 'clone' ) ) {
rmdir( ABSPATH . 'clone' );
}
$wpversion = $_POST['wpversion'];
global $wp_version;
$includeCoreFiles = ( $wpversion !== $wp_version );
$excludes = ( isset( $_POST['exclude'] ) ? explode( ',', $_POST['exclude'] ) : array() );
$excludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/uploads/mainwp';
$uploadDir = MainWP_Helper::get_mainwp_dir();
$uploadDir = $uploadDir[0];
$excludes[] = str_replace( ABSPATH, '', $uploadDir );
$excludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/object-cache.php';
if ( version_compare( phpversion(), '5.3.0' ) >= 0 || ! ini_get( 'safe_mode' ) ) {
set_time_limit( 6000 );
}
$newExcludes = array();
foreach ( $excludes as $exclude ) {
$newExcludes[] = rtrim( $exclude, '/' );
}
$method = ( ! isset( $_POST['zipmethod'] ) ? 'tar.gz' : $_POST['zipmethod'] );
if ( 'tar.gz' === $method && ! function_exists( 'gzopen' ) ) {
$method = 'zip';
}
$res = MainWP_Backup::get()->create_full_backup( $newExcludes, ( isset( $_POST['f'] ) ? $_POST['f'] : $_POST['file'] ), true, $includeCoreFiles, 0, false, false, false, false, $method );
if ( ! $res ) {
$information['backup'] = false;
} else {
$information['backup'] = $res['file'];
$information['size'] = $res['filesize'];
}
$plugins = array();
$dir = WP_CONTENT_DIR . '/plugins/';
$fh = opendir( $dir );
$entry = readdir( $fh );
while ( $entry ) {
if ( ! is_dir( $dir . $entry ) ) {
continue;
}
if ( ( '.' === $entry ) || ( '..' === $entry ) ) {
continue;
}
$plugins[] = $entry;
}
closedir( $fh );
$information['plugins'] = $plugins;
$themes = array();
$dir = WP_CONTENT_DIR . '/themes/';
$fh = opendir( $dir );
while ( $entry = readdir( $fh ) ) {
if ( ! is_dir( $dir . $entry ) ) {
continue;
}
if ( ( '.' === $entry ) || ( '..' === $entry ) ) {
continue;
}
$themes[] = $entry;
}
closedir( $fh );
$information['themes'] = $themes;
mainwp_child_helper()->write( $information );
}
}
}
global $wp_rewrite;
@ -1080,116 +973,16 @@ class MainWP_Child {
}
$this->update_htaccess();
global $current_user;
// if login required.
if ( isset( $_REQUEST['login_required'] ) && ( '1' === $_REQUEST['login_required'] ) && isset( $_REQUEST['user'] ) ) {
$alter_login_required = false;
$username = rawurldecode( $_REQUEST['user'] );
if ( isset( $_REQUEST['alt_user'] ) && ! empty( $_REQUEST['alt_user'] ) ) {
$alter_login_required = $this->check_login_as( $_REQUEST['alt_user'] );
if ( $alter_login_required ) {
$username = rawurldecode( $_REQUEST['alt_user'] );
}
}
if ( is_user_logged_in() ) {
global $current_user;
if ( 10 !== $current_user->wp_user_level && ( ! isset( $current_user->user_level ) || 10 !== $current_user->user_level ) && ! current_user_can( 'level_10' ) ) {
do_action( 'wp_logout' );
}
}
$signature = rawurldecode( isset( $_REQUEST['mainwpsignature'] ) ? $_REQUEST['mainwpsignature'] : '' );
$file = '';
if ( isset( $_REQUEST['f'] ) ) {
$file = $_REQUEST['f'];
} elseif ( isset( $_REQUEST['file'] ) ) {
$file = $_REQUEST['file'];
} elseif ( isset( $_REQUEST['fdl'] ) ) {
$file = $_REQUEST['fdl'];
}
$auth = $this->auth( $signature, rawurldecode( ( isset( $_REQUEST['where'] ) ? $_REQUEST['where'] : $file ) ), isset( $_REQUEST['nonce'] ) ? $_REQUEST['nonce'] : '', isset( $_REQUEST['nossl'] ) ? $_REQUEST['nossl'] : 0 );
if ( ! $auth ) {
$valid_login_required = $this->parse_login_required();
// retunr parse init if login required are not valid.
if ( ! $valid_login_required ) {
return;
}
if ( ! is_user_logged_in() || $username !== $current_user->user_login ) {
if ( ! $this->login( $username ) ) {
return;
}
global $current_user;
if ( 10 !== $current_user->wp_user_level && ( ! isset( $current_user->user_level ) || 10 !== $current_user->user_level ) && ! current_user_can( 'level_10' ) ) {
// if is not alternative admin login.
// it is connected admin login.
if ( ! $alter_login_required ) {
// log out if connected admin is not admin level 10.
do_action( 'wp_logout' );
return;
}
}
}
if ( isset( $_REQUEST['fdl'] ) ) {
if ( stristr( $_REQUEST['fdl'], '..' ) ) {
return;
}
$this->upload_file( $_REQUEST['fdl'], isset( $_REQUEST['foffset'] ) ? $_REQUEST['foffset'] : 0 );
exit;
}
$where = isset( $_REQUEST['where'] ) ? $_REQUEST['where'] : '';
if ( isset( $_POST['f'] ) || isset( $_POST['file'] ) ) {
$file = '';
if ( isset( $_POST['f'] ) ) {
$file = $_POST['f'];
} elseif ( isset( $_POST['file'] ) ) {
$file = $_POST['file'];
}
$where = 'admin.php?page=mainwp_child_tab&tab=restore-clone';
if ( '' === session_id() ) {
session_start();
}
$_SESSION['file'] = $file;
$_SESSION['size'] = $_POST['size'];
}
// to support open not wp-admin url.
$open_location = isset( $_REQUEST['open_location'] ) ? $_REQUEST['open_location'] : '';
if ( ! empty( $open_location ) ) {
$open_location = base64_decode( $open_location ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons.
$_vars = MainWP_Helper::parse_query( $open_location );
$_path = wp_parse_url( $open_location, PHP_URL_PATH );
if ( isset( $_vars['_mwpNoneName'] ) && isset( $_vars['_mwpNoneValue'] ) ) {
$_vars[ $_vars['_mwpNoneName'] ] = wp_create_nonce( $_vars['_mwpNoneValue'] );
unset( $_vars['_mwpNoneName'] );
unset( $_vars['_mwpNoneValue'] );
$open_url = '';
foreach ( $_vars as $key => $value ) {
$open_url .= $key . '=' . $value . '&';
}
$open_url = rtrim( $open_url, '&' );
$open_location = '/wp-admin/' . $_path . '?' . $open_url;
} else {
if ( strpos( $open_location, 'nonce=child_temp_nonce' ) !== false ) {
$open_location = str_replace( 'nonce=child_temp_nonce', 'nonce=' . wp_create_nonce( 'wp-ajax' ), $open_location );
}
}
wp_safe_redirect( site_url() . $open_location );
exit();
}
wp_safe_redirect( admin_url( $where ) );
exit();
}
/**
* Security
*/
@ -1219,8 +1012,9 @@ class MainWP_Child {
}
}
$auth_user = false;
if ( $auth ) {
if ( $auth ) {
$auth_user = false;
// Check if the user exists & is an administrator.
if ( isset( $_POST['function'] ) && isset( $_POST['user'] ) ) {
@ -1263,7 +1057,7 @@ class MainWP_Child {
}
}
// Redirect to the admin part if needed.
// Redirect to the admin side if needed.
if ( isset( $_POST['admin'] ) && '1' === $_POST['admin'] ) {
wp_safe_redirect( get_option( 'siteurl' ) . '/wp-admin/' );
die();
@ -1271,37 +1065,141 @@ class MainWP_Child {
}
// Init extensions.
// Handle fatal errors for those init if needed.
\MainWP_Child_IThemes_Security::instance()->ithemes_init();
\MainWP_Child_Updraft_Plus_Backups::instance()->updraftplus_init();
\MainWP_Child_Back_Up_WordPress::instance()->init();
\MainWP_Child_WP_Rocket::instance()->init();
\MainWP_Child_Back_WP_Up::instance()->init();
\MainWP_Child_Back_Up_Buddy::instance();
\MainWP_Child_Wordfence::instance()->wordfence_init();
\MainWP_Child_Timecapsule::instance()->init();
MainWP_Child_Staging::instance()->init();
MainWP_Child_Branding::instance()->branding_init();
MainWP_Client_Report::instance()->creport_init();
\MainWP_Child_Pagespeed::instance()->init();
\MainWP_Child_Links_Checker::instance()->init();
\MainWP_Child_WPvivid_BackupRestore::instance()->init();
$this->init_extensions();
global $_wp_submenu_nopriv;
if ( null === $_wp_submenu_nopriv ) {
$_wp_submenu_nopriv = array(); // phpcs:ignore -- to fix warning.
}
$this->parse_callable_functions( $auth );
$this->parse_keyword_links();
}
private function parse_login_required() {
global $current_user;
$alter_login_required = false;
$username = rawurldecode( $_REQUEST['user'] );
if ( isset( $_REQUEST['alt_user'] ) && ! empty( $_REQUEST['alt_user'] ) ) {
$alter_login_required = $this->check_login_as( $_REQUEST['alt_user'] );
if ( $alter_login_required ) {
$username = rawurldecode( $_REQUEST['alt_user'] );
}
}
if ( is_user_logged_in() ) {
global $current_user;
if ( 10 !== $current_user->wp_user_level && ( ! isset( $current_user->user_level ) || 10 !== $current_user->user_level ) && ! current_user_can( 'level_10' ) ) {
do_action( 'wp_logout' );
}
}
$signature = rawurldecode( isset( $_REQUEST['mainwpsignature'] ) ? $_REQUEST['mainwpsignature'] : '' );
$file = '';
if ( isset( $_REQUEST['f'] ) ) {
$file = $_REQUEST['f'];
} elseif ( isset( $_REQUEST['file'] ) ) {
$file = $_REQUEST['file'];
} elseif ( isset( $_REQUEST['fdl'] ) ) {
$file = $_REQUEST['fdl'];
}
$auth = $this->auth( $signature, rawurldecode( ( isset( $_REQUEST['where'] ) ? $_REQUEST['where'] : $file ) ), isset( $_REQUEST['nonce'] ) ? $_REQUEST['nonce'] : '', isset( $_REQUEST['nossl'] ) ? $_REQUEST['nossl'] : 0 );
if ( ! $auth ) {
return;
}
if ( ! is_user_logged_in() || $username !== $current_user->user_login ) {
if ( ! $this->login( $username ) ) {
return;
}
global $current_user;
if ( 10 !== $current_user->wp_user_level && ( ! isset( $current_user->user_level ) || 10 !== $current_user->user_level ) && ! current_user_can( 'level_10' ) ) {
// if is not alternative admin login.
// it is connected admin login.
if ( ! $alter_login_required ) {
// log out if connected admin is not admin level 10.
do_action( 'wp_logout' );
return;
}
}
}
if ( isset( $_REQUEST['fdl'] ) ) {
if ( stristr( $_REQUEST['fdl'], '..' ) ) {
return;
}
$this->upload_file( $_REQUEST['fdl'], isset( $_REQUEST['foffset'] ) ? $_REQUEST['foffset'] : 0 );
exit;
}
$where = isset( $_REQUEST['where'] ) ? $_REQUEST['where'] : '';
if ( isset( $_POST['f'] ) || isset( $_POST['file'] ) ) {
$file = '';
if ( isset( $_POST['f'] ) ) {
$file = $_POST['f'];
} elseif ( isset( $_POST['file'] ) ) {
$file = $_POST['file'];
}
$where = 'admin.php?page=mainwp_child_tab&tab=restore-clone';
if ( '' === session_id() ) {
session_start();
}
$_SESSION['file'] = $file;
$_SESSION['size'] = $_POST['size'];
}
// to support open not wp-admin url.
$open_location = isset( $_REQUEST['open_location'] ) ? $_REQUEST['open_location'] : '';
if ( ! empty( $open_location ) ) {
$open_location = base64_decode( $open_location ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons.
$_vars = MainWP_Helper::parse_query( $open_location );
$_path = wp_parse_url( $open_location, PHP_URL_PATH );
if ( isset( $_vars['_mwpNoneName'] ) && isset( $_vars['_mwpNoneValue'] ) ) {
$_vars[ $_vars['_mwpNoneName'] ] = wp_create_nonce( $_vars['_mwpNoneValue'] );
unset( $_vars['_mwpNoneName'] );
unset( $_vars['_mwpNoneValue'] );
$open_url = '';
foreach ( $_vars as $key => $value ) {
$open_url .= $key . '=' . $value . '&';
}
$open_url = rtrim( $open_url, '&' );
$open_location = '/wp-admin/' . $_path . '?' . $open_url;
} else {
if ( strpos( $open_location, 'nonce=child_temp_nonce' ) !== false ) {
$open_location = str_replace( 'nonce=child_temp_nonce', 'nonce=' . wp_create_nonce( 'wp-ajax' ), $open_location );
}
}
wp_safe_redirect( site_url() . $open_location );
exit();
}
wp_safe_redirect( admin_url( $where ) );
exit();
}
private function parse_callable_functions( $auth ){
$callable = false;
$func_auth = false;
$callable_no_auth = false;
$func_no_auth = false;
if ( isset( $_POST['function'] ) ) {
// check to execute mainwp child's callable functions.
if ( isset( $_POST['function'] ) ) {
$func = $_POST['function'];
$callable = MainWP_Child_Callable::get_instance()->is_callable_function( $func );
if ( $callable ) {
$func_auth = $func;
@ -1328,17 +1226,7 @@ class MainWP_Child {
} 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' ) );
}
if ( 1 === (int) get_option( 'mainwpKeywordLinks' ) ) {
new MainWP_Keyword_Links();
if ( ! is_admin() ) {
add_filter( 'the_content', array( MainWP_Keyword_Links::instance(), 'filter_content' ), 100 );
}
MainWP_Keyword_Links::instance()->update_htaccess();
MainWP_Keyword_Links::instance()->redirect_cloak();
} elseif ( 'yes' === get_option( 'mainwp_keyword_links_htaccess_set' ) ) {
MainWP_Keyword_Links::clear_htaccess();
}
}
// Check to support login by alternative admin.
@ -1485,6 +1373,40 @@ class MainWP_Child {
MainWP_Child_Stats::get_instance()->get_site_stats( $information );
}
private function init_extensions() {
// Handle fatal errors for those init if needed.
\MainWP_Child_IThemes_Security::instance()->ithemes_init();
\MainWP_Child_Updraft_Plus_Backups::instance()->updraftplus_init();
\MainWP_Child_Back_Up_WordPress::instance()->init();
\MainWP_Child_WP_Rocket::instance()->init();
\MainWP_Child_Back_WP_Up::instance()->init();
\MainWP_Child_Back_Up_Buddy::instance();
\MainWP_Child_Wordfence::instance()->wordfence_init();
\MainWP_Child_Timecapsule::instance()->init();
MainWP_Child_Staging::instance()->init();
MainWP_Child_Branding::instance()->branding_init();
MainWP_Client_Report::instance()->creport_init();
\MainWP_Child_Pagespeed::instance()->init();
\MainWP_Child_Links_Checker::instance()->init();
\MainWP_Child_WPvivid_BackupRestore::instance()->init();
}
private function parse_keyword_links() {
if ( 1 === (int) get_option( 'mainwpKeywordLinks' ) ) {
new MainWP_Keyword_Links();
if ( ! is_admin() ) {
add_filter( 'the_content', array( MainWP_Keyword_Links::instance(), 'filter_content' ), 100 );
}
MainWP_Keyword_Links::instance()->update_htaccess();
MainWP_Keyword_Links::instance()->redirect_cloak();
} elseif ( 'yes' === get_option( 'mainwp_keyword_links_htaccess_set' ) ) {
MainWP_Keyword_Links::clear_htaccess();
}
}
public function maintenance_alert_404() {
if ( ! is_404() ) {

View file

@ -579,21 +579,9 @@ class MainWP_Client_Report {
public function get_section_loop_data( $records, $tokens, $section, $skip_records = array() ) {
$maintenance_details = array(
'revisions' => __( 'Delete all post revisions', 'mainwp-child' ),
'revisions_max' => __( 'Delete all post revisions, except for the last:', 'mainwp-child' ),
'autodraft' => __( 'Delete all auto draft posts', 'mainwp-child' ),
'trashpost' => __( 'Delete trash posts', 'mainwp-child' ),
'spam' => __( 'Delete spam comments', 'mainwp-child' ),
'pending' => __( 'Delete pending comments', 'mainwp-child' ),
'trashcomment' => __( 'Delete trash comments', 'mainwp-child' ),
'tags' => __( 'Delete tags with 0 posts associated', 'mainwp-child' ),
'categories' => __( 'Delete categories with 0 posts associated', 'mainwp-child' ),
'optimize' => __( 'Optimize database tables', 'mainwp-child' ),
);
$context = '';
$action = '';
$str_tmp = str_replace( array( '[', ']' ), '', $section );
$array_tmp = explode( '.', $str_tmp );
if ( is_array( $array_tmp ) ) {
@ -617,10 +605,27 @@ class MainWP_Client_Report {
$context = 'users'; // see class-connector-user.php.
}
}
return $this->get_section_loop_records( $records, $tokens, $connector, $context, $action, $skip_records );
}
$loops = array();
$loop_count = 0;
public function get_section_loop_records( $records, $tokens, $connector, $context, $action, $skip_records ) {
$maintenance_details = array(
'revisions' => __( 'Delete all post revisions', 'mainwp-child' ),
'revisions_max' => __( 'Delete all post revisions, except for the last:', 'mainwp-child' ),
'autodraft' => __( 'Delete all auto draft posts', 'mainwp-child' ),
'trashpost' => __( 'Delete trash posts', 'mainwp-child' ),
'spam' => __( 'Delete spam comments', 'mainwp-child' ),
'pending' => __( 'Delete pending comments', 'mainwp-child' ),
'trashcomment' => __( 'Delete trash comments', 'mainwp-child' ),
'tags' => __( 'Delete tags with 0 posts associated', 'mainwp-child' ),
'categories' => __( 'Delete categories with 0 posts associated', 'mainwp-child' ),
'optimize' => __( 'Optimize database tables', 'mainwp-child' ),
);
$loops = array();
$loop_count = 0;
foreach ( $records as $record ) {
if ( in_array( $record->ID, $skip_records ) ) {
@ -682,6 +687,7 @@ class MainWP_Client_Report {
$token_values = array();
foreach ( $tokens as $token ) {
$data = '';
$token_name = str_replace( array( '[', ']' ), '', $token );
$array_tmp = explode( '.', $token_name );
@ -710,162 +716,171 @@ class MainWP_Client_Report {
$data = 'roles';
}
switch ( $data ) {
case 'ID':
$tok_value = $record->ID;
break;
case 'date':
$tok_value = MainWP_Helper::format_date( MainWP_Helper::get_timestamp( strtotime( $record->created ) ) );
break;
case 'time':
$tok_value = MainWP_Helper::format_time( MainWP_Helper::get_timestamp( strtotime( $record->created ) ) );
break;
case 'area':
$data = 'sidebar_name';
$tok_value = $this->get_stream_meta_data( $record, $data );
break;
case 'name':
case 'version':
case 'old_version':
case 'new_version':
case 'display_name':
case 'roles':
if ( 'name' == $data ) {
if ( 'profiles' == $context ) {
$data = 'display_name';
}
}
$tok_value = $this->get_stream_meta_data( $record, $data );
break;
case 'title':
if ( 'comments' === $context ) {
$tok_value = $record->summary;
} else {
if ( 'page' === $context || 'post' === $context ) {
$data = 'post_title';
} elseif ( 'menus' === $record->connector ) {
$data = 'name';
}
$tok_value = $this->get_stream_meta_data( $record, $data );
}
break;
case 'author':
if ( 'comment' == $connector ) {
$data = 'user_name';
} else {
$data = 'user_meta';
}
$value = $this->get_stream_meta_data( $record, $data );
if ( empty( $value ) && 'comments' === $context ) {
$value = __( 'Guest', 'mainwp-child' );
}
// check compatibility with old meta data.
if ( empty( $value ) ) {
$value = $this->get_stream_meta_data( $record, 'author_meta' );
}
$tok_value = $value;
break;
case 'status':
case 'webtrust':
if ( 'sucuri_scan' === $context ) {
$scan_data = $this->get_stream_meta_data( $record, 'scan_data' );
if ( ! empty( $scan_data ) ) {
$scan_data = maybe_unserialize( base64_decode( $scan_data ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons.
if ( is_array( $scan_data ) ) {
$blacklisted = $scan_data['blacklisted'];
$malware_exists = $scan_data['malware_exists'];
$status = array();
if ( $blacklisted ) {
$status[] = __( 'Site Blacklisted', 'mainwp-child' ); }
if ( $malware_exists ) {
$status[] = __( 'Site With Warnings', 'mainwp-child' ); }
if ( 'status' == $data ) {
$tok_value = count( $status ) > 0 ? implode( ', ', $status ) : __( 'Verified Clear', 'mainwp-child' );
} elseif ( 'webtrust' == $data ) {
$tok_value = $blacklisted ? __( 'Site Blacklisted', 'mainwp-child' ) : __( 'Trusted', 'mainwp-child' );
}
}
} else {
$tok_value = $this->get_stream_meta_data( $record, $data );
}
} else {
$tok_value = $value;
}
break;
case 'details':
case 'result':
if ( 'mainwp_maintenance' === $context && 'details' == $data ) {
$meta_value = $this->get_stream_meta_data( $record, $data );
$meta_value = explode( ',', $meta_value );
$details = array();
if ( is_array( $meta_value ) ) {
foreach ( $meta_value as $mt ) {
if ( isset( $maintenance_details[ $mt ] ) ) {
if ( 'revisions_max' == $mt ) {
$max_revisions = $this->get_stream_meta_data( $record, 'revisions' );
$dtl = $maintenance_details['revisions_max'] . ' ' . $max_revisions;
} else {
$dtl = $maintenance_details[ $mt ];
}
$details[] = $dtl;
}
}
}
$tok_value = implode( ', ', $details );
} elseif ( 'wordfence_scan' === $context || 'mainwp_maintenance' === $context ) {
$meta_value = $this->get_stream_meta_data( $record, $data );
if ( 'wordfence_scan' === $context && 'result' == $data ) {
// SUM_FINAL:Scan complete. You have xxx new issues to fix. See below.
// SUM_FINAL:Scan complete. Congratulations, no new problems found.
if ( stripos( $meta_value, 'Congratulations' ) ) {
$meta_value = 'No issues detected';
} elseif ( stripos( $meta_value, 'You have' ) ) {
$meta_value = 'Issues Detected';
} else {
$meta_value = '';
}
}
$tok_value = $meta_value;
}
break;
case 'type':
if ( 'backups' === $context ) {
$tok_value = $this->get_stream_meta_data( $record, $data );
} else {
$tok_value = $token;
}
break;
default:
$tok_value = 'N/A';
break;
}
$tok_value = $this->get_section_loop_token_value( $record, $data, $context, $token );
$token_values[ $token ] = $tok_value;
if ( empty( $tok_value ) ) {
if ( defined( 'MAINWP_CHILD_DEBUG' ) && MAINWP_CHILD_DEBUG === true ) {
error_log( 'MainWP Child Report:: skip empty value :: token :: ' . $token . ' :: record :: ' . print_r( $record, true ) ); // phpcs:ignore -- debug mode only.
}
$msg = 'MainWP Child Report:: skip empty value :: token :: ' . $token . ' :: record :: ' . print_r( $record, true ); // phpcs:ignore -- debug mode only.
MainWP_Helper::log_debug( $msg );
}
}
if ( ! empty( $token_values ) ) {
$loops[ $loop_count ] = $token_values;
$loop_count ++;
}
}
return $loops;
}
public function get_section_loop_token_value( $record, $data, $context, $token ) {
$tok_value = '';
switch ( $data ) {
case 'ID':
$tok_value = $record->ID;
break;
case 'date':
$tok_value = MainWP_Helper::format_date( MainWP_Helper::get_timestamp( strtotime( $record->created ) ) );
break;
case 'time':
$tok_value = MainWP_Helper::format_time( MainWP_Helper::get_timestamp( strtotime( $record->created ) ) );
break;
case 'area':
$data = 'sidebar_name';
$tok_value = $this->get_stream_meta_data( $record, $data );
break;
case 'name':
case 'version':
case 'old_version':
case 'new_version':
case 'display_name':
case 'roles':
if ( 'name' == $data ) {
if ( 'profiles' == $context ) {
$data = 'display_name';
}
}
$tok_value = $this->get_stream_meta_data( $record, $data );
break;
case 'title':
if ( 'comments' === $context ) {
$tok_value = $record->summary;
} else {
if ( 'page' === $context || 'post' === $context ) {
$data = 'post_title';
} elseif ( 'menus' === $record->connector ) {
$data = 'name';
}
$tok_value = $this->get_stream_meta_data( $record, $data );
}
break;
case 'author':
if ( 'comment' == $connector ) {
$data = 'user_name';
} else {
$data = 'user_meta';
}
$value = $this->get_stream_meta_data( $record, $data );
if ( empty( $value ) && 'comments' === $context ) {
$value = __( 'Guest', 'mainwp-child' );
}
// check compatibility with old meta data.
if ( empty( $value ) ) {
$value = $this->get_stream_meta_data( $record, 'author_meta' );
}
$tok_value = $value;
break;
case 'status':
case 'webtrust':
if ( 'sucuri_scan' === $context ) {
$scan_data = $this->get_stream_meta_data( $record, 'scan_data' );
if ( ! empty( $scan_data ) ) {
$scan_data = maybe_unserialize( base64_decode( $scan_data ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons.
if ( is_array( $scan_data ) ) {
$blacklisted = $scan_data['blacklisted'];
$malware_exists = $scan_data['malware_exists'];
$status = array();
if ( $blacklisted ) {
$status[] = __( 'Site Blacklisted', 'mainwp-child' ); }
if ( $malware_exists ) {
$status[] = __( 'Site With Warnings', 'mainwp-child' ); }
if ( 'status' == $data ) {
$tok_value = count( $status ) > 0 ? implode( ', ', $status ) : __( 'Verified Clear', 'mainwp-child' );
} elseif ( 'webtrust' == $data ) {
$tok_value = $blacklisted ? __( 'Site Blacklisted', 'mainwp-child' ) : __( 'Trusted', 'mainwp-child' );
}
}
} else {
$tok_value = $this->get_stream_meta_data( $record, $data );
}
} else {
$tok_value = $value;
}
break;
case 'details':
case 'result':
if ( 'mainwp_maintenance' === $context && 'details' == $data ) {
$meta_value = $this->get_stream_meta_data( $record, $data );
$meta_value = explode( ',', $meta_value );
$details = array();
if ( is_array( $meta_value ) ) {
foreach ( $meta_value as $mt ) {
if ( isset( $maintenance_details[ $mt ] ) ) {
if ( 'revisions_max' == $mt ) {
$max_revisions = $this->get_stream_meta_data( $record, 'revisions' );
$dtl = $maintenance_details['revisions_max'] . ' ' . $max_revisions;
} else {
$dtl = $maintenance_details[ $mt ];
}
$details[] = $dtl;
}
}
}
$tok_value = implode( ', ', $details );
} elseif ( 'wordfence_scan' === $context || 'mainwp_maintenance' === $context ) {
$meta_value = $this->get_stream_meta_data( $record, $data );
if ( 'wordfence_scan' === $context && 'result' == $data ) {
// SUM_FINAL:Scan complete. You have xxx new issues to fix. See below.
// SUM_FINAL:Scan complete. Congratulations, no new problems found.
if ( stripos( $meta_value, 'Congratulations' ) ) {
$meta_value = 'No issues detected';
} elseif ( stripos( $meta_value, 'You have' ) ) {
$meta_value = 'Issues Detected';
} else {
$meta_value = '';
}
}
$tok_value = $meta_value;
}
break;
case 'type':
if ( 'backups' === $context ) {
$tok_value = $this->get_stream_meta_data( $record, $data );
} else {
$tok_value = $token;
}
break;
default:
$tok_value = 'N/A';
break;
}
return $loops;
return $tok_value;
}
public function get_stream_meta_data( $record, $data ) {

View file

@ -137,7 +137,7 @@ class MainWP_Clone_Install {
if ( false === $configContents ) {
throw new \Exception( __( 'Cant read configuration file from the backup.', 'mainwp-child' ) );
}
if ( defined( 'MAINWP_DEBUG' ) && MAINWP_DEBUG ) {
if ( defined( 'MAINWP_CHILD_DEBUG' ) && MAINWP_CHILD_DEBUG ) {
$this->config = wp_json_decode( $configContents );
} else {
$this->config = maybe_unserialize( base64_decode( $configContents ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- safe.
@ -602,4 +602,128 @@ class MainWP_Clone_Install {
return $data;
}
public function request_clone_funct() {
if ( ! isset( $_REQUEST['key'] ) ) {
return;
}
if ( ! isset( $_REQUEST['f'] ) || ( '' === $_REQUEST['f'] ) ) {
return;
}
if ( ! $this->is_valid_auth( $_REQUEST['key'] ) ) {
return;
}
global $mainWPChild;
if ( 'dl' === $_REQUEST['cloneFunc'] ) {
$mainWPChild->upload_file( $_REQUEST['f'] );
exit;
} elseif ( 'deleteCloneBackup' === $_POST['cloneFunc'] ) {
$dirs = MainWP_Helper::get_mainwp_dir( 'backup' );
$backupdir = $dirs[0];
$result = glob( $backupdir . $_POST['f'] );
if ( 0 === count( $result ) ) {
return;
}
unlink( $result[0] );
mainwp_child_helper()->write( array( 'result' => 'ok' ) );
} elseif ( 'createCloneBackupPoll' === $_POST['cloneFunc'] ) {
$dirs = MainWP_Helper::get_mainwp_dir( 'backup' );
$backupdir = $dirs[0];
$result = glob( $backupdir . 'backup-' . $_POST['f'] . '-*' );
$archiveFile = false;
foreach ( $result as $file ) {
if ( MainWP_Helper::is_archive( $file, 'backup-' . $_POST['f'] . '-' ) ) {
$archiveFile = $file;
break;
}
}
if ( false === $archiveFile ) {
return;
}
mainwp_child_helper()->write( array( 'size' => filesize( $archiveFile ) ) );
} elseif ( 'createCloneBackup' === $_POST['cloneFunc'] ) {
MainWP_Helper::end_session();
$files = glob( WP_CONTENT_DIR . '/dbBackup*.sql' );
foreach ( $files as $file ) {
unlink( $file );
}
if ( file_exists( ABSPATH . 'clone/config.txt' ) ) {
unlink( ABSPATH . 'clone/config.txt' );
}
if ( MainWP_Helper::is_dir_empty( ABSPATH . 'clone' ) ) {
rmdir( ABSPATH . 'clone' );
}
$wpversion = $_POST['wpversion'];
global $wp_version;
$includeCoreFiles = ( $wpversion !== $wp_version );
$excludes = ( isset( $_POST['exclude'] ) ? explode( ',', $_POST['exclude'] ) : array() );
$excludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/uploads/mainwp';
$uploadDir = MainWP_Helper::get_mainwp_dir();
$uploadDir = $uploadDir[0];
$excludes[] = str_replace( ABSPATH, '', $uploadDir );
$excludes[] = str_replace( ABSPATH, '', WP_CONTENT_DIR ) . '/object-cache.php';
if ( version_compare( phpversion(), '5.3.0' ) >= 0 || ! ini_get( 'safe_mode' ) ) {
set_time_limit( 6000 );
}
$newExcludes = array();
foreach ( $excludes as $exclude ) {
$newExcludes[] = rtrim( $exclude, '/' );
}
$method = ( ! isset( $_POST['zipmethod'] ) ? 'tar.gz' : $_POST['zipmethod'] );
if ( 'tar.gz' === $method && ! function_exists( 'gzopen' ) ) {
$method = 'zip';
}
$res = MainWP_Backup::get()->create_full_backup( $newExcludes, ( isset( $_POST['f'] ) ? $_POST['f'] : $_POST['file'] ), true, $includeCoreFiles, 0, false, false, false, false, $method );
if ( ! $res ) {
$information['backup'] = false;
} else {
$information['backup'] = $res['file'];
$information['size'] = $res['filesize'];
}
$plugins = array();
$dir = WP_CONTENT_DIR . '/plugins/';
$fh = opendir( $dir );
$entry = readdir( $fh );
while ( $entry ) {
if ( ! is_dir( $dir . $entry ) ) {
continue;
}
if ( ( '.' === $entry ) || ( '..' === $entry ) ) {
continue;
}
$plugins[] = $entry;
}
closedir( $fh );
$information['plugins'] = $plugins;
$themes = array();
$dir = WP_CONTENT_DIR . '/themes/';
$fh = opendir( $dir );
while ( $entry = readdir( $fh ) ) {
if ( ! is_dir( $dir . $entry ) ) {
continue;
}
if ( ( '.' === $entry ) || ( '..' === $entry ) ) {
continue;
}
$themes[] = $entry;
}
closedir( $fh );
$information['themes'] = $themes;
mainwp_child_helper()->write( $information );
}
return true;
}
}

View file

@ -7,7 +7,7 @@ class MainWP_Debug {
* @param $mainWPChild MainWP_Child
*/
public static function process( &$mainWPChild ) {
if ( ! isset( $_GET['mainwpdebug'] ) || ! defined( 'MAINWP_DEBUG' ) || ( MAINWP_DEBUG !== true ) ) {
if ( ! isset( $_GET['mainwpdebug'] ) || ! defined( 'MAINWP_CHILD_DEBUG' ) || ( MAINWP_CHILD_DEBUG !== true ) ) {
return;
}

View file

@ -374,498 +374,6 @@ class MainWP_Helper {
return array( 'path' => $full_file_name );
}
public static function create_post( $new_post, $post_custom, $post_category, $post_featured_image, $upload_dir, $post_tags, $others = array() ) {
global $current_user;
/**
* Hook: `mainwp_before_post_update`
*
* Runs before creating or updating a post via MainWP dashboard.
*
* @param array $new_post Post data array.
* @param array $post_custom Post custom meta data.
* @param string $post_category Post categories.
* @param string $post_tags Post tags.
*/
do_action( 'mainwp_before_post_update', $new_post, $post_custom, $post_category, $post_tags );
// Options fields.
$wprocket_fields = array(
'lazyload',
'lazyload_iframes',
'minify_html',
'minify_css',
'minify_js',
'cdn',
'async_css',
'defer_all_js',
);
$wprocket_activated = false;
if ( \MainWP_Child_WP_Rocket::instance()->is_activated() ) {
if ( function_exists( 'get_rocket_option' ) ) {
$wprocket_activated = true;
foreach ( $wprocket_fields as $field ) {
if ( ! isset( $post_custom[ '_rocket_exclude_' . $field ] ) ) {
if ( ! get_rocket_option( $field ) ) {
$post_custom[ '_rocket_exclude_' . $field ] = array( true );
}
}
}
}
}
if ( ! $wprocket_activated ) {
foreach ( $wprocket_fields as $field ) {
if ( isset( $post_custom[ '_rocket_exclude_' . $field ] ) ) {
unset( $post_custom[ '_rocket_exclude_' . $field ] );
}
}
}
// current user may be connected admin or alternative admin.
$current_uid = $current_user->ID;
// Set up a new post (adding addition information).
$post_author = isset( $new_post['post_author'] ) ? $new_post['post_author'] : $current_uid;
if ( isset( $new_post['custom_post_author'] ) && ! empty( $new_post['custom_post_author'] ) ) {
$_author = get_user_by( 'login', $new_post['custom_post_author'] );
if ( ! empty( $_author ) ) {
$new_post['post_author'] = $_author->ID;
} else {
$new_post['post_author'] = $current_uid;
}
unset( $new_post['custom_post_author'] );
}
$post_author = ! empty( $post_author ) ? $post_author : $current_uid;
$new_post['post_author'] = $post_author;
$terms = isset( $new_post['_ezin_post_category'] ) ? $new_post['_ezin_post_category'] : false;
unset( $new_post['_ezin_post_category'] );
$is_post_plus = isset( $post_custom['_mainwp_post_plus'] ) ? true : false;
$wp_error = null;
if ( $is_post_plus ) {
if ( isset( $new_post['post_date_gmt'] ) && ! empty( $new_post['post_date_gmt'] ) && '0000-00-00 00:00:00' != $new_post['post_date_gmt'] ) {
$post_date_timestamp = strtotime( $new_post['post_date_gmt'] ) + get_option( 'gmt_offset' ) * 60 * 60;
$new_post['post_date'] = date( 'Y-m-d H:i:s', $post_date_timestamp ); // phpcs:ignore -- local time.
}
}
$wpr_options = isset( $_POST['wpr_options'] ) ? $_POST['wpr_options'] : array();
$edit_post_id = 0;
if ( isset( $post_custom['_mainwp_edit_post_id'] ) && $post_custom['_mainwp_edit_post_id'] ) {
$edit_post_id = current( $post_custom['_mainwp_edit_post_id'] );
} elseif ( isset( $new_post['ID'] ) && $new_post['ID'] ) {
$edit_post_id = $new_post['ID'];
}
require_once ABSPATH . 'wp-admin/includes/post.php';
if ( $edit_post_id ) {
$user_id = wp_check_post_lock( $edit_post_id );
if ( $user_id ) {
$user = get_userdata( $user_id );
$error = sprintf( __( 'This content is currently locked. %s is currently editing.', 'mainwp-child' ), $user->display_name );
return array( 'error' => $error );
}
}
$check_image_existed = false;
if ( $edit_post_id ) {
$check_image_existed = true; // if editing post then will check if image existed.
}
// Search for all the images added to the new post. Some images have a href tag to click to navigate to the image.. we need to replace this too.
$foundMatches = preg_match_all( '/(<a[^>]+href=\"(.*?)\"[^>]*>)?(<img[^>\/]*src=\"((.*?)(png|gif|jpg|jpeg))\")/ix', $new_post['post_content'], $matches, PREG_SET_ORDER );
if ( $foundMatches > 0 ) {
// We found images, now to download them so we can start balbal.
foreach ( $matches as $match ) {
$hrefLink = $match[2];
$imgUrl = $match[4];
if ( ! isset( $upload_dir['baseurl'] ) || ( 0 !== strripos( $imgUrl, $upload_dir['baseurl'] ) ) ) {
continue;
}
if ( preg_match( '/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', $imgUrl, $imgMatches ) ) {
$search = $imgMatches[0];
$replace = '.' . $match[6];
$originalImgUrl = str_replace( $search, $replace, $imgUrl );
} else {
$originalImgUrl = $imgUrl;
}
try {
$downloadfile = self::upload_image( $originalImgUrl, array(), $check_image_existed );
$localUrl = $downloadfile['url'];
$linkToReplaceWith = dirname( $localUrl );
if ( '' !== $hrefLink ) {
$server = get_option( 'mainwp_child_server' );
$serverHost = wp_parse_url( $server, PHP_URL_HOST );
if ( ! empty( $serverHost ) && strpos( $hrefLink, $serverHost ) !== false ) {
$serverHref = 'href="' . $serverHost;
$replaceServerHref = 'href="' . wp_parse_url( $localUrl, PHP_URL_SCHEME ) . '://' . wp_parse_url( $localUrl, PHP_URL_HOST );
$new_post['post_content'] = str_replace( $serverHref, $replaceServerHref, $new_post['post_content'] );
}
}
$lnkToReplace = dirname( $imgUrl );
if ( 'http:' !== $lnkToReplace && 'https:' !== $lnkToReplace ) {
$new_post['post_content'] = str_replace( $lnkToReplace, $linkToReplaceWith, $new_post['post_content'] );
}
} catch ( \Exception $e ) {
self::log_debug( $e->getMessage() );
}
}
}
if ( has_shortcode( $new_post['post_content'], 'gallery' ) ) {
if ( preg_match_all( '/\[gallery[^\]]+ids=\"(.*?)\"[^\]]*\]/ix', $new_post['post_content'], $matches, PREG_SET_ORDER ) ) {
$replaceAttachedIds = array();
if ( isset( $_POST['post_gallery_images'] ) ) {
$post_gallery_images = unserialize( base64_decode( $_POST['post_gallery_images'] ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons.
if ( is_array( $post_gallery_images ) ) {
foreach ( $post_gallery_images as $gallery ) {
if ( isset( $gallery['src'] ) ) {
try {
$upload = self::upload_image( $gallery['src'], $gallery ); // Upload image to WP.
if ( null !== $upload ) {
$replaceAttachedIds[ $gallery['id'] ] = $upload['id'];
}
} catch ( \Exception $e ) {
// ok!
}
}
}
}
}
if ( count( $replaceAttachedIds ) > 0 ) {
foreach ( $matches as $match ) {
$idsToReplace = $match[1];
$idsToReplaceWith = '';
$originalIds = explode( ',', $idsToReplace );
foreach ( $originalIds as $attached_id ) {
if ( ! empty( $originalIds ) && isset( $replaceAttachedIds[ $attached_id ] ) ) {
$idsToReplaceWith .= $replaceAttachedIds[ $attached_id ] . ',';
}
}
$idsToReplaceWith = rtrim( $idsToReplaceWith, ',' );
if ( ! empty( $idsToReplaceWith ) ) {
$new_post['post_content'] = str_replace( '"' . $idsToReplace . '"', '"' . $idsToReplaceWith . '"', $new_post['post_content'] );
}
}
}
}
}
if ( $is_post_plus ) {
$random_publish_date = isset( $post_custom['_saved_draft_random_publish_date'] ) ? $post_custom['_saved_draft_random_publish_date'] : false;
$random_publish_date = is_array( $random_publish_date ) ? current( $random_publish_date ) : null;
if ( ! empty( $random_publish_date ) ) {
$random_date_from = isset( $post_custom['_saved_draft_publish_date_from'] ) ? $post_custom['_saved_draft_publish_date_from'] : 0;
$random_date_from = is_array( $random_date_from ) ? current( $random_date_from ) : 0;
$random_date_to = isset( $post_custom['_saved_draft_publish_date_to'] ) ? $post_custom['_saved_draft_publish_date_to'] : 0;
$random_date_to = is_array( $random_date_to ) ? current( $random_date_to ) : 0;
$now = time();
if ( empty( $random_date_from ) ) {
$random_date_from = $now;
}
if ( empty( $random_date_to ) ) {
$random_date_to = $now;
}
if ( $random_date_from === $now && $random_date_from === $random_date_to ) {
$random_date_to = $now + 7 * 24 * 3600;
}
if ( $random_date_from > $random_date_to ) {
$tmp = $random_date_from;
$random_date_from = $random_date_to;
$random_date_to = $tmp;
}
$random_timestamp = wp_rand( $random_date_from, $random_date_to );
$new_post['post_date'] = date( 'Y-m-d H:i:s', $random_timestamp ); // phpcs:ignore -- local time.
}
}
if ( isset( $post_tags ) && '' !== $post_tags ) {
$new_post['tags_input'] = $post_tags;
}
// Save the post to the WP.
remove_filter( 'content_save_pre', 'wp_filter_post_kses' ); // to fix brake scripts or html.
$post_status = $new_post['post_status'];
$new_post['post_status'] = 'auto-draft'; // child reports: to logging as created post.
// update post.
if ( $edit_post_id ) {
// check if post existed.
$current_post = get_post( $edit_post_id );
if ( $current_post && ( ( ! isset( $new_post['post_type'] ) && 'post' == $current_post->post_type ) || ( isset( $new_post['post_type'] ) && $new_post['post_type'] == $current_post->post_type ) ) ) {
$new_post['ID'] = $edit_post_id;
}
$new_post['post_status'] = $post_status; // child reports: to logging as update post.
}
$new_post_id = wp_insert_post( $new_post, $wp_error );
// Show errors if something went wrong.
if ( is_wp_error( $wp_error ) ) {
return $wp_error->get_error_message();
}
if ( empty( $new_post_id ) ) {
return array( 'error' => 'Empty post id' );
}
if ( ! $edit_post_id ) {
wp_update_post(
array(
'ID' => $new_post_id,
'post_status' => $post_status,
)
);
}
if ( ! empty( $terms ) ) {
wp_set_object_terms( $new_post_id, array_map( intval, $terms ), 'category' );
}
$permalink = get_permalink( $new_post_id );
$seo_ext_activated = false;
if ( class_exists( 'WPSEO_Meta' ) && class_exists( 'WPSEO_Admin' ) ) {
$seo_ext_activated = true;
}
// Set custom fields.
$not_allowed = array(
'_slug',
'_tags',
'_edit_lock',
'_selected_sites',
'_selected_groups',
'_selected_by',
'_categories',
'_edit_last',
'_sticky',
'_mainwp_post_dripper',
'_bulkpost_do_not_del',
'_mainwp_spin_me',
);
$not_allowed[] = '_mainwp_boilerplate_sites_posts';
$not_allowed[] = '_ezine_post_keyword';
$not_allowed[] = '_ezine_post_display_sig';
$not_allowed[] = '_ezine_post_remove_link';
$not_allowed[] = '_ezine_post_grab_image';
$not_allowed[] = '_ezine_post_grab_image_placement';
$not_allowed[] = '_ezine_post_template_id';
$not_allowed[] = '_mainwp_post_plus';
$not_allowed[] = '_saved_as_draft';
$not_allowed[] = '_saved_draft_categories';
$not_allowed[] = '_saved_draft_tags';
$not_allowed[] = '_saved_draft_random_privelege';
$not_allowed[] = '_saved_draft_random_category';
$not_allowed[] = '_saved_draft_random_publish_date';
$not_allowed[] = '_saved_draft_publish_date_from';
$not_allowed[] = '_saved_draft_publish_date_to';
$not_allowed[] = '_post_to_only_existing_categories';
$not_allowed[] = '_mainwp_edit_post_site_id';
$not_allowed[] = '_mainwp_edit_post_id';
$not_allowed[] = '_edit_post_status';
$post_to_only_existing_categories = false;
if ( is_array( $post_custom ) ) {
foreach ( $post_custom as $meta_key => $meta_values ) {
if ( ! in_array( $meta_key, $not_allowed ) ) {
foreach ( $meta_values as $meta_value ) {
if ( 0 === strpos( $meta_key, '_mainwp_spinner_' ) ) {
continue;
}
if ( ! $seo_ext_activated ) {
// if WordPress SEO plugin is not activated do not save yoast post meta.
if ( false === strpos( $meta_key, '_yoast_wpseo_' ) ) {
update_post_meta( $new_post_id, $meta_key, $meta_value );
}
} else {
update_post_meta( $new_post_id, $meta_key, $meta_value );
}
}
} elseif ( '_sticky' === $meta_key ) {
foreach ( $meta_values as $meta_value ) {
if ( 'sticky' === base64_decode( $meta_value ) ) { // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons.
stick_post( $new_post_id );
}
}
} elseif ( '_post_to_only_existing_categories' === $meta_key ) {
if ( isset( $meta_values[0] ) && $meta_values[0] ) {
$post_to_only_existing_categories = true;
}
}
}
}
// yoast seo extension.
if ( $seo_ext_activated ) {
$_seo_opengraph_image = isset( $post_custom[ WPSEO_Meta::$meta_prefix . 'opengraph-image' ] ) ? $post_custom[ WPSEO_Meta::$meta_prefix . 'opengraph-image' ] : array();
$_seo_opengraph_image = current( $_seo_opengraph_image );
$_server_domain = '';
$_server = get_option( 'mainwp_child_server' );
if ( preg_match( '/(https?:\/\/[^\/]+\/).+/', $_server, $matchs ) ) {
$_server_domain = isset( $matchs[1] ) ? $matchs[1] : '';
}
// upload image if it on the server.
if ( ! empty( $_seo_opengraph_image ) && false !== strpos( $_seo_opengraph_image, $_server_domain ) ) {
try {
$upload = self::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!
}
} catch ( \Exception $e ) {
// ok!
}
}
}
// If categories exist, create them (second parameter of wp_create_categories adds the categories to the post).
include_once ABSPATH . 'wp-admin/includes/taxonomy.php'; // Contains wp_create_categories.
if ( isset( $post_category ) && '' !== $post_category ) {
$categories = explode( ',', $post_category );
if ( count( $categories ) > 0 ) {
if ( ! $post_to_only_existing_categories ) {
$post_category = wp_create_categories( $categories, $new_post_id );
} else {
$cat_ids = array();
foreach ( $categories as $cat ) {
$id = category_exists( $cat );
if ( $id ) {
$cat_ids[] = $id;
}
}
if ( count( $cat_ids ) > 0 ) {
wp_set_post_categories( $new_post_id, $cat_ids );
}
}
}
}
$featured_image_exist = false;
// If featured image exists - set it.
if ( null !== $post_featured_image ) {
try {
$upload = self::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;
if ( isset( $others['featured_image_data'] ) ) {
$_image_data = $others['featured_image_data'];
update_post_meta( $upload['id'], '_wp_attachment_image_alt', $_image_data['alt'] );
wp_update_post(
array(
'ID' => $upload['id'],
'post_excerpt' => $_image_data['caption'],
'post_content' => $_image_data['description'],
'post_title' => $_image_data['title'],
)
);
}
}
} catch ( \Exception $e ) {
// ok!
}
}
if ( ! $featured_image_exist ) {
delete_post_meta( $new_post_id, '_thumbnail_id' );
}
// post plus extension process.
if ( $is_post_plus ) {
$random_privelege = isset( $post_custom['_saved_draft_random_privelege'] ) ? $post_custom['_saved_draft_random_privelege'] : null;
$random_privelege = is_array( $random_privelege ) ? current( $random_privelege ) : null;
$random_privelege_base = base64_decode( $random_privelege ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons.
$random_privelege = maybe_unserialize( $random_privelege_base );
if ( is_array( $random_privelege ) && count( $random_privelege ) > 0 ) {
$random_post_authors = array();
foreach ( $random_privelege as $role ) {
$users = get_users( array( 'role' => $role ) );
foreach ( $users as $user ) {
$random_post_authors[] = $user->ID;
}
}
if ( count( $random_post_authors ) > 0 ) {
shuffle( $random_post_authors );
$key = array_rand( $random_post_authors );
wp_update_post(
array(
'ID' => $new_post_id,
'post_author' => $random_post_authors[ $key ],
)
);
}
}
$random_category = isset( $post_custom['_saved_draft_random_category'] ) ? $post_custom['_saved_draft_random_category'] : false;
$random_category = is_array( $random_category ) ? current( $random_category ) : null;
if ( ! empty( $random_category ) ) {
$cats = get_categories(
array(
'type' => 'post',
'hide_empty' => 0,
)
);
$random_cats = array();
if ( is_array( $cats ) ) {
foreach ( $cats as $cat ) {
$random_cats[] = $cat->term_id;
}
}
if ( count( $random_cats ) > 0 ) {
shuffle( $random_cats );
$key = array_rand( $random_cats );
wp_set_post_categories( $new_post_id, array( $random_cats[ $key ] ), false );
}
}
} // end of post plus.
// to support custom post author.
$custom_post_author = apply_filters( 'mainwp_create_post_custom_author', false, $new_post_id );
if ( ! empty( $custom_post_author ) ) {
wp_update_post(
array(
'ID' => $new_post_id,
'post_author' => $custom_post_author,
)
);
}
// unlock if edit post.
if ( $edit_post_id ) {
update_post_meta( $edit_post_id, '_edit_lock', '' );
}
$ret['success'] = true;
$ret['link'] = $permalink;
$ret['added_id'] = $new_post_id;
return $ret;
}
public static function get_mainwp_dir( $what = null, $dieOnError = true ) {
$upload_dir = wp_upload_dir();
$dir = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'mainwp' . DIRECTORY_SEPARATOR;
@ -1789,7 +1297,7 @@ class MainWP_Helper {
}
public static function log_debug( $msg ) {
if ( defined( 'MAINWP_DEBUG' ) && MAINWP_DEBUG ) {
if ( defined( 'MAINWP_CHILD_DEBUG' ) && MAINWP_CHILD_DEBUG ) {
error_log( $msg ); // phpcs:ignore -- debug mode only.
}
}

View file

@ -241,7 +241,7 @@ class Tar_Archiver {
}
closedir( $fh );
if ( defined( 'MAINWP_DEBUG' ) && MAINWP_DEBUG ) {
if ( defined( 'MAINWP_CHILD_DEBUG' ) && MAINWP_CHILD_DEBUG ) {
$string = wp_json_encode(
array(
'siteurl' => get_option( 'siteurl' ),

View file

@ -10,7 +10,7 @@
*/
require_once ABSPATH . 'wp-includes' . DIRECTORY_SEPARATOR . 'version.php'; // Version information from WordPress.
define( 'MAINWP_DEBUG', true );
define( 'MAINWP_CHILD_DEBUG', true );
if ( ! defined( 'MAINWP_CHILD_FILE' ) ) {
define( 'MAINWP_CHILD_FILE', __FILE__ );