-
Notifications
You must be signed in to change notification settings - Fork 4
/
acf-page-builder-field.php
505 lines (393 loc) · 17.5 KB
/
acf-page-builder-field.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
<?php
/*
Plugin Name: Advanced Custom Fields: Page Builder Field
Plugin URI: https://wordpress.org/plugins/acf-page-builder-field/
Description: This plugin will add a page builder field in Advanced custom fields
Version: 1.0.3
Author: Peter Elmered, Johan Möller, Viktor Fröberg, Olaf Lindström, Angry Creative
Author URI: https://angrycreative.se/
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
register_activation_hook( __FILE__, array( 'ACF_Page_Builder', 'check_required_plugins' ) );
class ACF_Page_Builder {
/**
* Variable that tells us if the plugin has added the needed filters
*
* @var boolean
*/
protected $activated = false;
/**
* Variable that holds the CSS/styles for the current page
*
* @var string
*/
protected $page_styles = '';
/**
* Keeps track of if we should activate the plugin on current page
*
* @var array - Array of page templates
*/
protected $use_on_current_page = null;
/**
* Holds the instance of this class
*
* @var null
*/
protected static $instance = null;
/**
* Return an instance of this class. Singleton pattern.
*
* @since 0.1.0
*
* @return object A single instance of this class.
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if (null == self::$instance) {
self::$instance = new self;
}
return self::$instance;
}
private function __construct() {
// add_action('template_redirect', array($this, 'init'), 20);
// Generate HTML for get_field()
add_filter('acf/format_value/type=page_builder_field', array( $this, 'render_page_builder_field' ), 10, 3);
$this->check_required_plugins();
}
/**
* Add the needed filters for correct output
*/
function init() {
// Only add the filters once
if( $this->activated ) {
return true;
}
// Prefix the panel id to make it unique for every field on a page
add_filter( 'siteorigin_panels_row_attributes', array( $this, 'siteorigin_panels_attributes' ), 10, 2 );
add_filter( 'siteorigin_panels_row_cell_attributes', array( $this, 'siteorigin_panels_attributes' ), 10, 2 );
add_filter( 'siteorigin_panels_layout_attributes', array( $this, 'siteorigin_panels_attributes' ), 10, 2 );
// The styles should outputted once per page, in the footer
add_action( 'wp_footer', array( $this, 'output_styles' ) );
$this->activated = true;
}
/**
* Check if required plugins are activated. If not deactivate this plugin to avoid problems and show an error
* message when you try to activate it.
*
* @param $already_activated
* @return bool
*/
static function check_required_plugins() {
// Check if SiteOrigin Page Builder and ACF is installed and active
if( function_exists( 'siteorigin_panels_render' ) && class_exists( 'acf' ) ) {
return true;
} else {
add_action( 'admin_notices', function() { ?>
<div class="notice-warning notice is-dismissible">
<p>Advanced Custom Fields: Page Builder Field will not work unless Advanced Custom Fields and Site Origin Page Builder is active.</p>
</div>
<?php } );
}
}
/**
* Generate HTML for a field
*
* @param $value
* @param $post_id
* @param $field
* @return string
*/
function start_style_wrapper( $name, $style = array(), $for = false ) {
$attributes = array();
if ( empty( $attributes['class'] ) ) {
$attributes['class'] = array();
}
if ( empty( $attributes['style'] ) ) {
$attributes['style'] = '';
}
// Get everything related to the style wrapper
$attributes = apply_filters( 'siteorigin_panels_' . $name . '_style_attributes', $attributes, $style );
$attributes = apply_filters( 'siteorigin_panels_general_style_attributes', $attributes, $style );
$standard_css = array();
$standard_css = apply_filters( 'siteorigin_panels_' . $name . '_style_css', $standard_css, $style );
$standard_css = apply_filters( 'siteorigin_panels_general_style_css', $standard_css, $style );
$mobile_css = array();
$mobile_css = apply_filters( 'siteorigin_panels_' . $name . '_style_mobile_css', $mobile_css, $style );
$mobile_css = apply_filters( 'siteorigin_panels_general_style_mobile_css', $mobile_css, $style );
// Remove anything we didn't actually use
if ( empty( $attributes['class'] ) ) {
unset( $attributes['class'] );
}
if ( empty( $attributes['style'] ) ) {
unset( $attributes['style'] );
}
$style_wrapper = '';
if ( ! empty( $attributes ) || ! empty( $standard_css ) || ! empty( $mobile_css ) ) {
if ( empty( $attributes['class'] ) ) {
$attributes['class'] = array();
}
$attributes['class'][] = 'panel-' . $name . '-style';
if ( ! empty( $for ) ) {
$attributes['class'][] = 'panel-' . $name . '-style-for-' . sanitize_html_class( $for );
}
$attributes['class'] = array_unique( $attributes['class'] );
// Filter and sanitize the classes
$attributes['class'] = apply_filters( 'siteorigin_panels_' . $name . '_style_classes', $attributes['class'], $attributes, $style );
$attributes['class'] = array_map( 'sanitize_html_class', $attributes['class'] );
$style_wrapper = '<div ';
foreach ( $attributes as $name => $value ) {
// Attributes start with _ are used for internal communication between filters, so are not added to the HTML
// We don't make use of this in our styling, so its left as a mechanism for other plugins.
if( substr( $name, 0, 1 ) === '_' ) continue;
if ( is_array( $value ) ) {
$style_wrapper .= $name . '="' . esc_attr( implode( " ", array_unique( $value ) ) ) . '" ';
} else {
$style_wrapper .= $name . '="' . esc_attr( $value ) . '" ';
}
}
$style_wrapper .= '>';
return $style_wrapper;
}
return $style_wrapper;
}
function render_page_builder_field( $value, $post_id, $field ) {
$this->init();
$uid = uniqid( true );
$len = strlen($uid);
$field_id = 'acfpbf_'.substr($uid, $len - 6, 6);
$acf_page_builder_content = $this->acf_siteorigin_panels_render( $field_id, $value );
$output = '';
if( $acf_page_builder_content ) {
$output .= '<div id="acf_page_builder_field_id_'.$field_id.'" class="acf-page-builder-field">';
$output .= $acf_page_builder_content;
$output .= '</div>';
}
return $output;
}
/**
* Prefixes the panel id to make it unique for every field on a page.
* Hooked in on:
* - siteorigin_panels_row_attributes
* - siteorigin_panels_row_cell_attributes
* - siteorigin_panels_layout_attributes
*
* @param $attr
* @param $panels_data
* @return mixed
*/
function siteorigin_panels_attributes( $attr, $panels_data )
{
global $panel_id;
$panel_id = $GLOBALS['panel_id'];
$attr['id'] = $panel_id.'-'.$attr['id'];
return $attr;
}
/**
* Enqueues the scripts that makes the ACF field work in the admin views
*/
function acf_field_page_builder_field_admin_enqueue_scripts()
{
wp_enqueue_script('acf-input-page_builder_field', plugin_dir_url( __FILE__ ).'js/input.js', array('jquery','so-panels-admin'), '1.0', true);
}
/**
* This is a rewrite of siteorigin_panels_render function in the SiteOrigin Page Builder plugin
*
* @return String - HTML of page builder field
*/
function acf_siteorigin_panels_render( $panel_id, $panels_data ) {
$renderer = SiteOrigin_Panels::renderer();
$GLOBALS['panel_id'] = $panel_id;
if( is_string( $panels_data ) )
{
$panels_data = json_decode( $panels_data, true );
}
if( empty($post_id) ) $post_id = get_the_ID();
$panels_data = apply_filters( 'siteorigin_panels_data', $panels_data, $post_id, $panel_id );
if( empty( $panels_data ) || empty( $panels_data['grids'] ) ) return '';
// Filter the widgets to add indexes
if ( !empty( $panels_data['widgets'] ) ) {
$last_gi = 0;
$last_ci = 0;
$last_wi = 0;
foreach ( $panels_data['widgets'] as $wid => &$widget_info ) {
if ( $widget_info['panels_info']['grid'] != $last_gi ) {
$last_gi = $widget_info['panels_info']['grid'];
$last_ci = 0;
$last_wi = 0;
}
elseif ( $widget_info['panels_info']['cell'] != $last_ci ) {
$last_ci = $widget_info['panels_info']['cell'];
$last_wi = 0;
}
$widget_info['panels_info']['cell_index'] = $last_wi++;
}
}
if( is_rtl() ) $panels_data = siteorigin_panels_make_rtl( $panels_data );
// Create the skeleton of the grids
$grids = array();
if( !empty( $panels_data['grids'] ) && !empty( $panels_data['grids'] ) ) {
foreach ( $panels_data['grids'] as $gi => $grid ) {
$gi = intval( $gi );
$grids[$gi] = array();
for ( $i = 0; $i < $grid['cells']; $i++ ) {
$grids[$gi][$i] = array();
}
}
}
// We need this to migrate from the old $panels_data that put widget meta into the "info" key instead of "panels_info"
if( !empty( $panels_data['widgets'] ) && is_array($panels_data['widgets']) ) {
foreach ( $panels_data['widgets'] as $i => $widget ) {
if( empty( $panels_data['widgets'][$i]['panels_info'] ) ) {
$panels_data['widgets'][$i]['panels_info'] = $panels_data['widgets'][$i]['info'];
unset($panels_data['widgets'][$i]['info']);
}
}
}
if( !empty( $panels_data['widgets'] ) && is_array($panels_data['widgets']) ){
foreach ( $panels_data['widgets'] as $widget ) {
// Put the widgets in the grids
$grids[ intval( $widget['panels_info']['grid']) ][ intval( $widget['panels_info']['cell'] ) ][] = $widget;
}
}
ob_start();
// Add the panel layout wrapper
$panel_layout_classes = apply_filters( 'siteorigin_panels_layout_classes', array(), $post_id, $panels_data, $panel_id );
$panel_layout_attributes = apply_filters( 'siteorigin_panels_layout_attributes', array(
'class' => implode( ' ', $panel_layout_classes ),
'id' => 'pl-' . $post_id
), $post_id, $panels_data );
echo '<div';
foreach ( $panel_layout_attributes as $name => $value ) {
if ($value) {
echo ' ' . $name . '="' . esc_attr($value) . '"';
}
}
echo '>';
global $acf_siteorigin_panels_inline_css;
if( empty($acf_siteorigin_panels_inline_css) ) $acf_siteorigin_panels_inline_css = array();
if( !isset($acf_siteorigin_panels_inline_css[$post_id]) ) {
wp_enqueue_style('siteorigin-panels-front');
$layout_data = $renderer->get_panels_layout_data( $panels_data );
$acf_siteorigin_panels_inline_css[$panel_id] = $renderer->generate_css( $post_id, $panels_data, $layout_data );
}
$this->page_styles = $acf_siteorigin_panels_inline_css;
echo apply_filters( 'siteorigin_panels_before_content', '', $panels_data, $post_id );
foreach ( $grids as $gi => $cells ) {
if( !empty( $panels_data['grids'][$gi]['style']['class'] ) ) {
$panels_data['grids'][$gi]['style']['class'] .= ' panel-row-style';
} else {
$panels_data['grids'][$gi]['style']['class'] = 'panel-row-style';
}
$grid_classes = apply_filters( 'siteorigin_panels_row_classes', array('panel-grid'), $panels_data['grids'][$gi] );
// Themes can add their own attributes to the style wrapper
$row_style_wrapper = $this->start_style_wrapper( 'row', $panels_data['grids'][$gi]['style'], $post_id . '-' . $gi );
$grid_classes[] = ! empty( $row_style_wrapper ) ? 'panel-has-style' : 'panel-no-style';
$grid_attributes = apply_filters( 'siteorigin_panels_row_attributes', array(
'class' => implode( ' ', $grid_classes ),
'id' => 'pg-' . $post_id . '-' . $gi
), $panels_data['grids'][$gi] );
// This allows other themes and plugins to add html before the row
echo apply_filters( 'siteorigin_panels_before_row', '', $panels_data['grids'][$gi], $grid_attributes );
echo '<div ';
foreach ( $grid_attributes as $name => $value ) {
echo $name.'="'.esc_attr($value).'" ';
}
echo '>';
if( !empty($row_style_wrapper) ) echo $row_style_wrapper;
foreach ( $cells as $ci => $widgets ) {
$cell_classes = array( 'panel-grid-cell' );
if ( empty( $widgets ) ) {
$cell_classes[] = 'panel-grid-cell-empty';
}
if ( $ci == count( $cells ) - 2 && count( $cells[ $ci + 1 ] ) == 0 ) {
$cell_classes[] = 'panel-grid-cell-mobile-last';
}
if(isset($panels_data['grids'][$gi]['style']['cell_class'])) {
$cell_classes[] = $panels_data['grids'][$gi]['style']['cell_class'];
}
// Themes can add their own styles to cells
$cell_classes = apply_filters( 'siteorigin_panels_cell_classes', $cell_classes, $panels_data, $widget );
$cell_attributes = apply_filters( 'siteorigin_panels_row_cell_attributes', array(
'class' => implode( ' ', $cell_classes ),
'id' => 'pgc-' . $post_id . '-' . $gi . '-' . $ci
), $panels_data );
echo '<div ';
foreach ( $cell_attributes as $name => $value ) {
echo $name.'="'.esc_attr($value).'" ';
}
echo '>';
$cell_style_wrapper = $this->start_style_wrapper( 'cell', array(), $post_id . '-' . $gi . '-' . $ci );
if( !empty($cell_style_wrapper) ) echo $cell_style_wrapper;
$widget_index = 0;
foreach ( $widgets as $pi => $widget_info ) {
$widget_info['panels_info']['widget_index'] = $widget_index;
$widget_index += 1;
// TODO this wrapper should go in the before/after widget arguments
$widget_style_wrapper = $this->start_style_wrapper( 'widget', ! empty( $widget_info['panels_info']['style'] ) ? $widget_info['panels_info']['style'] : array(), $post_id . '-' . $gi . '-' . $ci . '-' . $pi );
siteorigin_panels_the_widget( $widget_info['panels_info'], $widget_info, $gi, $ci, $pi, $pi == 0, $pi == count( $widgets ) - 1, $post_id, $widget_style_wrapper );
}
if ( empty( $widgets ) ) echo ' ';
if( !empty($cell_style_wrapper) ) echo '</div>';
echo '</div>';
}
echo '</div>';
// Close the
if( !empty($row_style_wrapper) ) echo '</div>';
// This allows other themes and plugins to add html after the row
echo apply_filters( 'siteorigin_panels_after_row', '', $panels_data['grids'][$gi], $grid_attributes );
}
echo apply_filters( 'siteorigin_panels_after_content', '', $panels_data, $post_id );
echo '</div>';
$html = ob_get_clean();
return apply_filters( 'siteorigin_panels_render', $html, $post_id, !empty($post) ? $post : null );
}
/**
* Output all the styles for the current page(all ACF fields in one chunk)
*/
function output_styles() {
global $acf_siteorigin_panels_inline_css;
// If we don't have any styles queued, bail out
if( empty( $acf_siteorigin_panels_inline_css ) || !is_array( $acf_siteorigin_panels_inline_css ) )
{
return;
}
$styles = '';
// Set unique IDs on the styles so we can target each field with CSS separately
foreach( $acf_siteorigin_panels_inline_css AS $style_key => $style_data )
{
$search = array( '#pgc', '#pg', '#pl' );
$replace = array( '#'.$style_key.'-pgc', '#'.$style_key.'-pg', '#'.$style_key.'-pl' );
$styles .= str_replace($search, $replace, $style_data);
}
echo '<style>'.$styles.'</style>';
}
}
/**
* Function to access the plugin object ( usage: ACFPB()->method() )
*
* @return object
*/
function ACFPB()
{
return ACF_Page_Builder::get_instance();
}
/**
* Enqueue admin scripts to make the page builder field work in the admin views
*/
add_action('init', function() {
if( is_admin() )
{
add_action( 'admin_print_scripts-post-new.php', array( ACFPB(), 'acf_field_page_builder_field_admin_enqueue_scripts' ) , 999 );
add_action( 'admin_print_scripts-post.php', array( ACFPB(), 'acf_field_page_builder_field_admin_enqueue_scripts' ), 999 );
}
} );
/**
* Register ACF field and instantiate the plugin
*/
add_action('acf/include_field_types', 'include_field_types_page_builder_field' );
// $acf_version = 5 and can be ignored until ACF6 exists
function include_field_types_page_builder_field( $acf_version ) {
ACFPB();
include_once('acf-page-builder-field-v5.php');
}