| 1 |
<?php |
| 2 |
/** |
| 3 |
* Fetch our TAXOPRESS SuggestTerms option. |
| 4 |
* |
| 5 |
* @return mixed |
| 6 |
*/ |
| 7 |
function taxopress_get_suggestterm_data() |
| 8 |
{ |
| 9 |
return array_filter((array)apply_filters('taxopress_get_suggestterm_data', get_option('taxopress_suggestterms', []), |
| 10 |
get_current_blog_id())); |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Get the selected suggestterm from the $_POST global. |
| 15 |
* |
| 16 |
* @return bool|string False on no result, sanitized suggestterm if set. |
| 17 |
* @internal |
| 18 |
* |
| 19 |
*/ |
| 20 |
function taxopress_get_current_suggestterm() |
| 21 |
{ |
| 22 |
|
| 23 |
$suggestterms = false; |
| 24 |
|
| 25 |
if (!empty($_GET) && isset($_GET['taxopress_suggestterms'])) { |
| 26 |
$suggestterms = sanitize_text_field($_GET['taxopress_suggestterms']); |
| 27 |
} else { |
| 28 |
$suggestterms = taxopress_get_suggestterm_data(); |
| 29 |
if (!empty($suggestterms)) { |
| 30 |
// Will return the first array key. |
| 31 |
$suggestterms = key($suggestterms); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Filters the current suggestterm to edit. |
| 37 |
* |
| 38 |
* @param string $suggestterms suggestterm slug. |
| 39 |
*/ |
| 40 |
return apply_filters('taxopress_current_suggestterm', $suggestterms); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Handle the save and deletion of suggestterm data. |
| 45 |
*/ |
| 46 |
function taxopress_process_suggestterm() |
| 47 |
{ |
| 48 |
|
| 49 |
if (wp_doing_ajax()) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
if (!is_admin()) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
if(!current_user_can('simple_tags')){ |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
if (empty($_GET)) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
if (!isset($_GET['page'])) { |
| 66 |
return; |
| 67 |
} |
| 68 |
if ('st_suggestterms' !== $_GET['page']) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
if (isset($_GET['new_suggestterm'])) { |
| 73 |
if ((int)$_GET['new_suggestterm'] === 1) { |
| 74 |
add_action('admin_notices', "taxopress_suggestterms_update_success_admin_notice"); |
| 75 |
add_filter('removable_query_args', 'taxopress_saved_suggestterm_filter_removable_query_args'); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
if (isset($_GET['deleted_suggestterm'])) { |
| 80 |
if ((int)$_GET['deleted_suggestterm'] === 1) { |
| 81 |
add_action('admin_notices', "taxopress_suggestterms_delete_success_admin_notice"); |
| 82 |
add_filter('removable_query_args', 'taxopress_deleted_suggestterm_filter_removable_query_args'); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
if (!empty($_POST) && isset($_POST['suggestterm_submit'])) { |
| 88 |
$result = ''; |
| 89 |
if (isset($_POST['suggestterm_submit'])) { |
| 90 |
check_admin_referer('taxopress_addedit_suggestterm_nonce_action', |
| 91 |
'taxopress_addedit_suggestterm_nonce_field'); |
| 92 |
$result = taxopress_update_suggestterm($_POST); |
| 93 |
} |
| 94 |
|
| 95 |
if ($result) { |
| 96 |
wp_safe_redirect( |
| 97 |
add_query_arg( |
| 98 |
[ |
| 99 |
'page' => 'st_suggestterms', |
| 100 |
'add' => 'new_item', |
| 101 |
'action' => 'edit', |
| 102 |
'taxopress_suggestterms' => $result, |
| 103 |
'new_suggestterm' => 1, |
| 104 |
], |
| 105 |
taxopress_admin_url('admin.php') |
| 106 |
) |
| 107 |
); |
| 108 |
|
| 109 |
exit(); |
| 110 |
} |
| 111 |
} elseif (isset($_REQUEST['action']) && $_REQUEST['action'] === 'taxopress-delete-suggestterm') { |
| 112 |
$nonce = sanitize_text_field($_REQUEST['_wpnonce']); |
| 113 |
if (wp_verify_nonce($nonce, 'suggestterm-action-request-nonce')) { |
| 114 |
taxopress_action_delete_suggestterm(sanitize_text_field($_REQUEST['taxopress_suggestterms'])); |
| 115 |
} |
| 116 |
add_filter('removable_query_args', 'taxopress_delete_suggestterm_filter_removable_query_args'); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
add_action('admin_init', 'taxopress_process_suggestterm', 8); |
| 121 |
|
| 122 |
|
| 123 |
/** |
| 124 |
* Create default suggestterm. |
| 125 |
*/ |
| 126 |
function taxopress_create_default_suggestterm() |
| 127 |
{ |
| 128 |
|
| 129 |
if (wp_doing_ajax()) { |
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
if (!is_admin()) { |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
if(!current_user_can('simple_tags')){ |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
if ((int)get_option('taxopress_default_suggestterms') > 0) { |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
if (count(taxopress_get_suggestterm_data()) > 0) { |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
$default = []; |
| 151 |
$default['taxopress_suggestterm']['title'] = 'Suggest Term'; |
| 152 |
$default['taxopress_suggestterm']['taxonomy'] = 'post_tag'; |
| 153 |
$default['post_types'] = ['post']; |
| 154 |
$default['taxopress_suggestterm']['number'] = '100'; |
| 155 |
$default['taxopress_suggestterm']['orderby'] = 'count'; |
| 156 |
$default['taxopress_suggestterm']['order'] = 'desc'; |
| 157 |
$default['taxopress_suggestterm']['disable_local'] = '0'; |
| 158 |
$default['taxopress_suggestterm']['suggest_term_use_local'] = '1'; |
| 159 |
$default['taxopress_suggestterm']['suggest_term_use_dandelion'] = '0'; |
| 160 |
$default['taxopress_suggestterm']['suggest_term_use_opencalais'] = '0'; |
| 161 |
$default['taxopress_suggestterm']['terms_opencalais_key'] = SimpleTags_Plugin::get_option_value( 'opencalais_key' ); |
| 162 |
$default['taxopress_suggestterm']['terms_datatxt_access_token'] = SimpleTags_Plugin::get_option_value( 'datatxt_access_token' ); |
| 163 |
$default['taxopress_suggestterm']['terms_datatxt_min_confidence'] = SimpleTags_Plugin::get_option_value( 'datatxt_min_confidence' ); |
| 164 |
$result = taxopress_update_suggestterm($default); |
| 165 |
|
| 166 |
update_option('taxopress_default_suggestterms', $result); |
| 167 |
} |
| 168 |
|
| 169 |
add_action('admin_init', 'taxopress_create_default_suggestterm', 8); |
| 170 |
|
| 171 |
|
| 172 |
/** |
| 173 |
* Add to or update our TAXOPRESS option with new data. |
| 174 |
* |
| 175 |
* |
| 176 |
* @param array $data Array of suggestterm data to update. Optional. |
| 177 |
* @return bool|string False on failure, string on success. |
| 178 |
* @internal |
| 179 |
* |
| 180 |
*/ |
| 181 |
function taxopress_update_suggestterm($data = []) |
| 182 |
{ |
| 183 |
foreach ($data as $key => $value) { |
| 184 |
|
| 185 |
if (is_string($value)) { |
| 186 |
$data[$key] = sanitize_text_field($value); |
| 187 |
} else { |
| 188 |
array_map('sanitize_text_field', $data[$key]); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
$suggestterms = taxopress_get_suggestterm_data(); |
| 193 |
|
| 194 |
$title = $data['taxopress_suggestterm']['title']; |
| 195 |
$title = str_replace('"', '', htmlspecialchars_decode($title)); |
| 196 |
$title = htmlspecialchars($title, ENT_QUOTES); |
| 197 |
$title = trim($title); |
| 198 |
$data['taxopress_suggestterm']['title'] = stripslashes_deep($title); |
| 199 |
|
| 200 |
|
| 201 |
//update other post post |
| 202 |
$data['taxopress_suggestterm']['post_types'] = isset($data['post_types']) ? $data['post_types'] : []; |
| 203 |
|
| 204 |
//update our custom checkbox value if not checked |
| 205 |
if (!isset($data['taxopress_suggestterm']['disable_local'])) { |
| 206 |
$data['taxopress_suggestterm']['disable_local'] = 0; |
| 207 |
} |
| 208 |
if (!isset($data['taxopress_suggestterm']['suggest_term_use_local'])) { |
| 209 |
$data['taxopress_suggestterm']['suggest_term_use_local'] = 0; |
| 210 |
} |
| 211 |
if (!isset($data['taxopress_suggestterm']['suggest_term_use_dandelion'])) { |
| 212 |
$data['taxopress_suggestterm']['suggest_term_use_dandelion'] = 0; |
| 213 |
} |
| 214 |
if (!isset($data['taxopress_suggestterm']['suggest_term_use_opencalais'])) { |
| 215 |
$data['taxopress_suggestterm']['suggest_term_use_opencalais'] = 0; |
| 216 |
} |
| 217 |
|
| 218 |
if (isset($data['edited_suggestterm'])) { |
| 219 |
$suggestterm_id = $data['edited_suggestterm']; |
| 220 |
$suggestterms[$suggestterm_id] = $data['taxopress_suggestterm']; |
| 221 |
$success = update_option('taxopress_suggestterms', $suggestterms); |
| 222 |
//return 'update_success'; |
| 223 |
} else { |
| 224 |
$suggestterm_id = (int)get_option('taxopress_suggestterm_ids_increament') + 1; |
| 225 |
$data['taxopress_suggestterm']['ID'] = $suggestterm_id; |
| 226 |
$suggestterms[$suggestterm_id] = $data['taxopress_suggestterm']; |
| 227 |
$success = update_option('taxopress_suggestterms', $suggestterms); |
| 228 |
$update_id = update_option('taxopress_suggestterm_ids_increament', $suggestterm_id); |
| 229 |
//return 'add_success'; |
| 230 |
} |
| 231 |
|
| 232 |
return $suggestterm_id; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Successful update callback. |
| 237 |
*/ |
| 238 |
function taxopress_suggestterms_update_success_admin_notice() |
| 239 |
{ |
| 240 |
echo taxopress_admin_notices_helper(__('Settings updated successfully.', 'simple-tags')); |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Successful deleted callback. |
| 245 |
*/ |
| 246 |
function taxopress_suggestterms_delete_success_admin_notice() |
| 247 |
{ |
| 248 |
echo taxopress_admin_notices_helper(__('Suggest Terms successfully deleted.', 'simple-tags'), false); |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Filters the list of query arguments which get removed from admin area URLs in WordPress. |
| 253 |
* |
| 254 |
* @link https://core.trac.wordpress.org/ticket/23367 |
| 255 |
* |
| 256 |
* @param string[] $args Array of removable query arguments. |
| 257 |
* @return string[] Updated array of removable query arguments. |
| 258 |
*/ |
| 259 |
function taxopress_saved_suggestterm_filter_removable_query_args(array $args) |
| 260 |
{ |
| 261 |
return array_merge($args, [ |
| 262 |
'new_suggestterm', |
| 263 |
]); |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Filters the list of query arguments which get removed from admin area URLs in WordPress. |
| 268 |
* |
| 269 |
* @link https://core.trac.wordpress.org/ticket/23367 |
| 270 |
* |
| 271 |
* @param string[] $args Array of removable query arguments. |
| 272 |
* @return string[] Updated array of removable query arguments. |
| 273 |
*/ |
| 274 |
function taxopress_deleted_suggestterm_filter_removable_query_args(array $args) |
| 275 |
{ |
| 276 |
return array_merge($args, [ |
| 277 |
'deleted_suggestterm', |
| 278 |
]); |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Filters the list of query arguments which get removed from admin area URLs in WordPress. |
| 283 |
* |
| 284 |
* @link https://core.trac.wordpress.org/ticket/23367 |
| 285 |
* |
| 286 |
* @param string[] $args Array of removable query arguments. |
| 287 |
* @return string[] Updated array of removable query arguments. |
| 288 |
*/ |
| 289 |
function taxopress_delete_suggestterm_filter_removable_query_args(array $args) |
| 290 |
{ |
| 291 |
return array_merge($args, [ |
| 292 |
'action', |
| 293 |
'taxopress_suggestterms', |
| 294 |
'_wpnonce', |
| 295 |
]); |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* Delete our custom suggestterm from the array of suggestterms. |
| 300 |
* @return bool|string False on failure, string on success. |
| 301 |
*/ |
| 302 |
function taxopress_action_delete_suggestterm($suggestterm_id) |
| 303 |
{ |
| 304 |
$suggestterms = taxopress_get_suggestterm_data(); |
| 305 |
|
| 306 |
if (array_key_exists($suggestterm_id, $suggestterms)) { |
| 307 |
unset($suggestterms[$suggestterm_id]); |
| 308 |
$success = update_option('taxopress_suggestterms', $suggestterms); |
| 309 |
} |
| 310 |
|
| 311 |
if (isset($success)) { |
| 312 |
add_action('admin_notices', "taxopress_taxdeleted_admin_notice"); |
| 313 |
wp_safe_redirect( |
| 314 |
add_query_arg( |
| 315 |
[ |
| 316 |
'page' => 'st_suggestterms', |
| 317 |
'deleted_suggestterm' => 1, |
| 318 |
], |
| 319 |
taxopress_admin_url('admin.php') |
| 320 |
) |
| 321 |
); |
| 322 |
exit(); |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* Get click tags for current post |
| 328 |
* |
| 329 |
* |
| 330 |
* @return mixed |
| 331 |
*/ |
| 332 |
function taxopress_current_post_suggest_terms($local_check = false) |
| 333 |
{ |
| 334 |
global $pagenow; |
| 335 |
|
| 336 |
$allowed_pages = ['post-new.php', 'post.php', 'page.php', 'page-new.php']; |
| 337 |
if(!in_array($pagenow, $allowed_pages)){ |
| 338 |
return false; |
| 339 |
} |
| 340 |
|
| 341 |
$suggested_terms = taxopress_get_suggestterm_data(); |
| 342 |
|
| 343 |
if (count($suggested_terms) > 0) { |
| 344 |
foreach ($suggested_terms as $suggested_term) { |
| 345 |
|
| 346 |
// Get option |
| 347 |
$post_types = (isset($suggested_term['post_types']) && is_array($suggested_term['post_types']) && count($suggested_term['post_types']) > 0) ? $suggested_term['post_types'] : false; |
| 348 |
|
| 349 |
if (!$post_types) { |
| 350 |
continue; |
| 351 |
} |
| 352 |
|
| 353 |
if($local_check && isset($suggested_term['disable_local']) && (int)$suggested_term['disable_local'] > 0){ |
| 354 |
continue; |
| 355 |
} |
| 356 |
|
| 357 |
$suggest_term_use_local = isset($suggested_term['suggest_term_use_local']) ? (int)$suggested_term['suggest_term_use_local'] : 0; |
| 358 |
$suggest_term_use_dandelion = isset($suggested_term['suggest_term_use_dandelion']) ? (int)$suggested_term['suggest_term_use_dandelion'] : 0; |
| 359 |
$suggest_term_use_opencalais = isset($suggested_term['suggest_term_use_opencalais']) ? (int)$suggested_term['suggest_term_use_opencalais'] : 0; |
| 360 |
if(!$local_check && $suggest_term_use_local === 0 && $suggest_term_use_dandelion === 0 && $suggest_term_use_opencalais === 0){ |
| 361 |
continue; |
| 362 |
} |
| 363 |
|
| 364 |
if (in_array(get_post_type(), $post_types)) { |
| 365 |
return $suggested_term; |
| 366 |
} |
| 367 |
|
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
return false; |
| 372 |
} |
| 373 |
?> |