-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshortcode-extended.php
More file actions
66 lines (56 loc) · 2.01 KB
/
Copy pathshortcode-extended.php
File metadata and controls
66 lines (56 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/**
* Shortcode Extended
*
* @package ShortcodeExtended
* @author Chris Sparshott
* @copyright 2022 Chris Sparshott
* @license GPL-2.0-or-later
*
* @wordpress-plugin
* Plugin Name: Shortcode Extended
* Plugin URI: https://github.com/seemly/shortcode-extended
* Description: Enable ACF custom field usage in Query Loop block
* Version: 0.0.1
* Requires at least: 5.2
* Author: Chris Sparshott
* Author URI: https://sparshott.co.uk
* Text Domain: shortcode-extended
* License: GPL v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
if(!class_exists('ShortcodeExtended'))
{
class ShortcodeExtended {
protected $originalRenderCallback;
public function __construct($originalRenderCallback = null) {
$this->originalRenderCallback = $originalRenderCallback;
}
public function render($attributes, $content, $block) {
// Failsafe
if (!($block instanceof WP_Block)) {
return $content;
}
// If no ACF or no shortcode found, defer to original callback
if (!function_exists('get_field') || strpos($content, '[acf') === false) {
return is_callable($this->originalRenderCallback)
? call_user_func($this->originalRenderCallback, $attributes, $content, $block)
: $content;
}
// Apply shortcodes safely
return do_shortcode($content);
}
}
add_filter('register_block_type_args', function ($args, $name)
{
// Here we list the native blocks we are likely to include ACF shortcodes in.
// This list probably needs to be expanded, but suits my immediate requirements.
$validBlocks = ['core/shortcode', 'core/paragraph', 'core/list'];
// override the native render_callback function to ensure ACF shortcodes run as expected.
if(in_array($name, $validBlocks))
{
$args['render_callback'] = [new ShortcodeExtended, 'render'];
}
return $args;
}, 10, 2);
}