File: /home/memecom/domains/me3me.com/public_html/wp-content/plugins/surerank-pro/loader.php
<?php
/**
* Loader.
*
* @package surerank-pro
* @since 1.0.0
*/
namespace SureRankPro;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
use SureRankPro\Inc\Admin\Dashboard;
use SureRankPro\Inc\Admin\Seo_Bar;
use SureRankPro\Inc\Admin\Seo_Popup;
use SureRankPro\Inc\Analytics\Analytics;
use SureRankPro\Inc\Database\Register;
use SureRankPro\Inc\Licensing\Licensing;
use SureRankPro\Inc\Migrations\Migrations;
use SureRankPro\Inc\Modules\Init as Modules_Init;
use SureRankPro\Inc\ThirdPartyPlugins\Bricks;
use SureRankPro\Inc\ThirdPartyPlugins\Elementor;
use WP_Site;
/**
* Plugin_Loader
*
* @since 1.0.0
*/
class Loader {
/**
* Instance
*
* @access private
* @var self|null Class Instance.
* @since 1.0.0
*/
private static $instance = null;
/**
* Constructor
*
* @since 1.0.0
*/
public function __construct() {
$this->register_autoloader();
$this->register_hooks();
}
/**
* Enqueue required classes after plugins loaded.
*
* @since 1.0.0
* @return void
*/
public function setup() {
if ( ! $this->is_core_version_compatible() ) {
add_action( 'admin_notices', [ $this, 'fail_load_out_of_date' ] );
return;
}
}
/**
* Initiator
*
* @since 1.0.0
* @return self initialized object of class.
*/
public static function get_instance(): self {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Autoload classes.
*
* @param string $class class name.
* @since 1.0.0
* @return void
*/
public function autoload( $class ) {
if ( ! $this->is_plugin_class( $class ) ) {
return;
}
$filename = $this->get_class_filename( $class );
if ( $filename && $this->is_file_readable( $filename ) ) {
require_once $filename;
}
}
/**
* Load Plugin Text Domain.
* This will load the translation textdomain depending on the file priorities.
* 1. Global Languages /wp-content/languages/surerank/ folder
* 2. Local directory /wp-content/plugins/surerank/languages/ folder
*
* @since 1.0.0
* @return void
*/
public function load_textdomain() {
$lang_dir = $this->get_languages_directory();
$locale = $this->get_plugin_locale();
$mofile = $this->get_mofile_name( $locale );
$this->load_translation_file( $lang_dir, $mofile );
}
/**
* Activation Hook
*
* @since 1.0.0
* @return void
*/
public function activation() {
if ( ! defined( 'SURERANK_VERSION' ) ) {
wp_die(
/* translators: %s: Product name */
sprintf( esc_html__( '%s requires SureRank SEO plugin to be installed and activated first.', 'surerank-pro' ), esc_html( SURERANK_PRO_PRODUCT ) ),
esc_html__( 'Plugin Activation Error', 'surerank-pro' ),
[
'link_url' => esc_url( self_admin_url( 'plugins.php' ) ),
'link_text' => esc_html__( 'Return to Plugins', 'surerank-pro' ),
]
);
}
Register::init();
update_option( 'surerank_pro_redirect_on_activation', 'yes' );
}
/**
* Deactivation Hook
*
* @since 1.0.0
* @return void
*/
public function deactivation() {
if ( class_exists( 'SureRankPro\Inc\Modules\Link_Suggestion\Cron' ) ) {
\SureRankPro\Inc\Modules\Link_Suggestion\Cron::clear_schedule();
}
}
/**
* Initialize database tables when a new site is created in multisite.
*
* @param WP_Site $site The new site's object.
* @since 1.0.0
* @return void
*/
public function initialize_new_site( $site ) {
Register::initialize_new_site( $site );
}
/**
* File Permission Notice
*
* @since 1.0.0
* @return void
*/
public function fail_load_out_of_date() {
if ( ! $this->should_show_admin_notice() ) {
return;
}
$message = $this->get_outdated_version_message();
echo '<div class="error">' . wp_kses_post( $message ) . '</div>';
}
/**
* Initialize plugin components
*
* @since 1.0.0
* @return void
*/
public function initialize_components() {
Dashboard::get_instance();
Seo_Popup::get_instance();
Seo_Bar::get_instance();
Modules_Init::get_instance();
if ( is_admin() ) {
if ( defined( 'ELEMENTOR_VERSION' ) ) {
Elementor::get_instance();
}
}
// Commenting this until v1.0.0 release.
Licensing::get_instance();
if ( defined( 'BRICKS_VERSION' ) ) {
Bricks::get_instance();
}
}
/**
* Load Analytics component
*
* @since 1.0.0
* @return void
*/
public function load_analytics() {
Analytics::get_instance();
}
/**
* Check if SureRank core is loaded.
*
* @since 1.0.0
* @return void
*/
public function check_core_availability() {
if ( ! did_action( 'surerank_before_load_routes' ) ) {
add_action( 'admin_notices', [ $this, 'notice_to_activate_core' ] );
add_action( 'network_admin_notices', [ $this, 'notice_to_activate_core' ] );
}
}
/**
* Fires admin notice when SureRank is not installed and activated.
*
* @since 1.0.0
* @return void
*/
public function notice_to_activate_core() {
$screen = get_current_screen();
if ( isset( $screen->parent_file ) && 'plugins.php' === $screen->parent_file && 'update' === $screen->id ) {
return;
}
$class = 'notice notice-error';
/* translators: %s: html tags */
$message = sprintf( __( 'The %1$sSureRank SEO Pro%2$s plugin requires %1$sSureRank SEO%2$s plugin installed & activated.', 'surerank-pro' ), '<strong>', '</strong>' );
$plugin = 'surerank/surerank.php';
if ( $this->is_core_installed() ) {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
$action_url = wp_nonce_url( 'plugins.php?action=activate&plugin=' . $plugin . '&plugin_status=all&paged=1&s', 'activate-plugin_' . $plugin );
$button_label = __( 'Activate SureRank SEO', 'surerank-pro' );
} else {
if ( ! current_user_can( 'install_plugins' ) ) {
return;
}
$action_url = wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=surerank' ), 'install-plugin_surerank' );
$button_label = __( 'Install SureRank SEO', 'surerank-pro' );
}
$button = '<a href="' . $action_url . '" class="button-primary" style="margin-left: 10px; margin-left: 10px;">' . $button_label . '</a>';
printf( '<div class="%1$s" style="display: flex; align-items: center; padding: 15px 0;"><p style="margin: 0; padding-left: 10px; padding-right: 10px;">%2$s</p>%3$s</div>', esc_attr( $class ), wp_kses_post( $message ), wp_kses_post( $button ) );
}
/**
* Redirect to dashboard after plugin activation.
*
* @since 1.0.0
* @return void
*/
public function redirect_to_license_page() {
// Avoid redirection in case of WP_CLI calls.
if ( defined( 'WP_CLI' ) && \WP_CLI ) {
return;
}
// Avoid redirection in case of ajax calls.
if ( wp_doing_ajax() ) {
return;
}
if ( 'licensed' === get_option( 'surerank_pro_license_status', 'unlicensed' ) ) {
// If license is already activated, no need to redirect.
return;
}
$do_redirect = apply_filters( 'surerank_pro_enable_redirect_on_activation', get_option( 'surerank_pro_redirect_on_activation' ) );
if ( 'yes' !== $do_redirect ) {
return;
}
update_option( 'surerank_pro_redirect_on_activation', 'no' );
if ( ! is_multisite() ) {
$url = add_query_arg(
[ 'page' => 'surerank' ],
admin_url( 'admin.php' )
);
$url .= '#/tools/license';
wp_safe_redirect( $url );
exit;
}
}
/**
* Load the plugin
*
* @since 1.0.0
* @return void
*/
public function load() {
Migrations::get_instance();
add_action( 'init', [ $this, 'load_textdomain' ] );
add_action( 'init', [ $this, 'setup' ] );
}
/**
* Add body class - Assign version class for reference.
*
* @param array<int, string> $classes body classes.
* @since 1.2.0
* @return array<int, string>
*/
public function add_body_class( $classes ) {
$classes[] = SURERANK_PRODUCT_PREFIX . '-' . SURERANK_PRO_VERSION;
return $classes;
}
/**
* Register autoloader
*
* @since 1.0.0
* @return void
*/
private function register_autoloader() {
spl_autoload_register( [ $this, 'autoload' ] );
}
/**
* Register WordPress hooks
*
* @since 1.0.0
* @return void
*/
private function register_hooks() {
add_action( 'plugins_loaded', [ $this, 'check_core_availability' ], 999 );
add_action( 'surerank_before_load_routes', [ $this, 'load' ] );
add_action( 'surerank_before_load_components', [ $this, 'initialize_components' ], 20 );
add_action( 'surerank_before_load_routes', [ $this, 'load_analytics' ] );
add_action( 'wp_initialize_site', [ $this, 'initialize_new_site' ] );
add_action( 'admin_init', [ $this, 'redirect_to_license_page' ] );
register_activation_hook( SURERANK_PRO_FILE, [ $this, 'activation' ] );
register_deactivation_hook( SURERANK_PRO_FILE, [ $this, 'deactivation' ] );
add_filter( 'body_class', [ $this, 'add_body_class' ] );
}
/**
* Check if SureRank is installed
*
* @since 1.0.0
* @return bool
*/
private function is_core_installed() {
$path = 'surerank/surerank.php';
$plugins = get_plugins();
return isset( $plugins[ $path ] );
}
/**
* Check if class belongs to this plugin
*
* @param string $class Class name.
* @return bool
*/
private function is_plugin_class( $class ) {
return 0 === strpos( $class, __NAMESPACE__ );
}
/**
* Get filename for class
*
* @param string $class Class name.
* @return string|null
*/
private function get_class_filename( $class ) {
$filename = preg_replace(
[ '/^' . __NAMESPACE__ . '\\\/', '/([a-z])([A-Z])/', '/_/', '/\\\/' ],
[ '', '$1-$2', '-', DIRECTORY_SEPARATOR ],
$class
);
if ( ! is_string( $filename ) ) {
return null;
}
return SURERANK_PRO_DIR . strtolower( $filename ) . '.php';
}
/**
* Check if file is readable
*
* @param string $file File path.
* @return bool
*/
private function is_file_readable( $file ) {
return is_readable( $file );
}
/**
* Get languages directory
*
* @return string
*/
private function get_languages_directory() {
$lang_dir = SURERANK_PRO_DIR . 'languages/';
/**
* Filters the languages directory path to use for plugin.
*
* @param string $lang_dir The languages directory path.
*/
return apply_filters( 'surerank_languages_directory', $lang_dir );
}
/**
* Get plugin locale
*
* @return string
*/
private function get_plugin_locale() {
$get_locale = get_user_locale();
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- wordpress hook
return apply_filters( 'plugin_locale', $get_locale, 'surerank-pro' );
}
/**
* Get MO file name
*
* @param string $locale Locale string.
* @return string
*/
private function get_mofile_name( $locale ) {
return sprintf( '%1$s-%2$s.mo', 'surerank-pro', $locale );
}
/**
* Load translation file
*
* @param string $lang_dir Languages directory.
* @param string $mofile MO file name.
* @return void
*/
private function load_translation_file( $lang_dir, $mofile ) {
$mofile_global = WP_LANG_DIR . '/plugins/' . $mofile;
$mofile_local = $lang_dir . $mofile;
if ( file_exists( $mofile_global ) ) {
// Look in global /wp-content/languages/surerank/ folder.
load_textdomain( 'surerank-pro', $mofile_global );
} elseif ( file_exists( $mofile_local ) ) {
// Look in local /wp-content/plugins/surerank/languages/ folder.
load_textdomain( 'surerank-pro', $mofile_local );
}
}
/**
* Check if the core SureRank version is compatible
*
* @return bool
*/
private function is_core_version_compatible() {
return version_compare( \SURERANK_VERSION, SURERANK_PRO_CORE_RQD_VER, '>=' );
}
/**
* Check if admin notice should be shown
*
* @return bool
*/
private function should_show_admin_notice() {
if ( ! current_user_can( 'update_plugins' ) ) {
return false;
}
$screen = get_current_screen();
if ( empty( $screen ) ) {
return false;
}
return $this->is_valid_admin_screen( $screen->base );
}
/**
* Check if current screen is valid for showing admin notice
*
* @param string $screen_base Current screen base.
* @return bool
*/
private function is_valid_admin_screen( $screen_base ) {
return 0 === strpos( $screen_base, 'dashboard' ) || 0 === strpos( $screen_base, 'plugins' );
}
/**
* Get the outdated version message
*
* @return string
*/
private function get_outdated_version_message() {
$file_path = 'surerank/surerank.php';
$upgrade_link = wp_nonce_url(
self_admin_url( 'update.php?action=upgrade-plugin&plugin=' ) . $file_path,
'upgrade-plugin_' . $file_path
);
$message = '<p>' . esc_html__( 'SureRank Pro is not working because you are using an old version of SureRank.', 'surerank-pro' ) . '</p>';
$message .= '<p>' . sprintf(
'<a href="%s" class="button-primary">%s</a>',
esc_url( $upgrade_link ),
esc_html__( 'Update SureRank Now', 'surerank-pro' )
) . '</p>';
return $message;
}
}
/**
* Kicking this off by calling 'get_instance()' method.
*/
Loader::get_instance();