Skip to content

Commit

Permalink
Merge pull request #72 from alleyinteractive/feature/issue-57/only-re…
Browse files Browse the repository at this point in the history
…gister-blocks-specified-post-types

Issue-57: Only register blocks on specified post types
  • Loading branch information
mogmarsh authored Oct 26, 2023
2 parents 4a5901e + 5e15228 commit cdfe4e5
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to `WP Curate` will be documented in this file.

## 1.3.0 - 2023-10-26

- Only show the blocks and register the meta on supported post types.
- Supported post types defaults to all Block Editor post types, but can be filtered by filtering `wp_curate_supported_post_types`.

## 1.2.0 - 2023-10-26

- Adds support for AND/OR operators in the Query Parameters, giving more control over what posts to show.
Expand Down
7 changes: 7 additions & 0 deletions blocks/post/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@
* @package wp-curate
*/

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 ) ) {
return;
}

// Register the block by passing the location of block.json.
register_block_type(
__DIR__
Expand Down
7 changes: 7 additions & 0 deletions blocks/query/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @package wp-curate
*/

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
Expand All @@ -13,6 +15,11 @@
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
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 ) ) {
return;
}

// Register the block by passing the location of block.json.
register_block_type(
__DIR__,
Expand Down
8 changes: 0 additions & 8 deletions config/post-meta.json
Original file line number Diff line number Diff line change
@@ -1,8 +0,0 @@
{
"wp_curate_deduplication": {
"post_types": [
"all"
],
"type": "boolean"
}
}
87 changes: 87 additions & 0 deletions src/class-supported-post-types.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Supported_Post_Types class file
*
* @package wp-curate
*/

namespace Alley\WP\WP_Curate;

/**
* The post types that should allow the Curation blocks and related meta.
*/
final class Supported_Post_Types {
/**
* Stores the supported types.
*
* @var string[]
*/
private array $supported_post_types;

/**
* Set up.
*/
public function __construct() {
$this->initialize_supported_post_types();
}

/**
* Initialize the supported post types.
*/
public function initialize_supported_post_types(): void {
// Get all post types.
$post_types = get_post_types( [], 'objects' );
$supported_post_types = array_filter( $post_types, fn( $type ) => $type->public && use_block_editor_for_post_type( $type->name ) );
$this->supported_post_types = ( wp_list_pluck( $supported_post_types, 'name' ) );
$this->register_post_meta();
}

/**
* Get the supported post types.
*
* @return string[]
*/
public function get_supported_post_types(): array {
return apply_filters( 'wp_curate_supported_post_types', $this->supported_post_types );
}

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

// Ensure we are in the admin before proceeding.
if ( is_admin() ) {
global $pagenow;

// 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 );
// phpcs:ignore WordPress.VIP.SuperGlobalInputUsage.AccessDetected, WordPress.Security.NonceVerification.NoNonceVerification, WordPress.Security.NonceVerification.Recommended
} elseif ( 'post-new.php' === $pagenow && ! empty( $_GET['post_type'] ) ) {
// phpcs:ignore WordPress.VIP.SuperGlobalInputUsage.AccessDetected, WordPress.Security.NonceVerification.Recommended
$post_type = sanitize_text_field( $_GET['post_type'] );
}
}
return $post_type;
}

/**
* Register the post meta on the supported post types.
*/
public function register_post_meta(): void {
register_meta_helper(
'post',
$this->get_supported_post_types(),
'wp_curate_deduplication',
[
'type' => 'boolean',
]
);
}
}
2 changes: 1 addition & 1 deletion wp-curate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: WP Curate
* Plugin URI: https://github.com/alleyinteractive/wp-curate
* Description: Plugin to curate homepages and other landing pages
* Version: 1.2.0
* Version: 1.3.0
* Author: Alley Interactive
* Author URI: https://github.com/alleyinteractive/wp-curate
* Requires at least: 6.3
Expand Down

0 comments on commit cdfe4e5

Please sign in to comment.