Skip to content

Commit

Permalink
Merge pull request #85 from alleyinteractive/feature/post-support
Browse files Browse the repository at this point in the history
Better support for the default `post` behavior
  • Loading branch information
renatonascalves authored Oct 31, 2023
2 parents 816e53a + 9e1e945 commit ce2e532
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 49 deletions.
1 change: 1 addition & 0 deletions blocks/query/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
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;
}
Expand Down
68 changes: 22 additions & 46 deletions entries/slotfills/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,35 @@

namespace Alley\WP\WP_Curate;

add_action(
'enqueue_block_editor_assets',
__NAMESPACE__ . '\action_enqueue_slotfills_assets'
);

/**
* Registers all slotfill assets so that they can be enqueued through Gutenberg in
* the corresponding context.
*/
function register_slotfills_scripts(): void {
// Automatically load dependencies and version.
$asset_file = include __DIR__ . '/index.asset.php';

/**
* 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.
*/
$allowed_post_types = apply_filters( 'wp_curate_duduplication_slotfill_post_types', [ 'page', 'post' ] );

$post_type = get_editor_post_type();

if ( in_array( $post_type, $allowed_post_types, true ) ) {
wp_register_script(
'wp-curate_slotfills',
plugins_url( 'index.js', __FILE__ ),
$asset_file['dependencies'],
$asset_file['version'],
true
);
wp_set_script_translations( 'wp-curate_slotfills', 'wp-curate' );
$supported_post_types = new Supported_Post_Types();
if ( ! in_array( $supported_post_types->get_current_post_type(), $allowed_post_types, true ) ) {
return;
}

// Automatically load dependencies and version.
$asset_file = include __DIR__ . '/index.asset.php';

wp_register_script(
'wp-curate_slotfills',
plugins_url( 'index.js', __FILE__ ),
$asset_file['dependencies'],
$asset_file['version'],
true
);

wp_set_script_translations( 'wp-curate_slotfills', 'wp-curate' );
}
add_action( 'init', __NAMESPACE__ . '\register_slotfills_scripts' );

Expand All @@ -48,30 +47,7 @@ function register_slotfills_scripts(): void {
function action_enqueue_slotfills_assets(): void {
wp_enqueue_script( 'wp-curate_slotfills' );
}

/**
* Gets the post type currently being edited.
*
* @return string|false
*/
function get_editor_post_type() {
// Set the default post type.
$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;
}
add_action(
'enqueue_block_editor_assets',
__NAMESPACE__ . '\action_enqueue_slotfills_assets'
);
15 changes: 12 additions & 3 deletions src/class-supported-post-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public function initialize_supported_post_types(): void {
* @return string[]
*/
public function get_supported_post_types(): array {
/**
* Filter the WP Curate supported post types.
*
* @param string[] $supported_post_types The supported post types.
*/
return apply_filters( 'wp_curate_supported_post_types', $this->supported_post_types );
}

Expand All @@ -63,9 +68,13 @@ public function get_current_post_type(): string|false {
$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'] );
} elseif ( 'post-new.php' === $pagenow ) {
if ( ! empty( $_GET['post_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$post_type = sanitize_text_field( $_GET['post_type'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
} else {
// Default to post.
$post_type = 'post';
}
}
}
return $post_type;
Expand Down

0 comments on commit ce2e532

Please sign in to comment.