| 1 |
<?php |
| 2 |
/** |
| 3 |
* Inline Posts Snippet |
| 4 |
* |
| 5 |
* @package Powerkit |
| 6 |
* @subpackage Modules |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Inline Posts Class |
| 11 |
*/ |
| 12 |
class Powerkit_Inline_Posts_Snippet { |
| 13 |
|
| 14 |
/** |
| 15 |
* Post ID |
| 16 |
* |
| 17 |
* @var int |
| 18 |
*/ |
| 19 |
public $post_id = null; |
| 20 |
|
| 21 |
/** |
| 22 |
* Options |
| 23 |
* |
| 24 |
* @var int |
| 25 |
*/ |
| 26 |
public $args = array(); |
| 27 |
|
| 28 |
/** |
| 29 |
* Need Posts Count |
| 30 |
* |
| 31 |
* @var int |
| 32 |
*/ |
| 33 |
public $need_posts_count = 0; |
| 34 |
|
| 35 |
/** |
| 36 |
* Posts Offset |
| 37 |
* |
| 38 |
* @var int|bool |
| 39 |
*/ |
| 40 |
public $founded_posts = false; |
| 41 |
|
| 42 |
/** |
| 43 |
* Parent Terms |
| 44 |
* |
| 45 |
* @var array |
| 46 |
*/ |
| 47 |
public $parent_terms = array(); |
| 48 |
|
| 49 |
/** |
| 50 |
* Posts List |
| 51 |
* |
| 52 |
* @var array |
| 53 |
*/ |
| 54 |
private $posts_list = array(); |
| 55 |
|
| 56 |
/** |
| 57 |
* Exclude Posts |
| 58 |
* |
| 59 |
* @var array |
| 60 |
*/ |
| 61 |
private $exclude_posts = array(); |
| 62 |
|
| 63 |
/** |
| 64 |
* Exclude Cats |
| 65 |
* |
| 66 |
* @var array |
| 67 |
*/ |
| 68 |
private $exclude_cats = array(); |
| 69 |
|
| 70 |
/** |
| 71 |
* Constructor. Set up cacheable values and settings. |
| 72 |
* |
| 73 |
* @param array $args Related posts options. |
| 74 |
*/ |
| 75 |
public function __construct( $args = array() ) { |
| 76 |
global $post; |
| 77 |
|
| 78 |
// Set Post ID. |
| 79 |
if ( is_object( $post ) ) { |
| 80 |
$this->args['post_id'] = intval( $post->ID ); |
| 81 |
} |
| 82 |
|
| 83 |
$this->args = array_merge( array( |
| 84 |
'ids' => null, |
| 85 |
'category' => null, |
| 86 |
'tag' => null, |
| 87 |
'time_frame' => null, |
| 88 |
'orderby' => 'date', |
| 89 |
'order' => 'DESC', |
| 90 |
'count' => 1, |
| 91 |
'offset' => 0, |
| 92 |
'exclude_posts' => array(), |
| 93 |
'exclude_cats' => array(), |
| 94 |
'output_type' => 'term, parent_term', // Variables: all, posts, tags, term, parent_term, post_type. |
| 95 |
'post_id' => $this->args['post_id'], |
| 96 |
), $args ); |
| 97 |
|
| 98 |
// Check Post ID. |
| 99 |
if ( (int) $this->args['post_id'] <= 0 ) { |
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
// Set Options. |
| 104 |
$this->ids = $this->args['ids']; |
| 105 |
$this->cats = $this->args['category']; |
| 106 |
$this->tags = $this->args['tag']; |
| 107 |
$this->time_frame = $this->args['time_frame']; |
| 108 |
$this->orderby = $this->args['orderby']; |
| 109 |
$this->order = $this->args['order']; |
| 110 |
$this->need_posts_count = $this->args['count'] + $this->args['offset']; |
| 111 |
$this->offset = $this->args['offset']; |
| 112 |
$this->post_terms = $this->get_post_terms( $this->args['post_id'] ); |
| 113 |
$this->post_type = get_post_type( $this->args['post_id'] ); |
| 114 |
|
| 115 |
// Exclude Options. |
| 116 |
$this->exclude_cats = is_array( $this->args['exclude_cats'] ) ? array_map( 'intval', $this->args['exclude_cats'] ) : array(); |
| 117 |
$this->exclude_posts = is_array( $this->args['exclude_posts'] ) ? array_map( 'intval', $this->args['exclude_posts'] ) : array(); |
| 118 |
$this->exclude_posts[] = (int) $this->args['post_id']; |
| 119 |
|
| 120 |
// Check Output Type. |
| 121 |
if ( is_string( $this->args['output_type'] ) ) { |
| 122 |
$output_type = explode( ',', $this->args['output_type'] ); |
| 123 |
} elseif ( is_array( $this->args['output_type'] ) ) { |
| 124 |
$output_type = $this->args['output_type']; |
| 125 |
} else { |
| 126 |
$output_type = array(); |
| 127 |
} |
| 128 |
|
| 129 |
// Set Output Type. |
| 130 |
if ( trim( $this->ids ) ) { |
| 131 |
$output_type = array( 'posts' ); |
| 132 |
|
| 133 |
} elseif ( trim( $this->cats ) ) { |
| 134 |
|
| 135 |
$output_type = array( 'posts' ); |
| 136 |
|
| 137 |
} elseif ( trim( $this->tags ) ) { |
| 138 |
|
| 139 |
$output_type = array( 'posts' ); |
| 140 |
|
| 141 |
} elseif ( in_array( 'all', $output_type, true ) ) { |
| 142 |
|
| 143 |
$output_type = array( 'tags', 'term', 'parent_term', 'post_type' ); |
| 144 |
} |
| 145 |
|
| 146 |
$this->args['output_type'] = array_map( 'trim', $output_type ); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Get Posts |
| 151 |
* |
| 152 |
* @param array $args Query args. |
| 153 |
*/ |
| 154 |
private function query_get_posts( $args ) { |
| 155 |
|
| 156 |
// Set Query Params. |
| 157 |
$args['no_found_rows'] = true; |
| 158 |
$args['ignore_sticky_posts'] = 1; |
| 159 |
$args['category__not_in'] = $this->exclude_cats; |
| 160 |
|
| 161 |
$args = $this->query_filter_args( $args ); |
| 162 |
|
| 163 |
// Result. |
| 164 |
$new_posts_list = new WP_Query( $args ); |
| 165 |
|
| 166 |
if ( isset( $new_posts_list->posts ) && is_array( $new_posts_list->posts ) ) { |
| 167 |
$this->posts_list = array_merge( $this->posts_list, $new_posts_list->posts ); |
| 168 |
|
| 169 |
// Exclude posts list. |
| 170 |
foreach ( $new_posts_list->posts as $obj_post ) { |
| 171 |
$this->exclude_posts[] = $obj_post->ID; |
| 172 |
} |
| 173 |
|
| 174 |
// Needed count posts. |
| 175 |
$this->need_posts_count -= $this->args['count']; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Pre posts process. |
| 181 |
*/ |
| 182 |
public function posts_process() { |
| 183 |
if ( count( $this->args['output_type'] ) <= 1 ) { |
| 184 |
return; |
| 185 |
} |
| 186 |
|
| 187 |
// Process. |
| 188 |
if ( $this->posts_list ) { |
| 189 |
$ids = array(); |
| 190 |
foreach ( $this->posts_list as $post ) { |
| 191 |
$ids[] = $post->ID; |
| 192 |
} |
| 193 |
|
| 194 |
// Set new args. |
| 195 |
$args = array( |
| 196 |
'post__in' => $ids, |
| 197 |
); |
| 198 |
|
| 199 |
// Filter args. |
| 200 |
$args = $this->query_filter_args( $args ); |
| 201 |
|
| 202 |
$the_query = new WP_Query( $args ); |
| 203 |
|
| 204 |
// Rewrite posts. |
| 205 |
$this->posts_list = $the_query->posts; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Set custom args. |
| 211 |
* |
| 212 |
* @param array $args The query args. |
| 213 |
*/ |
| 214 |
public function query_filter_args( $args = array() ) { |
| 215 |
// Post offset. |
| 216 |
$args['offset'] = $this->offset; |
| 217 |
|
| 218 |
// Post order. |
| 219 |
$args['order'] = $this->order; |
| 220 |
$args['orderby'] = $this->orderby; |
| 221 |
|
| 222 |
$type_post_views = powerkit_post_views_enabled(); |
| 223 |
|
| 224 |
// Post Views. |
| 225 |
if ( $type_post_views && 'post_views' === $this->orderby ) { |
| 226 |
$args['orderby'] = $type_post_views; |
| 227 |
// Don't hide posts without views. |
| 228 |
$args['views_query']['hide_empty'] = false; |
| 229 |
} |
| 230 |
|
| 231 |
// Time Frame. |
| 232 |
if ( $this->time_frame ) { |
| 233 |
$args['date_query'] = array( |
| 234 |
array( |
| 235 |
'column' => 'post_date_gmt', |
| 236 |
'after' => $this->time_frame . ' ago', |
| 237 |
), |
| 238 |
); |
| 239 |
} |
| 240 |
|
| 241 |
return $args; |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Get Posts by filters |
| 246 |
*/ |
| 247 |
public function get_posts_by_filters() { |
| 248 |
|
| 249 |
$args = array(); |
| 250 |
|
| 251 |
// Count. |
| 252 |
$args['posts_per_page'] = $this->args['count']; |
| 253 |
|
| 254 |
// Filter by posts. |
| 255 |
if ( $this->ids ) { |
| 256 |
$ids = explode( ',', $this->ids ); |
| 257 |
|
| 258 |
$args['post__in'] = array_map( 'trim', $ids ); |
| 259 |
} |
| 260 |
|
| 261 |
// Filter by categories. |
| 262 |
if ( $this->cats ) { |
| 263 |
$cats = str_replace( ' ', '', $this->cats ); |
| 264 |
|
| 265 |
$args['category_name'] = $cats; |
| 266 |
} |
| 267 |
|
| 268 |
// Filter by tags. |
| 269 |
if ( $this->tags ) { |
| 270 |
$tags = str_replace( ' ', '', $this->tags ); |
| 271 |
|
| 272 |
$args['tag'] = $tags; |
| 273 |
} |
| 274 |
|
| 275 |
// Result. |
| 276 |
$this->query_get_posts( $args ); |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Get Related By Tags |
| 281 |
*/ |
| 282 |
public function get_output_type_tags() { |
| 283 |
if ( $this->need_posts_count <= 0 ) { |
| 284 |
return false; |
| 285 |
} |
| 286 |
|
| 287 |
// Get Tags. |
| 288 |
$tags = get_the_terms( $this->args['post_id'], 'post_tag' ); |
| 289 |
|
| 290 |
if ( ! is_wp_error( $tags ) && ! empty( $tags ) ) { |
| 291 |
|
| 292 |
if ( 'video' === get_post_type( $this->args['post_id'] ) ) { |
| 293 |
$terms_array = array(); |
| 294 |
$tax_data = array(); |
| 295 |
|
| 296 |
// Terms array. |
| 297 |
foreach ( $tags as $post_term ) { |
| 298 |
$terms_array[ $post_term->taxonomy ][] = $post_term->term_id; |
| 299 |
} |
| 300 |
|
| 301 |
// Tax data. |
| 302 |
foreach ( $terms_array as $p_tax => $p_terms ) { |
| 303 |
|
| 304 |
if ( is_array( $p_terms ) ) { |
| 305 |
$terms_array[ $p_tax ] = array_unique( $terms_array[ $p_tax ] ); |
| 306 |
} |
| 307 |
|
| 308 |
$tax_data[] = array( |
| 309 |
'taxonomy' => $p_tax, |
| 310 |
'field' => 'id', |
| 311 |
'terms' => $terms_array[ $p_tax ], |
| 312 |
); |
| 313 |
} |
| 314 |
|
| 315 |
// Query. |
| 316 |
if ( ! empty( $tax_data ) ) { |
| 317 |
$args = array( |
| 318 |
'post__not_in' => $this->exclude_posts, |
| 319 |
'posts_per_page' => $this->need_posts_count, |
| 320 |
'post_type' => $this->post_type, |
| 321 |
'tax_query' => array( |
| 322 |
'relation' => 'OR', |
| 323 |
), |
| 324 |
); |
| 325 |
|
| 326 |
$args['tax_query'] = array_merge( $args['tax_query'], $tax_data ); |
| 327 |
|
| 328 |
// Result. |
| 329 |
$this->query_get_posts( $args ); |
| 330 |
} |
| 331 |
} else { |
| 332 |
// Tags ID's. |
| 333 |
$tag_ids = array(); |
| 334 |
foreach ( $tags as $individual_tag ) { |
| 335 |
$tag_ids[] = $individual_tag->term_id; |
| 336 |
} |
| 337 |
|
| 338 |
// Query. |
| 339 |
$args = array( |
| 340 |
'tag__in' => $tag_ids, |
| 341 |
'post__not_in' => $this->exclude_posts, |
| 342 |
'posts_per_page' => $this->need_posts_count, |
| 343 |
'post_type' => $this->post_type, |
| 344 |
); |
| 345 |
|
| 346 |
// Result. |
| 347 |
$this->query_get_posts( $args ); |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
/** |
| 353 |
* Get Related By Current Term |
| 354 |
*/ |
| 355 |
public function get_output_type_current_term() { |
| 356 |
if ( $this->need_posts_count <= 0 ) { |
| 357 |
return false; |
| 358 |
} |
| 359 |
|
| 360 |
// Post terms. |
| 361 |
if ( ! empty( $this->post_terms ) ) { |
| 362 |
$terms_array = array(); |
| 363 |
$tax_data = array(); |
| 364 |
$exclude_parent = array(); |
| 365 |
|
| 366 |
// Terms array. |
| 367 |
foreach ( $this->post_terms as $post_term ) { |
| 368 |
if ( $post_term->parent ) { |
| 369 |
$exclude_parent[ $post_term->taxonomy ][] = $post_term->parent; |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
foreach ( $this->post_terms as $post_term ) { |
| 374 |
$exclude = isset( $exclude_parent[ $post_term->taxonomy ] ) ? $exclude_parent[ $post_term->taxonomy ] : array(); |
| 375 |
|
| 376 |
if ( ! in_array( $post_term->term_id, (array) $exclude, true ) ) { |
| 377 |
$terms_array[ $post_term->taxonomy ][] = $post_term->term_id; |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
// Tax data. |
| 382 |
foreach ( $terms_array as $p_tax => $p_terms ) { |
| 383 |
|
| 384 |
if ( is_array( $p_terms ) ) { |
| 385 |
$terms_array[ $p_tax ] = array_unique( $terms_array[ $p_tax ] ); |
| 386 |
} |
| 387 |
|
| 388 |
$tax_data[] = array( |
| 389 |
'taxonomy' => $p_tax, |
| 390 |
'field' => 'id', |
| 391 |
'terms' => $terms_array[ $p_tax ], |
| 392 |
); |
| 393 |
} |
| 394 |
|
| 395 |
// Query. |
| 396 |
if ( ! empty( $tax_data ) ) { |
| 397 |
$args = array( |
| 398 |
'post__not_in' => $this->exclude_posts, |
| 399 |
'posts_per_page' => $this->need_posts_count, |
| 400 |
'post_type' => $this->post_type, |
| 401 |
'tax_query' => array( |
| 402 |
'relation' => 'OR', |
| 403 |
), |
| 404 |
); |
| 405 |
|
| 406 |
$args['tax_query'] = array_merge( $args['tax_query'], $tax_data ); |
| 407 |
|
| 408 |
// Result. |
| 409 |
$this->query_get_posts( $args ); |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
/** |
| 416 |
* Get Related By Parent Term |
| 417 |
*/ |
| 418 |
public function get_output_type_parent_term() { |
| 419 |
if ( $this->need_posts_count <= 0 ) { |
| 420 |
return false; |
| 421 |
} |
| 422 |
|
| 423 |
// Parent terms. |
| 424 |
if ( ! empty( $this->post_terms ) ) { |
| 425 |
$terms_array = array(); |
| 426 |
$tax_data = array(); |
| 427 |
|
| 428 |
// Terms array. |
| 429 |
foreach ( $this->post_terms as $post_term ) { |
| 430 |
if ( 0 !== $post_term->parent ) { |
| 431 |
$terms_array[ $post_term->taxonomy ][] = $post_term->parent; |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
// Tax data. |
| 436 |
foreach ( $terms_array as $p_tax => $p_terms ) { |
| 437 |
if ( is_array( $p_terms ) ) { |
| 438 |
$terms_array[ $p_tax ] = array_unique( $terms_array[ $p_tax ] ); |
| 439 |
} |
| 440 |
|
| 441 |
$tax_data[] = array( |
| 442 |
'taxonomy' => $p_tax, |
| 443 |
'field' => 'id', |
| 444 |
'terms' => $terms_array[ $p_tax ], |
| 445 |
); |
| 446 |
} |
| 447 |
|
| 448 |
// Query. |
| 449 |
if ( ! empty( $tax_data ) ) { |
| 450 |
$args = array( |
| 451 |
'post__not_in' => $this->exclude_posts, |
| 452 |
'posts_per_page' => $this->need_posts_count, |
| 453 |
'post_type' => $this->post_type, |
| 454 |
'tax_query' => array( |
| 455 |
'relation' => 'OR', |
| 456 |
), |
| 457 |
); |
| 458 |
|
| 459 |
$args['tax_query'] = array_merge( $args['tax_query'], $tax_data ); |
| 460 |
|
| 461 |
// Result. |
| 462 |
$this->query_get_posts( $args ); |
| 463 |
} |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
/** |
| 468 |
* Get Related By Post Type |
| 469 |
*/ |
| 470 |
public function get_output_type_post_type() { |
| 471 |
if ( $this->need_posts_count <= 0 ) { |
| 472 |
return false; |
| 473 |
} |
| 474 |
|
| 475 |
// Posts by Post type. |
| 476 |
$args = array( |
| 477 |
'post__not_in' => $this->exclude_posts, |
| 478 |
'posts_per_page' => $this->need_posts_count, |
| 479 |
'post_type' => $this->post_type, |
| 480 |
); |
| 481 |
|
| 482 |
// Result. |
| 483 |
$this->query_get_posts( $args ); |
| 484 |
} |
| 485 |
|
| 486 |
/** |
| 487 |
* Get Post terms |
| 488 |
* |
| 489 |
* @param array $post_id Post ID. |
| 490 |
* @param array $exclude_tags Exclude post tags taxonomy. Optional. |
| 491 |
* @return array Post Terms. |
| 492 |
*/ |
| 493 |
public function get_post_terms( $post_id, $exclude_tags = true ) { |
| 494 |
|
| 495 |
// Get Taxonomies. |
| 496 |
$taxonomies = get_taxonomies( array( |
| 497 |
'public' => true, |
| 498 |
), 'names' ); |
| 499 |
|
| 500 |
if ( ! is_array( $taxonomies ) ) { |
| 501 |
return false; |
| 502 |
} |
| 503 |
|
| 504 |
// Exclude post tags. |
| 505 |
if ( $exclude_tags ) { |
| 506 |
if ( isset( $taxonomies['post_tag'] ) ) { |
| 507 |
unset( $taxonomies['post_tag'] ); |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
// Add Post Terms. |
| 512 |
$post_terms = array(); |
| 513 |
foreach ( $taxonomies as $tax_name => $tax_data ) { |
| 514 |
$tax_terms = get_the_terms( $post_id, $tax_name ); |
| 515 |
|
| 516 |
if ( is_array( $tax_terms ) && ! is_wp_error( $tax_terms ) ) { |
| 517 |
$post_terms = array_merge( $post_terms, $tax_terms ); |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
return $post_terms; |
| 522 |
} |
| 523 |
|
| 524 |
/** |
| 525 |
* Output posts |
| 526 |
* |
| 527 |
* @return array Inline posts |
| 528 |
*/ |
| 529 |
public function output() { |
| 530 |
|
| 531 |
// Check Post ID. |
| 532 |
if ( intval( $this->args['post_id'] ) <= 0 ) { |
| 533 |
return array(); |
| 534 |
} |
| 535 |
|
| 536 |
// Get Related Posts. |
| 537 |
foreach ( $this->args['output_type'] as $related ) { |
| 538 |
switch ( $related ) { |
| 539 |
case 'posts': |
| 540 |
$this->get_posts_by_filters(); |
| 541 |
break; |
| 542 |
case 'tags': |
| 543 |
$this->get_output_type_tags(); |
| 544 |
break; |
| 545 |
case 'term': |
| 546 |
$this->get_output_type_current_term(); |
| 547 |
break; |
| 548 |
case 'parent_term': |
| 549 |
$this->get_output_type_parent_term(); |
| 550 |
break; |
| 551 |
case 'post_type': |
| 552 |
$this->get_output_type_post_type(); |
| 553 |
break; |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
// Posts process. |
| 558 |
$this->posts_process(); |
| 559 |
|
| 560 |
// Return posts. |
| 561 |
return $this->posts_list; |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
/** |
| 566 |
* Get Inline Posts |
| 567 |
* |
| 568 |
* @param array $args Inline posts settings. |
| 569 |
*/ |
| 570 |
function powerkit_get_inline_posts( $args = array() ) { |
| 571 |
|
| 572 |
// Create Related Posts Object. |
| 573 |
$related = new Powerkit_Inline_Posts_Snippet( $args ); |
| 574 |
|
| 575 |
return $related->output(); |
| 576 |
} |
| 577 |
|