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'; + ?>