]> _ Git - ccv-wordpress.git/commitdiff
WIP #3053
authorStephen Cameron <stephen@cubedesigners.com>
Thu, 19 Dec 2019 18:09:02 +0000 (19:09 +0100)
committerStephen Cameron <stephen@cubedesigners.com>
Thu, 19 Dec 2019 18:09:02 +0000 (19:09 +0100)
wp-content/mu-plugins/cube/src/Elementor/Setup.php
wp-content/mu-plugins/cube/src/Elementor/Widgets/ScientificNews.php [new file with mode: 0644]
wp-content/themes/CCV/resources/views/partials/content-scientific_news.blade.php
wp-content/themes/CCV/resources/views/partials/content-single-scientific_news.blade.php

index 556b0f6e6bdd8fc8b706c35331e86977ec47abd7..b52c12f18c6c655a91c545c68d189bb7172d5a69 100644 (file)
@@ -40,6 +40,7 @@ class Setup {
         $elementor->widgets_manager->register_widget_type( new Widgets\PhotoGrid() );
         $elementor->widgets_manager->register_widget_type( new Widgets\PeopleGrid() );
         $elementor->widgets_manager->register_widget_type( new Widgets\Timeline() );
+        $elementor->widgets_manager->register_widget_type( new Widgets\ScientificNews() );
     }
 
     protected function _customise_sections() {
diff --git a/wp-content/mu-plugins/cube/src/Elementor/Widgets/ScientificNews.php b/wp-content/mu-plugins/cube/src/Elementor/Widgets/ScientificNews.php
new file mode 100644 (file)
index 0000000..0184156
--- /dev/null
@@ -0,0 +1,105 @@
+<?php
+
+namespace Cube\Elementor\Widgets;
+
+use Elementor\Controls_Manager;
+
+use function Roots\view;
+
+
+class ScientificNews extends _Base {
+
+    protected $_has_template_content = false; // Tell Elementor that content is all rendered dynamically
+
+    // Widget name / ID
+    public function get_name() {
+        return 'cube-scientific-news';
+    }
+
+    // Elementor widget title
+    public function get_title() {
+        return __( 'Scientific News', 'cube' );
+    }
+
+    // Elementor interface icon
+    public function get_icon() {
+        return 'eicon-post-list';
+    }
+
+    /**
+     * 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() {
+        return [];
+    }
+    /**
+     * 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' => __( 'Scientific News Posts', 'cube' ),
+            ]
+        );
+
+        $this->add_control(
+            'widget_description',
+            [
+                'raw' => __( 'This widget will display the most recent scientific news posts.', 'cube' ),
+                'type' => Controls_Manager::RAW_HTML,
+                'content_classes' => 'elementor-descriptor',
+            ]
+        );
+
+        $this->add_control(
+            'posts_limit',
+            [
+                'label' => __( 'Number of posts to display', 'cube' ),
+                'type' => Controls_Manager::NUMBER,
+                'default' => 10,
+            ]
+        );
+        
+        $this->end_controls_section();
+
+        $this->common_controls();
+    }
+    /**
+     * 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() {
+
+        $posts_limit = $this->get_settings('posts_limit');
+
+        $science_posts = wp_get_recent_posts([
+            'numberposts' => $posts_limit,
+            'orderby' => 'post_date',
+            'order' => 'DESC',
+            'post_type' => 'scientific_news',
+            'post_status' => 'publish',
+            'suppress_filters' => true
+        ]);
+
+        if ($science_posts) {
+            foreach ($science_posts as $science_post) {
+                echo view('partials.content-scientific_news', compact('science_post'));
+            }
+        }
+
+    }
+}
index b18c0505a17dd3c40a48db7c0b763e272cb6fa34..8731dee2813dcec60fd740e2c2a15bbc22406bf4 100644 (file)
@@ -1,24 +1,33 @@
-<article @php(post_class('flex items-start mt-1v sm:block sm:mt-12'))>
+@php
+  // Note: since this template is used by the standard WordPress loop AND by the Elementor widget, which
+  // exists outside the loop, we are sometimes getting data that is passed in and other times relying on
+  // the global loop context. As a result, the code had to be modified a bit to work in both situations.
+  $postID = isset($science_post) ? $science_post['ID'] : $post->ID;
+@endphp
 
-  <div class="post-featured-image min-w-0 mr-1v mb-1v" style="background-image: url({{ get_the_post_thumbnail_url() }}); max-width: 135px;">
-    <a href="{{ get_permalink() }}">
+<article <?php post_class('flex items-start mt-1v sm:block sm:mt-12', $postID) ?>>
+
+  <div class="post-featured-image min-w-0 mr-1v mb-1v" style="background-image: url({{ get_the_post_thumbnail_url($postID) }}); max-width: 135px;">
+    <a href="{{ get_permalink($postID) }}">
       <div class="post-featured-image-sizer">{{-- Just here as a proportional sizer thanks to the padding --}}</div>
     </a>
   </div>
 
   <header style="flex-basis: 100%">
     <h2 class="plain text-lg mb-2">
-      <a href="{{ get_permalink() }}">
-        {!! $title !!}
+      <a href="{{ get_permalink($postID) }}">
+        {!! get_the_title($postID) !!}
       </a>
     </h2>
 
     <div class="entry-summary">
-      @php(the_excerpt())
+      @php
+        echo apply_filters( 'the_excerpt', get_the_excerpt($postID));
+      @endphp
     </div>
 
     <p class="mt-6 mb-1v">
-      <a class="uppercase text-pink inline-flex items-center" href="{{ get_permalink() }}">
+      <a class="uppercase text-pink inline-flex items-center" href="{{ get_permalink($postID) }}">
         @svg('arrow', 'h-3 mr-2 fill-current')
         <?= __('Lire la suite') ?>
       </a>
index eb145003f5ac1b469c5dd881f18b1a75652902fd..b2a20b579fd8de9fadc9b1fa0f7dbc3a75b62070 100644 (file)
@@ -29,7 +29,7 @@
     @php(the_content())
 
     <p class="mt-6">
-      <a class="uppercase text-pink inline-flex items-center" href="<?= get_post_type_archive_link('post') ?>">
+      <a class="uppercase text-pink inline-flex items-center" href="<?= get_post_type_archive_link('scientific_news') ?>">
         @svg('arrow', 'h-3 mr-2 fill-current')
         <?= __('Retour') ?>
       </a>