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

423 lines 13.4 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');
32
33 if(!is_array($click_terms)){
34 return;
35 }
36
37 //metabox edit line
38 if(current_user_can('admin_simple_tags')){
39 $click_term_edit = '<span class="edit-suggest-term-metabox">
40 '. sprintf(
41 '<a href="%s">%s</a>',
42 add_query_arg(
43 [
44 'page' => 'st_suggestterms',
45 'add' => 'new_item',
46 'action' => 'edit',
47 'taxopress_suggestterms' => $click_terms['ID'],
48 ],
49 admin_url('admin.php')
50 ),
51 __('Edit this metabox', 'simple-tags')
52 )
53 .'
54 </span>';
55 }else {
56 $click_term_edit = '';
57 }
58
59
60 wp_register_script( 'st-helper-suggested-tags', STAGS_URL . '/assets/js/helper-suggested-tags.js', array(
61 'jquery',
62 'st-helper-add-tags'
63 ), STAGS_VERSION );
64 wp_localize_script( 'st-helper-suggested-tags', 'stHelperSuggestedTagsL10n', array(
65 'title_bloc' => self::get_suggest_tags_title(),
66 'edit_metabox_link' => $click_term_edit,
67 'content_bloc' => esc_html__( 'Select an option above to load suggested terms.', 'simple-tags' )
68 ) );
69
70 // Helper for post type
71 wp_enqueue_script( 'st-helper-suggested-tags' );
72 }
73
74 /**
75 * Get Suggested tags title
76 *
77 */
78 public static function get_suggest_tags_title() {
79
80 $click_terms = taxopress_current_post_suggest_terms('term_suggestion');
81
82
83
84 $title = '<img style="display:none;" id="st_ajax_loading" src="' . STAGS_URL . '/assets/images/ajax-loader.gif" alt="' . esc_attr__( 'Ajax loading', 'simple-tags' ) . '" />';
85 $title .= esc_html__( 'Automatic term suggestions', 'simple-tags' ) . '';
86
87 $suggest_term_use_local = isset($click_terms['suggest_term_use_local']) ? (int)$click_terms['suggest_term_use_local'] : 0;
88 $suggest_term_use_dandelion = isset($click_terms['suggest_term_use_dandelion']) ? (int)$click_terms['suggest_term_use_dandelion'] : 0;
89 $suggest_term_use_opencalais = isset($click_terms['suggest_term_use_opencalais']) ? (int)$click_terms['suggest_term_use_opencalais'] : 0;
90
91 if($suggest_term_use_local > 0){
92 $title_options['tags_from_local_db'] = esc_html__( 'Existing terms on your site', 'simple-tags' );
93 }
94
95 if ( $click_terms['terms_datatxt_access_token'] !== '' && $suggest_term_use_dandelion > 0 ) {
96 $title_options['tags_from_datatxt'] = esc_html__( 'dataTXT by Dandelion', 'simple-tags' );
97 }
98 if ( $click_terms['terms_opencalais_key'] !== '' && $suggest_term_use_opencalais > 0 ) {
99 $title_options['tags_from_opencalais'] = esc_html__( 'OpenCalais', 'simple-tags' );
100 }
101
102 if(count($title_options) === 1){
103 $style = 'display: none';
104 }else{
105 $style = '';
106 }
107 $title .= '&nbsp;
108 <select style="'.$style.'" class="term_suggestion_select" name="term_suggestion_select" data-suggestterms="'.$click_terms['ID'].'">
109 <option value="" selected="selected">'.__( 'Select source to load suggested terms', 'simple-tags' ).'</option>';
110 foreach($title_options as $option => $label){
111 $title .= '<option value="'.$option.'">'.$label.'</option>';
112 }
113 $title .= '</select> <button class="term_suggestion_refresh">'.__( 'Refresh', 'simple-tags' ).'</button>';
114
115 return $title;
116 }
117
118 /**
119 * Register metabox for suggest tags, for post, and optionnaly page.
120 *
121 * @return void
122 * @author WebFactory Ltd
123 */
124 public static function admin_head() {
125
126 $click_terms = taxopress_current_post_suggest_terms('term_suggestion');
127
128 if(!is_array($click_terms)){
129 return;
130 }
131
132 add_meta_box( 'suggestedtags', esc_html__( 'Suggested tags', 'simple-tags' ), array(
133 __CLASS__,
134 'metabox'
135 ), get_post_type(), 'advanced', 'core' );
136 }
137
138 /**
139 * Print HTML for suggest tags box
140 *
141 **/
142 public static function metabox() {
143 ?>
144 <span class="container_clicktags">
145 <?php
146 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
147 echo SimpleTags_Admin::getDefaultContentBox(); ?>
148 <div class="clear"></div>
149 </span>
150 <?php
151 }
152
153 /**
154 * Ajax Dispatcher
155 *
156 */
157 public static function ajax_check() {
158 if ( isset( $_GET['stags_action'] ) ) {
159 switch ( $_GET['stags_action'] ) {
160 case 'tags_from_datatxt' :
161 self::ajax_datatxt();
162 break;
163 case 'tags_from_opencalais' :
164 self::ajax_opencalais();
165 break;
166 case 'tags_from_local_db' :
167 self::ajax_suggest_local();
168 break;
169 }
170 }else{
171 self::invalid_ajax_request();
172 }
173 }
174
175 /**
176 * Suggest tags from OpenCalais Service
177 *
178 */
179 public static function invalid_ajax_request() {
180 status_header( 200 );
181 header( "Content-Type: text/html; charset=" . get_bloginfo( 'charset' ) );
182 echo '<p>' . esc_html__( 'Invalid request.', 'simple-tags' ) . '</p>';
183 exit();
184 }
185
186 /**
187 * Suggest tags from OpenCalais Service
188 *
189 */
190 public static function ajax_opencalais() {
191 status_header( 200 );
192 header( "Content-Type: text/html; charset=" . get_bloginfo( 'charset' ) );
193
194
195 $suggestterms = taxopress_get_suggestterm_data();
196 $selected_suggestterm = (int)$_GET['suggestterms'];
197 $click_terms = false;
198 $taxonomy = 'post_tag';
199 if (array_key_exists($selected_suggestterm, $suggestterms)) {
200 $click_terms = $suggestterms[$selected_suggestterm];
201 $taxonomy = $click_terms['taxonomy'];
202 }
203
204 if(!$click_terms){
205 echo '<p>' . esc_html__( 'Suggest terms settings not found', 'simple-tags' ) . '</p>';
206 exit();
207 }
208
209 // API Key ?
210 if ( $click_terms['terms_opencalais_key'] == '' ) {
211 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>';
212 exit();
213 }
214
215 // Get data
216 $post_id = ( isset( $_POST['post_id'] ) ) ? intval( $_POST['post_id'] ) : 0;
217 $content = stripslashes( sanitize_textarea_field($_POST['content'])) . ' ' . stripslashes( sanitize_text_field($_POST['title']));
218 $content = trim( $content );
219 if ( empty( $content ) ) {
220 echo '<p>' . esc_html__( 'There\'s no content to scan.', 'simple-tags' ) . '</p>';
221 exit();
222 }
223
224 $response = wp_remote_post( 'https://api-eit.refinitiv.com/permid/calais', array(
225 'timeout' => 30,
226 'headers' => array(
227 'X-AG-Access-Token' => $click_terms['terms_opencalais_key'],
228 'Content-Type' => 'text/html',
229 'outputFormat' => 'application/json'
230 ),
231 'body' => $content
232 ) );
233
234 if ( ! is_wp_error( $response ) && $response != null ) {
235 if ( wp_remote_retrieve_response_code( $response ) == 200 ) {
236 $data_raw = json_decode( wp_remote_retrieve_body( $response ), true );
237
238 $data = array();
239 if ( isset( $data_raw ) && is_array( $data_raw ) ) {
240 foreach ( $data_raw as $_data_raw ) {
241 if ( isset( $_data_raw['_typeGroup'] ) && $_data_raw['_typeGroup'] == 'socialTag' ) {
242 $data[] = $_data_raw['name'];
243 }
244 }
245 }
246 }
247 }
248
249 if ( empty( $data ) || is_wp_error( $response ) ) {
250 echo '<p>' . esc_html__( 'No results from OpenCalais service.', 'simple-tags' ) . '</p>';
251 exit();
252 }
253
254 // Remove empty terms
255 $data = array_filter( $data, '_delete_empty_element' );
256 $data = array_unique( $data );
257
258 // Get terms for current post
259 $post_terms = array();
260 if ( $post_id > 0 ) {
261 $post_terms = wp_get_post_terms( $post_id, $taxonomy, array( 'fields' => 'names' ) );
262 }
263
264 foreach ( (array) $data as $term ) {
265 $class_current = in_array(strip_tags( $term ), $post_terms) ? 'used_term' : '';
266 echo '<span data-term_id="0" data-taxonomy="'.esc_attr($taxonomy).'" class="local ' . esc_attr( $class_current ) . '">' . esc_html( strip_tags( $term ) ) . '</span>' . "\n";
267 }
268 echo '<div class="clear"></div>';
269 exit();
270 }
271
272 /**
273 * Suggest tags from dataTXT
274 *
275 */
276 public static function ajax_datatxt() {
277 status_header( 200 );
278 header( "Content-Type: text/html; charset=" . get_bloginfo( 'charset' ) );
279
280 $request_ws_args = array();
281
282 $suggestterms = taxopress_get_suggestterm_data();
283 $selected_suggestterm = (int)$_GET['suggestterms'];
284 $click_terms = false;
285 $taxonomy = 'post_tag';
286 if (array_key_exists($selected_suggestterm, $suggestterms)) {
287 $click_terms = $suggestterms[$selected_suggestterm];
288 $taxonomy = $click_terms['taxonomy'];
289 }
290
291 if(!$click_terms){
292 echo '<p>' . esc_html__( 'Suggest terms settings not found', 'simple-tags' ) . '</p>';
293 exit();
294 }
295
296 // Get data
297 $post_id = ( isset( $_POST['post_id'] ) ) ? intval( $_POST['post_id'] ) : 0;
298 $content = stripslashes( sanitize_textarea_field($_POST['content'])) . ' ' . stripslashes( sanitize_text_field($_POST['title']));
299 $content = trim( $content );
300 if ( empty( $content ) ) {
301 echo '<p>' . esc_html__( 'There\'s no content to scan.', 'simple-tags' ) . '</p>';
302 exit();
303 }
304
305 $request_ws_args['text'] = $content;
306
307 // Custom confidence ?
308 $request_ws_args['min_confidence'] = 0.6;
309 if ( $click_terms['terms_datatxt_min_confidence'] != "" ) {
310 $request_ws_args['min_confidence'] = $click_terms['terms_datatxt_min_confidence'];
311 }
312
313 $request_ws_args['token'] = $click_terms['terms_datatxt_access_token'];
314
315 // Build params
316 $response = wp_remote_post( 'https://api.dandelion.eu/datatxt/nex/v1', array(
317 'user-agent' => 'WordPress simple-tags',
318 'body' => $request_ws_args
319 ) );
320
321 $data = false;
322 if ( ! is_wp_error( $response ) && $response != null ) {
323 if ( wp_remote_retrieve_response_code( $response ) == 200 ) {
324 $data = wp_remote_retrieve_body( $response );
325 } else {
326 echo '<p>' . esc_html__( 'Invalid access token !', 'simple-tags' ) . '</p>';
327 exit();
328 }
329 }
330
331 $data = json_decode( $data );
332
333 // echo $data;
334 $data = is_object($data) ? $data->annotations : '';
335
336 if ( empty( $data ) ) {
337 echo '<p>' . esc_html__( 'No results from dataTXT API.', 'simple-tags' ) . '</p>';
338 exit();
339 }
340
341 // Get terms for current post
342 $post_terms = array();
343 if ( $post_id > 0 ) {
344 $post_terms = wp_get_post_terms( $post_id, $taxonomy, array( 'fields' => 'names' ) );
345 }
346
347 foreach ( (array) $data as $term ) {
348 $class_current = in_array(strip_tags( $term ), $post_terms) ? 'used_term' : '';
349 echo '<span data-term_id="0" data-taxonomy="'.esc_attr($taxonomy).'" class="local ' . esc_attr( $class_current ) . '">' . esc_html( $term->title ) . '</span>' . "\n";
350 }
351 echo '<div class="clear"></div>';
352 exit();
353 }
354
355 /**
356 * Suggest tags from local database
357 *
358 */
359 public static function ajax_suggest_local() {
360 status_header( 200 );
361 header( "Content-Type: text/html; charset=" . get_bloginfo( 'charset' ) );
362
363
364 $taxonomy = 'post_tag';
365
366 if(isset($_GET['suggestterms'])){
367 $suggestterms = taxopress_get_suggestterm_data();
368 $selected_suggestterm = (int)$_GET['suggestterms'];
369
370 if (array_key_exists($selected_suggestterm, $suggestterms)) {
371 $taxonomy = $suggestterms[$selected_suggestterm]['taxonomy'];
372 }
373 }
374
375 if ( ( (int) wp_count_terms( $taxonomy, array( 'hide_empty' => false ) ) ) == 0 ) { // No tags to suggest
376 echo '<p>' . esc_html__( 'No terms in your WordPress database.', 'simple-tags' ) . '</p>';
377 exit();
378 }
379
380 // Get data
381 $post_id = ( isset( $_POST['post_id'] ) ) ? intval( $_POST['post_id'] ) : 0;
382 $content = stripslashes( sanitize_textarea_field($_POST['content'])) . ' ' . stripslashes( sanitize_text_field($_POST['title']));
383 $content = trim( $content );
384
385 if ( empty( $content ) ) {
386 echo '<p>' . esc_html__( 'There\'s no content to scan.', 'simple-tags' ) . '</p>';
387 exit();
388 }
389
390 // Get all terms
391 $terms = SimpleTags_Admin::getTermsForAjax( $taxonomy, '' );
392 if ( empty( $terms ) || $terms == false ) {
393 echo '<p>' . esc_html__( 'No results from your WordPress database.', 'simple-tags' ) . '</p>';
394 exit();
395 }
396
397 // Get terms for current post
398 $post_terms = array();
399 if ( $post_id > 0 ) {
400 $post_terms = wp_get_post_terms( $post_id, $taxonomy, array( 'fields' => 'ids' ) );
401 }
402
403 $flag = false;
404 foreach ( (array) $terms as $term ) {
405 $class_current = in_array($term->term_id, $post_terms) ? 'used_term' : '';
406 $term_id = $term->term_id;
407 $term = stripslashes( $term->name );
408 if ( is_string( $term ) && ! empty( $term ) && stristr( $content, $term ) ) {
409 $flag = true;
410 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";
411 }
412 }
413
414 if ( $flag == false ) {
415 echo '<p>' . esc_html__( 'There are no terms that are relevant to your content.', 'simple-tags' ) . '</p>';
416 } else {
417 echo '<div class="clear"></div>';
418 }
419
420 exit();
421 }
422 }
423