| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Fetch our TAXOPRESS Related Posts option. |
| 5 |
* |
| 6 |
* @return mixed |
| 7 |
*/ |
| 8 |
function taxopress_get_relatedpost_data() |
| 9 |
{ |
| 10 |
return array_filter((array)apply_filters( |
| 11 |
'taxopress_get_relatedpost_data', |
| 12 |
get_option('taxopress_relatedposts', []), |
| 13 |
get_current_blog_id() |
| 14 |
)); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Get the selected relatedpost from the $_POST global. |
| 19 |
* |
| 20 |
* @return bool|string False on no result, sanitized relatedpost if set. |
| 21 |
* @internal |
| 22 |
* |
| 23 |
*/ |
| 24 |
function taxopress_get_current_relatedpost() |
| 25 |
{ |
| 26 |
|
| 27 |
$relatedposts = false; |
| 28 |
|
| 29 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 30 |
if (!empty($_GET) && isset($_GET['taxopress_relatedposts'])) { |
| 31 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 32 |
$relatedposts = sanitize_text_field(wp_unslash($_GET['taxopress_relatedposts'])); |
| 33 |
} else { |
| 34 |
$relatedposts = taxopress_get_relatedpost_data(); |
| 35 |
if (!empty($relatedposts)) { |
| 36 |
// Will return the first array key. |
| 37 |
$relatedposts = key($relatedposts); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Filters the current relatedpost to edit. |
| 43 |
* |
| 44 |
* @param string $relatedposts relatedpost slug. |
| 45 |
*/ |
| 46 |
return apply_filters('taxopress_current_relatedpost', $relatedposts); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Handle the save and deletion of relatedpost data. |
| 51 |
*/ |
| 52 |
function taxopress_process_relatedpost() |
| 53 |
{ |
| 54 |
|
| 55 |
if (wp_doing_ajax()) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
if (!is_admin()) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
if (empty($_GET)) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
if (!isset($_GET['page'])) { |
| 68 |
return; |
| 69 |
} |
| 70 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 71 |
$page = sanitize_key(wp_unslash($_GET['page'])); |
| 72 |
if ('st_related_posts' !== $page) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
if (!current_user_can('simple_tags')) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 81 |
if (isset($_GET['new_relatedpost'])) { |
| 82 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 83 |
if ((int) $_GET['new_relatedpost'] === 1) { |
| 84 |
add_action('admin_notices', "taxopress_relatedposts_update_success_admin_notice"); |
| 85 |
add_filter('removable_query_args', 'taxopress_saved_relatedpost_filter_removable_query_args'); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 90 |
if (isset($_GET['deleted_relatedpost'])) { |
| 91 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 92 |
if ((int) $_GET['deleted_relatedpost'] === 1) { |
| 93 |
add_action('admin_notices', "taxopress_relatedposts_delete_success_admin_notice"); |
| 94 |
add_filter('removable_query_args', 'taxopress_deleted_relatedpost_filter_removable_query_args'); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
if (!empty($_POST) && isset($_POST['relatedpost_submit'])) { |
| 100 |
$result = ''; |
| 101 |
if (isset($_POST['relatedpost_submit'])) { |
| 102 |
check_admin_referer( |
| 103 |
'taxopress_addedit_relatedpost_nonce_action', |
| 104 |
'taxopress_addedit_relatedpost_nonce_field' |
| 105 |
); |
| 106 |
$result = taxopress_update_relatedpost($_POST); |
| 107 |
} |
| 108 |
|
| 109 |
if ($result) { |
| 110 |
wp_safe_redirect( |
| 111 |
add_query_arg( |
| 112 |
[ |
| 113 |
'page' => 'st_related_posts', |
| 114 |
'add' => 'new_item', |
| 115 |
'action' => 'edit', |
| 116 |
'taxopress_relatedposts' => $result, |
| 117 |
'new_relatedpost' => 1, |
| 118 |
], |
| 119 |
taxopress_admin_url('admin.php') |
| 120 |
) |
| 121 |
); |
| 122 |
|
| 123 |
exit(); |
| 124 |
} |
| 125 |
} elseif (isset($_REQUEST['action']) && $_REQUEST['action'] === 'taxopress-delete-relatedpost') { |
| 126 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 127 |
$nonce = isset($_REQUEST['_wpnonce']) ? sanitize_text_field(wp_unslash($_REQUEST['_wpnonce'])) : ''; |
| 128 |
if ($nonce && wp_verify_nonce($nonce, 'relatedpost-action-request-nonce')) { |
| 129 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 130 |
$relatedpost_id = isset($_REQUEST['taxopress_relatedposts']) ? sanitize_text_field(wp_unslash($_REQUEST['taxopress_relatedposts'])) : ''; |
| 131 |
if ($relatedpost_id !== '') { |
| 132 |
taxopress_action_delete_relatedpost($relatedpost_id); |
| 133 |
} |
| 134 |
} |
| 135 |
add_filter('removable_query_args', 'taxopress_delete_relatedpost_filter_removable_query_args'); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
/** |
| 141 |
* Create default related post. |
| 142 |
*/ |
| 143 |
function taxopress_create_default_related_post() |
| 144 |
{ |
| 145 |
|
| 146 |
if (wp_doing_ajax()) { |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
if (!is_admin()) { |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
if (!current_user_can('simple_tags')) { |
| 155 |
return; |
| 156 |
} |
| 157 |
|
| 158 |
if ((int)get_option('taxopress_default_relatedposts') > 0) { |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
if (count(taxopress_get_relatedpost_data()) > 0) { |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
$default = []; |
| 167 |
$default['taxopress_related_post']['title'] = esc_html__('Related Posts', 'simple-tags'); |
| 168 |
$default['taxopress_related_post']['title_header'] = 'h4'; |
| 169 |
$default['taxopress_related_post']['before'] = ''; |
| 170 |
$default['taxopress_related_post']['after'] = ''; |
| 171 |
$default['taxopress_related_post']['post_types'] = ['post']; |
| 172 |
$default['taxopress_related_post']['taxonomy'] = 'post_tag'; |
| 173 |
$default['taxopress_related_post']['number'] = 5; |
| 174 |
$default['taxopress_related_post']['limit_days'] = 0; |
| 175 |
$default['taxopress_related_post']['order'] = 'count-desc'; |
| 176 |
$default['taxopress_related_post']['nopoststext'] = __('No related posts.', 'simple-tags'); |
| 177 |
$default['taxopress_related_post']['xformat'] = '<a href="%post_permalink%" title="%post_title% (%post_date%)" style="font-size:%post_size%;color:%post_color%"><img src="%post_thumb_url%" height="200" width="200" class="custom-image-class"/><br>%post_title%<br>%post_category%</a>'; |
| 178 |
$default['taxopress_related_post']['format'] = 'box'; |
| 179 |
$default['taxopress_related_post']['default_featured_media'] = STAGS_URL . '/assets/images/taxopress-white-logo.png'; |
| 180 |
$default['taxopress_related_post']['imageresolution'] = '1536x1536'; |
| 181 |
$default['taxopress_related_post']['smallest'] = 12; |
| 182 |
$default['taxopress_related_post']['largest'] = 12; |
| 183 |
$default['taxopress_related_post']['unit'] = 'pt'; |
| 184 |
$default['taxopress_related_post']['color'] = 1; |
| 185 |
$default['taxopress_related_post']['mincolor'] = '#353535'; |
| 186 |
$default['taxopress_related_post']['maxcolor'] = '#000000'; |
| 187 |
$default['relatedpost_submit'] = 'Add Related Posts'; |
| 188 |
$default['cpt_tax_status'] = 'new'; |
| 189 |
$result = taxopress_update_relatedpost($default); |
| 190 |
update_option('taxopress_default_relatedposts', $result); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
/** |
| 195 |
* Add to or update our TAXOPRESS option with new data. |
| 196 |
* |
| 197 |
* |
| 198 |
* @param array $data Array of relatedpost data to update. Optional. |
| 199 |
* @return bool|string False on failure, string on success. |
| 200 |
* @internal |
| 201 |
* |
| 202 |
*/ |
| 203 |
function taxopress_update_relatedpost($data = []) |
| 204 |
{ |
| 205 |
$sanitized_data = []; |
| 206 |
foreach ($data as $key => $value) { |
| 207 |
if (!is_array($value)) { |
| 208 |
$sanitized_data[$key] = taxopress_sanitize_text_field($value); |
| 209 |
} else { |
| 210 |
$new_value = []; |
| 211 |
foreach ($data[$key] as $option_key => $option_value) { |
| 212 |
if ($option_key === 'xformat') { |
| 213 |
$new_value[$option_key] = taxopress_sanitize_text_field($option_value); |
| 214 |
} else { |
| 215 |
$new_value[$option_key] = taxopress_sanitize_text_field($option_value); |
| 216 |
} |
| 217 |
} |
| 218 |
$sanitized_data[$key] = $new_value; |
| 219 |
} |
| 220 |
} |
| 221 |
$data = $sanitized_data; |
| 222 |
|
| 223 |
$relatedposts = taxopress_get_relatedpost_data(); |
| 224 |
|
| 225 |
$title = $data['taxopress_related_post']['title']; |
| 226 |
$title = str_replace('"', '', htmlspecialchars_decode($title)); |
| 227 |
$title = htmlspecialchars($title, ENT_QUOTES); |
| 228 |
$title = trim($title); |
| 229 |
$data['taxopress_related_post']['title'] = stripslashes_deep($title); |
| 230 |
|
| 231 |
$xformat = $data['taxopress_related_post']['xformat']; |
| 232 |
$data['taxopress_related_post']['xformat'] = stripslashes_deep($xformat); |
| 233 |
$data['taxopress_related_post']['embedded'] = isset($data['embedded']) ? $data['embedded'] : []; |
| 234 |
$data['taxopress_related_post']['post_types'] = isset($data['post_types']) ? array_map('sanitize_key', (array) $data['post_types']) : []; |
| 235 |
$data['taxopress_related_post']['taxonomy'] = isset($data['taxopress_related_post']['taxonomy']) ? sanitize_key($data['taxopress_related_post']['taxonomy']) : 'post_tag'; |
| 236 |
$allowed_image_resolutions = ['thumbnail', 'medium', 'large', '1536x1536', '2048x2048']; |
| 237 |
$data['taxopress_related_post']['imageresolution'] = isset($data['taxopress_related_post']['imageresolution']) && in_array($data['taxopress_related_post']['imageresolution'], $allowed_image_resolutions, true) |
| 238 |
? $data['taxopress_related_post']['imageresolution'] |
| 239 |
: '1536x1536'; |
| 240 |
|
| 241 |
if (isset($data['edited_relatedpost'])) { |
| 242 |
$relatedpost_id = $data['edited_relatedpost']; |
| 243 |
$relatedposts[$relatedpost_id] = $data['taxopress_related_post']; |
| 244 |
$success = update_option('taxopress_relatedposts', $relatedposts); |
| 245 |
} else { |
| 246 |
$relatedpost_id = (int)get_option('taxopress_relatedpost_ids_increament') + 1; |
| 247 |
$data['taxopress_related_post']['ID'] = $relatedpost_id; |
| 248 |
$relatedposts[$relatedpost_id] = $data['taxopress_related_post']; |
| 249 |
$success = update_option('taxopress_relatedposts', $relatedposts); |
| 250 |
$update_id = update_option('taxopress_relatedpost_ids_increament', $relatedpost_id); |
| 251 |
} |
| 252 |
|
| 253 |
return $relatedpost_id; |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Successful update callback. |
| 258 |
*/ |
| 259 |
function taxopress_relatedposts_update_success_admin_notice() |
| 260 |
{ |
| 261 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 262 |
echo taxopress_admin_notices_helper(esc_html__('Settings updated successfully.', 'simple-tags')); |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Successful deleted callback. |
| 267 |
*/ |
| 268 |
function taxopress_relatedposts_delete_success_admin_notice() |
| 269 |
{ |
| 270 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 271 |
echo taxopress_admin_notices_helper(esc_html__('Related Posts successfully deleted.', 'simple-tags'), false); |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Filters the list of query arguments which get removed from admin area URLs in WordPress. |
| 276 |
* |
| 277 |
* @link https://core.trac.wordpress.org/ticket/23367 |
| 278 |
* |
| 279 |
* @param string[] $args Array of removable query arguments. |
| 280 |
* @return string[] Updated array of removable query arguments. |
| 281 |
*/ |
| 282 |
function taxopress_saved_relatedpost_filter_removable_query_args(array $args) |
| 283 |
{ |
| 284 |
return array_merge($args, [ |
| 285 |
'new_relatedpost', |
| 286 |
]); |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* Filters the list of query arguments which get removed from admin area URLs in WordPress. |
| 291 |
* |
| 292 |
* @link https://core.trac.wordpress.org/ticket/23367 |
| 293 |
* |
| 294 |
* @param string[] $args Array of removable query arguments. |
| 295 |
* @return string[] Updated array of removable query arguments. |
| 296 |
*/ |
| 297 |
function taxopress_deleted_relatedpost_filter_removable_query_args(array $args) |
| 298 |
{ |
| 299 |
return array_merge($args, [ |
| 300 |
'deleted_relatedpost', |
| 301 |
]); |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* Filters the list of query arguments which get removed from admin area URLs in WordPress. |
| 306 |
* |
| 307 |
* @link https://core.trac.wordpress.org/ticket/23367 |
| 308 |
* |
| 309 |
* @param string[] $args Array of removable query arguments. |
| 310 |
* @return string[] Updated array of removable query arguments. |
| 311 |
*/ |
| 312 |
function taxopress_delete_relatedpost_filter_removable_query_args(array $args) |
| 313 |
{ |
| 314 |
return array_merge($args, [ |
| 315 |
'action', |
| 316 |
'taxopress_relatedposts', |
| 317 |
'_wpnonce', |
| 318 |
]); |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* Delete our custom related post from the array of related posts. |
| 323 |
* @return bool|string False on failure, string on success. |
| 324 |
*/ |
| 325 |
function taxopress_action_delete_relatedpost($relatedpost_id) |
| 326 |
{ |
| 327 |
$relatedposts = taxopress_get_relatedpost_data(); |
| 328 |
|
| 329 |
if (array_key_exists($relatedpost_id, $relatedposts)) { |
| 330 |
unset($relatedposts[$relatedpost_id]); |
| 331 |
$success = update_option('taxopress_relatedposts', $relatedposts); |
| 332 |
} |
| 333 |
|
| 334 |
if (isset($success)) { |
| 335 |
add_action('admin_notices', "taxopress_taxdeleted_admin_notice"); |
| 336 |
wp_safe_redirect( |
| 337 |
add_query_arg( |
| 338 |
[ |
| 339 |
'page' => 'st_related_posts', |
| 340 |
'deleted_relatedpost' => 1, |
| 341 |
], |
| 342 |
taxopress_admin_url('admin.php') |
| 343 |
) |
| 344 |
); |
| 345 |
exit(); |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
function taxopress_relatedposts_shortcode($atts) |
| 350 |
{ |
| 351 |
extract(shortcode_atts([ |
| 352 |
'id' => 0 |
| 353 |
], $atts)); |
| 354 |
|
| 355 |
$relatedpost_id = $id; |
| 356 |
$relatedposts = taxopress_get_relatedpost_data(); |
| 357 |
|
| 358 |
ob_start(); |
| 359 |
if (array_key_exists($relatedpost_id, $relatedposts)) { |
| 360 |
$related_post_array = $relatedposts[$relatedpost_id]; |
| 361 |
$relatedpost_arg = build_query($related_post_array); |
| 362 |
|
| 363 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 364 |
echo SimpleTags_Client_RelatedPosts::get_related_posts($relatedpost_arg); |
| 365 |
} else { |
| 366 |
echo esc_html__('Related Posts not found.', 'simple-tags'); |
| 367 |
} |
| 368 |
|
| 369 |
$html = ob_get_clean(); |
| 370 |
|
| 371 |
return $html; |
| 372 |
} |
| 373 |
|
| 374 |
/** |
| 375 |
* Auto add related post to post content |
| 376 |
* |
| 377 |
* @param string $content |
| 378 |
* |
| 379 |
* @return string |
| 380 |
*/ |
| 381 |
function taxopress_relatedposts_the_content($content = '') |
| 382 |
{ |
| 383 |
|
| 384 |
$post_tags = taxopress_get_relatedpost_data(); |
| 385 |
|
| 386 |
if (count($post_tags) > 0) { |
| 387 |
foreach ($post_tags as $post_tag) { |
| 388 |
// Get option |
| 389 |
$embedded = (isset($post_tag['embedded']) && is_array($post_tag['embedded']) && count($post_tag['embedded']) > 0) ? $post_tag['embedded'] : false; |
| 390 |
|
| 391 |
if (!$embedded) { |
| 392 |
continue; |
| 393 |
} |
| 394 |
|
| 395 |
$marker = false; |
| 396 |
if (is_feed() && in_array('feed', $embedded)) { |
| 397 |
$marker = true; |
| 398 |
} elseif (is_home() && in_array('homeonly', $embedded)) { |
| 399 |
$marker = true; |
| 400 |
} elseif (is_feed() && in_array('blogonly', $embedded)) { |
| 401 |
$marker = true; |
| 402 |
} elseif (is_singular() && in_array('singleonly', $embedded)) { |
| 403 |
$marker = true; |
| 404 |
} elseif (is_singular() && in_array(get_post_type(), $embedded)) { |
| 405 |
$marker = true; |
| 406 |
} |
| 407 |
if (true === $marker) { |
| 408 |
$relatedpost_arg = build_query($post_tag); |
| 409 |
$content .= SimpleTags_Client_RelatedPosts::get_related_posts($relatedpost_arg); |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
return $content; |
| 415 |
} |
| 416 |
|