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 / post-tags-table.php

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

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