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

Adds filter for using facets in the block editor for posts #3845

Merged
merged 18 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading