Skip to content
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

Introduce parser for dynamic token system #42015

Draft
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Stub out main core token replacer.
  • Loading branch information
dmsnell committed Jul 5, 2022
commit 433b2f3d1065e207b3c92a1ae7071886ca6917ec
30 changes: 26 additions & 4 deletions lib/tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,31 @@ function ( $rendered, $token ) use ( $namespace, $name, $replacer ) {
}


function tokens_core_replacer( $rendered, $token ) {
global $post_id;

if ( 'core' !== $token->namespace ) {
return $rendered;
}

switch ( $token->name ) {
case 'featured-image':
return get_the_post_thumbnail_url();

case 'permalink':
$permalink = get_permalink( is_int( $token->value ) ? $token->value : $post_id );

return false !== $permalink ? $permalink : '';

case 'plugins_url':
return plugins_url();

default:
return $token->fallback;
}
}


function tokens_token_replacer( $token ) {
return apply_filters( 'render_token', $token->fallback, $token );
}
Expand All @@ -35,11 +60,8 @@ function tokens_swap_tokens( $text ) {
function tokens_init() {
require_once __DIR__ . '/../packages/tokens/token-parser.php';

add_filter( 'render_token', 'tokens_core_replacer', 2, 10 );
add_filter( 'the_content', 'tokens_swap_tokens', 1, 1000 );

register_token( 'core', 'echo', function ( $rendered, $token ) { return $token->value; } );
register_token( 'core', 'featured-image', function ( $rendered, $token ) { return get_the_post_thumbnail_url(); } );
register_token( 'core', 'plugins-url', function ( $rendered, $token ) { return plugins_url(); } );
}

add_action( 'init', 'tokens_init' );