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