From 4a1a3d2125cfa1af2c3ea43672b5c5b94688fad7 Mon Sep 17 00:00:00 2001 From: Graziano Vincini Date: Thu, 25 Aug 2016 10:05:40 +0200 Subject: [PATCH] Aeria section multiple support Signed-off-by: Graziano Vincini --- classes/AeriaSection.php | 103 ++++++++++++++++++++++++--------------- 1 file changed, 64 insertions(+), 39 deletions(-) diff --git a/classes/AeriaSection.php b/classes/AeriaSection.php index f8efbf8..e4f1f0a 100644 --- a/classes/AeriaSection.php +++ b/classes/AeriaSection.php @@ -8,11 +8,14 @@ public static function register($args){ if(empty($args['type'])) die("AeriaSection: You must define a post_type id"); if(empty($args['title'])) $args['title'] = 'Sections'; - $section_id = 'aeria_section' . ( !empty($args['id']) ? "_{$args['id']}" : "" ); - add_action('add_meta_boxes', function() use ($args, $section_id){ + $id_section = (empty($args['id']))?'':$args['id']; + + add_action('add_meta_boxes', function() use ($args, $id_section){ + + $id_metabox = !empty($id_section)?'aeria_section_'.$id_section:'aeria_section'; foreach ( (array) $args['type'] as $type ) { add_meta_box( - $section_id, + $id_metabox, $args['title'], function($post) use ($args) { AeriaSection::render_controls($args); @@ -24,14 +27,16 @@ function($post) use ($args) { }); foreach ( (array) $args['type'] as $type ) { - add_filter( "postbox_classes_{$type}_{$section_id}", function($classes) use ($args, $section_id) { - array_push( $classes, $section_id ); + add_filter( 'postbox_classes_' . $type . '_aeria_section', function( $classes ) use ( $args ) { + array_push( $classes, 'aeria_section_' . $args['title'] ); return $classes; }); } add_action('save_post', function($post_id) use($args) { + $id_section = (empty($args['id']))?'':$args['id'].'_'; + if (!isset($_POST['section_metabox_nonce'])){ return; } @@ -60,20 +65,20 @@ function($post) use ($args) { $sections = []; $s = 0; - while ( isset($_POST['post_section_columns_'.$s])) { + while ( isset($_POST[$id_section.'post_section_columns_'.$s])) { // check columns - $columns = $_POST['post_section_columns_'.$s]; + $columns = $_POST[$id_section.'post_section_columns_'.$s]; $content = []; for ($i=1; $i <= $columns ; $i++) { - if(isset($_POST['post_section_'.$s.'_'.$i])) $content['column_'.$i] = wpautop($_POST['post_section_'.$s.'_'.$i]); + if(isset($_POST[$id_section.'post_section_'.$s.'_'.$i])) $content['column_'.$i] = wpautop($_POST[$id_section.'post_section_'.$s.'_'.$i]); } $sections['section_'.$s] = [ 'columns' => $columns, - 'title' => sanitize_text_field($_POST['post_section_title_'.$s]), - 'background' => $_POST['post_section_background_'.$s], + 'title' => sanitize_text_field($_POST[$id_section.'post_section_title_'.$s]), + 'background' => $_POST[$id_section.'post_section_background_'.$s], 'content' => $content ]; @@ -81,24 +86,26 @@ function($post) use ($args) { if ( count( $args['fields'] ) === 1 ){ $value_type = array_keys( $args['fields'] )[0]; }else{ - $value_type = ( isset($_POST['section_type_'.$s] ) && !empty( $_POST['section_type_'.$s] ) )? $_POST['section_type_'.$s] : '' ; + $value_type = ( isset($_POST[$id_section.'section_type_'.$s] ) && !empty( $_POST[$id_section.'section_type_'.$s] ) )? $_POST[$id_section.'section_type_'.$s] : '' ; } if( $value_type ) $sections['section_'.$s]['section_type'] = $value_type; //save classic fields if(isset($args['fields']) && !empty($args['fields']) && isset($args['fields'][0]['type'])){ foreach ($args['fields'] as $field) { - $sections['section_'.$s]['fields'][$field['id']] = $_POST[$field['id'].'_'.$s]; + $sections['section_'.$s]['fields'][$field['id']] = $_POST[$id_section.$field['id'].'_'.$s]; } }elseif(count($args['fields']) && !empty($value_type)) { foreach ($args['fields'][$value_type]['fields'] as $field) { - $sections['section_'.$s]['fields'][$field['id']] = $_POST[$field['id'].'_'.$s]; + $sections['section_'.$s]['fields'][$field['id']] = $_POST[$id_section.$field['id'].'_'.$s]; } } $s++; } - if(!empty($sections)) update_post_meta( $post_id, 'post_sections', wp_slash(json_encode($sections,JSON_UNESCAPED_UNICODE)) ); + $id_meta_section = !empty($id_section)?'_'.str_replace('_','',$id_section):''; + + if(!empty($sections)) update_post_meta( $post_id, 'post_sections'.$id_meta_section, wp_slash(json_encode($sections,JSON_UNESCAPED_UNICODE)) ); }); @@ -115,18 +122,19 @@ function($post) use ($args) { ); }); - add_action( 'wp_ajax_add_section', function(){ - AeriaSection::render_section([],$_POST['section'],$_POST['ncol']); exit; + add_action( 'wp_ajax_add_section', function() { + AeriaSection::render_section([],$_POST['section'],$_POST['ncol'],['id' => $_POST['id_section']]); exit; }); add_action( 'wp_ajax_sort_section', function(){ - AeriaSection::sort_section($_POST['order'], $_POST['post_id']); exit; + AeriaSection::sort_section($_POST['order'], $_POST['post_id'],$_POST['id_section']); exit; }); } - public static function sort_section($order, $post_id) { - $sections = json_decode(get_post_meta( $post_id, 'post_sections', true ),true); + public static function sort_section($order, $post_id, $id_section) { + $id_meta_section = !empty($id_section)?'_'.$id_section:''; + $sections = json_decode(get_post_meta( $post_id, 'post_sections'.$id_meta_section, true ),true); $new_sections = []; $s = 0; @@ -136,7 +144,7 @@ public static function sort_section($order, $post_id) { $s++; } - update_post_meta( $post_id, 'post_sections', wp_slash(json_encode($new_sections,JSON_UNESCAPED_UNICODE)) ); + update_post_meta( $post_id, 'post_sections'.$id_meta_section, wp_slash(json_encode($new_sections,JSON_UNESCAPED_UNICODE)) ); die(json_encode([ 'success' => 1 @@ -144,6 +152,10 @@ public static function sort_section($order, $post_id) { } public static function render_controls($args=[]){ + + $id_section = (empty($args['id']))?'':$args['id']; + $id_metabox = !empty($id_section)?'aeria_section_'.$id_section:'aeria_section'; + ?>
@@ -156,16 +168,22 @@ public static function render_controls($args=[]){ - - - + + +
@@ -177,11 +195,11 @@ public static function render_sections($post_id, $args){ echo ''; - echo ''; + echo ''; } ?> @@ -203,9 +221,11 @@ public static function render_sections($post_id, $args){ '; echo ''; @@ -253,15 +273,17 @@ public static function render_field($field=null,$key=0,$val=''){ echo ''; } - public static function render_relation_fields($fields=null,$key=0,$val='',$preview_path){ + public static function render_relation_fields($fields=null,$key=0,$val='',$preview_path, $id_section=''){ if(!$fields) return; + $field_id = (empty($id_section))?'':$id_section.'_'; + $row_class = !empty($preview_path)?'row-half':'row-full'; $preview_attr = !empty($preview_path)?'data-select-preview="'.$preview_path.'"':''; echo '
'; - echo ''; echo ''; foreach ($fields as $k => $value) { $selected = ($val == $k)?'selected="selected"':''; @@ -288,6 +310,9 @@ public static function render_section($section_passed = [], $key = 0, $ncol = 1, $support_fields = (!isset($args['supports']) || empty($args['supports']) || in_array('fields', $args['supports'])); $preview_path = (isset($args['preview_path']) && !empty($args['preview_path']))?$args['preview_path']:false; + $id_section = (empty($args['id']))?'':$args['id']; + $id_meta_section = !empty($id_section)?'_'.$id_section:''; + $id_input = !empty($id_section)?$id_section.'_':''; if(empty($section_passed)) { $section = [ @@ -321,17 +346,17 @@ public static function render_section($section_passed = [], $key = 0, $ncol = 1, ?>
> - +
-
x
-
- +
x
+
+
- +
- +
@@ -352,7 +377,7 @@ public static function render_section($section_passed = [], $key = 0, $ncol = 1, echo '
'; foreach ( $args['fields'] as $field ) { $value = (isset($section['fields'][$field['id']]) && !empty($section['fields'][$field['id']]))?$section['fields'][$field['id']]:''; - AeriaSection::render_field($field,$key,$value); + AeriaSection::render_field($field,$key,$value,$id_section); } echo '
'; }elseif( count( $args['fields'] ) ) { @@ -364,7 +389,7 @@ public static function render_section($section_passed = [], $key = 0, $ncol = 1, $value_type = current(array_keys($args['fields'])); }else{ $value_type = isset( $section['section_type'] )?$section['section_type']:''; // get value from general section settings - AeriaSection::render_relation_fields( $args['fields'],$key,$value_type,$preview_path ); + AeriaSection::render_relation_fields( $args['fields'],$key,$value_type,$preview_path,$id_section ); } if(!empty($value_type)){ @@ -372,7 +397,7 @@ public static function render_section($section_passed = [], $key = 0, $ncol = 1, echo '
'; foreach ($args['fields'][$value_type]['fields'] as $field) { $value = ( isset( $section['fields'][$field['id']] ) && !empty( $section['fields'][$field['id']]))?$section['fields'][$field['id']]:''; - AeriaSection::render_field( $field,$key,$value ); + AeriaSection::render_field( $field,$key,$value,$id_section ); } echo '
'; }else{ @@ -386,7 +411,7 @@ public static function render_section($section_passed = [], $key = 0, $ncol = 1, if($support_columns){ for ($i=1; $i <= $section['columns']; $i++) { if($section['columns'] > 1) echo '

Column '.$i.'

'; - wp_editor( stripslashes($section['content']['column_'.$i]) , 'post_section_'.$key.'_'.$i ); + wp_editor( stripslashes($section['content']['column_'.$i]) , $id_meta_section.'post_section_'.$key.'_'.$i ); } } ?>