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

Post type setting field added #104

Merged
merged 7 commits into from
May 22, 2023
Merged
Changes from 2 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
41 changes: 40 additions & 1 deletion includes/ConvertToBlocks/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@
* ```
*/
class Plugin {
/**
* Post type field identifier key
*/
const POST_TYPE_FIELD = 'cb_supported_post_types';

/**
* Default post types to make CB supported
*
* @var array
*/
private static $default_post_types = array(
'post',
'page',
);

/**
* Singleton instance of the Plugin.
Expand Down Expand Up @@ -119,6 +133,30 @@ public function init_admin() {
new MigrationAgent(),
]
);

// Add setting section in writing page
add_settings_section( 'convert_to_block_settings', __( 'Convert to Block', 'convert-to-blocks' ), array( $this, 'settings_intro' ), 'writing' );
add_settings_field( 'convert_to_block_post_types', esc_html__( 'Post Types', 'convert-to-blocks' ), [ $this, 'convert_to_block_setting_form' ], 'writing', 'convert_to_block_settings' );
register_setting( 'writing', self::POST_TYPE_FIELD, array( 'type' => 'array' ) );
}

/**
* Render convert to block setting form
*
* @return void
*/
public function convert_to_block_setting_form() {
$post_types = get_post_types( array( 'show_in_rest' => true ) );
$options = get_option( self::POST_TYPE_FIELD, self::$default_post_types );

foreach ( $post_types as $type ) {
$post_type = get_post_type_object( $type );
$id = 'cb-supported-post-types-' . $type;
?>
<input id="<?php echo esc_attr( $id ); ?>" type="checkbox" name="<?php echo esc_attr( self::POST_TYPE_FIELD ); ?>[]" value="<?php echo esc_attr( $type ); ?>" <?php echo in_array( $type, $options, true ) ? 'checked="checked"' : ''; ?>/>
<label for="<?php echo esc_attr( $id ); ?>"><?php echo esc_html( $post_type->label ); ?></label><br/>
<?php
}
}

/**
Expand Down Expand Up @@ -257,7 +295,8 @@ 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' ] );
$defaults = get_option( self::POST_TYPE_FIELD, self::$default_post_types );
return apply_filters( 'convert_to_blocks_default_post_types', $defaults );
}

/**
Expand Down