PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.50.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.50.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 / post-tags-table.php

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

379 lines 11.5 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 PostTags_List extends WP_List_Table
7 {
8 /** Class constructor */
9 public function __construct()
10 {
11
12 parent::__construct([
13 'singular' => esc_html__('Terms for Current Post', 'simple-tags'), //singular name of the listed records
14 'plural' => esc_html__('Terms for Current Post', 'simple-tags'), //plural name of the listed records
15 'ajax' => false //does this table support ajax?
16 ]);
17
18 }
19
20 /**
21 * Retrieve st_post_tags 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_post_tags()
29 {
30 return taxopress_get_posttags_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_posttags_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-posttags-tr'];
51 $id = 'st-posttags-' . 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 'embedded' => esc_html__('Automatic display', 'simple-tags'),
68 'shortcode' => esc_html__('Shortcode', '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 esc_html($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_post_tags_per_page', 20);
140
141 /**
142 * Fetch the data
143 */
144 $data = self::get_st_post_tags();
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(
167 $a[$orderby],
168 $b[$orderby]
169 ); //Determine sort order, case insensitive, natural order
170
171 return ($order === 'asc') ? $result : -$result; //Send final sort direction to usort
172 }
173
174 usort($data, 'usort_reorder');
175
176 /**
177 * Pagination.
178 */
179 $current_page = $this->get_pagenum();
180 $total_items = count($data);
181
182
183 /**
184 * The WP_List_Table class does not handle pagination for us, so we need
185 * to ensure that the data is trimmed to only the current page. We can use
186 * array_slice() to
187 */
188 $data = array_slice($data, (($current_page - 1) * $per_page), $per_page);
189
190 /**
191 * Now we can add the data to the items property, where it can be used by the rest of the class.
192 */
193 $this->items = $data;
194
195 /**
196 * We also have to register our pagination options & calculations.
197 */
198 $this->set_pagination_args([
199 'total_items' => $total_items, //calculate the total number of items
200 'per_page' => $per_page, //determine how many items to show on a page
201 'total_pages' => ceil($total_items / $per_page) //calculate the total number of pages
202 ]);
203 }
204
205 /**
206 * Determine if a given string contains a given substring.
207 *
208 * @param string $haystack
209 * @param string|array $needles
210 * @param bool $sensitive Use case sensitive search
211 *
212 * @return bool
213 */
214 public function str_contains($haystack, $needles, $sensitive = true)
215 {
216 foreach ((array)$needles as $needle) {
217 $function = $sensitive ? 'mb_strpos' : 'mb_stripos';
218 if ($needle !== '' && $function($haystack, $needle) !== false) {
219 return true;
220 }
221 }
222
223 return false;
224 }
225
226 /**
227 * Columns to make sortable.
228 *
229 * @return array
230 */
231 protected function get_sortable_columns()
232 {
233 $sortable_columns = [
234 'title' => ['title', true],
235 'taxonomy' => ['taxonomy', true],
236 ];
237
238 return $sortable_columns;
239 }
240
241 /**
242 * Generates and display row actions links for the list table.
243 *
244 * @param object $item The item being acted upon.
245 * @param string $column_name Current column name.
246 * @param string $primary Primary column name.
247 *
248 * @return string The row actions HTML, or an empty string if the current column is the primary column.
249 */
250 protected function handle_row_actions($item, $column_name, $primary)
251 {
252 //Build row actions
253 $actions = [
254 'edit' => sprintf(
255 '<a href="%s">%s</a>',
256 add_query_arg(
257 [
258 'page' => 'st_post_tags',
259 'add' => 'new_item',
260 'action' => 'edit',
261 'taxopress_posttags' => $item['ID'],
262 ],
263 admin_url('admin.php')
264 ),
265 esc_html__('Edit', 'simple-tags')
266 ),
267 'delete' => sprintf(
268 '<a href="%s" class="delete-posttags">%s</a>',
269 add_query_arg(
270 [
271 'page' => 'st_post_tags',
272 'action' => 'taxopress-delete-posttags',
273 'taxopress_posttags' => esc_attr($item['ID']),
274 '_wpnonce' => wp_create_nonce('posttags-action-request-nonce')
275 ],
276 admin_url('admin.php')
277 ),
278 esc_html__('Delete', 'simple-tags')
279 ),
280 ];
281
282 $actions = apply_filters('taxopress_posttags_row_actions', $actions, $item);
283 return $column_name === $primary ? $this->row_actions($actions, false) : '';
284 }
285
286 /**
287 * Method for title column
288 *
289 * @param array $item
290 *
291 * @return string
292 */
293 protected function column_title($item)
294 {
295 $title = sprintf(
296 '<a href="%1$s"><strong><span class="row-title">%2$s</span></strong></a>',
297 add_query_arg(
298 [
299 'page' => 'st_post_tags',
300 'add' => 'new_item',
301 'action' => 'edit',
302 'taxopress_posttags' => $item['ID'],
303 ],
304 admin_url('admin.php')
305 ),
306 esc_html($item['title'])
307 );
308
309 return $title;
310 }
311
312 /**
313 * Method for taxonomy column
314 *
315 * @param array $item
316 *
317 * @return string
318 */
319 protected function column_taxonomy($item)
320 {
321 $taxonomy = get_taxonomy($item['taxonomy']);
322
323 return $taxonomy->labels->name;
324 }
325
326 /**
327 * The action column
328 *
329 * @param $item
330 *
331 * @return string
332 */
333 protected function column_embedded($item)
334 {
335 $embedded = (isset($item['embedded']) && is_array($item['embedded']) && count($item['embedded']) > 0) ? $item['embedded'] : false;
336 if ($embedded) {
337 $args = apply_filters('taxopress_attach_post_types_to_taxonomy', ['public' => true]);
338 if (!is_array($args)) {
339 $args = ['public' => true];
340 }
341 $output = 'objects'; // Or objects.
342 $post_types = apply_filters('taxopress_get_post_types_for_taxonomies', get_post_types($args, $output), $args, $output);
343
344 $result_array = [];
345 $embedded_options = [
346 'homeonly' => esc_attr__('Homepage', 'simple-tags'),
347 'blogonly' => esc_attr__('Blog display', 'simple-tags'),
348 'singleonly' => esc_attr__('Single post display', 'simple-tags'),
349 'feed' => esc_attr__('RSS feed', 'simple-tags'),
350 ];
351 foreach ($post_types as $post_type) {
352 $embedded_options[$post_type->name] = $post_type->label;
353 }
354 foreach ($embedded as $location) {
355 $result_array[] = isset($embedded_options[$location]) ? $embedded_options[$location] : '';
356 }
357 $result = join(', ', $result_array);
358 } else {
359 $result = esc_attr__('No', 'simple-tags');
360 }
361 return $result;
362 }
363
364 /**
365 * The action column
366 *
367 * @param $item
368 *
369 * @return string
370 */
371 protected function column_shortcode($item)
372 {
373
374 return '<input readonly type="text" value=\'[taxopress_postterms id="' . $item['ID'] . '"]\' />';
375 }
376
377
378 }
379