-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Add/button dynamic url #74814
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
Open
Vrishabhsk
wants to merge
3
commits into
WordPress:trunk
Choose a base branch
from
Vrishabhsk:add/button-dynamic-url
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+621
−57
Open
Add/button dynamic url #74814
Changes from all commits
Commits
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 hidden or 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,103 @@ | ||
| <?php | ||
| /** | ||
| * Entity source for the block bindings (explicit entity args). | ||
| * | ||
| * @since 6.9.0 | ||
| * @package gutenberg | ||
| * @subpackage Block Bindings | ||
| */ | ||
|
|
||
| /** | ||
| * Gets value for Entity source. | ||
| * | ||
| * This source is intentionally **not** tied to the current post context. | ||
| * Consumers must pass entity identifiers via `$source_args`. | ||
| * | ||
| * Supported args: | ||
| * - key: currently only supports `url` | ||
| * - kind: `post-type` | `taxonomy` | ||
| * - type: post type slug or taxonomy slug | ||
| * - id: entity ID | ||
| * | ||
| * @since 6.9.0 | ||
| * @access private | ||
| * | ||
| * @param array $source_args Array containing source arguments used to look up the value. | ||
| * @param WP_Block $block_instance The block instance. | ||
| * @return mixed The value computed for the source. | ||
| */ | ||
| function gutenberg_block_bindings_entity_get_value( array $source_args, $block_instance ) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to get test coverage here, e.g.,
|
||
| if ( empty( $source_args['key'] ) || 'url' !== $source_args['key'] ) { | ||
| return null; | ||
| } | ||
| if ( empty( $source_args['kind'] ) || empty( $source_args['type'] ) || empty( $source_args['id'] ) ) { | ||
| return null; | ||
| } | ||
|
|
||
| $kind = (string) $source_args['kind']; | ||
| $type = (string) $source_args['type']; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we check if the post type exists? E.g., if ( ! post_type_exists( $type ) ) {
return null;
} |
||
| $id = (int) $source_args['id']; | ||
|
|
||
| if ( $id <= 0 ) { | ||
| return null; | ||
| } | ||
|
|
||
| if ( 'post-type' === $kind ) { | ||
| $post = get_post( $id ); | ||
| if ( ! $post ) { | ||
| return null; | ||
| } | ||
| // Prevent unauthorized users from accessing non-public post data. | ||
| if ( ( ! is_post_publicly_viewable( $post ) && ! current_user_can( 'read_post', $id ) ) || post_password_required( $post ) ) { | ||
| return null; | ||
| } | ||
| $permalink = get_permalink( $id ); | ||
| return false === $permalink ? null : esc_url( $permalink ); | ||
| } | ||
|
|
||
| if ( 'taxonomy' === $kind ) { | ||
| // Map UI shorthand to taxonomy slug when using args. | ||
| $taxonomy = ( 'tag' === $type ) ? 'post_tag' : $type; | ||
|
|
||
| $term = get_term( $id, $taxonomy ); | ||
| if ( is_wp_error( $term ) || ! $term ) { | ||
| return null; | ||
| } | ||
|
|
||
| $taxonomy_object = get_taxonomy( $taxonomy ); | ||
| if ( ! $taxonomy_object || ! $taxonomy_object->publicly_queryable ) { | ||
| if ( ! current_user_can( 'read' ) ) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| $term_link = get_term_link( $term ); | ||
| return is_wp_error( $term_link ) ? null : esc_url( $term_link ); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Registers Entity source in the block bindings registry. | ||
| * | ||
| * @since 6.9.0 | ||
| * @access private | ||
| */ | ||
| function gutenberg_register_block_bindings_entity_source() { | ||
| if ( get_block_bindings_source( 'core/entity' ) ) { | ||
| // The source is already registered. | ||
| return; | ||
| } | ||
|
|
||
| register_block_bindings_source( | ||
| 'core/entity', | ||
| array( | ||
| 'label' => _x( 'Entity', 'block bindings source' ), | ||
| 'get_value_callback' => 'gutenberg_block_bindings_entity_get_value', | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| add_action( 'init', 'gutenberg_register_block_bindings_entity_source' ); | ||
|
|
||
This file contains hidden or 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.
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.
I know you've been waiting for a while for reviews, thanks for your patience!
I don't know if this will make the next WordPress release, but the next version is now
7.0.0