replace]. * * @static * @access private * @since 2.0 * @var array */ private static $replace_patterns = []; /** * The final CSS. * * Used here as a static property to avoid multiple constly calls * to the make_css() method. * * @static * @access private * @since 2.0 * @var string */ private static $final_css; /** * Constructor. * * @access protected * @since 1.0 */ protected function __construct() { self::$helpers = $this->get_helpers(); add_action( 'wp', [ $this, 'init' ], 999 ); // When a post is saved, reset its caches to force-regenerate the CSS. add_action( 'save_post', [ $this, 'reset_post_transient' ] ); add_action( 'save_post', [ $this, 'post_update_option' ] ); add_action( 'customize_save_after', [ $this, 'reset_all_caches' ] ); add_filter( 'fusion_dynamic_css', [ $this, 'add_extra_files' ] ); add_filter( 'fusion_dynamic_css', [ $this, 'icomoon_css' ] ); add_filter( 'fusion_dynamic_css_array', [ $this, 'add_css_vars_to_css' ], PHP_INT_MAX ); add_filter( 'fusion_dynamic_css_final', [ $this, 'maybe_replace_css_vars_in_styles' ], PHP_INT_MAX ); add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_extra_files' ], 11 ); add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_css_vars_polyfill' ] ); } /** * Gets the instance of this object. * * @static * @since 1.0 * @return object */ public static function get_instance() { if ( null === self::$instance ) { self::$instance = new self(); } return self::$instance; } /** * Add extra actions. * * @access public * @since 1.0 * @return void */ public function init() { // If builder frame or AJAX request, no need to run. if ( function_exists( 'fusion_is_builder_frame' ) && fusion_is_builder_frame() || fusion_doing_ajax() ) { return; } // Add options. $this->add_options(); // Set the $needs_update property. $this->needs_update(); // Set the $disable_cache property. $this->is_cache_disabled(); // Set mode. $this->set_mode(); if ( 'file' === $this->get_mode() ) { $this->file = new Fusion_Dynamic_CSS_File( $this ); return; } $this->inline = new Fusion_Dynamic_CSS_Inline( $this ); } /** * Determine if we're using file mode or inline mode. * * @access public * @since 1.0 * @return void */ public function set_mode() { $this->mode = 'inline'; $option = Fusion_Settings::get_option_name(); // Early exit if on the customizer. // This will force-using inline mode. global $wp_customize; if ( $wp_customize || ( isset( $_GET['builder_id'] ) && get_transient( 'fusion_app_emulated-' . sanitize_text_field( wp_unslash( $_GET['builder_id'] ) . '-' . $option ) ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification return; } // Make sure Avada is active if being used. if ( $this->is_cache_disabled() ) { $this->mode = 'inline'; return; } // Check if we're using file mode or inline mode. // This simply checks the css_cache_method options. if ( 'file' === fusion_library()->get_option( 'css_cache_method' ) ) { $this->mode = 'file'; } // Additional checks for file mode. if ( 'file' === $this->mode && self::$needs_update ) { // Only allow processing 1 file every 5 seconds. $current_time = (int) time(); $last_time = (int) get_option( 'fusion_dynamic_css_time' ); if ( 5 > ( $current_time - $last_time ) ) { $this->mode = 'inline'; return; } } } /** * Gets the mode we're using. * * @access public * @since 1.1.5 * @return string */ public function get_mode() { if ( ! $this->mode || null === $this->mode ) { $this->set_mode(); } return ( defined( 'FUSION_DISABLE_COMPILERS' ) && FUSION_DISABLE_COMPILERS ) ? 'inline' : $this->mode; } /** * Creates the final CSS. * * @access public * @since 2.0 * @return string The final CSS. */ public function generate_final_css() { if ( self::$final_css ) { return self::$final_css; } $helpers = self::$helpers; self::$final_css = $helpers->get_dynamic_css(); self::$final_css = apply_filters( 'fusion_dynamic_css_cached', self::$final_css ); // Apply replace patterns. $replace_patterns = self::get_replacement_patterns(); foreach ( $replace_patterns as $search => $replace ) { self::$final_css = str_replace( $search, $replace, self::$final_css ); } // When using domain-mapping plugins we have to make sure that any references to the original domain // are replaced with references to the mapped domain. // We're also stripping protocols from these domains so that there are no issues with SSL certificates. if ( defined( 'DOMAIN_MAPPING' ) && DOMAIN_MAPPING ) { if ( function_exists( 'domain_mapping_siteurl' ) && function_exists( 'get_original_url' ) ) { // The mapped domain of the site. $mapped_domain = domain_mapping_siteurl( false ); $mapped_domain = str_replace( [ 'https://', 'http://' ], '//', $mapped_domain ); // The original domain of the site. $original_domain = get_original_url( 'siteurl' ); $original_domain = str_replace( [ 'https://', 'http://' ], '//', $original_domain ); // Replace original domain with mapped domain. self::$final_css = str_replace( $original_domain, $mapped_domain, self::$final_css ); } } // Strip protocols. This helps avoid any issues with https sites. self::$final_css = str_replace( [ 'https://', 'http://' ], '//', self::$final_css ); self::$final_css = $this->maybe_replace_css_vars_in_styles( self::$final_css ); self::$final_css = apply_filters( 'fusion_dynamic_css_final', self::$final_css ); // We're adding a warning at the top of the file to prevent users from editing it. // The warning is then followed by the actual CSS content. self::$final_css = '/********* Compiled CSS - Do not edit *********/ ' . self::$final_css; // Security: strips all tags to avoid closing the