Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search: Migrate more helper classes to package #21751

Merged
merged 11 commits into from
Nov 25, 2021
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Migrate additional helper classes to package
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,23 @@
/**
* Jetpack Search Overlay Settings
*
* @package automattic/jetpack
* @package automattic/jetpack-search
*/

namespace Automattic\Jetpack\Search;

// Exit if file is accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Class to initialize search settings on the site.
*
* @since 8.3.0
*/
class Jetpack_Search_Settings {
class Settings {

/**
* Class initialization.
*
* @since 8.3.0
*/
public function __construct() {
add_action( 'admin_init', array( $this, 'settings_register' ) );
Expand All @@ -34,7 +32,7 @@ public function __construct() {
*/
public function settings_register() {
// NOTE: This contains significant code overlap with class-jetpack-search-customize.
$setting_prefix = Automattic\Jetpack\Search\Options::OPTION_PREFIX;
$setting_prefix = Options::OPTION_PREFIX;
$settings = array(
array( $setting_prefix . 'color_theme', 'string', 'light' ),
array( $setting_prefix . 'result_format', 'string', 'minimal' ),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
<?php
/**
* Jetpack Search: Jetpack_Search_Template_Tags class
* Template tags class used primarily for rendering widget-related HTML.
*
* @package Jetpack
* @subpackage Jetpack Search
* @since 5.8.0
* Currently, this package can only run in the Jetpack plugin due to its usage of Jetpack_Search.
* Once Jetpack_Search has been migrated to the package as Classic_Search,
* this library will be independent from the Jetpack plugin.
*
* @package automattic/jetpack-search
*/

use Automattic\Jetpack\Search\Helper;
namespace Automattic\Jetpack\Search;

/**
* Class that has various methods for outputting functionality into a theme that doesn't support widgets.
* Additionally the widget itself makes use of these class.
*
* @since 5.8.0
*/
class Jetpack_Search_Template_Tags {
class Template_Tags {

/**
* Renders all available filters that can be used to filter down search results on the frontend.
*
* @since 5.8.0
*
* @param array $filters The available filters for the current query.
* @param array $post_types An array of post types to make filterable
* @param array $post_types An array of post types to make filterable.
*/
public static function render_available_filters( $filters = null, $post_types = null ) {
if ( is_null( $filters ) ) {
$filters = Jetpack_Search::instance()->get_filters();
// TODO: Must be migrated to use Classic_Search once the migration is underway.
$filters = \Jetpack_Search::instance()->get_filters();
}

if ( is_null( $post_types ) ) {
Expand All @@ -40,12 +43,13 @@ public static function render_available_filters( $filters = null, $post_types =
*/
$active_post_types = array();
if ( Helper::post_types_differ_searchable( $post_types ) ) {
// get the active filter buckets from the query
$active_buckets = Jetpack_Search::instance()->get_active_filter_buckets();
// get the active filter buckets from the query.
// TODO: Must be migrated to use Classic_Search once the migration is underway.
$active_buckets = \Jetpack_Search::instance()->get_active_filter_buckets();
$post_types_differ_query = Helper::post_types_differ_query( $post_types );

// remove any post_type filters from display if the current query
// already specifies to match all post types
// already specifies to match all post types.
if ( ! $post_types_differ_query ) {
$active_buckets = array_filter( $active_buckets, array( __CLASS__, 'is_not_post_type_filter' ) );
}
Expand All @@ -65,7 +69,7 @@ public static function render_available_filters( $filters = null, $post_types =
}

foreach ( (array) $filters as $filter ) {
if ( 'post_type' == $filter['type'] ) {
if ( 'post_type' === $filter['type'] ) {
self::render_filter( $filter, $post_types );
} else {
self::render_filter( $filter, $active_post_types );
Expand All @@ -76,14 +80,12 @@ public static function render_available_filters( $filters = null, $post_types =
/**
* Renders filters for instant search.
*
* @since 8.3.0
*
* @param array $filters The available filters for the current query.
* @param array $post_types An array of post types (ignored).
*/
public static function render_instant_filters( $filters = null, $post_types = null ) {
public static function render_instant_filters( $filters = null ) {
if ( is_null( $filters ) ) {
$filters = Jetpack_Search::instance()->get_filters();
// TODO: Must be migrated to use Classic_Search once the migration is underway.
$filters = \Jetpack_Search::instance()->get_filters();
}

foreach ( (array) $filters as $filter ) {
Expand Down Expand Up @@ -133,7 +135,7 @@ public static function render_filter( $filter, $default_post_types ) {
<ul class="jetpack-search-filters-widget__filter-list">
<?php
foreach ( $filter['buckets'] as $item ) :
$url = ( empty( $item['active'] ) ) ? $item['url'] : $item['remove_url'];
$url = ( empty( $item['active'] ) ) ? $item['url'] : $item['remove_url'];
?>
<li>
<label>
Expand All @@ -142,10 +144,12 @@ public static function render_filter( $filter, $default_post_types ) {
<?php
echo esc_html( $item['name'] );
echo '&nbsp;';
echo esc_html( sprintf(
'(%s)',
number_format_i18n( absint( $item['count'] ) )
) );
echo esc_html(
sprintf(
'(%s)',
number_format_i18n( absint( $item['count'] ) )
)
);
?>
</a>
</label>
Expand Down Expand Up @@ -220,14 +224,17 @@ public static function render_instant_filter( $filter ) {
}
?>
<li>
<a href="#" class="jetpack-search-filter__link" <?php echo $data_str; ?>>
<?php // TODO: Figure out how to properly escape $data_str below. ?>
<a href="#" class="jetpack-search-filter__link" <?php echo $data_str; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
<?php
echo esc_html( $item['name'] );
echo '&nbsp;';
echo esc_html( sprintf(
'(%s)',
number_format_i18n( absint( $item['count'] ) )
) );
echo esc_html(
sprintf(
'(%s)',
number_format_i18n( absint( $item['count'] ) )
)
);
?>
</a>
</li>
Expand All @@ -241,12 +248,13 @@ public static function render_instant_filter( $filter ) {
*
* @since 5.8.0
*
* @param string $title The widget's title
* @param string $before_title The HTML tag to display before the title
* @param string $after_title The HTML tag to display after the title
* @param string $title The widget's title.
* @param string $before_title The HTML tag to display before the title.
* @param string $after_title The HTML tag to display after the title.
*/
public static function render_widget_title( $title, $before_title, $after_title ) {
echo $before_title . esc_html( $title ) . $after_title;
// TODO: figure out how to properly escape this.
echo $before_title . esc_html( $title ) . $after_title; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}

/**
Expand All @@ -263,7 +271,7 @@ public static function render_widget_search_form( $post_types, $orderby, $order

$fields_to_inject = array(
'orderby' => $orderby,
'order' => $order
'order' => $order,
);

// If the widget has specified post types to search within and IF the post types differ
Expand All @@ -276,7 +284,8 @@ public static function render_widget_search_form( $post_types, $orderby, $order
$form = self::inject_hidden_form_fields( $form, $fields_to_inject );

echo '<div class="jetpack-search-form">';
echo $form;
// TODO: Figure out how to properly escape this.
echo $form; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '</div>';
}

Expand All @@ -301,7 +310,7 @@ private static function inject_hidden_form_fields( $form, $fields ) {
);
}

// This shouldn't need to be escaped since we've escaped above as we built $form_injection
// This shouldn't need to be escaped since we've escaped above as we built $form_injection.
$form = str_replace(
'</form>',
$form_injection . '</form>',
Expand All @@ -316,11 +325,11 @@ private static function inject_hidden_form_fields( $form, $fields ) {
*
* @since 5.8.0
*
* @param array $filter
* @param array $filter Filter object.
*
* @return bool
*/
private static function is_not_post_type_filter( $filter ) {
return 'post_type' !== $filter['type'];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other

Search: Migrate more helper classes to package
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other

Updated package dependencies.
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ public static function instance() {
public function load_php() {
$this->base_load_php();

require_once __DIR__ . '/class-jetpack-search-settings.php';
new Jetpack_Search_Settings();
new Automattic\Jetpack\Search\Settings();
kangzj marked this conversation as resolved.
Show resolved Hide resolved

if ( class_exists( 'WP_Customize_Manager' ) ) {
require_once __DIR__ . '/class-jetpack-search-customize.php';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ public function load_php() {
* Loads the PHP common to all search. Should be called from extending classes.
*/
protected function base_load_php() {
require_once __DIR__ . '/class.jetpack-search-template-tags.php';
require_once JETPACK__PLUGIN_DIR . 'modules/widgets/search.php';
}

Expand Down
12 changes: 6 additions & 6 deletions projects/plugins/jetpack/modules/widgets/search.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
kangzj marked this conversation as resolved.
Show resolved Hide resolved
/**
* Jetpack Search: Jetpack_Search_Widget class
*
Expand Down Expand Up @@ -92,11 +92,11 @@ public function __construct( $name = null ) {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) );
}

add_action( 'jetpack_search_render_filters_widget_title', array( 'Jetpack_Search_Template_Tags', 'render_widget_title' ), 10, 3 );
add_action( 'jetpack_search_render_filters_widget_title', array( 'Automattic\Jetpack\Search\Template_Tags', 'render_widget_title' ), 10, 3 );
if ( Options::is_instant_enabled() ) {
add_action( 'jetpack_search_render_filters', array( 'Jetpack_Search_Template_Tags', 'render_instant_filters' ), 10, 2 );
add_action( 'jetpack_search_render_filters', array( 'Automattic\Jetpack\Search\Template_Tags', 'render_instant_filters' ), 10, 2 );
} else {
add_action( 'jetpack_search_render_filters', array( 'Jetpack_Search_Template_Tags', 'render_available_filters' ), 10, 2 );
add_action( 'jetpack_search_render_filters', array( 'Automattic\Jetpack\Search\Template_Tags', 'render_available_filters' ), 10, 2 );
}
}

Expand Down Expand Up @@ -369,7 +369,7 @@ public function widget_non_instant( $args, $instance ) {
// we need to dynamically inject the sort field into the search box when the search box is enabled, and display
// it separately when it's not.
if ( ! empty( $instance['search_box_enabled'] ) ) {
Jetpack_Search_Template_Tags::render_widget_search_form( $instance['post_types'], $orderby, $order );
Automattic\Jetpack\Search\Template_Tags::render_widget_search_form( $instance['post_types'], $orderby, $order );
}

if ( ! empty( $instance['search_box_enabled'] ) && ! empty( $instance['user_sort_enabled'] ) ) :
Expand Down Expand Up @@ -458,7 +458,7 @@ public function widget_instant( $args, $instance ) {
do_action( 'jetpack_search_render_filters_widget_title', $title, $args['before_title'], $args['after_title'] );
}

Jetpack_Search_Template_Tags::render_widget_search_form( array(), '', '' );
Automattic\Jetpack\Search\Template_Tags::render_widget_search_form( array(), '', '' );

if ( $display_filters ) {
/**
Expand Down
1 change: 0 additions & 1 deletion tools/phpcs-excludelist.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@
"projects/plugins/jetpack/modules/related-posts.php",
"projects/plugins/jetpack/modules/related-posts/class.related-posts-customize.php",
"projects/plugins/jetpack/modules/related-posts/jetpack-related-posts.php",
"projects/plugins/jetpack/modules/search/class.jetpack-search-template-tags.php",
"projects/plugins/jetpack/modules/seo-tools/jetpack-seo-posts.php",
"projects/plugins/jetpack/modules/seo-tools/jetpack-seo-titles.php",
"projects/plugins/jetpack/modules/seo-tools/jetpack-seo-utils.php",
Expand Down