| 1 |
<?php |
| 2 |
if (!defined('ABSPATH')) exit; // if direct access |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
function related_post_recursive_sanitize_arr($array) |
| 7 |
{ |
| 8 |
|
| 9 |
foreach ($array as $key => &$value) { |
| 10 |
if (is_array($value)) { |
| 11 |
$value = related_post_recursive_sanitize_arr($value); |
| 12 |
} else { |
| 13 |
$value = wp_kses_post($value); |
| 14 |
} |
| 15 |
} |
| 16 |
|
| 17 |
return $array; |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
add_action('the_content', 'related_post_display_auto'); |
| 23 |
|
| 24 |
function related_post_display_auto($content) |
| 25 |
{ |
| 26 |
|
| 27 |
$related_post_settings = get_option('related_post_settings'); |
| 28 |
|
| 29 |
$post_types_display = isset($related_post_settings['post_types_display']) ? $related_post_settings['post_types_display'] : array(); |
| 30 |
|
| 31 |
$post_id = get_the_ID(); |
| 32 |
$post_type = get_post_type($post_id); |
| 33 |
|
| 34 |
|
| 35 |
$posttype = $post_type; |
| 36 |
$enable = isset($post_types_display[$posttype]['enable']) ? $post_types_display[$posttype]['enable'] : 'no'; |
| 37 |
$related_post_enable = get_post_meta($post_id, 'related_post_enable', true); |
| 38 |
|
| 39 |
//var_dump($enable); |
| 40 |
|
| 41 |
if ($enable == 'yes' || $related_post_enable) { |
| 42 |
|
| 43 |
$content_position = isset($post_types_display[$posttype]['content_position']) ? $post_types_display[$posttype]['content_position'] : []; |
| 44 |
$paragraph_positions = isset($post_types_display[$posttype]['paragraph_positions']) ? $post_types_display[$posttype]['paragraph_positions'] : ''; |
| 45 |
$view_type = isset($post_types_display[$posttype]['view_type']) ? $post_types_display[$posttype]['view_type'] : ''; |
| 46 |
$headline_text = isset($post_types_display[$posttype]['headline_text']) ? $post_types_display[$posttype]['headline_text'] : ''; |
| 47 |
|
| 48 |
$related_post_html = do_shortcode('[related_post post_id="' . $post_id . '" view_type="' . $view_type . '" headline="' . $headline_text . '"]'); |
| 49 |
|
| 50 |
$paragraph_positions = !empty($paragraph_positions) ? explode(',', $paragraph_positions) : array(); |
| 51 |
|
| 52 |
|
| 53 |
//var_dump($paragraph_positions); |
| 54 |
|
| 55 |
$html = ''; |
| 56 |
|
| 57 |
if (in_array('before', $content_position)) { |
| 58 |
|
| 59 |
$html .= do_shortcode('[related_post post_id="' . get_the_id() . '" view_type="' . $view_type . '" headline="' . $headline_text . '"]'); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
if (!empty($paragraph_positions) && is_singular($post_type)) { |
| 69 |
$split_by = "\n"; |
| 70 |
$content_blocks = explode($split_by, $content); |
| 71 |
$content_blocks = array_filter($content_blocks); |
| 72 |
$content_blocks_count = count($content_blocks); |
| 73 |
|
| 74 |
$positions = array(); |
| 75 |
foreach ($paragraph_positions as $position) { |
| 76 |
if (strpos($position, 'N') !== false) { |
| 77 |
|
| 78 |
|
| 79 |
$position = str_replace('N', $content_blocks_count, $position); |
| 80 |
|
| 81 |
if (strpos($position, '-') !== false) { |
| 82 |
|
| 83 |
$position = explode('-', $position); |
| 84 |
|
| 85 |
$max_pos = (int)$position[0]; |
| 86 |
$sub_pos = (int)$position[1]; |
| 87 |
|
| 88 |
$position = $max_pos - $sub_pos; |
| 89 |
} |
| 90 |
|
| 91 |
$positions[] = $position; |
| 92 |
} else { |
| 93 |
$positions[] = $position; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
$content_html = ''; |
| 99 |
|
| 100 |
$i = 1; |
| 101 |
foreach ($content_blocks as $content_block) { |
| 102 |
|
| 103 |
if (in_array($i, $positions)) { |
| 104 |
$content_html .= $content_block . $related_post_html; |
| 105 |
} else { |
| 106 |
$content_html .= $content_block; |
| 107 |
} |
| 108 |
|
| 109 |
$i++; |
| 110 |
} |
| 111 |
|
| 112 |
$html .= $content_html; |
| 113 |
} else { |
| 114 |
$html .= $content; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
if (in_array('after', $content_position)) { |
| 128 |
|
| 129 |
$html .= do_shortcode('[related_post post_id="' . get_the_id() . '" view_type="' . $view_type . '" headline="' . $headline_text . '"]'); |
| 130 |
} |
| 131 |
|
| 132 |
return $html; |
| 133 |
} else { |
| 134 |
return $content; |
| 135 |
} |
| 136 |
|
| 137 |
//var_dump($post_types_display); |
| 138 |
|
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
add_action('the_excerpt', 'related_post_display_on_excerpt'); |
| 149 |
|
| 150 |
function related_post_display_on_excerpt($excerpt) |
| 151 |
{ |
| 152 |
|
| 153 |
$related_post_settings = get_option('related_post_settings'); |
| 154 |
|
| 155 |
$post_types_display = isset($related_post_settings['post_types_display']) ? $related_post_settings['post_types_display'] : array(); |
| 156 |
|
| 157 |
|
| 158 |
global $post; |
| 159 |
$posttype = isset($post->post_type) ? $post->post_type : ''; |
| 160 |
$enable = isset($post_types_display[$posttype]['enable']) ? $post_types_display[$posttype]['enable'] : 'no'; |
| 161 |
|
| 162 |
if ($enable == 'yes') { |
| 163 |
|
| 164 |
$excerpt_position = isset($post_types_display[$posttype]['excerpt_position']) ? $post_types_display[$posttype]['excerpt_position'] : []; |
| 165 |
$headline_text = isset($post_types_display[$posttype]['headline_text']) ? $post_types_display[$posttype]['headline_text'] : ''; |
| 166 |
$view_type = isset($post_types_display[$posttype]['view_type']) ? $post_types_display[$posttype]['view_type'] : ''; |
| 167 |
|
| 168 |
|
| 169 |
$html = ''; |
| 170 |
|
| 171 |
if (in_array('before', $excerpt_position)) { |
| 172 |
|
| 173 |
$html .= do_shortcode('[related_post post_id="' . get_the_id() . '" view_type="' . $view_type . '" headline="' . $headline_text . '"]'); |
| 174 |
} |
| 175 |
$html .= $excerpt; |
| 176 |
|
| 177 |
if (in_array('after', $excerpt_position)) { |
| 178 |
|
| 179 |
$html .= do_shortcode('[related_post post_id="' . get_the_id() . '" view_type="' . $view_type . '" headline="' . $headline_text . '"]'); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
return $html; |
| 184 |
} else { |
| 185 |
return $excerpt; |
| 186 |
} |
| 187 |
|
| 188 |
//var_dump($post_types_display); |
| 189 |
|
| 190 |
} |
| 191 |
|
| 192 |
add_action('comment_form_before', 'related_post_display_comment_form_before'); |
| 193 |
|
| 194 |
function related_post_display_comment_form_before($excerpt) |
| 195 |
{ |
| 196 |
|
| 197 |
$related_post_settings = get_option('related_post_settings'); |
| 198 |
|
| 199 |
$post_types_display = isset($related_post_settings['post_types_display']) ? $related_post_settings['post_types_display'] : array(); |
| 200 |
|
| 201 |
|
| 202 |
global $post; |
| 203 |
$posttype = isset($post->post_type) ? $post->post_type : ''; |
| 204 |
$enable = isset($post_types_display[$posttype]['enable']) ? $post_types_display[$posttype]['enable'] : 'no'; |
| 205 |
|
| 206 |
if ($enable == 'yes') { |
| 207 |
|
| 208 |
$comment_form_position = isset($post_types_display[$posttype]['comment_form_position']) ? $post_types_display[$posttype]['comment_form_position'] : []; |
| 209 |
$headline_text = isset($post_types_display[$posttype]['headline_text']) ? $post_types_display[$posttype]['headline_text'] : ''; |
| 210 |
$view_type = isset($post_types_display[$posttype]['view_type']) ? $post_types_display[$posttype]['view_type'] : ''; |
| 211 |
|
| 212 |
|
| 213 |
if (in_array('before', $comment_form_position)) { |
| 214 |
|
| 215 |
echo do_shortcode('[related_post post_id="' . get_the_id() . '" view_type="' . $view_type . '" headline="' . $headline_text . '"]'); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
//var_dump($post_types_display); |
| 220 |
|
| 221 |
} |
| 222 |
|
| 223 |
add_action('comment_form_after', 'related_post_display_comment_form_after'); |
| 224 |
|
| 225 |
function related_post_display_comment_form_after($excerpt) |
| 226 |
{ |
| 227 |
|
| 228 |
$related_post_settings = get_option('related_post_settings'); |
| 229 |
|
| 230 |
$post_types_display = isset($related_post_settings['post_types_display']) ? $related_post_settings['post_types_display'] : array(); |
| 231 |
|
| 232 |
|
| 233 |
global $post; |
| 234 |
$posttype = isset($post->post_type) ? $post->post_type : ''; |
| 235 |
$enable = isset($post_types_display[$posttype]['enable']) ? $post_types_display[$posttype]['enable'] : 'no'; |
| 236 |
|
| 237 |
if ($enable == 'yes') { |
| 238 |
|
| 239 |
$comment_form_position = isset($post_types_display[$posttype]['comment_form_position']) ? $post_types_display[$posttype]['comment_form_position'] : []; |
| 240 |
$headline_text = isset($post_types_display[$posttype]['headline_text']) ? $post_types_display[$posttype]['headline_text'] : ''; |
| 241 |
$view_type = isset($post_types_display[$posttype]['view_type']) ? $post_types_display[$posttype]['view_type'] : ''; |
| 242 |
|
| 243 |
if (in_array('after', $comment_form_position)) { |
| 244 |
|
| 245 |
echo do_shortcode('[related_post post_id="' . get_the_id() . '" view_type="' . $view_type . '" headline="' . $headline_text . '"]'); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
//var_dump($post_types_display); |
| 250 |
|
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
add_filter('wp_head', 'related_post_count_stats'); |
| 270 |
|
| 271 |
|
| 272 |
function related_post_count_stats() |
| 273 |
{ |
| 274 |
|
| 275 |
$related_post_settings = get_option('related_post_settings'); |
| 276 |
$enable_stats = isset($related_post_settings['enable_stats']) ? $related_post_settings['enable_stats'] : 'disable'; |
| 277 |
|
| 278 |
if ($enable_stats != 'enable') return; |
| 279 |
|
| 280 |
$gmt_offset = get_option('gmt_offset'); |
| 281 |
$date = wp_date('Y-m-d', strtotime('+' . $gmt_offset . ' hour')); |
| 282 |
|
| 283 |
|
| 284 |
if (is_singular() && !empty($_GET['related_post_from'])) { |
| 285 |
|
| 286 |
$to_id = get_the_id(); |
| 287 |
$related_post_from = sanitize_text_field($_GET['related_post_from']); |
| 288 |
|
| 289 |
global $wpdb; |
| 290 |
$table = $wpdb->prefix . "related_post_stats"; |
| 291 |
|
| 292 |
$wpdb->query($wpdb->prepare( |
| 293 |
"INSERT INTO $table |
| 294 |
( id, from_id, to_id, date ) |
| 295 |
VALUES ( %d, %d, %d, %s )", |
| 296 |
array('', $related_post_from, $to_id, $date) |
| 297 |
|
| 298 |
)); |
| 299 |
|
| 300 |
|
| 301 |
//echo '<pre>'.var_export($_GET).'</pre>'; |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
function related_post_is_archive_display($archives) |
| 310 |
{ |
| 311 |
|
| 312 |
|
| 313 |
if (is_front_page() && is_home()) { |
| 314 |
|
| 315 |
if (in_array('front_page', $archives)) { |
| 316 |
return true; |
| 317 |
} |
| 318 |
} elseif (is_front_page()) { |
| 319 |
if (in_array('home', $archives)) { |
| 320 |
return true; |
| 321 |
} |
| 322 |
} elseif (is_home()) { |
| 323 |
if (in_array('blog', $archives)) { |
| 324 |
return true; |
| 325 |
} |
| 326 |
} else if (is_tag()) { |
| 327 |
if (in_array('post_tag', $archives)) { |
| 328 |
return true; |
| 329 |
} |
| 330 |
} else if (is_category()) { |
| 331 |
if (in_array('category', $archives)) { |
| 332 |
return true; |
| 333 |
} |
| 334 |
} else if (is_tax()) { |
| 335 |
|
| 336 |
$queried_object = get_queried_object(); |
| 337 |
$taxonomy = $queried_object->taxonomy; |
| 338 |
//echo '<pre>'.var_export($taxonomy, true).'</pre>'; |
| 339 |
if (in_array($taxonomy, $archives)) { |
| 340 |
return true; |
| 341 |
} |
| 342 |
} else if (is_author()) { |
| 343 |
if (in_array('author', $archives)) { |
| 344 |
return true; |
| 345 |
} |
| 346 |
} else if (is_search()) { |
| 347 |
if (in_array('search', $archives)) { |
| 348 |
return true; |
| 349 |
} |
| 350 |
} else if (is_year()) { |
| 351 |
if (in_array('year', $archives)) { |
| 352 |
return true; |
| 353 |
} |
| 354 |
} else if (is_month()) { |
| 355 |
if (in_array('month', $archives)) { |
| 356 |
return true; |
| 357 |
} |
| 358 |
} else if (is_date()) { |
| 359 |
if (in_array('date', $archives)) { |
| 360 |
return true; |
| 361 |
} |
| 362 |
} else { |
| 363 |
return false; |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
//add_filter('the_excerpt','related_post_excerpt_display_auto'); |
| 368 |
|
| 369 |
|
| 370 |
function related_post_excerpt_display_auto($excerpt) |
| 371 |
{ |
| 372 |
|
| 373 |
|
| 374 |
$post_id = get_the_ID(); |
| 375 |
$post_type = get_post_type($post_id); |
| 376 |
$related_post_settings = get_option('related_post_settings'); |
| 377 |
$display_auto = !empty($related_post_settings['display_auto']) ? $related_post_settings['display_auto'] : ''; |
| 378 |
$archives = !empty($related_post_settings['archives']) ? $related_post_settings['archives'] : array(); |
| 379 |
|
| 380 |
$post_types = !empty($related_post_settings['post_types']) ? $related_post_settings['post_types'] : array(); |
| 381 |
$excerpt_positions = !empty($related_post_settings['excerpt_positions']) ? $related_post_settings['excerpt_positions'] : array(); |
| 382 |
|
| 383 |
|
| 384 |
$is_archive_display = related_post_is_archive_display($archives); |
| 385 |
//echo '<pre>'.var_export($is_archive_display, true).'</pre>'; |
| 386 |
//echo '<pre>'.var_export($display_auto, true).'</pre>'; |
| 387 |
|
| 388 |
$html = ''; |
| 389 |
|
| 390 |
if ($display_auto == 'yes' && $is_archive_display && in_array($post_type, $post_types) && in_array('before', $excerpt_positions)) { |
| 391 |
$html .= do_shortcode('[related_post post_id="' . $post_id . '"]'); |
| 392 |
} |
| 393 |
|
| 394 |
$html .= $excerpt; |
| 395 |
|
| 396 |
if ($display_auto == 'yes' && $is_archive_display && in_array($post_type, $post_types) && in_array('after', $excerpt_positions)) { |
| 397 |
$html .= do_shortcode('[related_post post_id="' . $post_id . '"]'); |
| 398 |
} |
| 399 |
|
| 400 |
return $html; |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
function related_post_ajax_get_post_ids() |
| 408 |
{ |
| 409 |
$response = array(); |
| 410 |
if (! current_user_can('manage_options')) { |
| 411 |
|
| 412 |
$response['html'] = __("Sorry you are not allowed to do that", "related-post"); |
| 413 |
echo json_encode($response); |
| 414 |
|
| 415 |
die(); |
| 416 |
} |
| 417 |
|
| 418 |
$post_id = isset($_POST['post_id']) ? (int)sanitize_text_field($_POST['post_id']) : ''; |
| 419 |
$title = isset($_POST['title']) ? sanitize_text_field($_POST['title']) : ''; |
| 420 |
$any_posttypes = isset($_POST['any_posttypes']) ? sanitize_text_field($_POST['any_posttypes']) : ''; |
| 421 |
|
| 422 |
$post_type = get_post_type($post_id); |
| 423 |
$args = array('post_type' => !empty($any_posttypes) ? 'any' : array($post_type), 's' => $title, 'post__not_in' => array($post_id), 'posts_per_page' => -1, 'post_status' => 'publish',); |
| 424 |
$wp_query = new WP_Query($args); |
| 425 |
|
| 426 |
ob_start(); |
| 427 |
|
| 428 |
if ($wp_query->have_posts()) : |
| 429 |
|
| 430 |
while ($wp_query->have_posts()) : $wp_query->the_post(); |
| 431 |
|
| 432 |
$post_id = get_the_id(); |
| 433 |
$post_title = get_the_title(); |
| 434 |
|
| 435 |
?> |
| 436 |
<div post_id="<?php echo esc_attr($post_id); ?>" post_title="<?php echo esc_attr($post_title); ?>" class="item"> |
| 437 |
<span class="icon-plus"><i class="far fa-plus-square"></i></span> |
| 438 |
<span class="icon-add"><i class="fas fa-plus-square"></i></span> |
| 439 |
<span class="title-text"><?php echo esc_html($post_title); ?></span> |
| 440 |
</div> |
| 441 |
<?php |
| 442 |
|
| 443 |
endwhile; |
| 444 |
wp_reset_postdata(); |
| 445 |
|
| 446 |
endif; |
| 447 |
|
| 448 |
$response['html'] = ob_get_clean(); |
| 449 |
|
| 450 |
echo json_encode($response); |
| 451 |
|
| 452 |
die(); |
| 453 |
} |
| 454 |
|
| 455 |
add_action('wp_ajax_related_post_ajax_get_post_ids', 'related_post_ajax_get_post_ids'); |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
function pprp_post_ids_by_tax_terms($post_id = 0) |
| 463 |
{ |
| 464 |
|
| 465 |
$post_id = !empty($post_id) ? $post_id : get_the_ID(); |
| 466 |
$post_ids = array(); |
| 467 |
$post_type = get_post_type($post_id); |
| 468 |
$taxonomy_terms = related_post_get_taxonomy_terms($post_id); |
| 469 |
|
| 470 |
if (!empty($taxonomy_terms)) { |
| 471 |
foreach ($taxonomy_terms as $taxonomy => $term_ids) { |
| 472 |
foreach ($term_ids as $term_id) { |
| 473 |
$wp_query = new WP_Query( |
| 474 |
array( |
| 475 |
'post_type' => $post_type, |
| 476 |
'post_status' => 'publish', |
| 477 |
'tax_query' => array( |
| 478 |
array( |
| 479 |
'taxonomy' => $taxonomy, |
| 480 |
'field' => 'id', |
| 481 |
'terms' => $term_id, |
| 482 |
) |
| 483 |
) |
| 484 |
) |
| 485 |
); |
| 486 |
|
| 487 |
if ($wp_query->have_posts()) : |
| 488 |
$i = 0; |
| 489 |
while ($wp_query->have_posts()) : $wp_query->the_post(); |
| 490 |
$post_ids[$i] = get_the_ID(); |
| 491 |
$i++; |
| 492 |
endwhile; |
| 493 |
wp_reset_postdata(); |
| 494 |
endif; |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
//remove current post id |
| 499 |
|
| 500 |
$current_post_id = array(get_the_ID()); |
| 501 |
$post_ids = array_diff($post_ids, $current_post_id); |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
//var_dump($post_ids); |
| 507 |
|
| 508 |
return $post_ids; |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
function related_post_get_taxonomy_terms($post_id) |
| 514 |
{ |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
// get post by post id |
| 519 |
$post = get_post($post_id); |
| 520 |
|
| 521 |
// get post type by post |
| 522 |
$post_type = $post->post_type; |
| 523 |
|
| 524 |
// get post type taxonomies |
| 525 |
$taxonomies = get_object_taxonomies($post_type); |
| 526 |
$post_taxonomies_terms = array(); |
| 527 |
|
| 528 |
if (!empty($taxonomies)) |
| 529 |
foreach ($taxonomies as $taxonomy) { |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
// get the terms related to post |
| 534 |
$terms = get_the_terms($post->ID, $taxonomy); |
| 535 |
if (!empty($terms)) { |
| 536 |
$i = 0; |
| 537 |
foreach ($terms as $term) { |
| 538 |
$post_taxonomies_terms[$taxonomy][$i] = $term->term_id; |
| 539 |
$i++; |
| 540 |
} |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
return $post_taxonomies_terms; |
| 545 |
} |
| 546 |
|