Plugin Directory

Changeset 165158


Ignore:
Timestamp:
10/20/2009 03:19:11 PM (16 years ago)
Author:
elebail
Message:

Major release

Location:
wp-category-meta/trunk
Files:
7 added
4 deleted
2 edited

Legend:

Unmodified
Added
Removed
  • wp-category-meta/trunk/readme.txt

    r117936 r165158  
    44Tags: category,meta,category meta,admin,plugin
    55Requires at least: 2.6
    6 Tested up to: 2.6.5
     6Tested up to: 2.8.4
    77Stable tag: trunk
    88
    9 Add the ability to attach meta to the wordpress categories.
     9Add the ability to attach meta to the wordperess categories.
    1010
    1111== Description ==
    1212
    1313This plugins add meta to the wordpress categories.
     14It Creates a wp-termsmeta table to store the entered meta.
     15It adds input fields to the category administration interface to enter the meta values.
     16It provides functions to retrive / create / update / delte the category meta.
    1417
    15 It Creates a wp-termsmeta table to store the entered meta.
    16 
    17 It adds input fields to the category administration interface to enter the meta values.
    18 
    19 It provides functions to retrive / create / update / delete the category meta.
     18This plugin has been tested with WP2.6.5, WP2.8.4 and WPmu2.8.4
    2019
    2120== Installation ==
     
    23221. Unzip into your `/wp-content/plugins/` directory. If you're uploading it make sure to upload
    2423the top-level folder. Don't just upload all the php files and put them in `/wp-content/plugins/`.
    25 
    26 2. Edit the wp-category-meta.php file to adapt the lis of meta you wants (line 38).
    27 
    28 3. Activate the plugin through the 'Plugins' menu in WordPress
    29 
    30 4. go to your Administration interface in, the "category" menu -> new fields are displayed in the category creation/modification form.
    31 
     242. Activate the plugin through the 'Plugins' menu in WordPress
     253. Go to your Administration interface in the "Settings" menu a new "Category Meta" page is created.
     26Configure the meta you want to use.
     274. go to your Administration interface, in the "Category" menu -> new fields are displayed in the category creation/modification form with the meta you configured.
    32285. That's it!
    33 
    34296. you can use the folowing functions into your templates to retreive 1 meta:
    35 `<?php if (function_exists('get_terms_meta'))
    36 { $metaValue = get_terms_meta($category_id, $meta_key); }
    37 ?>`
    38 
    39 7. you can use the folowing functions into your templates to retreive all meta:
    40 `<?php if (function_exists('get_all_terms_meta'))
    41 { $metaList = get_all_terms_meta($category_id); }
    42 ?>`
     30<?php
     31if (function_exists('get_terms_meta'))
     32{
     33    $metaValue = get_terms_meta($category_id, $meta_key);
     34}
     35?>
     366. you can use the folowing functions into your templates to retreive all meta:
     37<?php
     38if (function_exists('get_all_terms_meta'))
     39{
     40    $metaList = get_all_terms_meta($category_id);
     41}
     42?>
    4343
    4444== Frequently Asked Questions ==
    4545
    46 I use this plugin into my own blog sucessfuly.
    47 If you have any problem / corrections let me now
    48 (do not forget to give me the WordPress version You have).
     46This plugin is in final debuging phase.
     47
     48== Screenshots ==
     49
     50No screenshot at the moment.
     51
     52
  • wp-category-meta/trunk/wp-category-meta.php

    r117917 r165158  
    44 * Plugin URI: #
    55 * Description: Add the ability to attach meta to the Wordpress categories
    6  * Version: 0.0.1
     6 * Version: 1.0.0
    77 * Author: Eric Le Bail
    88 * Author URI: #
     
    2828 *
    2929 */
    30 // Please configure the meta you want to display here:
    31 // you can confgure a list of meta with
    32 // 'meta name' => 'meta type'
    33 // type 'text' display a single line text input.
    34 // type 'textarea' display a multi line texte area input.
    35 // Any other type will be ingnored.
    36 $metaList = array(
    37     'keyword' => 'text',
    38     'description' => 'text',
    39     'long-description' => 'textarea'
    40     );
    41 
    42     // Initialization and Hooks
    43     global $wpdb;
    44     global $wptm_version;
     30
     31if ( !defined('WP_CONTENT_DIR') )
     32define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
     33
     34if (!defined('DIRECTORY_SEPARATOR'))
     35{
     36    if (strpos(php_uname('s'), 'Win') !== false )
     37    define('DIRECTORY_SEPARATOR', '\\');
     38    else
     39    define('DIRECTORY_SEPARATOR', '/');
     40}
     41$pluginPath = ABSPATH.PLUGINDIR.DIRECTORY_SEPARATOR."wp-category-meta";
     42define('WPTM_PATH', $pluginPath);
     43$filePath = $pluginPath.DIRECTORY_SEPARATOR.basename(__FILE__);
     44$asolutePath = dirname(__FILE__).DIRECTORY_SEPARATOR;
     45define('WPTM_ABSPATH', $asolutePath);
     46
     47// Initialization and Hooks
     48global $wpdb;
     49global $wp_version;
     50global $wptm_version;
     51global $wptm_db_version;
     52global $wptm_table_name;
     53global $wp_version;
     54$wptm_version = '1.0.0';
     55$wptm_db_version = '0.0.1';
     56$wptm_table_name = $wpdb->prefix.'termsmeta';
     57
     58register_activation_hook($filePath,'wptm_install');
     59register_deactivation_hook($filePath,'wptm_uninstall');
     60
     61// Actions
     62add_action('init', 'wptm_init');
     63add_action('create_category', 'wptm_save_meta_tags');
     64add_action('edit_category', 'wptm_save_meta_tags');
     65add_action('delete_category', 'wptm_delete_meta_tags');
     66add_action('edit_category_form', 'wptm_add_meta_textinput');
     67
     68if (is_admin()) {
     69    include ( WPTM_ABSPATH  . 'views'.DIRECTORY_SEPARATOR.'options.php' );
     70    $WPTMAdmin = new wptm_admin();
     71}
     72
     73/**
     74 * Function called when installing or updgrading the plugin.
     75 * @return void.
     76 */
     77function wptm_install()
     78{
     79    global $wpdb;
     80    global $wptm_table_name;
    4581    global $wptm_db_version;
    46     global $wptm_table_name;
    47     $wptm_version = '0.0.1';
    48     $wptm_db_version = '0.0.1';
    49     $wptm_table_name = $wpdb->prefix.'termsmeta';
    50 
    51     register_activation_hook(__FILE__,'wptm_install');
    52     register_deactivation_hook(__FILE__,'wptm_uninstall');
    53 
    54     // Actions
    55     add_action('init', 'wptm_init');
    56     add_action('create_category', 'wptm_save_meta_tags');
    57     add_action('edit_category', 'wptm_save_meta_tags');
    58     add_action('delete_category', 'wptm_delete_meta_tags');
    59     add_action('edit_category_form', 'wptm_add_meta_textinput');
    60 
    61     /**
    62      * Function called when installing or updgrading the plugin.
    63      * @return void.
    64      */
    65     function wptm_install()
    66     {
    67         global $wpdb;
    68         global $wptm_table_name;
    69         global $wptm_db_version;
    70 
    71         // create table on first install
    72         if($wpdb->get_var("show tables like '$wptm_table_name'") != $wptm_table_name) {
    73 
    74             wptm_createTable($wpdb, $wptm_table_name);
    75             add_option("wptm_db_version", $wptm_db_version);
    76         }
    77 
    78         // On plugin update only the version nulmber is updated.
    79         $installed_ver = get_option( "wptm_db_version" );
    80         if( $installed_ver != $wptm_db_version ) {
    81 
    82             update_option( "wptm_db_version", $wptm_db_version );
    83         }
    84 
    85     }
    86 
    87     /**
    88      * Function called when un-installing the plugin.
    89      * @return void.
    90      */
    91     function wptm_uninstall()
    92     {
    93         global $wpdb;
    94         global $wptm_table_name;
    95 
    96         // delete table
    97         if($wpdb->get_var("show tables like '$wptm_table_name'") == $wptm_table_name) {
    98 
    99             wptm_dropTable($wpdb, $wptm_table_name);
    100         }
    101         delete_option("wptm_db_version");
    102     }
    103 
    104     /**
    105      * Function that creates the wptm table.
    106      *
    107      * @param $wpdb : database manipulation object.
    108      * @param $table_name : name of the table to create.
    109      * @return void.
    110      */
    111     function wptm_createTable($wpdb, $table_name)
    112     {
    113         $sql = "CREATE TABLE  ".$table_name." (
     82
     83    // create table on first install
     84    if($wpdb->get_var("show tables like '$wptm_table_name'") != $wptm_table_name) {
     85
     86        wptm_createTable($wpdb, $wptm_table_name);
     87        add_option("wptm_db_version", $wptm_db_version);
     88        add_option("wptm_configuration", '');
     89    }
     90
     91    // On plugin update only the version nulmber is updated.
     92    $installed_ver = get_option( "wptm_db_version" );
     93    if( $installed_ver != $wptm_db_version ) {
     94
     95        update_option( "wptm_db_version", $wptm_db_version );
     96    }
     97
     98}
     99
     100/**
     101 * Function called when un-installing the plugin.
     102 * @return void.
     103 */
     104function wptm_uninstall()
     105{
     106    global $wpdb;
     107    global $wptm_table_name;
     108
     109    // delete table
     110    if($wpdb->get_var("show tables like '$wptm_table_name'") == $wptm_table_name) {
     111
     112        wptm_dropTable($wpdb, $wptm_table_name);
     113    }
     114    delete_option("wptm_db_version");
     115    delete_option("wptm_configuration");
     116}
     117
     118/**
     119 * Function that creates the wptm table.
     120 *
     121 * @param $wpdb : database manipulation object.
     122 * @param $table_name : name of the table to create.
     123 * @return void.
     124 */
     125function wptm_createTable($wpdb, $table_name)
     126{
     127    $sql = "CREATE TABLE  ".$table_name." (
    114128          meta_id bigint(20) NOT NULL auto_increment,
    115129          terms_id bigint(20) NOT NULL default '0',
     
    121135        ) ENGINE=MyISAM AUTO_INCREMENT=6887 DEFAULT CHARSET=utf8;";
    122136
    123         $results = $wpdb->query($sql);
    124     }
    125 
    126     /**
    127      * Function that drops the plugin table.
    128      *
    129      * @param $wpdb : database manipulation object.
    130      * @param $table_name : name of the table to create.
    131      * @return void.
    132      */
    133     function wptm_dropTable($wpdb, $table_name)
     137    $results = $wpdb->query($sql);
     138}
     139
     140/**
     141 * Function that drops the plugin table.
     142 *
     143 * @param $wpdb : database manipulation object.
     144 * @param $table_name : name of the table to create.
     145 * @return void.
     146 */
     147function wptm_dropTable($wpdb, $table_name)
     148{
     149    $sql = "DROP TABLE  ".$table_name." ;";
     150
     151    $results = $wpdb->query($sql);
     152}
     153
     154/**
     155 * Function that initialise the plugin.
     156 * It loads the translation files.
     157 *
     158 * @return void.
     159 */
     160function wptm_init() {
     161    if (function_exists('load_plugin_textdomain')) {
     162        load_plugin_textdomain('wp-category-meta', PLUGINDIR.DIRECTORY_SEPARATOR."wp-category-meta".DIRECTORY_SEPARATOR.'lang');
     163    }
     164    else
    134165    {
    135         $sql = "DROP TABLE  ".$table_name." ;";
    136 
    137         $results = $wpdb->query($sql);
    138     }
    139 
    140     /**
    141      * Function that initialise the plugin.
    142      * It loads the translation files.
    143      *
    144      * @return void.
    145      */
    146     function wptm_init() {
    147         if (function_exists('load_plugin_textdomain')) {
    148             load_plugin_textdomain('wp-category-meta', 'wp-content/plugins/wp-category-meta');
     166        // Load language file
     167        $locale = get_locale();
     168        if ( !empty($locale) )
     169        load_textdomain('wp-category-meta', WPTM_ABSPATH.'lang'.DIRECTORY_SEPARATOR.'wp-category-meta-'.$locale.'.mo');
     170    }
     171}
     172
     173/**
     174 * add_terms_meta() - adds metadata for terms
     175 *
     176 *
     177 * @param int $terms_id terms (category/tag...) ID
     178 * @param string $key The meta key to add
     179 * @param mixed $value The meta value to add
     180 * @param bool $unique whether to check for a value with the same key
     181 * @return bool
     182 */
     183function add_terms_meta($terms_id, $meta_key, $meta_value, $unique = false) {
     184
     185    global $wpdb;
     186    global $wptm_table_name;
     187
     188    // expected_slashed ($meta_key)
     189    $meta_key = stripslashes( $meta_key );
     190    $meta_value = stripslashes( $meta_value );
     191
     192    if ( $unique && $wpdb->get_var( $wpdb->prepare( "SELECT meta_key FROM $wptm_table_name WHERE meta_key = %s AND terms_id = %d", $meta_key, $terms_id ) ) )
     193    return false;
     194
     195    $meta_value = maybe_serialize($meta_value);
     196
     197    $wpdb->insert( $wptm_table_name, compact( 'terms_id', 'meta_key', 'meta_value' ) );
     198
     199    wp_cache_delete($terms_id, 'terms_meta');
     200
     201    return true;
     202}
     203
     204/**
     205 * delete_terms_meta() - delete terms metadata
     206 *
     207 *
     208 * @param int $terms_id terms (category/tag...) ID
     209 * @param string $key The meta key to delete
     210 * @param mixed $value
     211 * @return bool
     212 */
     213function delete_terms_meta($terms_id, $key, $value = '') {
     214
     215    global $wpdb;
     216    global $wptm_table_name;
     217
     218    // expected_slashed ($key, $value)
     219    $key = stripslashes( $key );
     220    $value = stripslashes( $value );
     221
     222    if ( empty( $value ) )
     223    {
     224        $sql1 = $wpdb->prepare( "SELECT meta_id FROM $wptm_table_name WHERE terms_id = %d AND meta_key = %s", $terms_id, $key );
     225        $meta_id = $wpdb->get_var( $sql1 );
     226    } else {
     227        $sql2 = $wpdb->prepare( "SELECT meta_id FROM $wptm_table_name WHERE terms_id = %d AND meta_key = %s AND meta_value = %s", $terms_id, $key, $value );
     228        $meta_id = $wpdb->get_var( $sql2 );
     229    }
     230
     231    if ( !$meta_id )
     232    return false;
     233
     234    if ( empty( $value ) )
     235    $wpdb->query( $wpdb->prepare( "DELETE FROM $wptm_table_name WHERE terms_id = %d AND meta_key = %s", $terms_id, $key ) );
     236    else
     237    $wpdb->query( $wpdb->prepare( "DELETE FROM $wptm_table_name WHERE terms_id = %d AND meta_key = %s AND meta_value = %s", $terms_id, $key, $value ) );
     238
     239    wp_cache_delete($terms_id, 'terms_meta');
     240
     241    return true;
     242}
     243
     244/**
     245 * get_terms_meta() - Get a terms meta field
     246 *
     247 *
     248 * @param int $terms_id terms (category/tag...) ID
     249 * @param string $key The meta key to retrieve
     250 * @param bool $single Whether to return a single value
     251 * @return mixed The meta value or meta value list
     252 */
     253function get_terms_meta($terms_id, $key, $single = false) {
     254
     255    $terms_id = (int) $terms_id;
     256
     257    $meta_cache = wp_cache_get($terms_id, 'terms_meta');
     258
     259    if ( !$meta_cache ) {
     260        update_termsmeta_cache($terms_id);
     261        $meta_cache = wp_cache_get($terms_id, 'terms_meta');
     262    }
     263
     264    if ( isset($meta_cache[$key]) ) {
     265        if ( $single ) {
     266            return maybe_unserialize( $meta_cache[$key][0] );
     267        } else {
     268            return array_map('maybe_unserialize', $meta_cache[$key]);
    149269        }
    150270    }
    151271
    152     /**
    153      * add_terms_meta() - adds metadata for terms
    154      *
    155      *
    156      * @param int $terms_id terms (category/tag...) ID
    157      * @param string $key The meta key to add
    158      * @param mixed $value The meta value to add
    159      * @param bool $unique whether to check for a value with the same key
    160      * @return bool
    161      */
    162     function add_terms_meta($terms_id, $meta_key, $meta_value, $unique = false) {
    163 
    164         global $wpdb;
    165         global $wptm_table_name;
    166 
    167         // expected_slashed ($meta_key)
    168         $meta_key = stripslashes( $meta_key );
    169         $meta_value = stripslashes( $meta_value );
    170 
    171         if ( $unique && $wpdb->get_var( $wpdb->prepare( "SELECT meta_key FROM $wptm_table_name WHERE meta_key = %s AND terms_id = %d", $meta_key, $terms_id ) ) )
    172         return false;
    173 
    174         $meta_value = maybe_serialize($meta_value);
    175 
    176         $wpdb->insert( $wptm_table_name, compact( 'terms_id', 'meta_key', 'meta_value' ) );
    177 
    178         wp_cache_delete($terms_id, 'terms_meta');
    179 
    180         return true;
    181     }
    182 
    183     /**
    184      * delete_terms_meta() - delete terms metadata
    185      *
    186      *
    187      * @param int $terms_id terms (category/tag...) ID
    188      * @param string $key The meta key to delete
    189      * @param mixed $value
    190      * @return bool
    191      */
    192     function delete_terms_meta($terms_id, $key, $value = '') {
    193 
    194         global $wpdb;
    195         global $wptm_table_name;
    196 
    197         // expected_slashed ($key, $value)
    198         $key = stripslashes( $key );
    199         $value = stripslashes( $value );
    200 
    201         if ( empty( $value ) )
     272    return '';
     273}
     274
     275/**
     276 * get_all_terms_meta() - Get all meta fields for a terms (category/tag...)
     277 *
     278 *
     279 * @param int $terms_id terms (category/tag...) ID
     280 * @return array The meta (key => value) list
     281 */
     282function get_all_terms_meta($terms_id) {
     283
     284    $terms_id = (int) $terms_id;
     285
     286    $meta_cache = wp_cache_get($terms_id, 'terms_meta');
     287
     288    if ( !$meta_cache ) {
     289        update_termsmeta_cache($terms_id);
     290        $meta_cache = wp_cache_get($terms_id, 'terms_meta');
     291    }
     292
     293    return maybe_unserialize( $meta_cache );
     294
     295}
     296
     297/**
     298 * update_terms_meta() - Update a terms meta field
     299 *
     300 *
     301 * @param int $terms_id terms (category/tag...) ID
     302 * @param string $key The meta key to update
     303 * @param mixed $value The meta value to update
     304 * @param mixed $prev_value previous value (for differentiating between meta fields with the same key and terms ID)
     305 * @return bool
     306 */
     307function update_terms_meta($terms_id, $meta_key, $meta_value, $prev_value = '') {
     308
     309    global $wpdb;
     310    global $wptm_table_name;
     311
     312    // expected_slashed ($meta_key)
     313    $meta_key = stripslashes( $meta_key );
     314    $meta_value = stripslashes( $meta_value );
     315
     316    if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT meta_key FROM $wptm_table_name WHERE meta_key = %s AND terms_id = %d", $meta_key, $terms_id ) ) ) {
     317        return add_post_meta($terms_id, $meta_key, $meta_value);
     318    }
     319
     320    $meta_value = maybe_serialize($meta_value);
     321
     322    $data  = compact( 'meta_value' );
     323    $where = compact( 'meta_key', 'terms_id' );
     324
     325    if ( !empty( $prev_value ) ) {
     326        $prev_value = maybe_serialize($prev_value);
     327        $where['meta_value'] = $prev_value;
     328    }
     329
     330    $wpdb->update( $wptm_table_name, $data, $where );
     331    wp_cache_delete($terms_id, 'terms_meta');
     332    return true;
     333}
     334
     335/**
     336 * update_termsmeta_cache()
     337 *
     338 *
     339 * @uses $wpdb
     340 *
     341 * @param array $category_ids
     342 * @return bool|array Returns false if there is nothing to update or an array of metadata
     343 */
     344function update_termsmeta_cache($terms_ids) {
     345
     346    global $wpdb;
     347    global $wptm_table_name;
     348
     349    if ( empty( $terms_ids ) )
     350    return false;
     351
     352    if ( !is_array($terms_ids) ) {
     353        $terms_ids = preg_replace('|[^0-9,]|', '', $terms_ids);
     354        $terms_ids = explode(',', $terms_ids);
     355    }
     356
     357    $terms_ids = array_map('intval', $terms_ids);
     358
     359    $ids = array();
     360    foreach ( (array) $terms_ids as $id ) {
     361        if ( false === wp_cache_get($id, 'terms_meta') )
     362        $ids[] = $id;
     363    }
     364
     365    if ( empty( $ids ) )
     366    return false;
     367
     368    // Get terms-meta info
     369    $id_list = join(',', $ids);
     370    $cache = array();
     371    if ( $meta_list = $wpdb->get_results("SELECT terms_id, meta_key, meta_value FROM $wptm_table_name WHERE terms_id IN ($id_list) ORDER BY terms_id, meta_key", ARRAY_A) ) {
     372        foreach ( (array) $meta_list as $metarow) {
     373            $mpid = (int) $metarow['terms_id'];
     374            $mkey = $metarow['meta_key'];
     375            $mval = $metarow['meta_value'];
     376
     377            // Force subkeys to be array type:
     378            if ( !isset($cache[$mpid]) || !is_array($cache[$mpid]) )
     379            $cache[$mpid] = array();
     380            if ( !isset($cache[$mpid][$mkey]) || !is_array($cache[$mpid][$mkey]) )
     381            $cache[$mpid][$mkey] = array();
     382
     383            // Add a value to the current pid/key:
     384            $cache[$mpid][$mkey][] = $mval;
     385        }
     386    }
     387
     388    foreach ( (array) $ids as $id ) {
     389        if ( ! isset($cache[$id]) )
     390        $cache[$id] = array();
     391    }
     392
     393    foreach ( array_keys($cache) as $terms)
     394    wp_cache_set($terms, $cache[$terms], 'terms_meta');
     395
     396    return $cache;
     397}
     398
     399/**
     400 * Function that saves the meta from form.
     401 *
     402 * @param $id : terms (category) ID
     403 * @return void;
     404 */
     405function wptm_save_meta_tags($id) {
     406
     407    $metaList = get_option("wptm_configuration");
     408    // Check that the meta form is posted
     409    $wptm_edit = $_POST["wptm_edit"];
     410    if (isset($wptm_edit) && !empty($wptm_edit)) {
     411
     412        foreach($metaList as $inputName => $inputType)
    202413        {
    203             $sql1 = $wpdb->prepare( "SELECT meta_id FROM $wptm_table_name WHERE terms_id = %d AND meta_key = %s", $terms_id, $key );
    204             $meta_id = $wpdb->get_var( $sql1 );
    205         } else {
    206             $sql2 = $wpdb->prepare( "SELECT meta_id FROM $wptm_table_name WHERE terms_id = %d AND meta_key = %s AND meta_value = %s", $terms_id, $key, $value );
    207             $meta_id = $wpdb->get_var( $sql2 );
    208         }
    209 
    210         if ( !$meta_id )
    211         return false;
    212 
    213         if ( empty( $value ) )
    214         $wpdb->query( $wpdb->prepare( "DELETE FROM $wptm_table_name WHERE terms_id = %d AND meta_key = %s", $terms_id, $key ) );
    215         else
    216         $wpdb->query( $wpdb->prepare( "DELETE FROM $wptm_table_name WHERE terms_id = %d AND meta_key = %s AND meta_value = %s", $terms_id, $key, $value ) );
    217 
    218         wp_cache_delete($terms_id, 'terms_meta');
    219 
    220         return true;
    221     }
    222 
    223     /**
    224      * get_terms_meta() - Get a terms meta field
    225      *
    226      *
    227      * @param int $terms_id terms (category/tag...) ID
    228      * @param string $key The meta key to retrieve
    229      * @param bool $single Whether to return a single value
    230      * @return mixed The meta value or meta value list
    231      */
    232     function get_terms_meta($terms_id, $key, $single = false) {
    233 
    234         $terms_id = (int) $terms_id;
    235 
    236         $meta_cache = wp_cache_get($terms_id, 'terms_meta');
    237 
    238         if ( !$meta_cache ) {
    239             update_termsmeta_cache($terms_id);
    240             $meta_cache = wp_cache_get($terms_id, 'terms_meta');
    241         }
    242 
    243         if ( isset($meta_cache[$key]) ) {
    244             if ( $single ) {
    245                 return maybe_unserialize( $meta_cache[$key][0] );
    246             } else {
    247                 return array_map('maybe_unserialize', $meta_cache[$key]);
     414            $inputValue = $_POST['wptm_'.$inputName];
     415            delete_terms_meta($id, $inputName);
     416            if (isset($inputValue) && !empty($inputValue)) {
     417                add_terms_meta($id, $inputName, $inputValue);
    248418            }
    249419        }
    250 
    251         return '';
    252     }
    253 
    254     /**
    255      * get_all_terms_meta() - Get all meta fields for a terms (category/tag...)
    256      *
    257      *
    258      * @param int $terms_id terms (category/tag...) ID
    259      * @return array The meta (key => value) list
    260      */
    261     function get_all_terms_meta($terms_id) {
    262 
    263         $terms_id = (int) $terms_id;
    264 
    265         $meta_cache = wp_cache_get($terms_id, 'terms_meta');
    266 
    267         if ( !$meta_cache ) {
    268             update_termsmeta_cache($terms_id);
    269             $meta_cache = wp_cache_get($terms_id, 'terms_meta');
    270         }
    271 
    272         return maybe_unserialize( $meta_cache );
    273 
    274     }
    275 
    276     /**
    277      * update_terms_meta() - Update a terms meta field
    278      *
    279      *
    280      * @param int $terms_id terms (category/tag...) ID
    281      * @param string $key The meta key to update
    282      * @param mixed $value The meta value to update
    283      * @param mixed $prev_value previous value (for differentiating between meta fields with the same key and terms ID)
    284      * @return bool
    285      */
    286     function update_terms_meta($terms_id, $meta_key, $meta_value, $prev_value = '') {
    287 
    288         global $wpdb;
    289         global $wptm_table_name;
    290 
    291         // expected_slashed ($meta_key)
    292         $meta_key = stripslashes( $meta_key );
    293         $meta_value = stripslashes( $meta_value );
    294 
    295         if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT meta_key FROM $wptm_table_name WHERE meta_key = %s AND terms_id = %d", $meta_key, $terms_id ) ) ) {
    296             return add_post_meta($terms_id, $meta_key, $meta_value);
    297         }
    298 
    299         $meta_value = maybe_serialize($meta_value);
    300 
    301         $data  = compact( 'meta_value' );
    302         $where = compact( 'meta_key', 'terms_id' );
    303 
    304         if ( !empty( $prev_value ) ) {
    305             $prev_value = maybe_serialize($prev_value);
    306             $where['meta_value'] = $prev_value;
    307         }
    308 
    309         $wpdb->update( $wptm_table_name, $data, $where );
    310         wp_cache_delete($terms_id, 'terms_meta');
    311         return true;
    312     }
    313 
    314     /**
    315      * update_termsmeta_cache()
    316      *
    317      *
    318      * @uses $wpdb
    319      *
    320      * @param array $category_ids
    321      * @return bool|array Returns false if there is nothing to update or an array of metadata
    322      */
    323     function update_termsmeta_cache($terms_ids) {
    324 
    325         global $wpdb;
    326         global $wptm_table_name;
    327 
    328         if ( empty( $terms_ids ) )
    329         return false;
    330 
    331         if ( !is_array($terms_ids) ) {
    332             $terms_ids = preg_replace('|[^0-9,]|', '', $terms_ids);
    333             $terms_ids = explode(',', $terms_ids);
    334         }
    335 
    336         $terms_ids = array_map('intval', $terms_ids);
    337 
    338         $ids = array();
    339         foreach ( (array) $terms_ids as $id ) {
    340             if ( false === wp_cache_get($id, 'terms_meta') )
    341             $ids[] = $id;
    342         }
    343 
    344         if ( empty( $ids ) )
    345         return false;
    346 
    347         // Get terms-meta info
    348         $id_list = join(',', $ids);
    349         $cache = array();
    350         if ( $meta_list = $wpdb->get_results("SELECT terms_id, meta_key, meta_value FROM $wptm_table_name WHERE terms_id IN ($id_list) ORDER BY terms_id, meta_key", ARRAY_A) ) {
    351             foreach ( (array) $meta_list as $metarow) {
    352                 $mpid = (int) $metarow['terms_id'];
    353                 $mkey = $metarow['meta_key'];
    354                 $mval = $metarow['meta_value'];
    355 
    356                 // Force subkeys to be array type:
    357                 if ( !isset($cache[$mpid]) || !is_array($cache[$mpid]) )
    358                 $cache[$mpid] = array();
    359                 if ( !isset($cache[$mpid][$mkey]) || !is_array($cache[$mpid][$mkey]) )
    360                 $cache[$mpid][$mkey] = array();
    361 
    362                 // Add a value to the current pid/key:
    363                 $cache[$mpid][$mkey][] = $mval;
    364             }
    365         }
    366 
    367         foreach ( (array) $ids as $id ) {
    368             if ( ! isset($cache[$id]) )
    369             $cache[$id] = array();
    370         }
    371 
    372         foreach ( array_keys($cache) as $terms)
    373         wp_cache_set($terms, $cache[$terms], 'terms_meta');
    374 
    375         return $cache;
    376     }
    377 
    378     /**
    379      * Function that saves the meta from form.
    380      *
    381      * @param $id : terms (category) ID
    382      * @return void;
    383      */
    384     function wptm_save_meta_tags($id) {
    385 
    386         global $metaList;
    387         // Check that the meta form is posted
    388         $wptm_edit = $_POST["wptm_edit"];
    389         if (isset($wptm_edit) && !empty($wptm_edit)) {
    390 
    391             foreach($metaList as $inputName => $inputType)
    392             {
    393                 $inputValue = $_POST['wptm_'.$inputName];
    394                 delete_terms_meta($id, $inputName);
    395                 if (isset($inputValue) && !empty($inputValue)) {
    396                     add_terms_meta($id, $inputName, $inputValue);
    397                 }
    398             }
    399         }
    400     }
    401 
    402     /**
    403      * Function that deletes the meta for a terms (category/..)
    404      *
    405      * @param $id : terms (category) ID
    406      * @return void
    407      */
    408     function wptm_delete_meta_tags($id) {
    409 
    410         global $metaList;
    411         foreach($metaList as $inputName => $inputType)
    412         {
    413             delete_terms_meta($id, $inputName);
    414         }
    415     }
    416 
    417     /**
    418      * Function that display the meta text input.
    419      *
    420      * @return void.
    421      */
    422     function wptm_add_meta_textinput()
     420    }
     421}
     422
     423/**
     424 * Function that deletes the meta for a terms (category/..)
     425 *
     426 * @param $id : terms (category) ID
     427 * @return void
     428 */
     429function wptm_delete_meta_tags($id) {
     430
     431    $metaList = get_option("wptm_configuration");
     432    foreach($metaList as $inputName => $inputType)
    423433    {
    424         global $category;
    425         global $metaList;
    426         $category_id = $category;
    427         if (is_object($category_id)) {
    428             $category_id = $category_id->term_id;
    429         }
    430 
    431         ?>
     434        delete_terms_meta($id, $inputName);
     435    }
     436}
     437
     438/**
     439 * Function that display the meta text input.
     440 *
     441 * @return void.
     442 */
     443function wptm_add_meta_textinput()
     444{
     445    global $category;
     446    $metaList = get_option("wptm_configuration");
     447    $category_id = $category;
     448    if (is_object($category_id)) {
     449        $category_id = $category_id->term_id;
     450    }
     451    if(!is_null($metaList) && count($metaList) > 0)
     452    {
     453    ?>
     454<div id="commentstatusdiv" class="postbox " >
     455<h4><span style="padding: 10px"><?php _e('Category meta', 'wp-category-meta');?></span></h4>
     456<div class="inside">
    432457<input
    433458    value="wptm_edit" type="hidden" name="wptm_edit" />
    434459<table class="form-table">
    435     <tr>
    436         <th style="text-align: left;" colspan="2"><?php _e('Category meta', 'wp-category-meta');?></th>
    437     </tr>
    438460    <?php
    439461    foreach($metaList as $inputName => $inputType)
     
    456478            <?php _e('This additionnal data is attached to the current category', 'wp-category-meta');?></td>
    457479    </tr>
    458     <?php }
     480    <?php }//end foreach
     481    }//end IF
    459482    } ?>
    460483</table>
     484</div>
     485</div>
    461486    <?php
    462     }
    463     ?>
     487}
     488?>
Note: See TracChangeset for help on using the changeset viewer.