. $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;
}
}