*/ public function auto_config_fse_theme_footer_search_block() { if ( ! class_exists( 'WP_REST_Templates_Controller' ) ) { return; } // We currently check only for a core search block. // In the future, we will need to check for a Jetpack Search block once it's available. if ( $this->template_parts_have_search_block() ) { return; } $footer = $this->get_template_part( 'footer' ); if ( ! $footer instanceof \WP_Block_Template ) { return; } $content = $this->replace_block_patterns( $footer->content ); $template_part_id = $footer->id; $request = new \WP_REST_Request( 'PUT', "/wp/v2/template-parts/{$template_part_id}" ); $request->set_header( 'content-type', 'application/json' ); $request->set_param( 'content', static::inject_search_widget_to_block( $content ) ); $request->set_param( 'id', $template_part_id ); $controller = new WP_REST_Templates_Controller( 'wp_template_part' ); return $controller->update_item( $request ); } /** * Replace pattern blocks with their content. * We don't want to replace recursively for the sake of simplicity. * * @param string $block_content - Content of template part. */ protected function replace_block_patterns( $block_content ) { $matches = array(); if ( preg_match( '//', $block_content, $matches ) > 0 ) { foreach ( $matches as $match ) { $pattern_content = $this->get_block_pattern_content( $match ); $block_content = str_replace( $match, $pattern_content, $block_content ); } } return $block_content; } /** * Extracts block content only if it consists of a single pattern block. * * @param string $block_pattern - Block content. */ protected function get_block_pattern_content( $block_pattern ) { if ( ! class_exists( 'WP_Block_Parser' ) || ! class_exists( 'WP_Block_Patterns_Registry' ) ) { return $block_pattern; } $blocks = ( new WP_Block_Parser() )->parse( $block_pattern ); if ( 1 === count( $blocks ) && 'core/pattern' === $blocks[0]['blockName'] ) { $slug = $blocks[0]['attrs']['slug']; $registry = WP_Block_Patterns_Registry::get_instance(); if ( $registry->is_registered( $slug ) ) { $pattern = $registry->get_registered( $slug ); return $pattern['content']; } } return $block_pattern; } /** * Get template part for current theme. * * @param string $template_part_name - header, footer, home etc. * * @return \WP_Block_Template */ protected function get_template_part( $template_part_name ) { // Check whether block theme functions exist. if ( ! function_exists( 'get_block_template' ) ) { return null; } $active_theme = \wp_get_theme()->get_stylesheet(); $template_part_id = "{$active_theme}//{$template_part_name}"; $template_part = \get_block_template( $template_part_id, 'wp_template_part' ); if ( is_wp_error( $template_part ) || empty( $template_part ) ) { return null; } return $template_part; } /** * Returns true if 'header', 'footer' or 'home' has core search block * * @return boolean */ protected function template_parts_have_search_block() { $template_part_names = array( 'header', 'footer', 'home' ); foreach ( $template_part_names as $part_name ) { $part = $this->get_template_part( $part_name ); if ( $part instanceof \WP_Block_Template && static::content_has_search_block( $part->content ) ) { return true; } } return false; } /** * Append Search block to block if no 'wp:search' exists already. * * @param {string} $block_content - the content to append the search block. */ public static function inject_search_widget_to_block( $block_content ) { $search_block = sprintf( '', __( 'Search', 'jetpack-search-pkg' ) ); // Place the search block on bottom of the first column if there's any. $column_end_pattern = '/(<\s*\/div[^>]*>\s*)/'; if ( preg_match( $column_end_pattern, $block_content ) ) { return preg_replace( $column_end_pattern, "\n" . $search_block . "\n$1", $block_content, 1 ); } // Place the search block on top of footer contents in the most inner group. $group_start_pattern = '/(([.\s]*<\s*div[^>]*>\s*)+)/'; if ( preg_match( $group_start_pattern, $block_content, $matches ) ) { return preg_replace( $group_start_pattern, "$1\n" . $search_block . "\n", $block_content, 1 ); } return $block_content; } /** * Autoconfig search by adding filter widgets * * @since 8.4.0 * * @return array Array of config settings for search widget. */ protected function get_preconfig_widget_options() { $settings = array( 'title' => '', 'filters' => array(), ); $post_types = get_post_types( array( 'public' => true, '_builtin' => false, ) ); if ( ! empty( $post_types ) ) { $settings['filters'][] = array( 'name' => '', 'type' => 'post_type', 'count' => 5, ); } // Grab a maximum of 3 taxonomies. $taxonomies = array_slice( get_taxonomies( array( 'public' => true, '_builtin' => false, ) ), 0, 3 ); foreach ( $taxonomies as $t ) { $settings['filters'][] = array( 'name' => '', 'type' => 'taxonomy', 'taxonomy' => $t, 'count' => 5, ); } $settings['filters'][] = array( 'name' => '', 'type' => 'taxonomy', 'taxonomy' => 'category', 'count' => 5, ); $settings['filters'][] = array( 'name' => '', 'type' => 'taxonomy', 'taxonomy' => 'post_tag', 'count' => 5, ); $settings['filters'][] = array( 'name' => '', 'type' => 'date_histogram', 'count' => 5, 'field' => 'post_date', 'interval' => 'year', ); return $settings; } /** * Automatically configure post types to exclude from one of the search widgets. * Used primarily for backward compatibility with older Jetpack plugins, which used to store excluded post type configuration within the Jetpack Search plugin instead of as an option. * * @since 8.8.0 */ public function auto_config_excluded_post_types() { // if `excluded_post_types` exists, then we do nothing. if ( false !== get_option( Options::OPTION_PREFIX . 'excluded_post_types', false ) ) { return; } $post_types = get_post_types( array( 'exclude_from_search' => false, 'public' => true, ) ); $enabled_post_types = array(); $widget_options = get_option( Helper::get_widget_option_name(), array() ); // Prior to Jetpack 8.8, post types were enabled via Jetpack Search widgets rather than disabled via the Customizer. // To continue supporting post types set up in the old way, we iterate through each Jetpack Search // widget configuration and append each enabled post type to $enabled_post_types. foreach ( $widget_options as $widget_option ) { if ( isset( $widget_option['post_types'] ) && is_array( $widget_option['post_types'] ) ) { foreach ( $widget_option['post_types'] as $enabled_post_type ) { $enabled_post_types[ $enabled_post_type ] = $enabled_post_type; } } } if ( ! empty( $enabled_post_types ) ) { $post_types_to_disable = array_diff( $post_types, $enabled_post_types ); // better to use `add_option` which wouldn't override option value if exists. add_option( Options::OPTION_PREFIX . 'excluded_post_types', join( ',', $post_types_to_disable ) ); } } /** * Automatically set result format. * * @since 9.6.0 */ public function auto_config_result_format() { $result_format_option_name = Options::OPTION_PREFIX . 'result_format'; // Default format `expanded`. $result_format_option_value = Options::RESULT_FORMAT_EXPANDED; // Result format already set, skip. if ( get_option( $result_format_option_name, false ) ) { return; } // Check if WooCommerce plugin is active (based on https://docs.woocommerce.com/document/create-a-plugin/). if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', Helper::get_active_plugins() ), true ) ) { $result_format_option_value = Options::RESULT_FORMAT_PRODUCT; } update_option( $result_format_option_name, $result_format_option_value ); return true; } /** * Add current theme name as a body class for easier override * * @param string[] $classes An array of body class names. * * @return string[] The array of classes after filtering */ public function add_body_class( $classes ) { $classes[] = 'jps-theme-' . get_stylesheet(); return $classes; } }