PluginProbe ʕ •ᴥ•ʔ
Advanced Custom Fields: Extended / 0.9.2.7
Advanced Custom Fields: Extended v0.9.2.7
0.9.2.7 0.9.2.6 0.9.2.5 0.8.6 0.8.6.1 0.8.6.3 0.8.6.5 0.8.6.6 0.8.6.7 0.8.6.8 0.8.6.9 0.8.7 0.8.7.1 0.8.7.2 0.8.7.3 0.8.7.4 0.8.7.5 0.8.7.6 0.8.8 0.8.8.1 0.8.8.10 0.8.8.11 0.8.8.2 0.8.8.3 0.8.8.4 0.8.8.5 0.8.8.6 0.8.8.7 0.8.8.8 0.8.8.9 0.8.9 0.8.9.1 0.8.9.2 0.8.9.3 0.8.9.4 0.8.9.5 0.9 0.9.0.1 0.9.0.2 0.9.0.3 0.9.0.4 0.9.0.5 0.9.0.6 0.9.0.7 0.9.0.8 0.9.0.9 0.9.1 0.9.1.1 0.9.2 0.9.2.1 0.9.2.2 0.9.2.3 0.9.2.4 trunk 0.5 0.5.1 0.5.2 0.5.2.1 0.5.2.3 0.5.5 0.5.5.1 0.5.8 0.5.8.1 0.6 0.6.0.1 0.6.0.2 0.6.1 0.6.3 0.6.5 0.6.7 0.6.7.2 0.7 0.7.0.3 0.7.5 0.7.5.5 0.7.8 0.7.9 0.7.9.3 0.7.9.4 0.7.9.9.8 0.7.9.9.9 0.8 0.8.1 0.8.2 0.8.3 0.8.3.1 0.8.4 0.8.4.1 0.8.4.5 0.8.4.6 0.8.5 0.8.5.5
acf-extended / includes / fields / field-flexible-content-preview.php
acf-extended / includes / fields Last commit date
field-advanced-link.php 3 months ago field-button-group.php 1 week ago field-button.php 7 months ago field-checkbox.php 3 months ago field-clone.php 2 years ago field-code-editor.php 3 months ago field-column.php 3 years ago field-dynamic-render.php 3 years ago field-file.php 3 months ago field-flexible-content-actions-title.php 3 months ago field-flexible-content-actions-toggle.php 3 months ago field-flexible-content-actions.php 3 months ago field-flexible-content-async.php 4 months ago field-flexible-content-compatibility.php 3 months ago field-flexible-content-controls.php 9 months ago field-flexible-content-hide.php 3 months ago field-flexible-content-hooks.php 9 months ago field-flexible-content-modal-edit.php 9 months ago field-flexible-content-modal-select.php 3 months ago field-flexible-content-modal-settings.php 3 months ago field-flexible-content-popup.php 3 months ago field-flexible-content-preview.php 3 months ago field-flexible-content-state.php 9 months ago field-flexible-content-thumbnail.php 9 months ago field-flexible-content-wysiwyg.php 9 months ago field-flexible-content.php 3 months ago field-forms.php 3 months ago field-gallery.php 3 months ago field-google-map.php 3 months ago field-group.php 3 months ago field-hidden.php 3 years ago field-image.php 3 months ago field-post-object.php 3 months ago field-post-statuses.php 3 months ago field-post-types.php 3 months ago field-radio.php 3 months ago field-recaptcha.php 3 months ago field-relationship.php 3 months ago field-repeater.php 3 months ago field-select.php 3 months ago field-slug.php 1 year ago field-taxonomies.php 3 months ago field-taxonomy-terms.php 3 months ago field-taxonomy.php 3 months ago field-textarea.php 3 months ago field-user-roles.php 3 months ago field-user.php 3 months ago field-wysiwyg.php 3 months ago
field-flexible-content-preview.php
610 lines
1 <?php
2
3 if(!defined('ABSPATH')){
4 exit;
5 }
6
7 if(!class_exists('acfe_field_flexible_content_preview')):
8
9 class acfe_field_flexible_content_preview{
10
11 var $no_post = false;
12
13 /**
14 * construct
15 */
16 function __construct(){
17
18 // Hooks
19 add_filter('acfe/flexible/defaults_field', array($this, 'defaults_field'), 2);
20 add_filter('acfe/flexible/defaults_layout', array($this, 'defaults_layout'), 2);
21
22 add_action('acfe/flexible/render_field_settings', array($this, 'render_field_settings'), 2);
23 add_action('acfe/flexible/render_layout_settings', array($this, 'render_layout_settings'), 15, 3);
24
25 add_action('acf/render_field/type=flexible_content', array($this, 'render_field'), 8);
26 add_filter('acfe/flexible/wrapper_attributes', array($this, 'wrapper_attributes'), 10, 2);
27 add_filter('acfe/flexible/prepare_layout', array($this, 'prepare_layout'), 25, 5);
28
29 // Ajax
30 add_action('wp_ajax_acfe/flexible/layout_preview', array($this, 'layout_preview'));
31
32 }
33
34
35 /**
36 * defaults_field
37 *
38 * @param $field
39 *
40 * @return mixed
41 */
42 function defaults_field($field){
43
44 $field['acfe_flexible_layouts_templates'] = false;
45 $field['acfe_flexible_layouts_previews'] = false;
46 $field['acfe_flexible_layouts_placeholder'] = false;
47
48 return $field;
49
50 }
51
52
53 /**
54 * defaults_layout
55 *
56 * @param $layout
57 *
58 * @return mixed
59 */
60 function defaults_layout($layout){
61
62 $layout['acfe_flexible_render_template'] = false;
63 $layout['acfe_flexible_render_style'] = false;
64 $layout['acfe_flexible_render_script'] = false;
65
66 return $layout;
67
68 }
69
70
71 /**
72 * render_field_settings
73 *
74 * @param $field
75 */
76 function render_field_settings($field){
77
78 // Render
79 acf_render_field_setting($field, array(
80 'label' => __('Dynamic Render', 'acfe'),
81 'name' => 'acfe_flexible_layouts_templates',
82 'key' => 'acfe_flexible_layouts_templates',
83 'instructions' => __('Render the layout using custom template, style & javascript files', 'acfe') . '. ' . '<a href="https://www.acf-extended.com/features/fields/flexible-content/dynamic-render" target="_blank">' . __('See documentation', 'acfe') . '</a>',
84 'type' => 'true_false',
85 'message' => '',
86 'default_value' => false,
87 'ui' => true,
88 'ui_on_text' => '',
89 'ui_off_text' => '',
90 'conditional_logic' => array(
91 array(
92 array(
93 'field' => 'acfe_flexible_advanced',
94 'operator' => '==',
95 'value' => '1',
96 ),
97 )
98 )
99 ));
100
101 // Preview
102 acf_render_field_setting($field, array(
103 'label' => __('Dynamic Preview', 'acfe'),
104 'name' => 'acfe_flexible_layouts_previews',
105 'key' => 'acfe_flexible_layouts_previews',
106 'instructions' => __('Use layouts render settings to display a dynamic preview in the administration', 'acfe') . '. ' . '<a href="https://www.acf-extended.com/features/fields/flexible-content/dynamic-render#dynamic-preview" target="_blank">' . __('See documentation', 'acfe') . '</a>',
107 'type' => 'true_false',
108 'message' => '',
109 'default_value' => false,
110 'ui' => true,
111 'ui_on_text' => '',
112 'ui_off_text' => '',
113 'conditional_logic' => array(
114 array(
115 array(
116 'field' => 'acfe_flexible_advanced',
117 'operator' => '==',
118 'value' => '1',
119 ),
120 array(
121 'field' => 'acfe_flexible_layouts_templates',
122 'operator' => '==',
123 'value' => '1',
124 ),
125 )
126 )
127 ));
128
129 // Placholder
130 acf_render_field_setting($field, array(
131 'label' => __('Layouts Placeholder', 'acfe'),
132 'name' => 'acfe_flexible_layouts_placeholder',
133 'key' => 'acfe_flexible_layouts_placeholder',
134 'instructions' => __('Display a placeholder with an icon', 'acfe') . '. ' . '<a href="https://www.acf-extended.com/features/fields/flexible-content/advanced-settings#layouts-placeholder" target="_blank">' . __('See documentation', 'acfe') . '</a>',
135 'type' => 'true_false',
136 'message' => '',
137 'default_value' => false,
138 'ui' => true,
139 'ui_on_text' => '',
140 'ui_off_text' => '',
141 'conditional_logic' => array(
142 array(
143 array(
144 'field' => 'acfe_flexible_advanced',
145 'operator' => '==',
146 'value' => '1',
147 ),
148 array(
149 'field' => 'acfe_flexible_layouts_previews',
150 'operator' => '!=',
151 'value' => '1',
152 ),
153 )
154 )
155 ));
156
157 }
158
159
160 /**
161 * render_layout_settings
162 *
163 * @param $flexible
164 * @param $layout
165 * @param $prefix
166 */
167 function render_layout_settings($flexible, $layout, $prefix){
168
169 if(!acfe_get($flexible, 'acfe_flexible_layouts_templates')){
170 return;
171 }
172
173 // vars
174 $name = $flexible['name'];
175 $key = $flexible['key'];
176 $l_name = $layout['name'];
177 $prepend = acfe_get_setting('theme_folder') ? trailingslashit(acfe_get_setting('theme_folder')) : '';
178
179 // Title
180 echo '</li>';
181 acf_render_field_wrap(array(
182 'label' => __('Render', 'acfe'),
183 'type' => 'hidden',
184 'name' => 'acfe_flexible_render_label',
185 'wrapper' => array(
186 'class' => 'acfe-flexible-field-setting acfe-flexible-field-setting-row',
187 )
188 ), 'ul');
189 echo '<li>';
190
191 // Template
192 $prepend = apply_filters("acfe/flexible/prepend/template", $prepend, $flexible, $layout);
193 $prepend = apply_filters("acfe/flexible/prepend/template/name={$name}", $prepend, $flexible, $layout);
194 $prepend = apply_filters("acfe/flexible/prepend/template/key={$key}", $prepend, $flexible, $layout);
195 $prepend = apply_filters("acfe/flexible/prepend/template/layout={$l_name}", $prepend, $flexible, $layout);
196 $prepend = apply_filters("acfe/flexible/prepend/template/name={$name}&layout={$l_name}", $prepend, $flexible, $layout);
197 $prepend = apply_filters("acfe/flexible/prepend/template/key={$key}&layout={$l_name}", $prepend, $flexible, $layout);
198
199 acf_render_field_wrap(array(
200 'prepend' => $prepend,
201 'name' => 'acfe_flexible_render_template',
202 'type' => 'text',
203 'class' => 'acf-fc-meta-name',
204 'prefix' => $prefix,
205 'value' => $layout['acfe_flexible_render_template'],
206 'placeholder' => 'template.php',
207 ), 'ul');
208
209
210 // Style
211 $prepend = apply_filters("acfe/flexible/prepend/style", $prepend, $flexible, $layout);
212 $prepend = apply_filters("acfe/flexible/prepend/style/name={$name}", $prepend, $flexible, $layout);
213 $prepend = apply_filters("acfe/flexible/prepend/style/key={$key}", $prepend, $flexible, $layout);
214 $prepend = apply_filters("acfe/flexible/prepend/style/layout={$l_name}", $prepend, $flexible, $layout);
215 $prepend = apply_filters("acfe/flexible/prepend/style/name={$name}&layout={$l_name}", $prepend, $flexible, $layout);
216 $prepend = apply_filters("acfe/flexible/prepend/style/key={$key}&layout={$l_name}", $prepend, $flexible, $layout);
217
218 acf_render_field_wrap(array(
219 'prepend' => $prepend,
220 'name' => 'acfe_flexible_render_style',
221 'type' => 'text',
222 'class' => 'acf-fc-meta-name',
223 'prefix' => $prefix,
224 'value' => $layout['acfe_flexible_render_style'],
225 'placeholder' => 'style.css',
226 ), 'ul');
227
228
229 // Script
230 $prepend = apply_filters("acfe/flexible/prepend/script", $prepend, $flexible, $layout);
231 $prepend = apply_filters("acfe/flexible/prepend/script/name={$name}", $prepend, $flexible, $layout);
232 $prepend = apply_filters("acfe/flexible/prepend/script/key={$key}", $prepend, $flexible, $layout);
233 $prepend = apply_filters("acfe/flexible/prepend/script/layout={$l_name}", $prepend, $flexible, $layout);
234 $prepend = apply_filters("acfe/flexible/prepend/script/name={$name}&layout={$l_name}", $prepend, $flexible, $layout);
235 $prepend = apply_filters("acfe/flexible/prepend/script/key={$key}&layout={$l_name}", $prepend, $flexible, $layout);
236
237 acf_render_field_wrap(array(
238 'prepend' => $prepend,
239 'name' => 'acfe_flexible_render_script',
240 'type' => 'text',
241 'class' => 'acf-fc-meta-name',
242 'prefix' => $prefix,
243 'value' => $layout['acfe_flexible_render_script'],
244 'placeholder' => 'script.js',
245 ), 'ul');
246
247 }
248
249
250 /**
251 * render_field
252 *
253 * @param $field
254 */
255 function render_field($field){
256
257 // check setting
258 if(!acfe_get($field, 'acfe_flexible_layouts_templates') || !acfe_get($field, 'acfe_flexible_layouts_previews')){
259 return;
260 }
261
262 // vars
263 global $is_preview;
264 $is_preview = true;
265
266 // render: global enqueue
267 acfe_flexible_render_enqueue($field);
268
269 // loop layouts
270 foreach($field['layouts'] as $layout){
271
272 // render: layout enqueue
273 acfe_flexible_render_layout_enqueue($layout, $field);
274
275 }
276
277 }
278
279
280 /**
281 * wrapper_attributes
282 *
283 * @param $wrapper
284 * @param $field
285 *
286 * @return mixed
287 */
288 function wrapper_attributes($wrapper, $field){
289
290 if(acfe_get($field, 'acfe_flexible_layouts_placeholder')){
291 $wrapper['data-acfe-flexible-placeholder'] = 1;
292 }
293
294 if(acfe_get($field, 'acfe_flexible_layouts_templates') && acfe_get($field, 'acfe_flexible_layouts_previews')){
295 $wrapper['data-acfe-flexible-placeholder'] = 1;
296 $wrapper['data-acfe-flexible-preview'] = 1;
297 }
298
299 return $wrapper;
300
301 }
302
303
304 /**
305 * prepare_layout
306 *
307 * @param $layout
308 * @param $field
309 * @param $i
310 * @param $value
311 * @param $prefix
312 *
313 * @return mixed
314 */
315 function prepare_layout($layout, $field, $i, $value, $prefix){
316
317 if(!acfe_get($field, 'acfe_flexible_layouts_placeholder') && !acfe_get($field, 'acfe_flexible_layouts_previews')){
318 return $layout;
319 }
320
321 // Vars
322 $name = $field['_name'];
323 $key = $field['key'];
324 $l_name = $layout['name'];
325
326 $placeholder = array(
327 'class' => 'acfe-fc-placeholder',
328 'title' => __('Edit layout', 'acfe'),
329 );
330
331 $placeholder = apply_filters("acfe/flexible/layouts/placeholder", $placeholder, $layout, $field, $i, $value, $prefix);
332 $placeholder = apply_filters("acfe/flexible/layouts/placeholder/name={$name}", $placeholder, $layout, $field, $i, $value, $prefix);
333 $placeholder = apply_filters("acfe/flexible/layouts/placeholder/key={$key}", $placeholder, $layout, $field, $i, $value, $prefix);
334 $placeholder = apply_filters("acfe/flexible/layouts/placeholder/layout={$l_name}", $placeholder, $layout, $field, $i, $value, $prefix);
335 $placeholder = apply_filters("acfe/flexible/layouts/placeholder/name={$name}&layout={$l_name}", $placeholder, $layout, $field, $i, $value, $prefix);
336 $placeholder = apply_filters("acfe/flexible/layouts/placeholder/key={$key}&layout={$l_name}", $placeholder, $layout, $field, $i, $value, $prefix);
337
338 $html = false;
339
340 if(!empty($value) && acfe_get($field, 'acfe_flexible_layouts_previews')){
341
342 ob_start();
343
344 $this->layout_preview(array(
345 'post_id' => acf_get_valid_post_id(),
346 'i' => $i,
347 'field_key' => $key,
348 'layout' => $l_name,
349 'value' => $value,
350 ));
351
352 $html = ob_get_clean();
353
354 if(strlen($html) > 0){
355 $placeholder['class'] .= ' acfe-fc-preview';
356 }
357
358 }
359
360 ?>
361 <div <?php echo acf_esc_atts($placeholder); ?>>
362 <a href="#" class="button">
363 <span class="dashicons dashicons-edit"></span>
364 </a>
365 <div class="acfe-fc-overlay"></div>
366 <div class="acfe-flexible-placeholder -preview"><?php echo $html; ?></div>
367 </div>
368 <?php
369
370 return $layout;
371
372 }
373
374
375 /**
376 * layout_preview
377 *
378 * @param $options
379 *
380 * @return bool|null
381 */
382 function layout_preview($options = array()){
383
384 if(empty($options)){
385
386 // Options
387 $options = acf_parse_args($_POST, array(
388 'post_id' => 0,
389 'i' => 0,
390 'field_key' => '',
391 'nonce' => '',
392 'layout' => '',
393 'value' => array()
394 ));
395
396 }
397
398 // Load field
399 $field = acf_get_field($options['field_key']);
400 if(!$field){
401 return $this->return_or_die();
402 }
403
404 // Layout
405 $instance = acf_get_field_type('flexible_content');
406 $layout = $instance->get_layout($options['layout'], $field);
407
408 if(!$layout){
409 return $this->return_or_die();
410 }
411
412 // Global
413 global $is_preview;
414
415 // Vars
416 $i = (int) $options['i'];
417 $field_key = $options['field_key'];
418 $value = wp_unslash($options['value']);
419
420 $is_preview = true;
421 $name = $field['_name'];
422 $key = $field['key'];
423 $l_name = $layout['name'];
424 $post_id = acf_uniqid('acfe/flexible_content/preview');
425
426 // prepare meta
427 $meta = array(
428 $field_key => array()
429 );
430
431 // if preview index is higher than 0
432 // add empty layouts to mimic get_row_index()
433 if($i > 0){
434 for($j=0; $j < $i; $j++){
435 $meta[ $field_key ][] = array(
436 'acf_fc_layout' => $layout['name']
437 );
438 }
439 }
440
441 // append current layout
442 $meta[ $field_key ][] = $value;
443
444 // setup meta
445 acfe_setup_meta($meta, $post_id, true);
446
447 if(have_rows($field_key)):
448 while(have_rows($field_key)): the_row();
449
450 // continue to loop until the correct preview index
451 if(acf_get_loop('active', 'i') !== $i){
452
453 // remove previously created empty layouts
454 // so acf_get_loop('active', 'value') only return one row (current)
455 $loop = acf_get_loop('active');
456 unset($loop['value'][ $loop['i'] ]);
457 acf_update_loop('active', 'value', $loop['value']);
458
459 continue;
460 }
461
462 // global post
463 global $post;
464 if($this->no_post){
465 $post = null;
466 }
467
468 // context:ajax/taxonomy/user page
469 if(!isset($post)){
470
471 $post_id = acfe_get_post_id('array');
472
473 // context:ajax
474 if($post_id['type'] === 'post'){
475 $post = get_post($post_id['id']);
476
477 // context:taxonomy/user page
478 // assign for next call
479 // this fix the issue where doing new WP_Query() in a taxonomy page
480 // will setup the last $post as global and break next get_field() calls
481 }else{
482 $this->no_post = true;
483 }
484
485 }
486
487 if($this->no_post){
488 add_action('loop_end', array($this, 'loop_end'));
489 }
490
491 add_filter('acf/pre_load_post_id', array($this, 'pre_load_post_id'), 2, 2);
492 add_action('loop_start', array($this, 'loop_start'));
493
494 // deprecated
495 do_action_deprecated("acfe/flexible/preview/name={$name}", array($field, $layout), '0.8.6.7');
496 do_action_deprecated("acfe/flexible/preview/key={$key}", array($field, $layout), '0.8.6.7');
497 do_action_deprecated("acfe/flexible/layout/preview/layout={$l_name}", array($field, $layout), '0.8.6.7');
498 do_action_deprecated("acfe/flexible/layout/preview/name={$name}&layout={$l_name}", array($field, $layout), '0.8.6.7');
499 do_action_deprecated("acfe/flexible/layout/preview/key={$key}&layout={$l_name}", array($field, $layout), '0.8.6.7');
500 do_action_deprecated("acfe/flexible/preview", array($field, $layout), '0.8.6.7');
501
502 // include template
503 acfe_flexible_render_layout_template($layout, $field);
504
505 remove_filter('acf/pre_load_post_id', array($this, 'pre_load_post_id'), 2);
506 remove_action('loop_start', array($this, 'loop_start'));
507
508 if($this->no_post){
509 remove_action('loop_end', array($this, 'loop_end'));
510 }
511
512 endwhile;
513 endif;
514
515 acfe_reset_meta();
516
517 $is_preview = false;
518
519 return $this->return_or_die();
520
521 }
522
523 /**
524 * loop_start
525 *
526 * Allow to use new WP_Query() in the layout preview/admin
527 * https://core.trac.wordpress.org/ticket/18408
528 *
529 * @return void
530 */
531 function loop_start(){
532
533 if(is_admin()){
534 global $wp_query, $post;
535 $wp_query->post = $post;
536 }
537
538 }
539
540
541 /**
542 * loop_end
543 *
544 * Quick hack for taxonomy/user/options page
545 * This reset the global $post after the end of while($q->have_posts()): $q->the_post()
546 * Not ideal, but it works
547 *
548 * @return void
549 */
550 function loop_end(){
551
552 if(is_admin()){
553 global $post;
554 $post = null;
555 }
556
557 }
558
559
560 /**
561 * pre_load_post_id
562 *
563 * This only affect get_field() in the layout preview/admin
564 *
565 * @param $null
566 * @param $post_id
567 *
568 * @return false|mixed
569 */
570 function pre_load_post_id($null, $post_id){
571
572 // post id provided
573 if($post_id){
574 return $null;
575 }
576
577 // retrieve global post
578 global $post;
579 if($post){
580 return null; // let acf find the post from the $post
581 }
582
583 // retrieve the correct post id dynamically
584 // context:taxonomy/user/options page
585 return acfe_get_post_id();
586
587 }
588
589
590 /**
591 * return_or_die
592 *
593 * @return bool|void
594 */
595 function return_or_die(){
596
597 // check ajax & make sure the action is correct
598 if(wp_doing_ajax() && acf_maybe_get_POST('action') === 'acfe/flexible/layout_preview'){
599 die;
600 }
601
602 return true;
603
604 }
605
606 }
607
608 acf_new_instance('acfe_field_flexible_content_preview');
609
610 endif;