-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathclass-fw-extension-shortcodes.php
149 lines (124 loc) · 3.54 KB
/
class-fw-extension-shortcodes.php
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php if (!defined('FW')) die('Forbidden');
class FW_Extension_Shortcodes extends FW_Extension
{
/** @var FW_Shortcode[] $shortcodes */
private $shortcodes;
/**
* Gets a certain shortcode by a given tag
*
* @param string $tag The shortcode tag
* @return FW_Shortcode|null
*/
public function get_shortcode($tag)
{
$this->load_shortcodes();
return isset($this->shortcodes[$tag]) ? $this->shortcodes[$tag] : null;
}
/**
* Gets all shortcodes
*
* @return FW_Shortcode[]
*/
public function get_shortcodes()
{
$this->load_shortcodes();
return $this->shortcodes;
}
/**
* @internal
*/
protected function _init()
{
if (!is_admin()) {
// loads the shortcodes
add_action('fw_extensions_init', array($this, '_action_fw_extensions_init'));
// renders the shortcodes so that css will get in <head>
add_action('wp_enqueue_scripts', array($this, '_action_enqueue_shortcodes_static_in_frontend_head'));
} elseif (
defined('DOING_AJAX') &&
DOING_AJAX === true &&
FW_Request::POST('fw_load_shortcodes')
) {
// load the shortcodes if this was requested via ajax
add_action('fw_extensions_init', array($this, '_action_fw_extensions_init'));
}
}
/**
* @internal
*/
public function _action_fw_extensions_init()
{
$this->load_shortcodes();
$this->register_shortcodes();
}
public function load_shortcodes()
{
static $is_loading = false; // prevent recursion
if ($is_loading) {
trigger_error('Recursive shortcodes load', E_USER_WARNING);
return;
}
if ($this->shortcodes) {
return;
}
$is_loading = true;
$disabled_shortcodes = apply_filters('fw_ext_shortcodes_disable_shortcodes', array());
$this->shortcodes = _FW_Shortcodes_Loader::load(array(
'disabled_shortcodes' => $disabled_shortcodes
));
$is_loading = false;
}
private function register_shortcodes()
{
foreach ($this->shortcodes as $tag => $instance) {
add_shortcode($tag, array($instance, 'render'));
}
}
/**
* Make sure to enqueue shortcodes static in <head> (not in <body>)
* @internal
*/
public function _action_enqueue_shortcodes_static_in_frontend_head()
{
/** @var WP_Post $post */
global $post;
if (!$post) {
return;
}
$this->enqueue_shortcodes_static($post->post_content);
}
private function enqueue_shortcodes_static( $content ) {
preg_replace_callback( '/'. get_shortcode_regex() .'/s', array( $this, 'enqueue_shortcode_static'), $content );
}
private function enqueue_shortcode_static( $shortcode ) {
/**
* Remember the enqueued shortcodes and prevent enqueue static multiple times.
* There is no sense to call enqueue_static() multiple times
* because there is no dynamic data passed to it.
*/
static $enqueued_shortcodes = array();
// allow [[foo]] syntax for escaping a tag
if ( $shortcode[1] == '[' && $shortcode[6] == ']' ) {
return;
}
$tag = $shortcode[2];
if ( ! is_null( $this->get_shortcode( $tag ) ) ) {
if (!isset($enqueued_shortcodes[$tag])) {
$this->get_shortcode($tag)->_enqueue_static();
$enqueued_shortcodes[$tag] = true;
}
do_action('fw_ext_shortcodes_enqueue_static:'. $tag, array(
/**
* Transform to array:
* $attr = shortcode_parse_atts( $data['atts_string'] );
*
* By default it's not transformed, but sent as raw string,
* to prevent useless computation for every shortcode,
* because this action may be used very rare and only for a specific shortcode.
*/
'atts_string' => $shortcode[3],
));
$this->enqueue_shortcodes_static($shortcode[5]); // inner shortcodes
}
}
}