-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclass-fw-extension-wp-shortcodes.php
164 lines (131 loc) · 3.98 KB
/
class-fw-extension-wp-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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
if (! defined('FW')) { die('Forbidden'); }
class FW_Extension_WP_Shortcodes extends FW_Extension {
protected $default_shortcodes_list = null;
protected function _init() {
if ( is_admin() ) {
$this->add_admin_hooks();
} else {
$this->add_frontend_hooks();
}
}
public function default_shortcodes_list() {
if (! $this->default_shortcodes_list) {
$shortcodes = fw_ext('shortcodes')->get_shortcodes();
/**
* Filter default shortcodes list that will be displayed in
* default post editor and all of the wp-editors.
*/
$this->default_shortcodes_list = array_diff(apply_filters(
'fw:ext:wp-shortcodes:default-shortcodes',
array_diff(array_keys($shortcodes), array('column', 'section', 'row'))
), array(
'section', 'column', 'row'
));
sort( $this->default_shortcodes_list );
$this->default_shortcodes_list = apply_filters(
'fw:ext:wp-shortcodes:sort', $this->default_shortcodes_list
);
}
return $this->default_shortcodes_list;
}
protected function add_admin_hooks() {
add_filter(
'mce_buttons',
array($this, 'register_unyson_button')
);
add_filter(
'mce_external_plugins',
array($this, 'add_unyson_plugin')
);
add_filter(
'mce_css',
array($this, 'editor_styles')
);
add_action(
'fw_admin_enqueue_scripts:post',
array($this, 'enqueue_option_types_scripts')
);
}
/**
* Enqueue option types statics.
*/
public function enqueue_option_types_scripts() {
wp_enqueue_style('fw-unycon');
if (function_exists('fw_ext_shortcodes_enqueue_shortcodes_admin_scripts')) {
fw_ext_shortcodes_enqueue_shortcodes_admin_scripts();
}
}
protected function add_frontend_hooks() {
add_action(
'fw_ext_shortcodes:after_shortcode_enqueue_static',
array($this, 'enqueue_wp_shortcode_static')
);
}
public function add_unyson_plugin($plugins) {
$plugins['unyson_shortcodes'] = $this->locate_js_URI('plugin');
return $plugins;
}
public function register_unyson_button($buttons) {
array_push($buttons, 'unyson_shortcodes');
return $buttons;
}
public function editor_styles($mce_css) {
$mce_css .= ', ' . implode(', ', array(
$this->locate_css_URI('content'),
// in case some shortcodes use unycon icons
fw_get_framework_directory_uri('/static/libs/unycon/unycon.css'),
// in case some shortcodes use fontAwesome icons
fw_get_framework_directory_uri('/static/libs/font-awesome/css/font-awesome.min.css')
));
return $mce_css;
}
public function enqueue_wp_shortcode_static($shortcode) {
/**
* Try to enqueue static for shortcodes that may be in
* some attribute for a Page Builder Shortcode.
*/
if ($this->have_to_decode_with_aggresive($shortcode[3])) {
$atts = shortcode_parse_atts($shortcode[3]);
// 1. first decode with JSON coder
$atts = fw_ext('shortcodes')->get_attr_coder('json')->decode(
$atts, '', null
);
$this->enqueue_wp_shortcode_static_real($atts);
}
}
public function enqueue_wp_shortcode_static_real($atts) {
// 2. decode with aggressive, but only if we need it
/**
* Skip whole recursive branch if it doesn't contain a shortocde.
* That's a TREMENDOUS performance optimisation.
*/
$have_to_recur = $this->have_to_decode_with_aggresive(
json_encode($atts)
);
if (! $have_to_recur) { return; }
foreach ($atts as $key => $value) {
if (is_string($value)) {
if (! $this->have_to_decode_with_aggresive($value)) {
continue;
}
// 3. Enqueue
fw_ext_shortcodes_enqueue_shortcodes_static(
$value
);
} else if (is_array($value)) {
$this->enqueue_wp_shortcode_static_real($value);
}
}
}
private function have_to_decode_with_aggresive($str) {
if (! is_string($str)) { return false; }
$has_coder_inside = strpos($str, '_fw_coder=') !== false;
$has_aggessive_coder = strpos($str, 'aggressive') !== false;
$has_wp_editor_shortcode = strpos(
$str,
'__fw_editor_shortcodes_id'
) !== false;
return $has_coder_inside && $has_aggessive_coder && $has_wp_editor_shortcode;
}
}