Other plugins. * * @param mixed $fields Settings schema. * @return mixed */ public static function fields( $fields ) { if ( ! is_array( $fields ) || ! isset( $fields['checkout']['sections'] ) || ! is_array( $fields['checkout']['sections'] ) ) { return $fields; } $fields['checkout']['sections']['compat']['label'] = __( 'Other plugins', 'aiolos-checkout-for-woocommerce' ); $fields['checkout']['sections']['compat']['fields']['remove_builder_css'] = array( 'type' => 'toggle', 'label' => __( 'Leave page builder styles off the checkout', 'aiolos-checkout-for-woocommerce' ), 'default' => 'yes', 'description' => __( 'Elementor\'s styles and scripts (kit, global, frontend) are not needed on the checkout, which has no Elementor content: leaving them off makes it faster and keeps the kit from restyling buttons and fields.', 'aiolos-checkout-for-woocommerce' ), ); return $fields; } /** * Elementor's frontend, when it has the content filter methods. * * @return object|null */ private static function frontend() { if ( ! class_exists( '\Elementor\Plugin' ) || ! method_exists( '\Elementor\Plugin', 'instance' ) ) { return null; } $plugin = \Elementor\Plugin::instance(); $frontend = is_object( $plugin ) && isset( $plugin->frontend ) && is_object( $plugin->frontend ) ? $plugin->frontend : null; return $frontend && method_exists( $frontend, 'remove_content_filter' ) && method_exists( $frontend, 'add_content_filter' ) ? $frontend : null; } /** * Takes Elementor's content filter off before page content is rendered in the checkout. */ public function remove_content_filter(): void { $frontend = self::frontend(); if ( $frontend ) { $frontend->remove_content_filter(); ++$this->removed; } } /** * Puts it back afterwards (only if it was taken off here). */ public function add_content_filter(): void { if ( $this->removed < 1 ) { return; } $frontend = self::frontend(); if ( $frontend ) { $frontend->add_content_filter(); } --$this->removed; } /** * Whether a style or script belongs to Elementor. * * @param string $handle Handle. * @param mixed $src Source URL. */ private static function is_elementor_asset( string $handle, $src ): bool { if ( 0 === strpos( $handle, 'elementor-' ) ) { return true; } if ( ! is_string( $src ) || '' === $src ) { return false; } return (bool) preg_match( '#/plugins/elementor(-pro)?/|/uploads/elementor/(css|google-fonts)/#', $src ); } /** * Takes Elementor's styles and scripts off the checkout. Scripts other plugins depend on are * still printed (dequeuing does not remove dependencies). */ public static function dequeue(): void { if ( ! Checkout::active() || 'yes' !== Settings::get( 'remove_builder_css', 'yes' ) ) { return; } /** * Filters the Elementor style and script handles kept on the checkout. * * @since 1.6.0 * * @param string[] $handles Handles. */ $keep = (array) apply_filters( 'aiolos_kept_builder_assets', array() ); foreach ( array( wp_styles(), wp_scripts() ) as $deps ) { foreach ( (array) $deps->queue as $handle ) { $handle = (string) $handle; if ( in_array( $handle, $keep, true ) ) { continue; } $src = isset( $deps->registered[ $handle ] ) ? $deps->registered[ $handle ]->src : ''; if ( self::is_elementor_asset( $handle, $src ) ) { $deps->dequeue( $handle ); } } } } }