]> _ Git - physioassist-wordpress.git/commitdiff
WIP #1912 @7.5
authorstephen@cubedesigners.com <stephen@cubedesigners.com@f5622870-0f3c-0410-866d-9cb505b7a8ef>
Thu, 17 May 2018 17:12:35 +0000 (17:12 +0000)
committerstephen@cubedesigners.com <stephen@cubedesigners.com@f5622870-0f3c-0410-866d-9cb505b7a8ef>
Thu, 17 May 2018 17:12:35 +0000 (17:12 +0000)
wp-content/mu-plugins/physioassist/src/Elementor/Setup.php
wp-content/mu-plugins/physioassist/src/Elementor/Widgets/VideoGallery.php [new file with mode: 0644]
wp-content/themes/physioassist/app/setup.php
wp-content/themes/physioassist/resources/assets/config.json
wp-content/themes/physioassist/resources/assets/scripts/video-carousel.js [new file with mode: 0644]
wp-content/themes/physioassist/resources/assets/styles/main.styl
wp-content/themes/physioassist/resources/assets/styles/widgets/video-carousel.styl [new file with mode: 0644]
wp-content/themes/physioassist/resources/assets/styles/widgets/video-gallery.styl [new file with mode: 0644]
wp-content/themes/physioassist/resources/views/widgets/video-gallery-carousel.blade.php [new file with mode: 0644]
wp-content/themes/physioassist/resources/views/widgets/video-gallery-grid.blade.php [new file with mode: 0644]

index 2ecd08dca6374a41c77c90b86cc7c7088324848f..145f3a724613ae3515f0a0703e52a5bba1467372 100644 (file)
@@ -75,12 +75,12 @@ class Setup {
         $elementor->widgets_manager->register_widget_type( new Widgets\HeroBlock() );
         $elementor->widgets_manager->register_widget_type( new Widgets\ProfileGrid() );
         $elementor->widgets_manager->register_widget_type( new Widgets\ResourceGrid() );
+        $elementor->widgets_manager->register_widget_type( new Widgets\VideoGallery() );
         $elementor->widgets_manager->register_widget_type( new Widgets\BackgroundImage() );
     }
 
     public function register_scripts() {
-        // Widgets\GoogleMaps::register_scripts();
-        // Widgets\Carousel::register_scripts();
+        Widgets\VideoGallery::register_scripts();
     }
 
 
