--- /dev/null
+<?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'));
+ }
+ }
+
+ }
+}
-<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>