-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Update the Site Title block to use block bindings #67260
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ffb19a2
Implement basic site bindings source and hook it up to the site title…
talldan ee965d9
Implement server-side render callback with bindings
talldan f657dda
Switch to declaring the binding on the attribute itself
talldan d9deae8
Remove `getFieldsList` function for binding declaration - avoid showi…
talldan 1c3f559
Reduce diff size
talldan d1796a1
PHP linting things
talldan b60d3a7
Update fixture
talldan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
/** | ||
* Suite source for the block bindings. | ||
* | ||
* @since 6.8.0 | ||
* @package WordPress | ||
* @subpackage Block Bindings | ||
*/ | ||
|
||
|
||
if ( ! function_exists( '_block_bindings_site_get_value' ) ) { | ||
/** | ||
* Gets value for Site source. | ||
* | ||
* @since 6.8.0 | ||
* @access private | ||
* | ||
* @param array $source_args Array containing source arguments used to look up the override value. | ||
* Example: array( "key" => "foo" ). | ||
* @param WP_Block $block_instance The block instance. | ||
* @return mixed The value computed for the source. | ||
*/ | ||
function _block_bindings_site_get_value( array $source_args ) { | ||
if ( empty( $source_args['key'] ) ) { | ||
return null; | ||
} | ||
|
||
if ( 'title' === $source_args['key'] ) { | ||
return esc_html( get_bloginfo( 'name' ) ); | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
|
||
|
||
if ( ! function_exists( '_register_block_bindings_site_source' ) ) { | ||
/** | ||
* Registers Site source in the block bindings registry. | ||
* | ||
* @since 6.8.0 | ||
* @access private | ||
*/ | ||
function _register_block_bindings_site_source() { | ||
register_block_bindings_source( | ||
'core/site', | ||
array( | ||
'label' => _x( 'Site', 'block bindings source' ), | ||
'get_value_callback' => '_block_bindings_site_get_value', | ||
) | ||
); | ||
} | ||
|
||
add_action( 'init', '_register_block_bindings_site_source' ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { store as coreDataStore } from '@wordpress/core-data'; | ||
|
||
/** | ||
* Gets a list of site fields with their values and labels | ||
* to be consumed in the needed callbacks. | ||
* | ||
* @param {Object} select The select function from the data store. | ||
* @return {Object} List of post meta fields with their value and label. | ||
*/ | ||
const supportedFields = { title: { label: __( 'Title' ), type: 'string' } }; | ||
function getSiteFields( select ) { | ||
const { getEditedEntityRecord } = select( coreDataStore ); | ||
|
||
const entityValues = getEditedEntityRecord( 'root', 'site', undefined ); | ||
const siteFields = {}; | ||
Object.entries( supportedFields ).forEach( ( [ key, props ] ) => { | ||
// Don't include footnotes or private fields. | ||
siteFields[ key ] = { | ||
label: props.title || key, | ||
value: entityValues?.[ key ], | ||
type: props.type, | ||
}; | ||
} ); | ||
return siteFields; | ||
} | ||
|
||
export default { | ||
name: 'core/site', | ||
getValues( { select, bindings } ) { | ||
const metaFields = getSiteFields( select ); | ||
|
||
const newValues = {}; | ||
for ( const [ attributeName, source ] of Object.entries( bindings ) ) { | ||
// Use the value, the field label, or the field key. | ||
const fieldKey = source.args.key; | ||
const { value: fieldValue, label: fieldLabel } = | ||
metaFields?.[ fieldKey ] || {}; | ||
newValues[ attributeName ] = fieldValue ?? fieldLabel ?? fieldKey; | ||
} | ||
return newValues; | ||
}, | ||
setValues( { dispatch, bindings } ) { | ||
const newValues = {}; | ||
Object.values( bindings ).forEach( ( { args, newValue } ) => { | ||
newValues[ args.key ] = newValue; | ||
} ); | ||
|
||
dispatch( coreDataStore ).editEntityRecord( | ||
'root', | ||
'site', | ||
undefined, | ||
newValues | ||
); | ||
}, | ||
canUserEditValue( { select, args } ) { | ||
if ( ! supportedFields[ args.key ] ) { | ||
return false; | ||
} | ||
|
||
// Check that the user has the capability to edit post meta. | ||
return select( coreDataStore ).canUser( 'update', { | ||
kind: 'root', | ||
name: 'site', | ||
} ); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make this a generic "entity edit" binding where the connection would provide a "kind, name, property/key"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we'll need a matching server implementation. I guess REST API resources map fairly close to entities, but I'm wondering if there will be edge cases and whether there might need to be a translation layer.
I'll put it on the TODO list. 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generic entity edit sounds great on the client as a source for Block Bindings. Great idea. 👏
The specific details would need to be explored like validation whether all of them fit into the model, does the current user has permission to view or edit them. It wasn’t simple for Post Meta alone.
The server part for source registration might be a fun challenge, too. However there is so much potential 😀