| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) || exit; |
| 3 |
use YayMail\Utils\TemplateHelpers; |
| 4 |
use YayMail\Models\PostModel; |
| 5 |
|
| 6 |
/** |
| 7 |
* $args includes |
| 8 |
* $element |
| 9 |
* $render_data |
| 10 |
* $is_nested |
| 11 |
*/ |
| 12 |
if ( empty( $args['element'] ) ) { |
| 13 |
return; |
| 14 |
} |
| 15 |
|
| 16 |
$element = $args['element']; |
| 17 |
$settings = $args['settings']; |
| 18 |
$data = $element['data']; |
| 19 |
|
| 20 |
$showing_items = isset( $data['showing_items'] ) ? $data['showing_items'] : []; |
| 21 |
$top_content = isset( $data['top_content'] ) ? $data['top_content'] : ''; |
| 22 |
|
| 23 |
$params['number_of_posts'] = isset( $data['number_of_posts'] ) ? $data['number_of_posts'] : PostModel::DEFAULT_LIMIT; |
| 24 |
$params['post_type'] = isset( $data['post_type'] ) ? $data['post_type'] : 'newest'; |
| 25 |
$params['sorted_by'] = isset( $data['sorted_by'] ) ? $data['sorted_by'] : 'none'; |
| 26 |
$params['category_ids'] = isset( $data['categories'] ) ? array_map( |
| 27 |
function( $entity ) { |
| 28 |
return $entity['id']; |
| 29 |
}, |
| 30 |
$data['categories'] |
| 31 |
) : []; |
| 32 |
$params['tag_ids'] = isset( $data['tags'] ) ? array_map( |
| 33 |
function( $entity ) { |
| 34 |
return $entity['id']; |
| 35 |
}, |
| 36 |
$data['tags'] |
| 37 |
) : []; |
| 38 |
$params['post_ids'] = isset( $data['posts'] ) ? array_map( |
| 39 |
function( $entity ) { |
| 40 |
return $entity['id']; |
| 41 |
}, |
| 42 |
$data['posts'] |
| 43 |
) : []; |
| 44 |
|
| 45 |
$post_model = PostModel::get_instance(); |
| 46 |
$posts = $post_model->get_featured_posts( $params ); |
| 47 |
|
| 48 |
usort( |
| 49 |
$posts, |
| 50 |
function( $a, $b ) use ( $params ) { |
| 51 |
switch ( $params['sorted_by'] ) { |
| 52 |
case 'name_a_z': |
| 53 |
$title_a = isset( $a['title'] ) ? $a['title'] : ''; |
| 54 |
$title_b = isset( $b['title'] ) ? $b['title'] : ''; |
| 55 |
return strnatcasecmp( $title_a, $title_b ); |
| 56 |
|
| 57 |
case 'name_z_a': |
| 58 |
$title_a = isset( $a['title'] ) ? $a['title'] : ''; |
| 59 |
$title_b = isset( $b['title'] ) ? $b['title'] : ''; |
| 60 |
return strnatcasecmp( $title_b, $title_a ); |
| 61 |
|
| 62 |
case 'date_ascending': |
| 63 |
$date_a = isset( $a['date_timestamp'] ) ? (int) $a['date_timestamp'] : 0; |
| 64 |
$date_b = isset( $b['date_timestamp'] ) ? (int) $b['date_timestamp'] : 0; |
| 65 |
return $date_a - $date_b; |
| 66 |
|
| 67 |
case 'date_descending': |
| 68 |
$date_a = isset( $a['date_timestamp'] ) ? (int) $a['date_timestamp'] : 0; |
| 69 |
$date_b = isset( $b['date_timestamp'] ) ? (int) $b['date_timestamp'] : 0; |
| 70 |
return $date_b - $date_a; |
| 71 |
|
| 72 |
default: |
| 73 |
return 0; |
| 74 |
}//end switch |
| 75 |
} |
| 76 |
); |
| 77 |
|
| 78 |
$wrapper_style = TemplateHelpers::get_style( |
| 79 |
[ |
| 80 |
'word-break' => 'break-word', |
| 81 |
'background-color' => $data['background_color'], |
| 82 |
'font-family' => isset( $data['font_family'] ) ? TemplateHelpers::get_font_family_value( $data['font_family'] ) : 'initial', |
| 83 |
'padding' => TemplateHelpers::get_spacing_value( isset( $data['padding'] ) ? $data['padding'] : [] ), |
| 84 |
] |
| 85 |
); |
| 86 |
|
| 87 |
$text_color = isset( $data['text_color'] ) ? $data['text_color'] : 'initial'; |
| 88 |
|
| 89 |
$top_content_styles = TemplateHelpers::get_style( |
| 90 |
[ |
| 91 |
'color' => $text_color, |
| 92 |
] |
| 93 |
); |
| 94 |
|
| 95 |
$items_container_styles = TemplateHelpers::get_style( |
| 96 |
[ |
| 97 |
'width' => '100%', |
| 98 |
'text-align' => 'center', |
| 99 |
] |
| 100 |
); |
| 101 |
|
| 102 |
$posts_per_row = isset( $data['posts_per_row'] ) ? $data['posts_per_row'] : 2; |
| 103 |
$button_label = isset( $data['button_label'] ) ? $data['button_label'] : __( 'READ MORE', 'yaymail' ); |
| 104 |
$container_width = isset( $settings['container_width'] ) ? $settings['container_width'] : 605; |
| 105 |
$items_width = ( ( $container_width - 100 ) / $posts_per_row ) - 30; |
| 106 |
$items_styles = TemplateHelpers::get_style( |
| 107 |
[ |
| 108 |
'width' => "{$items_width}px", |
| 109 |
'padding' => '10px', |
| 110 |
'text-align' => 'center', |
| 111 |
'vertical-align' => 'top', |
| 112 |
] |
| 113 |
); |
| 114 |
|
| 115 |
$item_image_styles = TemplateHelpers::get_style( |
| 116 |
[ |
| 117 |
'width' => '100%', |
| 118 |
'object-fit' => 'cover', |
| 119 |
] |
| 120 |
); |
| 121 |
|
| 122 |
$post_title_styles = TemplateHelpers::get_style( |
| 123 |
[ |
| 124 |
'margin-top' => '5px', |
| 125 |
'font-weight' => 'bold', |
| 126 |
'color' => $text_color, |
| 127 |
] |
| 128 |
); |
| 129 |
|
| 130 |
$post_meta_styles = TemplateHelpers::get_style( |
| 131 |
[ |
| 132 |
'margin-top' => '5px', |
| 133 |
'color' => $text_color, |
| 134 |
'font-size' => '13px', |
| 135 |
] |
| 136 |
); |
| 137 |
|
| 138 |
$post_excerpt_styles = TemplateHelpers::get_style( |
| 139 |
[ |
| 140 |
'margin-top' => '5px', |
| 141 |
'color' => $text_color, |
| 142 |
'font-size' => '14px', |
| 143 |
] |
| 144 |
); |
| 145 |
|
| 146 |
$button_styles = TemplateHelpers::get_style( |
| 147 |
[ |
| 148 |
'background-color' => isset( $data['button_background_color'] ) ? $data['button_background_color'] : 'initial', |
| 149 |
'color' => isset( $data['button_text_color'] ) ? $data['button_text_color'] : 'initial', |
| 150 |
'line-height' => '21px', |
| 151 |
'font-family' => TemplateHelpers::get_font_family_value( isset( $data['font_family'] ) ? $data['font_family'] : 'inherit' ), |
| 152 |
'margin' => 0, |
| 153 |
'padding' => '10px 15px', |
| 154 |
'text-align' => 'center', |
| 155 |
'text-decoration' => 'none', |
| 156 |
'display' => 'inline-block', |
| 157 |
] |
| 158 |
); |
| 159 |
ob_start(); |
| 160 |
?> |
| 161 |
|
| 162 |
<?php if ( in_array( 'top_content', $showing_items, true ) ) : ?> |
| 163 |
<div style="<?php echo esc_attr( $top_content_styles ); ?>"> |
| 164 |
<?php echo wp_kses_post( $top_content ); ?> |
| 165 |
</div> |
| 166 |
<?php endif; ?> |
| 167 |
|
| 168 |
<table style="<?php echo esc_attr( $items_container_styles ); ?>"> |
| 169 |
<tbody> |
| 170 |
<tr> |
| 171 |
<td> |
| 172 |
<?php |
| 173 |
$post_count = 0; |
| 174 |
$total_rows = ceil( count( $posts ) / $posts_per_row ); |
| 175 |
$last_row_post_count = count( $posts ) % $posts_per_row; |
| 176 |
$current_row = 0; |
| 177 |
foreach ( $posts as $post ) : |
| 178 |
if ( $post_count % $posts_per_row === 0 ) { |
| 179 |
if ( (int) $current_row === (int) $total_rows - 1 && $last_row_post_count > 0 ) { |
| 180 |
echo '<table width="' . esc_attr( $last_row_post_count * $items_width ) . 'px" align="center">'; |
| 181 |
} else { |
| 182 |
echo '<table width="100%">'; |
| 183 |
} |
| 184 |
echo '<tr>'; |
| 185 |
++$current_row; |
| 186 |
} |
| 187 |
?> |
| 188 |
<td style="<?php echo esc_attr( $items_styles ); ?>"> |
| 189 |
<?php if ( in_array( 'post_image', $showing_items, true ) && ! empty( $post['thumbnail_src'] ) ) : ?> |
| 190 |
<a href="<?php echo esc_url( isset( $post['permalink'] ) ? $post['permalink'] : '#' ); ?>" target="_blank" rel="noreferrer"> |
| 191 |
<img style="<?php echo esc_attr( $item_image_styles ); ?>" src="<?php echo esc_attr( $post['thumbnail_src'] ); ?>" alt="<?php echo esc_attr( isset( $post['title'] ) ? $post['title'] : '' ); ?>"></img> |
| 192 |
</a> |
| 193 |
<?php endif; ?> |
| 194 |
|
| 195 |
<?php if ( in_array( 'post_title', $showing_items, true ) ) : ?> |
| 196 |
<div style="<?php echo esc_attr( $post_title_styles ); ?>"> |
| 197 |
<?php echo esc_html( isset( $post['title'] ) ? $post['title'] : '' ); ?> |
| 198 |
</div> |
| 199 |
<?php endif; ?> |
| 200 |
|
| 201 |
<?php if ( in_array( 'post_excerpt', $showing_items, true ) && ! empty( $post['excerpt'] ) ) : ?> |
| 202 |
<div style="<?php echo esc_attr( $post_excerpt_styles ); ?>"> |
| 203 |
<?php echo wp_kses_post( $post['excerpt'] ); ?> |
| 204 |
</div> |
| 205 |
<?php endif; ?> |
| 206 |
|
| 207 |
<?php if ( in_array( 'post_date', $showing_items, true ) && ! empty( $post['date'] ) ) : ?> |
| 208 |
<div style="<?php echo esc_attr( $post_meta_styles ); ?>"> |
| 209 |
<?php echo esc_html( $post['date'] ); ?> |
| 210 |
</div> |
| 211 |
<?php endif; ?> |
| 212 |
|
| 213 |
<?php if ( in_array( 'post_author', $showing_items, true ) && ! empty( $post['author'] ) ) : ?> |
| 214 |
<div style="<?php echo esc_attr( $post_meta_styles ); ?>"> |
| 215 |
<?php echo esc_html( $post['author'] ); ?> |
| 216 |
</div> |
| 217 |
<?php endif; ?> |
| 218 |
|
| 219 |
<?php if ( in_array( 'button', $showing_items, true ) ) : ?> |
| 220 |
<div style="margin-top: 10px;"> |
| 221 |
<a style="<?php echo esc_attr( $button_styles ); ?>" href="<?php echo esc_url( isset( $post['permalink'] ) ? $post['permalink'] : '#' ); ?>" target="_blank" rel="noreferrer" role="button"> |
| 222 |
<?php echo esc_html( $button_label ); ?> |
| 223 |
</a> |
| 224 |
</div> |
| 225 |
<?php endif; ?> |
| 226 |
</td> |
| 227 |
<?php |
| 228 |
++$post_count; |
| 229 |
if ( $post_count % $posts_per_row === 0 ) { |
| 230 |
echo '</tr>'; |
| 231 |
echo '</table>'; |
| 232 |
} |
| 233 |
endforeach; |
| 234 |
|
| 235 |
if ( $post_count % $posts_per_row !== 0 ) { |
| 236 |
echo '</tr>'; |
| 237 |
echo '</table>'; |
| 238 |
} |
| 239 |
?> |
| 240 |
</td> |
| 241 |
</tr> |
| 242 |
</tbody> |
| 243 |
</table> |
| 244 |
|
| 245 |
<?php |
| 246 |
$element_content = ob_get_clean(); |
| 247 |
|
| 248 |
TemplateHelpers::wrap_element_content( $element_content, $element, $wrapper_style ); |
| 249 |
|