/** * Sanitizes the setting data. * * @param string[][] $data Settings data. * * @return string[][] */ public function sanitize($data) { if (!$this->settings_page->current_user_can('edit')) { $error = $this->settings_error_factory->create('no-permission-to-edit'); $error->add(); $data = $this->option->get(); return $data; } $sanitized_data = array(); foreach ($data as $source => $targets) { if (!is_string($source) || $source === '') { $error = $this->settings_error_factory->create('invalid-taxonomy'); $error->add(); } else { foreach ($targets as $target => $link) { if (!is_string($target) || $target === '') { $error = $this->settings_error_factory->create('invalid-taxonomy'); $error->add(); } else { switch ((int) $link) { case 1: $sanitized_data = $this->add_link($sanitized_data, $source, $target); break; case 2: $sanitized_data = $this->add_link($sanitized_data, $source, $target, TRUE); break; } } } } } return $sanitized_data; }
/** * Renders the HTML. * * @return void */ public function render() { /** * Filters the args for getting all taxonomies. * * @see get_taxonomies() * * @param array $args Taxonomies args. */ $args = apply_filters('linked_taxonomies_get_taxonomies_args', array()); $this->all_taxonomies = get_taxonomies($args, 'objects'); /** * Filters the taxonomies that are available for linking. * * @param object[] $taxonomies Taxonomy objects. */ $this->all_taxonomies = (array) apply_filters('linkable_taxonomies', $this->all_taxonomies); $this->linked_taxonomies = $this->option->get(); ?> <div class="wrap"> <h2> <?php echo $this->title; ?> </h2> <?php if (!$this->all_taxonomies) { ?> <p> <?php esc_html_e('No linkable taxonomies found.', 'linked-taxonomies'); ?> </p> <?php } elseif ($this->current_user_can_edit) { ?> <?php $this->render_form(); ?> <?php } else { ?> <?php $this->render_table(); ?> <?php } ?> </div> <?php }
/** * Constructor. Sets up the properties. * * @param Option $option Option model. */ public function __construct(Option $option) { $this->linked_taxonomies = $option->get(); }
/** * @covers tfrommen\LinkedTaxonomies\Setting\Option::get * @dataProvider provide_get_data * * @param array $expected * @param array $default * @param mixed $option * * @return void */ public function test_get(array $expected, array $default, $option) { $testee = new Testee(); WP_Mock::wpFunction('get_option', array('args' => array(Mockery::type('string'), $default), 'return' => $option)); $this->assertSame($expected, $testee->get()); }