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

posts.php in Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms 3.51.0, at inc/posts.php

266 lines 7.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class SimpleTags_Posts
4 {
5 public const MENU_SLUG = 'st_options';
6
7 // class instance
8 public static $instance;
9
10 // WP_List_Table object
11 public $posts_table;
12
13 /**
14 * Constructor
15 *
16 * @return void
17 * @author Olatechpro
18 */
19 public function __construct()
20 {
21
22 global $hook_suffix; //avoid warning outputs
23 if (!isset($hook_suffix)) {
24 $hook_suffix = '';
25 }
26
27 add_filter('set-screen-option', [__CLASS__, 'set_screen'], 10, 3);
28 // Admin menu
29 add_action('admin_menu', [$this, 'admin_menu']);
30 // Javascript
31 add_action('admin_enqueue_scripts', [__CLASS__, 'admin_enqueue_scripts'], 11);
32
33 add_action('wp_ajax_taxopress_filter_term_search', [__CLASS__, 'handle_filter_term_search']);
34 }
35
36 /**
37 * Init somes JS and CSS need for this feature
38 *
39 * @return void
40 * @author Olatechpro
41 */
42 public static function admin_enqueue_scripts()
43 {
44
45 // add JS for manage click tags
46 if (isset($_GET['page']) && $_GET['page'] == 'st_posts') {
47 wp_enqueue_style('st-taxonomies-css');
48 wp_enqueue_script('admin-tags');
49 wp_enqueue_script('inline-edit-tax');
50 }
51 }
52
53 /**
54 * Handle an ajax request to search filter available terms
55 */
56 public static function handle_filter_term_search()
57 {
58 header('Content-Type: application/javascript');
59
60 if (empty($_GET['nonce'])
61 || !wp_verify_nonce(sanitize_key($_GET['nonce']), 'taxopress-term-search')
62 ) {
63 wp_send_json_error(null, 403);
64 }
65
66 if (! current_user_can('simple_tags')) {
67 wp_send_json_error(null, 403);
68 }
69
70 $search = !empty($_GET['q']) ? sanitize_text_field($_GET['q']) : '';
71 $field = !empty($_GET['field']) ? sanitize_text_field($_GET['field']) : 'slug';
72 $filter_format = SimpleTags_Plugin::get_option_value('post_terms_filter_format');
73 $taxonomy_type = SimpleTags_Plugin::get_option_value('post_terms_taxonomy_type');
74
75 $terms = self::get_possible_terms_for_search($search, $taxonomy_type);
76 $results = [];
77
78 foreach ($terms as $term) {
79 $text = $term->name;
80 if ($filter_format === 'term_name_taxonomy_name') {
81 $taxonomy = get_taxonomy($term->taxonomy);
82 $text .= ' ('. $taxonomy->labels->singular_name .')';
83 } elseif ($filter_format === 'term_name_taxonomy_slug') {
84 $text .= ' ('. $term->taxonomy .')';
85 }
86 $results[] = [
87 'id' => $term->$field,
88 'text' => $text,
89 ];
90 }
91
92 $response = [
93 'results' => $results,
94 ];
95 echo wp_json_encode($response);
96 exit;
97 }
98
99 /**
100 * Get the possible terms for a given search query.
101 *
102 * @param string $search Search query.
103 *
104 * @return object
105 */
106 public static function get_possible_terms_for_search($search, $taxonomy_type = 'public')
107 {
108 if ($taxonomy_type === 'public') {
109 $taxonomies = array_keys(get_taxonomies(['public' => true]));
110 } elseif ($taxonomy_type === 'private') {
111 $taxonomies = array_keys(get_taxonomies(['public' => false]));
112 } else {
113 $taxonomies = array_keys(get_taxonomies());
114 }
115
116 $term_args = [
117 'taxonomy' => $taxonomies,
118 'hide_empty' => true,
119 'number' => apply_filters('taxopress_terms_search_result_limit', 20),
120 'order_by' => 'name',
121 ];
122
123 if (!empty($search)) {
124 $search = str_replace(['\"', "\'"], '', $search);
125 $term_args['search'] = $search;
126 }
127
128 $terms = get_terms($term_args);
129
130 return $terms;
131 }
132
133
134
135 public static function set_screen($status, $option, $value)
136 {
137 return $value;
138 }
139
140 /** Singleton instance */
141 public static function get_instance()
142 {
143 if (!isset(self::$instance)) {
144 self::$instance = new self();
145 }
146
147 return self::$instance;
148 }
149
150 /**
151 * Add WP admin menu for Tags
152 *
153 * @return void
154 * @author Olatechpro
155 */
156 public function admin_menu()
157 {
158 $hook = add_submenu_page(
159 self::MENU_SLUG,
160 esc_html__('Posts', 'simple-tags'),
161 esc_html__('Posts', 'simple-tags'),
162 'simple_tags',
163 'st_posts',
164 [
165 $this,
166 'page_manage_posts',
167 ]
168 );
169
170 if (taxopress_is_screen_main_page()) {
171 add_action("load-$hook", [$this, 'screen_option']);
172 }
173 }
174
175 /**
176 * Screen options
177 */
178 public function screen_option()
179 {
180
181 $option = 'per_page';
182 $args = [
183 'label' => esc_html__('Number of items per page', 'simple-tags'),
184 'default' => 20,
185 'option' => 'st_posts_per_page'
186 ];
187
188 add_screen_option($option, $args);
189
190 $this->posts_table = new Taxopress_Posts_List();
191 }
192
193 /**
194 * Method for build the page HTML manage tags
195 *
196 * @return void
197 * @author Olatechpro
198 */
199 public function page_manage_posts()
200 {
201 // Default order
202 if (!isset($_GET['order'])) {
203 $_GET['order'] = 'name-asc';
204 }
205
206 settings_errors(__CLASS__);
207
208 ?>
209 <div class="wrap st_wrap st-manage-taxonomies-page manage-taxopress-posts">
210
211 <div id="">
212 <h1 class="wp-heading-inline"><?php esc_html_e('Posts', 'simple-tags'); ?></h1>
213 <div class="taxopress-description">
214 <?php esc_html_e('This feature allows you to search for terms and see all the posts attached to that term.', 'simple-tags'); ?>
215 </div>
216
217
218 <?php
219 if (isset($_REQUEST['s']) && $search = esc_attr(sanitize_text_field(wp_unslash($_REQUEST['s'])))) {
220 echo '<span class="subtitle__">';
221 printf(
222 /* translators: %s: Search query. */
223 esc_html__('Search results for: %s'),
224 '<strong>' . esc_html($search) . '</strong>'
225 );
226 echo '</span>';
227 }
228 ?>
229 <?php
230
231 //the posts table instance
232 $this->posts_table->prepare_items();
233 ?>
234
235
236 <hr class="wp-header-end">
237 <div id="ajax-response"></div>
238 <form class="search-form wp-clearfix st-taxonomies-search-form" method="get">
239 <?php $this->posts_table->search_box(esc_html__('Search Posts', 'simple-tags'), 'post'); ?>
240 </form>
241 <div class="clear"></div>
242
243 <div id="col-container" class="wp-clearfix">
244
245 <div class="col-wrap">
246 <form action="<?php echo esc_url(add_query_arg('', '')); ?>" method="post">
247 <?php $this->posts_table->display(); //Display the table
248 ?>
249 </form>
250 <div class="form-wrap edit-post-notes">
251 <p><?php esc_html__('Description here.', 'simple-tags') ?></p>
252 </div>
253 </div>
254
255
256 </div>
257
258
259 </div>
260 <?php $this->posts_table->inline_edit(); ?>
261 <?php SimpleTags_Admin::printAdminFooter(); ?>
262 </div>
263 <?php
264 }
265 }
266