1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* CMB2 ajax methods |
5
|
|
|
* (i.e. a lot of work to get oEmbeds to work with non-post objects) |
6
|
|
|
* |
7
|
|
|
* @since 0.9.5 |
8
|
|
|
* |
9
|
|
|
* @category WordPress_Plugin |
10
|
|
|
* @package CMB2 |
11
|
|
|
* @author WebDevStudios |
12
|
|
|
* @license GPL-2.0+ |
13
|
|
|
*/ |
14
|
|
|
class CMB2_Ajax { |
|
|
|
|
15
|
|
|
|
16
|
|
|
// Whether to hijack the oembed cache system |
17
|
|
|
protected $hijack = false; |
18
|
|
|
protected $object_id = 0; |
19
|
|
|
protected $embed_args = array(); |
20
|
|
|
protected $object_type = 'post'; |
21
|
|
|
protected $ajax_update = false; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Instance of this class |
25
|
|
|
* |
26
|
|
|
* @since 2.2.2 |
27
|
|
|
* @var object |
28
|
|
|
*/ |
29
|
|
|
protected static $instance; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get the singleton instance of this class |
33
|
|
|
* |
34
|
|
|
* @since 2.2.2 |
35
|
|
|
* @return CMB2_Ajax |
36
|
|
|
*/ |
37
|
5 |
|
public static function get_instance() { |
38
|
5 |
|
if ( ! ( self::$instance instanceof self ) ) { |
39
|
1 |
|
self::$instance = new self(); |
40
|
1 |
|
} |
41
|
|
|
|
42
|
5 |
|
return self::$instance; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Constructor |
47
|
|
|
* |
48
|
|
|
* @since 2.2.0 |
49
|
|
|
*/ |
50
|
1 |
|
protected function __construct() { |
51
|
1 |
|
add_action( 'wp_ajax_cmb2_oembed_handler', array( $this, 'oembed_handler' ) ); |
52
|
1 |
|
add_action( 'wp_ajax_nopriv_cmb2_oembed_handler', array( $this, 'oembed_handler' ) ); |
53
|
|
|
// Need to occasionally clean stale oembed cache data from the option value. |
54
|
1 |
|
add_action( 'cmb2_save_options-page_fields', array( __CLASS__, 'clean_stale_options_page_oembeds' ) ); |
55
|
1 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Handles our oEmbed ajax request |
59
|
|
|
* |
60
|
|
|
* @since 0.9.5 |
61
|
|
|
* @return object oEmbed embed code | fallback | error message |
62
|
|
|
*/ |
63
|
1 |
|
public function oembed_handler() { |
|
|
|
|
64
|
|
|
|
65
|
|
|
// Verify our nonce |
66
|
|
|
if ( ! ( isset( $_REQUEST['cmb2_ajax_nonce'], $_REQUEST['oembed_url'] ) && wp_verify_nonce( $_REQUEST['cmb2_ajax_nonce'], 'ajax_nonce' ) ) ) { |
67
|
|
|
die(); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// Sanitize our search string |
71
|
|
|
$oembed_string = sanitize_text_field( $_REQUEST['oembed_url'] ); |
72
|
|
|
|
73
|
|
|
// Send back error if empty |
74
|
|
|
if ( empty( $oembed_string ) ) { |
75
|
|
|
wp_send_json_error( '<p class="ui-state-error-text">' . esc_html__( 'Please Try Again', 'cmb2' ) . '</p>' ); |
76
|
1 |
|
} |
77
|
|
|
|
78
|
|
|
// Set width of embed |
79
|
|
|
$embed_width = isset( $_REQUEST['oembed_width'] ) && intval( $_REQUEST['oembed_width'] ) < 640 ? intval( $_REQUEST['oembed_width'] ) : '640'; |
80
|
|
|
|
81
|
|
|
// Set url |
82
|
|
|
$oembed_url = esc_url( $oembed_string ); |
83
|
|
|
|
84
|
|
|
// Set args |
85
|
|
|
$embed_args = array( |
86
|
|
|
'width' => $embed_width, |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$this->ajax_update = true; |
90
|
|
|
|
91
|
|
|
// Get embed code (or fallback link) |
92
|
|
|
$html = $this->get_oembed( array( |
93
|
|
|
'url' => $oembed_url, |
94
|
|
|
'object_id' => $_REQUEST['object_id'], |
95
|
|
|
'object_type' => isset( $_REQUEST['object_type'] ) ? $_REQUEST['object_type'] : 'post', |
96
|
|
|
'oembed_args' => $embed_args, |
97
|
|
|
'field_id' => $_REQUEST['field_id'], |
98
|
|
|
) ); |
99
|
|
|
|
100
|
|
|
wp_send_json_success( $html ); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Retrieves oEmbed from url/object ID |
105
|
|
|
* |
106
|
|
|
* @since 0.9.5 |
107
|
|
|
* @param array $args Arguments for method |
108
|
|
|
* @return string html markup with embed or fallback |
109
|
|
|
*/ |
110
|
3 |
|
public function get_oembed_no_edit( $args ) { |
111
|
3 |
|
global $wp_embed; |
|
|
|
|
112
|
|
|
|
113
|
3 |
|
$oembed_url = esc_url( $args['url'] ); |
114
|
|
|
|
115
|
|
|
// Sanitize object_id |
116
|
3 |
|
$this->object_id = is_numeric( $args['object_id'] ) ? absint( $args['object_id'] ) : sanitize_text_field( $args['object_id'] ); |
117
|
|
|
|
118
|
3 |
|
$args = wp_parse_args( $args, array( |
119
|
3 |
|
'object_type' => 'post', |
120
|
3 |
|
'oembed_args' => $this->embed_args, |
121
|
3 |
|
'field_id' => false, |
122
|
3 |
|
'wp_error' => false, |
123
|
3 |
|
) ); |
124
|
|
|
|
125
|
3 |
|
$this->embed_args =& $args; |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Set the post_ID so oEmbed won't fail |
129
|
|
|
* wp-includes/class-wp-embed.php, WP_Embed::shortcode() |
130
|
|
|
*/ |
131
|
3 |
|
$wp_embed->post_ID = $this->object_id; |
132
|
|
|
|
133
|
|
|
// Special scenario if NOT a post object |
134
|
3 |
|
if ( isset( $args['object_type'] ) && 'post' != $args['object_type'] ) { |
135
|
|
|
|
136
|
1 |
|
if ( 'options-page' == $args['object_type'] ) { |
137
|
|
|
|
138
|
|
|
// Bogus id to pass some numeric checks. Issue with a VERY large WP install? |
139
|
1 |
|
$wp_embed->post_ID = 1987645321; |
140
|
1 |
|
} |
141
|
|
|
|
142
|
|
|
// Ok, we need to hijack the oembed cache system |
143
|
1 |
|
$this->hijack = true; |
144
|
1 |
|
$this->object_type = $args['object_type']; |
145
|
|
|
|
146
|
|
|
// Gets ombed cache from our object's meta (vs postmeta) |
147
|
1 |
|
add_filter( 'get_post_metadata', array( $this, 'hijack_oembed_cache_get' ), 10, 3 ); |
148
|
|
|
|
149
|
|
|
// Sets ombed cache in our object's meta (vs postmeta) |
150
|
1 |
|
add_filter( 'update_post_metadata', array( $this, 'hijack_oembed_cache_set' ), 10, 4 ); |
151
|
|
|
|
152
|
1 |
|
} |
153
|
|
|
|
154
|
3 |
|
$embed_args = ''; |
155
|
|
|
|
156
|
3 |
|
foreach ( $args['oembed_args'] as $key => $val ) { |
157
|
3 |
|
$embed_args .= " $key=\"$val\""; |
158
|
3 |
|
} |
159
|
|
|
|
160
|
|
|
// Ping WordPress for an embed |
161
|
3 |
|
$embed = $wp_embed->run_shortcode( '[embed' . $embed_args . ']' . $oembed_url . '[/embed]' ); |
162
|
|
|
|
163
|
|
|
// Fallback that WordPress creates when no oEmbed was found |
164
|
3 |
|
$fallback = $wp_embed->maybe_make_link( $oembed_url ); |
165
|
|
|
|
166
|
3 |
|
return compact( 'embed', 'fallback', 'args' ); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Retrieves oEmbed from url/object ID |
171
|
|
|
* |
172
|
|
|
* @since 0.9.5 |
173
|
|
|
* @param array $args Arguments for method |
174
|
|
|
* @return string html markup with embed or fallback |
175
|
|
|
*/ |
176
|
2 |
|
public function get_oembed( $args ) { |
177
|
2 |
|
$oembed = $this->get_oembed_no_edit( $args ); |
178
|
|
|
|
179
|
|
|
// Send back our embed |
180
|
2 |
|
if ( $oembed['embed'] && $oembed['embed'] != $oembed['fallback'] ) { |
181
|
2 |
|
return '<div class="cmb2-oembed embed-status">' . $oembed['embed'] . '<p class="cmb2-remove-wrapper"><a href="#" class="cmb2-remove-file-button" rel="' . $oembed['args']['field_id'] . '">' . esc_html__( 'Remove Embed', 'cmb2' ) . '</a></p></div>'; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
// Otherwise, send back error info that no oEmbeds were found |
185
|
1 |
|
return sprintf( |
186
|
1 |
|
'<p class="ui-state-error-text">%s</p>', |
187
|
1 |
|
sprintf( |
188
|
|
|
/* translators: 1: results for. 2: link to codex.wordpress.org/Embeds */ |
189
|
1 |
|
esc_html__( 'No oEmbed Results Found for %1$s. View more info at %2$s.', 'cmb2' ), |
190
|
1 |
|
$oembed['fallback'], |
191
|
|
|
'<a href="https://codex.wordpress.org/Embeds" target="_blank">codex.wordpress.org/Embeds</a>' |
192
|
1 |
|
) |
193
|
1 |
|
); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Hijacks retrieving of cached oEmbed. |
198
|
|
|
* Returns cached data from relevant object metadata (vs postmeta) |
199
|
|
|
* |
200
|
|
|
* @since 0.9.5 |
201
|
|
|
* @param boolean $check Whether to retrieve postmeta or override |
202
|
|
|
* @param int $object_id Object ID |
203
|
|
|
* @param string $meta_key Object metakey |
204
|
|
|
* @return mixed Object's oEmbed cached data |
205
|
|
|
*/ |
206
|
111 |
|
public function hijack_oembed_cache_get( $check, $object_id, $meta_key ) { |
207
|
111 |
|
if ( ! $this->hijack || ( $this->object_id != $object_id && 1987645321 !== $object_id ) ) { |
208
|
110 |
|
return $check; |
209
|
|
|
} |
210
|
|
|
|
211
|
3 |
|
if ( $this->ajax_update ) { |
212
|
|
|
return false; |
213
|
|
|
} |
214
|
|
|
|
215
|
3 |
|
return $this->cache_action( $meta_key ); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Hijacks saving of cached oEmbed. |
220
|
|
|
* Saves cached data to relevant object metadata (vs postmeta) |
221
|
|
|
* |
222
|
|
|
* @since 0.9.5 |
223
|
|
|
* @param boolean $check Whether to continue setting postmeta |
224
|
|
|
* @param int $object_id Object ID to get postmeta from |
225
|
|
|
* @param string $meta_key Postmeta's key |
226
|
|
|
* @param mixed $meta_value Value of the postmeta to be saved |
227
|
|
|
* @return boolean Whether to continue setting |
228
|
|
|
*/ |
229
|
93 |
|
public function hijack_oembed_cache_set( $check, $object_id, $meta_key, $meta_value ) { |
230
|
|
|
|
231
|
|
|
if ( |
232
|
93 |
|
! $this->hijack |
233
|
93 |
|
|| ( $this->object_id != $object_id && 1987645321 !== $object_id ) |
234
|
|
|
// only want to hijack oembed meta values |
235
|
93 |
|
|| 0 !== strpos( $meta_key, '_oembed_' ) |
236
|
93 |
|
) { |
237
|
92 |
|
return $check; |
238
|
|
|
} |
239
|
|
|
|
240
|
3 |
|
$this->cache_action( $meta_key, $meta_value ); |
241
|
|
|
|
242
|
|
|
// Anything other than `null` to cancel saving to postmeta |
243
|
3 |
|
return true; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Gets/updates the cached oEmbed value from/to relevant object metadata (vs postmeta) |
248
|
|
|
* |
249
|
|
|
* @since 1.3.0 |
250
|
|
|
* @param string $meta_key Postmeta's key |
251
|
|
|
*/ |
252
|
3 |
|
protected function cache_action( $meta_key ) { |
253
|
3 |
|
$func_args = func_get_args(); |
254
|
3 |
|
$action = isset( $func_args[1] ) ? 'update' : 'get'; |
255
|
|
|
|
256
|
3 |
|
if ( 'options-page' === $this->object_type ) { |
257
|
|
|
|
258
|
3 |
|
$args = array( $meta_key ); |
259
|
|
|
|
260
|
3 |
|
if ( 'update' === $action ) { |
261
|
3 |
|
$args[] = $func_args[1]; |
262
|
3 |
|
$args[] = true; |
263
|
3 |
|
} |
264
|
|
|
|
265
|
|
|
// Cache the result to our options |
266
|
3 |
|
$status = call_user_func_array( array( cmb2_options( $this->object_id ), $action ), $args ); |
267
|
3 |
|
} else { |
268
|
|
|
|
269
|
|
|
$args = array( $this->object_type, $this->object_id, $meta_key ); |
270
|
|
|
$args[] = 'update' === $action ? $func_args : true; |
271
|
|
|
|
272
|
|
|
// Cache the result to our metadata |
273
|
|
|
$status = call_user_func_array( $action . '_metadata', $args ); |
274
|
|
|
} |
275
|
|
|
|
276
|
3 |
|
return $status; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Hooks in when options-page data is saved to clean stale |
281
|
|
|
* oembed cache data from the option value. |
282
|
|
|
* |
283
|
|
|
* @since 2.2.0 |
284
|
|
|
* @param string $option_key The options-page option key |
285
|
|
|
* @return void |
286
|
|
|
*/ |
287
|
1 |
|
public static function clean_stale_options_page_oembeds( $option_key ) { |
288
|
1 |
|
$options = cmb2_options( $option_key )->get_options(); |
289
|
1 |
|
$modified = false; |
290
|
1 |
|
if ( is_array( $options ) ) { |
291
|
|
|
|
292
|
1 |
|
$ttl = apply_filters( 'oembed_ttl', DAY_IN_SECONDS, '', array(), 0 ); |
293
|
1 |
|
$now = time(); |
294
|
|
|
|
295
|
1 |
|
foreach ( $options as $key => $value ) { |
296
|
|
|
// Check for cached oembed data |
297
|
1 |
|
if ( 0 === strpos( $key, '_oembed_time_' ) ) { |
298
|
|
|
$cached_recently = ( $now - $value ) < $ttl; |
299
|
|
|
|
300
|
|
|
if ( ! $cached_recently ) { |
301
|
|
|
$modified = true; |
302
|
|
|
// Remove the the cached ttl expiration, and the cached oembed value. |
303
|
|
|
unset( $options[ $key ] ); |
304
|
|
|
unset( $options[ str_replace( '_oembed_time_', '_oembed_', $key ) ] ); |
305
|
|
|
} |
306
|
|
|
} // End if(). |
|
|
|
|
307
|
|
|
// Remove the cached unknown values. |
308
|
1 |
|
elseif ( '{{unknown}}' === $value ) { |
309
|
1 |
|
$modified = true; |
310
|
1 |
|
unset( $options[ $key ] ); |
311
|
1 |
|
} |
312
|
1 |
|
} |
313
|
1 |
|
} |
314
|
|
|
|
315
|
|
|
// Update the option and remove stale cache data |
316
|
1 |
|
if ( $modified ) { |
317
|
1 |
|
$updated = cmb2_options( $option_key )->set( $options ); |
|
|
|
|
318
|
1 |
|
} |
319
|
1 |
|
} |
320
|
|
|
|
321
|
|
|
} |
322
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.