PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.44.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.44.0
3.53.0 3.52.0 3.51.0 3.50.0 3.45.0 3.38.0 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.40.0 3.40.1 3.41.0 3.42.0 3.43.0 3.44.0 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 All 177 releases
simple-tags / inc / autoterms-table.php

autoterms-table.php in Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms 3.44.0, at inc/autoterms-table.php

399 lines 12.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!class_exists('WP_List_Table')) {
3 require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
4 }
5
6 class Autoterms_List extends WP_List_Table
7 {
8
9 /** Class constructor */
10 public function __construct()
11 {
12
13 parent::__construct([
14 'singular' => __('Auto term', 'simple-tags'), //singular name of the listed records
15 'plural' => __('Auto terms', 'simple-tags'), //plural name of the listed records
16 'ajax' => false //does this table support ajax?
17 ]);
18
19 }
20
21 /**
22 * Retrieve st_autoterms data from the database
23 *
24 * @param int $per_page
25 * @param int $page_number
26 *
27 * @return mixed
28 */
29 public static function get_st_autoterms()
30 {
31 return taxopress_get_autoterm_data();
32 }
33
34 /**
35 * Returns the count of records in the database.
36 *
37 * @return null|string
38 */
39 public static function record_count()
40 {
41 return count(taxopress_get_autoterm_data());
42 }
43
44 /**
45 * Show single row item
46 *
47 * @param array $item
48 */
49 public function single_row($item)
50 {
51 $class = ['st-autoterm-tr'];
52 $id = 'st-autoterm-' . md5($item['ID']);
53 echo sprintf('<tr id="%s" class="%s">', esc_attr($id), esc_attr(implode(' ', $class)));
54 $this->single_row_columns($item);
55 echo '</tr>';
56 }
57
58 /**
59 * Associative array of columns
60 *
61 * @return array
62 */
63 function get_columns()
64 {
65 $columns = [
66 'title' => __('Title', 'simple-tags'),
67 'taxonomy' => __('Taxonomy', 'simple-tags'),
68 'post_types' => __('Post Type', 'simple-tags'),
69 'source' => __('Source', 'simple-tags')
70 ];
71
72 return $columns;
73 }
74
75 /**
76 * Render a column when no column specific method exist.
77 *
78 * @param array $item
79 * @param string $column_name
80 *
81 * @return mixed
82 */
83 public function column_default($item, $column_name)
84 {
85 return !empty($item[$column_name]) ? $item[$column_name] : '&mdash;';
86 }
87
88 /** Text displayed when no stterm data is available */
89 public function no_items()
90 {
91 _e('No item avaliable.', 'simple-tags');
92 }
93
94 /**
95 * Displays the search box.
96 *
97 * @param string $text The 'submit' button label.
98 * @param string $input_id ID attribute value for the search input field.
99 *
100 *
101 */
102 public function search_box($text, $input_id)
103 {
104 if (empty($_REQUEST['s']) && !$this->has_items()) {
105 return;
106 }
107
108 $input_id = $input_id . '-search-input';
109
110 if (!empty($_REQUEST['orderby'])) {
111 echo '<input type="hidden" name="orderby" value="' . esc_attr(sanitize_text_field($_REQUEST['orderby'])) . '" />';
112 }
113 if (!empty($_REQUEST['order'])) {
114 echo '<input type="hidden" name="order" value="' . esc_attr(sanitize_text_field($_REQUEST['order'])) . '" />';
115 }
116 if (!empty($_REQUEST['page'])) {
117 echo '<input type="hidden" name="page" value="' . esc_attr(sanitize_text_field($_REQUEST['page'])) . '" />';
118 }
119 ?>
120 <p class="search-box">
121 <label class="screen-reader-text" for="<?php echo esc_attr($input_id); ?>"><?php echo esc_html($text); ?>:</label>
122 <input type="search" id="<?php echo esc_attr($input_id); ?>" name="s"
123 value="<?php _admin_search_query(); ?>"/>
124 <?php submit_button($text, '', '', false, ['id' => 'search-submit']); ?>
125 </p>
126 <?php
127 }
128
129 /**
130 * Sets up the items (roles) to list.
131 */
132 public function prepare_items()
133 {
134
135 $this->_column_headers = $this->get_column_info();
136
137 /**
138 * First, lets decide how many records per page to show
139 */
140 $per_page = $this->get_items_per_page('st_autoterms_per_page', 20);
141
142 /**
143 * Fetch the data
144 */
145 $data = self::get_st_autoterms();
146
147 /**
148 * Handle search
149 */
150 if ((!empty($_REQUEST['s'])) && $search = sanitize_text_field($_REQUEST['s'])) {
151 $data_filtered = [];
152 foreach ($data as $item) {
153 if ($this->str_contains($item['title'], $search, false)) {
154 $data_filtered[] = $item;
155 }
156 }
157 $data = $data_filtered;
158 }
159
160 /**
161 * This checks for sorting input and sorts the data in our array accordingly.
162 */
163 function usort_reorder($a, $b)
164 {
165 $orderby = (!empty($_REQUEST['orderby'])) ? sanitize_text_field($_REQUEST['orderby']) : 'ID'; //If no sort, default to role
166 $order = (!empty($_REQUEST['order'])) ? sanitize_text_field($_REQUEST['order']) : 'desc'; //If no order, default to asc
167 $result = strnatcasecmp($a[$orderby],
168 $b[$orderby]); //Determine sort order, case insensitive, natural order
169
170 return ($order === 'asc') ? $result : -$result; //Send final sort direction to usort
171 }
172
173 usort($data, 'usort_reorder');
174
175 /**
176 * Pagination.
177 */
178 $current_page = $this->get_pagenum();
179 $total_items = count($data);
180
181
182 /**
183 * The WP_List_Table class does not handle pagination for us, so we need
184 * to ensure that the data is trimmed to only the current page. We can use
185 * array_slice() to
186 */
187 $data = array_slice($data, (($current_page - 1) * $per_page), $per_page);
188
189 /**
190 * Now we can add the data to the items property, where it can be used by the rest of the class.
191 */
192 $this->items = $data;
193
194 /**
195 * We also have to register our pagination options & calculations.
196 */
197 $this->set_pagination_args([
198 'total_items' => $total_items, //calculate the total number of items
199 'per_page' => $per_page, //determine how many items to show on a page
200 'total_pages' => ceil($total_items / $per_page) //calculate the total number of pages
201 ]);
202 }
203
204 /**
205 * Determine if a given string contains a given substring.
206 *
207 * @param string $haystack
208 * @param string|array $needles
209 * @param bool $sensitive Use case sensitive search
210 *
211 * @return bool
212 */
213 public function str_contains($haystack, $needles, $sensitive = true)
214 {
215 foreach ((array)$needles as $needle) {
216 $function = $sensitive ? 'mb_strpos' : 'mb_stripos';
217 if ($needle !== '' && $function($haystack, $needle) !== false) {
218 return true;
219 }
220 }
221
222 return false;
223 }
224
225 /**
226 * Columns to make sortable.
227 *
228 * @return array
229 */
230 protected function get_sortable_columns()
231 {
232 $sortable_columns = [
233 'title' => ['title', true],
234 'taxonomy' => ['taxonomy', true],
235 ];
236
237 return $sortable_columns;
238 }
239
240 /**
241 * Generates and display row actions links for the list table.
242 *
243 * @param object $item The item being acted upon.
244 * @param string $column_name Current column name.
245 * @param string $primary Primary column name.
246 *
247 * @return string The row actions HTML, or an empty string if the current column is the primary column.
248 */
249 protected function handle_row_actions($item, $column_name, $primary)
250 {
251 //Build row actions
252 $actions = [
253 'edit' => sprintf(
254 '<a href="%s">%s</a>',
255 add_query_arg(
256 [
257 'page' => 'st_autoterms',
258 'add' => 'new_item',
259 'action' => 'edit',
260 'taxopress_autoterms' => $item['ID'],
261 ],
262 admin_url('admin.php')
263 ),
264 __('Edit', 'simple-tags')
265 ),
266 'delete' => sprintf(
267 '<a href="%s" class="delete-autoterm">%s</a>',
268 add_query_arg([
269 'page' => 'st_autoterms',
270 'action' => 'taxopress-delete-autoterm',
271 'taxopress_autoterms' => esc_attr($item['ID']),
272 '_wpnonce' => wp_create_nonce('autoterm-action-request-nonce')
273 ],
274 admin_url('admin.php')),
275 __('Delete', 'simple-tags')
276 ),
277 ];
278
279 $actions = apply_filters('taxopress_autoterm_row_actions', $actions, $item);
280 return $column_name === $primary ? $this->row_actions($actions, false) : '';
281 }
282
283 /**
284 * Method for title column
285 *
286 * @param array $item
287 *
288 * @return string
289 */
290 protected function column_title($item)
291 {
292 $title = sprintf(
293 '<a href="%1$s"><strong><span class="row-title">%2$s</span></strong></a>',
294 add_query_arg(
295 [
296 'page' => 'st_autoterms',
297 'add' => 'new_item',
298 'action' => 'edit',
299 'taxopress_autoterms' => $item['ID'],
300 ],
301 admin_url('admin.php')
302 ),
303 esc_html($item['title'])
304 );
305
306 return $title;
307 }
308
309 /**
310 * Method for taxonomy column
311 *
312 * @param array $item
313 *
314 * @return string
315 */
316 protected function column_taxonomy($item)
317 {
318 $taxonomy = get_taxonomy($item['taxonomy']);
319
320 if($taxonomy){
321 $return = $taxonomy->labels->name;
322 }else{
323 $return = '&mdash;';
324 }
325
326 return $return;
327 }
328
329 /**
330 * The action column
331 *
332 * @param $item
333 *
334 * @return string
335 */
336 protected function column_post_types($item)
337 {
338 $embedded = (isset($item['post_types']) && is_array($item['post_types']) && count($item['post_types']) > 0) ? $item['post_types'] : false;
339 if ($embedded) {
340 $args = apply_filters('taxopress_attach_post_types_to_taxonomy', ['public' => true]);
341 if (!is_array($args)) {
342 $args = ['public' => true];
343 }
344 $output = 'objects'; // Or objects.
345 $post_types = apply_filters('taxopress_get_post_types_for_taxonomies', get_post_types($args, $output),
346 $args, $output);
347
348 $result_array = [];
349 foreach ($post_types as $post_type) {
350 $embedded_options[$post_type->name] = $post_type->label;
351 }
352 foreach ($embedded as $location) {
353 $result_array[] = isset($embedded_options[$location]) ? $embedded_options[$location] : '';
354 }
355 $result = join(', ', $result_array);
356 } else {
357 $result = esc_html__('None', 'simple-tags');
358 }
359
360 return $result;
361 }
362
363 /**
364 * The action column
365 *
366 * @param $item
367 *
368 * @return string
369 */
370 protected function column_source($item)
371 {
372 $used_source = [];
373
374 if(isset($item['autoterm_use_taxonomy']) && !empty(taxopress_disp_boolean($item['autoterm_use_taxonomy']))) {
375 $used_source[] = esc_html__('Existing taxonomy terms', 'simple-tags');
376 }
377
378 if(isset($item['autoterm_use_open_ai']) && !empty(taxopress_disp_boolean($item['autoterm_use_open_ai']))) {
379 $used_source[] = esc_html__('OpenAI', 'simple-tags');
380 }
381
382 if(isset($item['autoterm_use_ibm_watson']) && !empty(taxopress_disp_boolean($item['autoterm_use_ibm_watson']))) {
383 $used_source[] = esc_html__('IBM Watson', 'simple-tags');
384 }
385
386 if(isset($item['autoterm_use_dandelion']) && !empty(taxopress_disp_boolean($item['autoterm_use_dandelion']))) {
387 $used_source[] = esc_html__('Dandelion', 'simple-tags');
388 }
389
390 if(isset($item['autoterm_use_opencalais']) && !empty(taxopress_disp_boolean($item['autoterm_use_opencalais']))) {
391 $used_source[] = esc_html__('LSEG / Refinitiv', 'simple-tags');
392 }
393
394 return join(', ', $used_source);
395 }
396
397
398 }
399