Skip to content

Commit

Permalink
Merge pull request #3688 from 10up/fix/issue-2865
Browse files Browse the repository at this point in the history
Tear down WooCommerce functionalities when the plugin is not active
  • Loading branch information
felipeelia authored Oct 11, 2023
2 parents fb3884e + 7215235 commit ef84003
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 44 deletions.
15 changes: 15 additions & 0 deletions includes/classes/Feature/WooCommerce/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ public function setup() {
add_action( 'pre_get_posts', [ $this, 'translate_args' ], 11, 1 );
}

/**
* Unsetup order related hooks
*
* @since 5.0.0
*/
public function tear_down() {
remove_filter( 'ep_sync_insert_permissions_bypass', [ $this, 'bypass_order_permissions_check' ] );
remove_filter( 'ep_prepare_meta_allowed_protected_keys', [ $this, 'allow_meta_keys' ] );
remove_filter( 'ep_post_sync_args_post_prepare_meta', [ $this, 'add_order_items_search' ], 20 );
remove_filter( 'ep_pc_skip_post_content_cleanup', [ $this, 'keep_order_fields' ], 20 );
remove_action( 'parse_query', [ $this, 'maybe_hook_woocommerce_search_fields' ], 1 );
remove_action( 'parse_query', [ $this, 'search_order' ], 11 );
remove_action( 'pre_get_posts', [ $this, 'translate_args' ], 11 );
}

/**
* Allow order creations on the front end to get synced
*
Expand Down
20 changes: 20 additions & 0 deletions includes/classes/Feature/WooCommerce/OrdersAutosuggest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,26 @@ public function setup() {
add_filter( 'posts_where', [ $this, 'maybe_set_posts_where' ], 10, 2 );
}

/**
* Un-setup feature functionality.
*
* @since 5.0.0
*/
public function tear_down() {
remove_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_assets' ] );
remove_filter( 'ep_after_update_feature', [ $this, 'after_update_feature' ] );
remove_filter( 'ep_after_sync_index', [ $this, 'epio_save_search_template' ] );
remove_filter( 'ep_saved_weighting_configuration', [ $this, 'epio_save_search_template' ] );
remove_filter( 'ep_indexable_post_status', [ $this, 'post_statuses' ] );
remove_filter( 'ep_indexable_post_types', [ $this, 'post_types' ] );
remove_action( 'rest_api_init', [ $this, 'rest_api_init' ] );
remove_filter( 'ep_post_sync_args', [ $this, 'filter_term_suggest' ] );
remove_filter( 'ep_post_mapping', [ $this, 'mapping' ] );
remove_action( 'ep_woocommerce_shop_order_search_fields', [ $this, 'set_search_fields' ] );
remove_filter( 'ep_index_posts_args', [ $this, 'maybe_query_password_protected_posts' ] );
remove_filter( 'posts_where', [ $this, 'maybe_set_posts_where' ] );
}

/**
* Get the endpoint for WooCommerce Orders search.
*
Expand Down
28 changes: 28 additions & 0 deletions includes/classes/Feature/WooCommerce/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,34 @@ public function setup() {
add_filter( 'ep_is_decaying_enabled', [ $this, 'maybe_disable_decaying' ], 10, 3 );
}

/**
* Un-setup product related hooks
*
* @since 5.0.0
*/
public function tear_down() {
remove_action( 'ep_formatted_args', [ $this, 'price_filter' ] );
remove_filter( 'ep_prepare_meta_allowed_protected_keys', [ $this, 'allow_meta_keys' ] );
remove_filter( 'ep_sync_taxonomies', [ $this, 'sync_taxonomies' ] );
remove_filter( 'ep_term_suggest_post_type', [ $this, 'suggest_wc_add_post_type' ] );
remove_filter( 'ep_facet_include_taxonomies', [ $this, 'add_product_attributes' ] );
remove_filter( 'ep_weighting_fields_for_post_type', [ $this, 'add_product_attributes_to_weighting' ] );
remove_filter( 'ep_weighting_default_post_type_weights', [ $this, 'add_product_default_post_type_weights' ] );
remove_filter( 'ep_prepare_meta_data', [ $this, 'add_variations_skus_meta' ] );
remove_filter( 'request', [ $this, 'admin_product_list_request_query' ], 9 );
remove_action( 'pre_get_posts', [ $this, 'translate_args' ], 11 );
remove_filter( 'ep_facet_tax_special_slug_taxonomies', [ $this, 'add_taxonomy_attributes' ] );

// Custom product ordering
remove_action( 'ep_admin_notices', [ $this, 'maybe_display_notice_about_product_ordering' ] );
remove_action( 'woocommerce_after_product_ordering', [ $this, 'action_sync_on_woocommerce_sort_single' ] );

// Settings for Weight results by date
remove_action( 'ep_weight_settings_after_search', [ $this, 'add_weight_settings_search' ] );
remove_filter( 'ep_feature_settings_schema', [ $this, 'add_weight_settings_search_schema' ] );
remove_filter( 'ep_is_decaying_enabled', [ $this, 'maybe_disable_decaying' ] );
}

