Advertisement
Guenni007

class-avia-newsbox

Apr 3rd, 2023
490
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.54 KB | None | 0 0
  1. <?php
  2. namespace aviaFramework\widgets;
  3.  
  4. use WP_Query;
  5.  
  6. /**
  7.  * AVIA NEWSBOX
  8.  *
  9.  * Widget that creates a list of latest news entries
  10.  *
  11.  * @package AviaFramework
  12.  * @since ???
  13.  * @since 4.9           Code was moved from class-framework-widgets.php
  14.  */
  15. if( ! defined( 'AVIA_FW' ) ) {  exit( 'No direct script access allowed' );  }
  16.  
  17. if( ! class_exists( __NAMESPACE__ . '\avia_newsbox' ) )
  18. {
  19.     class avia_newsbox extends \aviaFramework\widgets\base\Avia_Widget
  20.     {
  21.         /**
  22.          *
  23.          * @var string
  24.          */
  25.         protected $avia_term;
  26.  
  27.         /**
  28.          *
  29.          * @var string
  30.          */
  31.         protected $avia_post_type;
  32.  
  33.         /**
  34.          *
  35.          * @var string
  36.          */
  37.         protected $avia_new_query;
  38.  
  39.         /**
  40.          * @since 4.9                       added parameters $id_base, ... $control_options
  41.          * @param string $id_base
  42.          * @param string $name
  43.          * @param array $widget_options
  44.          * @param array $control_options
  45.          */
  46.         public function __construct( $id_base = '', $name = '', $widget_options = array(), $control_options = array() )
  47.         {
  48.             if( empty( $id_base ) )
  49.             {
  50.                 $id_base = 'newsbox';
  51.             }
  52.  
  53.             if( empty( $name ) )
  54.             {
  55.                 $name = THEMENAME . ' ' . __( 'Latest News', 'avia_framework' );
  56.             }
  57.  
  58.             if( empty( $widget_options ) )
  59.             {
  60.                 $widget_options = array(
  61.                                 'classname'             => 'newsbox',
  62.                                 'description'           => __( 'A Sidebar widget to display latest post entries in your sidebar', 'avia_framework' ),
  63.                                 'show_instance_in_rest' => true,
  64.                                 'customize_selective_refresh' => false
  65.                             );
  66.             }
  67.  
  68.             parent::__construct( $id_base, $name, $widget_options, $control_options );
  69.  
  70.             $this->defaults = array(
  71.                                 'title'     => '',
  72.                                 'count'     => '',
  73.                                 'excerpt'   => '',
  74.                                 'cat'       => ''
  75.                             );
  76.  
  77.             $this->avia_term = '';
  78.             $this->avia_post_type = '';
  79.             $this->avia_new_query = '';
  80.         }
  81.  
  82.         /**
  83.          * Output the widget in frontend
  84.          *
  85.          * @since 4.9
  86.          * @param array $args
  87.          * @param array $instance
  88.          */
  89.         public function widget( $args, $instance )
  90.         {
  91.             global $avia_config;
  92.  
  93.             $instance = $this->parse_args_instance( $instance );
  94.  
  95.             extract( $args, EXTR_SKIP );
  96.  
  97.             echo $before_widget;
  98.  
  99.             $title = empty( $instance['title'] ) ? '' : apply_filters( 'widget_title', $instance['title'] );
  100.             $count = empty( $instance['count'] ) ? '' : $instance['count'];
  101.             $cat = empty( $instance['cat'] ) ? '' : $instance['cat'];
  102.             $excerpt = empty( $instance['excerpt'] ) ? '' : $instance['excerpt'];
  103.             $image_size = isset( $avia_config['widget_image_size'] ) ? $avia_config['widget_image_size'] : 'widget';
  104.  
  105.             /**
  106.              * @since 4.5.4
  107.              * @param string $image_size
  108.              * @param array $args
  109.              * @param array $instance
  110.              * @return string
  111.              */
  112.             $image_size = apply_filters( 'avf_newsbox_image_size', $image_size, $args, $instance );
  113.  
  114.             if( ! empty( $title ) )
  115.             {
  116.                 echo $before_title . $title . $after_title;
  117.             }
  118.  
  119.             if( empty( $this->avia_term ) )
  120.             {
  121.                 $additional_loop = new WP_Query( "cat={$cat}&posts_per_page={$count}" );
  122.             }
  123.             else
  124.             {
  125.                 $catarray = explode( ',', $cat );
  126.  
  127.                 if( empty( $catarray[0] ) )
  128.                 {
  129.                     $new_query = array(
  130.                                     'posts_per_page'    => $count,
  131.                                     'post_type'         => $this->avia_post_type
  132.                                 );
  133.                 }
  134.                 else
  135.                 {
  136.                     if( $this->avia_new_query )
  137.                     {
  138.                         $new_query = $this->avia_new_query;
  139.                     }
  140.                     else
  141.                     {
  142.                         $new_query = array(
  143.                                         'posts_per_page'    => $count,
  144.                                         'tax_query'         => array(
  145.                                                                 array(
  146.                                                                     'taxonomy'  => $this->avia_term,
  147.                                                                     'field'     => 'id',
  148.                                                                     'terms'     => explode( ',', $cat ),
  149.                                                                     'operator'  => 'IN'
  150.                                                                 )
  151.                                                             )
  152.                                                         );
  153.                     }
  154.                 }
  155.  
  156.                 $additional_loop = new WP_Query( $new_query );
  157.             }
  158.  
  159.             if( $additional_loop->have_posts() )
  160.             {
  161.                 echo '<ul class="news-wrap image_size_' . $image_size . '">';
  162.  
  163.                 while( $additional_loop->have_posts() )
  164.                 {
  165.                     $additional_loop->the_post();
  166.  
  167.                     $format = '';
  168.  
  169.                     if( empty( $this->avia_post_type ) )
  170.                     {
  171.                         $format = $this->avia_post_type;
  172.                     }
  173.  
  174.                     if( empty( $format ) )
  175.                     {
  176.                         $format = get_post_format();
  177.                     }
  178.  
  179.                     if( empty( $format ) )
  180.                     {
  181.                         $format = 'standard';
  182.                     }
  183.  
  184.                     $the_id = get_the_ID();
  185.                     $link = get_post_meta( $the_id , '_portfolio_custom_link', true ) != '' ? get_post_meta( $the_id ,'_portfolio_custom_link_url', true ) : get_permalink();
  186.  
  187.                     echo '<li class="news-content post-format-'.$format.'">';
  188.  
  189.                     //check for preview images:
  190.                     $image = '';
  191.  
  192.                     if( ! current_theme_supports( 'force-post-thumbnails-in-widget' ) )
  193.                     {
  194.                         $slides = avia_post_meta( get_the_ID(), 'slideshow', true );
  195.  
  196.                         if( $slides != '' && ! empty( $slides[0]['slideshow_image'] ) )
  197.                         {
  198.                             $image = avia_image_by_id( $slides[0]['slideshow_image'], $image_size, 'image' );
  199.                         }
  200.                     }
  201.  
  202.                     if( current_theme_supports( 'post-thumbnails' ) && ! $image )
  203.                     {
  204.                         $image = get_the_post_thumbnail( $the_id, $image_size );
  205.                     }
  206.  
  207.                     $time_format = apply_filters( 'avia_widget_time', get_option( 'date_format' ) . ' - ' . get_option( 'time_format' ), 'avia_newsbox' );
  208.  
  209.                     echo '<a class="news-link" title="' . get_the_title() . '" href="' . $link . '">';
  210.  
  211.                     $nothumb = ( ! $image ) ? 'no-news-thumb' : '';
  212.  
  213.                     echo "<span class='news-thumb {$nothumb}'>";
  214.                     echo    $image;
  215.                     echo '</span>';
  216.  
  217.                     if( empty( $avia_config['widget_image_size'] ) || 'display title and excerpt' != $excerpt )
  218.                     {
  219.                         echo '<strong class="news-headline">' . get_the_title();
  220.  
  221.                         echo '<br />'. __( 'in', 'avia_framework') . ': ';
  222.                         $cats = get_the_category();
  223.                         $catsArray = [];
  224.                         foreach($cats as $cat)
  225.                         $catsArray[] = '<span class = "widget_latest_category">' .$cat->cat_name. '</span>';
  226.                         echo implode(", ", $catsArray);
  227.  
  228.                         if( $time_format )
  229.                         {
  230.                             echo '<span class="news-time">' . get_the_time( $time_format ) . '</span>';
  231.                         }
  232.  
  233.                         echo '</strong>';
  234.                     }
  235.  
  236.                     echo '</a>';
  237.  
  238.                     if( 'display title and excerpt' == $excerpt )
  239.                     {
  240.                         echo '<div class="news-excerpt">';
  241.  
  242.                         if( ! empty( $avia_config['widget_image_size'] ) )
  243.                         {
  244.                             echo '<a class="news-link-inner" title="' . get_the_title() . '" href="' . $link . '">';
  245.                             echo    '<strong class="news-headline">' . get_the_title() . '</strong>';
  246.                             echo '</a>';
  247.  
  248.                             echo '<br />'. __( 'in', 'avia_framework') . ': ';
  249.                             $cats = get_the_category();
  250.                             $catsArray = [];
  251.                             foreach($cats as $cat)
  252.                             $catsArray[] = '<span class = "widget_latest_category">' .$cat->cat_name. '</span>';
  253.                             echo implode(", ", $catsArray);
  254.  
  255.                             if( $time_format )
  256.                             {
  257.                                 echo '<span class="news-time">' . get_the_time( $time_format ) . '</span>';
  258.                             }
  259.                         }
  260.  
  261.                         the_excerpt();
  262.  
  263.                         echo '</div>';
  264.                     }
  265.  
  266.                     echo '</li>';
  267.                 }
  268.  
  269.                 echo '</ul>';
  270.                 wp_reset_postdata();
  271.             }
  272.  
  273.             echo $after_widget;
  274.         }
  275.  
  276.         /**
  277.          * Update widget options
  278.          *
  279.          * @param array $new_instance
  280.          * @param array $old_instance
  281.          * @return array
  282.          */
  283.         public function update( $new_instance, $old_instance )
  284.         {
  285.             $instance = $this->parse_args_instance( $old_instance );
  286.  
  287.             $instance['title'] = strip_tags( $new_instance['title'] );
  288.             $instance['count'] = strip_tags( $new_instance['count'] );
  289.             $instance['excerpt'] = strip_tags( $new_instance['excerpt'] );
  290.  
  291.             if( ! empty( $new_instance['cat'] ) )
  292.             {
  293.                 $instance['cat'] = is_array( $new_instance['cat'] ) ? implode( ',', $new_instance['cat'] ) : strip_tags( $new_instance['cat'] );
  294.             }
  295.  
  296.             return $instance;
  297.         }
  298.  
  299.         /**
  300.          * Output the form in backend
  301.          *
  302.          * @param array $instance
  303.          */
  304.         public function form( $instance )
  305.         {
  306.             $instance = $this->parse_args_instance( $instance );
  307.  
  308.             $title = strip_tags( $instance['title'] );
  309.             $count = strip_tags( $instance['count'] );
  310.             $excerpt = strip_tags( $instance['excerpt'] );
  311.  
  312.             $elementCat = array(
  313.                         'name'      => __( 'Which categories should be used for the portfolio?', 'avia_framework' ),
  314.                         'desc'      => __( 'You can select multiple categories here', 'avia_framework' ),
  315.                         'id'        => $this->get_field_name( 'cat' ) . '[]',
  316.                         'type'      => 'select',
  317.                         'std'       => strip_tags( $instance['cat'] ),
  318.                         'class'     => '',
  319.                         'multiple'  => 6,
  320.                         'subtype'   => 'cat'
  321.                     );
  322.  
  323.             //check if a different taxonomy than the default is set
  324.             if( ! empty( $this->avia_term ) )
  325.             {
  326.                 $elementCat['taxonomy'] = $this->avia_term;
  327.             }
  328.  
  329.             $html = new \avia_htmlhelper();
  330.     ?>
  331.             <p>
  332.                 <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'avia_framework' ); ?>
  333.                 <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></label>
  334.             </p>
  335.  
  336.             <p>
  337.                 <label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php _e( 'How many entries do you want to display: ', 'avia_framework' ); ?></label>
  338.                 <select class="widefat" id="<?php echo $this->get_field_id( 'count ' ); ?>" name="<?php echo $this->get_field_name( 'count' ); ?>">
  339.                     <?php
  340.                     $list = '';
  341.                     for ($i = 1; $i <= 20; $i++ )
  342.                     {
  343.                         $selected = '';
  344.                         if( $count == $i )
  345.                         {
  346.                             $selected = 'selected="selected"';
  347.                         }
  348.  
  349.                         $list .= "<option {$selected} value='{$i}'>{$i}</option>";
  350.                     }
  351.                     $list .= '</select>';
  352.                     echo $list;
  353.                     ?>
  354.             </p>
  355.  
  356.             <p><label for="<?php echo $this->get_field_id( 'cat' ); ?>"><?php _e( 'Choose the categories you want to display (multiple selection possible):', 'avia_framework' ); ?>
  357.             <?php echo $html->select( $elementCat ); ?>
  358.             </label></p>
  359.  
  360.             <p>
  361.                 <label for="<?php echo $this->get_field_id( 'excerpt' ); ?>"><?php _e( 'Display title only or title &amp; excerpt', 'avia_framework' ); ?></label>
  362.                 <select class="widefat" id="<?php echo $this->get_field_id( 'excerpt' ); ?>" name="<?php echo $this->get_field_name( 'excerpt' ); ?>">
  363.                     <?php
  364.                     $list = '';
  365.                     $answers = array(
  366.                                 'show title only'           =>  __( 'show title only', 'avia_framework' ),
  367.                                 'display title and excerpt' =>  __( 'display title and excerpt', 'avia_framework' )
  368.                                 );
  369.  
  370.                     foreach ( $answers as $key => $answer )
  371.                     {
  372.                         $selected = '';
  373.                         if( $key == $excerpt )
  374.                         {
  375.                             $selected = 'selected="selected"';
  376.                         }
  377.  
  378.                         $list .= "<option {$selected} value='{$key}'>{$answer}</option>";
  379.                     }
  380.                     $list .= '</select>';
  381.                     echo $list;
  382.                     ?>
  383.             </p>
  384. <?php
  385.         }
  386.     }
  387. }
  388.  
  389.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement