Skip to content

Commit

Permalink
Improve initialization, move classes into own dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnmoon committed Oct 20, 2021
1 parent 551e734 commit e7bab4b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Classic Search: The legacy search experience.
* Classic Search: Our original search experience with filtering capability.
*
* @package @automattic/jetpack-search
*/
Expand All @@ -9,31 +9,28 @@

use Automattic\Jetpack\Connection\Client;
use Automattic\Jetpack\Search\Helper as Jetpack_Search_Helpers;
// TODO: Replace usage of WPES libraries here by migrating them to the package.
use Jetpack_WPES_Query_Builder;
use Jetpack_WPES_Search_Query_Parser;
use Automattic\Jetpack\Search\WPES\Query_Builder as Jetpack_WPES_Query_Builder;
use Automattic\Jetpack\Search\WPES\Query_Parser as Jetpack_WPES_Search_Query_Parser;
use WP_Error;
use WP_Query;
use WP_Tax_Query;

/**
* The legacy search experience that's used when instant search is disabled.
* Class responsible for enabling the Classic Search experience on the site.
*/
class Classic_Search {
/**
* The singleton instance of this class.
*
* @since 5.0.0
*
* @var Jetpack_Search
* @var Classic_Search
*/
protected static $instance;

/**
* The number of found posts.
*
* @since 5.0.0
*
* @var int
*/
protected $found_posts = 0;
Expand All @@ -42,7 +39,6 @@ class Classic_Search {
* The search result, as returned by the WordPress.com REST API.
*
* @since 5.0.0
*
* @var array
*/
protected $search_result;
Expand All @@ -51,7 +47,6 @@ class Classic_Search {
* This site's blog ID on WordPress.com.
*
* @since 5.0.0
*
* @var int
*/
protected $jetpack_blog_id;
Expand All @@ -60,7 +55,6 @@ class Classic_Search {
* The Elasticsearch aggregations (filters).
*
* @since 5.0.0
*
* @var array
*/
protected $aggregations = array();
Expand All @@ -69,7 +63,6 @@ class Classic_Search {
* The maximum number of aggregations allowed.
*
* @since 5.0.0
*
* @var int
*/
protected $max_aggregations_count = 100;
Expand All @@ -78,7 +71,6 @@ class Classic_Search {
* Statistics about the last Elasticsearch query.
*
* @since 5.6.0
*
* @var array
*/
protected $last_query_info = array();
Expand All @@ -87,7 +79,6 @@ class Classic_Search {
* Statistics about the last Elasticsearch query failure.
*
* @since 5.6.0
*
* @var array
*/
protected $last_query_failure_info = array();
Expand All @@ -96,7 +87,6 @@ class Classic_Search {
* Languages with custom analyzers. Other languages are supported, but are analyzed with the default analyzer.
*
* @since 5.0.0
*
* @var array
*/
public static $analyzed_langs = array( 'ar', 'bg', 'ca', 'cs', 'da', 'de', 'el', 'en', 'es', 'eu', 'fa', 'fi', 'fr', 'he', 'hi', 'hu', 'hy', 'id', 'it', 'ja', 'ko', 'nl', 'no', 'pt', 'ro', 'ru', 'sv', 'tr', 'zh' );
Expand All @@ -110,44 +100,29 @@ protected function __construct() {
/**
* Returns the singleton of the class. Instantiates and sets up a singleton instance if necessary.
*
* @param string $blog_id Blog id.
* @return static The class singleton.
*/
public static function instance() {
public static function instance( $blog_id ) {
if ( ! isset( self::$instance ) ) {
self::$instance = new static();
self::$instance->setup();
self::$instance->setup( $blog_id );
}

return self::$instance;
}

/**
* Performs setup tasks for the singleton. To be used exclusively after singleton instantitaion.
*
* @param string $blog_id Blog id.
*/
public function setup() {
// TODO: Replace Jetpack:: invocation.
if ( ! \Jetpack::is_connection_ready() || ! $this->is_search_supported() ) {
/**
* Fires when the Jetpack Search fails and would fallback to MySQL.
*
* @module search
* @since 7.9.0
*
* @param string $reason Reason for Search fallback.
* @param mixed $data Data associated with the request, such as attempted search parameters.
*/
do_action( 'jetpack_search_abort', 'inactive', null );
return;
}

// TODO: Replace Jetpack:: invocation.
$this->jetpack_blog_id = \Jetpack::get_option( 'id' );

if ( ! $this->jetpack_blog_id ) {
do_action( 'jetpack_search_abort', 'no_blog_id', null );
public function setup( $blog_id ) {
if ( ! $blog_id ) {
return;
}

$this->jetpack_blog_id = $blog_id;
$this->init_hooks();
}

Expand Down Expand Up @@ -193,20 +168,6 @@ public function init_hooks() {
add_action( 'jetpack_deactivate_module_search', array( $this, 'move_search_widgets_to_inactive' ) );
}

/**
* Is search supported on the current plan
*
* @since 6.0
* Loads scripts for Tracks analytics library
*/
public function is_search_supported() {
// TODO: Replace Jetpack_Plan:: invocation.
if ( method_exists( 'Jetpack_Plan', 'supports' ) ) {
return \Jetpack_Plan::supports( 'search' );
}
return false;
}

/**
* Does this site have a VIP index
* Get the version number to use when loading the file. Allows us to bypass cache when developing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,68 @@ class Jetpack_Initializer extends Initializer {
* Initializes either the Classic Search or the Instant Search experience.
*/
public static function initialize() {
// TODO: Port the search widget to package.
// Check whether Jetpack Search should be initialized in the first place.
if ( ! self::is_connected() || ! self::is_search_supported() ) {
/**
* Fires when the Jetpack Search fails and would fallback to MySQL.
*
* @since Jetpack 7.9.0
* @param string $reason Reason for Search fallback.
* @param mixed $data Data associated with the request, such as attempted search parameters.
*/
do_action( 'jetpack_search_abort', 'inactive', null );
return;
}

$blog_id = \Jetpack::get_option( 'id' );
if ( ! $blog_id ) {
do_action( 'jetpack_search_abort', 'no_blog_id', null );
return;
}

// TODO: Port the search widget to package for milestone 2.
require_once JETPACK__PLUGIN_DIR . 'modules/widgets/search.php';

if ( Options::is_instant_enabled() ) {
// TODO: Port this class to the package.
// Enable the instant search experience.
Instant_Search::instance( $blog_id );

// TODO: Port the settings class to the package.
// Register instant search configurables as WordPress settings.
require_once JETPACK__PLUGIN_DIR . 'modules/search/class-jetpack-search-settings.php';
new \Jetpack_Search_Settings();

// TODO: Port the Customberg class to the package.
// Instantiate "Customberg", the live search configuration interface.
require_once JETPACK__PLUGIN_DIR . 'modules/search/class-jetpack-search-customberg.php';
\Automattic\Jetpack\Search\Jetpack_Search_Customberg::instance();

// Enable configuring instant search within the Customizer.
if ( class_exists( 'WP_Customize_Manager' ) ) {
// TODO: Port this class to the package.
require_once JETPACK__PLUGIN_DIR . 'modules/search/class-jetpack-search-customize.php';
new \Jetpack_Search_Customize();
}
Instant_Search::instance();
} else {
Classic_Search::instance();
// Enable the classic search experience.
Classic_Search::instance( $blog_id );
}
}

/**
* Check if site has been connected.
*/
public static function is_connected() {
return \Jetpack::is_connection_ready();
}

/**
* Check if search is supported by current plan.
*/
public static function is_search_supported() {
if ( method_exists( 'Jetpack_Plan', 'supports' ) ) {
return \Jetpack_Plan::supports( 'search' );
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Automattic\Jetpack\Search;

use Automattic\Jetpack\Search;
use Jetpack_WPES_Query_Builder;
use Automattic\Jetpack\Search\WPES\Query_Builder as Jetpack_WPES_Query_Builder;
use WP_Error;

/**
Expand Down Expand Up @@ -550,13 +550,12 @@ public function auto_config_excluded_post_types() {
* @since 9.6.0
*/
public function auto_config_woo_result_format() {
if ( ! method_exists( 'Jetpack', 'get_active_plugins' ) ) {
return false;
}

// TODO: Replace Jetpack:: invocation.
// 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', \Jetpack::get_active_plugins() ), true ) ) {
if ( ! in_array(
'woocommerce/woocommerce.php',
apply_filters( 'active_plugins', Search\Helper::get_active_plugins() ),
true
) ) {
return false;
}

Expand Down

0 comments on commit e7bab4b

Please sign in to comment.