PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.4.4
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.4.4
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 / suggestterms-table.php

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

363 lines 10.8 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 SuggestTerms_List extends WP_List_Table
7 {
8
9 /** Class constructor */
10 public function __construct()
11 {
12
13 parent::__construct([
14 'singular' => __('Suggest Term', 'simple-tags'), //singular name of the listed records
15 'plural' => __('Suggest 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_suggestterms 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_suggestterms()
30 {
31 return taxopress_get_suggestterm_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_suggestterm_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-suggestterm-tr'];
52 $id = 'st-suggestterm-' . md5($item['ID']);
53 echo sprintf('<tr id="%s" class="%s">', $id, 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' => __('Suggest Term Post type', 'simple-tags')
69 ];
70
71 return $columns;
72 }
73
74 /**
75 * Render a column when no column specific method exist.
76 *
77 * @param array $item
78 * @param string $column_name
79 *
80 * @return mixed
81 */
82 public function column_default($item, $column_name)
83 {
84 return !empty($item[$column_name]) ? $item[$column_name] : '&mdash;';
85 }
86
87 /** Text displayed when no stterm data is available */
88 public function no_items()
89 {
90 _e('No item avaliable.', 'simple-tags');
91 }
92
93 /**
94 * Displays the search box.
95 *
96 * @param string $text The 'submit' button label.
97 * @param string $input_id ID attribute value for the search input field.
98 *
99 *
100 */
101 public function search_box($text, $input_id)
102 {
103 if (empty($_REQUEST['s']) && !$this->has_items()) {
104 return;
105 }
106
107 $input_id = $input_id . '-search-input';
108
109 if (!empty($_REQUEST['orderby'])) {
110 echo '<input type="hidden" name="orderby" value="' . esc_attr(sanitize_text_field($_REQUEST['orderby'])) . '" />';
111 }
112 if (!empty($_REQUEST['order'])) {
113 echo '<input type="hidden" name="order" value="' . esc_attr(sanitize_text_field($_REQUEST['order'])) . '" />';
114 }
115 if (!empty($_REQUEST['page'])) {
116 echo '<input type="hidden" name="page" value="' . esc_attr(sanitize_text_field($_REQUEST['page'])) . '" />';
117 }
118 ?>
119 <p class="search-box">
120 <label class="screen-reader-text" for="<?php echo esc_attr($input_id); ?>"><?php echo $text; ?>:</label>
121 <input type="search" id="<?php echo esc_attr($input_id); ?>" name="s"
122 value="<?php _admin_search_query(); ?>"/>
123 <?php submit_button($text, '', '', false, ['id' => 'search-submit']); ?>
124 </p>
125 <?php
126 }
127
128 /**
129 * Sets up the items (roles) to list.
130 */
131 public function prepare_items()
132 {
133
134 $this->_column_headers = $this->get_column_info();
135
136 /**
137 * First, lets decide how many records per page to show
138 */
139 $per_page = $this->get_items_per_page('st_suggestterms_per_page', 20);
140
141 /**
142 * Fetch the data
143 */
144 $data = self::get_st_suggestterms();
145
146 /**
147 * Handle search
148 */
149 if ((!empty($_REQUEST['s'])) && $search = sanitize_text_field($_REQUEST['s'])) {
150 $data_filtered = [];
151 foreach ($data as $item) {
152 if ($this->str_contains($item['title'], $search, false)) {
153 $data_filtered[] = $item;
154 }
155 }
156 $data = $data_filtered;
157 }
158
159 /**
160 * This checks for sorting input and sorts the data in our array accordingly.
161 */
162 function usort_reorder($a, $b)
163 {
164 $orderby = (!empty($_REQUEST['orderby'])) ? sanitize_text_field($_REQUEST['orderby']) : 'ID'; //If no sort, default to role
165 $order = (!empty($_REQUEST['order'])) ? sanitize_text_field($_REQUEST['order']) : 'desc'; //If no order, default to asc
166 $result = strnatcasecmp($a[$orderby],
167 $b[$orderby]); //Determine sort order, case insensitive, natural order
168
169 return ($order === 'asc') ? $result : -$result; //Send final sort direction to usort
170 }
171
172 usort($data, 'usort_reorder');
173
174 /**
175 * Pagination.
176 */
177 $current_page = $this->get_pagenum();
178 $total_items = count($data);
179
180
181 /**
182 * The WP_List_Table class does not handle pagination for us, so we need
183 * to ensure that the data is trimmed to only the current page. We can use
184 * array_slice() to
185 */
186 $data = array_slice($data, (($current_page - 1) * $per_page), $per_page);
187
188 /**
189 * Now we can add the data to the items property, where it can be used by the rest of the class.
190 */
191 $this->items = $data;
192
193 /**
194 * We also have to register our pagination options & calculations.
195 */
196 $this->set_pagination_args([
197 'total_items' => $total_items, //calculate the total number of items
198 'per_page' => $per_page, //determine how many items to show on a page
199 'total_pages' => ceil($total_items / $per_page) //calculate the total number of pages
200 ]);
201 }
202
203 /**
204 * Determine if a given string contains a given substring.
205 *
206 * @param string $haystack
207 * @param string|array $needles
208 * @param bool $sensitive Use case sensitive search
209 *
210 * @return bool
211 */
212 public function str_contains($haystack, $needles, $sensitive = true)
213 {
214 foreach ((array)$needles as $needle) {
215 $function = $sensitive ? 'mb_strpos' : 'mb_stripos';
216 if ($needle !== '' && $function($haystack, $needle) !== false) {
217 return true;
218 }
219 }
220
221 return false;
222 }
223
224 /**
225 * Columns to make sortable.
226 *
227 * @return array
228 */
229 protected function get_sortable_columns()
230 {
231 $sortable_columns = [
232 'title' => ['title', true],
233 'taxonomy' => ['taxonomy', true],
234 ];
235
236 return $sortable_columns;
237 }
238
239 /**
240 * Generates and display row actions links for the list table.
241 *
242 * @param object $item The item being acted upon.
243 * @param string $column_name Current column name.
244 * @param string $primary Primary column name.
245 *
246 * @return string The row actions HTML, or an empty string if the current column is the primary column.
247 */
248 protected function handle_row_actions($item, $column_name, $primary)
249 {
250 //Build row actions
251 $actions = [
252 'edit' => sprintf(
253 '<a href="%s">%s</a>',
254 add_query_arg(
255 [
256 'page' => 'st_suggestterms',
257 'add' => 'new_item',
258 'action' => 'edit',
259 'taxopress_suggestterms' => $item['ID'],
260 ],
261 admin_url('admin.php')
262 ),
263 __('Edit', 'simple-tags')
264 ),
265 'delete' => sprintf(
266 '<a href="%s" class="delete-suggestterm">%s</a>',
267 add_query_arg([
268 'page' => 'st_suggestterms',
269 'action' => 'taxopress-delete-suggestterm',
270 'taxopress_suggestterms' => esc_attr($item['ID']),
271 '_wpnonce' => wp_create_nonce('suggestterm-action-request-nonce')
272 ],
273 admin_url('admin.php')),
274 __('Delete', 'simple-tags')
275 ),
276 ];
277
278 return $column_name === $primary ? $this->row_actions($actions, false) : '';
279 }
280
281 /**
282 * Method for title column
283 *
284 * @param array $item
285 *
286 * @return string
287 */
288 protected function column_title($item)
289 {
290 $title = sprintf(
291 '<a href="%1$s"><strong><span class="row-title">%2$s</span></strong></a>',
292 add_query_arg(
293 [
294 'page' => 'st_suggestterms',
295 'add' => 'new_item',
296 'action' => 'edit',
297 'taxopress_suggestterms' => $item['ID'],
298 ],
299 admin_url('admin.php')
300 ),
301 esc_html($item['title'])
302 );
303
304 return $title;
305 }
306
307 /**
308 * Method for taxonomy column
309 *
310 * @param array $item
311 *
312 * @return string
313 */
314 protected function column_taxonomy($item)
315 {
316 $taxonomy = get_taxonomy($item['taxonomy']);
317
318 if($taxonomy){
319 $return = $taxonomy->labels->name;
320 }else{
321 $return = '&mdash;';
322 }
323
324 return $return;
325 }
326
327 /**
328 * The action column
329 *
330 * @param $item
331 *
332 * @return string
333 */
334 protected function column_post_types($item)
335 {
336 $embedded = (isset($item['post_types']) && is_array($item['post_types']) && count($item['post_types']) > 0) ? $item['post_types'] : false;
337 if ($embedded) {
338 $args = apply_filters('taxopress_attach_post_types_to_taxonomy', ['public' => true]);
339 if (!is_array($args)) {
340 $args = ['public' => true];
341 }
342 $output = 'objects'; // Or objects.
343 $post_types = apply_filters('taxopress_get_post_types_for_taxonomies', get_post_types($args, $output),
344 $args, $output);
345
346 $result_array = [];
347 foreach ($post_types as $post_type) {
348 $embedded_options[$post_type->name] = $post_type->label;
349 }
350 foreach ($embedded as $location) {
351 $result_array[] = isset($embedded_options[$location]) ? $embedded_options[$location] : '';
352 }
353 $result = join(', ', $result_array);
354 } else {
355 $result = esc_attr__('None', 'simple-tags');
356 }
357
358 return $result;
359 }
360
361
362 }
363