Plugin Directory

Changeset 2994671

Timestamp:
11/12/2023 11:28:00 AM (9 months ago)
Author:
nielslange
Message:

Update to version 2.1 from GitHub

Location:
smntcs-simple-events-widget
Files:
8 added
8 edited
1 copied

Legend:

Unmodified
Added
Removed
  • smntcs-simple-events-widget/tags/2.1/.editorconfig

    r2651293 r2994671  
    1313trim_trailing_whitespace = true
    1414indent_style = tab
    15 indent_size = 4
    1615
    1716[*.yml]
  • smntcs-simple-events-widget/tags/2.1/README.md

    r2985246 r2994671  
    2626
    2727## Changelog
     28
     29
     30
     31
    2832
    2933### 2.0 (2023.10.28)
  • smntcs-simple-events-widget/tags/2.1/README.txt

    r2985246 r2994671  
    33Contributors:       nielslange
    44Tags:               Simple Events, Event, Widget, Sidebar
    5 Stable tag:         2.0
     5Stable tag:         2.
    66Tested up to:       6.4
    77Requires at least:  3.4
     
    3232
    3333== Changelog ==
     34
     35
     36
     37
    3438
    3539= 2.0 (2023.10.28) =
  • smntcs-simple-events-widget/tags/2.1/smntcs-simple-events-widget.php

    r2985246 r2994671  
    77 * Author URI:            https://nielslange.de
    88 * Text Domain:           smntcs-simple-events-widget
    9  * Version:               2.0
     9 * Version:               2.
    1010 * Requires PHP:          5.6
    1111 * Requires at least:     3.4
     
    1818defined( 'ABSPATH' ) || exit;
    1919
    20 /**
    21  * Add settings link on plugin page.
    22  *
    23  * @param  array $links The original array with links.
    24  * @return array The updated array with links.
    25  */
    26 function smntcs_plugin_settings_link( $links ) {
    27     $admin_url    = admin_url( 'widgets.php' );
    28     $settings_url = sprintf( '<a href="%s">%s</a>', $admin_url, __( 'Settings', 'smntcs-simple-events-widget' ) );
    29     array_unshift( $links, $settings_url );
     20// Include the main class file.
     21require_once plugin_dir_path( __FILE__ ) . 'includes/class-smntcs-simple-events.php';
    3022
    31     return $links;
    32 }
    33 add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'smntcs_plugin_settings_link' );
    34 
    35 /**
    36  * Load jQuery datepicker.
    37  *
    38  * @return void
    39  */
    40 function smntcs_admin_enqueue_scripts() {
    41     $plugin_data = get_plugin_data( __FILE__ );
    42     wp_enqueue_script( 'jquery-ui-datepicker' );
    43     wp_enqueue_script( 'smntcs-simple-events-script', plugin_dir_url( __FILE__ ) . '/js/custom.js', array( 'jquery' ), $plugin_data['Version'] );
    44     wp_enqueue_style( 'smntcs-simple-events-styles', plugin_dir_url( __FILE__ ) . '/js/jquery-ui.css', array(), $plugin_data['Version'] );
    45 }
    46 add_action( 'admin_enqueue_scripts', 'smntcs_admin_enqueue_scripts' );
    47 
    48 /**
    49  * Register meta box.
    50  *
    51  * @return void
    52  */
    53 function smntcs_add_meta_boxes() {
    54     add_meta_box( 'meta-box-id', __( 'Event', 'smntcs-simple-events-widget' ), 'smntcs_display_callback', 'post', 'side' );
    55     add_meta_box( 'meta-box-id', __( 'Event', 'smntcs-simple-events-widget' ), 'smntcs_display_callback', 'page', 'side' );
    56 }
    57 add_action( 'add_meta_boxes', 'smntcs_add_meta_boxes' );
    58 
    59 /**
    60  * Convert date to timestamp.
    61  *
    62  * @param  string $date The date to convert, e.g. 01-01-2017.
    63  * @return int The converted timestamp, e.g. 1483228800.
    64  */
    65 function smntcs_date_to_timestamp( $date ) {
    66     return DateTime::createFromFormat( 'd-m-Y', $date, new DateTimeZone( 'UTC' ) )->getTimestamp();
    67 }
    68 
    69 /**
    70  * Convert timestamp to date.
    71  *
    72  * @param string $timestamp The timestamp to convert, e.g. 1483228800.
    73  * @return string The converted date, e.g. 01-01-2017.
    74  */
    75 function smntcs_timestamp_to_date( $timestamp ) {
    76     return gmdate( 'd-m-Y', $timestamp );
    77 }
    78 
    79 /**
    80  * Meta box display callback.
    81  *
    82  * @param  WP_Post $post The original post object.
    83  * @return void
    84  */
    85 function smntcs_display_callback( $post ) {
    86 
    87     $start_date             = get_post_meta( $post->ID, 'datepicker_start', true );
    88     $start_date_value       = ! empty( $start_date ) ? smntcs_timestamp_to_date( $start_date ) : null;
    89     $start_date_placeholder = ! empty( $start_date ) ? smntcs_timestamp_to_date( $start_date ) : 'dd-mm-yyyy';
    90     $end_date               = get_post_meta( $post->ID, 'datepicker_end', true );
    91     $end_date_value         = ! empty( $end_date ) ? smntcs_timestamp_to_date( $end_date ) : null;
    92     $end_date_placeholder   = ! empty( $end_date ) ? smntcs_timestamp_to_date( $end_date ) : 'dd-mm-yyyy';
    93     wp_nonce_field( 'smntcs_add_simple_event', 'smntcs_wpnonce' );
    94     ?>
    95     <table>
    96         <tr class="wrap">
    97             <td><?php echo esc_html_e( 'Start date', 'smntcs-simple-events-widget' ); ?></td>
    98             <td><input type="text" class="datepicker" name="datepicker_start" value="<?php echo esc_html( $start_date_value ); ?>" placeholder="<?php echo esc_html( $start_date_placeholder ); ?>"></td>
    99         </tr>
    100         <tr class="wrap">
    101             <td><?php echo esc_html_e( 'End date', 'smntcs-simple-events-widget' ); ?></td>
    102             <td><input type="text" class="datepicker" name="datepicker_end" value="<?php echo esc_html( $end_date_value ); ?>" placeholder="<?php echo esc_html( $end_date_placeholder ); ?>"></td>
    103         </tr>
    104     </table>
    105     <?php
    106 }
    107 
    108 /**
    109  * Save meta box content.
    110  *
    111  * @param int $post_id The ID of the post to save.
    112  * @return void
    113  */
    114 function smntcs_save_post( $post_id ) {
    115 
    116     // Return if user does not have necessary permissions.
    117     if ( ! current_user_can( 'edit_post', $post_id ) ) {
    118         return;
    119     }
    120 
    121     // Return if user is not on page or post.
    122     if ( ! isset( $_POST['post_type'] ) || ( 'page' !== $_POST['post_type'] && 'post' !== $_POST['post_type'] ) ) {
    123         return;
    124     }
    125 
    126     // Return if nonce is incorrect or not available.
    127     if ( ! isset( $_POST['smntcs_wpnonce'] ) &&
    128          ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['smntcs_wpnonce'] ) ), 'smntcs_add_simple_event' ) ) {
    129         return;
    130     }
    131 
    132     // Return if start date and end date are not available.
    133     if ( ! isset( $_POST['datepicker_start'] ) && ! isset( $_POST['datepicker_end'] ) ) {
    134         return;
    135     }
    136 
    137     // Return if only end date is available.
    138     if ( isset( $_POST['datepicker_start'] ) && ! isset( $_POST['datepicker_end'] ) ) {
    139         return;
    140     }
    141 
    142     // Return if start date is bigger then end date.
    143     if ( '' !== $_POST['datepicker_start'] && '' !== $_POST['datepicker_end'] ) {
    144         $start_date = smntcs_date_to_timestamp( sanitize_text_field( wp_unslash( $_POST['datepicker_start'] ) ) );
    145         $end_date   = smntcs_date_to_timestamp( sanitize_text_field( wp_unslash( $_POST['datepicker_end'] ) ) );
    146 
    147         if ( $start_date > $end_date ) {
    148             return;
    149         }
    150     }
    151 
    152     // Convert, sanitize and save start date, if available, otherwise delete post meta.
    153     if ( '' !== $_POST['datepicker_start'] ) {
    154         $start_date = smntcs_date_to_timestamp( sanitize_text_field( wp_unslash( $_POST['datepicker_start'] ) ) );
    155         update_post_meta( $post_id, 'datepicker_start', sanitize_text_field( $start_date ) );
    156     } else {
    157         delete_post_meta( $post_id, 'datepicker_start' );
    158     }
    159 
    160     // Convert, sanitize and save end date, if available, otherwise delete post meta.
    161     if ( '' !== $_POST['datepicker_end'] ) {
    162         $end_date = smntcs_date_to_timestamp( sanitize_text_field( wp_unslash( $_POST['datepicker_end'] ) ) );
    163         update_post_meta( $post_id, 'datepicker_end', sanitize_text_field( $end_date ) );
    164     } else {
    165         delete_post_meta( $post_id, 'datepicker_end' );
    166     }
    167 }
    168 add_action( 'save_post', 'smntcs_save_post' );
    169 
    170 /**
    171  * Class SMNTCS_Simple_Events_Widget
    172  *
    173  * @extends WP_Widget
    174  */
    175 class SMNTCS_Simple_Events_Widget extends WP_Widget {
    176     /**
    177      * SMNTCS_Simple_Events_Widget constructor.
    178      */
    179     public function __construct() {
    180         $widget_options = array(
    181             'classname'   => 'smntcs_simple_events_widget',
    182             'description' => 'Display Simple Events Widget',
    183         );
    184         parent::__construct( 'smntcs_simple_events_widget', 'Simple Events Widget', $widget_options );
    185     }
    186 
    187     /**
    188      * Create widget.
    189      *
    190      * @param array $args Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'.
    191      * @param array $instance The settings for the particular instance of the widget.
    192      */
    193     public function widget( $args, $instance ) {
    194 
    195         $title     = apply_filters( 'widget_title', $instance['title'] );
    196         $timestamp = strtotime( gmdate( 'd' ) . ' ' . gmdate( 'F' ) . ' ' . gmdate( 'Y' ) );
    197         $temp      = $args;
    198 
    199         printf(
    200             '%s %s %s %s',
    201             $args['before_widget'], // phpcs:ignore.
    202             $args['before_title'],  // phpcs:ignore.
    203             esc_attr( $title ),
    204             $args['after_title']    // phpcs:ignore.
    205         );
    206 
    207         $args = array(
    208             'post_type' => array( 'post', 'page' ),
    209             'meta_key'  => 'datepicker_start',
    210             'order'     => 'asc',
    211             'orderby'   => 'datepicker_start',
    212         );
    213 
    214         if ( 'upcoming-events' === $instance['display_events'] ) {
    215             $args['meta_value']   = $timestamp;
    216             $args['meta_compare'] = '>';
    217         }
    218 
    219         if ( 'previous-events' === $instance['display_events'] ) {
    220             $args['meta_value']   = $timestamp;
    221             $args['meta_compare'] = '<';
    222         }
    223 
    224         $the_query = new WP_Query( $args );
    225 
    226         if ( $the_query->have_posts() ) {
    227             print( '<ul>' );
    228             foreach ( $the_query->get_posts() as $event ) {
    229                 $start_date = gmdate( get_option( 'date_format' ), get_post_meta( $event->ID, 'datepicker_start', true ) );
    230                 $end_date   = gmdate( get_option( 'date_format' ), get_post_meta( $event->ID, 'datepicker_end', true ) );
    231                 $link       = get_permalink( $event->ID );
    232                 $title      = $event->post_title;
    233 
    234                 if ( 'start-and-end-date' === $instance['display_dates'] ) {
    235                     printf(
    236                         '<li>%s - %s: <a href="%s">%s</a></li>',
    237                         esc_attr( $start_date ),
    238                         esc_attr( $end_date ),
    239                         esc_html( $link ),
    240                         esc_attr( $title )
    241                     );
    242                 } else {
    243                     printf(
    244                         '<li>%s: <a href="%s">%s</a></li>',
    245                         esc_attr( $start_date ),
    246                         esc_html( $link ),
    247                         esc_attr( $title )
    248                     );
    249                 }
    250             }
    251             print( '</ul>' );
    252         } else {
    253             print( '<p>' . esc_html( __( 'No events found.', 'smntcs-simple-events-widget' ) ) . '</p>' );
    254         }
    255 
    256         $args = $temp;
    257 
    258         printf(
    259             '%s',
    260             $args['after_widget'] // phpcs:ignore.
    261         );
    262     }
    263 
    264     /**
    265      * Create form.
    266      *
    267      * @param array $instance The original widget instance.
    268      *
    269      * @return void
    270      */
    271     public function form( $instance ) {
    272         $title = $instance['title'] ?? '';
    273         ?>
    274         <p>
    275             <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'smntcs-simple-events-widget' ); ?></label>
    276             <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $title ); ?>" type="text">
    277         </p>
    278         <p>
    279             <label for="<?php echo esc_attr( $this->get_field_id( 'display_events' ) ); ?>"><?php esc_html_e( 'Display events:', 'smntcs-simple-events-widget' ); ?></label>
    280             <select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'display_events' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'display_events' ) ); ?>">
    281                 <option <?php isset( $instance['display_events'] ) ? selected( $instance['display_events'], 'upcoming-events' ) : ''; ?> value="upcoming-events"><?php esc_html_e( 'Only upcoming events', 'smntcs-simple-events-widget' ); ?></option>
    282                 <option <?php isset( $instance['display_events'] ) ? selected( $instance['display_events'], 'previous-events' ) : ''; ?> value="previous-events"><?php esc_html_e( 'Only previous events', 'smntcs-simple-events-widget' ); ?></option>
    283                 <option <?php isset( $instance['display_events'] ) ? selected( $instance['display_events'], 'upcoming-and-previous-events' ) : ''; ?>value="upcoming-and-previous-events"><?php esc_html_e( 'Upcoming and previous events', 'smntcs-simple-events-widget' ); ?></option>
    284             </select>
    285         </p>
    286         <p>
    287             <label for="<?php echo esc_attr( $this->get_field_id( 'display_dates' ) ); ?>"><?php esc_html_e( 'Display dates:', 'smntcs-simple-events-widget' ); ?></label>
    288             <select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'display_dates' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'display_dates' ) ); ?>">
    289                 <option <?php isset( $instance['display_dates'] ) ? selected( $instance['display_dates'], 'start-date' ) : ''; ?> value="start-date"><?php esc_html_e( 'Only start date', 'smntcs-simple-events-widget' ); ?></option>
    290                 <option <?php isset( $instance['display_dates'] ) ? selected( $instance['display_dates'], 'start-and-end-date' ) : ''; ?>value="start-and-end-date"><?php esc_html_e( 'Start and end date', 'smntcs-simple-events-widget' ); ?></option>
    291             </select>
    292         </p>
    293         <?php
    294     }
    295 
    296     /**
    297      * Update widget.
    298      *
    299      * @param array $new_instance The new array of the widget instance.
    300      * @param array $old_instance The old array of the widget instance.
    301      *
    302      * @return array The updated array of the widget instance.
    303      */
    304     public function update( $new_instance, $old_instance ) {
    305         $instance = $old_instance;
    306 
    307         $instance['title']          = strip_tags( $new_instance['title'] );
    308         $instance['display_events'] = strip_tags( $new_instance['display_events'] );
    309         $instance['display_dates']  = strip_tags( $new_instance['display_dates'] );
    310 
    311         return $instance;
    312     }
    313 }
    314 
    315 /**
    316  * Register widget.
    317  *
    318  * @return void
    319  */
    320 function smntcs_widgets_init() {
    321     register_widget( 'SMNTCS_Simple_Events_Widget' );
    322 }
    323 add_action( 'widgets_init', 'smntcs_widgets_init' );
     23// Include the widget class file.
     24require_once plugin_dir_path( __FILE__ ) . 'includes/class-smntcs-simple-events-widget.php';
  • smntcs-simple-events-widget/trunk/.editorconfig

    r2651293 r2994671  
    1313trim_trailing_whitespace = true
    1414indent_style = tab
    15 indent_size = 4
    1615
    1716[*.yml]
  • smntcs-simple-events-widget/trunk/README.md

    r2985246 r2994671  
    2626
    2727## Changelog
     28
     29
     30
     31
    2832
    2933### 2.0 (2023.10.28)
  • smntcs-simple-events-widget/trunk/README.txt

    r2985246 r2994671  
    33Contributors:       nielslange
    44Tags:               Simple Events, Event, Widget, Sidebar
    5 Stable tag:         2.0
     5Stable tag:         2.
    66Tested up to:       6.4
    77Requires at least:  3.4
     
    3232
    3333== Changelog ==
     34
     35
     36
     37
    3438
    3539= 2.0 (2023.10.28) =
  • smntcs-simple-events-widget/trunk/smntcs-simple-events-widget.php

    r2985246 r2994671  
    77 * Author URI:            https://nielslange.de
    88 * Text Domain:           smntcs-simple-events-widget
    9  * Version:               2.0
     9 * Version:               2.
    1010 * Requires PHP:          5.6
    1111 * Requires at least:     3.4
     
    1818defined( 'ABSPATH' ) || exit;
    1919
    20 /**
    21  * Add settings link on plugin page.
    22  *
    23  * @param  array $links The original array with links.
    24  * @return array The updated array with links.
    25  */
    26 function smntcs_plugin_settings_link( $links ) {
    27     $admin_url    = admin_url( 'widgets.php' );
    28     $settings_url = sprintf( '<a href="%s">%s</a>', $admin_url, __( 'Settings', 'smntcs-simple-events-widget' ) );
    29     array_unshift( $links, $settings_url );
     20// Include the main class file.
     21require_once plugin_dir_path( __FILE__ ) . 'includes/class-smntcs-simple-events.php';
    3022
    31     return $links;
    32 }
    33 add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'smntcs_plugin_settings_link' );
    34 
    35 /**
    36  * Load jQuery datepicker.
    37  *
    38  * @return void
    39  */
    40 function smntcs_admin_enqueue_scripts() {
    41     $plugin_data = get_plugin_data( __FILE__ );
    42     wp_enqueue_script( 'jquery-ui-datepicker' );
    43     wp_enqueue_script( 'smntcs-simple-events-script', plugin_dir_url( __FILE__ ) . '/js/custom.js', array( 'jquery' ), $plugin_data['Version'] );
    44     wp_enqueue_style( 'smntcs-simple-events-styles', plugin_dir_url( __FILE__ ) . '/js/jquery-ui.css', array(), $plugin_data['Version'] );
    45 }
    46 add_action( 'admin_enqueue_scripts', 'smntcs_admin_enqueue_scripts' );
    47 
    48 /**
    49  * Register meta box.
    50  *
    51  * @return void
    52  */
    53 function smntcs_add_meta_boxes() {
    54     add_meta_box( 'meta-box-id', __( 'Event', 'smntcs-simple-events-widget' ), 'smntcs_display_callback', 'post', 'side' );
    55     add_meta_box( 'meta-box-id', __( 'Event', 'smntcs-simple-events-widget' ), 'smntcs_display_callback', 'page', 'side' );
    56 }
    57 add_action( 'add_meta_boxes', 'smntcs_add_meta_boxes' );
    58 
    59 /**
    60  * Convert date to timestamp.
    61  *
    62  * @param  string $date The date to convert, e.g. 01-01-2017.
    63  * @return int The converted timestamp, e.g. 1483228800.
    64  */
    65 function smntcs_date_to_timestamp( $date ) {
    66     return DateTime::createFromFormat( 'd-m-Y', $date, new DateTimeZone( 'UTC' ) )->getTimestamp();
    67 }
    68 
    69 /**
    70  * Convert timestamp to date.
    71  *
    72  * @param string $timestamp The timestamp to convert, e.g. 1483228800.
    73  * @return string The converted date, e.g. 01-01-2017.
    74  */
    75 function smntcs_timestamp_to_date( $timestamp ) {
    76     return gmdate( 'd-m-Y', $timestamp );
    77 }
    78 
    79 /**
    80  * Meta box display callback.
    81  *
    82  * @param  WP_Post $post The original post object.
    83  * @return void
    84  */
    85 function smntcs_display_callback( $post ) {
    86 
    87     $start_date             = get_post_meta( $post->ID, 'datepicker_start', true );
    88     $start_date_value       = ! empty( $start_date ) ? smntcs_timestamp_to_date( $start_date ) : null;
    89     $start_date_placeholder = ! empty( $start_date ) ? smntcs_timestamp_to_date( $start_date ) : 'dd-mm-yyyy';
    90     $end_date               = get_post_meta( $post->ID, 'datepicker_end', true );
    91     $end_date_value         = ! empty( $end_date ) ? smntcs_timestamp_to_date( $end_date ) : null;
    92     $end_date_placeholder   = ! empty( $end_date ) ? smntcs_timestamp_to_date( $end_date ) : 'dd-mm-yyyy';
    93     wp_nonce_field( 'smntcs_add_simple_event', 'smntcs_wpnonce' );
    94     ?>
    95     <table>
    96         <tr class="wrap">
    97             <td><?php echo esc_html_e( 'Start date', 'smntcs-simple-events-widget' ); ?></td>
    98             <td><input type="text" class="datepicker" name="datepicker_start" value="<?php echo esc_html( $start_date_value ); ?>" placeholder="<?php echo esc_html( $start_date_placeholder ); ?>"></td>
    99         </tr>
    100         <tr class="wrap">
    101             <td><?php echo esc_html_e( 'End date', 'smntcs-simple-events-widget' ); ?></td>
    102             <td><input type="text" class="datepicker" name="datepicker_end" value="<?php echo esc_html( $end_date_value ); ?>" placeholder="<?php echo esc_html( $end_date_placeholder ); ?>"></td>
    103         </tr>
    104     </table>
    105     <?php
    106 }
    107 
    108 /**
    109  * Save meta box content.
    110  *
    111  * @param int $post_id The ID of the post to save.
    112  * @return void
    113  */
    114 function smntcs_save_post( $post_id ) {
    115 
    116     // Return if user does not have necessary permissions.
    117     if ( ! current_user_can( 'edit_post', $post_id ) ) {
    118         return;
    119     }
    120 
    121     // Return if user is not on page or post.
    122     if ( ! isset( $_POST['post_type'] ) || ( 'page' !== $_POST['post_type'] && 'post' !== $_POST['post_type'] ) ) {
    123         return;
    124     }
    125 
    126     // Return if nonce is incorrect or not available.
    127     if ( ! isset( $_POST['smntcs_wpnonce'] ) &&
    128          ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['smntcs_wpnonce'] ) ), 'smntcs_add_simple_event' ) ) {
    129         return;
    130     }
    131 
    132     // Return if start date and end date are not available.
    133     if ( ! isset( $_POST['datepicker_start'] ) && ! isset( $_POST['datepicker_end'] ) ) {
    134         return;
    135     }
    136 
    137     // Return if only end date is available.
    138     if ( isset( $_POST['datepicker_start'] ) && ! isset( $_POST['datepicker_end'] ) ) {
    139         return;
    140     }
    141 
    142     // Return if start date is bigger then end date.
    143     if ( '' !== $_POST['datepicker_start'] && '' !== $_POST['datepicker_end'] ) {
    144         $start_date = smntcs_date_to_timestamp( sanitize_text_field( wp_unslash( $_POST['datepicker_start'] ) ) );
    145         $end_date   = smntcs_date_to_timestamp( sanitize_text_field( wp_unslash( $_POST['datepicker_end'] ) ) );
    146 
    147         if ( $start_date > $end_date ) {
    148             return;
    149         }
    150     }
    151 
    152     // Convert, sanitize and save start date, if available, otherwise delete post meta.
    153     if ( '' !== $_POST['datepicker_start'] ) {
    154         $start_date = smntcs_date_to_timestamp( sanitize_text_field( wp_unslash( $_POST['datepicker_start'] ) ) );
    155         update_post_meta( $post_id, 'datepicker_start', sanitize_text_field( $start_date ) );
    156     } else {
    157         delete_post_meta( $post_id, 'datepicker_start' );
    158     }
    159 
    160     // Convert, sanitize and save end date, if available, otherwise delete post meta.
    161     if ( '' !== $_POST['datepicker_end'] ) {
    162         $end_date = smntcs_date_to_timestamp( sanitize_text_field( wp_unslash( $_POST['datepicker_end'] ) ) );
    163         update_post_meta( $post_id, 'datepicker_end', sanitize_text_field( $end_date ) );
    164     } else {
    165         delete_post_meta( $post_id, 'datepicker_end' );
    166     }
    167 }
    168 add_action( 'save_post', 'smntcs_save_post' );
    169 
    170 /**
    171  * Class SMNTCS_Simple_Events_Widget
    172  *
    173  * @extends WP_Widget
    174  */
    175 class SMNTCS_Simple_Events_Widget extends WP_Widget {
    176     /**
    177      * SMNTCS_Simple_Events_Widget constructor.
    178      */
    179     public function __construct() {
    180         $widget_options = array(
    181             'classname'   => 'smntcs_simple_events_widget',
    182             'description' => 'Display Simple Events Widget',
    183         );
    184         parent::__construct( 'smntcs_simple_events_widget', 'Simple Events Widget', $widget_options );
    185     }
    186 
    187     /**
    188      * Create widget.
    189      *
    190      * @param array $args Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'.
    191      * @param array $instance The settings for the particular instance of the widget.
    192      */
    193     public function widget( $args, $instance ) {
    194 
    195         $title     = apply_filters( 'widget_title', $instance['title'] );
    196         $timestamp = strtotime( gmdate( 'd' ) . ' ' . gmdate( 'F' ) . ' ' . gmdate( 'Y' ) );
    197         $temp      = $args;
    198 
    199         printf(
    200             '%s %s %s %s',
    201             $args['before_widget'], // phpcs:ignore.
    202             $args['before_title'],  // phpcs:ignore.
    203             esc_attr( $title ),
    204             $args['after_title']    // phpcs:ignore.
    205         );
    206 
    207         $args = array(
    208             'post_type' => array( 'post', 'page' ),
    209             'meta_key'  => 'datepicker_start',
    210             'order'     => 'asc',
    211             'orderby'   => 'datepicker_start',
    212         );
    213 
    214         if ( 'upcoming-events' === $instance['display_events'] ) {
    215             $args['meta_value']   = $timestamp;
    216             $args['meta_compare'] = '>';
    217         }
    218 
    219         if ( 'previous-events' === $instance['display_events'] ) {
    220             $args['meta_value']   = $timestamp;
    221             $args['meta_compare'] = '<';
    222         }
    223 
    224         $the_query = new WP_Query( $args );
    225 
    226         if ( $the_query->have_posts() ) {
    227             print( '<ul>' );
    228             foreach ( $the_query->get_posts() as $event ) {
    229                 $start_date = gmdate( get_option( 'date_format' ), get_post_meta( $event->ID, 'datepicker_start', true ) );
    230                 $end_date   = gmdate( get_option( 'date_format' ), get_post_meta( $event->ID, 'datepicker_end', true ) );
    231                 $link       = get_permalink( $event->ID );
    232                 $title      = $event->post_title;
    233 
    234                 if ( 'start-and-end-date' === $instance['display_dates'] ) {
    235                     printf(
    236                         '<li>%s - %s: <a href="%s">%s</a></li>',
    237                         esc_attr( $start_date ),
    238                         esc_attr( $end_date ),
    239                         esc_html( $link ),
    240                         esc_attr( $title )
    241                     );
    242                 } else {
    243                     printf(
    244                         '<li>%s: <a href="%s">%s</a></li>',
    245                         esc_attr( $start_date ),
    246                         esc_html( $link ),
    247                         esc_attr( $title )
    248                     );
    249                 }
    250             }
    251             print( '</ul>' );
    252         } else {
    253             print( '<p>' . esc_html( __( 'No events found.', 'smntcs-simple-events-widget' ) ) . '</p>' );
    254         }
    255 
    256         $args = $temp;
    257 
    258         printf(
    259             '%s',
    260             $args['after_widget'] // phpcs:ignore.
    261         );
    262     }
    263 
    264     /**
    265      * Create form.
    266      *
    267      * @param array $instance The original widget instance.
    268      *
    269      * @return void
    270      */
    271     public function form( $instance ) {
    272         $title = $instance['title'] ?? '';
    273         ?>
    274         <p>
    275             <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'smntcs-simple-events-widget' ); ?></label>
    276             <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $title ); ?>" type="text">
    277         </p>
    278         <p>
    279             <label for="<?php echo esc_attr( $this->get_field_id( 'display_events' ) ); ?>"><?php esc_html_e( 'Display events:', 'smntcs-simple-events-widget' ); ?></label>
    280             <select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'display_events' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'display_events' ) ); ?>">
    281                 <option <?php isset( $instance['display_events'] ) ? selected( $instance['display_events'], 'upcoming-events' ) : ''; ?> value="upcoming-events"><?php esc_html_e( 'Only upcoming events', 'smntcs-simple-events-widget' ); ?></option>
    282                 <option <?php isset( $instance['display_events'] ) ? selected( $instance['display_events'], 'previous-events' ) : ''; ?> value="previous-events"><?php esc_html_e( 'Only previous events', 'smntcs-simple-events-widget' ); ?></option>
    283                 <option <?php isset( $instance['display_events'] ) ? selected( $instance['display_events'], 'upcoming-and-previous-events' ) : ''; ?>value="upcoming-and-previous-events"><?php esc_html_e( 'Upcoming and previous events', 'smntcs-simple-events-widget' ); ?></option>
    284             </select>
    285         </p>
    286         <p>
    287             <label for="<?php echo esc_attr( $this->get_field_id( 'display_dates' ) ); ?>"><?php esc_html_e( 'Display dates:', 'smntcs-simple-events-widget' ); ?></label>
    288             <select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'display_dates' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'display_dates' ) ); ?>">
    289                 <option <?php isset( $instance['display_dates'] ) ? selected( $instance['display_dates'], 'start-date' ) : ''; ?> value="start-date"><?php esc_html_e( 'Only start date', 'smntcs-simple-events-widget' ); ?></option>
    290                 <option <?php isset( $instance['display_dates'] ) ? selected( $instance['display_dates'], 'start-and-end-date' ) : ''; ?>value="start-and-end-date"><?php esc_html_e( 'Start and end date', 'smntcs-simple-events-widget' ); ?></option>
    291             </select>
    292         </p>
    293         <?php
    294     }
    295 
    296     /**
    297      * Update widget.
    298      *
    299      * @param array $new_instance The new array of the widget instance.
    300      * @param array $old_instance The old array of the widget instance.
    301      *
    302      * @return array The updated array of the widget instance.
    303      */
    304     public function update( $new_instance, $old_instance ) {
    305         $instance = $old_instance;
    306 
    307         $instance['title']          = strip_tags( $new_instance['title'] );
    308         $instance['display_events'] = strip_tags( $new_instance['display_events'] );
    309         $instance['display_dates']  = strip_tags( $new_instance['display_dates'] );
    310 
    311         return $instance;
    312     }
    313 }
    314 
    315 /**
    316  * Register widget.
    317  *
    318  * @return void
    319  */
    320 function smntcs_widgets_init() {
    321     register_widget( 'SMNTCS_Simple_Events_Widget' );
    322 }
    323 add_action( 'widgets_init', 'smntcs_widgets_init' );
     23// Include the widget class file.
     24require_once plugin_dir_path( __FILE__ ) . 'includes/class-smntcs-simple-events-widget.php';
Note: See TracChangeset for help on using the changeset viewer.