Skip to content

Commit

Permalink
Apply suggested PR changes
Browse files Browse the repository at this point in the history
  • Loading branch information
akshitsethi committed May 12, 2022
1 parent e05f49b commit d33e248
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 34 deletions.
1 change: 1 addition & 0 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
convert_to_blocks_define( 'CONVERT_TO_BLOCKS_DIR', plugin_dir_path( __FILE__ ) );
convert_to_blocks_define( 'CONVERT_TO_BLOCKS_URL', plugin_dir_url( __FILE__ ) );
convert_to_blocks_define( 'CONVERT_TO_BLOCKS_SLUG', 'convert-to-blocks' );
convert_to_blocks_define( 'CONVERT_TO_BLOCKS_DEFAULT_POST_TYPES', [ 'post', 'page' ] );

/* Labels */

Expand Down
24 changes: 20 additions & 4 deletions includes/ConvertToBlocks/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,30 @@ public function init_locale() {
* @return bool
*/
public function post_type_supports_convert_to_blocks( $post_type ) {
$supports = post_type_supports( $post_type, 'convert-to-blocks' );
$use_defaults = apply_filters( 'convert_to_blocks_defaults', true );
$default_post_types = $this->get_default_post_types();
$supports = post_type_supports( $post_type, 'convert-to-blocks' );
$use_defaults = apply_filters( 'convert_to_blocks_defaults', true );
$default_post_types = $this->get_default_post_types();
$user_selected_post_types = get_option( sprintf( '%s_post_types', CONVERT_TO_BLOCKS_SLUG ), $default_post_types );

if ( ! $supports && $use_defaults && in_array( $post_type, $default_post_types, true ) ) {
$supports = true;
}

// For user-selected option via the Settings UI.
if ( false !== $user_selected_post_types ) {
$supports = false;

// If no post_type is selected.
if ( empty( $user_selected_post_types ) ) {
$supports = false;
}

// Check if post_type is selected by the user.
if ( in_array( $post_type, $user_selected_post_types, true ) ) {
$supports = true;
}
}

$supports = apply_filters( 'post_type_supports_convert_to_blocks', $supports, $post_type );

return $supports;
Expand Down Expand Up @@ -256,7 +272,7 @@ public function is_classic_editor_post( $post_id ) {
* @return array
*/
public function get_default_post_types() {
return apply_filters( 'convert_to_blocks_default_post_types', [ 'post', 'page' ] );
return apply_filters( 'convert_to_blocks_default_post_types', CONVERT_TO_BLOCKS_DEFAULT_POST_TYPES );
}

/**
Expand Down
59 changes: 29 additions & 30 deletions includes/ConvertToBlocks/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ public function register() {
add_action( 'admin_menu', [ $this, 'add_menu' ] );
add_action( 'admin_init', [ $this, 'register_section' ], 10 );
add_action( 'admin_init', [ $this, 'register_fields' ], 20 );

add_filter( 'post_type_supports_convert_to_blocks', [ $this, 'supported_post_types' ], PHP_INT_MAX, 2 );
add_action( 'admin_notices', [ $this, 'filter_notice' ], 10 );
}

/**
Expand Down Expand Up @@ -190,7 +189,10 @@ public function register_fields() {
* @return void
*/
public function field_post_types() {
$post_types = get_option( sprintf( '%s_post_types', CONVERT_TO_BLOCKS_SLUG ), [] );
$post_types = get_option(
sprintf( '%s_post_types', CONVERT_TO_BLOCKS_SLUG ),
apply_filters( 'convert_to_blocks_default_post_types', CONVERT_TO_BLOCKS_DEFAULT_POST_TYPES )
);
$output_html = '';

foreach ( $this->post_types as $post_type ) {
Expand Down Expand Up @@ -226,36 +228,33 @@ public function sanitize_post_types( $input ) {
}

/**
* Adds support for supported post types to the plugin.
*
* @param bool $supports Whether the post_type is supported or not.
* @param string $post_type Post type to be be checked.
*
* @return bool
* Adds an admin notice if a filter is active for `post_type_supports_convert_to_blocks` as
* this might overwrite the outcome of the settings stored in DB.
*/
public function supported_post_types( $supports, $post_type ) {
$supported_post_types = get_option( sprintf( '%s_post_types', CONVERT_TO_BLOCKS_SLUG ) );

// If the settings option does not exist in DB.
if ( false === $supported_post_types ) {
return $supports;
}

// If no post_type is selected.
if ( empty( $supported_post_types ) ) {
return false;
}

// Check if post_type is selected by the user.
if ( in_array( $post_type, $supported_post_types, true ) ) {
return true;
public function filter_notice() {
if ( ! has_filter( 'post_type_supports_convert_to_blocks' ) ) {
return;
}

/**
* This also overrides default post_types since we have the option to de-select the
* default ones via the settings panel.
*/
return false;
// URL to the settings panel.
$settings_url = add_query_arg(
[ 'page' => CONVERT_TO_BLOCKS_SLUG ],
admin_url( 'options-general.php' )
);
?>
<div class="notice notice-success is-dismissible">
<p>
<?php
printf(
/* translators: %1$s: link to switch to settings panel, %2$s: closing anchor tag */
esc_html__( 'A filter hook (post_type_supports_convert_to_blocks) is active which can lead to undesirable outcome. Kindly remove it and configure settings via the %1$sSettings Panel%2$s.', 'convert-to-blocks' ),
'<a href="' . esc_url( $settings_url ) . '">',
'</a>'
);
?>
</p>
</div>
<?php
}

}

0 comments on commit d33e248

Please sign in to comment.