public function test_garbage_collection()
 {
     $term_option = new Fieldmanager_Textfield(array('name' => 'term_option'));
     // check normal save and fetch behavior
     $text = rand_str();
     $this->save_values($term_option, $this->term, $text);
     $data = fm_get_term_meta($this->term->term_id, $this->term->taxonomy, 'term_option', true);
     $this->assertEquals($text, $data);
     // Verify the FM term post exists
     $post = get_page_by_path("fm-term-meta-{$this->term->term_id}-category", OBJECT, 'fm-term-meta');
     $this->assertTrue(!empty($post->ID));
     $this->assertEquals('fm-term-meta', $post->post_type);
     $post_id = $post->ID;
     $this->assertEquals($text, get_post_meta($post_id, 'term_option', true));
     // Delete the term
     wp_delete_term($this->term->term_id, 'category');
     // The post and meta should be deleted
     $post = get_page_by_path("fm-term-meta-{$this->term->term_id}-category", OBJECT, 'fm-term-meta');
     $this->assertEmpty($post);
     $this->assertEquals('', get_post_meta($post_id, 'term_option', true));
 }
 /**
  * @group term_splitting
  */
 public function test_term_splitting()
 {
     // Ensure that term splitting exists
     if (!function_exists('wp_get_split_terms')) {
         return;
     }
     global $wpdb;
     // Add our first term. This is the one that will split off.
     $t1 = wp_insert_term('Joined Term', 'category');
     // Add term meta to the term
     $value = rand_str();
     $term_id_1 = $t1['term_id'];
     fm_add_term_meta($term_id_1, 'category', 'split_test', $value);
     // Add a second term to a custom taxonomy
     register_taxonomy('fm_test_tax', 'post');
     $t2 = wp_insert_term('Second Joined Term', 'fm_test_tax');
     // Manually modify the second term to setup the term splitting
     // condition. Shared terms don't naturally occur any longer.
     $wpdb->update($wpdb->term_taxonomy, array('term_id' => $term_id_1), array('term_taxonomy_id' => $t2['term_taxonomy_id']), array('%d'), array('%d'));
     // Verify that we can retrieve the term meta
     $this->assertEquals($value, fm_get_term_meta($term_id_1, 'category', 'split_test', true));
     // Update the term to cause it to split
     $new_t1 = wp_update_term($term_id_1, 'category', array('name' => 'Split Term'));
     // Verify that the term updated and split
     $this->assertTrue(isset($new_t1['term_id']));
     $this->assertNotEquals($new_t1['term_id'], $term_id_1);
     // Verify that the term meta works at the new term id
     $this->assertEquals($value, fm_get_term_meta($new_t1['term_id'], 'category', 'split_test', true));
     // Verify that we CANNOT access the term meta at the old term id
     $this->assertEquals('', fm_get_term_meta($term_id_1, 'category', 'split_test', true));
 }
 /**
  * Callback to get term meta for the given term ID and current taxonomy.
  *
  * @see get_term_meta().
  * @see Fieldmanager_Util_Term_Meta::get_term_meta() (Deprecated).
  */
 protected function get_data($term_id, $meta_key, $single = false)
 {
     if ($this->use_fm_meta) {
         return fm_get_term_meta($term_id, $this->current_taxonomy, $meta_key, $single);
     } else {
         return get_term_meta($term_id, $meta_key, $single);
     }
 }
 /**
  * @group serialize_data
  */
 public function test_unserialize_data_mixed_depth()
 {
     $base = new Fieldmanager_Group(array('name' => 'base_group', 'serialize_data' => false, 'children' => array('test_text' => new Fieldmanager_TextField(), 'test_group' => new Fieldmanager_Group(array('serialize_data' => false, 'children' => array('deep_text' => new Fieldmanager_TextArea()))))));
     $data = array('test_text' => rand_str(), 'test_group' => array('deep_text' => rand_str()));
     $html = $this->_get_html_for($base, $data);
     $this->assertEquals($data['test_text'], fm_get_term_meta($this->term_id, $this->taxonomy, 'base_group_test_text', true));
     $this->assertEquals($data['test_group']['deep_text'], fm_get_term_meta($this->term_id, $this->taxonomy, 'base_group_test_group_deep_text', true));
     $this->assertContains('name="base_group[test_text]"', $html);
     $this->assertContains('value="' . $data['test_text'] . '"', $html);
     $this->assertContains('name="base_group[test_group][deep_text]"', $html);
     $this->assertContains('>' . $data['test_group']['deep_text'] . '</textarea>', $html);
 }
 /**
  * Migrate all FM term meta to core term meta
  *
  * ## OPTIONS
  *
  * [--destructive]
  * : If present, FM term meta will be deleted after it is migrated, and
  * each FM term meta post will be deleted once its meta is migrated.
  *
  * [--dry-run]
  * : If present, no updates will be made.
  *
  * [--verbose]
  * : If present, script will output additional details.
  *
  * ## EXAMPLES
  *
  *     wp fm-term-meta migrate_term_meta
  *
  * @synopsis [--destructive] [--dry-run] [--verbose]
  */
 public function migrate_term_meta($args, $assoc_args)
 {
     $dry_run = !empty($assoc_args['dry-run']);
     $verbose = !empty($assoc_args['verbose']);
     $destructive = !empty($assoc_args['destructive']);
     WP_CLI::line("Starting term meta migration");
     if ($dry_run) {
         WP_CLI::warning('THIS IS A DRY RUN');
     } elseif ($destructive) {
         WP_CLI::warning('With the --destructive flag set, this will delete all FM term meta after it is successfully migrated. There is no undo for this.');
         WP_CLI::confirm('Do you want to continue?');
     }
     if (get_option('db_version') < 34370) {
         WP_CLI::error('This WordPress installation is not ready for term meta! You must be running WordPress 4.4 and the database update must be complete.');
     }
     WP_CLI::warning("Muting user-generated PHP notices for this command in order to hide deprecation notices");
     error_reporting(error_reporting() & ~E_USER_NOTICE);
     $terms = $this->get_terms_with_fm_term_meta();
     foreach ($terms as $term) {
         if ($verbose) {
             WP_CLI::line("Processing {$term->taxonomy} `{$term->name}' ({$term->slug}, {$term->term_id})");
         }
         $term_meta = fm_get_term_meta($term->term_id, $term->taxonomy);
         if ($verbose) {
             WP_CLI::line(sprintf("\tFound %d meta entries", count($term_meta)));
         }
         foreach ($term_meta as $meta_key => $meta_values) {
             if ($verbose) {
                 WP_CLI::line(sprintf("\tMigrating %d meta values for meta key %s", count($meta_values), $meta_key));
             }
             $result = true;
             foreach ($meta_values as $meta_value) {
                 if ($dry_run || $verbose) {
                     WP_CLI::line(sprintf("\tadd_term_meta( %d, '%s', '%s' );", $term->term_id, $meta_key, strlen($meta_value) < 50 ? $meta_value : '[too long to output]'));
                 }
                 if (!$dry_run) {
                     $this_result = add_term_meta($term->term_id, $meta_key, $meta_value);
                     if (!is_int($this_result)) {
                         $result = false;
                         WP_CLI::warning(sprintf("\tError running add_term_meta( %d, '%s', '%s' );", $term->term_id, $meta_key, $meta_value));
                         if (is_wp_error($this_result)) {
                             WP_CLI::warning(sprintf("\t\t%s: %s", $this_result->get_error_code(), $this_result->get_error_message()));
                         } else {
                             WP_CLI::warning(sprintf("\t\t%s", var_export($this_result, 1)));
                         }
                     }
                 }
             }
             if ($destructive) {
                 if (!$result) {
                     WP_CLI::warning("\tSkipping FM term meta deletion for {$meta_key} because an error was encountered while adding data");
                 } else {
                     if ($dry_run || $verbose) {
                         WP_CLI::line("\tDeleting this term's FM term meta for {$meta_key}");
                     }
                     if (!$dry_run) {
                         fm_delete_term_meta($term->term_id, $term->taxonomy, $meta_key);
                     }
                 }
             }
         }
         if (empty($term_meta)) {
             WP_CLI::line("\tNo FM term meta remaining for this term.");
             if ($destructive && get_post($term->post_id)) {
                 if ($verbose || $dry_run) {
                     WP_CLI::line("\tDeleting post ID {$term->post_id}");
                 }
                 if (!$dry_run) {
                     wp_delete_post($term->post_id, true);
                 }
             }
         }
     }
     // Print a success message
     WP_CLI::success("Process complete!");
     if (!$dry_run) {
         WP_CLI::line("\n");
         WP_CLI::line("You're almost done! To use the new term meta, you need to update Fieldmanager, then update your code accordingly:");
         WP_CLI::line("- Replace any call to Fieldmanager_Field::add_term_form() with Fieldmanager_Field::add_term_meta_box().");
         WP_CLI::line("- You need to update the arguments anywhere you're instantiating Fieldmanager_Context_Term directly.");
         WP_CLI::line("See https://github.com/alleyinteractive/wordpress-fieldmanager/issues/400 for details.");
         WP_CLI::line("Happy coding!");
         WP_CLI::line("\n");
     }
 }