- Widget Definition
• widgets/portfolio-list-widget.php
THIS CODE IS GOING TO BE COPIED INTO TWO PARTS BECAUSE IS TOO LONG.
-CODE PART 2 STARTS HERE-
protected function render() {
settings=this->get_settings_for_display();
items=settings['portfolio_items'];
if ( empty( $items ) ) {
return;
}
$swiper_options = [
'slidesPerView' => $settings['slides_per_view'],
'spaceBetween' => $settings['space_between'],
'loop' => ( 'yes' === $settings['loop_carousel'] ),
'autoplay' => ( 'yes' === $settings['autoplay'] ) ? ['delay' => $settings['autoplay_delay']] : false,
'navigation' => ( 'yes' === $settings['show_navigation'] ) ? ['nextEl' => '.swiper-button-next', 'prevEl' => '.swiper-button-prev'] : false,
'pagination' => ( 'yes' === $settings['show_pagination'] ) ? ['el' => '.swiper-pagination', 'clickable' => true] : false,
'breakpoints' => [
640 => ['slidesPerView' => 1, 'spaceBetween' => 20],
768 => ['slidesPerView' => 2, 'spaceBetween' => 40],
1024 => [
'slidesPerView' => $settings['slides_per_view'],
'spaceBetween' => $settings['space_between'],
],
],
];
?>
<div class="my-custom-portfolio-list swiper" data-swiper-options='<?php echo wp_json_encode( $swiper_options ); ?>'>
<div class="swiper-wrapper">
<?php foreach ( $items as $item ) :
$link_url = $item['portfolio_item_link']['url'] ?? '';
$link_target = ! empty( $item['portfolio_item_link']['is_external'] ) ? '_blank' : '_self';
$link_nofollow = ! empty( $item['portfolio_item_link']['nofollow'] ) ? 'nofollow' : '';
?>
<div class="swiper-slide my-custom-portfolio-list-item">
<div class="my-custom-portfolio-list-item-media">
<?php
switch ( $item['media_type'] ) {
case 'image':
echo Group_Control_Image_Size::get_attachment_image_html( $item, 'full', 'portfolio_item_image' );
break;
case 'self_hosted_video':
if ( ! empty( $item['portfolio_item_video']['url'] ) ) : ?>
<video controls muted playsinline>
<source src="<?php echo esc_url( $item['portfolio_item_video']['url'] ); ?>" type="<?php echo esc_attr( $item['portfolio_item_video']['mime'] ); ?>">
<?php esc_html_e( 'Your browser does not support the video tag.', 'my-custom-portfolio-widget' ); ?>
</video>
<?php endif;
break;
case 'youtube':
$youtube_id = $this->get_youtube_id( $item['youtube_video_url']['url'] ?? '' );
if ( $youtube_id ) : ?>
<div class="my-custom-portfolio-video-embed">
<iframe src="https://www.youtube.com/embed/<?php echo esc_attr( $youtube_id ); ?>?controls=0&showinfo=0&rel=0&autoplay=0&loop=0&mute=1" frameborder="0" allowfullscreen allow="autoplay"></iframe>
</div>
<?php endif;
break;
case 'vimeo':
$vimeo_id = $this->get_vimeo_id( $item['vimeo_video_url']['url'] ?? '' );
if ( $vimeo_id ) : ?>
<div class="my-custom-portfolio-video-embed">
<iframe src="https://player.vimeo.com/video/<?php echo esc_attr( $vimeo_id ); ?>?autoplay=0&loop=0&autopause=0&muted=1" frameborder="0" allowfullscreen allow="autoplay"></iframe>
</div>
<?php endif;
break;
}
?>
</div>
<div class="my-custom-portfolio-list-item-footer">
<div class="my-custom-title">
<?php if ( ! empty( $link_url ) && $link_url !== '#' ) : ?>
<a href="<?php echo esc_url( $link_url ); ?>" target="<?php echo esc_attr( $link_target ); ?>" rel="<?php echo esc_attr( $link_nofollow ); ?>" class="my-custom-no-style-link">
<?php echo esc_html( $item['portfolio_item_title'] ); ?>
</a>
<?php else: ?>
<?php echo esc_html( $item['portfolio_item_title'] ); ?>
<?php endif; ?>
</div>
<div class="my-custom-category">
<?php if ( ! empty( $link_url ) && $link_url !== '#' ) : ?>
<a href="<?php echo esc_url( $link_url ); ?>" target="<?php echo esc_attr( $link_target ); ?>" rel="<?php echo esc_attr( $link_nofollow ); ?>" class="my-custom-no-style-link">
<?php echo esc_html( $item['portfolio_item_category'] ); ?>
</a>
<?php else: ?>
<?php echo esc_html( $item['portfolio_item_category'] ); ?>
<?php endif; ?>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php if ( 'yes' === $settings['show_navigation'] ) : ?>
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
<?php endif; ?>
<?php if ( 'yes' === $settings['show_pagination'] ) : ?>
<div class="swiper-pagination"></div>
<?php endif; ?>
</div>
<?php
}
protected function content_template() {
?>
<#
var swiperOptions = {
slidesPerView: settings.slides_per_view,
spaceBetween: settings.space_between,
loop: ( 'yes' === settings.loop_carousel ),
autoplay: ( 'yes' === settings.autoplay ) ? { delay: settings.autoplay_delay } : false,
navigation: ( 'yes' === settings.show_navigation ) ? { nextEl: '.swiper-button-next', prevEl: '.swiper-button-prev' } : false,
pagination: ( 'yes' === settings.show_pagination ) ? { el: '.swiper-pagination', clickable: true } : false,
breakpoints: {
640: { slidesPerView: 1, spaceBetween: 20 },
768: { slidesPerView: 2, spaceBetween: 40 },
1024: { slidesPerView: settings.slides_per_view, spaceBetween: settings.space_between }
}
};
if ( ! settings.portfolio_items || ! settings.portfolio_items.length ) {
return;
}
#>
<div class="my-custom-portfolio-list swiper" data-swiper-options='{{ JSON.stringify(swiperOptions) }}'>
<div class="swiper-wrapper">
<# _.each( settings.portfolio_items, function( item ) {
var mediaHtml = '';
if ( 'image' === item.media_type ) {
var image = {
id: item.portfolio_item_image.id,
url: item.portfolio_item_image.url,
size: item.portfolio_item_image.size,
dimension: item.portfolio_item_image.dimension,
model: item._image,
};
var imageUrl = elementor.imagesManager.getImageUrl( image );
mediaHtml = '<img src="' + imageUrl + '" alt="' + item.portfolio_item_title + '">';
} else if ( 'self_hosted_video' === item.media_type && item.portfolio_item_video && item.portfolio_item_video.url ) {
mediaHtml = '<video controls muted playsinline><source src="' + item.portfolio_item_video.url + '" type="' + item.portfolio_item_video.mime + '">Your browser does not support the video tag.</video>';
} else if ( 'youtube' === item.media_type && item.youtube_video_url && item.youtube_video_url.url ) {
var youtubeId = item.youtube_video_url.url.match(/(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/\s]{11})/);
if ( youtubeId && youtubeId[1] ) {
mediaHtml = '<div class="my-custom-portfolio-video-embed"><iframe src="https://www.youtube.com/embed/' + youtubeId[1] + '?controls=0&showinfo=0&rel=0&autoplay=0&loop=0&mute=1" frameborder="0" allowfullscreen allow="autoplay"></iframe></div>';
}
} else if ( 'vimeo' === item.media_type && item.vimeo_video_url && item.vimeo_video_url.url ) {
var vimeoId = item.vimeo_video_url.url.match(/(?:vimeo\.com\/)(?:channels\/[^\/]+\/|groups\/[^\/]+\/videos\/|album\/\d+\/video\/|video\/|)(\d+)/);
if ( vimeoId && vimeoId[1] ) {
mediaHtml = '<div class="my-custom-portfolio-video-embed"><iframe src="https://player.vimeo.com/video/' + vimeoId[1] + '?autoplay=0&loop=0&autopause=0&muted=1" frameborder="0" allowfullscreen allow="autoplay"></iframe></div>';
}
}
var linkUrl = item.portfolio_item_link.url;
var linkTarget = item.portfolio_item_link.is_external ? '_blank' : '_self';
var linkNofollow = item.portfolio_item_link.nofollow ? 'nofollow' : '';
#>
<div class="swiper-slide my-custom-portfolio-list-item">
<# if ( linkUrl && linkUrl !== '#' ) { #>
<a href="{{ linkUrl }}" target="{{ linkTarget }}" rel="{{ linkNofollow }}">
<# } #>
<div class="my-custom-portfolio-list-item-media">
{{{ mediaHtml }}}
</div>
<div class="my-custom-portfolio-list-item-footer">
<div class="my-custom-title">{{{ item.portfolio_item_title }}}</div>
<div class="my-custom-category">{{{ item.portfolio_item_category }}}</div>
</div>
<# if ( linkUrl && linkUrl !== '#' ) { #>
</a>
<# } #>
</div>
<# }); #>
</div>
<# if ( 'yes' === settings.show_navigation ) { #>
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
<# } #>
<# if ( 'yes' === settings.show_pagination ) { #>
<div class="swiper-pagination"></div>
<# } #>
</div>
<?php
}
private function get_youtube_id( $url ) {
preg_match( '/(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/\s]{11})/i', $url, $matches );
return $matches[1] ?? '';
}
private function get_vimeo_id( $url ) {
preg_match( '/(?:vimeo\.com\/)(?:channels\/[^\/]+\/|groups\/[^\/]+\/videos\/|album\/\d+\/video\/|video\/|)(\d+)/i', $url, $matches );
return $matches[1] ?? '';
}
}
-CODE PART 2 ENDS HERE-