/**
  * Test behavior when using the term meta fields.
  */
 public function test_save_term_meta()
 {
     $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);
     $data = fm_get_term_meta($this->term->term_id, $this->term->taxonomy, 'term_option', false);
     $this->assertEquals(array($text), $data);
     // check update and fetch
     $text_updated = rand_str();
     $this->save_values($term_option, $this->term, $text_updated);
     $data = fm_get_term_meta($this->term->term_id, $this->term->taxonomy, 'term_option', true);
     $this->assertEquals($text_updated, $data);
     $this->assertInternalType('int', Fieldmanager_Util_Term_Meta()->get_term_meta_post_id($this->term->term_id, $this->term->taxonomy));
     $cache_key = Fieldmanager_Util_Term_Meta()->get_term_meta_post_id_cache_key($this->term->term_id, $this->term->taxonomy);
     $this->assertNotEquals(false, wp_cache_get($cache_key));
     fm_delete_term_meta($this->term->term_id, $this->term->taxonomy, 'term_option');
     // post id not cached right after removal of only meta value, which results in deletion of the post
     $this->assertEquals(false, wp_cache_get($cache_key));
     // checking that the post id is reported as false when it doesn't exist now
     $this->assertEquals(false, Fieldmanager_Util_Term_Meta()->get_term_meta_post_id($this->term->term_id, $this->term->taxonomy));
     // checking that the post id is cached now to return false since it doesn't exist
     $this->assertNotEquals(false, wp_cache_get($cache_key));
 }
 public function tearDown()
 {
     $meta = fm_get_term_meta($this->term_id, $this->taxonomy);
     foreach ($meta as $key => $value) {
         fm_delete_term_meta($this->term_id, $this->taxonomy, $key);
     }
     if (get_current_user_id() != $this->current_user) {
         wp_delete_user(get_current_user_id());
     }
     wp_set_current_user($this->current_user);
 }
 /**
  * Callback to delete term meta for the given term ID and current taxonomy.
  *
  * @see delete_term_meta().
  * @see Fieldmanager_Util_Term_Meta::delete_term_meta() (Deprecated).
  */
 protected function delete_data($term_id, $meta_key, $meta_value = '')
 {
     if ($this->use_fm_meta) {
         return fm_delete_term_meta($term_id, $this->current_taxonomy, $meta_key, $meta_value);
     } else {
         return delete_term_meta($term_id, $meta_key, $meta_value);
     }
 }
 /**
  * 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");
     }
 }