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

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

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