_scss
6 years ago
class-vk-component-button.php
6 years ago
class-vk-component-mini-contents.php
6 years ago
class-vk-component-posts.php
6 years ago
class-vk-component-posts.php
422 lines
| 1 | <?php |
| 2 | /* |
| 3 | The original of this file is located at: |
| 4 | https://github.com/vektor-inc/vektor-wp-libraries |
| 5 | If you want to change this file, please change the original file. |
| 6 | */ |
| 7 | |
| 8 | if ( ! class_exists( 'VK_Component_Posts' ) ) { |
| 9 | |
| 10 | class VK_Component_Posts { |
| 11 | |
| 12 | /*-------------------------------------------*/ |
| 13 | /* Basic method |
| 14 | /*-------------------------------------------*/ |
| 15 | /* Common Parts |
| 16 | /*-------------------------------------------*/ |
| 17 | /* Layout patterns |
| 18 | /*-------------------------------------------*/ |
| 19 | /* UI Helper method |
| 20 | /*-------------------------------------------*/ |
| 21 | |
| 22 | /*-------------------------------------------*/ |
| 23 | /* Basic method |
| 24 | /*-------------------------------------------*/ |
| 25 | static public function get_loop_post_view_options( $options ) { |
| 26 | global $vk_components_textdomain; |
| 27 | $default = array( |
| 28 | 'layout' => 'card', |
| 29 | 'display_image' => true, |
| 30 | 'display_image_overlay_term' => true, |
| 31 | 'display_excerpt' => false, |
| 32 | 'display_date' => true, |
| 33 | 'display_new' => true, |
| 34 | 'display_btn' => false, |
| 35 | 'image_default_url' => false, |
| 36 | 'overlay' => false, |
| 37 | 'btn_text' => __( 'Read more', $vk_components_textdomain ), |
| 38 | 'btn_align' => 'text-right', |
| 39 | 'new_text' => __( 'New!!', $vk_components_textdomain ), |
| 40 | 'new_date' => 7, |
| 41 | 'textlink' => true, |
| 42 | 'class_outer' => '', |
| 43 | 'class_title' => '', |
| 44 | 'body_prepend' => '', |
| 45 | 'body_append' => '', |
| 46 | ); |
| 47 | $return = wp_parse_args( $options, $default ); |
| 48 | return $return; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * [public description] |
| 53 | * @var [type] |
| 54 | */ |
| 55 | static public function get_view( $post, $options ) { |
| 56 | |
| 57 | $options = self::get_loop_post_view_options( $options ); |
| 58 | |
| 59 | if ( $options['layout'] == 'card-horizontal' ) { |
| 60 | $html = self::get_view_type_card_horizontal( $post, $options ); |
| 61 | } elseif ( $options['layout'] == 'media' ) { |
| 62 | $html = self::get_view_type_media( $post, $options ); |
| 63 | } else { |
| 64 | $html = self::get_view_type_card( $post, $options ); |
| 65 | } |
| 66 | return $html; |
| 67 | } |
| 68 | |
| 69 | static public function the_view( $post, $options ) { |
| 70 | echo wp_kses_post( self::get_view( $post, $options ) ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * [public description] |
| 75 | * @var [type] |
| 76 | */ |
| 77 | static public function get_loop( $wp_query, $options, $options_loop = array() ) { |
| 78 | |
| 79 | $options_loop_dafault = array( |
| 80 | 'class_loop_outer' => '', |
| 81 | ); |
| 82 | $options_loop = wp_parse_args( $options_loop, $options_loop_dafault ); |
| 83 | |
| 84 | $loop = ''; |
| 85 | if ( $wp_query->have_posts() ) : |
| 86 | |
| 87 | $outer_class = ''; |
| 88 | if ( $options_loop['class_loop_outer'] ) { |
| 89 | $outer_class = ' ' . $options_loop['class_loop_outer']; |
| 90 | } |
| 91 | |
| 92 | $loop .= '<div class="vk_posts' . $outer_class . '">'; |
| 93 | |
| 94 | while ( $wp_query->have_posts() ) { |
| 95 | $wp_query->the_post(); |
| 96 | global $post; |
| 97 | $loop .= VK_Component_Posts::get_view( $post, $options ); |
| 98 | } // while ( have_posts() ) { |
| 99 | endif; |
| 100 | |
| 101 | $loop .= '</div>'; |
| 102 | |
| 103 | wp_reset_query(); |
| 104 | wp_reset_postdata(); |
| 105 | return $loop; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * [public description] |
| 110 | * @var [type] |
| 111 | */ |
| 112 | static public function the_loop( $wp_query, $options, $options_loop = array() ) { |
| 113 | echo self::get_loop( $wp_query, $options, $options_loop ); |
| 114 | } |
| 115 | |
| 116 | /*-------------------------------------------*/ |
| 117 | /* Common Parts |
| 118 | /*-------------------------------------------*/ |
| 119 | /** |
| 120 | * Common Part _ first DIV |
| 121 | * @var [type] |
| 122 | */ |
| 123 | static public function get_view_first_div( $post, $options ) { |
| 124 | if ( $options['layout'] == 'card-horizontal' ) { |
| 125 | $class_outer = 'card card-post card-horizontal'; |
| 126 | } elseif ( $options['layout'] == 'media' ) { |
| 127 | $class_outer = 'media'; |
| 128 | } else { |
| 129 | $class_outer = 'card card-post'; |
| 130 | } |
| 131 | if ( ! empty( $options['class_outer'] ) ) { |
| 132 | $class_outer .= ' ' . esc_attr( $options['class_outer'] ); |
| 133 | } |
| 134 | if ( $options['display_btn'] ) { |
| 135 | $class_outer .= ' vk_post-btn-display'; |
| 136 | } |
| 137 | $html = '<div id="post-' . esc_attr( $post->ID ) . '" class="vk_post ' . join( ' ', get_post_class( $class_outer ) ) . '">'; |
| 138 | return $html; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Common Part _ post thumbnail |
| 143 | * @param [type] $post [description] |
| 144 | * @param [type] $options [description] |
| 145 | * @param string $class [description] |
| 146 | * @return [type] [description] |
| 147 | */ |
| 148 | static public function get_thumbnail_image( $post, $options, $attr = array() ) { |
| 149 | |
| 150 | $default = array( |
| 151 | 'class_outer' => '', |
| 152 | 'class_image' => '', |
| 153 | ); |
| 154 | $classes = wp_parse_args( $attr, $default ); |
| 155 | |
| 156 | $html = ''; |
| 157 | if ( $options['display_image'] ) { |
| 158 | if ( $classes['class_outer'] ) { |
| 159 | $classes['class_outer'] = ' ' . $classes['class_outer']; |
| 160 | } |
| 161 | |
| 162 | $image_src = get_the_post_thumbnail_url( $post->ID, 'large' ); |
| 163 | if ( ! $image_src && $options['image_default_url'] ) { |
| 164 | $image_src = esc_url( $options['image_default_url'] ); |
| 165 | } |
| 166 | $style = ' style="background-image:url(' . $image_src . ')"'; |
| 167 | |
| 168 | $html .= '<div class="vk_post_imgOuter' . $classes['class_outer'] . '"' . $style . '>'; |
| 169 | $html .= '<a href="' . get_the_permalink( $post->ID ) . '">'; |
| 170 | |
| 171 | if ( $options['overlay'] ) { |
| 172 | $html .= '<div class="card-img-overlay">'; |
| 173 | $html .= $options['overlay']; |
| 174 | $html .= '</div>'; |
| 175 | } |
| 176 | |
| 177 | if ( $options['display_image_overlay_term'] ) { |
| 178 | |
| 179 | $html .= '<div class="card-img-overlay">'; |
| 180 | $term_args = array( |
| 181 | 'class' => 'vk_post_imgOuter_singleTermLabel', |
| 182 | ); |
| 183 | if ( method_exists( 'Vk_term_color', 'get_single_term_with_color' ) ) { |
| 184 | $html .= Vk_term_color::get_single_term_with_color( false, $term_args ); |
| 185 | } |
| 186 | $html .= '</div>'; |
| 187 | |
| 188 | } |
| 189 | if ( $classes['class_image'] ) { |
| 190 | $image_class = 'vk_post_imgOuter_img ' . $classes['class_image']; |
| 191 | } else { |
| 192 | $image_class = 'vk_post_imgOuter_img'; |
| 193 | } |
| 194 | |
| 195 | $image_attr = array( 'class' => $image_class ); |
| 196 | $img = get_the_post_thumbnail( $post->ID, 'medium', $image_attr ); |
| 197 | if ( $img ) { |
| 198 | $html .= $img; |
| 199 | } elseif ( $options['image_default_url'] ) { |
| 200 | $html .= '<img src="' . esc_url( $options['image_default_url'] ) . '" alt="" class="' . $image_class . '" />'; |
| 201 | } |
| 202 | $html .= '</a>'; |
| 203 | $html .= '</div><!-- [ /.vk_post_imgOuter ] -->'; |
| 204 | } // if ( $options['display_image'] ) { |
| 205 | return $html; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Common Part _ post body |
| 210 | * @var [type] |
| 211 | */ |
| 212 | static public function get_view_body( $post, $options ) { |
| 213 | // $default = array( |
| 214 | // 'textlink' => false, |
| 215 | // ); |
| 216 | // $attr = wp_parse_args( $attr, $default ); |
| 217 | |
| 218 | $layout_type = $options['layout']; |
| 219 | if ( $layout_type == 'card-horizontal' ) { |
| 220 | $layout_type = 'card'; |
| 221 | } |
| 222 | |
| 223 | $html = ''; |
| 224 | |
| 225 | $html .= '<div class="vk_post_body ' . $layout_type . '-body">'; |
| 226 | |
| 227 | if ( ! empty( $options['body_prepend'] ) ) { |
| 228 | $html .= $options['body_prepend']; |
| 229 | } |
| 230 | |
| 231 | $html .= '<h5 class="vk_post_title ' . $layout_type . '-title">'; |
| 232 | |
| 233 | if ( $options['textlink'] ) { |
| 234 | $html .= '<a href="' . get_the_permalink( $post->ID ) . '">'; |
| 235 | } |
| 236 | |
| 237 | $html .= get_the_title( $post->ID ); |
| 238 | |
| 239 | if ( $options['display_new'] ) { |
| 240 | $today = date_i18n( 'U' ); |
| 241 | $entry = get_the_time( 'U' ); |
| 242 | $kiji = date( 'U', ( $today - $entry ) ) / 86400; |
| 243 | if ( $options['new_date'] > $kiji ) { |
| 244 | $html .= '<span class="vk_post_title_new">' . $options['new_text'] . '</span>'; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | if ( $options['textlink'] ) { |
| 249 | $html .= '</a>'; |
| 250 | } |
| 251 | |
| 252 | $html .= '</h5>'; |
| 253 | |
| 254 | if ( $options['display_date'] ) { |
| 255 | $html .= '<div class="vk_post_date ' . $layout_type . '-date published">'; |
| 256 | $html .= esc_html( get_the_date( null, $post->ID ) ); |
| 257 | $html .= '</div>'; |
| 258 | } |
| 259 | |
| 260 | if ( $options['display_excerpt'] ) { |
| 261 | $html .= '<p class="vk_post_excerpt ' . $layout_type . '-text">'; |
| 262 | $html .= wp_kses_post( get_the_excerpt( $post->ID ) ); |
| 263 | $html .= '</p>'; |
| 264 | } |
| 265 | |
| 266 | if ( $options['display_btn'] ) { |
| 267 | $button_options = array( |
| 268 | 'outer_id' => '', |
| 269 | 'outer_class' => '', |
| 270 | 'btn_text' => $options['btn_text'], |
| 271 | 'btn_url' => get_the_permalink( $post->ID ), |
| 272 | 'btn_class' => 'btn btn-sm btn-primary vk_post_btn', |
| 273 | 'btn_target' => '', |
| 274 | 'btn_ghost' => false, |
| 275 | 'btn_color_text' => '', |
| 276 | 'btn_color_bg' => '', |
| 277 | 'shadow_use' => false, |
| 278 | 'shadow_color' => '', |
| 279 | ); |
| 280 | |
| 281 | // $text_align = ''; |
| 282 | // if ( $options['btn_align'] == 'right' ) { |
| 283 | // $text_align = ' text-right'; |
| 284 | // } |
| 285 | $html .= '<div class="vk_post_btnOuter ' . $options['btn_align'] . '">'; |
| 286 | $html .= VK_Component_Button::get_view( $button_options ); |
| 287 | $html .= '</div>'; |
| 288 | } |
| 289 | |
| 290 | if ( ! empty( $options['body_append'] ) ) { |
| 291 | $html .= $options['body_append']; |
| 292 | } |
| 293 | |
| 294 | $html .= '</div><!-- [ /.' . $layout_type . '-body ] -->'; |
| 295 | |
| 296 | return $html; |
| 297 | } |
| 298 | |
| 299 | /*-------------------------------------------*/ |
| 300 | /* Layout patterns |
| 301 | /*-------------------------------------------*/ |
| 302 | /** |
| 303 | * Card |
| 304 | * @var [type] |
| 305 | */ |
| 306 | static public function get_view_type_card( $post, $options ) { |
| 307 | $html = ''; |
| 308 | $html .= self::get_view_first_div( $post, $options ); |
| 309 | |
| 310 | $attr = array( |
| 311 | 'class_outer' => '', |
| 312 | 'class_image' => 'card-img-top', |
| 313 | ); |
| 314 | $html .= self::get_thumbnail_image( $post, $options, $attr ); |
| 315 | |
| 316 | $html .= self::get_view_body( $post, $options ); |
| 317 | |
| 318 | $html .= '</div><!-- [ /.card ] -->'; |
| 319 | return $html; |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Card horizontal |
| 324 | * @var [type] |
| 325 | */ |
| 326 | static public function get_view_type_card_horizontal( $post, $options ) { |
| 327 | $html = ''; |
| 328 | $html .= self::get_view_first_div( $post, $options ); |
| 329 | // $html .= '<a href="' . get_the_permalink( $post->ID ) . '" class="card-horizontal-inner">'; |
| 330 | $html .= '<div class="row no-gutters card-horizontal-inner-row">'; |
| 331 | |
| 332 | // $image_src = ''; |
| 333 | if ( $options['display_image'] ) { |
| 334 | $html .= '<div class="col-5 card-img-outer">'; |
| 335 | $attr = array( |
| 336 | 'class_outer' => '', |
| 337 | 'class_image' => 'card-img card-img-use-bg', |
| 338 | ); |
| 339 | $html .= self::get_thumbnail_image( $post, $options, $attr ); |
| 340 | $html .= '</div><!-- /.col -->'; |
| 341 | $html .= '<div class="col-7">'; |
| 342 | } |
| 343 | |
| 344 | $html .= self::get_view_body( $post, $options ); |
| 345 | |
| 346 | if ( $options['display_image'] ) { |
| 347 | $html .= '</div><!-- /.col -->'; |
| 348 | } |
| 349 | |
| 350 | $html .= '</div><!-- [ /.row ] -->'; |
| 351 | // $html .= '</a>'; |
| 352 | $html .= '</div><!-- [ /.card ] -->'; |
| 353 | return $html; |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Media |
| 358 | * @var [type] |
| 359 | */ |
| 360 | static public function get_view_type_media( $post, $options ) { |
| 361 | $html = ''; |
| 362 | $html .= self::get_view_first_div( $post, $options ); |
| 363 | if ( $options['display_image'] ) { |
| 364 | // $html .= '<a href="' . get_the_permalink() . '" class="media-img">'; |
| 365 | $attr = array( |
| 366 | 'class_outer' => 'media-img', |
| 367 | 'class_image' => '', |
| 368 | ); |
| 369 | $html .= self::get_thumbnail_image( $post, $options, $attr ); |
| 370 | // $html .= '</a>'; |
| 371 | } |
| 372 | |
| 373 | // $attr = array( |
| 374 | // 'textlink' => true, |
| 375 | // ); |
| 376 | $html .= self::get_view_body( $post, $options ); |
| 377 | |
| 378 | $html .= '</div><!-- [ /.media ] -->'; |
| 379 | return $html; |
| 380 | } |
| 381 | |
| 382 | /*-------------------------------------------*/ |
| 383 | /* UI Helper method |
| 384 | /*-------------------------------------------*/ |
| 385 | /** |
| 386 | * Convert col-count from inputed column count. |
| 387 | * @param integer $input_col [description] |
| 388 | * @return [type] [description] |
| 389 | */ |
| 390 | public static function get_col_converted_size( $input_col = 4 ) { |
| 391 | if ( $input_col == 1 ) { |
| 392 | $col = 12; |
| 393 | } elseif ( $input_col == 2 ) { |
| 394 | $col = 6; |
| 395 | } elseif ( $input_col == 3 ) { |
| 396 | $col = 4; |
| 397 | } elseif ( $input_col == 4 ) { |
| 398 | $col = 3; |
| 399 | } |
| 400 | return strval( $col ); |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * Get all size col classes |
| 405 | * @param [type] $attributes inputed col numbers array |
| 406 | * @return [type] [description] |
| 407 | */ |
| 408 | public static function get_col_size_classes( $attributes ) { |
| 409 | $col_class_array = array(); |
| 410 | $sizes = array( 'xs', 'sm', 'md', 'lg', 'xl' ); |
| 411 | foreach ( $sizes as $key => $size ) { |
| 412 | if ( ! empty( $attributes[ 'col_' . $size ] ) ) { |
| 413 | $col_class_array[] = 'vk_post-col-' . $size . '-' . self::get_col_converted_size( $attributes[ 'col_' . $size ] ); |
| 414 | } |
| 415 | } |
| 416 | $col_class = implode( ' ', $col_class_array ); |
| 417 | return $col_class; |
| 418 | } |
| 419 | |
| 420 | } |
| 421 | } |
| 422 |