mainwp-child/class/class-mainwp-child.php

401 lines
11 KiB
PHP
Raw Normal View History

2015-10-15 22:52:37 +10:00
<?php
2020-05-21 20:06:30 -04:00
/**
* MainWP Child
*
* This file handles all of the task that deal with the
* MainWP Child Plugin itself.
*/
2020-05-28 02:06:59 -04:00
2020-05-05 20:13:38 +07:00
namespace MainWP\Child;
2020-05-07 19:34:36 +07:00
// phpcs:disable
2020-05-13 01:18:02 +07:00
if ( defined( 'MAINWP_CHILD_DEBUG' ) && MAINWP_CHILD_DEBUG === true ) {
2020-04-23 19:16:35 +02:00
error_reporting( E_ALL );
ini_set( 'display_errors', true );
ini_set( 'display_startup_errors', true );
} else {
2020-04-07 16:53:05 +02:00
if ( isset( $_REQUEST['mainwpsignature'] ) ) {
2020-04-23 19:16:35 +02:00
ini_set( 'display_errors', false );
error_reporting( 0 );
2020-03-26 19:51:58 +00:00
}
}
2020-05-07 19:34:36 +07:00
// phpcs:enable
2015-10-15 22:52:37 +10:00
2020-05-13 18:48:37 +07:00
require_once ABSPATH . '/wp-admin/includes/file.php';
require_once ABSPATH . '/wp-admin/includes/plugin.php';
2015-10-15 22:52:37 +10:00
2020-05-21 20:06:30 -04:00
/**
* Class MainWP_Child
*
2020-05-21 20:06:30 -04:00
* @package MainWP\Child
*/
2015-10-15 22:52:37 +10:00
class MainWP_Child {
/**
* @static
* @var string MainWP Child Plugin Version.
*/
public static $version = '4.0.7.1';
/**
* @var string Update Version.
*/
private $update_version = '1.5';
/**
* @var string MainWP Child Plugin slug.
*/
public $plugin_slug;
/**
* @var string MainWP Child Plugin directory.
*/
private $plugin_dir;
/**
* MainWP_Child constructor.
*
* @param $plugin_file MainWP Child Plugin file.
*/
public function __construct( $plugin_file ) {
2015-10-15 22:52:37 +10:00
$this->update();
$this->load_all_options();
$this->plugin_dir = dirname( $plugin_file );
$this->plugin_slug = plugin_basename( $plugin_file );
2015-10-15 22:52:37 +10:00
add_action( 'template_redirect', array( $this, 'template_redirect' ) );
2020-05-20 01:07:47 +07:00
add_action( 'init', array( &$this, 'init_check_login' ), 1 );
add_action( 'init', array( &$this, 'parse_init' ), 9999 );
add_action( 'init', array( &$this, 'localization' ), 33 );
2020-05-15 01:04:08 +07:00
add_action( 'admin_init', array( &$this, 'admin_init' ) );
2020-05-11 20:30:56 +07:00
add_action( 'pre_current_active_plugins', array( MainWP_Child_Updates::get_instance(), 'detect_premium_themesplugins_updates' ) ); // to support detect premium plugins update.
add_action( 'core_upgrade_preamble', array( MainWP_Child_Updates::get_instance(), 'detect_premium_themesplugins_updates' ) ); // to support detect premium themes.
2020-05-15 01:04:08 +07:00
MainWP_Pages::get_instance()->init();
2015-10-15 22:52:37 +10:00
if ( is_admin() ) {
MainWP_Helper::update_option( 'mainwp_child_plugin_version', self::$version, 'yes' );
2015-10-15 22:52:37 +10:00
}
2020-05-14 00:57:25 +07:00
MainWP_Connect::instance()->check_other_auth();
2015-10-15 22:52:37 +10:00
2020-05-15 01:04:08 +07:00
// init functions.
2020-05-14 17:26:44 +07:00
MainWP_Clone::get()->init();
MainWP_Child_Server_Information::init();
MainWP_Client_Report::instance()->init();
MainWP_Child_Plugins_Check::instance();
MainWP_Child_Themes_Check::instance();
MainWP_Utility::instance()->run_saved_snippets();
2015-10-15 22:52:37 +10:00
if ( ! get_option( 'mainwp_child_pubkey' ) ) {
MainWP_Child_Branding::instance()->save_branding_options( 'branding_disconnected', 'yes' );
2015-10-15 22:52:37 +10:00
}
2016-03-03 20:28:07 +01:00
if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
2020-04-07 16:53:05 +02:00
if ( isset( $_GET['mainwp_child_run'] ) && ! empty( $_GET['mainwp_child_run'] ) ) {
2020-05-14 00:57:25 +07:00
add_action( 'init', array( MainWP_Utility::get_class_name(), 'cron_active' ), PHP_INT_MAX );
2016-03-03 20:28:07 +01:00
}
}
2015-10-15 22:52:37 +10:00
}
/**
* Load all MainWP Child Plugin options.
*
* @return array|bool Return array of options $alloptions[] or FALSE on failure.
*/
public function load_all_options() {
2020-05-21 20:06:30 -04:00
/** @var global $wbdb wpdb. */
2017-08-24 20:41:12 +02:00
global $wpdb;
2020-03-26 17:03:00 +00:00
if ( ! defined( 'WP_INSTALLING' ) || ! is_multisite() ) {
2017-08-24 20:41:12 +02:00
$alloptions = wp_cache_get( 'alloptions', 'options' );
} else {
2017-08-24 20:41:12 +02:00
$alloptions = false;
}
2017-08-24 20:41:12 +02:00
2020-03-26 17:03:00 +00:00
if ( ! defined( 'WP_INSTALLING' ) || ! is_multisite() ) {
2017-08-24 20:41:12 +02:00
$notoptions = wp_cache_get( 'notoptions', 'options' );
} else {
2017-08-24 20:41:12 +02:00
$notoptions = false;
}
2017-08-24 20:41:12 +02:00
2020-04-07 16:53:05 +02:00
if ( ! isset( $alloptions['mainwp_db_version'] ) ) {
2017-08-24 20:41:12 +02:00
$suppress = $wpdb->suppress_errors();
2020-03-26 19:45:07 +00:00
$options = array(
'mainwp_child_auth',
'mainwp_child_reports_db',
'mainwp_child_fix_htaccess',
'mainwp_child_pluginDir',
'mainwp_updraftplus_hide_plugin',
'mainwp_backwpup_ext_enabled',
'mainwpKeywordLinks',
'mainwp_child_server',
'mainwp_kwl_options',
'mainwp_kwl_keyword_links',
'mainwp_keyword_links_htaccess_set',
'mainwp_pagespeed_hide_plugin',
'mainwp_kwl_enable_statistic',
'mainwp_child_clone_permalink',
'mainwp_child_restore_permalink',
'mainwp_ext_snippets_enabled',
'mainwp_child_pubkey',
'mainwp_child_nossl',
'mainwp_security',
'mainwp_backupwordpress_ext_enabled',
'mainwp_pagespeed_ext_enabled',
'mainwp_linkschecker_ext_enabled',
'mainwp_child_branding_settings',
'mainwp_child_plugintheme_days_outdate',
);
2020-03-26 19:45:07 +00:00
$query = "SELECT option_name, option_value FROM $wpdb->options WHERE option_name in (";
foreach ( $options as $option ) {
2017-08-24 20:41:12 +02:00
$query .= "'" . $option . "', ";
}
2020-04-07 16:53:05 +02:00
$query = substr( $query, 0, strlen( $query ) - 2 );
2020-04-07 19:32:01 +02:00
$query .= ")"; // phpcs:ignore
2017-08-24 20:41:12 +02:00
2020-05-08 00:51:43 +07:00
$alloptions_db = $wpdb->get_results( $query ); // phpcs:ignore -- safe query
2020-04-07 16:53:05 +02:00
$wpdb->suppress_errors( $suppress );
2020-03-26 17:03:00 +00:00
if ( ! is_array( $alloptions ) ) {
2020-03-26 19:51:58 +00:00
$alloptions = array();
}
2017-08-24 20:41:12 +02:00
if ( is_array( $alloptions_db ) ) {
foreach ( (array) $alloptions_db as $o ) {
$alloptions[ $o->option_name ] = $o->option_value;
2020-04-07 16:53:05 +02:00
unset( $options[ array_search( $o->option_name, $options ) ] );
2017-08-24 20:41:12 +02:00
}
foreach ( $options as $option ) {
2017-08-24 20:41:12 +02:00
$notoptions[ $option ] = true;
}
if ( ! defined( 'WP_INSTALLING' ) || ! is_multisite() ) {
wp_cache_set( 'alloptions', $alloptions, 'options' );
wp_cache_set( 'notoptions', $notoptions, 'options' );
}
}
}
return $alloptions;
}
/**
* Update MainWP Child Plugin.
*
* @return string Update verison.
*/
public function update() {
2015-10-15 22:52:37 +10:00
$update_version = get_option( 'mainwp_child_update_version' );
if ( $update_version === $this->update_version ) {
return;
}
MainWP_Helper::update_option( 'mainwp_child_update_version', $this->update_version, 'yes' );
}
/**
* Load MainWP Child Plugin textdomains.
*/
public function localization() {
2015-10-15 22:52:37 +10:00
load_plugin_textdomain( 'mainwp-child', false, dirname( dirname( plugin_basename( __FILE__ ) ) ) . '/languages/' );
}
/**
* Template redirect.
*/
public function template_redirect() {
2020-05-14 00:57:25 +07:00
MainWP_Utility::instance()->maintenance_alert();
2015-10-15 22:52:37 +10:00
}
/**
2020-05-28 02:06:59 -04:00
* Parse init.
*
* @deprecated Unused Element.
*/
public function parse_init() {
2015-10-15 22:52:37 +10:00
if ( isset( $_REQUEST['cloneFunc'] ) ) {
2020-05-13 01:18:02 +07:00
// if not valid result then return.
$valid_clone = MainWP_Clone_Install::get()->request_clone_funct();
2020-05-13 01:18:02 +07:00
// not valid clone.
if ( ! $valid_clone ) {
2015-10-15 22:52:37 +10:00
return;
}
2015-10-15 22:52:37 +10:00
}
/** @var global $wp_rewrite Core class used to implement a rewrite component API. */
2015-10-15 22:52:37 +10:00
global $wp_rewrite;
2020-05-21 20:06:30 -04:00
2015-10-15 22:52:37 +10:00
$snPluginDir = basename( $this->plugin_dir );
if ( isset( $wp_rewrite->non_wp_rules[ 'wp-content/plugins/' . $snPluginDir . '/([^js\/]*)$' ] ) ) {
unset( $wp_rewrite->non_wp_rules[ 'wp-content/plugins/' . $snPluginDir . '/([^js\/]*)$' ] );
}
if ( isset( $wp_rewrite->non_wp_rules[ 'wp-content/plugins/' . $snPluginDir . '/(.*)$' ] ) ) {
unset( $wp_rewrite->non_wp_rules[ 'wp-content/plugins/' . $snPluginDir . '/(.*)$' ] );
}
if ( get_option( 'mainwp_child_fix_htaccess' ) === false ) {
include_once ABSPATH . '/wp-admin/includes/misc.php';
2015-10-15 22:52:37 +10:00
$wp_rewrite->flush_rules();
MainWP_Helper::update_option( 'mainwp_child_fix_htaccess', 'yes', 'yes' );
}
2020-05-13 01:18:02 +07:00
// if login required.
2015-12-06 19:05:27 +01:00
if ( isset( $_REQUEST['login_required'] ) && ( '1' === $_REQUEST['login_required'] ) && isset( $_REQUEST['user'] ) ) {
2020-05-14 00:57:25 +07:00
$valid_login_required = MainWP_Connect::instance()->parse_login_required();
// return parse init if login required are not valid.
2020-05-13 01:18:02 +07:00
if ( ! $valid_login_required ) {
2015-10-15 22:52:37 +10:00
return;
}
}
2015-10-15 22:52:37 +10:00
/**
* Security
*/
2020-05-06 20:22:11 +07:00
MainWP_Security::fix_all();
2020-04-07 16:53:05 +02:00
MainWP_Debug::process( $this );
2015-10-15 22:52:37 +10:00
2020-04-07 16:53:05 +02:00
// Register does not require auth, so we register here.
2015-10-15 22:52:37 +10:00
if ( isset( $_POST['function'] ) && 'register' === $_POST['function'] ) {
2016-02-17 20:38:44 +01:00
define( 'DOING_CRON', true );
2020-05-14 00:57:25 +07:00
MainWP_Utility::fix_for_custom_themes();
2020-05-15 01:04:08 +07:00
MainWP_Connect::instance()->register_site(); // register the site and exit.
}
2020-05-15 01:04:08 +07:00
// auth here.
2020-05-14 00:57:25 +07:00
$auth = MainWP_Connect::instance()->auth( isset( $_POST['mainwpsignature'] ) ? $_POST['mainwpsignature'] : '', isset( $_POST['function'] ) ? $_POST['function'] : '', isset( $_POST['nonce'] ) ? $_POST['nonce'] : '', isset( $_POST['nossl'] ) ? $_POST['nossl'] : 0 );
2020-05-15 01:04:08 +07:00
// parse auth, if it is not correct actions then exit with message or return.
if ( ! MainWP_Connect::instance()->parse_init_auth( $auth ) ) {
return;
2015-10-15 22:52:37 +10:00
}
2020-05-15 01:04:08 +07:00
$this->parse_init_extensions();
global $_wp_submenu_nopriv;
2020-04-07 18:33:47 +02:00
if ( null === $_wp_submenu_nopriv ) {
2020-05-05 00:56:15 +07:00
$_wp_submenu_nopriv = array(); // phpcs:ignore -- to fix warning.
}
2016-08-02 19:51:22 +02:00
2020-05-15 01:04:08 +07:00
// execute callable functions here.
2020-05-14 00:57:25 +07:00
MainWP_Child_Callable::get_instance()->init_call_functions( $auth );
2020-05-14 00:57:25 +07:00
MainWP_Keyword_Links::instance()->parse_init_keyword_links();
2015-10-15 22:52:37 +10:00
}
/**
* Check login.
2020-05-28 02:06:59 -04:00
*
* @deprecated Unused Element.
*/
public function init_check_login() {
2020-05-14 00:57:25 +07:00
MainWP_Connect::instance()->check_login();
2015-10-15 22:52:37 +10:00
}
/**
* If user is administrator initiate the admin ajax.
2020-05-28 02:06:59 -04:00
*
* @deprecated Unused Element.
*/
public function admin_init() {
2020-05-15 01:04:08 +07:00
if ( MainWP_Helper::is_admin() && is_admin() ) {
MainWP_Clone::get()->init_ajax();
}
}
/**
* Parse MainWP Extension initiations.
*/
private function parse_init_extensions() {
// Handle fatal errors for those init if needed.
2020-05-14 17:26:44 +07:00
MainWP_Child_Branding::instance()->branding_init();
MainWP_Client_Report::instance()->creport_init();
2020-05-13 01:18:02 +07:00
\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();
2020-05-13 01:18:02 +07:00
\MainWP_Child_Pagespeed::instance()->init();
\MainWP_Child_Links_Checker::instance()->init();
\MainWP_Child_WPvivid_BackupRestore::instance()->init();
}
/**
* Hook to deactivate MainWP Child Plugin.
*
* @param bool $deact Whether or not to deactivate pugin. Default: true.
*/
public function deactivation( $deact = true ) {
2016-10-24 20:33:37 +02:00
2020-05-12 20:19:58 +07:00
$mu_plugin_enabled = apply_filters( 'mainwp_child_mu_plugin_enabled', false );
if ( $mu_plugin_enabled ) {
return;
2020-03-26 19:51:58 +00:00
}
2016-10-24 20:33:37 +02:00
2020-05-12 20:19:58 +07:00
$to_delete = array(
'mainwp_child_pubkey',
'mainwp_child_nonce',
'mainwp_child_nossl',
'mainwp_child_nossl_key',
'mainwp_security',
'mainwp_child_server',
);
$to_delete[] = 'mainwp_ext_snippets_enabled';
$to_delete[] = 'mainwp_ext_code_snippets';
2016-10-24 20:33:37 +02:00
2020-05-12 20:19:58 +07:00
foreach ( $to_delete as $delete ) {
if ( get_option( $delete ) ) {
delete_option( $delete );
wp_cache_delete( $delete, 'options' );
2020-03-26 19:51:58 +00:00
}
}
2018-09-27 19:52:32 +02:00
2020-05-12 20:19:58 +07:00
if ( $deact ) {
do_action( 'mainwp_child_deactivation' );
2020-03-26 19:51:58 +00:00
}
2020-05-12 20:19:58 +07:00
}
/**
* Hook to deactivate Child Plugin.
2020-05-28 02:06:59 -04:00
*
* @deprecated Unused Element.
*/
public function activation() {
2020-05-12 20:19:58 +07:00
$mu_plugin_enabled = apply_filters( 'mainwp_child_mu_plugin_enabled', false );
if ( $mu_plugin_enabled ) {
return;
2020-03-26 19:51:58 +00:00
}
2016-10-24 20:33:37 +02:00
2020-05-12 20:19:58 +07:00
$to_delete = array(
'mainwp_child_pubkey',
'mainwp_child_nonce',
'mainwp_child_nossl',
'mainwp_child_nossl_key',
2020-04-07 17:12:41 +02:00
);
2020-05-12 20:19:58 +07:00
foreach ( $to_delete as $delete ) {
if ( get_option( $delete ) ) {
delete_option( $delete );
2015-10-15 22:52:37 +10:00
}
}
2020-05-12 20:19:58 +07:00
MainWP_Helper::update_option( 'mainwp_child_activated_once', true );
2015-10-15 22:52:37 +10:00
2020-05-12 20:19:58 +07:00
// delete bad data if existed.
$to_delete = array( 'mainwp_ext_snippets_enabled', 'mainwp_ext_code_snippets' );
foreach ( $to_delete as $delete ) {
delete_option( $delete );
2015-10-15 22:52:37 +10:00
}
}
2015-10-15 22:52:37 +10:00
}