/**
 * On `plugins_loaded`, create an instance of the Fields API manager class.
 */
function _wp_fields_api_include()
{
    // Bail if we're already in WP core (depending on the name used)
    if (class_exists('WP_Fields_API') || class_exists('Fields_API')) {
        return;
    }
    require_once WP_FIELDS_API_DIR . 'implementation/wp-includes/fields-api/class-wp-fields-api.php';
    // Init Fields API class
    $GLOBALS['wp_fields'] = WP_Fields_API::get_instance();
}
 /**
  * Register fields config for sections/fields created using this plugin
  *
  * @param WP_Fields_API $wp_fields
  */
 protected function register_fields_config($wp_fields)
 {
     // Get supported forms/control types
     $fields_api_forms = $this->get_fields_api_forms();
     $fields_api_control_types = $this->get_fields_api_control_types();
     $args = array('post_type' => 'fields_api_section', 'posts_per_page' => 50, 'meta_query' => array(array('key' => 'fields_api_form', 'compare' => 'EXISTS')));
     $sections = get_posts($args);
     foreach ($sections as $section) {
         $form = get_post_meta($section->ID, 'fields_api_form', true);
         // Skip sections without forms or forms that aren't supported
         if (empty($form) || empty($fields_api_forms[$form])) {
             continue;
         }
         $object_type = $fields_api_forms[$form]['object_type'];
         $object_subtype = get_post_meta($section->ID, 'fields_api_object_subtype', true);
         // Set empty Object subtype to null
         if (empty($object_subtype)) {
             $object_subtype = null;
         }
         // Section config
         $section_id = get_post_meta($section->ID, 'fields_api_section_id', true);
         if (empty($section_id)) {
             $section_id = sanitize_key('fields-admin-ui-' . $section->ID . '-' . $section->post_name);
         }
         $section_config = array('form' => $form, 'label' => $section->post_title, 'controls' => array());
         $section_desc = get_post_meta($section->ID, 'fields_api_section_desc', true);
         if (!empty($section_desc)) {
             $section_config['description'] = $section_desc;
         }
         // Get controls for section
         $args = array('post_type' => 'fields_api_control', 'posts_per_page' => 50, 'meta_query' => array(array('key' => 'fields_api_section', 'value' => $section->ID)));
         $controls = get_posts($args);
         if (!empty($controls)) {
             foreach ($controls as $control) {
                 // Control config
                 $control_id = get_post_meta($control->ID, 'fields_api_control_id', true);
                 if (empty($control_id)) {
                     $control_id = sanitize_key($section->post_name . '_' . $control->post_name);
                 }
                 $section_config['controls'][$control_id] = array('label' => $control->post_title);
                 $control_type = get_post_meta($control->ID, 'fields_api_control_type', true);
                 if (!empty($control_type) && isset($fields_api_control_types[$control_type])) {
                     $section_config['controls'][$control_id]['type'] = $control_type;
                 }
                 $control_desc = get_post_meta($control->ID, 'fields_api_control_desc', true);
                 if (!empty($control_desc)) {
                     $section_config['controls'][$control_id]['description'] = $control_desc;
                 }
             }
             $wp_fields->add_section($object_type, $section_id, $object_subtype, $section_config);
         }
     }
 }
 /**
  * Encapsulated registering of sections, controls, and fields for a form
  *
  * @param WP_Fields_API $wp_fields
  */
 public function register_fields($wp_fields)
 {
     // @todo Remove this when done testing
     if (!defined('WP_FIELDS_API_EXAMPLES') || !WP_FIELDS_API_EXAMPLES) {
         return;
     }
     //////////////
     // Examples //
     //////////////
     $total_examples = 1;
     if (defined('WP_FIELDS_API_TESTING') && WP_FIELDS_API_TESTING && !empty($_GET['fields-api-memory-test'])) {
         $total_examples = 25;
         if (1 < $_GET['fields-api-memory-test']) {
             $total_examples = absint($_GET['fields-api-memory-test']);
         }
     }
     for ($x = 1; $x <= $total_examples; $x++) {
         // Section
         $section_id = $this->id . '-example-my-fields-' . $x;
         $section_args = array('label' => __('Fields API Example - My Fields', 'fields-api'), 'form' => $this->id);
         if (1 < $total_examples) {
             $section_args['label'] .= ' ' . $x;
         }
         if (in_array($this->object_type, array('post', 'comment'))) {
             $section_args['type'] = 'meta-box';
         }
         $wp_fields->add_section($this->object_type, $section_id, $this->object_name, $section_args);
         // Add example for each control type
         $control_types = array('text', 'textarea', 'checkbox', 'multi-checkbox', 'radio', 'select', 'dropdown-pages', 'dropdown-terms', 'color', 'media', 'media-file');
         $option_types = array('multi-checkbox', 'radio', 'select');
         foreach ($control_types as $control_type) {
             $field_id = 'example_my_' . $x . '_' . $control_type . '_field';
             $label = sprintf(__('%s Field'), ucwords(str_replace('-', ' ', $control_type)));
             $field_args = array('control' => array('type' => $control_type, 'id' => $this->id . '-' . $field_id, 'section' => $section_id, 'label' => $label, 'description' => 'Example field description'));
             if (in_array($control_type, $option_types)) {
                 $field_args['control']['choices'] = array('' => 'N/A', 'option-1' => 'Option 1', 'option-2' => 'Option 2', 'option-3' => 'Option 3', 'option-4' => 'Option 4', 'option-5' => 'Option 5');
                 if ('multi-checkbox' == $control_type) {
                     unset($field_args['control']['choices']['']);
                 }
             } elseif ('checkbox' == $control_type) {
                 $field_args['control']['checkbox_label'] = 'Example checkbox label';
             }
             if ('dropdown-terms' == $control_type) {
                 $field_args['control']['taxonomy'] = 'category';
             }
             $wp_fields->add_field($this->object_type, $field_id, $this->object_name, $field_args);
         }
     }
 }
 /**
  * Remove a field control.
  *
  * @access public
  *
  * @param string $object_type Object type, set true to remove all controls.
  * @param string $id          Control ID to remove, set true to remove all controls from an object.
  * @param string $object_name Object name (for post types and taxonomies), set true to remove to all objects from an object type.
  */
 public function remove_control($object_type, $id, $object_name = null)
 {
     if (true === $object_type) {
         // Remove all controls
         self::$controls = array();
     } elseif (true === $object_name) {
         // Remove all controls for an object type
         if (isset(self::$controls[$object_type])) {
             unset(self::$controls[$object_type]);
         }
     } else {
         if (empty($object_name) && !empty($object_type)) {
             $object_name = '_' . $object_type;
             // Default to _object_type for internal handling
         }
         if (true === $id && null !== $object_name) {
             // Remove all controls for an object type
             if (isset(self::$controls[$object_type][$object_name])) {
                 unset(self::$controls[$object_type][$object_name]);
             }
         } elseif (isset(self::$controls[$object_type][$object_name][$id])) {
             // Remove control from object type and name
             unset(self::$controls[$object_type][$object_name][$id]);
         }
     }
 }
 /**
  * Register term fields once for all term forms
  *
  * @param WP_Fields_API $wp_fields
  */
 public function register_term_fields($wp_fields)
 {
     static $registered;
     if ($registered) {
         return;
     }
     $registered = true;
     $wp_fields->add_field($this->object_type, 'name');
     $wp_fields->add_field($this->object_type, 'slug');
     $wp_fields->add_field($this->object_type, 'parent');
     $wp_fields->add_field($this->object_type, 'description');
 }
 /**
  * Encapsulated registering of sections, controls, and fields for a form
  *
  * @param WP_Fields_API $wp_fields
  */
 public function register_fields($wp_fields)
 {
     /*
     // Register control types
     $wp_fields->register_control_type( 'control-type-id', 'Control_Class_Name' );
     
     // Add section(s)
     $wp_fields->add_section( $this->object_type, 'section-id', $this->object_name, array(
     	'title' => __( 'Section Heading' ),
         'form' => $this->id,
     ) );
     
     $field_args = array(
     	// 'sanitize_callback' => array( $this, 'my_sanitize_callback' ),
     	'control'                   => array(
     		'type'                  => 'text',
     		'section'               => 'section-id',
     		'label'                 => __( 'Control Label' ),
     		'description'           => __( 'Description of control' ),
     		// 'capabilities_callback' => array( $this, 'my_capabilities_callback' ),
     	),
     );
     
     $wp_fields->add_field( $this->object_type, 'field-id', $this->object_name, $field_args );
     */
     //////////////
     // Examples //
     //////////////
     // Section
     $section_args = array('title' => __('Fields API Example - My Fields'), 'form' => $this->id);
     if (in_array($this->object_type, array('post', 'comment'))) {
         $section_args['type'] = 'meta-box';
     }
     $wp_fields->add_section($this->object_type, $this->id . '-example-my-fields', $this->object_name, $section_args);
     // Add example for each control type
     $control_types = array('text', 'textarea', 'checkbox', 'multi-checkbox', 'radio', 'select', 'dropdown-pages', 'dropdown-terms', 'color', 'media', 'media-file');
     $option_types = array('multi-checkbox', 'radio', 'select');
     foreach ($control_types as $control_type) {
         $id = 'example_my_' . $control_type . '_field';
         $label = sprintf(__('%s Field'), ucwords(str_replace('-', ' ', $control_type)));
         $field_args = array('control' => array('type' => $control_type, 'id' => $this->id . '-' . $id, 'section' => $this->id . '-example-my-fields', 'label' => $label));
         if (in_array($control_type, $option_types)) {
             $field_args['control']['choices'] = array('' => 'N/A', 'option-1' => 'Option 1', 'option-2' => 'Option 2', 'option-3' => 'Option 3', 'option-4' => 'Option 4', 'option-5' => 'Option 5');
         }
         $wp_fields->add_field($this->object_type, $id, $this->object_name, $field_args);
     }
 }
/**
 * On `plugins_loaded`, create an instance of the Fields API manager class.
 */
function _wp_fields_api_include()
{
    require_once WP_FIELDS_API_DIR . 'implementation/wp-includes/fields-api/class-wp-fields-api.php';
    // Init Fields API class
    $GLOBALS['wp_fields'] = WP_Fields_API::get_instance();
}