PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.51.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.51.0
3.54.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 All 178 releases
simple-tags / inc / class-tag-table.php

class-tag-table.php in Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms 3.51.0, at inc/class-tag-table.php

390 lines 11.3 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 Termcloud_List extends WP_List_Table
7 {
8 /** Class constructor */
9 public function __construct()
10 {
11
12 parent::__construct([
13 'singular' => __('Tag Cloud', 'simple-tags'), //singular name of the listed records
14 'plural' => __('Tags Cloud', 'simple-tags'), //plural name of the listed records
15 'ajax' => false //does this table support ajax?
16 ]);
17
18 }
19
20 /**
21 * Show single row item
22 *
23 * @param array $item
24 */
25 public function single_row($item)
26 {
27 $class = ['st-tax-tr'];
28 $id = 'st-tax-' . md5($item->slug);
29 echo sprintf('<tr id="%s" class="%s">', esc_attr($id), esc_attr(implode(' ', $class)));
30 $this->single_row_columns($item);
31 echo '</tr>';
32 }
33
34 /**
35 * Associative array of columns
36 *
37 * @return array
38 */
39 public function get_columns()
40 {
41 $columns = [
42 'cb' => '<input type="checkbox" />',
43 'name' => __('Name', 'simple-tags'),
44 'slug' => __('Slug', 'simple-tags'),
45 'count' => __('Count', 'simple-tags')
46 ];
47
48 return $columns;
49 }
50
51
52 /**
53 * Columns to make sortable.
54 *
55 * @return array
56 */
57 protected function get_sortable_columns()
58 {
59 $sortable_columns = array(
60 'name' => array( 'name', true ),
61 'slug' => array( 'slug', true ),
62 'count' => array( 'count', false )
63 );
64
65 return $sortable_columns;
66 }
67
68
69 /**
70 * Render a column when no column specific method exist.
71 *
72 * @param array $item
73 * @param string $column_name
74 *
75 * @return mixed
76 */
77 public function column_default($item, $column_name)
78 {
79 return !empty($item->$column_name) ? $item->$column_name : '&mdash;';
80 }
81
82
83 /**
84 * Retrieve termcloud data from the database
85 *
86 * @param int $per_page
87 * @param int $page_number
88 *
89 * @return mixed
90 */
91 public static function get_termcloud()
92 {
93
94 $order = '&selectionby=name&selection=asc&orderby=name&order=asc';
95 $args = 'hide_empty=false&number=&color=false&get=all&title=' . $order . '&taxonomy=' . SimpleTags_Admin::$taxonomy ;
96
97 $result = SimpleTags_Client_TagCloud::extendedTagResult($args);
98
99 return $result;
100 }
101
102
103 /**
104 * Delete a stterm record.
105 *
106 * @param int $id stterm ID
107 */
108 public static function delete_stterm($id)
109 {
110 $term = get_term($id);
111 wp_delete_term($term->term_id, $term->taxonomy);
112 }
113
114
115 /**
116 * Returns the count of records in the database.
117 *
118 * @return null|string
119 */
120 public static function record_count()
121 {
122 $order = '&selectionby=name&selection=asc&orderby=name&order=asc';
123 $args = 'hide_empty=false&number=&color=false&get=all&title=' . $order . '&taxonomy=' . SimpleTags_Admin::$taxonomy ;
124 $result = SimpleTags_Client_TagCloud::extendedTagResult($args);
125
126 return count($result);
127 }
128
129
130 /** Text displayed when no stterm data is available */
131 public function no_items()
132 {
133 _e('No term avaliable.', 'simple-tags');
134 }
135
136 /**
137 * Render the bulk edit checkbox
138 *
139 * @param array $item
140 *
141 * @return string
142 */
143 public function column_cb($item)
144 {
145 return sprintf(
146 '<input type="checkbox" name="st-bulk-delete-term[]" value="%s" />',
147 $item->term_id
148 );
149 }
150
151
152 /**
153 * Method for name column
154 *
155 * @param array $item
156 *
157 * @return string
158 */
159 protected function column_name($item)
160 {
161 $tag_link = get_term_link($item, $item->taxonomy);
162 $title = sprintf(
163 '<a href="%1$s" class="tag-cloud-link"><strong><span class="row-title">%2$s</span></strong></a>',
164 esc_url($tag_link),
165 esc_html($item->name)
166 );
167
168 return $title;
169 }
170
171 /**
172 * The action column
173 *
174 * @param $item
175 *
176 * @return string
177 */
178 protected function column_slug($item)
179 {
180 return !empty($item->slug) ? $item->slug : '&mdash;';
181 }
182
183 /**
184 * The action column
185 *
186 * @param $item
187 *
188 * @return string
189 */
190 protected function column_count($item)
191 {
192 return sprintf(
193 '<a href="%s" class="">%s</a>',
194 add_query_arg(
195 get_taxonomy($item->taxonomy)->query_var,
196 esc_attr($item->slug),
197 admin_url('edit.php')
198 ),
199 number_format_i18n($item->count)
200 );
201 }
202
203 /**
204 * Returns an associative array containing the bulk action
205 *
206 * @return array
207 */
208 public function get_bulk_actions()
209 {
210 $actions = [
211 'st-bulk-delete-term' => __('Delete', 'simple-tags')
212 ];
213
214 return $actions;
215 }
216
217 public function process_bulk_action()
218 {
219
220 //Detect when a bulk action is being triggered...
221 if ('delete' === $this->current_action()) {
222
223 // In our file that handles the request, verify the nonce.
224 $nonce = sanitize_text_field($_REQUEST['_wpnonce']);
225
226 if (! wp_verify_nonce($nonce, 'sp_delete_stterm')) {
227 die('Go get a life script kiddies');
228 } else {
229 self::delete_stterm(absint(sanitize_text_field($_GET['stterm'])));
230 }
231
232 }
233
234 // If the delete bulk action is triggered
235 if ((isset($_POST['action']) && $_POST['action'] == 'st-bulk-delete-term')
236 || (isset($_POST['action2']) && $_POST['action2'] == 'st-bulk-delete-term')
237 ) {
238
239 $delete_ids = array_map('sanitize_text_field', $_POST['st-bulk-delete-term']);
240
241 // loop over the array of record IDs and delete them
242 foreach ($delete_ids as $id) {
243 self::delete_stterm($id);
244
245 }
246 }
247 }
248
249 /**
250 * Determine if a given string contains a given substring.
251 *
252 * @param string $haystack
253 * @param string|array $needles
254 * @param bool $sensitive Use case sensitive search
255 *
256 * @return bool
257 */
258 public function str_contains($haystack, $needles, $sensitive = true)
259 {
260 foreach ((array)$needles as $needle) {
261 $function = $sensitive ? 'mb_strpos' : 'mb_stripos';
262 if ($needle !== '' && $function($haystack, $needle) !== false) {
263 return true;
264 }
265 }
266
267 return false;
268 }
269
270 /**
271 * Displays the search box.
272 *
273 * @param string $text The 'submit' button label.
274 * @param string $input_id ID attribute value for the search input field.
275 *
276 *
277 */
278 public function search_box($text, $input_id)
279 {
280 $input_id = $input_id . '-search-input';
281
282 if (!empty($_REQUEST['orderby'])) {
283 echo '<input type="hidden" name="orderby" value="' . esc_attr(sanitize_text_field($_REQUEST['orderby'])) . '" />';
284 }
285 if (!empty($_REQUEST['order'])) {
286 echo '<input type="hidden" name="order" value="' . esc_attr(sanitize_text_field($_REQUEST['order'])) . '" />';
287 }
288 if (!empty($_REQUEST['page'])) {
289 echo '<input type="hidden" name="page" value="' . esc_attr(sanitize_text_field($_REQUEST['page'])) . '" />';
290 }
291 if (!empty($_REQUEST['cpt'])) {
292 echo '<input type="hidden" name="cpt" value="' . esc_attr(sanitize_text_field($_REQUEST['cpt'])) . '" />';
293 }
294 if (!empty($_REQUEST['taxo'])) {
295 echo '<input type="hidden" name="taxo" value="' . esc_attr(sanitize_text_field($_REQUEST['taxo'])) . '" />';
296 }
297 $searchbox_search = (empty($_REQUEST['s']) && !$this->has_items()) ? 'visibility:hidden;' : '';
298 ?>
299 <p class="search-box" style="<?php echo esc_attr($searchbox_search); ?>">
300 <label class="screen-reader-text" for="<?php echo esc_attr($input_id); ?>"><?php echo esc_html($text); ?>:</label>
301 <input type="search" id="<?php echo esc_attr($input_id); ?>" name="s"
302 value="<?php _admin_search_query(); ?>"/>
303 <?php submit_button($text, '', '', false, ['id' => 'search-submit']); ?>
304 </p>
305 <?php
306 }
307
308
309 /**
310 * Sets up the items (roles) to list.
311 */
312 public function prepare_items()
313 {
314
315 $this->_column_headers = $this->get_column_info();
316
317 /**
318 * First, lets decide how many records per page to show
319 */
320 $per_page = $this->get_items_per_page('termcloud_per_page', 10);
321
322 /**
323 * handle bulk actions.
324 */
325 $this->process_bulk_action();
326
327 /**
328 * Fetch the data
329 */
330 $data = self::get_termcloud();
331
332 /**
333 * Handle search
334 */
335 if ((!empty($_REQUEST['s'])) && $search = sanitize_text_field($_REQUEST['s'])) {
336 $data_filtered = [];
337 foreach ($data as $item) {
338 if ($this->str_contains($item->slug, $search, false) || $this->str_contains($item->name, $search, false)) {
339 $data_filtered[] = $item;
340 }
341 }
342 $data = $data_filtered;
343 }
344
345 /**
346 * This checks for sorting input and sorts the data in our array accordingly.
347 */
348 function usort_reorder($a, $b)
349 {
350 $orderby = (!empty($_REQUEST['orderby'])) ? sanitize_text_field($_REQUEST['orderby']) : 'name'; //If no sort, default to role
351 $order = (!empty($_REQUEST['order'])) ? sanitize_text_field($_REQUEST['order']) : 'asc'; //If no order, default to asc
352 $result = strnatcasecmp($a->$orderby, $b->$orderby); //Determine sort order, case insensitive, natural order
353
354 return ($order === 'asc') ? $result : -$result; //Send final sort direction to usort
355 }
356
357 usort($data, 'usort_reorder');
358
359 /**
360 * Pagination.
361 */
362 $current_page = $this->get_pagenum();
363 $total_items = count($data);
364
365
366 /**
367 * The WP_List_Table class does not handle pagination for us, so we need
368 * to ensure that the data is trimmed to only the current page. We can use
369 * array_slice() to
370 */
371 $data = array_slice($data, (($current_page - 1) * $per_page), $per_page);
372
373 /**
374 * Now we can add the data to the items property, where it can be used by the rest of the class.
375 */
376 $this->items = $data;
377
378 /**
379 * We also have to register our pagination options & calculations.
380 */
381 $this->set_pagination_args([
382 'total_items' => $total_items, //calculate the total number of items
383 'per_page' => $per_page, //determine how many items to show on a page
384 'total_pages' => ceil($total_items / $per_page) //calculate the total number of pages
385 ]);
386 }
387
388
389 }
390