PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.6.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.6.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 / related-posts-table.php

related-posts-table.php in Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms 3.6.0, at inc/related-posts-table.php

416 lines 12.8 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 RelatedPosts_List extends WP_List_Table
7 {
8
9 /** Class constructor */
10 public function __construct()
11 {
12
13 parent::__construct([
14 'singular' => esc_html__('Related Post', 'simple-tags'), //singular name of the listed records
15 'plural' => esc_html__('Related Posts', 'simple-tags'), //plural name of the listed records
16 'ajax' => false //does this table support ajax?
17 ]);
18
19 }
20
21 /**
22 * Retrieve st_related_posts data from the database
23 *
24 * @param int $per_page
25 * @param int $page_number
26 *
27 * @return mixed
28 */
29 public static function get_st_related_posts()
30 {
31 return taxopress_get_relatedpost_data();
32 }
33
34 /**
35 * Returns the count of records in the database.
36 *
37 * @return null|string
38 */
39 public static function record_count()
40 {
41 return count(taxopress_get_relatedpost_data());
42 }
43
44 /**
45 * Show single row item
46 *
47 * @param array $item
48 */
49 public function single_row($item)
50 {
51 $class = ['st-relatedpost-tr'];
52 $id = 'st-relatedpost-' . md5($item['ID']);
53 echo sprintf('<tr id="%s" class="%s">', esc_attr($id), esc_attr(implode(' ', $class)));
54 $this->single_row_columns($item);
55 echo '</tr>';
56 }
57
58 /**
59 * Associative array of columns
60 *
61 * @return array
62 */
63 function get_columns()
64 {
65 $columns = [
66 'title' => esc_html__('Title', 'simple-tags'),
67 'taxonomy' => esc_html__('Taxonomy', 'simple-tags'),
68 'post_type' => esc_html__('Post Type', '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($_REQUEST['orderby'])) . '" />';
113 }
114 if (!empty($_REQUEST['order'])) {
115 echo '<input type="hidden" name="order" value="' . esc_attr(sanitize_text_field($_REQUEST['order'])) . '" />';
116 }
117 if (!empty($_REQUEST['page'])) {
118 echo '<input type="hidden" name="page" value="' . esc_attr(sanitize_text_field($_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_related_posts_per_page', 20);
142
143 /**
144 * Fetch the data
145 */
146 $data = self::get_st_related_posts();
147
148 /**
149 * Handle search
150 */
151 if ((!empty($_REQUEST['s'])) && $search = sanitize_text_field($_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($_REQUEST['orderby']) : 'ID'; //If no sort, default to role
167 $order = (!empty($_REQUEST['order'])) ? sanitize_text_field($_REQUEST['order']) : 'desc'; //If no order, default to asc
168 $result = strnatcasecmp($a[$orderby],
169 $b[$orderby]); //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_related_posts',
259 'add' => 'new_item',
260 'action' => 'edit',
261 'taxopress_relatedposts' => $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-relatedpost">%s</a>',
269 add_query_arg([
270 'page' => 'st_related_posts',
271 'action' => 'taxopress-delete-relatedpost',
272 'taxopress_relatedposts' => esc_attr($item['ID']),
273 '_wpnonce' => wp_create_nonce('relatedpost-action-request-nonce')
274 ],
275 admin_url('admin.php')),
276 esc_html__('Delete', 'simple-tags')
277 ),
278 ];
279
280 return $column_name === $primary ? $this->row_actions($actions, false) : '';
281 }
282
283 /**
284 * Method for title column
285 *
286 * @param array $item
287 *
288 * @return string
289 */
290 protected function column_title($item)
291 {
292 $title = sprintf(
293 '<a href="%1$s"><strong><span class="row-title">%2$s</span></strong></a>',
294 add_query_arg(
295 [
296 'page' => 'st_related_posts',
297 'add' => 'new_item',
298 'action' => 'edit',
299 'taxopress_relatedposts' => $item['ID'],
300 ],
301 admin_url('admin.php')
302 ),
303 esc_html($item['title'])
304 );
305
306 return $title;
307 }
308
309 /**
310 * Method for taxonomy column
311 *
312 * @param array $item
313 *
314 * @return string
315 */
316 protected function column_taxonomy($item)
317 {
318 $taxonomy = get_taxonomy($item['taxonomy']);
319
320 return $taxonomy->labels->name;
321 }
322
323 /**
324 * Method for post_type column
325 *
326 * @param array $item
327 *
328 * @return string
329 */
330 protected function column_post_type($item)
331 {
332 $title = '';
333 if (isset($item['post_type']) && !empty(trim($item['post_type']))) {
334
335 if ($item['post_type'] === 'st_current_posttype') {
336 $title = esc_html__('Current post type', 'simple-tags');
337 } elseif ($item['post_type'] === 'st_all_posttype') {
338 $title = esc_html__('All', 'simple-tags');
339 } else {
340 $post_type = get_post_type_object($item['post_type']);
341 if (is_object($post_type)) {
342 $title = sprintf(
343 '<a href="%1$s"><strong><span class="row-title">%2$s</span></strong></a>',
344 add_query_arg(
345 [
346 'post_type' => $item['post_type'],
347 ],
348 admin_url('edit.php')
349 ),
350 esc_html($post_type->label)
351 );
352 }
353 }
354 } else {
355 $title = esc_html__('All', 'simple-tags');
356 }
357
358 return $title;
359 }
360
361 /**
362 * The action column
363 *
364 * @param $item
365 *
366 * @return string
367 */
368 protected function column_embedded($item)
369 {
370 $embedded = (isset($item['embedded']) && is_array($item['embedded']) && count($item['embedded']) > 0) ? $item['embedded'] : false;
371 if ($embedded) {
372 $args = apply_filters('taxopress_attach_post_types_to_taxonomy', ['public' => true]);
373 if (!is_array($args)) {
374 $args = ['public' => true];
375 }
376 $output = 'objects'; // Or objects.
377 $post_types = apply_filters('taxopress_get_post_types_for_taxonomies', get_post_types($args, $output),
378 $args, $output);
379
380 $result_array = [];
381 $embedded_options = [
382 'homeonly' => esc_html__('Homepage', 'simple-tags'),
383 'blogonly' => esc_html__('Blog display', 'simple-tags'),
384 'singleonly' => esc_html__('Single post display', 'simple-tags'),
385 'feed' => esc_html__('RSS feed', 'simple-tags'),
386 ];
387 foreach ($post_types as $post_type) {
388 $embedded_options[$post_type->name] = $post_type->label;
389 }
390 foreach ($embedded as $location) {
391 $result_array[] = isset($embedded_options[$location]) ? $embedded_options[$location] : '';
392 }
393 $result = join(', ', $result_array);
394 } else {
395 $result = esc_html__('No', 'simple-tags');
396 }
397
398 return $result;
399 }
400
401 /**
402 * The action column
403 *
404 * @param $item
405 *
406 * @return string
407 */
408 protected function column_shortcode($item)
409 {
410
411 return '<input type="text" value=\'[taxopress_relatedposts id="' . $item['ID'] . '"]\' />';
412 }
413
414
415 }
416