/** * When the post is saved, save the custom data contained in the meta box. * * @since 0.1.0 * @param int $post_id The ID of the post being saved. */ public function save($post_id) { if (!ev_user_can_save($post_id, 'ev_meta_box')) { return; } $elements = $this->elements(); if (!empty($elements)) { $skip_field_types = ev_skip_on_saving_field_types(); foreach ($elements as $index => $element) { if ($element['type'] === 'group') { foreach ($element['fields'] as $field) { if (!ev_is_skipped_on_saving($field['type'])) { if (!isset($_POST[$field['handle']])) { delete_post_meta($post_id, $field['handle']); } else { $this->_save_single_field($post_id, $field, $_POST[$field['handle']]); } } } } else { if (!ev_is_skipped_on_saving($element['type'])) { if (!isset($_POST[$element['handle']])) { delete_post_meta($post_id, $element['handle']); } else { $this->_save_single_field($post_id, $element, $_POST[$element['handle']]); } } } } } }
/** * When the page is refreshed, save the custom data contained in the admin page. * * @since 0.1.0 * @param string $group The group of the page that is being saved. */ public function save($group = '') { /* This should run on admin only. */ if (!is_admin()) { return; } /* Verify that we're submitting any data. */ if (empty($_POST)) { return; } /* Verify the validity of the supplied nonce. */ $is_valid_nonce = ev_is_post_nonce_valid('ev_admin_page'); /* Check the user has the capability to save the page. */ $is_valid_cap = current_user_can($this->capability()); /* Exit if the nonce is invalid or the user doesn't have the required capability to save the page. */ if (!$is_valid_nonce || !$is_valid_cap) { return; } $elements = $this->elements(); if (!empty($elements)) { foreach ($elements as $index => $element) { if ($element['type'] === 'group' && $element['handle'] === $group) { foreach ($element['fields'] as $field) { if (!ev_is_skipped_on_saving($field['type'])) { if (!isset($_POST[$field['handle']])) { $this->_delete_single_field($field['handle']); } else { $this->_save_single_field($field, $_POST[$field['handle']]); } } } break; } else { if (!ev_is_skipped_on_saving($element['type'])) { if (!isset($_POST[$element['handle']])) { $this->_delete_single_field($element['handle']); } else { $this->_save_single_field($element, $_POST[$element['handle']]); } } } } $type = 'success'; $message = apply_filters('ev_save_options_tab_response_message', __('All saved!', 'ev_framework'), $type); $heading = apply_filters('ev_save_options_tab_response_heading', '', $type); $args = apply_filters("ev_save_options_tab_response_args[tab:{$group}]", array()); ev_ajax_message($message, $type, $heading, $args); } }
/** * Render the field inner content. * * @since 0.1.0 * @param Ev_Field $field A field object. */ public function render_inner($field = false) { if ($field === false) { $field = $this; } echo '<div class="ev-field-inner">'; echo '<div class="ev-field-panel-controls-wrapper">'; echo '<div class="ev-field-panel-controls-inner-wrapper">'; echo '<span class="ev-repeatable-remove"></span>'; echo '<span class="ev-sortable-handle"></span>'; echo '</div>'; echo '</div>'; if ($this->_bundle === false && !ev_is_skipped_on_saving($this->_type)) { $this->_render_repeatable_controls('prepend'); } $template = EV_FRAMEWORK_TEMPLATES_FOLDER . "fields/{$this->_type}"; $template = apply_filters("ev_field_template[type:{$this->_type}]", $template); ev_template($template, array('field' => $field)); if ($this->_bundle === false && !ev_is_skipped_on_saving($this->_type)) { $this->_render_repeatable_controls('append'); } echo '</div>'; }
/** * Render the field inner content. * * @since 0.1.0 * @param Ev_Field $field A field object. */ public function render_inner($field = false) { $field_types = ev_field_types(); $value = $this->value(); $handle = $this->handle(); if ($field !== false) { $value = $field->value(); $handle = $field->handle(); } echo '<div class="ev-bundle-fields-wrapper">'; echo '<div class="ev-field-panel-controls-wrapper">'; echo '<div class="ev-field-panel-controls-inner-wrapper">'; echo '<span class="ev-repeatable-remove"></span>'; echo '<span class="ev-sortable-handle"></span>'; echo '</div>'; echo '</div>'; if (!ev_is_skipped_on_saving($this->_type)) { $this->_render_repeatable_controls('prepend', 'medium'); } foreach ($this->_fields as $index => $field_data) { $field_class = $field_types[$field_data['type']]; $field_data['bundle'] = $handle; $fld = new $field_class($field_data); if (isset($value[$field_data['handle']])) { $fld->value($value[$field_data['handle']]); } $fld->render(); } if (!ev_is_skipped_on_saving($this->_type)) { $this->_render_repeatable_controls('append', 'medium'); } echo '</div>'; }
/** * Render a group of fields in the fields container. * * @since 0.1.0 * @param array $element The group data. * @param integer $index The group index. * @param integer $current_index The index of the current group. */ private function render_group($group, $index, $current_index = 0) { $class = ''; if (isset($_GET['tab'])) { if ($_GET['tab'] === $group['handle']) { $class = 'ev-active'; } } elseif ($index == $current_index) { $class = 'ev-active'; } printf('<div aria-labelledby="%s" id="ev-tab-%s" class="ev-tab %s" role="tabpanel">', esc_attr($group['handle']), esc_attr($group['handle']), esc_attr($class)); $can_be_saved = false; if ($this->_groups_with_form === true) { foreach ($group['fields'] as $index => $field) { if (!ev_is_skipped_on_saving($field['type'])) { $can_be_saved = true; break; } } } if ($can_be_saved && $this->_groups_with_form === true) { printf('<form method="%s" action="%s">', 'post', admin_url('/admin-ajax.php')); printf('<input type="hidden" name="group" value="%s">', esc_attr($group['handle'])); printf('<input type="hidden" name="context" value="%s">', esc_attr($this->handle())); } foreach ($group['fields'] as $index => $field) { $this->render_field($field); } if ($can_be_saved && $this->_groups_with_form === true) { $group_callback = 'ev_save_options_tab'; echo '<div class="ev-form-submit-container">'; ev_btn(__('Save', 'ev_framework'), 'save', array('attrs' => array('data-callback' => $group_callback, 'type' => 'submit'), 'size' => 'medium')); echo '</div>'; echo '</form>'; } echo '</div>'; }
/** * Render the field interface. * * @since 0.1.0 */ public function render() { $label = $this->label(); printf('<div class="%s" %s>', esc_attr(implode(' ', $this->classes())), $this->attrs()); echo '<div class="ev-field-header ev-field-header-label-' . esc_attr($label["type"]) . '">'; $this->_render_label(); $this->_render_help(); echo '</div>'; $values = (array) $this->value(); $container_class = ''; if (isset($this->_repeatable['empty_state']) && $this->_repeatable['empty_state'] !== '') { if (empty($values) || isset($values[0]) && empty($values[0])) { $container_class .= ' ev-container-empty'; } } if ($this->_repeatable !== false) { if (empty($values) || isset($values[0]) && empty($values[0])) { $container_class .= ' ev-no-fields'; } } printf('<div class="ev-container %s">', esc_attr($container_class)); if ($this->_repeatable !== false) { if (!ev_is_skipped_on_saving($this->_type)) { $this->_render_repeatable_controls(); } $this->_render_inner_repeatable(); if (!isset($this->_repeatable['append']) || $this->_repeatable['append'] === true) { if (!ev_is_skipped_on_saving($this->_type)) { $this->_render_repeatable_controls(false); } } } else { $this->render_inner(); } echo '</div>'; echo '</div>'; }