PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.6.2
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.6.2
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 / class.admin.suggest.php

class.admin.suggest.php in Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms 3.6.2, at inc/class.admin.suggest.php

381 lines 11.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class SimpleTags_Admin_Suggest {
4
5 // Application entrypoint -> https://wordpress.org/plugins/simple-tags/wiki/
6
7 /**
8 * SimpleTags_Admin_Suggest constructor.
9 */
10 public function __construct() {
11 // Ajax action, JS Helper and admin action
12 add_action( 'wp_ajax_simpletags', array( __CLASS__, 'ajax_check' ) );
13
14 if ( 1 === (int) SimpleTags_Plugin::get_option_value( 'active_suggest_terms' )){
15 // Box for post/page
16 add_action( 'admin_head', array( __CLASS__, 'admin_head' ), 1 );
17 // Javascript
18 add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_enqueue_scripts' ), 11 );
19 }
20 }
21
22 /**
23 * Init somes JS and CSS need for this feature
24 *
25 * @return void
26 * @author WebFactory Ltd
27 */
28 public static function admin_enqueue_scripts() {
29 global $pagenow;
30
31 $click_terms = taxopress_current_post_suggest_terms('term_suggestion', false, false);
32
33 if(!is_array($click_terms)){
34 return;
35 }
36
37 $manage_link = add_query_arg(
38 [
39 'page' => 'st_suggestterms',
40 'add' => 'new_item',
41 'action' => 'edit',
42 ],
43 admin_url('admin.php')
44 );
45
46 wp_register_script( 'st-helper-suggested-tags', STAGS_URL . '/assets/js/helper-suggested-tags.js', array(
47 'jquery',
48 'st-helper-add-tags'
49 ), STAGS_VERSION );
50 wp_localize_script( 'st-helper-suggested-tags', 'stHelperSuggestedTagsL10n', array(
51 'click_terms' => $click_terms,
52 'manage_link' => $manage_link,
53 'stag_url' => STAGS_URL,
54 'manage_metabox' => current_user_can('admin_simple_tags') ? 1 : 0,
55 'edit_metabox_text' => esc_html__('Edit this metabox', 'simple-tags'),
56 'local_term_text' => esc_html__('Existing terms on your site', 'simple-tags'),
57 'dandelion_text' => esc_html__('dataTXT by Dandelion', 'simple-tags'),
58 'opencalais_text' => esc_html__('OpenCalais', 'simple-tags'),
59 'refresh_text' => esc_html__('Refresh', 'simple-tags'),
60 'content_bloc' => esc_html__('Select an option above to load suggested terms.', 'simple-tags'),
61 'source_text' => esc_html__('Select source to load suggested terms', 'simple-tags')
62 ) );
63
64 // Helper for post type
65 wp_enqueue_script( 'st-helper-suggested-tags' );
66 }
67
68 /**
69 * Register metabox for suggest tags, for post, and optionnaly page.
70 *
71 * @return void
72 * @author WebFactory Ltd
73 */
74 public static function admin_head() {
75
76 $click_terms = taxopress_current_post_suggest_terms('term_suggestion', false, false);
77
78 if(!is_array($click_terms)){
79 return;
80 }
81 $key_index = 0;
82 foreach ($click_terms as $click_term) {
83 add_meta_box(
84 'suggestedtags-'.$key_index,
85 esc_html__('Suggested tags', 'simple-tags'),
86 array(__CLASS__, 'metabox'),
87 get_post_type(),
88 'advanced',
89 'core',
90 ['key_index' => $key_index]
91 );
92 $key_index++;
93 }
94 }
95
96 /**
97 * Print HTML for suggest tags box
98 *
99 **/
100 public static function metabox($post, $callback_args) {
101 ?>
102 <span class="container_clicktags <?php esc_attr_e('container_clicktags_' . $callback_args['args']['key_index']); ?> multiple" data-key_index="<?php esc_attr_e($callback_args['args']['key_index']); ?>">
103 <?php
104 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
105 echo SimpleTags_Admin::getDefaultContentBox(); ?>
106 <div class="clear"></div>
107 </span>
108 <?php
109 }
110
111 /**
112 * Ajax Dispatcher
113 *
114 */
115 public static function ajax_check() {
116 if ( isset( $_GET['stags_action'] ) ) {
117 switch ( $_GET['stags_action'] ) {
118 case 'tags_from_datatxt' :
119 self::ajax_datatxt();
120 break;
121 case 'tags_from_opencalais' :
122 self::ajax_opencalais();
123 break;
124 case 'tags_from_local_db' :
125 self::ajax_suggest_local();
126 break;
127 }
128 }else{
129 self::invalid_ajax_request();
130 }
131 }
132
133 /**
134 * Suggest tags from OpenCalais Service
135 *
136 */
137 public static function invalid_ajax_request() {
138 status_header( 200 );
139 header( "Content-Type: text/html; charset=" . get_bloginfo( 'charset' ) );
140 echo '<p>' . esc_html__( 'Invalid request.', 'simple-tags' ) . '</p>';
141 exit();
142 }
143
144 /**
145 * Suggest tags from OpenCalais Service
146 *
147 */
148 public static function ajax_opencalais() {
149 status_header( 200 );
150 header( "Content-Type: text/html; charset=" . get_bloginfo( 'charset' ) );
151
152
153 $suggestterms = taxopress_get_suggestterm_data();
154 $selected_suggestterm = (int)$_GET['suggestterms'];
155 $click_terms = false;
156 $taxonomy = 'post_tag';
157 if (array_key_exists($selected_suggestterm, $suggestterms)) {
158 $click_terms = $suggestterms[$selected_suggestterm];
159 $taxonomy = $click_terms['taxonomy'];
160 }
161
162 if(!$click_terms){
163 echo '<p>' . esc_html__( 'Suggest terms settings not found', 'simple-tags' ) . '</p>';
164 exit();
165 }
166
167 // API Key ?
168 if ( $click_terms['terms_opencalais_key'] == '' ) {
169 echo '<p>' . esc_html__( 'OpenCalais need an API key to work. You can register on service website to obtain a key and set it on TaxoPress options.', 'simple-tags' ) . '</p>';
170 exit();
171 }
172
173 // Get data
174 $post_id = ( isset( $_POST['post_id'] ) ) ? intval( $_POST['post_id'] ) : 0;
175 $content = stripslashes( sanitize_textarea_field($_POST['content'])) . ' ' . stripslashes( sanitize_text_field($_POST['title']));
176 $content = trim( $content );
177 if ( empty( $content ) ) {
178 echo '<p>' . esc_html__( 'There\'s no content to scan.', 'simple-tags' ) . '</p>';
179 exit();
180 }
181
182 $response = wp_remote_post( 'https://api-eit.refinitiv.com/permid/calais', array(
183 'timeout' => 30,
184 'headers' => array(
185 'X-AG-Access-Token' => $click_terms['terms_opencalais_key'],
186 'Content-Type' => 'text/html',
187 'outputFormat' => 'application/json'
188 ),
189 'body' => $content
190 ) );
191
192 if ( ! is_wp_error( $response ) && $response != null ) {
193 if ( wp_remote_retrieve_response_code( $response ) == 200 ) {
194 $data_raw = json_decode( wp_remote_retrieve_body( $response ), true );
195
196 $data = array();
197 if ( isset( $data_raw ) && is_array( $data_raw ) ) {
198 foreach ( $data_raw as $_data_raw ) {
199 if ( isset( $_data_raw['_typeGroup'] ) && $_data_raw['_typeGroup'] == 'socialTag' ) {
200 $data[] = $_data_raw['name'];
201 }
202 }
203 }
204 }
205 }
206
207 if ( empty( $data ) || is_wp_error( $response ) ) {
208 echo '<p>' . esc_html__( 'No results from OpenCalais service.', 'simple-tags' ) . '</p>';
209 exit();
210 }
211
212 // Remove empty terms
213 $data = array_filter( $data, '_delete_empty_element' );
214 $data = array_unique( $data );
215
216 // Get terms for current post
217 $post_terms = array();
218 if ( $post_id > 0 ) {
219 $post_terms = wp_get_post_terms( $post_id, $taxonomy, array( 'fields' => 'names' ) );
220 }
221
222 foreach ( (array) $data as $term ) {
223 $class_current = in_array(strip_tags( $term ), $post_terms) ? 'used_term' : '';
224 echo '<span data-term_id="0" data-taxonomy="'.esc_attr($taxonomy).'" class="local ' . esc_attr( $class_current ) . '">' . esc_html( strip_tags( $term ) ) . '</span>' . "\n";
225 }
226 echo '<div class="clear"></div>';
227 exit();
228 }
229
230 /**
231 * Suggest tags from dataTXT
232 *
233 */
234 public static function ajax_datatxt() {
235 status_header( 200 );
236 header( "Content-Type: text/html; charset=" . get_bloginfo( 'charset' ) );
237
238 $request_ws_args = array();
239
240 $suggestterms = taxopress_get_suggestterm_data();
241 $selected_suggestterm = (int)$_GET['suggestterms'];
242 $click_terms = false;
243 $taxonomy = 'post_tag';
244 if (array_key_exists($selected_suggestterm, $suggestterms)) {
245 $click_terms = $suggestterms[$selected_suggestterm];
246 $taxonomy = $click_terms['taxonomy'];
247 }
248
249 if(!$click_terms){
250 echo '<p>' . esc_html__( 'Suggest terms settings not found', 'simple-tags' ) . '</p>';
251 exit();
252 }
253
254 // Get data
255 $post_id = ( isset( $_POST['post_id'] ) ) ? intval( $_POST['post_id'] ) : 0;
256 $content = stripslashes( sanitize_textarea_field($_POST['content'])) . ' ' . stripslashes( sanitize_text_field($_POST['title']));
257 $content = trim( $content );
258 if ( empty( $content ) ) {
259 echo '<p>' . esc_html__( 'There\'s no content to scan.', 'simple-tags' ) . '</p>';
260 exit();
261 }
262
263 $request_ws_args['text'] = $content;
264
265 // Custom confidence ?
266 $request_ws_args['min_confidence'] = 0.6;
267 if ( $click_terms['terms_datatxt_min_confidence'] != "" ) {
268 $request_ws_args['min_confidence'] = $click_terms['terms_datatxt_min_confidence'];
269 }
270
271 $request_ws_args['token'] = $click_terms['terms_datatxt_access_token'];
272
273 // Build params
274 $response = wp_remote_post( 'https://api.dandelion.eu/datatxt/nex/v1', array(
275 'user-agent' => 'WordPress simple-tags',
276 'body' => $request_ws_args
277 ) );
278
279 $data = false;
280 if ( ! is_wp_error( $response ) && $response != null ) {
281 if ( wp_remote_retrieve_response_code( $response ) == 200 ) {
282 $data = wp_remote_retrieve_body( $response );
283 } else {
284 echo '<p>' . esc_html__( 'Invalid access token !', 'simple-tags' ) . '</p>';
285 exit();
286 }
287 }
288
289 $data = json_decode( $data );
290
291 // echo $data;
292 $data = is_object($data) ? $data->annotations : '';
293
294 if ( empty( $data ) ) {
295 echo '<p>' . esc_html__( 'No results from dataTXT API.', 'simple-tags' ) . '</p>';
296 exit();
297 }
298
299 // Get terms for current post
300 $post_terms = array();
301 if ( $post_id > 0 ) {
302 $post_terms = wp_get_post_terms( $post_id, $taxonomy, array( 'fields' => 'names' ) );
303 }
304
305 foreach ( (array) $data as $term ) {
306 $class_current = in_array(strip_tags( $term ), $post_terms) ? 'used_term' : '';
307 echo '<span data-term_id="0" data-taxonomy="'.esc_attr($taxonomy).'" class="local ' . esc_attr( $class_current ) . '">' . esc_html( $term->title ) . '</span>' . "\n";
308 }
309 echo '<div class="clear"></div>';
310 exit();
311 }
312
313 /**
314 * Suggest tags from local database
315 *
316 */
317 public static function ajax_suggest_local() {
318 status_header( 200 );
319 header( "Content-Type: text/html; charset=" . get_bloginfo( 'charset' ) );
320
321
322 $taxonomy = 'post_tag';
323
324 if(isset($_GET['suggestterms'])){
325 $suggestterms = taxopress_get_suggestterm_data();
326 $selected_suggestterm = (int)$_GET['suggestterms'];
327
328 if (array_key_exists($selected_suggestterm, $suggestterms)) {
329 $taxonomy = $suggestterms[$selected_suggestterm]['taxonomy'];
330 }
331 }
332
333 if ( ( (int) wp_count_terms( $taxonomy, array( 'hide_empty' => false ) ) ) == 0 ) { // No tags to suggest
334 echo '<p>' . esc_html__( 'No terms in your WordPress database.', 'simple-tags' ) . '</p>';
335 exit();
336 }
337
338 // Get data
339 $post_id = ( isset( $_POST['post_id'] ) ) ? intval( $_POST['post_id'] ) : 0;
340 $content = stripslashes( sanitize_textarea_field($_POST['content'])) . ' ' . stripslashes( sanitize_text_field($_POST['title']));
341 $content = trim( $content );
342
343 if ( empty( $content ) ) {
344 echo '<p>' . esc_html__( 'There\'s no content to scan.', 'simple-tags' ) . '</p>';
345 exit();
346 }
347
348 // Get all terms
349 $terms = SimpleTags_Admin::getTermsForAjax( $taxonomy, '' );
350 if ( empty( $terms ) || $terms == false ) {
351 echo '<p>' . esc_html__( 'No results from your WordPress database.', 'simple-tags' ) . '</p>';
352 exit();
353 }
354
355 // Get terms for current post
356 $post_terms = array();
357 if ( $post_id > 0 ) {
358 $post_terms = wp_get_post_terms( $post_id, $taxonomy, array( 'fields' => 'ids' ) );
359 }
360
361 $flag = false;
362 foreach ( (array) $terms as $term ) {
363 $class_current = in_array($term->term_id, $post_terms) ? 'used_term' : '';
364 $term_id = $term->term_id;
365 $term = stripslashes( $term->name );
366 if ( is_string( $term ) && ! empty( $term ) && stristr( $content, $term ) ) {
367 $flag = true;
368 echo '<span data-term_id="'.esc_attr($term_id).'" data-taxonomy="'.esc_attr($taxonomy).'" class="local ' . esc_attr( $class_current ) . '">' . esc_html( $term ) . '</span>' . "\n";
369 }
370 }
371
372 if ( $flag == false ) {
373 echo '<p>' . esc_html__( 'There are no terms that are relevant to your content.', 'simple-tags' ) . '</p>';
374 } else {
375 echo '<div class="clear"></div>';
376 }
377
378 exit();
379 }
380 }
381