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

339 lines 10.0 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 wp_register_script( 'st-helper-suggested-tags', STAGS_URL . '/assets/js/helper-suggested-tags.js', array(
38 'jquery',
39 'st-helper-add-tags'
40 ), STAGS_VERSION );
41 wp_localize_script( 'st-helper-suggested-tags', 'stHelperSuggestedTagsL10n', array(
42 'title_bloc' => self::get_suggest_tags_title(),
43 'content_bloc' => __( 'Select an option above to load suggested terms.', 'simple-tags' )
44 ) );
45
46 // Helper for post type
47 wp_enqueue_script( 'st-helper-suggested-tags' );
48 }
49
50 /**
51 * Get Suggested tags title
52 *
53 */
54 public static function get_suggest_tags_title() {
55
56 $click_terms = taxopress_current_post_suggest_terms();
57
58
59
60 $title = '<img style="display:none;" id="st_ajax_loading" src="' . STAGS_URL . '/assets/images/ajax-loader.gif" alt="' . __( 'Ajax loading', 'simple-tags' ) . '" />';
61 $title .= __( 'Automatic term suggestions', 'simple-tags' ) . '';
62
63 $suggest_term_use_local = isset($click_terms['suggest_term_use_local']) ? (int)$click_terms['suggest_term_use_local'] : 0;
64 $suggest_term_use_dandelion = isset($click_terms['suggest_term_use_dandelion']) ? (int)$click_terms['suggest_term_use_dandelion'] : 0;
65 $suggest_term_use_opencalais = isset($click_terms['suggest_term_use_opencalais']) ? (int)$click_terms['suggest_term_use_opencalais'] : 0;
66
67 if($suggest_term_use_local > 0){
68 $title_options['tags_from_local_db'] = __( 'Local tags', 'simple-tags' );
69 }
70
71 if ( $click_terms['terms_datatxt_access_token'] !== '' && $suggest_term_use_dandelion > 0 ) {
72 $title_options['tags_from_datatxt'] = __( 'dataTXT by Dandelion', 'simple-tags' );
73 }
74 if ( $click_terms['terms_opencalais_key'] !== '' && $suggest_term_use_opencalais > 0 ) {
75 $title_options['tags_from_opencalais'] = __( 'OpenCalais', 'simple-tags' );
76 }
77
78 $title .= '&nbsp;
79 <select class="term_suggestion_select" name="term_suggestion_select" data-suggestterms="'.$click_terms['ID'].'">
80 <option value="" selected="selected">'.__( 'Select source to load suggested terms', 'simple-tags' ).'</option>';
81 foreach($title_options as $option => $label){
82 $title .= '<option value="'.$option.'">'.$label.'</option>';
83 }
84 $title .= '</select>';
85
86
87 return $title;
88 }
89
90 /**
91 * Register metabox for suggest tags, for post, and optionnaly page.
92 *
93 * @return void
94 * @author WebFactory Ltd
95 */
96 public static function admin_head() {
97
98 $click_terms = taxopress_current_post_suggest_terms();
99
100 if(!is_array($click_terms)){
101 return;
102 }
103
104 add_meta_box( 'suggestedtags', __( 'Suggested tags', 'simple-tags' ), array(
105 __CLASS__,
106 'metabox'
107 ), get_post_type(), 'advanced', 'core' );
108 }
109
110 /**
111 * Print HTML for suggest tags box
112 *
113 **/
114 public static function metabox() {
115 ?>
116 <span class="container_clicktags">
117 <?php echo SimpleTags_Admin::getDefaultContentBox(); ?>
118 <div class="clear"></div>
119 </span>
120 <?php
121 }
122
123 /**
124 * Ajax Dispatcher
125 *
126 */
127 public static function ajax_check() {
128 if ( isset( $_GET['stags_action'] ) ) {
129 switch ( $_GET['stags_action'] ) {
130 case 'tags_from_datatxt' :
131 self::ajax_datatxt();
132 break;
133 case 'tags_from_opencalais' :
134 self::ajax_opencalais();
135 break;
136 case 'tags_from_local_db' :
137 self::ajax_suggest_local();
138 break;
139 }
140 }
141 }
142
143 /**
144 * Suggest tags from OpenCalais Service
145 *
146 */
147 public static function ajax_opencalais() {
148 status_header( 200 );
149 header( "Content-Type: text/html; charset=" . get_bloginfo( 'charset' ) );
150
151
152 $suggestterms = taxopress_get_suggestterm_data();
153 $selected_suggestterm = (int)$_GET['suggestterms'];
154 $click_terms = false;
155 if (array_key_exists($selected_suggestterm, $suggestterms)) {
156 $click_terms = $suggestterms[$selected_suggestterm];
157 }
158
159 if(!$click_terms){
160 echo '<p>' . __( 'Suggest terms settings not found', 'simple-tags' ) . '</p>';
161 exit();
162 }
163
164 // API Key ?
165 if ( $click_terms['terms_opencalais_key'] == '' ) {
166 echo '<p>' . __( '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>';
167 exit();
168 }
169
170 // Get data
171 $content = stripslashes( $_POST['content'] ) . ' ' . stripslashes( $_POST['title'] );
172 $content = trim( $content );
173 if ( empty( $content ) ) {
174 echo '<p>' . __( 'No text was sent.', 'simple-tags' ) . '</p>';
175 exit();
176 }
177
178 $response = wp_remote_post( 'https://api-eit.refinitiv.com/permid/calais', array(
179 'timeout' => 30,
180 'headers' => array(
181 'X-AG-Access-Token' => $click_terms['terms_opencalais_key'],
182 'Content-Type' => 'text/html',
183 'outputFormat' => 'application/json'
184 ),
185 'body' => $content
186 ) );
187
188 if ( ! is_wp_error( $response ) && $response != null ) {
189 if ( wp_remote_retrieve_response_code( $response ) == 200 ) {
190 $data_raw = json_decode( wp_remote_retrieve_body( $response ), true );
191
192 $data = array();
193 if ( isset( $data_raw ) && is_array( $data_raw ) ) {
194 foreach ( $data_raw as $_data_raw ) {
195 if ( isset( $_data_raw['_typeGroup'] ) && $_data_raw['_typeGroup'] == 'socialTag' ) {
196 $data[] = $_data_raw['name'];
197 }
198 }
199 }
200 }
201 }
202
203 if ( empty( $data ) || is_wp_error( $response ) ) {
204 echo '<p>' . __( 'No results from OpenCalais service.', 'simple-tags' ) . '</p>';
205 exit();
206 }
207
208 // Remove empty terms
209 $data = array_filter( $data, '_delete_empty_element' );
210 $data = array_unique( $data );
211
212 foreach ( (array) $data as $term ) {
213 echo '<span class="local">' . esc_html( strip_tags( $term ) ) . '</span>' . "\n";
214 }
215 echo '<div class="clear"></div>';
216 exit();
217 }
218
219 /**
220 * Suggest tags from dataTXT
221 *
222 */
223 public static function ajax_datatxt() {
224 status_header( 200 );
225 header( "Content-Type: text/html; charset=" . get_bloginfo( 'charset' ) );
226
227 $request_ws_args = array();
228
229 $suggestterms = taxopress_get_suggestterm_data();
230 $selected_suggestterm = (int)$_GET['suggestterms'];
231 $click_terms = false;
232 if (array_key_exists($selected_suggestterm, $suggestterms)) {
233 $click_terms = $suggestterms[$selected_suggestterm];
234 }
235
236 if(!$click_terms){
237 echo '<p>' . __( 'Suggest terms settings not found', 'simple-tags' ) . '</p>';
238 exit();
239 }
240
241 // Get data
242 $content = stripslashes( $_POST['content'] ) . ' ' . stripslashes( $_POST['title'] );
243 $content = trim( $content );
244 if ( empty( $content ) ) {
245 echo '<p>' . __( 'No text was sent.', 'simple-tags' ) . '</p>';
246 exit();
247 }
248
249 $request_ws_args['text'] = $content;
250
251 // Custom confidence ?
252 $request_ws_args['min_confidence'] = 0.6;
253 if ( $click_terms['terms_datatxt_min_confidence'] != "" ) {
254 $request_ws_args['min_confidence'] = $click_terms['terms_datatxt_min_confidence'];
255 }
256
257 $request_ws_args['token'] = $click_terms['terms_datatxt_access_token'];
258
259 // Build params
260 $response = wp_remote_post( 'https://api.dandelion.eu/datatxt/nex/v1', array(
261 'user-agent' => 'WordPress simple-tags',
262 'body' => $request_ws_args
263 ) );
264
265 $data = false;
266 if ( ! is_wp_error( $response ) && $response != null ) {
267 if ( wp_remote_retrieve_response_code( $response ) == 200 ) {
268 $data = wp_remote_retrieve_body( $response );
269 } else {
270 echo '<p>' . __( 'Invalid access token !', 'simple-tags' ) . '</p>';
271 exit();
272 }
273 }
274
275 $data = json_decode( $data );
276
277 // echo $data;
278 $data = is_object($data) ? $data->annotations : '';
279
280 if ( empty( $data ) ) {
281 echo '<p>' . __( 'No results from dataTXT API.', 'simple-tags' ) . '</p>';
282 exit();
283 }
284
285 foreach ( (array) $data as $term ) {
286 echo '<span class="local">' . esc_html( $term->title ) . '</span>' . "\n";
287 }
288 echo '<div class="clear"></div>';
289 exit();
290 }
291
292 /**
293 * Suggest tags from local database
294 *
295 */
296 public static function ajax_suggest_local() {
297 status_header( 200 );
298 header( "Content-Type: text/html; charset=" . get_bloginfo( 'charset' ) );
299
300 if ( ( (int) wp_count_terms( 'post_tag', array( 'hide_empty' => false ) ) ) == 0 ) { // No tags to suggest
301 echo '<p>' . __( 'No terms in your WordPress database.', 'simple-tags' ) . '</p>';
302 exit();
303 }
304
305 // Get data
306 $content = stripslashes( $_POST['content'] ) . ' ' . stripslashes( $_POST['title'] );
307 $content = trim( $content );
308
309 if ( empty( $content ) ) {
310 echo '<p>' . __( 'No text was sent.', 'simple-tags' ) . '</p>';
311 exit();
312 }
313
314 // Get all terms
315 $terms = SimpleTags_Admin::getTermsForAjax( 'post_tag', '' );
316 if ( empty( $terms ) || $terms == false ) {
317 echo '<p>' . __( 'No results from your WordPress database.', 'simple-tags' ) . '</p>';
318 exit();
319 }
320
321 $flag = false;
322 foreach ( (array) $terms as $term ) {
323 $term = stripslashes( $term->name );
324 if ( is_string( $term ) && ! empty( $term ) && stristr( $content, $term ) ) {
325 $flag = true;
326 echo '<span class="local">' . esc_html( $term ) . '</span>' . "\n";
327 }
328 }
329
330 if ( $flag == false ) {
331 echo '<p>' . __( 'No correspondence between your content and terms from the WordPress database.', 'simple-tags' ) . '</p>';
332 } else {
333 echo '<div class="clear"></div>';
334 }
335
336 exit();
337 }
338 }
339