settings->get( 'maintenance_mode' ) ) { add_action( ( is_admin() ? 'init' : 'template_redirect' ), [ $this, 'init' ] ); // Make sure PO work correctly. add_action( 'wp', [ $this, 'change_post_id' ] ); } } /** * Add site mode badge to WP admin bar. * * @since 7.11.11 * @access public * @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. * @return void */ public function site_mode_badge( $wp_admin_bar ) { $labels = [ 'coming_soon' => __( 'Coming Soon', 'Avada' ), 'maintenance' => __( 'Maintenance', 'Avada' ), 'live' => __( 'Live', 'Avada' ), 'woo_store_coming_soon' => __( 'Woo: Store Coming Soon', 'Avada' ), 'woo_coming_soon' => __( 'Woo: Coming Soon', 'Avada' ), ]; $key = Avada()->settings->get( 'maintenance_mode' ) ? Avada()->settings->get( 'maintenance_mode' ) : 'live'; $link = admin_url( 'themes.php?page=avada_options#heading_maintenance' ); $class = 'awb-site-mode-badge-avada '; if ( class_exists( 'Woocommerce' ) ) { $wp_admin_bar->remove_node( 'woocommerce-site-visibility-badge' ); if ( 'live' === $key && 'yes' === get_option( 'woocommerce_coming_soon' ) ) { if ( 'yes' === get_option( 'woocommerce_store_pages_only' ) ) { $key = 'woo_store_coming_soon'; } else { $key = 'woo_coming_soon'; } $link = admin_url( 'admin.php?page=wc-settings&tab=site-visibility' ); $class = ''; } } $args = [ 'id' => 'awb-site-mode', 'title' => apply_filters( 'awb_site_mode_title', $labels[ $key ] ), 'href' => $link, 'meta' => [ 'class' => $class . 'awb-site-mode-badge-' . str_replace( [ 'woo_', 'woo_store', '_' ], [ '', '', '-' ], $key ), ], ]; $wp_admin_bar->add_node( $args ); } /** * Creates or returns an instance of this class. * * @static * @since 7.9 * @access public * @return object AWB_Maintenance_Mode */ public static function get_instance() { // If an instance hasn't been created and set to $instance create an instance and set it to $instance. if ( null === self::$instance ) { self::$instance = new AWB_Maintenance_Mode(); } return self::$instance; } /** * Init maintenance mode. * * @since 7.9 * @access public * @return void */ public function init() { if ( $this->should_redirect() ) { // URL redirect if set. if ( ! empty( Avada()->settings->get( 'maintenance_redirect_url' ) ) ) { $redirect_url = esc_url_raw( Avada()->settings->get( 'maintenance_redirect_url' ) ); wp_redirect( $redirect_url ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect exit; } elseif ( is_admin() && is_user_logged_in() ) { // Admin requests need to be redirected to front-end, if user doesn't have needed role. wp_redirect( home_url() ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect exit; } $this->default_heading = 'maintenance' === Avada()->settings->get( 'maintenance_mode' ) ? __( 'Maintenance Mode', 'Avada' ) : __( 'Coming Soon', 'Avada' ); $charset = get_bloginfo( 'charset' ) ? get_bloginfo( 'charset' ) : 'UTF-8'; $protocol = wp_get_server_protocol(); $status_code = apply_filters( 'awb_maintenance_status_code', 503 ); $check_back = apply_filters( 'awb_maintenance_check_back_time', 3600 ); add_action( 'awb_maintenance_head', [ $this, 'add_head' ] ); add_action( 'awb_maintenance_content', [ $this, 'add_content' ] ); add_action( 'awb_maintenance_footer', [ $this, 'add_footer' ] ); ob_start(); nocache_headers(); if ( 'maintenance' === Avada()->settings->get( 'maintenance_mode' ) ) { header( $protocol . ' ' . $status_code . ' Service Unavailable', true, $status_code ); header( 'Retry-After: ' . $check_back ); } include_once Avada::$template_dir_path . '/maintenance.php'; ob_flush(); exit(); } } /** * Adds filter to alter post ID on regulare front-end. * * @since 7.9 * @access private * @return void */ public function change_post_id() { if ( $this->should_redirect() ) { add_filter( 'fusion-page-id', [ $this, 'post_id' ] ); } } /** * Return template ID. * * @since 7.9 * @param string $post_id the default post ID. * @access public * @return string */ public function post_id( $post_id ) { return Avada()->settings->get( 'maintenance_template' ); } /** * Checks if redirect to maintenance mode / coming soon mode should occur. * * @since 7.9 * @access public * @return bool */ public function should_redirect() { // phpcs:disable WordPress.Security.ValidatedSanitizedInput $should_redirect = ! $this->can_user_access() && ! $this->is_excluded() && ! ( defined( 'WP_CLI' ) && WP_CLI ) && ! strpos( $_SERVER['PHP_SELF'], 'wp-login.php' ) && ! ( strpos( $_SERVER['PHP_SELF'], 'wp-admin/' ) && ! is_user_logged_in() ) && ! strpos( $_SERVER['PHP_SELF'], 'wp-admin/admin-ajax.php' ) && ! strpos( $_SERVER['PHP_SELF'], 'wp-cron.php' ) && ! strpos( $_SERVER['PHP_SELF'], 'async-upload.php' ); // phpcs:enable WordPress.Security.ValidatedSanitizedInput return apply_filters( 'awb_maintenance_should_redirect', $should_redirect, $_SERVER['PHP_SELF'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput } /** * Adds page title and meta. * * @since 7.9 * @access public * @return void */ public function add_head() { $this->add_title_and_meta(); wp_head(); $this->add_css(); /** * The setting below is not sanitized. * In order to be able to take advantage of this, * a user would have to gain access to the database * in which case this is the least of your worries. */ echo apply_filters( 'avada_space_head', Avada()->settings->get( 'space_head' ) ); // phpcs:ignore WordPress.Security.EscapeOutput } /** * Adds page title and meta. * * @since 7.9 * @access public * @return void */ public function add_title_and_meta() { $page_title = '' !== Avada()->settings->get( 'maintenance_page_title' ) ? Avada()->settings->get( 'maintenance_page_title' ) : $this->default_heading . ' - ' . get_bloginfo( 'name', 'display' ); $robots = 'index' === Avada()->settings->get( 'maintenance_robots_meta' ) ? 'index, follow' : 'noindex, nofollow'; ?>