Skip to content

Commit

Permalink
Merge pull request #3845 from 10up/JiveDig/develop
Browse files Browse the repository at this point in the history
Adds filter for using facets in the block editor for posts
  • Loading branch information
felipeelia authored Feb 28, 2024
2 parents 64fb22c + 6d2f4bc commit a926b83
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
30 changes: 28 additions & 2 deletions includes/classes/Feature/Facets/Facets.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,17 @@ public function __construct() {
public function setup() {
global $pagenow;

// This feature should not run while in the editor.
if ( in_array( $pagenow, [ 'post-new.php', 'post.php' ], true ) ) {
$in_editor = in_array( $pagenow, [ 'post-new.php', 'post.php' ], true );

/**
* Filter if facet should be enabled in the editor. Default: false
*
* @hook ep_facet_enabled_in_editor
* @since 5.1.0
* @param {bool} $enabled
* @return {bool} If enabled or not
*/
if ( $in_editor && ! apply_filters( 'ep_facet_enabled_in_editor', false ) ) {
return;
}

Expand All @@ -131,6 +140,23 @@ public function setup() {
add_action( 'rest_api_init', [ $this, 'setup_endpoints' ] );
}

/**
* Unsetup Facets related hooks
*
* @since 5.1.0
*/
public function tear_down() {
remove_filter( 'widget_types_to_hide_from_legacy_widget_block', [ $this, 'hide_legacy_widget' ] );
remove_action( 'ep_valid_response', [ $this, 'get_aggs' ] );
remove_action( 'wp_enqueue_scripts', [ $this, 'front_scripts' ] );
remove_action( 'enqueue_block_editor_assets', [ $this, 'front_scripts' ] );
remove_action( 'ep_feature_box_settings_facets', [ $this, 'settings' ] );
remove_filter( 'ep_post_formatted_args', [ $this, 'set_agg_filters' ] );
remove_action( 'pre_get_posts', [ $this, 'facet_query' ] );
remove_filter( 'ep_post_filters', [ $this, 'apply_facets_filters' ] );
remove_action( 'rest_api_init', [ $this, 'setup_endpoints' ] );
}

/**
* Dashboard facet settings
*
Expand Down
62 changes: 62 additions & 0 deletions tests/php/features/TestFacet.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,68 @@
* Facet test class
*/
class TestFacet extends BaseTestCase {
/**
* Clean up after each test.
*
* @since 5.1.0
*/
public function tear_down() {
parent::tear_down();

$GLOBALS['pagenow'] = '';
}

/**
* Test the setup method
*
* @since 5.1.0
* @group facets
*/
public function test_setup() {
$facet_feature = Features::factory()->get_registered_feature( 'facets' );
$facet_feature->setup();

$this->assertSame( 10, has_action( 'rest_api_init', [ $facet_feature, 'setup_endpoints' ] ) );
}

/**
* Test the feature is not loaded in the editor screen
*
* @since 5.1.0
* @group facets
*/
public function test_setup_editor_screen() {
$GLOBALS['pagenow'] = 'post-new.php';
set_current_screen( 'post-new.php' );

$facet_feature = Features::factory()->get_registered_feature( 'facets' );
$facet_feature->tear_down();
$facet_feature->setup();

$this->assertFalse( has_action( 'rest_api_init', [ $facet_feature, 'setup_endpoints' ] ) );

set_current_screen( 'front' );
}

/**
* Test the ep_facet_enabled_in_editor filter
*
* @since 5.1.0
* @group facets
*/
public function test_setup_ep_facet_enabled_in_editor() {
add_filter( 'ep_facet_enabled_in_editor', '__return_true' );

$GLOBALS['pagenow'] = 'post-new.php';
set_current_screen( 'post-new.php' );

$facet_feature = Features::factory()->get_registered_feature( 'facets' );
$facet_feature->tear_down();
$facet_feature->setup();

$this->assertSame( 10, has_action( 'rest_api_init', [ $facet_feature, 'setup_endpoints' ] ) );
}

/**
* Test facet type registration
*
Expand Down

0 comments on commit a926b83

Please sign in to comment.