|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Datastore; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon_Fields\Field\Field; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Term meta datastore class. |
|
9
|
|
|
*/ |
|
10
|
|
|
class Term_Meta_Datastore extends Meta_Datastore { |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Initialization tasks. |
|
14
|
|
|
*/ |
|
15
|
|
|
public function init() { |
|
16
|
|
|
global $wpdb; |
|
17
|
|
|
|
|
18
|
|
|
// Setup termmeta table and hooks only once |
|
19
|
|
|
if ( ! empty( $wpdb->termmeta ) ) { |
|
20
|
|
|
return; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
$wpdb->termmeta = $wpdb->prefix . 'termmeta'; |
|
24
|
|
|
|
|
25
|
|
|
static::create_table(); |
|
26
|
|
|
|
|
27
|
|
|
// Delete all meta associated with the deleted term |
|
28
|
|
|
add_action( 'delete_term', array( __CLASS__, 'on_delete_term' ), 10, 3 ); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Create term meta database table (for WP < 4.4) |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function create_table() { |
|
35
|
|
|
global $wpdb; |
|
36
|
|
|
|
|
37
|
|
|
$tables = $wpdb->get_results( 'SHOW TABLES LIKE "' . $wpdb->prefix . 'termmeta"' ); |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
if ( ! empty( $tables ) ) { |
|
40
|
|
|
return; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$charset_collate = ''; |
|
44
|
|
|
if ( ! empty( $wpdb->charset ) ) { |
|
45
|
|
|
$charset_collate = 'DEFAULT CHARACTER SET ' . $wpdb->charset; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if ( ! empty( $wpdb->collate ) ) { |
|
49
|
|
|
$charset_collate .= ' COLLATE ' . $wpdb->collate; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$wpdb->query( 'CREATE TABLE ' . $wpdb->prefix . 'termmeta ( |
|
|
|
|
|
|
53
|
|
|
meta_id bigint(20) unsigned NOT NULL auto_increment, |
|
54
|
|
|
term_id bigint(20) unsigned NOT NULL default "0", |
|
55
|
|
|
meta_key varchar(255) default NULL, |
|
56
|
|
|
meta_value longtext, |
|
57
|
|
|
PRIMARY KEY (meta_id), |
|
58
|
|
|
KEY term_id (term_id), |
|
59
|
|
|
KEY meta_key (meta_key) |
|
60
|
|
|
) ' . $charset_collate . ';' ); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Delete term meta on term deletion. |
|
65
|
|
|
* Useful for WP < 4.4. |
|
66
|
|
|
* |
|
67
|
|
|
* @param int $term_id Term ID. |
|
68
|
|
|
* @return bool Result of the deletion operation. |
|
69
|
|
|
*/ |
|
70
|
|
|
public static function on_delete_term( $term_id ) { |
|
71
|
|
|
global $wpdb; |
|
72
|
|
|
|
|
73
|
|
|
return $wpdb->query( ' |
|
|
|
|
|
|
74
|
|
|
DELETE FROM ' . $wpdb->termmeta . ' |
|
75
|
|
|
WHERE `term_id` = "' . intval( $term_id ) . '" |
|
76
|
|
|
' ); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Retrieve the type of meta data. |
|
81
|
|
|
* |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
|
|
public function get_meta_type() { |
|
85
|
|
|
return 'term'; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Retrieve the meta table name to query. |
|
90
|
|
|
* |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
|
|
public function get_table_name() { |
|
94
|
|
|
global $wpdb; |
|
95
|
|
|
return $wpdb->termmeta; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Retrieve the meta table field name to query by. |
|
100
|
|
|
* |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
|
|
public function get_table_field_name() { |
|
104
|
|
|
return 'term_id'; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|