| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Fetch our TAXOPRESS Terms Display option. |
| 5 |
* |
| 6 |
* @return mixed |
| 7 |
*/ |
| 8 |
function taxopress_get_tagcloud_data() |
| 9 |
{ |
| 10 |
return array_filter((array)apply_filters('taxopress_get_tagcloud_data', get_option('taxopress_tagclouds', []), get_current_blog_id())); |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Get the selected tagcloud from the $_POST global. |
| 15 |
* |
| 16 |
* @return bool|string False on no result, sanitized tagcloud if set. |
| 17 |
* @internal |
| 18 |
* |
| 19 |
*/ |
| 20 |
function taxopress_get_current_tagcloud() |
| 21 |
{ |
| 22 |
|
| 23 |
$tagclouds = false; |
| 24 |
|
| 25 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading GET parameter for display selection, no state change |
| 26 |
if (!empty($_GET) && isset($_GET['taxopress_termsdisplay'])) { |
| 27 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 28 |
$tagclouds = sanitize_text_field(wp_unslash($_GET['taxopress_termsdisplay'])); |
| 29 |
} else { |
| 30 |
$tagclouds = taxopress_get_tagcloud_data(); |
| 31 |
if (!empty($tagclouds)) { |
| 32 |
// Will return the first array key. |
| 33 |
$tagclouds = key($tagclouds); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Filters the current tagcloud to edit. |
| 39 |
* |
| 40 |
* @param string $tagclouds tagcloud slug. |
| 41 |
*/ |
| 42 |
return apply_filters('taxopress_current_tagcloud', $tagclouds); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Handle the save and deletion of tagcloud data. |
| 47 |
*/ |
| 48 |
function taxopress_process_tagcloud() |
| 49 |
{ |
| 50 |
|
| 51 |
if (wp_doing_ajax()) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
if (!is_admin()) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
if (empty($_GET)) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
if (!isset($_GET['page'])) { |
| 64 |
return; |
| 65 |
} |
| 66 |
if ('st_terms_display' !== $_GET['page']) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
if (!current_user_can('simple_tags')) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
if (isset($_GET['new_tagcloud'])) { |
| 75 |
if ((int)$_GET['new_tagcloud'] === 1) { |
| 76 |
add_action('admin_notices', "taxopress_termsdisplay_update_success_admin_notice"); |
| 77 |
add_filter('removable_query_args', 'taxopress_saved_tagcloud_filter_removable_query_args'); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
if (isset($_GET['deleted_tagcloud'])) { |
| 82 |
if ((int)$_GET['deleted_tagcloud'] === 1) { |
| 83 |
add_action('admin_notices', "taxopress_termsdisplay_delete_success_admin_notice"); |
| 84 |
add_filter('removable_query_args', 'taxopress_deleted_tagcloud_filter_removable_query_args'); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
if (!empty($_POST) && isset($_POST['tagcloud_submit'])) { |
| 90 |
$result = ''; |
| 91 |
if (isset($_POST['tagcloud_submit'])) { |
| 92 |
check_admin_referer('taxopress_addedit_tagcloud_nonce_action', 'taxopress_addedit_tagcloud_nonce_field'); |
| 93 |
$result = taxopress_update_tagcloud($_POST); |
| 94 |
} |
| 95 |
|
| 96 |
if ($result) { |
| 97 |
wp_safe_redirect( |
| 98 |
add_query_arg( |
| 99 |
[ |
| 100 |
'page' => 'st_terms_display', |
| 101 |
'add' => 'new_item', |
| 102 |
'action' => 'edit', |
| 103 |
'taxopress_termsdisplay' => $result, |
| 104 |
'new_tagcloud' => 1, |
| 105 |
], |
| 106 |
taxopress_admin_url('admin.php') |
| 107 |
) |
| 108 |
); |
| 109 |
|
| 110 |
exit(); |
| 111 |
} |
| 112 |
} elseif (isset($_REQUEST['action']) && $_REQUEST['action'] === 'taxopress-delete-tagcloud') { |
| 113 |
$nonce = isset($_REQUEST['_wpnonce']) ? sanitize_text_field(wp_unslash($_REQUEST['_wpnonce'])) : ''; |
| 114 |
if ($nonce && wp_verify_nonce($nonce, 'tagcloud-action-request-nonce')) { |
| 115 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated -- Verified by nonce check above |
| 116 |
if (isset($_REQUEST['taxopress_termsdisplay'])) { |
| 117 |
taxopress_action_delete_tagcloud(sanitize_text_field(wp_unslash($_REQUEST['taxopress_termsdisplay']))); |
| 118 |
} |
| 119 |
} |
| 120 |
add_filter('removable_query_args', 'taxopress_delete_tagcloud_filter_removable_query_args'); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
/** |
| 127 |
* Create default cloud tag. |
| 128 |
*/ |
| 129 |
function taxopress_create_default_tag_cloud() |
| 130 |
{ |
| 131 |
|
| 132 |
if (wp_doing_ajax()) { |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
if (!is_admin()) { |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
if ((int)get_option('taxopress_default_tagclouds') > 0) { |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
if (count(taxopress_get_tagcloud_data()) > 0) { |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
if (!current_user_can('simple_tags')) { |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
$default = []; |
| 153 |
$default['taxopress_tag_cloud']['title'] = esc_html__('Terms Display', 'simple-tags'); |
| 154 |
$default['taxopress_tag_cloud']['post_type'] = ''; |
| 155 |
$default['taxopress_tag_cloud']['before'] = ''; |
| 156 |
$default['taxopress_tag_cloud']['after'] = ''; |
| 157 |
$default['taxopress_tag_cloud']['taxonomy'] = 'post_tag'; |
| 158 |
$default['taxopress_tag_cloud']['max'] = 20; |
| 159 |
$default['taxopress_tag_cloud']['selectionby'] = 'count'; |
| 160 |
$default['taxopress_tag_cloud']['selection'] = 'desc'; |
| 161 |
$default['taxopress_tag_cloud']['orderby'] = 'random'; |
| 162 |
$default['taxopress_tag_cloud']['order'] = 'desc'; |
| 163 |
$default['taxopress_tag_cloud']['smallest'] = 12; |
| 164 |
$default['taxopress_tag_cloud']['largest'] = 12; |
| 165 |
$default['taxopress_tag_cloud']['unit'] = 'pt'; |
| 166 |
$default['taxopress_tag_cloud']['format'] = 'border'; |
| 167 |
$default['taxopress_tag_cloud']['color'] = 1; |
| 168 |
$default['taxopress_tag_cloud']['mincolor'] = '#353535'; |
| 169 |
$default['taxopress_tag_cloud']['maxcolor'] = '#000000'; |
| 170 |
$default['taxopress_tag_cloud']['xformat'] = '<a href="%tag_link%" id="tag-link-%tag_id%" class="st-tags t%tag_scale%" title="%tag_count% topics" %tag_rel% style="%tag_size% %tag_color%">%tag_name%</a>'; |
| 171 |
$default['taxopress_tag_cloud']['limit_days'] = 0; |
| 172 |
$default['taxopress_tag_cloud']['term_selection_mode'] = 'automatic'; |
| 173 |
$default['taxopress_tag_cloud']['include_terms'] = ''; |
| 174 |
$default['tagcloud_submit'] = 'Add Terms Display'; |
| 175 |
$default['cpt_tax_status'] = 'new'; |
| 176 |
$result = taxopress_update_tagcloud($default); |
| 177 |
update_option('taxopress_default_tagclouds', $result); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
/** |
| 182 |
* Add to or update our TAXOPRESS option with new data. |
| 183 |
* |
| 184 |
* |
| 185 |
* @param array $data Array of tagcloud data to update. Optional. |
| 186 |
* @return bool|string False on failure, string on success. |
| 187 |
* @internal |
| 188 |
* |
| 189 |
*/ |
| 190 |
function taxopress_update_tagcloud($data = []) |
| 191 |
{ |
| 192 |
$sanitized_data = []; |
| 193 |
foreach ($data as $key => $value) { |
| 194 |
if (!is_array($value)) { |
| 195 |
$sanitized_data[$key] = taxopress_sanitize_text_field($value); |
| 196 |
} else { |
| 197 |
$new_value = []; |
| 198 |
foreach ($data[$key] as $option_key => $option_value) { |
| 199 |
if ($option_key === 'xformat') { |
| 200 |
$new_value[$option_key] = taxopress_sanitize_text_field($option_value); |
| 201 |
} else { |
| 202 |
$new_value[$option_key] = taxopress_sanitize_text_field($option_value); |
| 203 |
} |
| 204 |
} |
| 205 |
$sanitized_data[$key] = $new_value; |
| 206 |
} |
| 207 |
} |
| 208 |
$data = $sanitized_data; |
| 209 |
|
| 210 |
$tagclouds = taxopress_get_tagcloud_data(); |
| 211 |
|
| 212 |
$title = $data['taxopress_tag_cloud']['title']; |
| 213 |
$title = str_replace('"', '', htmlspecialchars_decode($title)); |
| 214 |
$title = htmlspecialchars($title, ENT_QUOTES); |
| 215 |
$title = trim($title); |
| 216 |
$data['taxopress_tag_cloud']['title'] = stripslashes_deep($title); |
| 217 |
|
| 218 |
$xformat = $data['taxopress_tag_cloud']['xformat']; |
| 219 |
$data['taxopress_tag_cloud']['xformat'] = stripslashes_deep($xformat); |
| 220 |
|
| 221 |
if (!empty($data['taxopress_tag_cloud']['color'])) { |
| 222 |
$data['taxopress_tag_cloud']['color'] = taxopress_disp_boolean($data['taxopress_tag_cloud']['color']); |
| 223 |
} |
| 224 |
|
| 225 |
if (isset($data['edited_tagcloud'])) { |
| 226 |
$tagcloud_id = $data['edited_tagcloud']; |
| 227 |
$tagclouds[$tagcloud_id] = $data['taxopress_tag_cloud']; |
| 228 |
$success = update_option('taxopress_tagclouds', $tagclouds); |
| 229 |
} else { |
| 230 |
$tagcloud_id = (int)get_option('taxopress_tagcloud_ids_increament') + 1; |
| 231 |
$data['taxopress_tag_cloud']['ID'] = $tagcloud_id; |
| 232 |
$tagclouds[$tagcloud_id] = $data['taxopress_tag_cloud']; |
| 233 |
$success = update_option('taxopress_tagclouds', $tagclouds); |
| 234 |
$update_id = update_option('taxopress_tagcloud_ids_increament', $tagcloud_id); |
| 235 |
} |
| 236 |
return $tagcloud_id; |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Successful update callback. |
| 241 |
*/ |
| 242 |
function taxopress_termsdisplay_update_success_admin_notice() |
| 243 |
{ |
| 244 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 245 |
echo taxopress_admin_notices_helper(esc_html__('Settings updated successfully.', 'simple-tags')); |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Successful deleted callback. |
| 250 |
*/ |
| 251 |
function taxopress_termsdisplay_delete_success_admin_notice() |
| 252 |
{ |
| 253 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 254 |
echo taxopress_admin_notices_helper(esc_html__('Terms Display successfully deleted.', 'simple-tags'), false); |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Filters the list of query arguments which get removed from admin area URLs in WordPress. |
| 259 |
* |
| 260 |
* @link https://core.trac.wordpress.org/ticket/23367 |
| 261 |
* |
| 262 |
* @param string[] $args Array of removable query arguments. |
| 263 |
* @return string[] Updated array of removable query arguments. |
| 264 |
*/ |
| 265 |
function taxopress_saved_tagcloud_filter_removable_query_args(array $args) |
| 266 |
{ |
| 267 |
return array_merge($args, [ |
| 268 |
'new_tagcloud', |
| 269 |
]); |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Filters the list of query arguments which get removed from admin area URLs in WordPress. |
| 274 |
* |
| 275 |
* @link https://core.trac.wordpress.org/ticket/23367 |
| 276 |
* |
| 277 |
* @param string[] $args Array of removable query arguments. |
| 278 |
* @return string[] Updated array of removable query arguments. |
| 279 |
*/ |
| 280 |
function taxopress_deleted_tagcloud_filter_removable_query_args(array $args) |
| 281 |
{ |
| 282 |
return array_merge($args, [ |
| 283 |
'deleted_tagcloud', |
| 284 |
]); |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Filters the list of query arguments which get removed from admin area URLs in WordPress. |
| 289 |
* |
| 290 |
* @link https://core.trac.wordpress.org/ticket/23367 |
| 291 |
* |
| 292 |
* @param string[] $args Array of removable query arguments. |
| 293 |
* @return string[] Updated array of removable query arguments. |
| 294 |
*/ |
| 295 |
function taxopress_delete_tagcloud_filter_removable_query_args(array $args) |
| 296 |
{ |
| 297 |
return array_merge($args, [ |
| 298 |
'action', |
| 299 |
'taxopress_termsdisplay', |
| 300 |
'_wpnonce', |
| 301 |
]); |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* Delete our custom taxonomy from the array of taxonomies. |
| 306 |
* @return bool|string False on failure, string on success. |
| 307 |
*/ |
| 308 |
function taxopress_action_delete_tagcloud($tagcloud_id) |
| 309 |
{ |
| 310 |
$tagclouds = taxopress_get_tagcloud_data(); |
| 311 |
|
| 312 |
if (array_key_exists($tagcloud_id, $tagclouds)) { |
| 313 |
unset($tagclouds[$tagcloud_id]); |
| 314 |
$success = update_option('taxopress_tagclouds', $tagclouds); |
| 315 |
} |
| 316 |
|
| 317 |
if (isset($success)) { |
| 318 |
add_action('admin_notices', "taxopress_taxdeleted_admin_notice"); |
| 319 |
wp_safe_redirect( |
| 320 |
add_query_arg( |
| 321 |
[ |
| 322 |
'page' => 'st_terms_display', |
| 323 |
'deleted_tagcloud' => 1, |
| 324 |
], |
| 325 |
taxopress_admin_url('admin.php') |
| 326 |
) |
| 327 |
); |
| 328 |
exit(); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
function taxopress_termsdisplay_shortcode($atts) |
| 333 |
{ |
| 334 |
extract(shortcode_atts(array( |
| 335 |
'id' => 0 |
| 336 |
), $atts)); |
| 337 |
|
| 338 |
$tagcloud_id = $id; |
| 339 |
$tagclouds = taxopress_get_tagcloud_data(); |
| 340 |
|
| 341 |
ob_start(); |
| 342 |
if (array_key_exists($tagcloud_id, $tagclouds)) { |
| 343 |
$tagclouds[$tagcloud_id]['number'] = $tagclouds[$tagcloud_id]['max']; |
| 344 |
if (!isset($tagclouds[$tagcloud_id]['color'])) { |
| 345 |
$tagclouds[$tagcloud_id]['color'] = 'false'; |
| 346 |
} |
| 347 |
$tagcloud_arg = build_query($tagclouds[$tagcloud_id]); |
| 348 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 349 |
echo SimpleTags_Client_TagCloud::extendedTagCloud($tagcloud_arg); |
| 350 |
} |
| 351 |
|
| 352 |
$html = ob_get_clean(); |
| 353 |
return $html; |
| 354 |
} |
| 355 |
|