PluginProbe ʕ •ᴥ•ʔ
Search Regex / trunk
Search Regex vtrunk
trunk 1.4.12 1.4.13 1.4.14 1.4.15 1.4.16 2.0 2.0.1 2.1 2.2 2.2.1 2.3 2.3.1 2.3.2 2.3.3 2.4 2.4.1 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.1 3.1.1 3.1.2 3.2 3.3 3.3.0 3.3.1 3.4 3.4.1 3.4.2
search-regex / includes / source / core / class-terms.php
search-regex / includes / source / core Last commit date
class-comment-meta.php 5 months ago class-comment.php 5 months ago class-meta.php 5 months ago class-options.php 5 months ago class-post-meta.php 5 months ago class-post.php 5 months ago class-terms.php 4 months ago class-user-meta.php 5 months ago class-user.php 5 months ago
class-terms.php
174 lines
1 <?php
2
3 namespace SearchRegex\Source\Core;
4
5 use SearchRegex\Source;
6 use SearchRegex\Plugin;
7
8 /**
9 * Term source
10 */
11 class Terms extends Source\Source {
12 public function get_table_id() {
13 return 'term_id';
14 }
15
16 public function get_table_name() {
17 global $wpdb;
18
19 return $wpdb->prefix . 'terms';
20 }
21
22 public function get_title_column() {
23 return 'name';
24 }
25
26 public function save( $row_id, array $changes ) {
27 $term = $this->get_columns_to_change( $changes );
28
29 if ( count( $term ) > 0 ) {
30 $update = [];
31 global $wpdb;
32
33 $existing = $wpdb->get_row( $wpdb->prepare(
34 "SELECT t.term_id, t.name, t.slug, tt.taxonomy
35 FROM {$wpdb->terms} t
36 INNER JOIN {$wpdb->term_taxonomy} tt ON t.term_id = tt.term_id
37 WHERE t.term_id = %d
38 LIMIT 1",
39 $row_id
40 ) );
41
42 if ( $existing === null ) {
43 return new \WP_Error( 'searchregex', 'Term not found.' );
44 }
45
46 if ( isset( $term['name'] ) ) {
47 $update['name'] = (string) $term['name'];
48 }
49
50 if ( isset( $term['slug'] ) ) {
51 $update['slug'] = (string) $term['slug'];
52 }
53
54 if ( isset( $term['description'] ) ) {
55 $update['description'] = (string) $term['description'];
56 }
57
58 $this->log_save( 'term', array_merge( [ 'term_id' => $row_id ], $term ) );
59
60 // This does all the sanitization
61 $result = true;
62
63 if ( Plugin\Settings::init()->can_save() && count( $update ) > 0 ) {
64 $result = wp_update_term( $row_id, $existing->taxonomy, $update );
65 }
66
67 if ( $result ) {
68 return true;
69 }
70
71 return new \WP_Error( 'searchregex', 'Failed to update term.' );
72 }
73
74 return true;
75 }
76
77 public function delete_row( $row_id ) {
78 $this->log_save( 'delete term', $row_id );
79
80 if ( Plugin\Settings::init()->can_save() ) {
81 $term = get_term( $row_id );
82 if ( $term instanceof \WP_Term && wp_delete_term( $row_id, $term->taxonomy ) ) {
83 return true;
84 }
85
86 return new \WP_Error( 'searchregex_delete', 'Failed to delete term', 401 );
87 }
88
89 return true;
90 }
91
92 public function autocomplete( array $column, $value ) {
93 global $wpdb;
94
95 if ( $column['column'] === 'name' ) {
96 // phpcs:ignore
97 return $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT name as id,name as value FROM {$this->get_table_name()} WHERE name LIKE %s LIMIT %d", '%' . $wpdb->esc_like( $value ) . '%', self::AUTOCOMPLETE_LIMIT ) );
98 }
99
100 if ( $column['column'] === 'slug' ) {
101 // phpcs:ignore
102 return $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT slug as id,slug as value FROM {$this->get_table_name()} WHERE slug LIKE %s LIMIT %d", '%' . $wpdb->esc_like( $value ) . '%', self::AUTOCOMPLETE_LIMIT ) );
103 }
104
105 if ( $column['column'] === 'description' ) {
106 // phpcs:ignore
107 return $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT tt.description as id,tt.description as value FROM {$wpdb->term_taxonomy} AS tt WHERE tt.description LIKE %s AND tt.description != '' LIMIT %d", '%' . $wpdb->esc_like( $value ) . '%', self::AUTOCOMPLETE_LIMIT ) );
108 }
109
110 return [];
111 }
112
113 public function get_schema() {
114 global $wpdb;
115
116 $taxonomies = [];
117 $taxes = get_taxonomies( [], 'objects' );
118
119 foreach ( $taxes as $tax ) {
120 // @phpstan-ignore function.alreadyNarrowedType
121 if ( is_object( $tax ) ) {
122 $taxonomies[] = [
123 'value' => $tax->name,
124 'label' => $tax->label,
125 ];
126 }
127 }
128
129 return [
130 'name' => __( 'Terms', 'search-regex' ),
131 'table' => $wpdb->prefix . 'terms',
132 'columns' => [
133 [
134 'column' => 'term_id',
135 'type' => 'integer',
136 'title' => __( 'ID', 'search-regex' ),
137 'options' => 'api',
138 'modify' => false,
139 ],
140 [
141 'column' => 'name',
142 'type' => 'string',
143 'title' => __( 'Name', 'search-regex' ),
144 'options' => 'api',
145 'global' => true,
146 ],
147 [
148 'column' => 'slug',
149 'type' => 'string',
150 'title' => __( 'Slug', 'search-regex' ),
151 'options' => 'api',
152 'global' => true,
153 ],
154 [
155 'column' => 'description',
156 'type' => 'string',
157 'title' => __( 'Description', 'search-regex' ),
158 'options' => 'api',
159 'join' => 'description',
160 'global' => true,
161 ],
162 [
163 'column' => 'taxonomy',
164 'type' => 'member',
165 'title' => __( 'Taxonomy', 'search-regex' ),
166 'options' => $taxonomies,
167 'join' => 'taxonomy',
168 'modify' => false,
169 ],
170 ],
171 ];
172 }
173 }
174