PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / trunk
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms vtrunk
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 / tag-clouds-table.php

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

401 lines 12.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!class_exists('WP_List_Table')) {
4 require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
5 }
6
7 class TagClouds_List extends WP_List_Table
8 {
9 /** Class constructor */
10 public function __construct()
11 {
12
13 parent::__construct([
14 'singular' => esc_html__('Terms Display', 'simple-tags'), //singular name of the listed records
15 'plural' => esc_html__('Terms Display', 'simple-tags'), //plural name of the listed records
16 'ajax' => false //does this table support ajax?
17 ]);
18 }
19
20 /**
21 * Retrieve st_terms_display data from the database
22 *
23 * @param int $per_page
24 * @param int $page_number
25 *
26 * @return mixed
27 */
28 public static function get_st_terms_display()
29 {
30 return taxopress_get_tagcloud_data();
31 }
32
33 /**
34 * Returns the count of records in the database.
35 *
36 * @return null|string
37 */
38 public static function record_count()
39 {
40 return count(taxopress_get_tagcloud_data());
41 }
42
43 /**
44 * Show single row item
45 *
46 * @param array $item
47 */
48 public function single_row($item)
49 {
50 $class = ['st-cloudtag-tr'];
51 $id = 'st-cloudtag-' . md5($item['ID']);
52 echo sprintf('<tr id="%s" class="%s">', esc_attr($id), esc_attr(implode(' ', $class)));
53 $this->single_row_columns($item);
54 echo '</tr>';
55 }
56
57 /**
58 * Associative array of columns
59 *
60 * @return array
61 */
62 public function get_columns()
63 {
64 $columns = [
65 'title' => esc_html__('Title', 'simple-tags'),
66 'taxonomy' => esc_html__('Taxonomy', 'simple-tags'),
67 'post_type' => esc_html__('Post Type', 'simple-tags'),
68 'format' => esc_html__('Display format', 'simple-tags'),
69 'shortcode' => esc_html__('Shortcode', '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 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading GET parameters for search/filter display, no state change
105 if (empty($_REQUEST['s']) && !$this->has_items()) {
106 return;
107 }
108
109 $input_id = $input_id . '-search-input';
110
111 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading GET parameters for hidden form fields, preserving filter state
112 if (!empty($_REQUEST['orderby'])) {
113 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
114 echo '<input type="hidden" name="orderby" value="' . esc_attr(sanitize_text_field(wp_unslash($_REQUEST['orderby']))) . '" />';
115 }
116 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading GET parameters for hidden form fields, preserving sort order
117 if (!empty($_REQUEST['order'])) {
118 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
119 echo '<input type="hidden" name="order" value="' . esc_attr(sanitize_text_field(wp_unslash($_REQUEST['order']))) . '" />';
120 }
121 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading GET parameters for hidden form fields, preserving pagination
122 if (!empty($_REQUEST['page'])) {
123 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
124 echo '<input type="hidden" name="page" value="' . esc_attr(sanitize_text_field(wp_unslash($_REQUEST['page']))) . '" />';
125 }
126 ?>
127 <p class="search-box">
128 <label class="screen-reader-text" for="<?php echo esc_attr($input_id); ?>"><?php echo esc_html($text); ?>:</label>
129 <input type="search" id="<?php echo esc_attr($input_id); ?>" name="s"
130 value="<?php _admin_search_query(); ?>"/>
131 <?php submit_button($text, '', '', false, ['id' => 'search-submit']); ?>
132 </p>
133 <?php
134 }
135
136 /**
137 * Sets up the items (roles) to list.
138 */
139 public function prepare_items()
140 {
141
142 $this->_column_headers = $this->get_column_info();
143
144 /**
145 * First, lets decide how many records per page to show
146 */
147 $per_page = $this->get_items_per_page('st_terms_display_per_page', 20);
148
149 /**
150 * Fetch the data
151 */
152 $data = self::get_st_terms_display();
153
154 /**
155 * Handle search
156 */
157 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading GET parameter for search filtering display, no state change
158 if ((!empty($_REQUEST['s'])) && $search = sanitize_text_field(wp_unslash($_REQUEST['s']))) {
159 $data_filtered = [];
160 foreach ($data as $item) {
161 if ($this->str_contains($item['title'], $search, false)) {
162 $data_filtered[] = $item;
163 }
164 }
165 $data = $data_filtered;
166 }
167
168 /**
169 * This checks for sorting input and sorts the data in our array accordingly.
170 */
171 function usort_reorder($a, $b)
172 {
173 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading GET parameters for sorting display, no state change
174 $orderby = (!empty($_REQUEST['orderby'])) ? sanitize_text_field(wp_unslash($_REQUEST['orderby'])) : 'ID'; //If no sort, default to role
175 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading GET parameter for sort order display, no state change
176 $order = (!empty($_REQUEST['order'])) ? sanitize_text_field(wp_unslash($_REQUEST['order'])) : 'desc'; //If no order, default to asc
177 $result = strnatcasecmp(
178 $a[$orderby],
179 $b[$orderby]
180 ); //Determine sort order, case insensitive, natural order
181
182 return ($order === 'asc') ? $result : -$result; //Send final sort direction to usort
183 }
184
185 usort($data, 'usort_reorder');
186
187 /**
188 * Pagination.
189 */
190 $current_page = $this->get_pagenum();
191 $total_items = count($data);
192
193
194 /**
195 * The WP_List_Table class does not handle pagination for us, so we need
196 * to ensure that the data is trimmed to only the current page. We can use
197 * array_slice() to
198 */
199 $data = array_slice($data, (($current_page - 1) * $per_page), $per_page);
200
201 /**
202 * Now we can add the data to the items property, where it can be used by the rest of the class.
203 */
204 $this->items = $data;
205
206 /**
207 * We also have to register our pagination options & calculations.
208 */
209 $this->set_pagination_args([
210 'total_items' => $total_items, //calculate the total number of items
211 'per_page' => $per_page, //determine how many items to show on a page
212 'total_pages' => ceil($total_items / $per_page) //calculate the total number of pages
213 ]);
214 }
215
216 /**
217 * Determine if a given string contains a given substring.
218 *
219 * @param string $haystack
220 * @param string|array $needles
221 * @param bool $sensitive Use case sensitive search
222 *
223 * @return bool
224 */
225 public function str_contains($haystack, $needles, $sensitive = true)
226 {
227 foreach ((array)$needles as $needle) {
228 $function = $sensitive ? 'mb_strpos' : 'mb_stripos';
229 if ($needle !== '' && $function($haystack, $needle) !== false) {
230 return true;
231 }
232 }
233
234 return false;
235 }
236
237 /**
238 * Columns to make sortable.
239 *
240 * @return array
241 */
242 protected function get_sortable_columns()
243 {
244 $sortable_columns = [
245 'title' => ['title', true],
246 'taxonomy' => ['taxonomy', true],
247 ];
248
249 return $sortable_columns;
250 }
251
252 /**
253 * Generates and display row actions links for the list table.
254 *
255 * @param object $item The item being acted upon.
256 * @param string $column_name Current column name.
257 * @param string $primary Primary column name.
258 *
259 * @return string The row actions HTML, or an empty string if the current column is the primary column.
260 */
261 protected function handle_row_actions($item, $column_name, $primary)
262 {
263 //Build row actions
264 $actions = [
265 'edit' => sprintf(
266 '<a href="%s">%s</a>',
267 add_query_arg(
268 [
269 'page' => 'st_terms_display',
270 'add' => 'new_item',
271 'action' => 'edit',
272 'taxopress_termsdisplay' => $item['ID'],
273 ],
274 admin_url('admin.php')
275 ),
276 esc_html__('Edit', 'simple-tags')
277 ),
278 'delete' => sprintf(
279 '<a href="%s" class="delete-tagcloud">%s</a>',
280 add_query_arg(
281 [
282 'page' => 'st_terms_display',
283 'action' => 'taxopress-delete-tagcloud',
284 'taxopress_termsdisplay' => esc_attr($item['ID']),
285 '_wpnonce' => wp_create_nonce('tagcloud-action-request-nonce')
286 ],
287 admin_url('admin.php')
288 ),
289 esc_html__('Delete', 'simple-tags')
290 ),
291 ];
292
293 $actions = apply_filters('taxopress_tagclouds_row_actions', $actions, $item);
294 return $column_name === $primary ? $this->row_actions($actions, false) : '';
295 }
296
297 /**
298 * Method for title column
299 *
300 * @param array $item
301 *
302 * @return string
303 */
304 protected function column_title($item)
305 {
306 $title = sprintf(
307 '<a href="%1$s"><strong><span class="row-title">%2$s</span></strong></a>',
308 add_query_arg(
309 [
310 'page' => 'st_terms_display',
311 'add' => 'new_item',
312 'action' => 'edit',
313 'taxopress_termsdisplay' => $item['ID'],
314 ],
315 admin_url('admin.php')
316 ),
317 esc_html($item['title'])
318 );
319
320 return $title;
321 }
322
323 /**
324 * Method for taxonomy column
325 *
326 * @param array $item
327 *
328 * @return string
329 */
330 protected function column_taxonomy($item)
331 {
332 $taxonomy = get_taxonomy($item['taxonomy']);
333
334 return $taxonomy->labels->name;
335 }
336
337 /**
338 * Method for post_type column
339 *
340 * @param array $item
341 *
342 * @return string
343 */
344 protected function column_post_type($item)
345 {
346 if (isset($item['post_type']) && !empty(trim($item['post_type']))) {
347 $post_type = get_post_type_object($item['post_type']);
348 $title = sprintf(
349 '<a href="%1$s"><strong><span class="row-title">%2$s</span></strong></a>',
350 add_query_arg(
351 [
352 'post_type' => $item['post_type'],
353 ],
354 admin_url('edit.php')
355 ),
356 esc_html($post_type->label)
357 );
358 } else {
359 $title = esc_html__('All', 'simple-tags');
360 }
361
362 return $title;
363 }
364
365 /**
366 * The action column
367 *
368 * @param $item
369 *
370 * @return string
371 */
372 protected function column_format($item)
373 {
374
375 $days_options = [
376 'flat' => esc_attr__('Cloud', 'simple-tags'),
377 'list' => esc_attr__('Unordered List (UL/LI)', 'simple-tags'),
378 'ol' => esc_attr__('Ordered List (OL/LI)', 'simple-tags'),
379 'comma' => esc_attr__('WordPress Default', 'simple-tags'),
380 'table' => esc_attr__('Table List', 'simple-tags'),
381 'border' => esc_attr__('Border Cloud', 'simple-tags'),
382 'parent/child' => esc_attr__('Parent / Child', 'simple-tags'),
383 ];
384
385 return $days_options[$item['format']];
386 }
387
388 /**
389 * The action column
390 *
391 * @param $item
392 *
393 * @return string
394 */
395 protected function column_shortcode($item)
396 {
397
398 return '<input readonly type="text" value=\'[taxopress_termsdisplay id="' . $item['ID'] . '"]\' />';
399 }
400 }
401