+<?php\r
+/*\r
+Plugin Name: danixland CountDown\r
+Plugin URI: http://danixland.net/?p=3330 \r
+Description: A simple plugin that shows a widget with a countdown\r
+Version: 0.4\r
+Author: Danilo 'danix' Macri\r
+Author URI: http://danixland.net\r
+Text Domain: dnxcd\r
+\r
+This program is distributed in the hope that it will be useful,\r
+but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\r
+*/\r
+\r
+\r
+/**\r
+ * Add plugin i18n domain: dnxcd\r
+ * @since 0.1\r
+ */\r
+load_plugin_textdomain('dnxcd', plugins_url() . '/danixland-countdown/i18n/', 'danixland-countdown/i18n/');\r
+\r
+/**\r
+ * Function that installs our widget options\r
+ * @since 0.1\r
+ */\r
+function dnxcd_options_set() {\r
+ add_option( 'dnxcd_future_date', '06-05-2020', '', 'yes' );\r
+ add_option( 'dnxcd_widget_link', '', '', 'yes' );\r
+ add_option( 'dnxcd_use_style', 1, '', 'yes' );\r
+}\r
+\r
+/**\r
+ * Function that deletes our widget options\r
+ * @since 0.3\r
+ */\r
+function dnxcd_options_unset() {\r
+ delete_option( 'dnxcd_future_date' );\r
+ delete_option( 'dnxcd_widget_link' );\r
+ delete_option( 'dnxcd_use_style' );\r
+}\r
+\r
+/**\r
+ * Use widget's stylesheet if user wants it\r
+ * @since 0.3\r
+ */\r
+function dnxcd_style_handle() {\r
+ wp_register_style('dnxcd_basic', plugins_url() . '/danixland-countdown/inc/style/basic.css');\r
+ if( false == get_option('dnxcd_use_style') )\r
+ return;\r
+ wp_enqueue_style('dnxcd_basic');\r
+}\r
+add_action( 'wp_enqueue_scripts', 'dnxcd_style_handle' );\r
+\r
+/**\r
+ * Add function on plugin activation that'll set our plugin options\r
+ * @since 0.1\r
+ */\r
+register_activation_hook( __FILE__, 'dnxcd_options_set' );\r
+\r
+/**\r
+ * Add function on plugin deactivation that'll unset our plugin options\r
+ * @since 0.3\r
+ */\r
+register_deactivation_hook( __FILE__, 'dnxcd_options_unset' );\r
+\r
+/**\r
+ * Add function to widgets_init that'll load our widget.\r
+ * @since 0.1\r
+ */\r
+add_action( 'widgets_init', 'danixland_countdown' );\r
+\r
+/**\r
+ * Register our widget.\r
+ * 'dnx_CountDown' is the widget class used below.\r
+ *\r
+ * @since 0.1\r
+ */\r
+function danixland_countdown() {\r
+ register_widget( 'dnx_CountDown' );\r
+}\r
+\r
+/**\r
+ * dnx_CountDown class.\r
+ * This class handles everything that needs to be handled with the widget:\r
+ * the settings, form, display, and update. Nice!\r
+ *\r
+ * @since 0.1\r
+ */\r
+class dnx_CountDown extends WP_Widget {\r
+\r
+\r
+ /**\r
+ * Widget setup.\r
+ */\r
+ public function __construct() {\r
+ $control_ops = array('width' => 400, 'height' => 350);\r
+ parent::__construct(\r
+ 'dnx-countdown', // id_base\r
+ __('danixland CountDown', 'dnxcd' ), // Name\r
+ array( 'description' => __('Use this widget to add a simple Count Down to your Sidebar', 'dnxcd') ),\r
+ $control_ops\r
+ );\r
+ }\r
+\r
+ /**\r
+ * How to display the widget on the public side of the site.\r
+ */\r
+ public function widget( $args, $instance ) {\r
+ extract( $args );\r
+\r
+ /* Our variables from the widget settings. */\r
+ $title = apply_filters('widget_title', $instance['title'] );\r
+\r
+ /* Before widget (defined by themes). */\r
+ echo $before_widget;\r
+\r
+ /* Display the widget title if one was input (before and after defined by themes). */\r
+ if ( $title ) {\r
+ echo $before_title . $title . $after_title;\r
+ }\r
+\r
+ if ( '' != get_option('dnxcd_future_date') ) {\r
+ echo '<div class="dnx-countdown">';\r
+ $date = gmdate('U', strtotime( get_option('dnxcd_future_date') ));\r
+ $diff = $date - gmdate('U');\r
+ if ( '' != get_option('dnxcd_widget_link') ) {\r
+ echo '<a href="' . get_option('dnxcd_widget_link') . '">';\r
+ echo '-' . floor($diff / (24 * 60 * 60));\r
+ echo '</a>';\r
+ } else {\r
+ echo '-' . floor($diff / (24 * 60 * 60));\r
+ }\r
+ echo '</div> <!-- #dnx-countdown -->';\r
+ echo '<div class="dnx-days">' . __('days to go', 'dnxcd') . '</div>'; \r
+ } else {\r
+ echo "<div class='dnx-warning'><a href='" . get_admin_url('', 'widgets.php', '') . "' title='" . __('configure widget', 'dnxcd') . "'>" . __('the date is missing<br />configure widget', 'dnxcd') . "</a></div>";\r
+ }\r
+\r
+ /* After widget (defined by themes). */\r
+ echo $after_widget;\r
+ }\r
+\r
+ //Update the widget \r
+ \r
+ public function update( $new_instance, $old_instance ) {\r
+ $instance = $old_instance;\r
+\r
+ //Strip tags from title and name to remove HTML \r
+ $instance['title'] = sanitize_text_field( $new_instance['title'] );\r
+ $instance['date'] = strip_tags( $new_instance['date'] );\r
+ $instance['link'] = esc_url( $new_instance['link'], array('http', 'https') );\r
+ $instance['style'] = (bool) $new_instance['style'];\r
+\r
+ update_option( 'dnxcd_future_date', $instance['date'] );\r
+ update_option( 'dnxcd_widget_link', $instance['link'] );\r
+ update_option( 'dnxcd_use_style', $instance['style'] );\r
+ \r
+ return $instance;\r
+ }\r
+\r
+ /**\r
+ * Displays just a quick notice with a link to the Settings page\r
+ */\r
+ public function form( $instance ) {\r
+ $defaults = array( \r
+ 'title' => __( 'Count Down', 'dnxcd' ),\r
+ 'date' => get_option( 'dnxcd_future_date' ),\r
+ 'link' => get_option( 'dnxcd_widget_link' ),\r
+ 'style' => get_option( 'dnxcd_use_style' )\r
+ );\r
+ $instance = wp_parse_args( (array) $instance, $defaults );\r
+ extract( $instance, EXTR_SKIP );\r
+ ?>\r
+\r
+ <p>\r
+ <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'dnxcd'); ?></label>\r
+ <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $title; ?>" style="width:100%;" />\r
+ </p>\r
+ <p>\r
+ <label for="<?php echo $this->get_field_id( 'date' ); ?>"><?php _e('Future Date (dd-mm-yyyy):', 'dnxcd'); ?></label>\r
+ <input type="date" class="widefat" id="<?php echo $this->get_field_id( 'date' ); ?>" name="<?php echo $this->get_field_name( 'date' ); ?>" value="<?php echo $date ?>" style="width:100%;" />\r
+ <label for="<?php echo $this->get_field_id( 'link' ); ?>"><?php _e('Link (only http/https allowed):', 'dnxcd'); ?></label>\r
+ <input class="widefat" id="<?php echo $this->get_field_id( 'link' ); ?>" name="<?php echo $this->get_field_name( 'link' ); ?>" value="<?php echo $link; ?>" style="width:100%;" />\r
+ </p>\r
+ <p>\r
+ <input class="checkbox" type="checkbox" <?php checked( $style, true ); ?> id="<?php echo $this->get_field_id( 'style' ); ?>" name="<?php echo $this->get_field_name( 'style' ); ?>" />\r
+ <label for="<?php echo $this->get_field_id( 'style' ); ?>"><?php _e('Enable Widget Stylesheet?', 'dnxcd'); ?></label>\r
+ </p>\r
+ <?php\r
+ }\r
+}\r
+\r
+?>\r