PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.5.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.5.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 / autolinks-table.php

autolinks-table.php in Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms 3.5.0, at inc/autolinks-table.php

395 lines 11.9 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 Autolinks_List extends WP_List_Table
7 {
8
9 /** Class constructor */
10 public function __construct()
11 {
12
13 parent::__construct([
14 'singular' => esc_html__('Auto Link', 'simple-tags'), //singular name of the listed records
15 'plural' => esc_html__('Auto Links', 'simple-tags'), //plural name of the listed records
16 'ajax' => false //does this table support ajax?
17 ]);
18
19 }
20
21 /**
22 * Retrieve st_autolinks 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_autolinks()
30 {
31 return taxopress_get_autolink_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_autolink_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-autolink-tr'];
52 $id = 'st-autolink-' . 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' => __('Title', 'simple-tags'),
67 'taxonomy' => __('Taxonomy', 'simple-tags'),
68 'embedded' => __('Auto Link Post type', 'simple-tags'),
69 'autolink_display' => __('Auto Link areas', '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 if (empty($_REQUEST['s']) && !$this->has_items()) {
105 return;
106 }
107
108 $input_id = $input_id . '-search-input';
109
110 if (!empty($_REQUEST['orderby'])) {
111 echo '<input type="hidden" name="orderby" value="' . esc_attr(sanitize_text_field($_REQUEST['orderby'])) . '" />';
112 }
113 if (!empty($_REQUEST['order'])) {
114 echo '<input type="hidden" name="order" value="' . esc_attr(sanitize_text_field($_REQUEST['order'])) . '" />';
115 }
116 if (!empty($_REQUEST['page'])) {
117 echo '<input type="hidden" name="page" value="' . esc_attr(sanitize_text_field($_REQUEST['page'])) . '" />';
118 }
119 ?>
120 <p class="search-box">
121 <label class="screen-reader-text" for="<?php echo esc_attr($input_id); ?>"><?php echo esc_html($text); ?>:</label>
122 <input type="search" id="<?php echo esc_attr($input_id); ?>" name="s"
123 value="<?php _admin_search_query(); ?>"/>
124 <?php submit_button($text, '', '', false, ['id' => 'search-submit']); ?>
125 </p>
126 <?php
127 }
128
129 /**
130 * Sets up the items (roles) to list.
131 */
132 public function prepare_items()
133 {
134
135 $this->_column_headers = $this->get_column_info();
136
137 /**
138 * First, lets decide how many records per page to show
139 */
140 $per_page = $this->get_items_per_page('st_autolinks_per_page', 20);
141
142 /**
143 * Fetch the data
144 */
145 $data = self::get_st_autolinks();
146
147 /**
148 * Handle search
149 */
150 if ((!empty($_REQUEST['s'])) && $search = sanitize_text_field($_REQUEST['s'])) {
151 $data_filtered = [];
152 foreach ($data as $item) {
153 if ($this->str_contains($item['title'], $search, false)) {
154 $data_filtered[] = $item;
155 }
156 }
157 $data = $data_filtered;
158 }
159
160 /**
161 * This checks for sorting input and sorts the data in our array accordingly.
162 */
163 function usort_reorder($a, $b)
164 {
165 $orderby = (!empty($_REQUEST['orderby'])) ? sanitize_text_field($_REQUEST['orderby']) : 'ID'; //If no sort, default to role
166 $order = (!empty($_REQUEST['order'])) ? sanitize_text_field($_REQUEST['order']) : 'desc'; //If no order, default to asc
167 $result = strnatcasecmp($a[$orderby],
168 $b[$orderby]); //Determine sort order, case insensitive, natural order
169
170 return ($order === 'asc') ? $result : -$result; //Send final sort direction to usort
171 }
172
173 usort($data, 'usort_reorder');
174
175 /**
176 * Pagination.
177 */
178 $current_page = $this->get_pagenum();
179 $total_items = count($data);
180
181
182 /**
183 * The WP_List_Table class does not handle pagination for us, so we need
184 * to ensure that the data is trimmed to only the current page. We can use
185 * array_slice() to
186 */
187 $data = array_slice($data, (($current_page - 1) * $per_page), $per_page);
188
189 /**
190 * Now we can add the data to the items property, where it can be used by the rest of the class.
191 */
192 $this->items = $data;
193
194 /**
195 * We also have to register our pagination options & calculations.
196 */
197 $this->set_pagination_args([
198 'total_items' => $total_items, //calculate the total number of items
199 'per_page' => $per_page, //determine how many items to show on a page
200 'total_pages' => ceil($total_items / $per_page) //calculate the total number of pages
201 ]);
202 }
203
204 /**
205 * Determine if a given string contains a given substring.
206 *
207 * @param string $haystack
208 * @param string|array $needles
209 * @param bool $sensitive Use case sensitive search
210 *
211 * @return bool
212 */
213 public function str_contains($haystack, $needles, $sensitive = true)
214 {
215 foreach ((array)$needles as $needle) {
216 $function = $sensitive ? 'mb_strpos' : 'mb_stripos';
217 if ($needle !== '' && $function($haystack, $needle) !== false) {
218 return true;
219 }
220 }
221
222 return false;
223 }
224
225 /**
226 * Columns to make sortable.
227 *
228 * @return array
229 */
230 protected function get_sortable_columns()
231 {
232 $sortable_columns = [
233 'title' => ['title', true],
234 'taxonomy' => ['taxonomy', true],
235 ];
236
237 return $sortable_columns;
238 }
239
240 /**
241 * Generates and display row actions links for the list table.
242 *
243 * @param object $item The item being acted upon.
244 * @param string $column_name Current column name.
245 * @param string $primary Primary column name.
246 *
247 * @return string The row actions HTML, or an empty string if the current column is the primary column.
248 */
249 protected function handle_row_actions($item, $column_name, $primary)
250 {
251 //Build row actions
252 $actions = [
253 'edit' => sprintf(
254 '<a href="%s">%s</a>',
255 add_query_arg(
256 [
257 'page' => 'st_autolinks',
258 'add' => 'new_item',
259 'action' => 'edit',
260 'taxopress_autolinks' => $item['ID'],
261 ],
262 admin_url('admin.php')
263 ),
264 __('Edit', 'simple-tags')
265 ),
266 'delete' => sprintf(
267 '<a href="%s" class="delete-autolink">%s</a>',
268 add_query_arg([
269 'page' => 'st_autolinks',
270 'action' => 'taxopress-delete-autolink',
271 'taxopress_autolinks' => esc_attr($item['ID']),
272 '_wpnonce' => wp_create_nonce('autolink-action-request-nonce')
273 ],
274 admin_url('admin.php')),
275 __('Delete', 'simple-tags')
276 ),
277 ];
278
279 return $column_name === $primary ? $this->row_actions($actions, false) : '';
280 }
281
282 /**
283 * Method for title column
284 *
285 * @param array $item
286 *
287 * @return string
288 */
289 protected function column_title($item)
290 {
291 $title = sprintf(
292 '<a href="%1$s"><strong><span class="row-title">%2$s</span></strong></a>',
293 add_query_arg(
294 [
295 'page' => 'st_autolinks',
296 'add' => 'new_item',
297 'action' => 'edit',
298 'taxopress_autolinks' => $item['ID'],
299 ],
300 admin_url('admin.php')
301 ),
302 esc_html($item['title'])
303 );
304
305 return $title;
306 }
307
308 /**
309 * Method for taxonomy column
310 *
311 * @param array $item
312 *
313 * @return string
314 */
315 protected function column_taxonomy($item)
316 {
317 $taxonomy = get_taxonomy($item['taxonomy']);
318
319 return $taxonomy->labels->name;
320 }
321
322 /**
323 * The action column
324 *
325 * @param $item
326 *
327 * @return string
328 */
329 protected function column_embedded($item)
330 {
331 $embedded = (isset($item['embedded']) && is_array($item['embedded']) && count($item['embedded']) > 0) ? $item['embedded'] : false;
332 if ($embedded) {
333 $args = apply_filters('taxopress_attach_post_types_to_taxonomy', ['public' => true]);
334 if (!is_array($args)) {
335 $args = ['public' => true];
336 }
337 $output = 'objects'; // Or objects.
338 $post_types = apply_filters('taxopress_get_post_types_for_taxonomies', get_post_types($args, $output),
339 $args, $output);
340
341 $result_array = [];
342 $embedded_options = [
343 'homeonly' => esc_attr__('Homepage', 'simple-tags'),
344 'blogonly' => esc_attr__('Blog display', 'simple-tags'),
345 'singleonly' => esc_attr__('Single post display', 'simple-tags'),
346 'feed' => esc_attr__('RSS feed', 'simple-tags'),
347 ];
348 foreach ($post_types as $post_type) {
349 $embedded_options[$post_type->name] = $post_type->label;
350 }
351 foreach ($embedded as $location) {
352 $result_array[] = isset($embedded_options[$location]) ? $embedded_options[$location] : '';
353 }
354 $result = join(', ', $result_array);
355 } else {
356 $result = esc_attr__('None', 'simple-tags');
357 }
358
359 return $result;
360 }
361
362 /**
363 * The action column
364 *
365 * @param $item
366 *
367 * @return string
368 */
369 protected function column_autolink_display($item)
370 {
371 $autolink_display_options = [
372 'post_content' => esc_attr__('Post Content', 'simple-tags'),
373 'post_title' => esc_attr__('Post Title', 'simple-tags'),
374 'posts' => esc_attr__('Post Content and Title', 'simple-tags'),
375 ];
376
377 return $autolink_display_options[$item['autolink_display']];
378 }
379
380 /**
381 * The action column
382 *
383 * @param $item
384 *
385 * @return string
386 */
387 protected function column_shortcode($item)
388 {
389
390 return '<input type="text" value=\'[taxopress_autolinks id="' . $item['ID'] . '"]\' />';
391 }
392
393
394 }
395