/**
* Modifies main query to allow filtering by price with WooCommerce "Filter by price" widget.
*
Expand Down
72 changes: 37 additions & 35 deletions includes/classes/Feature/WooCommerce/WooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,53 @@ public function setup() {
return;
}

$this->products->setup();
$this->orders->setup();
add_action( 'switch_blog', [ $this, 'setup_or_tear_down' ] );

add_filter( 'ep_integrate_search_queries', [ $this, 'disallow_coupons' ], 10, 2 );

// These hooks are deprecated and will be removed in an upcoming major version of ElasticPress
add_filter( 'woocommerce_layered_nav_query_post_ids', [ $this, 'convert_post_object_to_id' ], 10, 4 );
add_filter( 'woocommerce_unfiltered_product_ids', [ $this, 'convert_post_object_to_id' ], 10, 4 );
add_action( 'ep_wp_query_search_cached_posts', [ $this, 'disallow_duplicated_query' ], 10, 2 );
$this->products->setup();
$this->orders->setup();

// Orders Autosuggest feature.
if ( $this->is_orders_autosuggest_enabled() ) {
$this->orders_autosuggest->setup();
}
}

/**
* Setup or tear down the functionality depending on the plugin being active for the current site.
*
* If the site wasn't initialized yet (it does not have its database tables created) we skip it.
*
* @since 5.0.0
* @param int $blog_id Blog ID
* @return void
*/
public function setup_or_tear_down( $blog_id ) {
if ( wp_is_site_initialized( $blog_id ) && \is_plugin_active( 'woocommerce/woocommerce.php' ) ) {
$this->setup();
} else {
$this->tear_down();
}
}

/**
* Un-setup all feature filters
*
* @since 5.0.0
*/
public function tear_down() {
remove_filter( 'ep_integrate_search_queries', [ $this, 'disallow_coupons' ] );

$this->products->tear_down();
$this->orders->tear_down();

// Orders Autosuggest feature.
if ( $this->is_orders_autosuggest_enabled() ) {
$this->orders_autosuggest->tear_down();
}
}

/**
* Given a WP_Query object, return its search term (if any)
*
Expand Down Expand Up @@ -396,19 +427,6 @@ public function whitelist_meta_keys( $meta, $post ) {
);
}

/**
* DEPRECATED. Make sure all loop shop post ins are IDS. We have to pass post objects here since we override
* the fields=>id query for the layered filter nav query
*
* @param array $posts Post object array.
* @since 2.1
* @return array
*/
public function convert_post_object_to_id( $posts ) {
_doing_it_wrong( __METHOD__, 'This filter was removed from WooCommerce and will be removed from ElasticPress in a future release.', '4.5.0' );
return $posts;
}

/**
* DEPRECATED. Index WooCommerce taxonomies
*
Expand All @@ -422,22 +440,6 @@ public function whitelist_taxonomies( $taxonomies, $post ) {
return $this->products->sync_taxonomies( $taxonomies );
}

/**
* DEPRECATED. Disallow duplicated ES queries on Orders page.
*
* @since 2.4
*
* @param array $value Original filter values.
* @param WP_Query $query WP_Query
*
* @return array
*/
public function disallow_duplicated_query( $value, $query ) {
_doing_it_wrong( __METHOD__, 'This filter was removed from WooCommerce and will be removed from ElasticPress in a future release.', '4.5.0' );

return $value;
}

/**
* DEPRECATED. Returns the WooCommerce-oriented post types in admin that EP will search
*
Expand Down
33 changes: 24 additions & 9 deletions tests/php/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,37 @@ function load_plugin() {
* @since 3.0
*/
function setup_wc() {
if ( class_exists( '\WC_Install' ) ) {
define( 'WP_UNINSTALL_PLUGIN', true );
if ( ! class_exists( '\WC_Install' ) ) {
return;
}

update_option( 'woocommerce_status_options', array( 'uninstall_data' => 1 ) );
include_once __DIR__ . '/../../vendor/woocommerce/uninstall.php';
define( 'WP_UNINSTALL_PLUGIN', true );

\WC_Install::install();
update_option( 'woocommerce_status_options', array( 'uninstall_data' => 1 ) );
include_once __DIR__ . '/../../vendor/woocommerce/uninstall.php';

$GLOBALS['wp_roles'] = new \WP_Roles();
\WC_Install::install();

echo 'Installing WooCommerce version ' . WC()->version . ' ...' . PHP_EOL; // phpcs:ignore
}
}
$GLOBALS['wp_roles'] = new \WP_Roles();

echo 'Installing WooCommerce version ' . WC()->version . ' ...' . PHP_EOL; // phpcs:ignore
}
tests_add_filter( 'setup_theme', __NAMESPACE__ . '\setup_wc' );

/**
* Set WooCommerce as an active plugin
*
* @since 5.0.0
* @param array $active_plugins Active plugins
* @return array
*/
function add_woocommerce_to_active_plugins( $active_plugins ) {
$active_plugins = (array) $active_plugins;
$active_plugins[] = 'woocommerce/woocommerce.php';
return $active_plugins;
}
tests_add_filter( 'option_active_plugins', __NAMESPACE__ . '\add_woocommerce_to_active_plugins' );

/**
* Completely skip looking up translations
*
Expand Down

0 comments on commit ef84003

Please sign in to comment.