/
wp-term-meta.php
148 lines (125 loc) · 4.12 KB
/
wp-term-meta.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/*
Plugin Name: WP Term Meta
Description: Lightweight term meta that doesn't depend on new tables or pollute the options table.
Author: Ben Doherty @ Oomph, Inc.
Version: 0.1.1
Author URI: http://www.oomphinc.com/thinking/author/bdoherty/
License: GPLv2 or later
Copyright © 2015 Oomph, Inc. <http://oomphinc.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class WP_Term_Meta {
const post_type = '_term_meta';
static function init() {
register_post_type( self::post_type, array( 'public' => false ) );
register_post_status( self::post_type, array( 'public' => true ) );
}
private static function _post( $term_id, $taxonomy = null ) {
// Allow first argument to be term object instead
if( is_object( $term_id ) ) {
$taxonomy = $term_id->taxonomy;
$term_id = $term_id->term_id;
}
// Get or create the post whose name is _meta_{$taxonomy}_{$term_id}.
//
// This naming format is used to take advantage of the index on post_name
// so we can quickly pull the meta-containing post object from a known term object.
//
// Conversely, if we need to do a search on meta (which is non-optimal,) we can
// easily extract the correct taxonomy and term from the post_name
$post_name = sanitize_key( '_meta_' . $taxonomy . '_' . $term_id );
$post_query = new WP_Query( array(
'post_type' => self::post_type,
'post_status' => self::post_type,
'posts_per_page' => 1,
'name' => $post_name
) );
if( $post_query->have_posts() ) {
$post = $post_query->next_post();
}
else if( current_user_can( 'edit_posts' ) ) {
$post_id = wp_insert_post( array(
'post_name' => $post_name,
'post_type' => self::post_type,
'post_status' => self::post_type,
) );
$post = get_post( $post_id );
}
return isset( $post ) ? $post : null;
}
/**
* Update a term meta
*/
static function update( $term_id, $taxonomy, $key_or_array = null, $value = null, $prev_value = '' ) {
// Allow first argument to be term object instead, which then the second and
// third arguments become the key and possible value instead
if( is_object( $term_id ) ) {
$prev_value = $value;
$value = $key_or_array;
$key_or_array = $taxonomy;
}
$post = self::_post( $term_id, $taxonomy );
if( $post ) {
if( is_string( $key_or_array ) ) {
$key_or_array = array( $key_or_array => $value );
}
foreach( $key_or_array as $key => $value ) {
if( isset( $value ) ) {
update_post_meta( $post->ID, $key, $value, $prev_value );
}
else {
delete_post_meta( $post->ID, $key, $prev_value );
}
}
return true;
}
else {
return false;
}
}
/**
* Get a term meta, or all, term meta.
*
* @return mixed|null
*/
static function get( $term_id, $taxonomy = null, $key = null, $prev_value = null ) {
if( is_object( $term_id ) ) {
$prev_value = $key;
$key = $taxonomy;
}
$post = self::_post( $term_id, $taxonomy );
if( $post ) {
if( empty( $key ) ) {
$custom = get_post_custom( $post->ID );
// Flatten down to key-value instead of key-value-array
$values = array();
foreach( $custom as $key => $meta_values ) {
$values[$key] = $meta_values[0];
}
return $values;
}
return get_post_meta( $post->ID, $key, true );
}
}
/**
* Delete a term meta value.
*
* @param int|object $term_id (or term object)
* @param string $taxonomy or $key
* @param string $key optional
*/
static function delete( $term_id, $taxonomy, $key = null ) {
self::update( $term_id, $taxonomy, $key );
}
}
add_action( 'init', array( 'WP_Term_Meta', 'init' ), 0 );