| 1 |
<?php |
| 2 |
class BBlocksPosts extends BBlocks{ |
| 3 |
public function __construct(){ |
| 4 |
add_action( 'init', [$this, 'register'] ); |
| 5 |
add_action( 'rest_api_init', [$this, 'customRestAPI'] ); |
| 6 |
} |
| 7 |
|
| 8 |
function register(){ |
| 9 |
$this::registerScripts( 'posts', array( |
| 10 |
'attributes' => $this->attributes(), |
| 11 |
'render_callback' => [$this, 'render'] |
| 12 |
) ); |
| 13 |
} |
| 14 |
|
| 15 |
function attributes(){ |
| 16 |
return array( |
| 17 |
'align' => array( 'type' => 'string', 'default' => 'wide' ), |
| 18 |
'cId' => array( 'type' => 'string', 'default' => '' ), |
| 19 |
|
| 20 |
'layout' => array( 'type' => 'string', 'default' => 'grid' ), |
| 21 |
'subLayout' => array( 'type' => 'string', 'default' => 'default' ), |
| 22 |
'columns' => array( 'type' => 'object', 'default' => array( 'desktop' => 3, 'tablet' => 2, 'mobile' => 1 ) ), |
| 23 |
'columnGap' => array( 'type' => 'number', 'default' => 15 ), |
| 24 |
'rowGap' => array( 'type' => 'number', 'default' => 15 ), |
| 25 |
'isContentEqualHight' => array( 'type' => 'boolean', 'default' => true ), |
| 26 |
|
| 27 |
'postType' => array( 'type' => 'string', 'default' => 'post' ), |
| 28 |
'selectedCategories' => array( 'type' => 'array', 'default' => array() ), |
| 29 |
'isPostsPerPageAll' => array( 'type' => 'boolean', 'default' => false ), |
| 30 |
'postsPerPage' => array( 'type' => 'number', 'default' => 12 ), |
| 31 |
'postsOrderBy' => array( 'type' => 'string', 'default' => 'date' ), |
| 32 |
'postsOrder' => array( 'type' => 'string', 'default' => 'desc' ), |
| 33 |
|
| 34 |
'contentAlign' => array( 'type' => 'string', 'default' => 'left' ), |
| 35 |
'contentBG' => array( 'type' => 'object', 'default' => array( 'color' => '#f4f2fc' ) ), |
| 36 |
'contentPadding' => array( 'type' => 'object', 'default' => array( 'vertical' => '20px', 'horizontal' => '25px' ) ), |
| 37 |
'border' => array( 'type' => 'object', 'default' => array( 'radius' => '5px' ) ), |
| 38 |
|
| 39 |
'isFImg' => array( 'type' => 'boolean', 'default' => true ), |
| 40 |
'isFImgLink' => array( 'type' => 'boolean', 'default' => false ), |
| 41 |
|
| 42 |
'isTitle' => array( 'type' => 'boolean', 'default' => true ), |
| 43 |
'isTitleLink' => array( 'type' => 'boolean', 'default' => true ), |
| 44 |
'titleTypo' => array( 'type' => 'object', 'default' => array( 'fontFamily' => 'Roboto', 'fontSize' => 25, 'googleFontLink' => 'https://fonts.googleapis.com/css2?family=Roboto&display=swap' ) ), |
| 45 |
'titleColor' => array( 'type' => 'string', 'default' => '#4527a4' ), |
| 46 |
'titleMargin' => array( 'type' => 'object', 'default' => array( 'bottom' => '15px' ) ), |
| 47 |
|
| 48 |
'isMeta' => array( 'type' => 'boolean', 'default' => true ), |
| 49 |
'isMetaAuthor' => array( 'type' => 'boolean', 'default' => true ), |
| 50 |
'isMetaDate' => array( 'type' => 'boolean', 'default' => true ), |
| 51 |
'isMetaCategory' => array( 'type' => 'boolean', 'default' => true ), |
| 52 |
'metaCategoryIn' => array( 'type' => 'string', 'default' => 'content' ), |
| 53 |
'isMetaComment' => array( 'type' => 'boolean', 'default' => false ), |
| 54 |
'metaTypo' => array( 'type' => 'object', 'default' => array( 'fontSize' => 13, 'textTransform' => 'uppercase' ) ), |
| 55 |
'metaTextColor' => array( 'type' => 'string', 'default' => '#333' ), |
| 56 |
'metaLinkColor' => array( 'type' => 'string', 'default' => '#8344c5' ), |
| 57 |
'metaIconColor' => array( 'type' => 'string', 'default' => '#4527a4' ), |
| 58 |
'metaMargin' => array( 'type' => 'object', 'default' => array( 'bottom' => '15px' ) ), |
| 59 |
|
| 60 |
'isExcerpt' => array( 'type' => 'boolean', 'default' => true ), |
| 61 |
'excerptLength' => array( 'type' => 'number', 'default' => 25 ), |
| 62 |
'excerptAlign' => array( 'type' => 'string', 'default' => 'justify' ), |
| 63 |
'excerptTypo' => array( 'type' => 'object', 'default' => array( 'fontSize' => 15 ) ), |
| 64 |
'excerptColor' => array( 'type' => 'string', 'default' => '#333' ), |
| 65 |
'excerptMargin' => array( 'type' => 'object', 'default' => array( 'bottom' => '10px' ) ), |
| 66 |
|
| 67 |
'isReadMore' => array( 'type' => 'boolean', 'default' => true ), |
| 68 |
'readMoreLabel' => array( 'type' => 'string', 'default' => 'Read More' ), |
| 69 |
'isLinkNewTab' => array( 'type' => 'boolean', 'default' => false ), |
| 70 |
'readMoreAlign' => array( 'type' => 'string', 'default' => 'left' ), |
| 71 |
'readMoreTypo' => array( 'type' => 'object', 'default' => array( 'fontSize' => 14, 'textTransform' => 'uppercase', 'fontWeight' => 600 ) ), |
| 72 |
'readMoreColors' => array( 'type' => 'object', 'default' => array( 'color' => '#fff', 'bg' => '#8344c5' ) ), |
| 73 |
'readMoreHovColors' => array( 'type' => 'object', 'default' => array( 'color' => '#fff', 'bg' => '#4527a4' ) ), |
| 74 |
'readMorePadding' => array( 'type' => 'object', 'default' => array( 'vertical' => '12px', 'horizontal' => '35px' ) ), |
| 75 |
'readMoreBorder' => array( 'type' => 'object', 'default' => array( 'radius' => '3px' ) ) |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
function render( $attributes ){ |
| 80 |
extract( $attributes ); |
| 81 |
|
| 82 |
// $taxonomies = get_object_taxonomies( $postType, 'array' ); |
| 83 |
// foreach( $taxonomies as $tax ){ |
| 84 |
// if( $tax->hierarchical ){ |
| 85 |
// $terms = get_terms( $tax->name, array( |
| 86 |
// 'orderby' => 'term_id', |
| 87 |
// 'order' => 'ASC', |
| 88 |
// 'hide_empty' => true, |
| 89 |
// ) ); |
| 90 |
// } |
| 91 |
// } |
| 92 |
|
| 93 |
// Generate Styles |
| 94 |
$postsStyles = new BBlocksStyleGenerator(); |
| 95 |
$postsStyles::addStyle( "#bBlocksPosts-$cId .bBlocksPost", array( |
| 96 |
'margin-bottom' => 'masonry' === $layout ? $rowGap.'px' : '0px', |
| 97 |
$border['styles'] ?? 'border-radius: 5px;' => '' |
| 98 |
) ); |
| 99 |
$postsStyles::addStyle( "#bBlocksPosts-$cId .bBlocksPostDefault, #bBlocksPosts-$cId .bBlocksPostSideImage", array( |
| 100 |
'text-align' => $contentAlign, |
| 101 |
$contentBG['styles'] ?? 'background-color: #f4f2fc;' => '' |
| 102 |
) ); |
| 103 |
$postsStyles::addStyle( "#bBlocksPosts-$cId .bBlocksPost .bBlocksPostText", array( |
| 104 |
'padding' => $contentPadding['styles'] ?? '20px 25px' |
| 105 |
) ); |
| 106 |
$postsStyles::addStyle( "#bBlocksPosts-$cId .bBlocksPostOverlay .bBlocksPostText", array( |
| 107 |
$contentBG['styles'] ?? 'background-color: #f4f2fc;' => '', |
| 108 |
'align-items' => 'left' === $contentAlign ? 'flex-start' : ( 'right' === $contentAlign ? 'flex-end' : ( 'center' === $contentAlign ? 'center' : 'stretch' ) ) |
| 109 |
) ); |
| 110 |
$postsStyles::addStyle( "#bBlocksPosts-$cId .bBlocksPost .bBlocksPostTitle", array( |
| 111 |
'text-align' => $contentAlign, |
| 112 |
$titleTypo['styles'] ?? 'font-size: 25px;' => '', |
| 113 |
'color' => $titleColor, |
| 114 |
'margin' => $titleMargin['styles'] ?? '0 0 15px 0' |
| 115 |
) ); |
| 116 |
$postsStyles::addStyle( "#bBlocksPosts-$cId .bBlocksPost .bBlocksPostTitle a", array( 'color' => $titleColor ) ); |
| 117 |
$postsStyles::addStyle( "#bBlocksPosts-$cId .bBlocksPost .bBlocksPostMeta", array( |
| 118 |
'text-align' => $contentAlign, |
| 119 |
$metaTypo['styles'] ?? 'font-size: 13px; text-transform: uppercase;' => '', |
| 120 |
'color' => $metaTextColor, |
| 121 |
'margin' => $metaMargin['styles'] ?? '0 0 15px 0' |
| 122 |
) ); |
| 123 |
$postsStyles::addStyle( "#bBlocksPosts-$cId .bBlocksPost .bBlocksPostMeta a", array( |
| 124 |
'color' => $metaLinkColor |
| 125 |
) ); |
| 126 |
$postsStyles::addStyle( "#bBlocksPosts-$cId .bBlocksPost .bBlocksPostMeta .dashicons", array( |
| 127 |
'color' => $metaIconColor |
| 128 |
) ); |
| 129 |
$postsStyles::addStyle( "#bBlocksPosts-$cId .bBlocksPost .bBlocksPostFImgCats", array( |
| 130 |
$metaTypo['styles'] ?? 'font-size: 13px; text-transform: uppercase;' => '' |
| 131 |
) ); |
| 132 |
$postsStyles::addStyle( "#bBlocksPosts-$cId .bBlocksPost .bBlocksPostExcerpt", array( |
| 133 |
'text-align' => $excerptAlign, |
| 134 |
$excerptTypo['styles'] ?? 'font-size: 15px;' => '', |
| 135 |
'color' => $excerptColor, |
| 136 |
'margin' => $excerptMargin['styles'] ?? '0 0 10px 0' |
| 137 |
) ); |
| 138 |
$postsStyles::addStyle( "#bBlocksPosts-$cId .bBlocksPost .bBlocksPostReadMore", array( |
| 139 |
'text-align' => $readMoreAlign |
| 140 |
) ); |
| 141 |
$postsStyles::addStyle( "#bBlocksPosts-$cId .bBlocksPost .bBlocksPostReadMore a", array( |
| 142 |
$readMoreTypo['styles'] ?? 'font-size: 14px; text-transform: uppercase; font-weight: 600;' => '', |
| 143 |
$readMoreColors['styles'] ?? 'color: #fff; background: #8344c5;' => '', |
| 144 |
'padding' => $readMorePadding['styles'] ?? '12px 35px', |
| 145 |
$readMoreBorder['styles'] ?? 'border-radius: 3px;' => '' |
| 146 |
) ); |
| 147 |
$postsStyles::addStyle( "#bBlocksPosts-$cId .bBlocksPost .bBlocksPostReadMore a:hover", array( |
| 148 |
$readMoreHovColors['styles'] ?? 'color: #fff; background: #4527a4;' => '' |
| 149 |
) ); |
| 150 |
$postsStyles::addStyle( "#bBlocksPosts-$cId .bBlocksGridPosts", array( |
| 151 |
'grid-gap' => $rowGap .'px '. $columnGap .'px', |
| 152 |
'align-items' => false === $isContentEqualHight ? 'start' : 'initial' |
| 153 |
) ); |
| 154 |
$postsStyles::addStyle( "#bBlocksPosts-$cId .bBlocksMasonryPosts", array( |
| 155 |
'gap' => $columnGap . 'px' |
| 156 |
) ); |
| 157 |
|
| 158 |
// All Posts |
| 159 |
$posts = get_posts( [ |
| 160 |
'post_type' => $postType, |
| 161 |
'category' => $selectedCategories, |
| 162 |
'posts_per_page' => $isPostsPerPageAll ? -1 : $postsPerPage, |
| 163 |
'orderby' => $postsOrderBy, |
| 164 |
'order' => $postsOrder |
| 165 |
] ); |
| 166 |
|
| 167 |
ob_start(); ?> |
| 168 |
<div class='wp-block-b-blocks-posts bBlocksPosts <?php echo 'align' . esc_attr( $align ); ?>' id='bBlocksPosts-<?php echo esc_attr( $cId ); ?>'> |
| 169 |
<style> |
| 170 |
@import url(<?php echo esc_url( $titleTypo['googleFontLink'] ?? 'https://fonts.googleapis.com/css2?family=Roboto&display=swap' ); ?>); |
| 171 |
@import url(<?php echo esc_url( $metaTypo['googleFontLink'] ?? '' ); ?>); |
| 172 |
@import url(<?php echo esc_url( $excerptTypo['googleFontLink'] ?? '' ); ?>); |
| 173 |
@import url(<?php echo esc_url( $readMoreTypo['googleFontLink'] ?? '' ); ?>); |
| 174 |
<?php echo wp_kses( $postsStyles::renderStyle(), array() ); ?> |
| 175 |
|
| 176 |
<?php foreach ( $posts as $post ) { |
| 177 |
$imgUrl = get_the_post_thumbnail_url( $post->ID ); |
| 178 |
$displayCSS = $imgUrl ? 'grid' : 'flex'; |
| 179 |
|
| 180 |
$sideImgCSS = "#bBlocksPosts-$cId .bBlocksPostSideImage.bBlocksPost-$post->ID{ display: $displayCSS; }"; |
| 181 |
$fImgCSS = $isFImg && $imgUrl ? "#bBlocksPosts-$cId .bBlocksPostOverlay.bBlocksPost-$post->ID, #bBlocksPosts-$cId .bBlocksPost .bBlocksPostFImg-$post->ID{ background-image: url( $imgUrl ); }" : ''; |
| 182 |
|
| 183 |
echo esc_html( $sideImgCSS . $fImgCSS ); |
| 184 |
} ?> |
| 185 |
</style> |
| 186 |
|
| 187 |
|
| 188 |
<?php if( 'grid' === $layout ){ ?> |
| 189 |
<div class='bBlocksGridPosts columns-<?php echo esc_attr( $columns['desktop'] ); ?> columns-tablet-<?php echo esc_attr( $columns['tablet'] ); ?> columns-mobile-<?php echo esc_attr( $columns['mobile'] ); ?>'> |
| 190 |
<?php echo $this->foreachPosts( $attributes, $posts ); ?> |
| 191 |
</div> |
| 192 |
<?php }else if( 'masonry' === $layout ){ ?> |
| 193 |
<div class='bBlocksMasonryPosts cols-<?php echo esc_attr( $columns['desktop'] ); ?> cols-tablet-<?php echo esc_attr( $columns['tablet'] ); ?> cols-mobile-<?php echo esc_attr( $columns['mobile'] ); ?>'> |
| 194 |
<?php $this->foreachPosts( $attributes, $posts ); ?> |
| 195 |
</div> |
| 196 |
<?php }else{ echo ''; } ?> |
| 197 |
</div> |
| 198 |
<?php $postsStyles::$styles = array(); // Empty styles |
| 199 |
return ob_get_clean(); |
| 200 |
} // Render |
| 201 |
|
| 202 |
// ForEach Posts |
| 203 |
function foreachPosts( $attributes, $posts ){ |
| 204 |
extract( $attributes ); |
| 205 |
$subLayout = $subLayout ?? 'default'; |
| 206 |
|
| 207 |
foreach ( $posts as $post ) { |
| 208 |
if ( 'default' === $subLayout || 'title-meta' === $subLayout ) { |
| 209 |
echo $this->defaultLayout( $attributes, $post ); |
| 210 |
} else if ( 'left-image' === $subLayout || 'right-image' === $subLayout ) { |
| 211 |
echo $this->sideImgLayout( $attributes, $post ); |
| 212 |
} else if ( 'overlay-content' === $subLayout || 'overlay-content-hover' === $subLayout || 'overlay-box' === $subLayout ) { |
| 213 |
echo $this->overlayLayout( $attributes, $post ); |
| 214 |
} else { ?><p><?php _e( 'Please, select a sub layout', 'b-blocks' ); ?></p><?php } |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
// layout Components |
| 219 |
function defaultLayout( $attributes, $post ) { |
| 220 |
extract( $attributes ); |
| 221 |
$layout = $layout ?? 'grid'; |
| 222 |
$subLayout = $subLayout ?? 'default'; |
| 223 |
|
| 224 |
$titleMetaFilter = 'title-meta' !== $subLayout ? $this->postExcerpt( $attributes, $post ) . $this->postReadMore( $attributes, $post ) : ''; |
| 225 |
|
| 226 |
ob_start(); ?> |
| 227 |
<article class='bBlocksPost bBlocksPost-<?php echo esc_attr( $post->ID ); ?> bBlocksPostDefault'> |
| 228 |
<?php echo $this->postFeatureImg( $attributes, $post ); ?> |
| 229 |
|
| 230 |
<div class='bBlocksPostText'> |
| 231 |
<?php echo $this->postTitle( $attributes, $post ) . $this->postMetaData( $attributes, $post ) . $titleMetaFilter; ?> |
| 232 |
</div> |
| 233 |
</article> |
| 234 |
<?php return ob_get_clean(); |
| 235 |
} // Default |
| 236 |
|
| 237 |
function sideImgLayout( $attributes, $post ) { |
| 238 |
extract( $attributes ); |
| 239 |
$layout = $layout ?? 'grid'; |
| 240 |
$subLayout = $subLayout ?? 'default'; |
| 241 |
|
| 242 |
ob_start(); ?> |
| 243 |
<article class='bBlocksPost bBlocksPost-<?php echo esc_attr( $post->ID ); ?> bBlocksPostSideImage <?php echo 'left-image' === $subLayout ? 'leftImage' : ( 'right-image' === $subLayout ? 'rightImage' : '' ); ?>'> |
| 244 |
<?php echo 'left-image' === $subLayout ? $this->postFeatureImg( $attributes, $post ) : ''; ?> |
| 245 |
|
| 246 |
<div class='bBlocksPostText'> |
| 247 |
<?php echo $this->postTitle( $attributes, $post ) . $this->postMetaData( $attributes, $post ) . $this->postExcerpt( $attributes, $post ) . $this->postReadMore( $attributes, $post ); ?> |
| 248 |
</div> |
| 249 |
|
| 250 |
<?php echo 'right-image' === $subLayout ? $this->postFeatureImg( $attributes, $post ) : ''; ?> |
| 251 |
</article> |
| 252 |
<?php return ob_get_clean(); |
| 253 |
} // Side Image |
| 254 |
|
| 255 |
function overlayLayout( $attributes, $post ) { |
| 256 |
extract( $attributes ); |
| 257 |
|
| 258 |
$imgUrl = get_the_post_thumbnail_url( $post->ID ); |
| 259 |
|
| 260 |
ob_start(); ?> |
| 261 |
<article class='bBlocksPost bBlocksPost-<?php echo esc_attr( $post->ID ); ?> bBlocksPostOverlay <?php echo 'overlay-content-hover' === $subLayout && $imgUrl ? 'bBlocksPostOverlayHover' : ''; ?> <?php echo 'overlay-box' === $subLayout ? 'bBlocksPostOverlayBox' : ''; ?>'> |
| 262 |
<div class='bBlocksPostText'> |
| 263 |
<?php echo $this->postTitle( $attributes, $post ) . $this->postMetaData( $attributes, $post ); ?> |
| 264 |
|
| 265 |
<?php echo 'overlay-box' !== $subLayout ? $this->postExcerpt( $attributes, $post ) . $this->postReadMore( $attributes, $post ) : ''; ?> |
| 266 |
</div> |
| 267 |
</article> |
| 268 |
<?php return ob_get_clean(); |
| 269 |
} // Overlay |
| 270 |
|
| 271 |
// Single Components |
| 272 |
function postFeatureImg( $attributes, $post ) { |
| 273 |
extract( $attributes ); |
| 274 |
|
| 275 |
$imgUrl = get_the_post_thumbnail_url( $post->ID ); |
| 276 |
$tab = $isLinkNewTab ? '_blank' : '_self'; |
| 277 |
|
| 278 |
if( $isFImg && $imgUrl ){ |
| 279 |
ob_start(); ?> |
| 280 |
<figure class='bBlocksPostFImg bBlocksPostFImg-<?php echo esc_attr( $post->ID ); ?>'> |
| 281 |
<?php echo $isFImgLink ? "<a href=". esc_url( get_post_permalink( $post->ID ) ) ." target='$tab' rel='noreferrer'></a>" : ''; ?> |
| 282 |
|
| 283 |
<?php echo $isMeta && $isMetaCategory && 'image' === $metaCategoryIn ? "<div class='bBlocksPostFImgCats'>". get_the_category_list( ' ', '', $post->ID ) ."</div>" : ''; ?> |
| 284 |
</figure> |
| 285 |
<?php return ob_get_clean(); |
| 286 |
}else{ |
| 287 |
return ''; |
| 288 |
} |
| 289 |
} // Feature Image |
| 290 |
|
| 291 |
function postTitle( $attributes, $post ) { |
| 292 |
extract( $attributes ); |
| 293 |
|
| 294 |
$tab = $isLinkNewTab ? '_blank' : '_self'; |
| 295 |
|
| 296 |
if ( $isTitle ) { |
| 297 |
ob_start(); ?> |
| 298 |
<h2 class='bBlocksPostTitle'> |
| 299 |
<?php echo $isTitleLink ? "<a href=". esc_url( get_post_permalink( $post->ID ) ) ." target='$tab' rel='noreferrer'>$post->post_title</a>" : $post->post_title; ?> |
| 300 |
</h2> |
| 301 |
<?php return ob_get_clean(); |
| 302 |
} else { |
| 303 |
return ''; |
| 304 |
} |
| 305 |
} // Title |
| 306 |
|
| 307 |
function postMetaData( $attributes, $post ) { |
| 308 |
extract( $attributes ); |
| 309 |
|
| 310 |
if ( $isMeta ) { |
| 311 |
ob_start(); ?> |
| 312 |
<div class='bBlocksPostMeta'> |
| 313 |
<?php echo $this->metaAuthor( $attributes, $post ) . $this->metaDate( $attributes, $post ) . $this->metaCategories( $attributes, $post ) . $this->metaComment( $attributes, $post ); ?> |
| 314 |
</div> |
| 315 |
<?php return ob_get_clean(); |
| 316 |
} else { |
| 317 |
return ''; |
| 318 |
} |
| 319 |
} // Meta Data |
| 320 |
|
| 321 |
function metaAuthor( $attributes, $post ) { |
| 322 |
extract( $attributes ); |
| 323 |
|
| 324 |
if ( $isMetaAuthor ) { |
| 325 |
ob_start(); ?> |
| 326 |
<span> |
| 327 |
<span class='dashicons dashicons-admin-users'></span> |
| 328 |
<span><?php echo get_the_author_posts_link( $post->ID ); ?></span> |
| 329 |
</span> |
| 330 |
<?php return ob_get_clean(); |
| 331 |
} else { |
| 332 |
return ''; |
| 333 |
} |
| 334 |
} // Meta Author |
| 335 |
function metaDate( $attributes, $post ) { |
| 336 |
extract( $attributes ); |
| 337 |
|
| 338 |
if ( $isMetaDate ) { |
| 339 |
ob_start(); ?> |
| 340 |
<span> |
| 341 |
<span class='dashicons dashicons-calendar'></span> |
| 342 |
<span><?php echo get_the_date( 'M j, Y', $post->ID ); ?></span> |
| 343 |
</span> |
| 344 |
<?php return ob_get_clean(); |
| 345 |
} else { |
| 346 |
return ''; |
| 347 |
} |
| 348 |
} // Meta Date |
| 349 |
function metaCategories( $attributes, $post ) { |
| 350 |
extract( $attributes ); |
| 351 |
|
| 352 |
if ( $isMetaCategory && 'content' === $metaCategoryIn ) { |
| 353 |
ob_start(); ?> |
| 354 |
<span> |
| 355 |
<span class='dashicons dashicons-category'></span> |
| 356 |
<span><?php echo get_the_category_list( esc_html__( ', ' ), '', $post->ID ); ?></span> |
| 357 |
</span> |
| 358 |
<?php return ob_get_clean(); |
| 359 |
} else { |
| 360 |
return ''; |
| 361 |
} |
| 362 |
} // Meta Categories |
| 363 |
function metaComment( $attributes, $post ) { |
| 364 |
extract( $attributes ); |
| 365 |
|
| 366 |
if ( $isMetaComment ) { |
| 367 |
ob_start(); ?> |
| 368 |
<span> |
| 369 |
<span class='dashicons dashicons-admin-comments'></span> |
| 370 |
<a href='<?php echo esc_url( get_post_permalink( $post->ID ) ); ?>/#comments' target='_blank' rel='noreferrer'><?php echo wp_count_comments( $post->ID )->total_comments; ?></a> |
| 371 |
</span> |
| 372 |
<?php return ob_get_clean(); |
| 373 |
} else { |
| 374 |
return ''; |
| 375 |
} |
| 376 |
} // Meta Comment |
| 377 |
|
| 378 |
function postExcerpt( $attributes, $post ) { |
| 379 |
extract( $attributes ); |
| 380 |
|
| 381 |
if ( $isExcerpt ) { |
| 382 |
ob_start(); ?> |
| 383 |
<div class='bBlocksPostExcerpt bBlocksInnerPostContent'><?php echo implode( ' ', array_slice( explode( ' ', get_post_field( 'post_content', $post->ID ) ), 0, $excerptLength ) ); ?></div> |
| 384 |
<?php return ob_get_clean(); |
| 385 |
} else { |
| 386 |
return ''; |
| 387 |
} |
| 388 |
} // Excerpt |
| 389 |
|
| 390 |
function postReadMore( $attributes, $post ) { |
| 391 |
extract( $attributes ); |
| 392 |
|
| 393 |
$tab = $isLinkNewTab ? '_blank' : '_self'; |
| 394 |
|
| 395 |
if ( $isReadMore ) { |
| 396 |
ob_start(); ?> |
| 397 |
<div class='bBlocksPostReadMore'> |
| 398 |
<a href='<?php echo esc_url( get_post_permalink( $post->ID ) ); ?>' target='<?php echo esc_attr( $tab ); ?>' rel='noreferrer'><?php echo esc_html( $readMoreLabel ); ?></a> |
| 399 |
</div> |
| 400 |
<?php return ob_get_clean(); |
| 401 |
} else { |
| 402 |
return ''; |
| 403 |
} |
| 404 |
} // Read More |
| 405 |
|
| 406 |
// Post Types |
| 407 |
function post_types() { |
| 408 |
$post_types = get_post_types( array( |
| 409 |
'public' => true, |
| 410 |
'show_in_rest' => true, |
| 411 |
), 'objects' ); |
| 412 |
|
| 413 |
$options = array(); |
| 414 |
foreach ( $post_types as $post_type ) { |
| 415 |
if ( 'product' === $post_type->name ) { continue; } |
| 416 |
if ( 'attachment' === $post_type->name ) { continue; } |
| 417 |
if ( 'page' === $post_type->name ) { continue; } |
| 418 |
|
| 419 |
$options[] = array( |
| 420 |
'value' => $post_type->name, |
| 421 |
'label' => $post_type->label |
| 422 |
); |
| 423 |
} |
| 424 |
return $options; |
| 425 |
} // Post Types |
| 426 |
|
| 427 |
function customRestAPI() { |
| 428 |
$post_type = $this->post_types(); |
| 429 |
foreach ( $post_type as $key => $value ) { |
| 430 |
register_rest_field( $value['value'], 'wbAuthor', array( |
| 431 |
'get_callback' => function ( $obj ) { |
| 432 |
$author['name'] = get_the_author_meta( 'display_name', isset( $obj['author'] ) ? $obj['author'] : '' ); |
| 433 |
$author['link'] = get_author_posts_url( isset( $obj['author'] ) ? $obj['author'] : '' ); |
| 434 |
return $author; |
| 435 |
}, |
| 436 |
'schema' => array( |
| 437 |
'description' => __( 'Author name and link', 'b-blocks' ), |
| 438 |
'type' => 'string', |
| 439 |
) |
| 440 |
) ); |
| 441 |
|
| 442 |
register_rest_field( $value['value'], 'wbDate', array( |
| 443 |
'get_callback' => function ( $obj ) { |
| 444 |
return get_the_date( 'M j, Y', $obj['id'] ); |
| 445 |
}, |
| 446 |
'schema' => array( |
| 447 |
'description' => __( 'Author name and link', 'b-blocks' ), |
| 448 |
'type' => 'string', |
| 449 |
) |
| 450 |
) ); |
| 451 |
|
| 452 |
register_rest_field( $value['value'], 'wbCategories', array( |
| 453 |
'get_callback' => function ( $obj ) { |
| 454 |
$catsLink['space'] = get_the_category_list( esc_html__( ' ' ), '', $obj['id'] ); |
| 455 |
$catsLink['coma'] = get_the_category_list( esc_html__( ', ' ), '', $obj['id'] ); |
| 456 |
return $catsLink; |
| 457 |
}, |
| 458 |
'schema' => array( |
| 459 |
'description' => __( 'Category link lists', 'b-blocks' ), |
| 460 |
'type' => 'string', |
| 461 |
) |
| 462 |
) ); |
| 463 |
|
| 464 |
register_rest_field( $value['value'], 'wbComment', array( |
| 465 |
'get_callback' => function ( $obj ) { |
| 466 |
return wp_count_comments( $obj['id'] )->total_comments; |
| 467 |
}, |
| 468 |
'schema' => array( |
| 469 |
'description' => __( 'Comment', 'b-blocks' ), |
| 470 |
'type' => 'number', |
| 471 |
) |
| 472 |
) ); |
| 473 |
} |
| 474 |
} // Custom Rest API |
| 475 |
} |
| 476 |
new BBlocksPosts(); |