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 / class-tag-table.php

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

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