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