public function test_repeatable_multiselect_save()
 {
     $fm = new Fieldmanager_Select(array('name' => 'base_field', 'multiple' => true, 'limit' => 0, 'options' => array('one', 'two', 'three')));
     $fm->add_meta_box('base_field', $this->post->post_type)->save_to_post_meta($this->post->ID, array('two'));
     $saved_value = get_post_meta($this->post->ID, 'base_field', true);
     $this->assertSame(array('two'), $saved_value);
     $fm->add_meta_box('base_field', $this->post->post_type)->save_to_post_meta($this->post->ID, array('two', 'three'));
     $saved_value = get_post_meta($this->post->ID, 'base_field', true);
     $this->assertSame(array('two', 'three'), $saved_value);
     $fm->add_meta_box('base_field', $this->post->post_type)->save_to_post_meta($this->post->ID, '');
     $saved_value = get_post_meta($this->post->ID, 'base_field', true);
     $this->assertEquals(null, $saved_value);
 }
 public function init()
 {
     $fm = new Fieldmanager_Select('Basic Select', array('name' => 'basic_select', 'options' => array('One', 'Two', 'Three')));
     $fm->add_meta_box('Basic Select Field', 'demo-select');
     $fm = new Fieldmanager_Group(array('name' => 'select_assorted', 'children' => array('first_empty' => new Fieldmanager_Select(array('label' => 'First Empty', 'first_empty' => true, 'options' => array(1 => 'One', 2 => 'Two', 3 => 'Three'), 'attributes' => array('style' => 'width:150px'))), 'first_instruct' => new Fieldmanager_Select(array('label' => 'First Instruction', 'options' => array('' => 'Choose one', 1 => 'One', 2 => 'Two', 3 => 'Three'), 'attributes' => array('style' => 'width:150px'))), 'default_set' => new Fieldmanager_Select(array('label' => 'Default Set', 'default_value' => '2', 'options' => array(1 => 'One', 2 => 'Two', 3 => 'Three'), 'attributes' => array('style' => 'width:150px'))), 'multiple' => new Fieldmanager_Select(array('label' => 'Multiple Select', 'options' => array(1 => 'One', 2 => 'Two', 3 => 'Three'), 'attributes' => array('multiple' => 'mutliple', 'style' => 'width:150px'))))));
     $fm->add_meta_box('Select Fields with Assorted Options', 'demo-select');
     $fm = new Fieldmanager_Group(array('name' => 'repeatable_select', 'limit' => 0, 'add_more_label' => 'Add another field', 'sortable' => true, 'label' => 'Field', 'children' => array('select_field' => new Fieldmanager_Select('Repeatable Field', array('options' => array('One', 'Two', 'Three'))))));
     $fm->add_meta_box('Repeatable Select Fields', 'demo-select');
     $fm = new Fieldmanager_Select('Basic Select', array('name' => 'sidebar_select', 'options' => array('One', 'Two', 'Three')));
     $fm->add_meta_box('Sidebar Select Field', 'demo-select', 'side');
     $fm = new Fieldmanager_Group(array('name' => 'sidebar_repeatable_select', 'limit' => 0, 'add_more_label' => 'Add another field', 'sortable' => true, 'label' => 'Field', 'children' => array('select_field' => new Fieldmanager_Select('Repeatable Field', array('options' => array('One', 'Two', 'Three'))))));
     $fm->add_meta_box('Sidebar Repeatable Select Fields', 'demo-select', 'side');
 }
 public function test_options_post_parent_render()
 {
     $fm = new Fieldmanager_Select(array('name' => 'test_parent', 'datasource' => new Fieldmanager_Datasource_Post(array('only_save_to_post_parent' => true, 'query_args' => array('post_type' => 'post', 'post_status' => 'draft')))));
     ob_start();
     $fm->add_meta_box('Test Select', 'post')->render_meta_box($this->child_post_a, array());
     $html = ob_get_clean();
     $this->assertRegExp(sprintf('#<option\\s*value="%s"\\s*>%s</option>#i', $this->parent_post->ID, $this->parent_post->post_title), $html);
     $this->save_values($fm, $this->child_post_a, $this->parent_post->ID);
     $child = get_post($this->child_post_a->ID);
     $this->assertEquals($this->parent_post->ID, $child->post_parent);
     $this->assertEquals('', get_post_meta($this->child_post_a->ID, 'test_parent', true));
     ob_start();
     $fm->add_meta_box('Test Select', 'post')->render_meta_box($this->child_post_a, array());
     $html = ob_get_clean();
     $this->assertRegExp(sprintf('#<option\\s*value="%s"\\s*selected(?:\\s*=\\s*"selected")?\\s*>%s</option>#i', $this->parent_post->ID, $this->parent_post->post_title), $html);
 }
 /**
  * Adds the meta boxes required to manage an ad layer.
  *
  * @param string|array $post_types
  * @param string $context
  * @param string $priority
  */
 public function add_meta_boxes()
 {
     if (!class_exists('Fieldmanager_Field')) {
         return;
     }
     // Add ad units
     $ad_unit_args = array('name' => 'ad_layer_ad_units', 'limit' => 0, 'extra_elements' => 0, 'one_label_per_item' => false, 'sortable' => true, 'label' => __('Select one or more ad units.', 'ad-layers'), 'add_more_label' => __('Add an ad unit', 'ad-layers'), 'children' => array('ad_unit' => new Fieldmanager_Select(array('label' => __('Ad Unit', 'ad-layers'), 'options' => Ad_Layers_Ad_Server::instance()->get_ad_units())), 'do_not_render' => new Fieldmanager_Checkbox(__('Do not render the ad on load', 'ad-layers'))));
     $targeting_args = Ad_Layers_Ad_Server::instance()->get_custom_targeting_args('custom_targeting');
     if (!empty($targeting_args)) {
         $ad_unit_args['children']['custom_targeting'] = new Fieldmanager_Group(apply_filters('ad_layers_custom_targeting_ad_unit_args', $targeting_args));
     }
     $fm_ad_units = new Fieldmanager_Group(apply_filters('ad_layers_ad_units_field_args', $ad_unit_args));
     $fm_ad_units->add_meta_box(__('Ad Units', 'ad-layers'), $this->post_type, 'normal', 'high');
     // Add page types
     $fm_page_types = new Fieldmanager_Select(apply_filters('ad_layers_page_types_field_args', array('name' => 'ad_layer_page_types', 'limit' => 0, 'extra_elements' => 0, 'one_label_per_item' => false, 'label' => __('Select one or more page types to be targeted with this ad layer.', 'ad-layers'), 'add_more_label' => __('Add a page type', 'ad-layers'), 'options' => Ad_Layers::instance()->get_page_types())));
     $fm_page_types->add_meta_box(__('Page Types', 'ad-layers'), $this->post_type, 'normal', 'high');
     // Add taxonomies
     $fm_taxonomies = new Fieldmanager_Select(apply_filters('ad_layers_taxonomies_field_args', array('name' => 'ad_layer_taxonomies', 'limit' => 0, 'extra_elements' => 0, 'one_label_per_item' => false, 'label' => __('Select one or more optional taxonomies for targeting. Posts with any term in these taxonomies will get the ad layer.', 'ad-layers'), 'add_more_label' => __('Add a taxonomy', 'ad-layers'), 'options' => Ad_Layers::instance()->get_taxonomies())));
     $fm_taxonomies->add_meta_box(__('Taxonomies', 'ad-layers'), $this->post_type, 'normal', 'high');
     // Add post types
     $fm_post_types = new Fieldmanager_Select(apply_filters('ad_layers_post_types_field_args', array('name' => 'ad_layer_post_types', 'limit' => 0, 'extra_elements' => 0, 'one_label_per_item' => false, 'label' => __('Select one or more optional post types for targeting. Any post of this type will get the ad layer.', 'ad-layers'), 'add_more_label' => __('Add a post type', 'ad-layers'), 'options' => Ad_Layers::instance()->get_post_types())));
     $fm_post_types->add_meta_box(__('Post Types', 'ad-layers'), $this->post_type, 'normal', 'high');
     // Custom targeting variables
     $targeting_args = Ad_Layers_Ad_Server::instance()->get_custom_targeting_args();
     if (!empty($targeting_args)) {
         $fm_custom = new Fieldmanager_Group(apply_filters('ad_layers_custom_targeting_field_args', $targeting_args));
         $fm_custom->add_meta_box(__('Page Level Custom Targeting', 'ad-layers'), $this->post_type, 'normal', 'low');
     }
 }