public static function render_section($section_passed = [], $key = 0, $ncol = 1, $args = []) { //check supports $support_columns = !isset($args['supports']) || empty($args['supports']) || in_array('columns', $args['supports']); $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 = ['columns' => $ncol, 'title' => '', 'background' => '', 'content' => ['column_1' => '']]; } else { $section = ['columns' => $section_passed['columns'], 'title' => $section_passed['title'], 'background' => $section_passed['background']]; for ($i = 1; $i <= $section_passed['columns']; $i++) { $section['content']['column_' . $i] = $section_passed['content']['column_' . $i]; } if (!empty($section_passed['fields'])) { $section['fields'] = $section_passed['fields']; } if (!empty($section_passed['section_type'])) { $section['section_type'] = $section_passed['section_type']; } } ?> <div class="box-section<?php echo $section_passed['section_type'] ? ' section_' . $section_passed['section_type'] : ''; ?> " data-section-num=<?php echo $key; ?> > <input type="hidden" data-section-columns="<?php echo $id_section; ?> " name="<?php echo $id_input; ?> post_section_columns_<?php echo $key; ?> " value="<?php echo $section['columns']; ?> "> <div class="header-section" > <div class="remove_background" data-remove-background="<?php echo $id_section; ?> ">x</div> <div class="background" data-section-background="<?php echo $id_section; ?> " style="background-image:url(<?php echo stripslashes($section['background']); ?> );"> <input type="hidden" name="<?php echo $id_input; ?> post_section_background_<?php echo $key; ?> " id="<?php echo $id_input; ?> post_section_background_<?php echo $key; ?> " value="<?php echo $section['background']; ?> " /> </div> <div class="title"> <input type="text" placeholder="Here the title" class="post_section_title" name="<?php echo $id_input; ?> post_section_title_<?php echo $key; ?> " id="<?php echo $id_input; ?> post_section_title_<?php echo $key; ?> " value="<?php echo stripslashes($section['title']); ?> " > </div> <div class="controls"> <button class="button button-small" data-section-expand="<?php echo $id_section; ?> " ><span class="dashicons dashicons-welcome-write-blog"></span></button> </div> <div style="clear:both;"></div> </div> <div class="body-section"> <?php /** * check pre render field * classic or conditional fields? */ if (isset($args['fields']) && !empty($args['fields'])) { if (isset($args['fields'][0]['type'])) { /** * Classic list fields */ echo '<div class="wrap-fields">'; 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, $id_section); } echo '</div>'; } elseif (count($args['fields'])) { /** * Relation list fields */ if (count($args['fields']) === 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, $id_section); } if (!empty($value_type)) { echo '<div class="row-full"><h4>' . strtoupper($args['fields'][$value_type]['description']) . '</h4></div>'; echo '<div class="wrap-fields">'; 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, $id_section); } echo '</div>'; } else { echo '<div class="row-full"><p>Nessuna tipologia di sezione attiva</p></div>'; } } } if ($support_columns) { for ($i = 1; $i <= $section['columns']; $i++) { if ($section['columns'] > 1) { echo '<h2>Column ' . $i . '</h2>'; } wp_editor(stripslashes($section['content']['column_' . $i]), $id_meta_section . 'post_section_' . $key . '_' . $i); } } ?> </div> </div> <?php }
public static function register($type) { if (empty($type['id'])) { trigger_error('AeriaType: You must define a post_type id.', E_USER_ERROR); } $type['options'] = empty($type['options']) ? array() : $type['options']; $post_type = $type['id']; $post_name = isset($type['title']) ? $type['title'] : ucfirst($type['id']); static::$types[$post_type] = $type; if (false === empty($type['metabox'])) { // Support one or multiple metabox definitions if (isset($type['metabox']['id'])) { $type['metabox'] = [$type['metabox']]; } foreach ($type['metabox'] as $mbox) { if (empty($mbox['pages'])) { $mbox['pages'] = [$post_type]; } if (!in_array($post_type, $mbox['pages'])) { $mbox['pages'][] = $post_type; } AeriaMetabox::register($mbox); } unset($type['metabox']); } if (false === empty($type['taxonomy'])) { // Support one or multiple taxonomy definitions if (isset($type['taxonomy']['id'])) { $type['taxonomy'] = [$type['taxonomy']]; } foreach ($type['taxonomy'] as $tax) { if (empty($tax['types'])) { $tax['types'] = [$post_type]; } if (!in_array($post_type, $tax['types'])) { $tax['types'][] = $post_type; } AeriaTaxonomy::register($tax); } unset($type['taxonomy']); } if (false === empty($type['columns'])) { AeriaColumns::register($type['id'], $type['columns']); unset($type['columns']); } if (false === empty($type['sections'])) { $sections_args = ['type' => $post_type]; if (isset($type['sections']['fields'])) { $sections_args['fields'] = $type['sections']['fields']; } if (isset($type['sections']['description'])) { $sections_args['description'] = $type['sections']['description']; } if (isset($type['sections']['supports'])) { $sections_args['supports'] = $type['sections']['supports']; } AeriaSection::register($sections_args); unset($type['sections']); } add_action('init', function () use($type, $post_type, $post_name) { $options = array_merge_replace(array('public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => ['slug' => $post_type, 'with_front' => false], 'capability_type' => 'post', 'with_front' => false, 'has_archive' => true, 'hierarchical' => true, 'feeds' => false, 'menu_position' => null, 'reorder' => false, 'supports' => false), $type['options']); unset($type['options']); $options_supports = $options['supports']; $options_supports = !$options_supports ? 'title,editor' : str_replace(' ', '', $options_supports); $options['supports'] = explode(',', $options_supports); if ($options['hierarchical']) { $options['supports'][] = 'page-attributes'; } $options['labels'] = array_merge_replace(array('name' => $post_name, 'singular_name' => $post_name, 'add_new' => 'Add new', 'add_new_item' => 'Add new Item', 'edit_item' => 'Edit', 'new_item' => 'New', 'all_items' => 'Show all', 'view_item' => 'Show item', 'search_items' => 'Search', 'not_found' => 'Not found', 'not_found_in_trash' => 'Not found in trash', 'parent_item_colon' => '', 'taxonomies' => [], 'menu_name' => $post_name, 'menu_icon' => null), $type); register_post_type($post_type, $options); if ($options['reorder']) { new AeriaReorder(array('post_type' => $post_type, 'order' => 'ASC', 'heading' => $options['labels']['singular_name'], 'final' => '', 'initial' => '', 'menu_label' => __('Reorder', 'reorder'), 'icon' => '', 'post_status' => 'publish', 'show_title' => isset($options['reorder']['show_title']) ? $options['reorder']['show_title'] : true, 'fields' => $options['reorder']['fields'] ?: false)); } // Check and register relations $relations = isset($type['relations']) ? $type['relations'] : false; if ($relations) { $meta_fields = []; foreach ((array) $relations as $key => $relation) { $multiple = isset($relation['multiple']) ? $relation['multiple'] : false; $meta_fields[] = ['name' => $relation['title'], 'id' => 'relations_' . $post_type . '_' . $relation['type'], 'type' => 'select_ajax', 'multiple' => $multiple, 'relation' => $relation['type']]; } AeriaMetabox::register(['id' => 'relations_' . $post_type, 'title' => 'Relations ' . $options['labels']['name'], 'pages' => [$post_type], 'fields' => $meta_fields]); } }); }