diff --git a/wp-content/mu-plugins/physioassist/src/Elementor/Widgets/VideoGallery.php b/wp-content/mu-plugins/physioassist/src/Elementor/Widgets/VideoGallery.php
new file mode 100644 (file)
index 0000000..8239edd
--- /dev/null
@@ -0,0 +1,215 @@
+<?php
+
+namespace PhysioAssist\Elementor\Widgets;
+
+use Elementor\Widget_Base;
+use Elementor\Controls_Manager;
+use Elementor\Utils;
+use Elementor\Embed;
+
+
+class VideoGallery extends Widget_Base {
+
+    // Widget name / ID
+    public function get_name() {
+        return 'cube-video-gallery';
+    }
+
+    // Elementor widget title
+    public function get_title() {
+        return __( 'Video Gallery', 'cube' );
+    }
+
+    // Elementor interface icon
+    public function get_icon() {
+        return 'eicon-play';
+    }
+
+    // Where to display the widget in the Elementor interface
+    public function get_categories() {
+        return [ 'theme-elements' ];
+    }
+
+
+    // Register scripts related to this widget (called by the Elementor\Setup)
+    // See also get_script_depends()
+    public static function register_scripts() {
+        wp_register_script(
+            'cube-video-carousel',
+            \App\asset_path('scripts/video-carousel.js'),
+            ['jquery'], // Dependencies
+            null, // Version
+            true // In footer?
+        );
+    }
+
+
+    /**
+     * List of scripts the widget depends on.
+     * Used to set scripts dependencies required to run the widget.
+     *
+     * @since 1.0.0
+     * @access public
+     * @return array Widget scripts dependencies.
+     */
+    public function get_script_depends() {
+        // Use script already registered by Elementor so we don't have to include another copy of Slick
+        return [ 'jquery-slick', 'cube-video-carousel' ];
+    }
+    /**
+     * Register the widget controls.
+     * Adds different input fields to allow the user to change and customize the widget settings.
+     *
+     * @since 1.0.0
+     * @access protected
+     */
+    protected function _register_controls() {
+
+        $this->start_controls_section(
+            'section_content',
+            [
+                'label' => __( 'Video Gallery', 'cube' ),
+            ]
+        );
+
+        $this->add_control(
+            'type',
+            [
+                'label' => __('Gallery Type', 'cube'),
+                'type' => Controls_Manager::SELECT,
+                'options' => [
+                    'grid' => __( 'Grid', 'cube' ),
+                    'carousel' => __( 'Carousel', 'cube' ),
+                ],
+                'default' => 'grid',
+                'render_type' => 'template', // Make editor re-render when this changes
+                'prefix_class' => 'video-gallery-type-',
+            ]
+        );
+
+        $this->add_control(
+            'videos',
+            [
+                'label' => __( 'Videos', 'cube' ),
+                'type' => Controls_Manager::REPEATER,
+                'fields' => [
+                    [
+                        'name' => 'url',
+                        'label' => __( 'Video URL', 'cube' ),
+                        'placeholder' => __( 'Enter YouTube link', 'cube' ),
+                        'type' => Controls_Manager::TEXT,
+                        'label_block' => true,
+                    ],
+                    [
+                        'name' => 'title',
+                        'label' => __( 'Title', 'cube' ),
+                        'type' => Controls_Manager::TEXT,
+                        'label_block' => true,
+                    ],
+                    [
+                        'name' => 'details',
+                        'label' => __( 'Additional Details', 'cube' ),
+                        'placeholder' => __( 'Extra information for slider view', 'cube' ),
+                        'type' => Controls_Manager::TEXT,
+                        'label_block' => true,
+                    ],
+                    [
+                        'name' => 'image',
+                        'label' => __('Preview Image (optional)', 'cube'),
+                        'label_block' => true,
+                        'type' => Controls_Manager::MEDIA,
+                        'default' => [
+                            'url' => Utils::get_placeholder_image_src(),
+                        ],
+                    ],
+                ],
+                'title_field' => '{{{ title }}}',
+            ]
+        );
+        $this->end_controls_section();
+
+
+    }
+    /**
+     * Render the widget output on the frontend.
+     * Written in PHP and used to generate the final HTML.
+     *
+     * @since 1.0.0
+     * @access protected
+     */
+    protected function render() {
+
+        $gallery_type = $this->get_settings('type');
+        $videos = $this->get_settings('videos');
+
+        // First, filter out any items that don't have a video URL
+        $videos = array_filter($videos, function($v) {
+            return (!empty($v['url']));
+        });
+
+        // Get videos and process each video URL so it can be used with Elementor lightbox
+        $videos = array_map(function($video) {
+
+            // Video settings for lightbox embed
+            $embed_params = [
+                'rel' => 0, // Don't show related videos at the end of playback
+                'showinfo' => 0 // Hide info
+            ];
+
+            $lightbox_options = [
+                'type'         => 'video',
+                'url'          => Embed::get_embed_url($video['url'], $embed_params),
+                'modalOptions' => [
+                    'id'                => 'elementor-lightbox-' . $this->get_id(),
+                    'entranceAnimation' => 'zoomIn',
+                    'videoAspectRatio'  => '169',
+                ],
+            ];
+
+            $video['lightbox'] = wp_json_encode($lightbox_options);
+
+            $video_properties = Embed::get_video_properties($video['url']);
+
+
+            // If no preview image is set, fetch the one from Youtube
+            if (empty($video['image']['id'])) {
+                $video['preview'] = $this->youtube_image($video_properties['video_id']);
+            } else {
+                $video['preview'] = wp_get_attachment_image_url($video['image']['id'], 'video-preview');
+            }
+
+            return $video;
+        }, $videos);
+
+        if ($gallery_type == 'carousel') {
+            echo \App\template('widgets/video-gallery-carousel', compact('videos'));
+        } else {
+            echo \App\template('widgets/video-gallery-grid', compact('videos'));
+        }
+
+    }
+
+
+    // We can't guarantee that the video's maxresdefault.jpg will exist (it's not created for non-HD uploads)
+    // So this function will find the highest res image available...
+    // Ref: https://stackoverflow.com/a/20655623
+    function youtube_image($id) {
+        $resolution = [
+            'maxresdefault',
+            'hqdefault', // Might have black bars
+            'mqdefault', // No black bars on this size normally
+            'sddefault',
+            'default'
+        ];
+
+        for ($x = 0; $x < sizeof($resolution); $x++) {
+            $url = 'https://img.youtube.com/vi/' . $id . '/' . $resolution[$x] . '.jpg';
+            // Make sure we get a 200 OK HTTP response
+            if (strpos(get_headers($url)[0], '200 OK') !== false) {
+                break;
+            }
+        }
+        return $url;
+    }
+
+}
index 265cfe76a19a6dbc0d92091ce07487153310c0ee..4890390e16f50e09b2c05c50518542210515281a 100644 (file)
@@ -49,6 +49,7 @@ add_action('after_setup_theme', function () {
      */
     add_theme_support('post-thumbnails');
     set_post_thumbnail_size(237, 237, true);
+    add_image_size('video-preview', 640, 360, true); // 16:9 ratio cropped
 
     /**
      * Enable HTML5 markup support
index 4b3a8d4d67bd00f04c7a9199c168c4e378db8086..32f4a54ae5a2c9b5c7faf760bbb3faa12cd0944f 100644 (file)
@@ -4,6 +4,9 @@
       "./scripts/main.js",
       "./styles/main.styl"
     ],
+    "video-carousel": [
+      "./scripts/video-carousel.js"
+    ],
     "customizer": [
       "./scripts/customizer.js"
     ]
diff --git a/wp-content/themes/physioassist/resources/assets/scripts/video-carousel.js b/wp-content/themes/physioassist/resources/assets/scripts/video-carousel.js
new file mode 100644 (file)
index 0000000..d466105
--- /dev/null
@@ -0,0 +1,15 @@
+// ELEMENTOR Trigger
+(function($) {
+  // Trigger handler when element ready
+  $(window).on( 'elementor/frontend/init', function() {
+    // eslint-disable-next-line
+    elementorFrontend.hooks.addAction('frontend/element_ready/cube-video-gallery.default', function ($scope) {
+
+      // Only trigger if it is a carousel type gallery
+      if ($scope.hasClass('video-gallery-type-carousel')) {
+        console.log('CAROUSEL enabled') // eslint-disable-line
+        $scope.find('.video-carousel').slick(); // Note: settings come from data-attribute in HTML
+      }
+    });
+  });
+})(jQuery);
index bb4e2aaeed7e910d6c26f8da6e6353f6293ec0b7..58f83daf5f2bad094d9d41d2923640853f1867d9 100644 (file)
@@ -23,7 +23,7 @@
 @import 'layouts/header'
 @import 'layouts/sidebar'
 @import 'layouts/footer'
- @import 'layouts/sections'
+@import 'layouts/sections'
 @import 'layouts/pages'
 @import 'layouts/posts'
 @import 'layouts/tinymce'
diff --git a/wp-content/themes/physioassist/resources/assets/styles/widgets/video-carousel.styl b/wp-content/themes/physioassist/resources/assets/styles/widgets/video-carousel.styl
new file mode 100644 (file)
index 0000000..e64f000
--- /dev/null
@@ -0,0 +1,66 @@
+.video-carousel
+  padding: 0 80px
+
+  &-item
+    cursor: pointer
+    text-align: center
+    margin: 0 20px
+    outline: none
+
+    &-image
+      display: block
+      width: 100%
+      position: relative
+
+      &:before
+        content: ''
+        position: absolute
+        top: 0
+        right: 0
+        bottom: 0
+        left: 0
+        background: rgba(#000, 0.2)
+
+      // Todo: Add play icon on image
+
+
+    &-title
+      color: $colors.headings
+      font-size: 18px
+      font-weight: 600
+      text-transform: uppercase
+      line-height: 1.2
+      margin-top: 1.5em
+
+    &-details
+      color: $colors.sub-headings
+
+
+  // Slick overrides
+  .slick-arrow
+    width: auto
+    background: $colors.light-grey
+    border-radius: 50%
+    font-size: 56px
+    line-height: 1.25
+    width: 1.25em
+    height: @width
+    text-align: center
+
+    &:before
+      display: inline-block
+      color: $colors.headings
+      font-size: 1em
+
+    &:hover
+      background: darken($colors.light-grey, 5%)
+
+
+  .slick-prev
+    left: 0
+    &:before
+      margin-right: 0.1em
+  .slick-next
+    right: 0
+    &:before
+      margin-left: 0.1em
diff --git a/wp-content/themes/physioassist/resources/assets/styles/widgets/video-gallery.styl b/wp-content/themes/physioassist/resources/assets/styles/widgets/video-gallery.styl
new file mode 100644 (file)
index 0000000..1ce762a
--- /dev/null
@@ -0,0 +1,27 @@
+.video-gallery
+  display: flex
+  flex-wrap: wrap
+  constrain(margin-bottom, -3vw) // Counteract space between rows for the last row
+
+  &-item
+    constrain(margin-bottom, 3vw)
+    cursor: pointer
+    lost-column: 1/3 3 60px
+
+    +below(768px)
+      lost-column: 1/2
+    +below(500px)
+      lost-column: 1/1
+
+    &-image
+      display: block
+      width: 100%
+
+    &-title
+      color: $colors.sub-headings
+      font-size: 16px
+      font-weight: 400
+      text-transform: uppercase
+      text-align: center
+      line-height: 1.2
+      margin-top: 1.5em
diff --git a/wp-content/themes/physioassist/resources/views/widgets/video-gallery-carousel.blade.php b/wp-content/themes/physioassist/resources/views/widgets/video-gallery-carousel.blade.php
new file mode 100644 (file)
index 0000000..23cb148
--- /dev/null
@@ -0,0 +1,25 @@
+{{--VIDEO GALLERY CAROUSEL--}}
+@php
+  $settings = [
+    'slidesToShow' => 2,
+    'slidesToScroll' => 1,
+    'infinite' => true,
+  ];
+
+  $slick = json_encode($settings);
+
+@endphp
+
+<div class="video-carousel elementor-slick-slider" data-slick="{{ $slick }}">
+  @foreach ($videos as $video)
+    <div class="video-carousel-item slick-slide">
+      <div class="slick-slide-inner" data-elementor-lightbox="{{ $video['lightbox'] }}" data-elementor-open-lightbox="yes">
+        <div class="video-carousel-item-image">
+          <img src="{{ $video['preview'] }}">
+        </div>
+        <h4 class="video-carousel-item-title">{{ $video['title'] }}</h4>
+        <p class="video-carousel-item-details">{{ $video['details'] }}</p>
+      </div>
+    </div>
+  @endforeach
+</div>
diff --git a/wp-content/themes/physioassist/resources/views/widgets/video-gallery-grid.blade.php b/wp-content/themes/physioassist/resources/views/widgets/video-gallery-grid.blade.php
new file mode 100644 (file)
index 0000000..f13e1e9
--- /dev/null
@@ -0,0 +1,10 @@
+{{--VIDEO GALLERY GRID--}}
+
+<div class="video-gallery">
+  @foreach ($videos as $video)
+    <div class="video-gallery-item" data-elementor-lightbox="{{ $video['lightbox'] }}" data-elementor-open-lightbox="yes">
+      <img src="{{ $video['preview'] }}" class="video-gallery-item-image">
+      <h4 class="video-gallery-item-title">{{ $video['title'] }}</h4>
+    </div>
+  @endforeach
+</div>