Last active
May 29, 2024 04:05
-
-
Save carstingaxion/f98c213aad40d6413fe50eac1cc92228 to your computer and use it in GitHub Desktop.
Show post_type slug, post_type labels or post_type supports using this bound block.
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
{ | |
"meta": { | |
"title": "Install mu-plugin from a gist", | |
"author": "carstenbach", | |
"description": "Install and activate a WordPress must-use plugin from a .php file stored in a gist.", | |
"categories": [ | |
"mu-plugins" | |
] | |
}, | |
"landingPage": "/wp-admin/post.php?post=4&action=edit", | |
"preferredVersions": { | |
"wp": "beta", | |
"php": "8.0" | |
}, | |
"steps": [ | |
{ | |
"step": "login" | |
}, | |
{ | |
"step": "writeFile", | |
"path": "/wordpress/wp-content/post_content.md", | |
"data": { | |
"resource": "url", | |
"url": "https://gist.githubusercontent.com/carstingaxion/f98c213aad40d6413fe50eac1cc92228/raw/d6d1918c29b9e36d04d97dd50e0772e8088fc669/post_content.md" | |
} | |
}, | |
{ | |
"step": "wp-cli", | |
"command": "wp post create --post_title='post_type bound block' --post_status='published' /wordpress/wp-content/post_content.md" | |
}, | |
{ | |
"step": "writeFile", | |
"path": "/wordpress/wp-content/mu-plugins/0-plugin.php", | |
"data": { | |
"resource": "url", | |
"url": "https://gist.githubusercontent.com/carstingaxion/f98c213aad40d6413fe50eac1cc92228/raw/0d47fb1441b5e5aeac161a2f000575614f6f12f3/cbprototype__post-type-block--bindings-api.php" | |
} | |
} | |
] | |
} |
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
<?php | |
/** | |
* Plugin Name: 'post_type' Block via Block Bindings API | |
* Description: Show post_type slug, post_type labels or post_type supports using this bound block. | |
* Author: Carsten Bach, Liam Gladdy | |
* Version: 0.1-alpha1 | |
* | |
* @package cbprototype | |
* @link https://gist.github.com/carstingaxion/f98c213aad40d6413fe50eac1cc92228 | |
* | |
* @see Based on the skeleton of https://github.com/lgladdy/acf-block-bindings | |
* | |
* @example 1 Get the current post type slug. | |
* | |
* <!-- wp:paragraph { | |
* "placeholder":"POST TYPE", | |
* "metadata":{ | |
* "bindings":{ | |
* "content":{ | |
* "source":"cbprototype/posttype", | |
* } | |
* }, | |
* "name":"POST TYPE" | |
* } | |
* } --> | |
* <p></p> | |
* <!-- /wp:paragraph --> | |
* | |
* @example 2 Get a specific label from the current post type. | |
* @see https://developer.wordpress.org/reference/functions/get_post_type_labels/ | |
* | |
* <!-- wp:paragraph { | |
* "placeholder":"POST TYPE (label:singular_name)", | |
* "metadata":{ | |
* "bindings":{ | |
* "content":{ | |
* "source":"cbprototype/posttype", | |
* "args": { | |
* "label":"singular_name" | |
* } | |
* } | |
* }, | |
* "name":"POST TYPE (label:singular_name)" | |
* } | |
* } --> | |
* <p></p> | |
* <!-- /wp:paragraph --> | |
* | |
* @example 3 Get a specific post_type_support from the current post type. | |
* @see https://developer.wordpress.org/reference/functions/get_all_post_type_supports/ | |
* @see https://developer.wordpress.org/reference/functions/post_type_supports/#more-information | |
* | |
* <!-- wp:paragraph { | |
* "placeholder":"POST TYPE (support:author)", | |
* "metadata":{ | |
* "bindings":{ | |
* "content":{ | |
* "source":"cbprototype/posttype", | |
* "args": { | |
* "support":"author" | |
* } | |
* } | |
* }, | |
* "name":"POST TYPE (support:author)" | |
* } | |
* } --> | |
* <p></p> | |
* <!-- /wp:paragraph --> | |
*/ | |
/** | |
* Register plugin bindings on init. | |
* | |
* @since 0.1 | |
*/ | |
add_action( | |
'init', | |
function () { | |
// Do some checking ... maybe. | |
// And start. | |
register_cbprototype_block_bindings_source(); | |
} | |
); | |
/** | |
* Handle returing the block binding value for the current post type. | |
* | |
* @since 0.1 | |
* | |
* @param array $source_args An array of arguments passed via the metadata.bindings.$attribute.args property from the block. | |
* @param WP_Block $block_instance The current instance of the block the binding is connected to as a WP_Block object. | |
* @param mixed $attribute_name The current attribute set via the metadata.bindings.$attribute property on the block. | |
* | |
* @return string The block binding value | |
*/ | |
function cbprototype_get_block_binding_post_value( $source_args, $block_instance ) { | |
// If no 'label' or 'support' argument is set, bail early with the current post type. | |
if ( ! isset( $source_args['label'] ) && ! isset( $source_args['support'] ) ) { | |
// Get the post type from context. | |
return ( $block_instance->context['postType'] ) | |
? sprintf( | |
__( '<code>%s</code>', 'cbprototype' ), | |
$block_instance->context['postType'] | |
) | |
: null; | |
} | |
// Get the post type from context. | |
$post_type = $block_instance->context['postType']; | |
$post_type_object = get_post_type_object( $post_type ); | |
// If 'label' argument is set, return with the requested label of the current post type. | |
if ( ! empty( $source_args['label'] ) && $post_type_object instanceof WP_Post_Type ) { | |
// Check if this is an accepted key of a post_type_label. | |
if ( property_exists( $post_type_object->labels, $source_args['label'] ) ) { | |
return $post_type_object->labels->{$source_args['label']}; | |
} | |
} | |
// If 'support' argument is set, return with the requested post_type_support of the current post type. | |
if ( ! empty( $source_args['support'] ) && $post_type ) { | |
$supports = get_all_post_type_supports( $post_type ); | |
// Check if this is an accepted post_type_support key and if it is enabled. | |
$support = ( isset( $supports[ $source_args['support'] ] ) && $supports[ $source_args['support'] ] ) ? '✅' : '🚫'; | |
return sprintf( | |
__( '<code>%1$s</code> %2$s', 'cbprototype' ), | |
$source_args['support'], | |
$support | |
); | |
} | |
return null; | |
} | |
/** | |
* Registers the "cbprototype/posttype" source for the Block Bindings API. | |
* This allows you to access the post type of the current post. | |
* | |
* @since 0.1 | |
* | |
* $source_name: A unique name for your custom binding source in the form of namespace/slug. | |
* $source_properties: An array of properties to define your binding source: | |
* label: An internationalized text string to represent the binding source. Note: this is not currently shown anywhere in the UI. | |
* get_value_callback: A PHP callable (function, closure, etc.) that is called when a block’s attribute matches the $source_name parameter. | |
* uses_context: (Optional) Extends the block instance with an array of contexts if needed for the callback. | |
* For example, if you need the current post ID, you’d set this to [ 'postId' ]. | |
*/ | |
function register_cbprototype_block_bindings_source() { | |
register_block_bindings_source( | |
'cbprototype/posttype', | |
array( | |
'label' => __( 'Post Type', 'cbprototype' ), | |
'get_value_callback' => 'cbprototype_get_block_binding_post_value', | |
'uses_context' => [ 'postType' ], | |
) | |
); | |
} |
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
Show post_type slug, post_type labels or post_type supports using this bound block. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
WordPress/blueprints#48