/** * Module settings field * * @since 1.0.0 * @access public * @return array */ public function fields() { $template_parts = TF_Model::get_posts('tf_template_part'); if (!empty($template_parts)) { $options = array(); foreach ($template_parts as $t) { if (!isset($_REQUEST['template_id']) || $_REQUEST['template_id'] != $t->ID) { $options[] = array('name' => $t->post_title, 'value' => $t->ID); } } if (!empty($options)) { return apply_filters('tf_module_template_part_fields', array('part' => array('type' => 'select', 'label' => __('Template_Part', 'themify-flow'), 'options' => $options))); } } return apply_filters('tf_module_template_part_fields', array()); }
/** * Print the field based on field type. * * @since 1.0.0 * @access public * @param string $field_name Fieldname. * @param array $field Field Properties. * @param array $exist_data Exist field values. * @return string */ public static function print_field($field_name, $field, $exist_data = array()) { global $wp_registered_sidebars, $wp_widget_factory; $field = wp_parse_args($field, array('class' => '', 'default' => '')); $field_id = 'tf_field_' . $field_name; // prefix field id to avoid identic tag ID from another elements $output = ''; $exist_value = isset($exist_data[$field_name]) ? $exist_data[$field_name] : ''; switch ($field['type']) { case 'text': case 'number': $exist_value = '' == $exist_value ? $field['default'] : $exist_value; $output .= sprintf('<input type="%s" name="%s" id="%s" class="%s" value="%s">', esc_attr($field['type']), esc_attr($field_name), esc_attr($field_id), esc_attr($field['class']), esc_attr($exist_value)); break; case 'hidden': $exist_value = '' == $exist_value ? $field['default'] : $exist_value; $output .= sprintf('<input type="hidden" name="%s" id="%s" class="%s" value="%s">', esc_attr($field_name), esc_attr($field_id), esc_attr($field['class']), esc_attr($exist_value)); break; case 'textarea': $exist_value = '' == $exist_value ? $field['default'] : $exist_value; $output .= sprintf('<textarea id="%s" name="%s" class="%s" rows="4" cols="3">%s</textarea>', esc_attr($field_id), esc_attr($field_name), esc_attr($field['class']), esc_textarea($exist_value)); break; case 'select': $exist_value = '' == $exist_value ? $field['default'] : $exist_value; $size = isset($field['size']) && intval($field['size']) > 0 ? $field['size'] : 10; $multi = isset($field['multiple']) ? 'multiple="multiple" size="' . $size . '"' : ''; if ($multi) { $field_name = $field_name . '[]'; if ($exist_value && !is_array($exist_value)) { $exist_value = maybe_unserialize(stripslashes($exist_value)); } else { if (!is_array($exist_value)) { $exist_value = array(); } } } $output .= sprintf('<select name="%s" id="%s" class="%s" ' . $multi . '>', esc_attr($field_name), esc_attr($field_id), esc_attr($field['class'])); foreach ($field['options'] as $option) { if ($multi) { $selected = in_array($option['value'], $exist_value) ? 'selected="selected"' : ''; } else { $selected = selected($exist_value, $option['value'], false); } $output .= sprintf('<option value="%s"%s>%s</option>', esc_attr($option['value']), $selected, esc_html($option['name'])); } $output .= '</select>'; break; case 'select_group': $exist_value = '' == $exist_value ? $field['default'] : $exist_value; $output .= sprintf('<select name="%s" id="%s" class="%s">', esc_attr($field_name), esc_attr($field_id), esc_attr($field['class'])); if (isset($field['show_empty'])) { $output .= '<option value=""> </option>'; } foreach ($field['options'] as $key => $group) { $output .= sprintf('<optgroup label="%s">', $group['label']); foreach ($group['options'] as $option) { $output .= sprintf('<option value="%s"%s>%s</option>', esc_attr($option['value']), selected($exist_value, $option['value'], false), esc_html($option['name'])); } $output .= '</optgroup>'; } $output .= '</select>'; break; case 'wp_editor': $exist_value = '' == $exist_value ? $field['default'] : $exist_value; $rows = isset($field['rows']) ? $field['rows'] : 6; ob_start(); wp_editor($exist_value, $field_id, array('textarea_name' => $field_name, 'textarea_rows' => $rows, 'editor_class' => 'tf_wp_editor ' . $field['class'])); $output .= ob_get_contents(); ob_end_clean(); break; case 'separator': $output .= isset($field['meta']['html']) && '' != $field['meta']['html'] ? $field['meta']['html'] : '<hr class="meta_fields_separator" />'; break; case 'image': case 'video': $exist_value = '' == $exist_value ? $field['default'] : $exist_value; $input_type = 'image' == $field['type'] ? 'hidden' : 'text'; $output .= sprintf('<input type="%s" name="%s" id="%s" class="tf_upload_value tf_type-%s %s" value="%s">', esc_attr($input_type), esc_attr($field_name), esc_attr($field_id), $field['type'], esc_attr($field['class']), esc_attr($exist_value)); $output .= '<div class="tf_small">'; if (is_multisite() && !is_upload_space_available()) { $output .= sprintf(__('<p>Sorry, you have filled your %s MB storage quota so uploading has been disabled.</p>', 'themify-flow'), get_space_allowed()); } else { $output .= sprintf('<a href="#" class="tf_upload_media_library" data-uploader-title="%s" data-uploader-button-text="%s" data-library-type="%s">%s</a>', 'image' == $field['type'] ? esc_attr__('Upload an Image', 'themify-flow') : esc_attr__('Upload a Video', 'themify-flow'), esc_attr__('Insert file URL', 'themify-flow'), esc_attr($field['type']), 'image' == $field['type'] ? esc_attr__('+ Add Image', 'themify-flow') : esc_attr__('+ Add Video', 'themify-flow')); $max_upload_size = (int) wp_max_upload_size() / (1024 * 1024); $output .= sprintf(__('<p class="tf_max_upload_size">Maximum upload file size: %d MB.</p>', 'themify-flow'), $max_upload_size); } $output .= '</div>'; if ('image' == $field['type']) { $img_el = TF_Model::get_attachment_image($exist_value, array(50, 50)); $output .= sprintf('<p class="tf_thumb_preview"> <span class="tf_thumb_preview_placeholder">%s</span> <a href="#" class="tf_thumb_preview_delete"><span class="ti-close"></span></a> </p>', $img_el); } break; case 'color': $color = '' != $exist_value ? explode('_', $exist_value) : array(); $color_val = isset($color[0]) ? $color[0] : ''; $exist_value = '' == $exist_value ? $field['default'] : $exist_value; $output .= '<div class="tf_custom_color_wrap">'; $output .= sprintf('<input type="text" class="tf_color_picker %s" value="%s">', esc_attr($field['class']), esc_attr($color_val)); $output .= sprintf('<input type="hidden" name="%s" id="%s" class="tf_color_pick_value" value="%s">', esc_attr($field_name), esc_attr($field_id), esc_attr($exist_value)); $output .= '<a class="remove-color ti-close" href="#"></a></div>'; break; case 'radio': $meta_attr = ''; foreach ($field['options'] as $option) { $checked = isset($option['selected']) && $option['selected'] == true ? 'checked="checked"' : ''; $checked = '' != $exist_value && $option['value'] == $exist_value ? 'checked="checked"' : $checked; $meta_attr = isset($field['toggleable']) ? ' data-toggleable="' . $field['toggleable']['target_class'] . '-' . $option['value'] . '"' : ''; $output .= sprintf('<input type="radio" id="%s" class="%s" name="%s" value="%s" %s%s>', esc_attr($field_name . '_' . $option['value']), esc_attr($field['class']), esc_attr($field_name), esc_attr($option['value']), $checked, $meta_attr); $output .= sprintf('<label for="%s">%s</label>', esc_attr($field_name . '_' . $option['value']), esc_html($option['name'])); } break; case 'checkbox': $checked = $exist_value || isset($field['checked']) && true == $field['checked'] ? 'checked="checked"' : ''; $output .= sprintf('<label><input type="checkbox" name="%s" id="%s" class="%s" value="1" %s> %s</label>', esc_attr($field_name), esc_attr($field_id), esc_attr($field['class']), $checked, $field['text']); break; case 'checkbox_group': $output .= sprintf('<div class="tf_checkbox_group" data-field-name="%s">', $field_name); foreach ($field['options'] as $key => $value) { $output .= sprintf('<label><input type="checkbox" name="%s[%s]" id="%s" class="%s" value="1" data-key="%s"> %s</label>', esc_attr($field_name), $key, esc_attr($field_id), esc_attr($field['class']), $key, $value); } $output .= '</div>'; break; case 'layout': $output .= sprintf('<p id="%s" class="tf_option_icons tf-layout-icon">', esc_attr($field_name)); $value = ''; foreach ($field['options'] as $option) { $selected = isset($option['selected']) && true == $option['selected'] ? ' selected' : ''; $value = isset($option['selected']) && true == $option['selected'] && '' == $value ? $option['value'] : $value; // Check exist value if ('' != $exist_value) { $selected = $option['value'] == $exist_value ? ' selected' : ''; $value = $exist_value; } $output .= sprintf('<a href="#" data-value="%s" title="%s" class="tf-layout-option%s"><img src="%s" alt="%s" /></a>', esc_attr($option['value']), esc_attr($option['label']), $selected, esc_url($option['img']), esc_attr($option['label'])); } $output .= sprintf('<input type="hidden" name="%s" value="%s" class="val">', esc_attr($field_name), esc_attr($value)); $output .= '</p>'; break; case 'template_assign': $selected = '' != $exist_value ? $exist_value : array(); $output .= self::print_archive_tabs($field_name, $selected); $output .= self::print_single_tabs($field_name, $selected); $output .= self::print_page_tabs($field_name, $selected); break; case 'template_part_select': $output .= sprintf('<select name="%s" id="%s" class="%s">', esc_attr($field_name), esc_attr($field_id), esc_attr($field['class'])); $output .= '<option></option>'; $posts = TF_Model::get_posts('tf_template_part'); foreach ($posts as $part) { $selected = selected($exist_value, $part->post_name, false); $output .= sprintf('<option value="%s" %s>%s</option>', esc_attr($part->post_name), $selected, esc_html($part->post_title)); } $output .= '</select><br/>'; if (isset($field['show_extra_link']) && true == $field['show_extra_link'] || !isset($field['show_extra_link'])) { $output .= sprintf('<a href="%s" target="_blank" class="manage_template_part_link"><span class="tf_icon ti-folder"></span> %s</a>', admin_url('edit.php?post_type=tf_template_part'), __('Manage Template Part', 'themify-flow')); } break; case 'builder': ob_start(); global $TF; $TF->in_builder_lightbox = true; $exist_value = '' == $exist_value ? $field['default'] : wp_kses_stripslashes($exist_value); $output .= '<div class="tf_back_builder tf_lightbox_builder_field-init" data-builder-field="' . $field_id . '">'; TF_Interface::builder_element_panel($field['options']['category']); TF_Interface::builder_row_panel($exist_value); $output .= ob_get_contents(); $output .= '</div>'; $TF->in_builder_lightbox = false; ob_end_clean(); // Hold the builder value $output .= sprintf('<input type="hidden" name="_has_builder[]" value="%s">', esc_attr($field_name)); $output .= sprintf('<input type="hidden" name="%s" id="%s" class="tf_field_builder" value="%s">', esc_attr($field_name), esc_attr($field_id), esc_attr($exist_value)); break; case 'icon': $exist_value = '' == $exist_value ? $field['default'] : $exist_value; $output .= sprintf('<input type="text" name="%s" id="%s" class="%s" value="%s">', esc_attr($field_name), esc_attr($field_id), esc_attr($field['class']), esc_attr($exist_value)); $output .= '<a class="button button-secondary hide-if-no-js tf_fa_toggle" href="#">' . __('Insert Icon', 'themify-flow') . '</a>'; break; case 'gallery': $exist_value = '' == $exist_value ? $field['default'] : $exist_value; $output .= sprintf('<input type="hidden" id="%s" name="%s" class="tf_gallery_value %s" value="%s">', esc_attr($field_id), esc_attr($field_name), esc_attr($field['class']), esc_textarea($exist_value)); $output .= sprintf('<a href="#" class="tf_browse_gallery_btn">%s</a>', __('+ Insert Gallery', 'themify-flow')); $output .= '<div class="tf_gallery_preview">'; // place outside images loop for js placeholder if ($slider_images = tf_get_images_from_gallery_shortcode($exist_value)) { foreach ($slider_images as $image) { $output .= sprintf('<p class="tf_thumb_preview"> <span class="tf_thumb_preview_placeholder">%s</span> </p>', wp_get_attachment_image($image->ID, array(50, 50))); } } $output .= '</div>'; break; case 'widget_area': $exist_value = '' == $exist_value ? $field['default'] : $exist_value; $output .= sprintf('<select name="%s" id="%s" class="%s">', esc_attr($field_name), esc_attr($field_id), esc_attr($field['class'])); $output .= '<option value=""></option>'; foreach ($wp_registered_sidebars as $k => $v) { $output .= sprintf('<option value="%s"%s>%s</option>', esc_attr($v['id']), selected($exist_value, $v['id'], false), esc_html($v['name'])); } $output .= '</select>'; break; case 'widget': $exist_value = '' == $exist_value ? $field['default'] : $exist_value; $output .= sprintf('<select name="%s" id="%s" class="tf-field-widget-select %s">', esc_attr($field_name), esc_attr($field_id), esc_attr($field['class'])); $output .= '<option value=""></option>'; foreach ($wp_widget_factory->widgets as $class => $widget) { $output .= sprintf('<option data-idbase="%s" value="%s"%s>%s</option>', esc_attr($widget->id_base), esc_attr($class), selected($exist_value, $class, false), esc_html($widget->name)); } $output .= '</select>'; $output .= '<div class="tf-widget-form"></div>'; break; case 'html': $output .= $field['html']; break; default: /** * Hook your own field type. * * @since 1.0.0 */ $output .= apply_filters('tf_form_fields', $field_id, $field_name, $exist_value); break; } $output .= isset($field['description']) ? sprintf('<small>%s</small>', $field['description']) : ''; return $output; }
/** * Render Meta Box content. * * @param WP_Post $post The post object. */ public function tf_render_metabox($post) { global $TF; $templates = new WP_Query(array('post_type' => 'tf_template', 'post_status' => 'publish', 'meta_query' => array('relation' => 'AND', array('key' => 'tf_template_type', 'value' => $post->post_type == 'post' ? 'single' : 'page'), array('key' => 'associated_theme', 'value' => $TF->active_theme->slug)), 'order' => 'ASC', 'orderby' => 'title', 'posts_per_page' => -1)); $template_parts = TF_Model::get_posts('tf_template_part'); wp_nonce_field(self::PREFIX . '_metabox_nonce', self::PREFIX . '_metabox_nonce'); $template_id = get_post_meta($post->ID, self::PREFIX . '_template', true); $header = get_post_meta($post->ID, self::PREFIX . '_header', true); $header = $header ? esc_attr($header) : 'default'; $header_template = $header == 'custom' ? get_post_meta($post->ID, self::PREFIX . '_header_template', true) : false; $sidebar = get_post_meta($post->ID, self::PREFIX . '_sidebar', true); $sidebar = $sidebar ? esc_attr($sidebar) : 'default'; $sidebar_template = $sidebar == 'custom' ? get_post_meta($post->ID, self::PREFIX . '_sidebar_template', true) : false; $footer = get_post_meta($post->ID, self::PREFIX . '_footer', true); $footer = $footer ? esc_attr($footer) : 'default'; $footer_template = $footer == 'custom' ? get_post_meta($post->ID, self::PREFIX . '_footer_template', true) : false; ?> <div class="tf_interface"> <div class="tf_lightbox_row"> <div class="tf_lightbox_label"> <label for="<?php echo self::PREFIX; ?> _template"><?php _e('Template', 'themify-flow'); ?> </label> </div> <div class="tf_lightbox_input"> <div class="tf_custom_select"> <select name="<?php echo self::PREFIX; ?> [template]" id="<?php echo self::PREFIX; ?> _template"> <option value=""><?php _e('Select Template', 'themify-flow'); ?> </option> <?php while ($templates->have_posts()) { $templates->the_post(); ?> <option <?php if ($template_id == get_the_ID()) { ?> selected="selected"<?php } ?> value="<?php the_ID(); ?> "><?php the_title(); ?> </option> <?php } ?> </select> </div> </div> </div> <div id="tf_lightbox_radios"> <div class="tf_lightbox_row"> <div class="tf_lightbox_label"> <label for="<?php echo self::PREFIX; ?> _header_default"><?php _e('Header', 'themify-flow'); ?> </label> </div> <div class="tf_lightbox_input"> <input <?php checked($header, 'default', true); ?> type="radio" name="<?php echo self::PREFIX; ?> [header]" id="<?php echo self::PREFIX; ?> _header_default" value="default" /> <label for="<?php echo self::PREFIX; ?> _header_default"><?php _e('Default', 'themify-flow'); ?> </label> <input <?php checked($header, 'no', true); ?> type="radio" name="<?php echo self::PREFIX; ?> [header]" id="<?php echo self::PREFIX; ?> _header_no" value="no" /> <label for="<?php echo self::PREFIX; ?> _header_no"><?php _e('No', 'themify-flow'); ?> </label> <?php if (!empty($template_parts)) { ?> <input <?php checked($header, 'custom', true); ?> type="radio" name="<?php echo self::PREFIX; ?> [header]" id="<?php echo self::PREFIX; ?> _header_custom" value="custom" /> <label for="<?php echo self::PREFIX; ?> _header_custom"><?php _e('Custom', 'themify-flow'); ?> </label> <div class="tf_custom_select"> <select name="<?php echo self::PREFIX; ?> [header_template]"> <?php foreach ($template_parts as $part) { ?> <option <?php selected($header_template, $part->post_name, true); ?> value="<?php echo esc_attr($part->post_name); ?> "><?php echo esc_html($part->post_title); ?> </option> <?php } ?> </select> </div> <?php } ?> </div> </div> <div class="tf_lightbox_row"> <div class="tf_lightbox_label"> <label for="<?php echo self::PREFIX; ?> _sidebar_default"><?php _e('Sidebar', 'themify-flow'); ?> </label> </div> <div class="tf_lightbox_input"> <input <?php checked($sidebar, 'default', true); ?> type="radio" name="<?php echo self::PREFIX; ?> [sidebar]" id="<?php echo self::PREFIX; ?> _sidebar_default" value="default" /> <label for="<?php echo self::PREFIX; ?> _sidebar_default"><?php _e('Default', 'themify-flow'); ?> </label> <input <?php checked($sidebar, 'sidebar_none', true); ?> type="radio" name="<?php echo self::PREFIX; ?> [sidebar]" id="<?php echo self::PREFIX; ?> _sidebar_no" value="sidebar_none" /> <label for="<?php echo self::PREFIX; ?> _sidebar_no"><?php _e('No', 'themify-flow'); ?> </label> <?php if (!empty($template_parts)) { ?> <input <?php checked($sidebar, 'custom', true); ?> type="radio" name="<?php echo self::PREFIX; ?> [sidebar]" id="<?php echo self::PREFIX; ?> _sidebar_custom" value="custom" /> <label for="<?php echo self::PREFIX; ?> _sidebar_custom"><?php _e('Custom', 'themify-flow'); ?> </label> <div class="tf_custom_select"> <select name="<?php echo self::PREFIX; ?> [sidebar_template]"> <?php foreach ($template_parts as $part) { ?> <option <?php selected($sidebar_template, $part->post_name, true); ?> value="<?php echo esc_attr($part->post_name); ?> "><?php echo esc_html($part->post_title); ?> </option> <?php } ?> </select> </div> <?php } ?> </div> </div> <div class="tf_lightbox_row"> <div class="tf_lightbox_label"> <label for="<?php echo self::PREFIX; ?> _footer_default"><?php _e('Footer', 'themify-flow'); ?> </label> </div> <div class="tf_lightbox_input"> <input <?php checked($footer, 'default', true); ?> type="radio" name="<?php echo self::PREFIX; ?> [footer]" id="<?php echo self::PREFIX; ?> _footer_default" value="default" /> <label for="<?php echo self::PREFIX; ?> _footer_default"><?php _e('Default', 'themify-flow'); ?> </label> <input <?php checked($footer, 'no', true); ?> type="radio" name="<?php echo self::PREFIX; ?> [footer]" id="<?php echo self::PREFIX; ?> _footer_no" value="no" /> <label for="<?php echo self::PREFIX; ?> _footer_no"><?php _e('No', 'themify-flow'); ?> </label> <?php if (!empty($template_parts)) { ?> <input <?php checked($footer, 'custom', true); ?> type="radio" name="<?php echo self::PREFIX; ?> [footer]" id="<?php echo self::PREFIX; ?> _footer_custom" value="custom" /> <label for="<?php echo self::PREFIX; ?> _footer_custom"><?php _e('Custom', 'themify-flow'); ?> </label> <div class="tf_custom_select"> <select name="<?php echo self::PREFIX; ?> [footer_template]"> <?php foreach ($template_parts as $part) { ?> <option <?php selected($footer_template, $part->post_name, true); ?> value="<?php echo esc_attr($part->post_name); ?> "><?php echo esc_html($part->post_title); ?> </option> <?php } ?> </select> </div> <?php } ?> </div> </div> </div> </div> <?php wp_reset_postdata(); ?> <?php }