post_content; $tb_layouts = et_theme_builder_get_template_layouts(); // Add TB templates content. if ( ! empty( $tb_layouts ) ) { $post_types = [ ET_THEME_BUILDER_HEADER_LAYOUT_POST_TYPE, ET_THEME_BUILDER_BODY_LAYOUT_POST_TYPE, ET_THEME_BUILDER_FOOTER_LAYOUT_POST_TYPE, ]; foreach ( $post_types as $post_type ) { $layout = $tb_layouts[ $post_type ]; if ( $layout['override'] && ! empty( $layout['enabled'] ) ) { $template = get_post( intval( $layout['id'] ) ); $all_content .= $template->post_content; } } } $integrations = [ 'head', 'body', 'single_top', 'single_bottom', ]; // Add Integration code. foreach ( $integrations as $integration ) { $all_content .= et_get_option( $shortname . '_integration_' . $integration ); } return $all_content; } /** * Recursively check if a script (or its deps) depends on JQuery. * * @since 4.10.0 * @param string $script Script Handle. * * @return bool */ public function has_jquery_dep( $script ) { global $wp_scripts; $registered = $wp_scripts->registered; $handles = [ $script ]; $stack = []; $result = false; while ( false === $result && $handles ) { foreach ( $handles as $handle ) { if ( ! empty( $this->_has_jquery_dep[ $handle ] ) ) { $result = true; break; } if ( isset( $registered[ $handle ] ) ) { $deps = $registered[ $handle ]->deps; if ( $deps ) { if ( in_array( 'jquery-core', $deps, true ) ) { $result = true; break; } array_push( $stack, $deps ); } } } $handles = array_pop( $stack ); } $this->_has_jquery_dep[ $script ] = $result; return $result; } /** * Get script deps. * * @since 4.10.0 * @param string $script Script Handle. * * @return array */ public function get_deps( $script ) { global $wp_scripts; $registered = $wp_scripts->registered; $handles = is_array( $script ) ? $script : [ $script ]; $all_deps = $handles; $stack = []; $done = []; while ( $handles ) { foreach ( $handles as $handle ) { if ( ! isset( $done[ $handle ] ) && isset( $registered[ $handle ] ) ) { $deps = $registered[ $handle ]->deps; if ( $deps ) { $all_deps = array_merge( $all_deps, $deps ); array_push( $stack, $deps ); } $done[ $handle ] = true; } } $handles = array_pop( $stack ); } return array_unique( $all_deps ); } /** * Check if a script is currently enqueued in HEAD. * * @since 4.10.0 * @param string $handle Script Handle. * * @return bool */ public function in_head( $handle ) { global $wp_scripts; if ( empty( $wp_scripts->registered[ $handle ] ) ) { return false; } $script = $wp_scripts->registered[ $handle ]; if ( isset( $script->args ) && 1 === $script->args ) { return false; } if ( isset( $script->extra['group'] ) && 1 === $script->extra['group'] ) { return false; } return true; } /** * Check whether some content includes jQuery or not. * * @since 4.10.0 * @param string $content Content. * * @return bool */ public static function has_jquery_content( $content ) { $has_jquery_regex = '/\(jQuery|jQuery\.|jQuery\)/'; return 1 === preg_match( $has_jquery_regex, $content ); } /** * Move jQuery / migrate / 3P scripts in BODY. * * @since 4.10.0 * * @return void */ public function wp_print_scripts() { global $shortname; if ( $this->should_move_jquery() ) { global $wp_scripts; $queue = $this->get_deps( $wp_scripts->queue ); $head = in_array( 'jquery-migrate', $queue, true ) ? [ 'jquery-migrate' ] : []; $mode = 'on' === et_get_option( $shortname . '_enable_jquery_body_super' ) ? 'super' : 'safe'; // Find all head scripts that depends on JQuery. foreach ( $queue as $handle ) { if ( $this->in_head( $handle ) && $this->has_jquery_dep( $handle ) ) { if ( 'safe' === $mode && 'jquery' !== $handle && 'jquery-migrate' !== $handle ) { // Bail out when a script requiring jQuery is found in head. return; } $head[] = $handle; } } $registered = $wp_scripts->registered; // Disable the feature when finding a script which does not depend on jQuery // but still uses it inside its inlined content (before/after). foreach ( $queue as $handle ) { if ( ! $this->has_jquery_dep( $handle ) ) { $script = $registered[ $handle ]; $data = ''; if ( isset( $script->extra['data'] ) ) { $data .= $script->extra['data']; } if ( isset( $script->extra['before'] ) ) { $data .= join( '', $script->extra['before'] ); } if ( isset( $script->extra['after'] ) ) { $data .= join( '', $script->extra['after'] ); } if ( ! empty( $data ) && self::has_jquery_content( $data ) ) { return; } } } // Disable the feature when jQuery is found in TB/Post Content. if ( self::has_jquery_content( $this->get_all_content() ) ) { return; } if ( ! empty( $head ) ) { foreach ( $this->get_deps( $head ) as $handle ) { $wp_scripts->add_data( $handle, 'group', 1 ); } } } } /** * Get the class instance. * * @since 4.10.0 * * @return ET_Builder_JQuery_Body */ public static function instance() { if ( ! self::$_instance ) { self::$_instance = new self(); } return self::$_instance; } /** * Fake jQuery in head when jQuery body option is true. */ public function add_jquery_sub() { global $shortname; $jquery_compatibility_enabled = 'on' === et_get_option( $shortname . '_enable_jquery_compatibility', 'on' ) ? true : false; if ( $this->should_move_jquery() && $jquery_compatibility_enabled ) { echo ''; } } /** * Disable the Fake jQuery in head when jQuery body option is true. */ public function remove_jquery_sub() { global $shortname; $jquery_compatibility_enabled = 'on' === et_get_option( $shortname . '_enable_jquery_compatibility', 'on' ) ? true : false; if ( $this->should_move_jquery() && $jquery_compatibility_enabled ) { $script = 'jqueryParams.length&&$.each(jqueryParams,function(e,r){if("function"==typeof r){var n=String(r);n.replace("$","jQuery");var a=new Function("return "+n)();$(document).ready(a)}});'; wp_add_inline_script( 'jquery', $script, 'after' ); } } /** * Check if jQuery should be moved to the body. */ public function should_move_jquery() { global $shortname; if ( null === self::$_should_move_jquery ) { /** * Filters whether JQuery Body feature is enabled or not. * * @since 4.10.5 * * @param bool $enabled JQuery Body enabled value. * @param string $content TB/Post Content. */ if ( false === apply_filters( 'et_builder_enable_jquery_body', true, $this->get_all_content() ) ) { // Bail out if disabled by filter. self::$_should_move_jquery = false; return false; } self::$_should_move_jquery = ! ( is_admin() || wp_doing_ajax() || et_is_builder_plugin_active() || is_customize_preview() || is_et_pb_preview() || 'wp-login.php' === $GLOBALS['pagenow'] || et_fb_is_enabled() ) && 'on' === et_get_option( $shortname . '_enable_jquery_body', 'on' ); } return self::$_should_move_jquery; } } ET_Builder_JQuery_Body::instance();