Skip to content

Conversation

@ockham
Copy link
Owner

@ockham ockham commented May 30, 2024

In a similar vein as @tjcafferkey's WordPress#6604, this introduces a new insert_hooked_blocks_into_rest_response filter and adds it to the rest_prepare_wp_navigation hook. (Note that this branch is actually based on Tom's; the reason is that it uses at least one function that's introduced there.)

This is part of an effort to move code from the Navigation block into Core. Specifically, insert_hooked_blocks_into_rest_response is based on block_core_navigation_insert_hooked_blocks_into_rest_response.

Testing instructions:

Preparation: Make sure you're using a Block Theme to test, ideally TT4. Add the following code to your site (e.g. to your theme's functions.php).

function register_logout_block_as_navigation_last_child( $hooked_blocks, $position, $anchor_block, $context ) {
	if ( $anchor_block === 'core/navigation' && $position === 'last_child' ) {
		$hooked_blocks[] = 'core/loginout';
	}

	return $hooked_blocks;
}

add_filter( 'hooked_block_types', 'register_logout_block_as_navigation_last_child', 10, 4 );
  1. If there's an existing wp_navigation post object for you Navigation menu in your database, make sure that it doesn't have any _wp_ignored_hooked_blocks post meta set. (You can check the latter via npm run wp-env run cli wp post meta list <postId>.) Otherwise, you'll need to delete said post meta.
  2. Go to the site editor and verify that the Login/out button block is added to the Navigation menu. You will probably see two copies. The reason is that the Navigation block code still keeps inserting hooked blocks on its own.
  3. To avoid the latter, apply the following patch, and reload the editor. You should now see only one copy of the Login/out block added.
diff --git a/src/wp-includes/blocks/navigation.php b/src/wp-includes/blocks/navigation.php
index ba1644cf60..4a62295c09 100644
--- a/src/wp-includes/blocks/navigation.php
+++ b/src/wp-includes/blocks/navigation.php
@@ -1566,10 +1566,3 @@ function block_core_navigation_insert_hooked_blocks_into_rest_response( $respons
  */
 $rest_prepare_wp_navigation_core_callback = 'block_core_navigation_' . 'insert_hooked_blocks_into_rest_response';
 
-/*
- * Injection of hooked blocks into the Navigation block relies on some functions present in WP >= 6.5
- * that are not present in Gutenberg's WP 6.5 compatibility layer.
- */
-if ( function_exists( 'set_ignored_hooked_blocks_metadata' ) && ! has_filter( 'rest_prepare_wp_navigation', $rest_prepare_wp_navigation_core_callback ) ) {
-       add_filter( 'rest_prepare_wp_navigation', 'block_core_navigation_insert_hooked_blocks_into_rest_response', 10, 3 );
-}

I will follow up with a Gutenberg PR to make the Navigation block's insertion of hooked blocks conditional. That other PR will have to get merged and sync'd to Core before we can land this change.

Trac ticket: https://core.trac.wordpress.org/ticket/60759


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@ockham ockham self-assigned this May 30, 2024
@ockham ockham closed this May 30, 2024
@ockham
Copy link
Owner Author

ockham commented May 30, 2024

Moved to WordPress#6677.

ockham pushed a commit that referenced this pull request Jun 18, 2024
When saving options from the Settings page, include the `'ping_sites'` option in the allowed "writing" options list only when the `'blog_public'` option is `'1'`.

Fixes a PHP 8.1 and above "null to non-nullable" deprecation notice in `sanitize_option()` ([https://core.trac.wordpress.org/browser/trunk/src/wp-includes/formatting.php?annotate=blame#L4952 which happens when here] as part of [22255]):

{{{
Deprecated: explode(): Passing null to parameter #2 ($string) of type string is deprecated in .../wp-includes/formatting.php
}}}

**Explanation**

[https://developer.wordpress.org/apis/options/#writing Per the documentation], the `ping_sites` option requires the `'blog_public'` option to have a value of `'1'` and must be a `string` data type. `null` is not valid for this option.

The relationship between the 2 options shows itself in the `options-writing.php` code ([https://core.trac.wordpress.org/browser/tags/6.5.4/src/wp-admin/options-writing.php#L233 shown here] and in [4326]), as the `textarea#ping_sites` only renders when `'1' === get_option( 'blog_public' )`.

**What happens if `'blog_public'` is not `'1'`?**

The `'ping_sites'` option will not be a field on the page. Upon saving:

* HTTP POST (`$_POST`) does not include `'ping_sites'`. 
* Before this commit:
   * The [https://core.trac.wordpress.org/browser/trunk/src/wp-admin/options.php#L333 option's value was set to] `null` before being passed to `update_option()`. 
   * `update_option()` invokes `sanitize_option()`.
   * A `null` value for the `'ping_sites'` case was passed to `explode()`, which threw a deprecation notice on PHP 8.1 and above.
* With this commit, the `'ping_sites'` option is no longer included in the allow list and thus will not be passed to `update_options()` > `sanitize_option()` > `explode()`.

Follow-up to [22255], [12825], [4326], [949].

Props kitchin, SergeyBiryukov, swissspidy, devmuhib, rajinsharwar, hellofromTonya.
Fixes #59818.

git-svn-id: https://develop.svn.wordpress.org/trunk@58425 602fd350-edb4-49c9-b593-d223f7449a82
ockham pushed a commit that referenced this pull request Jul 2, 2024
…t_mime_types().

Fixes a PHP 8.1 and above "null to non-nullable" deprecation notice in `get_available_post_mime_types()`:

{{{
Deprecated: preg_match(): Passing null to parameter #2 ($subject) of type string is deprecated in ./wp-includes/post.php on line 3395
}}}

[https://developer.wordpress.org/reference/functions/get_available_post_mime_types/#return This function is documented] to:
* Return `An array of MIME types.`
* as an array of `string`s, i.e. `string[]`.

A `null` or empty element within the returned array is not a valid MIME type. If a `null` exists in the returned array, it is the root cause of PHP throwing the deprecation notice.

This commit removes the `null` and empty elements from the returned array of MIME types. It also adds a unit test.

Follow-up to [56623], [56452].

Props nosilver4u, jrf, ironprogrammer, antpb, antonvlasenko, rajinsharwar, hellofromTonya. 
Fixes #59195.

git-svn-id: https://develop.svn.wordpress.org/trunk@58437 602fd350-edb4-49c9-b593-d223f7449a82
ockham pushed a commit that referenced this pull request Sep 15, 2025
…pgrade_690()`.

This resolves an error on Multisite networks where an individual site doesn't have plugins active:
{{{
array_search(): Argument #2 ($haystack) must be of type array, string given
}}}

Follow-up to [60666], [60721].

Props dd32.
See #53323.

git-svn-id: https://develop.svn.wordpress.org/trunk@60725 602fd350-edb4-49c9-b593-d223f7449a82
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants