* @copyright 2007-2022 Leotheme * @license http://leotheme.com - prestashop template provider */ namespace LeoElements; use LeoElements\TemplateLibrary\Source_Local; use LeoElements\Leo_Helper; if ( ! defined( '_PS_VERSION_' ) ) { exit; // Exit if accessed directly. } /** * Elementor maintenance mode. * * Elementor maintenance mode handler class is responsible for the Elementor * "Maintenance Mode" and the "Coming Soon" features. * * @since 1.4.0 */ class Maintenance_Mode { /** * The options prefix. */ const OPTION_PREFIX = 'elementor_maintenance_mode_'; /** * The maintenance mode. */ const MODE_MAINTENANCE = 'maintenance'; /** * The coming soon mode. */ const MODE_COMING_SOON = 'coming_soon'; /** * Get elementor option. * * Retrieve elementor option from the database. * * @since 1.4.0 * @access public * @static * * @param string $option Option name. Expected to not be SQL-escaped. * @param mixed $default Optional. Default value to return if the option * does not exist. Default is false. * * @return bool False if value was not updated and true if value was updated. */ public static function get( $option, $default = false ) { return Leo_Helper::get_option( self::OPTION_PREFIX . $option, $default ); } /** * Set elementor option. * * Update elementor option in the database. * * @since 1.4.0 * @access public * @static * * @param string $option Option name. Expected to not be SQL-escaped. * @param mixed $value Option value. Must be serializable if non-scalar. * Expected to not be SQL-escaped. * * @return bool False if value was not updated and true if value was updated. */ public static function set( $option, $value ) { return Leo_Helper::update_option( self::OPTION_PREFIX . $option, $value ); } /** * Body class. * * Add "Maintenance Mode" CSS classes to the body tag. * * Fired by `body_class` filter. * * @since 1.4.0 * @access public * * @param array $classes An array of body classes. * * @return array An array of body classes. */ public function body_class( $classes ) { $classes[] = 'elementor-maintenance-mode'; return $classes; } /** * Template redirect. * * Redirect to the "Maintenance Mode" template. * * Fired by `template_redirect` action. * * @since 1.4.0 * @access public */ public function template_redirect() { if ( Plugin::$instance->preview->is_preview_mode() ) { return; } $user = wp_get_current_user(); $exclude_mode = self::get( 'exclude_mode', [] ); $is_login_page = Leo_Helper::apply_filters( 'elementor/maintenance_mode/is_login_page', false ); if ( $is_login_page ) { return; } if ( 'logged_in' === $exclude_mode && is_user_logged_in() ) { return; } if ( 'custom' === $exclude_mode ) { $exclude_roles = self::get( 'exclude_roles', [] ); $user_roles = $user->roles; if ( is_multisite() && is_super_admin() ) { $user_roles[] = 'super_admin'; } $compare_roles = array_intersect( $user_roles, $exclude_roles ); if ( ! empty( $compare_roles ) ) { return; } } Leo_Helper::add_filter( 'body_class', [ $this, 'body_class' ] ); if ( 'maintenance' === self::get( 'mode' ) ) { $protocol = wp_get_server_protocol(); header( "$protocol 503 Service Unavailable", true, 503 ); header( 'Content-Type: text/html; charset=utf-8' ); header( 'Retry-After: 600' ); } // Setup global post for Elementor\frontend so `_has_elementor_in_page = true`. $GLOBALS['post'] = get_post( self::get( 'template_id' ) ); // WPCS: override ok. // Set the template as `$wp_query->current_object` for `wp_title` and etc. query_posts( [ 'p' => self::get( 'template_id' ), 'post_type' => Source_Local::CPT, ] ); } /** * Register settings fields. * * Adds new "Maintenance Mode" settings fields to Elementor admin page. * * The method need to receive the an instance of the Tools settings page * to add the new maintenance mode functionality. * * Fired by `elementor/admin/after_create_settings/{$page_id}` action. * * @since 1.4.0 * @access public * * @param Tools $tools An instance of the Tools settings page. */ public function register_settings_fields( Tools $tools ) { $templates = Plugin::$instance->templates_manager->get_source( 'local' )->get_items( [ 'type' => 'page', ] ); $templates_options = []; foreach ( $templates as $template ) { $templates_options[ $template['template_id'] ] = $template['title']; } ob_start(); $this->print_template_description(); $template_description = ob_get_clean(); $tools->add_tab( 'maintenance_mode', [ 'label' => Leo_Helper::__( 'Maintenance Mode', 'elementor' ), 'sections' => [ 'maintenance_mode' => [ 'callback' => function() { echo '