| 1 |
<?php |
| 2 |
/** |
| 3 |
* Fetch our TAXOPRESS Terms Display option. |
| 4 |
* |
| 5 |
* @return mixed |
| 6 |
*/ |
| 7 |
function taxopress_get_posttags_data() |
| 8 |
{ |
| 9 |
return array_filter((array)apply_filters('taxopress_get_posttags_data', get_option('taxopress_posttagss', []), |
| 10 |
get_current_blog_id())); |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Get the selected posttags from the $_POST global. |
| 15 |
* |
| 16 |
* @return bool|string False on no result, sanitized posttags if set. |
| 17 |
* @internal |
| 18 |
* |
| 19 |
*/ |
| 20 |
function taxopress_get_current_posttags() |
| 21 |
{ |
| 22 |
|
| 23 |
$posttagss = false; |
| 24 |
|
| 25 |
if (!empty($_GET) && isset($_GET['taxopress_posttags'])) { |
| 26 |
$posttagss = sanitize_text_field($_GET['taxopress_posttags']); |
| 27 |
} else { |
| 28 |
$posttagss = taxopress_get_posttags_data(); |
| 29 |
if (!empty($posttagss)) { |
| 30 |
// Will return the first array key. |
| 31 |
$posttagss = key($posttagss); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Filters the current posttags to edit. |
| 37 |
* |
| 38 |
* @param string $posttagss posttags slug. |
| 39 |
*/ |
| 40 |
return apply_filters('taxopress_current_posttags', $posttagss); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
/** |
| 45 |
* Handle the save and deletion of posttags data. |
| 46 |
*/ |
| 47 |
function taxopress_process_posttags() |
| 48 |
{ |
| 49 |
|
| 50 |
if (wp_doing_ajax()) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
if (!is_admin()) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
if (empty($_GET)) { |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
if (!isset($_GET['page'])) { |
| 63 |
return; |
| 64 |
} |
| 65 |
if ('st_post_tags' !== $_GET['page']) { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
if(!current_user_can('simple_tags')){ |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
if (isset($_GET['new_posttags'])) { |
| 74 |
if ((int)$_GET['new_posttags'] === 1) { |
| 75 |
add_action('admin_notices', "taxopress_posttags_update_success_admin_notice"); |
| 76 |
add_filter('removable_query_args', 'taxopress_saved_posttags_filter_removable_query_args'); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
if (isset($_GET['deleted_posttags'])) { |
| 81 |
if ((int)$_GET['deleted_posttags'] === 1) { |
| 82 |
add_action('admin_notices', "taxopress_posttags_delete_success_admin_notice"); |
| 83 |
add_filter('removable_query_args', 'taxopress_deleted_posttags_filter_removable_query_args'); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
if (!empty($_POST) && isset($_POST['posttags_submit'])) { |
| 89 |
$result = ''; |
| 90 |
if (isset($_POST['posttags_submit'])) { |
| 91 |
check_admin_referer('taxopress_addedit_posttags_nonce_action', 'taxopress_addedit_posttags_nonce_field'); |
| 92 |
$result = taxopress_update_posttags($_POST); |
| 93 |
} |
| 94 |
|
| 95 |
if ($result) { |
| 96 |
wp_safe_redirect( |
| 97 |
add_query_arg( |
| 98 |
[ |
| 99 |
'page' => 'st_post_tags', |
| 100 |
'add' => 'new_item', |
| 101 |
'action' => 'edit', |
| 102 |
'taxopress_posttags' => $result, |
| 103 |
'new_posttags' => 1, |
| 104 |
], |
| 105 |
taxopress_admin_url('admin.php') |
| 106 |
) |
| 107 |
); |
| 108 |
|
| 109 |
exit(); |
| 110 |
} |
| 111 |
} elseif (isset($_REQUEST['action']) && $_REQUEST['action'] === 'taxopress-delete-posttags') { |
| 112 |
$nonce = sanitize_text_field($_REQUEST['_wpnonce']); |
| 113 |
if (wp_verify_nonce($nonce, 'posttags-action-request-nonce')) { |
| 114 |
taxopress_action_delete_posttags(sanitize_text_field($_REQUEST['taxopress_posttags'])); |
| 115 |
} |
| 116 |
add_filter('removable_query_args', 'taxopress_delete_posttags_filter_removable_query_args'); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
/** |
| 122 |
* Create default post tags. |
| 123 |
*/ |
| 124 |
function taxopress_create_default_post_tags() |
| 125 |
{ |
| 126 |
|
| 127 |
if (wp_doing_ajax()) { |
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
if (!is_admin()) { |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
if ((int)get_option('taxopress_default_posttagss') > 0) { |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
if (count(taxopress_get_posttags_data()) > 0) { |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
if(!current_user_can('simple_tags')){ |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
// Get options |
| 148 |
$options = SimpleTags_Plugin::get_option(); |
| 149 |
|
| 150 |
// Default values |
| 151 |
$defaults = [ |
| 152 |
'before' => __('Tags: ', 'simple-tags'), |
| 153 |
'separator' => ', ', |
| 154 |
'after' => '<br />', |
| 155 |
'xformat' => __('<a href="%tag_link%" title="%tag_name_attribute%" class="st-post-tags t%tag_scale%" style="%tag_size% %tag_color%" %tag_rel%>%tag_name%</a>', 'simple-tags'), |
| 156 |
'notagtext' => __('No tag for this post.', 'simple-tags'), |
| 157 |
'number' => 0, |
| 158 |
'format' => '', |
| 159 |
'smallest' => 12, |
| 160 |
'largest' => 12, |
| 161 |
'unit' => 'pt', |
| 162 |
'mincolor' => '#353535', |
| 163 |
'maxcolor' => '#000000', |
| 164 |
'color' => 1, |
| 165 |
'selectionby' => 'count', |
| 166 |
'selection' => 'desc', |
| 167 |
'orderby' => 'random', |
| 168 |
'order' => 'asc', |
| 169 |
'limit_days' => 0, |
| 170 |
]; |
| 171 |
// Get values in DB |
| 172 |
$defaults['before'] = $options['tt_before']; |
| 173 |
$defaults['separator'] = $options['tt_separator']; |
| 174 |
$defaults['after'] = $options['tt_after']; |
| 175 |
$defaults['xformat'] = $options['tt_xformat']; |
| 176 |
$defaults['notagtext'] = $options['tt_notagstext']; |
| 177 |
$defaults['number'] = (int)$options['tt_number']; |
| 178 |
$defaults['selectionby'] = $options['tt_selectionby']; |
| 179 |
$defaults['selection'] = $options['tt_selection']; |
| 180 |
$defaults['orderby'] = $options['tt_orderby']; |
| 181 |
$defaults['order'] = $options['tt_order']; |
| 182 |
$defaults['limit_days'] = (int)$options['tt_limit_days']; |
| 183 |
|
| 184 |
$post_tags_default = []; |
| 185 |
$post_tags_default['taxopress_post_tags']['title'] = esc_html__('Terms for Current Post', 'simple-tags'); |
| 186 |
$post_tags_default['taxopress_post_tags']['taxonomy'] = 'post_tag'; |
| 187 |
$post_tags_default['taxopress_post_tags']['embedded'] = []; |
| 188 |
$post_tags_default['taxopress_post_tags']['number'] = $defaults['number']; |
| 189 |
$post_tags_default['taxopress_post_tags']['separator'] = $defaults['separator']; |
| 190 |
$post_tags_default['taxopress_post_tags']['after'] = $defaults['after']; |
| 191 |
$post_tags_default['taxopress_post_tags']['before'] = $defaults['before']; |
| 192 |
$post_tags_default['taxopress_post_tags']['notagtext'] = $defaults['notagtext']; |
| 193 |
$post_tags_default['taxopress_post_tags']['xformat'] = $defaults['xformat']; |
| 194 |
$post_tags_default['taxopress_post_tags']['smallest'] = $defaults['smallest']; |
| 195 |
$post_tags_default['taxopress_post_tags']['largest'] = $defaults['largest']; |
| 196 |
$post_tags_default['taxopress_post_tags']['unit'] = $defaults['unit']; |
| 197 |
$post_tags_default['taxopress_post_tags']['mincolor'] = $defaults['mincolor']; |
| 198 |
$post_tags_default['taxopress_post_tags']['maxcolor'] = $defaults['maxcolor']; |
| 199 |
$post_tags_default['taxopress_post_tags']['color'] = $defaults['color']; |
| 200 |
$post_tags_default['taxopress_post_tags']['format'] = $defaults['format']; |
| 201 |
$post_tags_default['taxopress_post_tags']['selectionby'] = $defaults['selectionby']; |
| 202 |
$post_tags_default['taxopress_post_tags']['selection'] = $defaults['selection']; |
| 203 |
$post_tags_default['taxopress_post_tags']['orderby'] = $defaults['orderby']; |
| 204 |
$post_tags_default['taxopress_post_tags']['order'] = $defaults['order']; |
| 205 |
$post_tags_default['taxopress_post_tags']['limit_days'] = $defaults['limit_days']; |
| 206 |
$result = taxopress_update_posttags($post_tags_default); |
| 207 |
update_option('taxopress_default_posttagss', $result); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
/** |
| 212 |
* Successful update callback. |
| 213 |
*/ |
| 214 |
function taxopress_posttags_update_success_admin_notice() |
| 215 |
{ |
| 216 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 217 |
echo taxopress_admin_notices_helper(esc_html__('Settings updated successfully.', 'simple-tags')); |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Successful deleted callback. |
| 222 |
*/ |
| 223 |
function taxopress_posttags_delete_success_admin_notice() |
| 224 |
{ |
| 225 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 226 |
echo taxopress_admin_notices_helper(esc_html__('Shortcode entry successfully deleted.', 'simple-tags'), false); |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Filters the list of query arguments which get removed from admin area URLs in WordPress. |
| 231 |
* |
| 232 |
* @link https://core.trac.wordpress.org/ticket/23367 |
| 233 |
* |
| 234 |
* @param string[] $args Array of removable query arguments. |
| 235 |
* @return string[] Updated array of removable query arguments. |
| 236 |
*/ |
| 237 |
function taxopress_saved_posttags_filter_removable_query_args(array $args) |
| 238 |
{ |
| 239 |
return array_merge($args, [ |
| 240 |
'new_posttags', |
| 241 |
]); |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Filters the list of query arguments which get removed from admin area URLs in WordPress. |
| 246 |
* |
| 247 |
* @link https://core.trac.wordpress.org/ticket/23367 |
| 248 |
* |
| 249 |
* @param string[] $args Array of removable query arguments. |
| 250 |
* @return string[] Updated array of removable query arguments. |
| 251 |
*/ |
| 252 |
function taxopress_deleted_posttags_filter_removable_query_args(array $args) |
| 253 |
{ |
| 254 |
return array_merge($args, [ |
| 255 |
'deleted_posttags', |
| 256 |
]); |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Filters the list of query arguments which get removed from admin area URLs in WordPress. |
| 261 |
* |
| 262 |
* @link https://core.trac.wordpress.org/ticket/23367 |
| 263 |
* |
| 264 |
* @param string[] $args Array of removable query arguments. |
| 265 |
* @return string[] Updated array of removable query arguments. |
| 266 |
*/ |
| 267 |
function taxopress_delete_posttags_filter_removable_query_args(array $args) |
| 268 |
{ |
| 269 |
return array_merge($args, [ |
| 270 |
'action', |
| 271 |
'taxopress_posttags', |
| 272 |
'_wpnonce', |
| 273 |
]); |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Add to or update our TAXOPRESS option with new data. |
| 278 |
* |
| 279 |
* |
| 280 |
* @param array $data Array of posttags data to update. Optional. |
| 281 |
* @return bool|string False on failure, string on success. |
| 282 |
* @internal |
| 283 |
* |
| 284 |
*/ |
| 285 |
function taxopress_update_posttags($data = []) |
| 286 |
{ |
| 287 |
$sanitized_data = []; |
| 288 |
foreach ($data as $key => $value) { |
| 289 |
if (!is_array($value)) { |
| 290 |
$sanitized_data[$key] = taxopress_sanitize_text_field($value); |
| 291 |
} else { |
| 292 |
$new_value = []; |
| 293 |
foreach ($data[$key] as $option_key => $option_value) { |
| 294 |
if ($option_key === 'xformat') { |
| 295 |
$new_value[$option_key] = taxopress_sanitize_text_field($option_value); |
| 296 |
} else { |
| 297 |
$new_value[$option_key] = taxopress_sanitize_text_field($option_value); |
| 298 |
} |
| 299 |
} |
| 300 |
$sanitized_data[$key] = $new_value; |
| 301 |
} |
| 302 |
} |
| 303 |
$data = $sanitized_data; |
| 304 |
|
| 305 |
$posttagss = taxopress_get_posttags_data(); |
| 306 |
|
| 307 |
$title = $data['taxopress_post_tags']['title']; |
| 308 |
$title = str_replace('"', '', htmlspecialchars_decode($title)); |
| 309 |
$title = htmlspecialchars($title, ENT_QUOTES); |
| 310 |
$title = trim($title); |
| 311 |
$data['taxopress_post_tags']['title'] = stripslashes_deep($title); |
| 312 |
|
| 313 |
$xformat = $data['taxopress_post_tags']['xformat']; |
| 314 |
$data['taxopress_post_tags']['xformat'] = stripslashes_deep($xformat); |
| 315 |
$data['taxopress_post_tags']['embedded'] = isset($data['embedded']) ? $data['embedded'] : []; |
| 316 |
|
| 317 |
if (isset($data['edited_posttags'])) { |
| 318 |
$posttags_id = $data['edited_posttags']; |
| 319 |
$posttagss[$posttags_id] = $data['taxopress_post_tags']; |
| 320 |
$success = update_option('taxopress_posttagss', $posttagss); |
| 321 |
//return 'update_success'; |
| 322 |
} else { |
| 323 |
$posttags_id = (int)get_option('taxopress_posttags_ids_increament') + 1; |
| 324 |
$data['taxopress_post_tags']['ID'] = $posttags_id; |
| 325 |
$posttagss[$posttags_id] = $data['taxopress_post_tags']; |
| 326 |
$success = update_option('taxopress_posttagss', $posttagss); |
| 327 |
$update_id = update_option('taxopress_posttags_ids_increament', $posttags_id); |
| 328 |
//return 'add_success'; |
| 329 |
} |
| 330 |
|
| 331 |
return $posttags_id; |
| 332 |
|
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
/** |
| 337 |
* Delete our custom taxonomy from the array of taxonomies. |
| 338 |
* @return bool|string False on failure, string on success. |
| 339 |
*/ |
| 340 |
function taxopress_action_delete_posttags($posttags_id) |
| 341 |
{ |
| 342 |
$posttagss = taxopress_get_posttags_data(); |
| 343 |
|
| 344 |
if (array_key_exists($posttags_id, $posttagss)) { |
| 345 |
unset($posttagss[$posttags_id]); |
| 346 |
$success = update_option('taxopress_posttagss', $posttagss); |
| 347 |
} |
| 348 |
|
| 349 |
if (isset($success)) { |
| 350 |
add_action('admin_notices', "taxopress_taxdeleted_admin_notice"); |
| 351 |
wp_safe_redirect( |
| 352 |
add_query_arg( |
| 353 |
[ |
| 354 |
'page' => 'st_post_tags', |
| 355 |
'deleted_posttags' => 1, |
| 356 |
], |
| 357 |
taxopress_admin_url('admin.php') |
| 358 |
) |
| 359 |
); |
| 360 |
exit(); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
function taxopress_posttags_shortcode($atts) |
| 365 |
{ |
| 366 |
extract(shortcode_atts([ |
| 367 |
'id' => 0 |
| 368 |
], $atts)); |
| 369 |
|
| 370 |
$posttags_id = $id; |
| 371 |
$posttagss = taxopress_get_posttags_data(); |
| 372 |
|
| 373 |
ob_start(); |
| 374 |
if (array_key_exists($posttags_id, $posttagss)) { |
| 375 |
$posttags_arg = build_query($posttagss[$posttags_id]); |
| 376 |
|
| 377 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 378 |
echo SimpleTags_Client_PostTags::extendedPostTags($posttags_arg); |
| 379 |
|
| 380 |
} else { |
| 381 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 382 |
echo esc_html__('Invalid post terms ID.', 'simple-tags'); |
| 383 |
} |
| 384 |
|
| 385 |
$html = ob_get_clean(); |
| 386 |
|
| 387 |
return $html; |
| 388 |
|
| 389 |
|
| 390 |
} |
| 391 |
|
| 392 |
/** |
| 393 |
* Auto add current tags post to post content |
| 394 |
* |
| 395 |
* @param string $content |
| 396 |
* |
| 397 |
* @return string |
| 398 |
*/ |
| 399 |
function taxopress_posttags_the_content($content = '') |
| 400 |
{ |
| 401 |
|
| 402 |
$post_tags = taxopress_get_posttags_data(); |
| 403 |
|
| 404 |
if (count($post_tags) > 0) { |
| 405 |
foreach ($post_tags as $post_tag) { |
| 406 |
|
| 407 |
// Get option |
| 408 |
$embedded = (isset($post_tag['embedded']) && is_array($post_tag['embedded']) && count($post_tag['embedded']) > 0) ? $post_tag['embedded'] : false; |
| 409 |
|
| 410 |
if (!$embedded) { |
| 411 |
continue; |
| 412 |
} |
| 413 |
|
| 414 |
$marker = false; |
| 415 |
if (is_feed() && in_array('feed', $embedded)) { |
| 416 |
$marker = true; |
| 417 |
}elseif (is_home() && in_array('homeonly', $embedded)) { |
| 418 |
$marker = true; |
| 419 |
}elseif (is_feed() && in_array('blogonly', $embedded)) { |
| 420 |
$marker = true; |
| 421 |
}elseif (is_singular() && in_array('singleonly', $embedded)) { |
| 422 |
$marker = true; |
| 423 |
}elseif (is_singular() && in_array(get_post_type(), $embedded)) { |
| 424 |
$marker = true; |
| 425 |
} |
| 426 |
if (true === $marker) { |
| 427 |
$posttags_arg = build_query($post_tag); |
| 428 |
$content .= SimpleTags_Client_PostTags::extendedPostTags($posttags_arg); |
| 429 |
} |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
return $content; |
| 434 |
} |