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