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

Loading enhancement for blocks and slotfills #90

Closed
wants to merge 10 commits into from
1 change: 1 addition & 0 deletions .phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<!-- Exclude a few directories and autogenerated files. -->
<exclude-pattern>build/</exclude-pattern>
<exclude-pattern>vendor/</exclude-pattern>
<exclude-pattern>tests/</exclude-pattern>

<!-- Define the prefixes that can be used by the plugin -->
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
Expand Down
5 changes: 4 additions & 1 deletion blocks/post/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
* @package wp-curate
*/

declare(strict_types=1);

use Alley\WP\WP_Curate\Supported_Post_Types;

/**
* Registers the wp-curate/post block using the metadata loaded from the `block.json` file.
*/
function wp_curate_post_block_init(): void {
$supported_post_types = new Supported_Post_Types();
if ( ! in_array( $supported_post_types->get_current_post_type(), $supported_post_types->get_supported_post_types(), true ) ) {

if ( ! $supported_post_types->load() ) {
return;
}

Expand Down
14 changes: 8 additions & 6 deletions blocks/query/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@
* @package wp-curate
*/

declare(strict_types=1);

use Alley\WP\WP_Curate\Supported_Post_Types;

/**
* Registers the block using the metadata loaded from the `block.json` file.
* Behind the scenes, it registers also all assets so they can be enqueued
* through the block editor in the corresponding context.
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
* Registers the wp-curate/query block using the metadata loaded from the `block.json` file.
*/
function wp_curate_query_block_init(): void {
$supported_post_types = new Supported_Post_Types();

if ( ! in_array( $supported_post_types->get_current_post_type(), $supported_post_types->get_supported_post_types(), true ) ) {
if ( ! $supported_post_types->load() ) {
return;
}

Expand All @@ -31,11 +29,15 @@ function wp_curate_query_block_init(): void {

/**
* Filter the post types that can be used in the Query block.
*
* @param string[] $allowed_post_types The post types that can be used in the Query block.
*/
$allowed_post_types = apply_filters( 'wp_curate_allowed_post_types', [ 'post' ] );

/**
* Filter the taxonomies that can be used in the Query block.
*
* @param string[] $allowed_taxonomies The taxonomies that can be used in the Query block.
*/
$allowed_taxonomies = apply_filters( 'wp_curate_allowed_taxonomies', [ 'category', 'post_tag' ] );

Expand Down
11 changes: 7 additions & 4 deletions entries/slotfills/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@
* @package wp-curate
*/

declare(strict_types=1);

namespace Alley\WP\WP_Curate;

/**
* Registers all slotfill assets so that they can be enqueued through Gutenberg in
* the corresponding context.
*/
function register_slotfills_scripts(): void {
$supported_post_types = new Supported_Post_Types();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to use a singleton for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does!

/**
* Filter the post types that will show the "Enable Deduplication" slotfill.
*
* @param array $allowed_post_types The post types that will show the "Enable Deduplication" slotfill.
* @param array $allowed_post_types The post types that will show the "Enable Deduplication" slotfill. Defaults to the supported post types.
*/
$allowed_post_types = apply_filters( 'wp_curate_duduplication_slotfill_post_types', [ 'page', 'post' ] );
$allowed_post_types = apply_filters( 'wp_curate_duduplication_slotfill_post_types', $supported_post_types->get_supported_post_types() );

$supported_post_types = new Supported_Post_Types();
if ( ! in_array( $supported_post_types->get_current_post_type(), $allowed_post_types, true ) ) {
if ( ! $supported_post_types->load( $allowed_post_types ) ) {
return;
}

Expand Down
44 changes: 40 additions & 4 deletions src/class-supported-post-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @package wp-curate
*/

declare(strict_types=1);

namespace Alley\WP\WP_Curate;

/**
Expand Down Expand Up @@ -50,12 +52,42 @@ public function get_supported_post_types(): array {
return apply_filters( 'wp_curate_supported_post_types', $this->supported_post_types );
}

/**
* Load WP Curate block or slotfill using supported post types.
*
* Remove the block/slotfill from the post editor only. Allowing it to be used in other contexts.
*
* @param string[] $post_types The post types to load. Defaults to the supported post types.
* @return bool
*/
public function load( array $post_types = [] ): bool {
$retval = true;

if ( empty( $post_types ) ) {
$post_types = $this->get_supported_post_types();
}

if ( is_admin() && ! in_array( $this->get_current_post_type(), $post_types, true ) ) {
$retval = false;
}

/**
* Load WP Curate block or slotfill.
*
* @param bool $retval Whether or not to load WP Curate block or slotfill.
* @param Supported_Post_Types $instance The Supported_Post_Types instance.
*/
return apply_filters( 'wp_curate_load', $retval, $this );
}

/**
* Get the current post type.
*
* @return string|false
* @global string $pagenow The filename of the current screen.
*
* @return string
*/
public function get_current_post_type(): string|false {
public function get_current_post_type(): string {
$post_type = '';

// Ensure we are in the admin before proceeding.
Expand All @@ -65,8 +97,11 @@ public function get_current_post_type(): string|false {
// phpcs:ignore WordPress.VIP.SuperGlobalInputUsage.AccessDetected, WordPress.Security.NonceVerification.NoNonceVerification, WordPress.Security.NonceVerification.Recommended
if ( 'post.php' === $pagenow && ! empty( $_GET['post'] ) ) {
// phpcs:ignore WordPress.VIP.SuperGlobalInputUsage.AccessDetected, WordPress.Security.NonceVerification.Recommended
$post_id = absint( $_GET['post'] );
$post_type = get_post_type( $post_id );
$post_type = get_post_type( absint( $_GET['post'] ) );

if ( ! $post_type ) {
$post_type = '';
}
// phpcs:ignore WordPress.VIP.SuperGlobalInputUsage.AccessDetected, WordPress.Security.NonceVerification.NoNonceVerification, WordPress.Security.NonceVerification.Recommended
} elseif ( 'post-new.php' === $pagenow ) {
attackant marked this conversation as resolved.
Show resolved Hide resolved
if ( ! empty( $_GET['post_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
renatonascalves marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -77,6 +112,7 @@ public function get_current_post_type(): string|false {
}
}
}

return $post_type;
}

Expand Down
25 changes: 0 additions & 25 deletions tests/feature/class-example-feature-test.php

This file was deleted.

52 changes: 52 additions & 0 deletions tests/feature/class-supported-post-types-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* WP Curate Tests: Supported Post Types Feature Test
*
* @package wp-curate
*/

namespace Alley\WP\WP_Curate\Tests\Feature;

use Alley\WP\WP_Curate\Supported_Post_Types;
use Alley\WP\WP_Curate\Tests\Test_Case;

/**
* Supported_Post_Types_Test feature test.
*/
class Supported_Post_Types_Test extends Test_Case {

public function test_get_default_post_types(): void {
$post_types = new Supported_Post_Types();

$this->assertIsArray( $post_types->get_supported_post_types() );
$this->assertContains( 'post', $post_types->get_supported_post_types() );
}

public function test_get_excluded_post_types(): void {
$post_types = new Supported_Post_Types();

add_filter( 'wp_curate_supported_post_types', fn () => [ 'page' ] );

$this->assertNotContains( 'post', $post_types->get_supported_post_types() );
$this->assertContains( 'page', $post_types->get_supported_post_types() );

remove_filter( 'wp_curate_supported_post_types', fn () => [ 'page' ] );
}

public function test_returning_default_load(): void {
$post_types = new Supported_Post_Types();

$this->assertTrue( $post_types->load() );
$this->assertTrue( $post_types->load( [ 'page' ] ) );
}

public function test_hooking_into_load(): void {
$post_types = new Supported_Post_Types();

add_filter( 'wp_curate_load', '__return_true' );

$this->assertTrue( $post_types->load() );

remove_filter( 'wp_curate_load', '__return_true' );
}
}