Skip to content

Instantly share code, notes, and snippets.

@dlh01
dlh01 / editProps.ts
Last active February 26, 2024 04:21
Block editor
/*
attributes
clientId
context
insertBlocksAfter (fn)
isSelected
isSelectionEnabled
mergeBlocks (fn)
name
onRemove (fn)
@dlh01
dlh01 / gb5187.php
Last active September 17, 2023 02:06
Decorator pattern for serializing blocks
<?php
// https://github.com/WordPress/wordpress-develop/pull/5187
namespace GH5187;
interface Block_Content {
public function content( string $content, array $block ): string;
}
@dlh01
dlh01 / functions.php
Last active August 7, 2023 17:59
Infinite loop test theme
<?php
add_action(
'enqueue_block_editor_assets',
function () {
wp_enqueue_script(
'title-checker-js',
get_theme_file_uri( 'title-checker.js' ),
[],
null,
@dlh01
dlh01 / elasticsearch-extensions-example.php
Created February 1, 2023 03:41
Elasticsearch Extensions example
<?php
add_action(
'elasticsearch_extensions_config',
function ( $es_config ) {
$es_config
->restrict_post_types( [ 'post', 'page' ] )
->enable_empty_search()
->enable_post_type_aggregation()
->enable_taxonomy_aggregation( 'category' )
@dlh01
dlh01 / find-one-example.php
Created February 1, 2023 03:37
Find One example
<?php
$person = Alley\WP\find_one_post(
[
'meta_key' => 'twitter',
'meta_value' => '@potatomaster',
'post_type' => 'person',
]
); // ?WP_Post
@dlh01
dlh01 / wp-bulk-task-example.php
Last active February 1, 2023 03:36
WP Bulk Task example
<?php
( new \Alley\WP_Bulk_Task\Bulk_Task( 'bananaify' ) )->run(
[
'post_status' => 'publish',
],
function ( $post ) {
if ( str_contains( $post->post_content, 'apple' ) ) {
$new_value = str_replace( 'apple', 'banana', $post->post_content );
@dlh01
dlh01 / caper-example.php
Created February 1, 2023 03:33
Caper example
<?php
Alley\WP\Caper::grant_to( 'editor' )->primitives( 'edit_theme_options' );
Alley\WP\Caper::deny_to( 'administrator' )->primitives( 'manage_options' );
Alley\WP\Caper::grant_to( 'author' )->caps_for( 'page' );
Alley\WP\Caper::deny_to( 'editor' )->caps_for( 'category' );
Alley\WP\Caper::grant_to( 'editor' )->caps_for( 'category' )->only( 'delete_terms' );
Alley\WP\Caper::deny( 'author' )->caps_for( 'page' )->except( 'delete_posts' );
@dlh01
dlh01 / match-blocks-example.php
Created February 1, 2023 03:32
Match Blocks example
<?php
// Find all paragraph blocks in a post:
$grafs = \Alley\WP\match_blocks( $post, [ 'name' => 'core/paragraph' ] );
// Include paragraphs in inner blocks:
$grafs = \Alley\WP\match_blocks(
$post,
[
'flatten' => true,
@dlh01
dlh01 / laminas-validator-extensions-example.php
Created February 1, 2023 03:31
Laminas Validator Extensions example
<?php
$valid = new \Alley\Validator\ValidatorByOperator('REGEX', '/^foo/');
$valid->isValid('foobar'); // true
$valid = new \Alley\Validator\ValidatorByOperator('NOT IN', ['bar', 'baz']);
$valid->isValid('bar'); // false
$valid = new \Alley\Validator\ValidatorByOperator('!==', 42);
$valid->isValid(43); // true