Skip to content

Instantly share code, notes, and snippets.

@dlh01
dlh01 / block-content.html
Last active May 25, 2020 00:33
The blocks in the Gutenberg plugin's demo post (and then some) as raw HTML, a PHP array of parsed blocks, and a JSON object of the PHP array.
<!-- wp:cover {"url":"https://cldup.com/Fz-ASbo2s3.jpg","className":"alignwide"} -->
<div class="wp-block-cover has-background-dim alignwide" style="background-image:url(https://cldup.com/Fz-ASbo2s3.jpg)"><div class="wp-block-cover__inner-container"><!-- wp:paragraph {"align":"center","placeholder":"Write title…","fontSize":"large"} -->
<p class="has-text-align-center has-large-font-size">Of Mountains &amp; Printing Presses</p>
<!-- /wp:paragraph --></div></div>
<!-- /wp:cover -->
<!-- wp:paragraph -->
<p>The goal of this new editor is to make adding rich content to WordPress simple and enjoyable. This whole post is composed of <em>pieces of content</em>—somewhat similar to LEGO bricks—that you can move around and interact with. Move your cursor around and you&#8217;ll notice the different blocks light up with outlines and arrows. Press the arrows to reposition blocks quickly, without fearing about losing things in the process of copying and pasting.</p>
<!-- /wp:paragraph -->
const useEffectOnceOnDemand = (effect) => {
const [count, setCount] = useState(0);
useEffect(() => {
if (1 === count) {
effect();
}
});
return () => setCount((prev) => Math.min(2, prev + 1));
<?php
function flatten_blocks( array $blocks ) {
reset( $blocks );
$out = [];
while ( $block = array_shift( $blocks ) ) {
$out[] = $block;
@dlh01
dlh01 / traverse-reshape-example.php
Created February 1, 2023 03:00
traverse/reshape example
<?php
$arr = [
'apples' => [
'red' => [
'gala',
'mcintosh',
],
'green' => [
'granny_smith',
@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
@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 / 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 / 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 / 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 / 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' )