delete_options = $delete_options; $this->hooks(); } function hooks(){ add_action('init',array($this, 'migrate_data_maybe') ); if ( $this->delete_options ){ add_action('shutdown',array( $this, 'delete_options') ); } } function migrate_data_maybe(){ //needs migration? $do_migration = get_option('tax_meta_migrated', 'do_migration'); if ( 'do_migration' == $do_migration ){ $this->do_migration(); update_option( 'tax_meta_migrated', 'done' ); } } function do_migration(){ $options = $this->get_options(); foreach ($options as $option) { $term_id = $this->term_id_from_option( $option ); $meta = get_option( $option ); $this->tax_to_term( $term_id, $meta ); if( $this->delete_options ){ delete_option( $option ); } } } function tax_to_term( $term_id, $meta ){ foreach ((array)$meta as $meta_name => $meta_value) { update_term_meta( $term_id, $meta_name, $meta_value ); } } function term_id_from_option( $option ){ return str_replace('tax_meta_', '', $option ); } function get_options(){ global $wpdb; $query = "SELECT option_name FROM $wpdb->options WHERE option_name LIKE 'tax_meta_%' "; $options = $wpdb->get_col( $query ); return $options; } function delete_options(){ $options = $this->get_options(); foreach ($options as $option) { delete_option( $option ); } } } /* * meta functions for easy access using term meta api */ //get term meta field if (!function_exists('get_tax_meta')){ function get_tax_meta($term_id,$key,$multi = false){ $term_id = (is_object($term_id))? $term_id->term_id: $term_id; return get_term_meta( $term_id, $key, $multi ); } } //delete meta if (!function_exists('delete_tax_meta')){ function delete_tax_meta($term_id,$key){ $term_id = (is_object($term_id))? $term_id->term_id: $term_id; return delete_term_meta( $term_id, $key ); } } //update meta if (!function_exists('update_tax_meta')){ function update_tax_meta($term_id,$key,$value){ $term_id = (is_object($term_id))? $term_id->term_id: $term_id; return update_term_meta( $term_id, $key, $value ); } } //get term meta field and strip slashes if (!function_exists('get_tax_meta_strip')){ function get_tax_meta_strip($term_id,$key,$multi = false){ return stripslashes( get_term_meta( $term_id, $key, $multi ) ); } } //get all meta fields of a term if (!function_exists('get_tax_meta_all')){ function get_tax_meta_all( $term_id){ $term_id = (is_object($term_id))? $term_id->term_id: $term_id; return get_term_meta( $term_id ); } }