Completed
Push — master ( 5fe409...71e3f4 )
by Marin
02:59
created

Term_Meta_Datastore::set_id()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
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
	 * ID of the term.
13
	 *
14
	 * @var int
15
	 */
16
	protected $term_id;
17
18
	/**
19
	 * Initialization tasks.
20
	 **/
21
	public function init() {
22
		global $wpdb;
23
24
		// Setup termmeta table and hooks only once
25
		if ( ! empty( $wpdb->termmeta ) ) {
26
			return;
27
		}
28
29
		$wpdb->termmeta = $wpdb->prefix . 'termmeta';
30
31
		self::create_table();
32
33
		// Delete all meta associated with the deleted term
34
		add_action( 'delete_term', array( __CLASS__, 'on_delete_term' ), 10, 3 );
35
	}
36
37
	/**
38
	 * Create term meta database table (for WP < 4.4)
39
	 **/
40
	public static function create_table() {
41
		global $wpdb;
42
43
		$tables = $wpdb->get_results( 'SHOW TABLES LIKE "' . $wpdb->prefix . 'termmeta"' );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
44
45
		if ( ! empty( $tables ) ) {
46
			return;
47
		}
48
49
		$charset_collate = '';	
50
		if ( ! empty( $wpdb->charset ) ) {
51
			$charset_collate = 'DEFAULT CHARACTER SET ' . $wpdb->charset;
52
		}
53
			
54
		if ( ! empty( $wpdb->collate ) ) {
55
			$charset_collate .= ' COLLATE ' . $wpdb->collate;
56
		}
57
58
		$wpdb->query( 'CREATE TABLE ' . $wpdb->prefix . 'termmeta (
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
introduced by
Attempting a database schema change is highly discouraged.
Loading history...
59
			meta_id bigint(20) unsigned NOT NULL auto_increment,
60
			term_id bigint(20) unsigned NOT NULL default "0",
61
			meta_key varchar(255) default NULL,
62
			meta_value longtext,
63
			PRIMARY KEY	(meta_id),
64
			KEY term_id (term_id),
65
			KEY meta_key (meta_key)
66
		) ' . $charset_collate . ';' );
67
	}
68
69
	/**
70
	 * Delete term meta on term deletion.
71
	 * Useful for WP < 4.4.
72
	 * 
73
	 * @param  int $term_id  Term ID.
74
	 * @return bool Result of the deletion operation.
75
	 */
76
	public static function on_delete_term( $term_id ) {
77
		global $wpdb;
78
79
		return $wpdb->query( '
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
80
			DELETE FROM ' . $wpdb->termmeta . '
81
			WHERE `term_id` = "' . intval( $term_id ) . '"
82
		' );
83
	}
84
85
	/**
86
	 * Retrieve the type of meta data.
87
	 *
88
	 * @return string
89
	 */
90
	public function get_meta_type() {
91
		return 'term';
92
	}
93
94
	/**
95
	 * Retrieve the meta table name to query.
96
	 *
97
	 * @return string
98
	 */
99
	public function get_table_name() {
100
		global $wpdb;
101
		return $wpdb->termmeta;
102
	}
103
104
	/**
105
	 * Retrieve the meta table field name to query by.
106
	 *
107
	 * @return string
108
	 */
109
	public function get_table_field_name() {
110
		return 'term_id';
111
	}
112
113
	/**
114
	 * Set the term ID of the datastore.
115
	 * 
116
	 * @param int $term_id ID of the term.
117
	 */
118
	public function set_id( $term_id ) {
119
		$this->term_id = $term_id;
120
	}
121
122
	/**
123
	 * Retrieve the term ID of the datastore.
124
	 * 
125
	 * @return int ID of the term.
126
	 */
127
	public function get_id() {
128
		return $this->term_id;
129
	}
130
131
}
132