Skip to content

Commit

Permalink
Plugin: Remove 'function_exists' checks for methods with 'gutenberg' …
Browse files Browse the repository at this point in the history
…prefix (WordPress#65260)

* Plugin: Remove 'function_exists' checks for methods with 'gutenberg' prefix
* Move code into the correct compat file

Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: ramonjd <[email protected]>
  • Loading branch information
3 people authored Sep 12, 2024
1 parent f073eb1 commit f1403c4
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 145 deletions.
160 changes: 100 additions & 60 deletions lib/compat/wordpress-6.6/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,76 +88,116 @@ function gutenberg_register_global_styles_revisions_endpoints() {

add_action( 'rest_api_init', 'gutenberg_register_global_styles_revisions_endpoints' );

if ( ! function_exists( 'gutenberg_register_wp_rest_themes_stylesheet_directory_uri_field' ) ) {
/**
* Adds `stylesheet_uri` fields to WP_REST_Themes_Controller class.
*/
function gutenberg_register_wp_rest_themes_stylesheet_directory_uri_field() {
register_rest_field(
'theme',
'stylesheet_uri',
array(
'get_callback' => function ( $item ) {
if ( ! empty( $item['stylesheet'] ) ) {
$theme = wp_get_theme( $item['stylesheet'] );
$current_theme = wp_get_theme();
if ( $theme->get_stylesheet() === $current_theme->get_stylesheet() ) {
return get_stylesheet_directory_uri();
} else {
return $theme->get_stylesheet_directory_uri();
}
/**
* Adds `stylesheet_uri` fields to WP_REST_Themes_Controller class.
*/
function gutenberg_register_wp_rest_themes_stylesheet_directory_uri_field() {
register_rest_field(
'theme',
'stylesheet_uri',
array(
'get_callback' => function ( $item ) {
if ( ! empty( $item['stylesheet'] ) ) {
$theme = wp_get_theme( $item['stylesheet'] );
$current_theme = wp_get_theme();
if ( $theme->get_stylesheet() === $current_theme->get_stylesheet() ) {
return get_stylesheet_directory_uri();
} else {
return $theme->get_stylesheet_directory_uri();
}
}

return null;
},
'schema' => array(
'type' => 'string',
'description' => __( 'The uri for the theme\'s stylesheet directory.', 'gutenberg' ),
'format' => 'uri',
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
),
)
);
}
return null;
},
'schema' => array(
'type' => 'string',
'description' => __( 'The uri for the theme\'s stylesheet directory.', 'gutenberg' ),
'format' => 'uri',
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
),
)
);
}
add_action( 'rest_api_init', 'gutenberg_register_wp_rest_themes_stylesheet_directory_uri_field' );

if ( ! function_exists( 'gutenberg_register_wp_rest_themes_template_directory_uri_field' ) ) {
/**
* Adds `template_uri` fields to WP_REST_Themes_Controller class.
*/
function gutenberg_register_wp_rest_themes_template_directory_uri_field() {
register_rest_field(
'theme',
'template_uri',
array(
'get_callback' => function ( $item ) {
if ( ! empty( $item['stylesheet'] ) ) {
$theme = wp_get_theme( $item['stylesheet'] );
$current_theme = wp_get_theme();
if ( $theme->get_stylesheet() === $current_theme->get_stylesheet() ) {
return get_template_directory_uri();
} else {
return $theme->get_template_directory_uri();
}
/**
* Adds `template_uri` fields to WP_REST_Themes_Controller class.
*/
function gutenberg_register_wp_rest_themes_template_directory_uri_field() {
register_rest_field(
'theme',
'template_uri',
array(
'get_callback' => function ( $item ) {
if ( ! empty( $item['stylesheet'] ) ) {
$theme = wp_get_theme( $item['stylesheet'] );
$current_theme = wp_get_theme();
if ( $theme->get_stylesheet() === $current_theme->get_stylesheet() ) {
return get_template_directory_uri();
} else {
return $theme->get_template_directory_uri();
}
}

return null;
},
'schema' => array(
'type' => 'string',
'description' => __( 'The uri for the theme\'s template directory. If this is a child theme, this refers to the parent theme, otherwise this is the same as the theme\'s stylesheet directory.', 'gutenberg' ),
'format' => 'uri',
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
),
)
);
}
return null;
},
'schema' => array(
'type' => 'string',
'description' => __( 'The uri for the theme\'s template directory. If this is a child theme, this refers to the parent theme, otherwise this is the same as the theme\'s stylesheet directory.', 'gutenberg' ),
'format' => 'uri',
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
),
)
);
}
add_action( 'rest_api_init', 'gutenberg_register_wp_rest_themes_template_directory_uri_field' );

/**
* Adds `template` and `template_lock` fields to WP_REST_Post_Types_Controller class.
*/
function gutenberg_register_wp_rest_post_types_controller_fields() {
register_rest_field(
'type',
'template',
array(
'get_callback' => function ( $item ) {
$post_type = get_post_type_object( $item['slug'] );
if ( ! empty( $post_type ) ) {
return $post_type->template ?? array();
}
},
'schema' => array(
'type' => 'array',
'description' => __( 'The block template associated with the post type.', 'gutenberg' ),
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
),
)
);
register_rest_field(
'type',
'template_lock',
array(
'get_callback' => function ( $item ) {
$post_type = get_post_type_object( $item['slug'] );
if ( ! empty( $post_type ) ) {
return ! empty( $post_type->template_lock ) ? $post_type->template_lock : false;
}
},
'schema' => array(
'type' => array( 'string', 'boolean' ),
'enum' => array( 'all', 'insert', 'contentOnly', false ),
'description' => __( 'The template_lock associated with the post type, or false if none.', 'gutenberg' ),
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
),
)
);
}
add_action( 'rest_api_init', 'gutenberg_register_wp_rest_post_types_controller_fields' );

/**
* Preload theme and global styles paths to avoid flash of variation styles in post editor.
*
Expand Down
57 changes: 27 additions & 30 deletions lib/experimental/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,45 +78,42 @@ function wp_enqueue_block_view_script( $block_name, $args ) {
}
}

/*
/**
* Registers a new block style for one or more block types.
*
* WP_Block_Styles_Registry was marked as `final` in core so it cannot be
* updated via Gutenberg to allow registration of a style across multiple
* block types as well as with an optional style object. This function will
* support the desired functionality until the styles registry can be updated
* in core.
*
* @param string|array $block_name Block type name including namespace or array of namespaced block type names.
* @param array $style_properties Array containing the properties of the style name, label,
* style_handle (name of the stylesheet to be enqueued),
* inline_style (string containing the CSS to be added),
* style_data (theme.json-like object to generate CSS from).
*
* @return bool True if all block styles were registered with success and false otherwise.
*/
if ( ! function_exists( 'gutenberg_register_block_style' ) ) {
/**
* Registers a new block style for one or more block types.
*
* @param string|array $block_name Block type name including namespace or array of namespaced block type names.
* @param array $style_properties Array containing the properties of the style name, label,
* style_handle (name of the stylesheet to be enqueued),
* inline_style (string containing the CSS to be added),
* style_data (theme.json-like object to generate CSS from).
*
* @return bool True if all block styles were registered with success and false otherwise.
*/
function gutenberg_register_block_style( $block_name, $style_properties ) {
if ( ! is_string( $block_name ) && ! is_array( $block_name ) ) {
_doing_it_wrong(
__METHOD__,
__( 'Block name must be a string or array.', 'gutenberg' ),
'6.6.0'
);
function gutenberg_register_block_style( $block_name, $style_properties ) {
if ( ! is_string( $block_name ) && ! is_array( $block_name ) ) {
_doing_it_wrong(
__METHOD__,
__( 'Block name must be a string or array.', 'gutenberg' ),
'6.6.0'
);

return false;
}
return false;
}

$block_names = is_string( $block_name ) ? array( $block_name ) : $block_name;
$result = true;
$block_names = is_string( $block_name ) ? array( $block_name ) : $block_name;
$result = true;

foreach ( $block_names as $name ) {
if ( ! WP_Block_Styles_Registry::get_instance()->register( $name, $style_properties ) ) {
$result = false;
}
foreach ( $block_names as $name ) {
if ( ! WP_Block_Styles_Registry::get_instance()->register( $name, $style_properties ) ) {
$result = false;
}

return $result;
}

return $result;
}
62 changes: 7 additions & 55 deletions lib/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,60 +19,12 @@ function gutenberg_register_global_styles_endpoints() {
}
add_action( 'rest_api_init', 'gutenberg_register_global_styles_endpoints' );

if ( ! function_exists( 'gutenberg_register_edit_site_export_controller_endpoints' ) ) {
/**
* Registers the Edit Site Export REST API routes.
*/
function gutenberg_register_edit_site_export_controller_endpoints() {
$edit_site_export_controller = new WP_REST_Edit_Site_Export_Controller_Gutenberg();
$edit_site_export_controller->register_routes();
}
}

add_action( 'rest_api_init', 'gutenberg_register_edit_site_export_controller_endpoints' );

if ( ! function_exists( 'gutenberg_register_wp_rest_post_types_controller_fields' ) ) {
/**
* Adds `template` and `template_lock` fields to WP_REST_Post_Types_Controller class.
*/
function gutenberg_register_wp_rest_post_types_controller_fields() {
register_rest_field(
'type',
'template',
array(
'get_callback' => function ( $item ) {
$post_type = get_post_type_object( $item['slug'] );
if ( ! empty( $post_type ) ) {
return $post_type->template ?? array();
}
},
'schema' => array(
'type' => 'array',
'description' => __( 'The block template associated with the post type.', 'gutenberg' ),
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
),
)
);
register_rest_field(
'type',
'template_lock',
array(
'get_callback' => function ( $item ) {
$post_type = get_post_type_object( $item['slug'] );
if ( ! empty( $post_type ) ) {
return ! empty( $post_type->template_lock ) ? $post_type->template_lock : false;
}
},
'schema' => array(
'type' => array( 'string', 'boolean' ),
'enum' => array( 'all', 'insert', 'contentOnly', false ),
'description' => __( 'The template_lock associated with the post type, or false if none.', 'gutenberg' ),
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
),
)
);
}
/**
* Registers the Edit Site Export REST API routes.
*/
function gutenberg_register_edit_site_export_controller_endpoints() {
$edit_site_export_controller = new WP_REST_Edit_Site_Export_Controller_Gutenberg();
$edit_site_export_controller->register_routes();
}
add_action( 'rest_api_init', 'gutenberg_register_wp_rest_post_types_controller_fields' );
add_action( 'rest_api_init', 'gutenberg_register_edit_site_export_controller_endpoints' );

0 comments on commit f1403c4

Please sign in to comment.