| 1 |
<?php |
| 2 |
function render_b_blocks_posts( $attributes ) { |
| 3 |
extract( $attributes ); |
| 4 |
|
| 5 |
$align = isset( $align ) ? $align : 'full'; |
| 6 |
|
| 7 |
$selectedCategories = isset( $selectedCategories ) ? $selectedCategories : []; |
| 8 |
$postsPerPageAll = isset( $postsPerPageAll ) ? $postsPerPageAll : 'false'; |
| 9 |
$postsPerPage = isset( $postsPerPage ) ? $postsPerPage : 12; |
| 10 |
$postsOrderBy = isset( $postsOrderBy ) ? $postsOrderBy : 'date'; |
| 11 |
$postsOrder = isset( $postsOrder ) ? $postsOrder : 'desc'; |
| 12 |
|
| 13 |
$layout = isset( $layout ) ? $layout : 'grid'; |
| 14 |
$columns = isset( $columns ) ? $columns : 3; |
| 15 |
$columnGap = isset( $columnGap ) ? $columnGap : 10; |
| 16 |
$rowGap = isset( $rowGap ) ? $rowGap : 10; |
| 17 |
$contentEqualHight = isset( $contentEqualHight ) ? $contentEqualHight : 'true'; |
| 18 |
|
| 19 |
$contentAlign = isset( $contentAlign ) ? $contentAlign : 'left'; |
| 20 |
$contentBGColor = isset( $contentBGColor ) ? $contentBGColor : '#fff'; |
| 21 |
$postTextPTB = isset( $postTextPTB ) ? $postTextPTB : 15; |
| 22 |
$postTextPLR = isset( $postTextPLR ) ? $postTextPLR : 15; |
| 23 |
$shadow = isset( $shadow ) ? $shadow : 'shadow'; |
| 24 |
|
| 25 |
$featureImgShow = isset( $featureImgShow ) ? $featureImgShow : 'true'; |
| 26 |
|
| 27 |
$titleShow = isset( $titleShow ) ? $titleShow : 'true'; |
| 28 |
$titleFontSize = isset( $titleFontSize ) ? $titleFontSize : 20; |
| 29 |
$titleColor = isset( $titleColor ) ? $titleColor : '#4527a4'; |
| 30 |
$titleMB = isset( $titleMB ) ? $titleMB : 15; |
| 31 |
|
| 32 |
$metaShow = isset( $metaShow ) ? $metaShow : 'true'; |
| 33 |
$metaAuthorShow = isset( $metaAuthorShow ) ? $metaAuthorShow : 'true'; |
| 34 |
$metaDateShow = isset( $metaDateShow ) ? $metaDateShow : 'true'; |
| 35 |
$metaCategoryShow = isset( $metaCategoryShow ) ? $metaCategoryShow : 'true'; |
| 36 |
$metaCommentShow = isset( $metaCommentShow ) ? $metaCommentShow : 'false'; |
| 37 |
$metaFontSize = isset( $metaFontSize ) ? $metaFontSize : 13; |
| 38 |
$metaTransform = isset( $metaTransform ) ? $metaTransform : 'uppercase'; |
| 39 |
$metaTextColor = isset( $metaTextColor ) ? $metaTextColor : '#333'; |
| 40 |
$metaLinkColor = isset( $metaLinkColor ) ? $metaLinkColor : '#8344c5'; |
| 41 |
$metaIconColor = isset( $metaIconColor ) ? $metaIconColor : '#4527a4'; |
| 42 |
$metaMB = isset( $metaMB ) ? $metaMB : 15; |
| 43 |
|
| 44 |
$excerptShow = isset( $excerptShow ) ? $excerptShow : 'true'; |
| 45 |
$excerptLength = isset( $excerptLength ) ? $excerptLength : 45; |
| 46 |
$excerptAlign = isset( $excerptAlign ) ? $excerptAlign : 'justify'; |
| 47 |
$excerptFontSize = isset( $excerptFontSize ) ? $excerptFontSize : 15; |
| 48 |
$excerptColor = isset( $excerptColor ) ? $excerptColor : '#333'; |
| 49 |
$excerptMB = isset( $excerptMB ) ? $excerptMB : 15; |
| 50 |
|
| 51 |
$readMoreShow = isset( $readMoreShow ) ? $readMoreShow : 'true'; |
| 52 |
$readMoreLabel = isset( $readMoreLabel ) ? $readMoreLabel : 'Read More'; |
| 53 |
$readMoreAlign = isset( $readMoreAlign ) ? $readMoreAlign : 'left'; |
| 54 |
$readMoreFontSize = isset( $readMoreFontSize ) ? $readMoreFontSize : 14; |
| 55 |
$readMoreTransform = isset( $readMoreTransform ) ? $readMoreTransform : 'uppercase'; |
| 56 |
$readMoreFontWeight = isset( $readMoreFontWeight ) ? $readMoreFontWeight : '600'; |
| 57 |
$readMoreColor = isset( $readMoreColor ) ? $readMoreColor : '#4527a4'; |
| 58 |
$readMoreHovColor = isset( $readMoreHovColor ) ? $readMoreHovColor : '#8344c5'; |
| 59 |
|
| 60 |
// All Posts |
| 61 |
$posts = get_posts( [ |
| 62 |
'category' => $selectedCategories, |
| 63 |
'posts_per_page' => 'true' == $postsPerPageAll ? -1 : $postsPerPage, |
| 64 |
'orderby' => $postsOrderBy, |
| 65 |
'order' => $postsOrder, |
| 66 |
] ); |
| 67 |
|
| 68 |
$alignClass = '' == $align ? '' : 'align' . $align; |
| 69 |
|
| 70 |
ob_start(); |
| 71 |
echo '<div class="b_blocks_posts ' . $alignClass . '">'; |
| 72 |
|
| 73 |
if ( 'grid' == $layout ) { |
| 74 |
echo '<div id="bBlocksGridPosts-' . $cId . '" class="b_blocks_grid_posts" style="display: grid; grid-template-columns: repeat(' . $columns . ', 1fr); grid-gap: ' . $rowGap . 'px ' . $columnGap . 'px;">'; |
| 75 |
foreach ( $posts as $post ) { |
| 76 |
$contentHeight = 'true' == $contentEqualHight ? "100%" : "auto"; |
| 77 |
|
| 78 |
echo '<article class="b_blocks_article"> |
| 79 |
<div class="post_content ' . $shadow . ' rounded" style="text-align: ' . $contentAlign . '; height: ' . $contentHeight . '; background-color: ' . $contentBGColor . ';"> |
| 80 |
' . bBlocksPostFeatureImg( $featureImgShow, $post ) . ' |
| 81 |
|
| 82 |
<div class="post_text" style="padding: ' . $postTextPTB . 'px ' . $postTextPLR . 'px;"> |
| 83 |
' . bBlocksPostTitle( $titleShow, $post, $contentAlign, $titleFontSize, $titleColor, $titleMB ) . bBlocksPostMetaData( $metaShow, $post, $metaAuthorShow, $metaDateShow, $metaCategoryShow, $metaCommentShow, $contentAlign, $metaFontSize, $metaTransform, $metaTextColor, $metaLinkColor, $metaIconColor, $metaMB ) . bBlocksPostExcerpt( $excerptShow, $post, $excerptAlign, $excerptFontSize, $excerptColor, $excerptMB, $excerptLength ) . bBlocksPostReadMore( $readMoreShow, $post, $readMoreAlign, $readMoreFontSize, $readMoreTransform, $readMoreFontWeight, $readMoreColor, $readMoreHovColor, $readMoreLabel ) . ' |
| 84 |
</div> |
| 85 |
</div> |
| 86 |
</article>'; |
| 87 |
} |
| 88 |
echo '</div>'; |
| 89 |
|
| 90 |
} else if ( 'masonry' == $layout ) { |
| 91 |
echo '<div id="bBlocksMasonryPosts-' . $cId . '" class="b_blocks_masonry_posts" style="columns: ' . $columns . '; gap: ' . $columnGap . 'px;">'; |
| 92 |
foreach ( $posts as $post ) { |
| 93 |
echo '<article class="b_blocks_article" style="break-inside: avoid; margin-bottom: ' . $rowGap . 'px;"> |
| 94 |
<div class="post_content ' . $shadow . ' rounded" style="text-align: ' . $contentAlign . '; background-color: ' . $contentBGColor . ';"> |
| 95 |
' . bBlocksPostFeatureImg( $featureImgShow, $post ) . ' |
| 96 |
|
| 97 |
<div class="post_text" style="padding: ' . $postTextPTB . 'px ' . $postTextPLR . 'px;"> |
| 98 |
' . bBlocksPostTitle( $titleShow, $post, $contentAlign, $titleFontSize, $titleColor, $titleMB ) . bBlocksPostMetaData( $metaShow, $post, $metaAuthorShow, $metaDateShow, $metaCategoryShow, $metaCommentShow, $contentAlign, $metaFontSize, $metaTransform, $metaTextColor, $metaLinkColor, $metaIconColor, $metaMB ) . bBlocksPostExcerpt( $excerptShow, $post, $excerptAlign, $excerptFontSize, $excerptColor, $excerptMB, $excerptLength ) . bBlocksPostReadMore( $readMoreShow, $post, $readMoreAlign, $readMoreFontSize, $readMoreTransform, $readMoreFontWeight, $readMoreColor, $readMoreHovColor, $readMoreLabel ) . ' |
| 99 |
</div> |
| 100 |
</div> |
| 101 |
</article>'; |
| 102 |
} |
| 103 |
echo '</div>'; |
| 104 |
} else { |
| 105 |
echo ''; |
| 106 |
} |
| 107 |
|
| 108 |
echo '</div>'; |
| 109 |
return ob_get_clean(); |
| 110 |
} |
| 111 |
|
| 112 |
// Components |
| 113 |
// Feature Image |
| 114 |
function bBlocksPostFeatureImg( $show, $post ) { |
| 115 |
if ( 'true' == $show ) { |
| 116 |
return '<div class="feature_img"> |
| 117 |
<a href="' . get_post_permalink( $post->ID ) . '" target="_blank">' . get_the_post_thumbnail( $post->ID ) . '</a> |
| 118 |
</div>'; |
| 119 |
} else { |
| 120 |
return ''; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
// Title |
| 125 |
function bBlocksPostTitle( $show, $post, $align, $fontSize, $color, $mb ) { |
| 126 |
if ( 'true' == $show ) { |
| 127 |
return '<h3 class="title" style="text-align:' . $align . '; font-size:' . $fontSize . 'px; color: ' . $color . '; margin: 0 0 ' . $mb . 'px 0"> |
| 128 |
<a href="' . get_post_permalink( $post->ID ) . '" target="_blank" style="color: ' . $color . ';">' . $post->post_title . '</a> |
| 129 |
</h3>'; |
| 130 |
} else { |
| 131 |
return ''; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
// Meta Data |
| 136 |
function bBlocksPostMetaData( $show, $post, $authorShow, $dateShow, $categoryShow, $commentShow, $align, $fontSize, $transform, $textColor, $linkColor, $iconColor, $mb ) { |
| 137 |
if ( 'true' == $show ) { |
| 138 |
return '<p class="meta" style="text-align:' . $align . '; font-size:' . $fontSize . 'px; text-transform: ' . $transform . '; color: ' . $textColor . '; margin: 0 0 ' . $mb . 'px 0"> |
| 139 |
' . bBlocksPostMetaAuthor( $authorShow, $post, $iconColor, $linkColor ) . bBlocksPostMetaDate( $dateShow, $post, $iconColor, $textColor ) . bBlocksPostMetaCategory( $categoryShow, $post, $iconColor, $linkColor ) . bBlocksPostMetaComment( $commentShow, $post, $iconColor, $linkColor ) . ' |
| 140 |
</p>'; |
| 141 |
} else { |
| 142 |
return ''; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
function bBlocksPostMetaAuthor( $show, $post, $iconColor, $linkColor ) { |
| 147 |
if ( 'true' == $show ) { |
| 148 |
return '<span> |
| 149 |
<span class="dashicons dashicons-admin-users" style="color: ' . $iconColor . ';"></span> |
| 150 |
<span style="color: ' . $linkColor . ';">' . get_the_author_posts_link( $post->ID ) . '</span> |
| 151 |
</span>'; |
| 152 |
} else { |
| 153 |
return ''; |
| 154 |
} |
| 155 |
} |
| 156 |
function bBlocksPostMetaDate( $show, $post, $iconColor, $textColor ) { |
| 157 |
if ( 'true' == $show ) { |
| 158 |
return '<span> |
| 159 |
<span class="dashicons dashicons-calendar" style="color: ' . $iconColor . ';"></span> |
| 160 |
<span style="color: ' . $textColor . ';">' . get_the_date( 'M j, Y', $post->ID ) . '</span> |
| 161 |
</span>'; |
| 162 |
} else { |
| 163 |
return ''; |
| 164 |
} |
| 165 |
} |
| 166 |
function bBlocksPostMetaCategory( $show, $post, $iconColor, $linkColor ) { |
| 167 |
if ( 'true' == $show ) { |
| 168 |
return '<span> |
| 169 |
<span class="dashicons dashicons-category" style="color: ' . $iconColor . ';"></span> |
| 170 |
<span style="color: ' . $linkColor . ';">' . get_the_category_list( ', ', '', $post->ID ) . '</span> |
| 171 |
</span>'; |
| 172 |
} else { |
| 173 |
return ''; |
| 174 |
} |
| 175 |
} |
| 176 |
function bBlocksPostMetaComment( $show, $post, $iconColor, $linkColor ) { |
| 177 |
if ( 'true' == $show ) { |
| 178 |
$comment_count = get_comments( array( 'post_id' => $post->ID, 'count' => true ) ); |
| 179 |
$view_comment = ''; |
| 180 |
switch ( $comment_count ) { |
| 181 |
case "0": |
| 182 |
$view_comment = esc_html__( 'No Comments', 'b-blocks' ); |
| 183 |
break; |
| 184 |
case "1": |
| 185 |
$view_comment = esc_html__( '1 Comment', 'b-blocks' ); |
| 186 |
break; |
| 187 |
default: |
| 188 |
$view_comment = esc_html__( "$comment_count Comments", 'b-blocks' ); |
| 189 |
} |
| 190 |
|
| 191 |
return '<span> |
| 192 |
<span class="dashicons dashicons-admin-comments" style="color: ' . $iconColor . ';"></span> |
| 193 |
<a href="' . get_post_permalink( $post->ID ) . '/#comments" target="_blank" style="color: ' . $linkColor . ';">' . $view_comment . '</a> |
| 194 |
</span>'; |
| 195 |
} else { |
| 196 |
return ''; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
// Excerpt |
| 201 |
function bBlocksPostExcerpt( $show, $post, $align, $fontSize, $color, $mb, $length ) { |
| 202 |
if ( 'true' == $show ) { |
| 203 |
return '<div class="excerpt b_blocks_inner_content" style="text-align:' . $align . '; font-size:' . $fontSize . 'px; color: ' . $color . '; margin: 0 0 ' . $mb . 'px 0"">' . implode( ' ', array_slice( explode( ' ', get_post_field( 'post_content', $post->ID ) ), 0, $length ) ) . '</div>'; |
| 204 |
} else { |
| 205 |
return ''; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
// Read More |
| 210 |
function bBlocksPostReadMore( $show, $post, $align, $fontSize, $transform, $fontWeight, $color, $hovColor, $label ) { |
| 211 |
if ( 'true' == $show ) { |
| 212 |
return '<div class="read_more" style="text-align: ' . $align . ';"> |
| 213 |
<a href="' . get_post_permalink( $post->ID ) . '" target="_blank" |
| 214 |
style="display: inline-block; font-size:' . $fontSize . 'px; text-transform: ' . $transform . '; font-weight: ' . $fontWeight . '; color: ' . $color . '; transition: all .3s ease;" |
| 215 |
|
| 216 |
onMouseOver="this.style.color=\'' . $hovColor . '\';" |
| 217 |
onMouseOut="this.style.color=\'' . $color . '\';">' . $label . '</a> |
| 218 |
</div>'; |
| 219 |
} else { |
| 220 |
return ''; |
| 221 |
} |
| 222 |
} |