| 1 |
<?php |
| 2 |
namespace BBlocks\Inc\Posts; |
| 3 |
|
| 4 |
use BBlocks\Inc\Utils as Utils; |
| 5 |
|
| 6 |
require_once B_BLOCKS_DIR_PATH . '/includes/posts/Functions.php'; |
| 7 |
|
| 8 |
class Posts{ |
| 9 |
public function __construct(){ |
| 10 |
add_filter( 'b_blocks_posts_excerpt_filter', function( $plainText, $htmlContent ){ |
| 11 |
return $htmlContent; |
| 12 |
}, 10, 3 ); |
| 13 |
} |
| 14 |
|
| 15 |
static function query( $attributes ){ |
| 16 |
extract( $attributes ); |
| 17 |
|
| 18 |
$selectedCategories = $selectedCategories ?? []; |
| 19 |
$selectedTaxonomies = $selectedTaxonomies ?? []; |
| 20 |
|
| 21 |
$termsQuery = ['relation' => 'AND']; |
| 22 |
foreach ( $selectedTaxonomies as $taxonomy => $terms ){ |
| 23 |
if( count( $terms ) ){ |
| 24 |
$termsQuery[] = [ |
| 25 |
'taxonomy' => $taxonomy, |
| 26 |
'field' => 'term_id', |
| 27 |
'terms' => $terms, |
| 28 |
]; |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
$defaultPostQuery = 'post' === $postType ? [ |
| 33 |
'category__in' => $selectedCategories, |
| 34 |
'tag__in' => $selectedTags ?? [] |
| 35 |
] : []; |
| 36 |
|
| 37 |
$postsInclude = Functions::filterNaN( $postsInclude ?? [] ); |
| 38 |
$post__in = !empty( $postsInclude ) ? [ 'post__in' => $postsInclude ] : []; |
| 39 |
$postsExclude = Functions::filterNaN( $postsExclude ?? [] ); |
| 40 |
|
| 41 |
$query = array_merge( [ |
| 42 |
'post_type' => $postType, |
| 43 |
'posts_per_page' => $isPostsPerPageAll ? -1 : $postsPerPage, |
| 44 |
'orderby' => $postsOrderBy, |
| 45 |
'order' => $postsOrder, |
| 46 |
'tax_query' => $termsQuery, |
| 47 |
'offset' => $isPostsPerPageAll ? 0 : $postsOffset, |
| 48 |
'post__not_in' => $isExcludeCurrent ? array_merge( [ get_the_ID() ], $postsExclude ) : $postsExclude, |
| 49 |
'has_password' => false, |
| 50 |
'post_status' => 'publish' |
| 51 |
], $post__in, $defaultPostQuery ); |
| 52 |
|
| 53 |
if( Utils::isPro() ) { |
| 54 |
$query = apply_filters( 'b_blocks_posts_query', $query ); |
| 55 |
} |
| 56 |
|
| 57 |
return $query; |
| 58 |
} |
| 59 |
|
| 60 |
static function getPosts( $attributes, $pageNumber = 1 ){ |
| 61 |
extract( $attributes ); |
| 62 |
|
| 63 |
$attributes['isPostsPerPageAll'] = 'true' === $isPostsPerPageAll; |
| 64 |
$attributes['isExcludeCurrent'] = 'true' === $isExcludeCurrent; |
| 65 |
|
| 66 |
$newArgs = wp_parse_args( [ 'offset' => ( $postsPerPage * ( $pageNumber - 1 ) ) + $postsOffset ], self::query( $attributes ) ); |
| 67 |
$posts = Functions::arrangedPosts( |
| 68 |
get_posts( $newArgs ), |
| 69 |
$postType, |
| 70 |
$fImgSize, |
| 71 |
$metaDateFormat, |
| 72 |
$isExcerptFromContent || 'true' === $isExcerptFromContent, |
| 73 |
$excerptLength |
| 74 |
); |
| 75 |
|
| 76 |
return $posts; |
| 77 |
} |
| 78 |
|
| 79 |
static function skeletonArticle( $prefix ){ |
| 80 |
$articleEl = "<article class='". $prefix ."Article'> |
| 81 |
<span class='". $prefix ."LoadingItem ". $prefix ."ElThumb'></span> |
| 82 |
|
| 83 |
<div class='". $prefix ."ElTexts'> |
| 84 |
<div class='". $prefix ."ElTitle'> |
| 85 |
<span class='". $prefix ."LoadingItem'></span> |
| 86 |
<span class='". $prefix ."LoadingItem'></span> |
| 87 |
</div> |
| 88 |
<div class='". $prefix ."ElMeta'> |
| 89 |
<span class='". $prefix ."LoadingItem'></span> |
| 90 |
</div> |
| 91 |
<div class='". $prefix ."ElExcerpt'> |
| 92 |
<span class='". $prefix ."LoadingItem'></span> |
| 93 |
<span class='". $prefix ."LoadingItem'></span> |
| 94 |
<span class='". $prefix ."LoadingItem'></span> |
| 95 |
<span class='". $prefix ."LoadingItem'></span> |
| 96 |
</div> |
| 97 |
<div class='". $prefix ."ElReadMore'> |
| 98 |
<span class='". $prefix ."LoadingItem'></span> |
| 99 |
</div> |
| 100 |
</div> |
| 101 |
</article>"; |
| 102 |
|
| 103 |
ob_start(); |
| 104 |
echo wp_kses( $articleEl, [ 'article' => [ 'class' => [] ], 'div' => [ 'class' => [] ], 'span' => [ 'class' => [] ] ] ); |
| 105 |
return ob_get_clean(); |
| 106 |
} |
| 107 |
|
| 108 |
static function loadingPlaceholder( $attributes, $prefix ){ |
| 109 |
extract( $attributes ); |
| 110 |
$posts = self::getPosts( $attributes ); |
| 111 |
|
| 112 |
$colD = $columns['desktop']; |
| 113 |
$colT = $columns['tablet']; |
| 114 |
$colM = $columns['mobile']; |
| 115 |
$gridClass = $prefix ."LayoutGrid columns-$colD columns-tablet-$colT columns-mobile-$colM"; |
| 116 |
|
| 117 |
$placeholderId = wp_unique_id( $prefix .'LoadingPlaceholder-' ); |
| 118 |
|
| 119 |
$sliderStyles = "#$placeholderId .$prefix" . "SliderSkeleton article{ |
| 120 |
min-height: $sliderHeight; |
| 121 |
}"; |
| 122 |
|
| 123 |
ob_start(); ?> |
| 124 |
<div class='<?php echo esc_attr( $prefix ); ?>LoadingPlaceholder' id='<?php echo esc_attr( $placeholderId ); ?>'> |
| 125 |
<?php switch ( $layout ) { |
| 126 |
case 'grid1': ?> |
| 127 |
<div class='<?php echo esc_attr( $prefix ); ?>LayoutGrid1'> |
| 128 |
<?php foreach ( range( 1, count( $posts ) ) as $item ) { |
| 129 |
echo self::skeletonArticle( $prefix ); |
| 130 |
} ?> |
| 131 |
</div> |
| 132 |
<?php break; |
| 133 |
case 'slider': ?> |
| 134 |
<style> |
| 135 |
<?php echo esc_html( $sliderStyles ); ?> |
| 136 |
</style> |
| 137 |
<div class='<?php echo esc_attr( $prefix ); ?>SliderSkeleton'> |
| 138 |
<div class='swiper-wrapper'> |
| 139 |
<?php foreach ( range( 1, 2 ) as $item ) { |
| 140 |
echo self::skeletonArticle( $prefix ); |
| 141 |
} ?> |
| 142 |
</div> |
| 143 |
<?php echo $sliderIsPage ? "<div class='swiper-pagination'></div>" : ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Static HTML literal, no user data. ?> |
| 144 |
<?php echo $sliderIsPrevNext ? "<div class='swiper-button-prev'></div><div class='swiper-button-next'></div>" : ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Static HTML literal, no user data. ?> |
| 145 |
</div> |
| 146 |
<?php break; |
| 147 |
case 'ticker': ?> |
| 148 |
<div class='<?php echo esc_attr( $prefix ); ?>LayoutTicker'> |
| 149 |
<?php foreach ( range( 1, $tickerVisible ) as $item ) { |
| 150 |
echo self::skeletonArticle( $prefix ); |
| 151 |
} ?> |
| 152 |
</div> |
| 153 |
<?php break; |
| 154 |
default: ?> |
| 155 |
<div class='<?php echo esc_attr( $gridClass ); ?>'> |
| 156 |
<?php foreach ( range( 1, count( $posts ) ) as $item ) { |
| 157 |
echo self::skeletonArticle( $prefix ); |
| 158 |
} ?> |
| 159 |
</div> |
| 160 |
<?php break; |
| 161 |
} ?> |
| 162 |
</div> |
| 163 |
<?php return ob_get_clean(); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
if( Utils::isPro() ) { |
| 168 |
require_once B_BLOCKS_DIR_PATH . '/includes/posts/Ajax.php'; |
| 169 |
} |