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

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

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