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

386 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');
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 }
171 }
172
173 /**
174 * Suggest tags from OpenCalais Service
175 *
176 */
177 public static function ajax_opencalais() {
178 status_header( 200 );
179 header( "Content-Type: text/html; charset=" . get_bloginfo( 'charset' ) );
180
181
182 $suggestterms = taxopress_get_suggestterm_data();
183 $selected_suggestterm = (int)$_GET['suggestterms'];
184 $click_terms = false;
185 $taxonomy = 'post_tag';
186 if (array_key_exists($selected_suggestterm, $suggestterms)) {
187 $click_terms = $suggestterms[$selected_suggestterm];
188 $taxonomy = $click_terms['taxonomy'];
189 }
190
191 if(!$click_terms){
192 echo '<p>' . esc_html__( 'Suggest terms settings not found', 'simple-tags' ) . '</p>';
193 exit();
194 }
195
196 // API Key ?
197 if ( $click_terms['terms_opencalais_key'] == '' ) {
198 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>';
199 exit();
200 }
201
202 // Get data
203 $content = stripslashes( sanitize_textarea_field($_POST['content'])) . ' ' . stripslashes( sanitize_text_field($_POST['title']));
204 $content = trim( $content );
205 if ( empty( $content ) ) {
206 echo '<p>' . esc_html__( 'There\'s no content to scan.', 'simple-tags' ) . '</p>';
207 exit();
208 }
209
210 $response = wp_remote_post( 'https://api-eit.refinitiv.com/permid/calais', array(
211 'timeout' => 30,
212 'headers' => array(
213 'X-AG-Access-Token' => $click_terms['terms_opencalais_key'],
214 'Content-Type' => 'text/html',
215 'outputFormat' => 'application/json'
216 ),
217 'body' => $content
218 ) );
219
220 if ( ! is_wp_error( $response ) && $response != null ) {
221 if ( wp_remote_retrieve_response_code( $response ) == 200 ) {
222 $data_raw = json_decode( wp_remote_retrieve_body( $response ), true );
223
224 $data = array();
225 if ( isset( $data_raw ) && is_array( $data_raw ) ) {
226 foreach ( $data_raw as $_data_raw ) {
227 if ( isset( $_data_raw['_typeGroup'] ) && $_data_raw['_typeGroup'] == 'socialTag' ) {
228 $data[] = $_data_raw['name'];
229 }
230 }
231 }
232 }
233 }
234
235 if ( empty( $data ) || is_wp_error( $response ) ) {
236 echo '<p>' . esc_html__( 'No results from OpenCalais service.', 'simple-tags' ) . '</p>';
237 exit();
238 }
239
240 // Remove empty terms
241 $data = array_filter( $data, '_delete_empty_element' );
242 $data = array_unique( $data );
243
244 foreach ( (array) $data as $term ) {
245 echo '<span data-term_id="0" data-taxonomy="'.esc_attr($taxonomy).'" class="local">' . esc_html( strip_tags( $term ) ) . '</span>' . "\n";
246 }
247 echo '<div class="clear"></div>';
248 exit();
249 }
250
251 /**
252 * Suggest tags from dataTXT
253 *
254 */
255 public static function ajax_datatxt() {
256 status_header( 200 );
257 header( "Content-Type: text/html; charset=" . get_bloginfo( 'charset' ) );
258
259 $request_ws_args = array();
260
261 $suggestterms = taxopress_get_suggestterm_data();
262 $selected_suggestterm = (int)$_GET['suggestterms'];
263 $click_terms = false;
264 $taxonomy = 'post_tag';
265 if (array_key_exists($selected_suggestterm, $suggestterms)) {
266 $click_terms = $suggestterms[$selected_suggestterm];
267 $taxonomy = $click_terms['taxonomy'];
268 }
269
270 if(!$click_terms){
271 echo '<p>' . esc_html__( 'Suggest terms settings not found', 'simple-tags' ) . '</p>';
272 exit();
273 }
274
275 // Get data
276 $content = stripslashes( sanitize_textarea_field($_POST['content'])) . ' ' . stripslashes( sanitize_text_field($_POST['title']));
277 $content = trim( $content );
278 if ( empty( $content ) ) {
279 echo '<p>' . esc_html__( 'There\'s no content to scan.', 'simple-tags' ) . '</p>';
280 exit();
281 }
282
283 $request_ws_args['text'] = $content;
284
285 // Custom confidence ?
286 $request_ws_args['min_confidence'] = 0.6;
287 if ( $click_terms['terms_datatxt_min_confidence'] != "" ) {
288 $request_ws_args['min_confidence'] = $click_terms['terms_datatxt_min_confidence'];
289 }
290
291 $request_ws_args['token'] = $click_terms['terms_datatxt_access_token'];
292
293 // Build params
294 $response = wp_remote_post( 'https://api.dandelion.eu/datatxt/nex/v1', array(
295 'user-agent' => 'WordPress simple-tags',
296 'body' => $request_ws_args
297 ) );
298
299 $data = false;
300 if ( ! is_wp_error( $response ) && $response != null ) {
301 if ( wp_remote_retrieve_response_code( $response ) == 200 ) {
302 $data = wp_remote_retrieve_body( $response );
303 } else {
304 echo '<p>' . esc_html__( 'Invalid access token !', 'simple-tags' ) . '</p>';
305 exit();
306 }
307 }
308
309 $data = json_decode( $data );
310
311 // echo $data;
312 $data = is_object($data) ? $data->annotations : '';
313
314 if ( empty( $data ) ) {
315 echo '<p>' . esc_html__( 'No results from dataTXT API.', 'simple-tags' ) . '</p>';
316 exit();
317 }
318
319 foreach ( (array) $data as $term ) {
320 echo '<span data-term_id="0" data-taxonomy="'.esc_attr($taxonomy).'" class="local">' . esc_html( $term->title ) . '</span>' . "\n";
321 }
322 echo '<div class="clear"></div>';
323 exit();
324 }
325
326 /**
327 * Suggest tags from local database
328 *
329 */
330 public static function ajax_suggest_local() {
331 status_header( 200 );
332 header( "Content-Type: text/html; charset=" . get_bloginfo( 'charset' ) );
333
334
335 $taxonomy = 'post_tag';
336
337 if(isset($_GET['suggestterms'])){
338 $suggestterms = taxopress_get_suggestterm_data();
339 $selected_suggestterm = (int)$_GET['suggestterms'];
340
341 if (array_key_exists($selected_suggestterm, $suggestterms)) {
342 $taxonomy = $suggestterms[$selected_suggestterm]['taxonomy'];
343 }
344 }
345
346 if ( ( (int) wp_count_terms( $taxonomy, array( 'hide_empty' => false ) ) ) == 0 ) { // No tags to suggest
347 echo '<p>' . esc_html__( 'No terms in your WordPress database.', 'simple-tags' ) . '</p>';
348 exit();
349 }
350
351 // Get data
352 $content = stripslashes( sanitize_textarea_field($_POST['content'])) . ' ' . stripslashes( sanitize_text_field($_POST['title']));
353 $content = trim( $content );
354
355 if ( empty( $content ) ) {
356 echo '<p>' . esc_html__( 'There\'s no content to scan.', 'simple-tags' ) . '</p>';
357 exit();
358 }
359
360 // Get all terms
361 $terms = SimpleTags_Admin::getTermsForAjax( $taxonomy, '' );
362 if ( empty( $terms ) || $terms == false ) {
363 echo '<p>' . esc_html__( 'No results from your WordPress database.', 'simple-tags' ) . '</p>';
364 exit();
365 }
366
367 $flag = false;
368 foreach ( (array) $terms as $term ) {
369 $term_id = $term->term_id;
370 $term = stripslashes( $term->name );
371 if ( is_string( $term ) && ! empty( $term ) && stristr( $content, $term ) ) {
372 $flag = true;
373 echo '<span data-term_id="'.esc_attr($term_id).'" data-taxonomy="'.esc_attr($taxonomy).'" class="local">' . esc_html( $term ) . '</span>' . "\n";
374 }
375 }
376
377 if ( $flag == false ) {
378 echo '<p>' . esc_html__( 'There are no terms that are relevant to your content.', 'simple-tags' ) . '</p>';
379 } else {
380 echo '<div class="clear"></div>';
381 }
382
383 exit();
384 }
385 }
386