Advertisement
helgatheviki

Countdown Widget modified with layout

Oct 19th, 2013
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 17.37 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Countdown Widget
  4. Plugin URI: http://shailan.com/wordpress/plugins/countdown
  5. Description: A beautiful jquery countdown widget. Allows Multiple instances, Shortcode usage, and Customizations. Powered by: <a href="http://shailan.com" title="Wordpress, Web design, Freelancing">shailan.com</a>.
  6. Version: 2.5.4
  7. Author: Matt Say
  8. Author URI: http://shailan.com
  9. */
  10.  
  11. global $countdown_shortcode_ids;
  12.  
  13. /**
  14.  * Shailan Countdown Widget Class
  15.  */
  16. class shailan_CountdownWidget extends WP_Widget {
  17.     /** constructor */
  18.     function shailan_CountdownWidget() {
  19.         $widget_ops = array('classname' => 'shailan_CountdownWidget', 'description' => __( 'jQuery Countdown widget' ) );
  20.         $this->WP_Widget('shailan-countdown-widget', __('Countdown'), $widget_ops);
  21.         $this->alt_option_name = 'widget_shailan_countdown';
  22.  
  23.         // localization
  24.         $lang = substr( get_bloginfo('language'), 0, 2 );
  25.  
  26.         wp_enqueue_script('jquery');
  27.         wp_enqueue_script('countdown', plugins_url('js/jquery.countdown.min.js', __FILE__), 'jquery', '1.0', false);
  28.         if($lang!='en' && file_exists( plugin_dir_path(__FILE__) . 'js/jquery.countdown-' . $lang . '.js') ){
  29.             wp_enqueue_script('countdown-l10n', plugins_url('js/jquery.countdown-' . $lang . '.js',__FILE__), 'countdown', '1.0', false);
  30.         }
  31.         wp_enqueue_style('countdown-style', plugins_url('css/jquery.countdown.css', __FILE__), '', '1.1', false);
  32.  
  33.         add_action( 'wp_head', array(&$this, 'header'), 10, 1 );
  34.         //add_action( 'wp_footer', array(&$this, 'footer'), 10, 1 );
  35.  
  36.         $current_offset = get_option('gmt_offset');
  37.  
  38.         $this->defaults = array(
  39.             'title'     =>'',
  40.             'event'     => '',
  41.             'month'     => date( 'm' ),
  42.             'day'       => date( 'd' ),
  43.             'hour'      => ( gmdate( 'H' ) + $current_offset + 1 ),
  44.             'minutes'   => date( 'i' ),
  45.             'seconds'   => date( 's' ),
  46.             'year'      => date( 'Y' ),
  47.             'format'    => 'yowdHMS',
  48.             'layout' => '',
  49.             'color'     => '000000',
  50.             'bgcolor'   => '',
  51.             'width'     => '',
  52.             'link'      => false,
  53.             'timezone'  => $current_offset,
  54.             'eventurl'  => '',
  55.             'direction' => 'Down',
  56.             'height'    => '',
  57.             'border-radius' => ''
  58.         );
  59.  
  60.         $this->timezones = array(
  61.             "SCW_NONE" => "None",
  62.             "-12" => "-12",
  63.             "-11" => "-11",
  64.             "-10" => "-10",
  65.             "-9" => "-9",
  66.             "-8" => "-8",
  67.             "-7" => "-7",
  68.             "-6" => "-6",
  69.             "-5" => "-5",
  70.             "-4" => "-4",
  71.             "-3.5" => "-3",
  72.             "-3" => "-3",
  73.             "-2" => "-2",
  74.             "-1" => "-1",
  75.             "0" => "GMT",
  76.             "+1" => "+1",
  77.             "+2" => "+2",
  78.             "+3" => "+3",
  79.             "+3.5" => "+3:30",
  80.             "+4" => "+4",
  81.             "+4.5" => "+4:30",
  82.             "+5" => "+5",
  83.             "+5.5" => "+5:30",
  84.             "+6" => "+6",
  85.             "+7" => "+7",
  86.             "+8" => "+8",
  87.             "+9" => "+9",
  88.             "+9.5" => "+9:30",
  89.             "+10" => "+10",
  90.             "+11" => "+11",
  91.             "+12" => "+12"
  92.         );
  93.  
  94.     }
  95.  
  96.     /** @see WP_Widget::widget */
  97.     function widget($args, $instance) {
  98.         global $post, $countdown_shortcode_ids;
  99.  
  100.         extract( $args );
  101.  
  102.         $widget_options = wp_parse_args( $instance, $this->defaults );
  103.         extract( $widget_options, EXTR_SKIP );
  104.  
  105.         // Get a new id
  106.         $countdown_shortcode_ids++;
  107.  
  108.         if(!empty($instance['link'])){ $link = (bool) $link; }
  109.         // $height = 80*$width/250;
  110.  
  111.         $path = get_plugin_path(__FILE__);
  112.  
  113.         $style = "";
  114.  
  115.         // If this is not a widget
  116.         if(isset($isWidget) && false === $isWidget){
  117.             $style=" style=\"";
  118.  
  119.             // Fix HEX color codes
  120.             if( (preg_match('/^#[a-f0-9]{6}$/i', $color) != 1 && preg_match('/^[a-f0-9]{6}$/i', $color) ) || (preg_match('/^#[a-f0-9]{3}$/i', $color) != 1 && preg_match('/^[a-f0-9]{3}$/i', $color) ) ){
  121.                 $color = "#" . $color;
  122.             }
  123.             if( (preg_match('/^#[a-f0-9]{6}$/i', $bgcolor) != 1 && preg_match('/^[a-f0-9]{6}$/i', $bgcolor) ) || (preg_match('/^#[a-f0-9]{3}$/i', $bgcolor) != 1 && preg_match('/^[a-f0-9]{3}$/i', $bgcolor)) ){
  124.                 $bgcolor = "#" . $bgcolor;
  125.             }
  126.  
  127.             if(!empty($bgcolor)){
  128.                 $style .= "background-color:".$bgcolor.";";
  129.             }
  130.             if(!empty($color)){ $style .=  " color:".$color. ";"; }
  131.             if(!empty($width) && $width>0){ $style .= " width:".$width."px;"; }
  132.             if(!empty($radius) && $radius>0){ $style .= " border-radius:".$radius."px;"; }
  133.             // if(!empty($height) && $height>0){ $style .= " height:".$height."px;"; } // Gotta work on this
  134.                 $style .= " margin:0px auto; \"";
  135.  
  136.         }
  137.  
  138.             ?>
  139.                   <?php echo $before_widget; ?>
  140.                     <?php if ( $title )
  141.                             echo $before_title . $title . $after_title;
  142.                     ?>
  143.  
  144.                 <div id="shailan-countdown-<?php echo $this->number . "_" . $countdown_shortcode_ids; ?>" class="shailan-countdown-<?php echo $this->number ?> countdown" <?php echo $style; ?>></div>
  145.  
  146.                 <?php
  147.                 if(!$link){echo '<div '.$style.'><small><a href="http://shailan.com/wordpress/plugins/countdown" title="Get your own counter widget!" style="float:right;">&uarr; Get this</a></small></div>';};
  148.                 ?>
  149.  
  150. <script type="text/javascript">
  151. <!--//
  152. // Dom Ready
  153.     jQuery(document).ready(function($) {
  154.         var event_month = <?php echo $month; ?> - 1;
  155.         desc = '<?php echo $event; ?>';
  156.         eventDate = new Date(<?php echo $year; ?>, event_month, <?php echo $day; ?>, <?php echo $hour; ?>, <?php echo $minutes; ?>, <?php echo $seconds; ?>, 0);
  157.         $('#shailan-countdown-<?php echo $this->number . "_" . $countdown_shortcode_ids; ?>').countdown({
  158.             <?php if($direction == 'down'){ ?>until<?php } else { ?>since<?php } ?>: eventDate,
  159.             description: desc,
  160.             layout: <?php echo json_encode ( $layout ); ?>,
  161.             format: '<?php echo $format; ?>'<?php if($timezone != 'SCW_NONE'){ ?>,
  162.             timezone: <?php echo $timezone; } ?>
  163.         });
  164.     });
  165. //-->
  166. </script>
  167.                   <?php echo $after_widget; ?>
  168.             <?php
  169.  
  170.     }
  171.  
  172.     function update($new_instance, $old_instance) {
  173.         // Check month
  174.         if( $new_instance['month'] < 1 ) { $new_instance['month'] = '1'; }
  175.         if( $new_instance['month'] > 12 ) { $new_instance['month'] = '12'; }
  176.         if( $new_instance['day'] < 1 ) { $new_instance['day'] = '1'; }
  177.         if( $new_instance['timezone'] == '' ){ $new_instance['timezone'] = 'SCW_NONE'; }
  178.  
  179.         return $new_instance;
  180.     }
  181.  
  182.     function form($instance) {
  183.         global $post, $countdown_shortcode_ids;
  184.         $widget_options = wp_parse_args( $instance, $this->defaults );
  185.         extract( $widget_options, EXTR_SKIP );
  186.  
  187.         $event = esc_attr($event);
  188.  
  189.         if(!empty($instance['link'])){ $link = (bool) $link; }
  190.         $height = 80*$width/250;
  191.  
  192.         $countdown_shortcode_ids++;
  193.  
  194.         ?>
  195.         <p><small>Preview:</small></p>
  196.         <div id="countdown-preview">
  197.             <div id="shailan-countdown-<?php echo $this->number . "_" . $countdown_shortcode_ids; ?>" class="shailan-countdown-<?php echo $this->number . "_" . $countdown_shortcode_ids; ?> countdown"></div>
  198.             <script type="text/javascript">
  199.             <!--//
  200.             // Dom Ready
  201.                 jQuery(document).ready(function($) {
  202.                     var event_month = <?php echo $month; ?> - 1;
  203.                     desc = '<?php echo $event; ?>';
  204.                     eventDate = new Date(<?php echo $year; ?>, event_month, <?php echo $day; ?>, <?php echo $hour; ?>, <?php echo $minutes; ?>, <?php echo $seconds; ?>, 0);
  205.                     $('#shailan-countdown-<?php echo $this->number . "_" . $countdown_shortcode_ids; ?>').countdown({
  206.                         <?php if($direction == 'down'){ ?>until<?php } else { ?>since<?php } ?>: eventDate,
  207.                         description: desc,
  208.                         layout: <?php echo json_encode ( $layout ); ?>,
  209.                         format: '<?php echo $format; ?>'<?php if($timezone != 'SCW_NONE'){ ?>,
  210.                         timezone: <?php echo $timezone; } ?>
  211.                     });
  212.  
  213.                     <?php if( $color != '' ){ ?>
  214.                     $('#shailan-countdown-<?php echo $this->number . "_" . $countdown_shortcode_ids; ?>').css('color', '<?php echo $color; ?>');
  215.                     <?php } ?>
  216.  
  217.                     <?php if( $bgcolor != '' ){ ?>
  218.                     $('#shailan-countdown-<?php echo $this->number . "_" . $countdown_shortcode_ids; ?>').css('backgroundColor', '<?php echo $bgcolor; ?>');
  219.                     <?php } ?>
  220.  
  221.                 });
  222.             //-->
  223.             </script>
  224.         </div>
  225.  
  226.         <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <small><a href="http://shailan.com/wordpress/plugins/countdown/help/#title" target="_blank" rel="external">(?)</a></small> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label></p>
  227.  
  228.         <p><label for="<?php echo $this->get_field_id('event'); ?>"><?php _e('Event Title:'); ?> <small><a href="http://shailan.com/wordpress/plugins/countdown/help/#event-title" target="_blank" rel="external">(?)</a></small> <input class="widefat" id="<?php echo $this->get_field_id('event'); ?>" name="<?php echo $this->get_field_name('event'); ?>" type="text" value="<?php echo $event; ?>" /></label></p>
  229.  
  230.         <p><label for="<?php echo $this->get_field_id('direction'); ?>"> <?php _e('Count Down/Up :'); ?></label>
  231.             <select name="<?php echo $this->get_field_name('direction'); ?>" id="<?php echo $this->get_field_id('direction'); ?>" >
  232.                 <option value="down" <?php if($direction == "down") { ?> selected="selected" <?php } ?>>Down</option>
  233.                 <option value="up" <?php if($direction == "up") { ?> selected="selected" <?php } ?>>Up</option>
  234.             </select> <a href="http://shailan.com/wordpress/plugins/countdown/help/#direction">(?)</a>
  235.         </p>
  236.  
  237.         <p><label for="<?php echo $this->get_field_id('month'); ?>"><?php _e('Date:'); ?></label><input id="<?php echo $this->get_field_id('month'); ?>" name="<?php echo $this->get_field_name('month'); ?>" type="text" value="<?php echo $month; ?>" size="2" maxlength="2" />/<input id="<?php echo $this->get_field_id('day'); ?>" name="<?php echo $this->get_field_name('day'); ?>" type="text" value="<?php echo $day; ?>" size="2" maxlength="2" />/<input id="<?php echo $this->get_field_id('year'); ?>" name="<?php echo $this->get_field_name('year'); ?>" type="text" value="<?php echo $year; ?>" size="4" maxlength="4" /><br />
  238.         <small>MM DD YYYY</small> <small><a href="http://shailan.com/wordpress/plugins/countdown/help/#date" target="_blank" rel="external">(?)</a></small></p>
  239.  
  240.         <p><label for="<?php echo $this->get_field_id('hour'); ?>"><?php _e('Time:'); ?></label><input id="<?php echo $this->get_field_id('hour'); ?>" name="<?php echo $this->get_field_name('hour'); ?>" type="text" value="<?php echo $hour; ?>" size="2" maxlength="2" />:<input id="<?php echo $this->get_field_id('minutes'); ?>" name="<?php echo $this->get_field_name('minutes'); ?>" type="text" value="<?php echo $minutes; ?>" size="2" maxlength="2" />:<input id="<?php echo $this->get_field_id('seconds'); ?>" name="<?php echo $this->get_field_name('seconds'); ?>" type="text" value="<?php echo $seconds; ?>" size="4" maxlength="4" /><br />
  241.         <small>HH:MM:SS</small> <small><a href="http://shailan.com/wordpress/plugins/countdown/help/#time" target="_blank" rel="external">(?)</a></small></p>
  242.  
  243.         <p><label for="<?php echo $this->get_field_id('layout'); ?>"><?php _e('Layout:'); ?> <textarea id="<?php echo $this->get_field_id('layout'); ?>" name="<?php echo $this->get_field_name('layout'); ?>" type="textarea"?><?php echo $layout; ?></textarea></label><small><a href="http://shailan.com/wordpress/plugins/countdown/help/#layout" target="_blank" rel="external">(?)</a></small> </p>
  244.  
  245.         <p><label for="<?php echo $this->get_field_id('format'); ?>"><?php _e('Format:'); ?> <input id="<?php echo $this->get_field_id('format'); ?>" name="<?php echo $this->get_field_name('format'); ?>" type="text" value="<?php echo $format; ?>" size="10" maxlength="8" /></label><br />
  246.         <small>(Default : HMS)</small> <small><a href="http://shailan.com/wordpress/plugins/countdown/help/#format" target="_blank" rel="external">(?)</a></small> </p>
  247.  
  248.         <p><label for="<?php echo $this->get_field_id('timezone'); ?>"><?php _e('Timezone :'); ?>
  249.             <select name="<?php echo $this->get_field_name('timezone'); ?>" id="<?php echo $this->get_field_id('timezone'); ?>" >
  250.             <?php
  251.                 foreach ( $this->timezones as $key=>$name ) {
  252.                     $option = '<option value="'. $key .'" '. ( '_'.$timezone == '_'.$key ? ' selected="selected"' : '' ) .'>';
  253.                     $option .= $name;
  254.                     $option .= '</option>\n';
  255.                     echo $option;
  256.                 }
  257.             ?>
  258.             </select></label> <a href="http://shailan.com/wordpress/plugins/countdown/help/#timezone">(?)</a></p>
  259.  
  260.         <p><label for="<?php echo $this->get_field_id('color'); ?>"><?php _e('Color:'); ?> <input id="<?php echo $this->get_field_id('color'); ?>" name="<?php echo $this->get_field_name('color'); ?>" type="text" value="<?php echo $color; ?>" size="6" /></label> <small><a href="http://shailan.com/wordpress/plugins/countdown/help/#color" target="_blank" rel="external">(?)</a></small></p>
  261.         <p><label for="<?php echo $this->get_field_id('bgcolor'); ?>"><?php _e('Background color:'); ?> <input id="<?php echo $this->get_field_id('bgcolor'); ?>" name="<?php echo $this->get_field_name('bgcolor'); ?>" type="text" value="<?php echo $bgcolor; ?>" size="6" /></label> <small><a href="http://shailan.com/wordpress/plugins/countdown/help/#background-color" target="_blank" rel="external">(?)</a></small></p>
  262.         <p><label for="<?php echo $this->get_field_id('width'); ?>"><?php _e('Width:'); ?> <input id="<?php echo $this->get_field_id('width'); ?>" name="<?php echo $this->get_field_name('width'); ?>" type="text" value="<?php echo $width; ?>" size="4" maxlength="4" />px</label> <small><a href="http://shailan.com/wordpress/plugins/countdown/help/#width" target="_blank" rel="external">(?)</a></small></p>
  263.  
  264.         <p><label for="<?php echo $this->get_field_id('radius'); ?>"><?php _e('Border Radius:'); ?> <input id="<?php echo $this->get_field_id('radius'); ?>" name="<?php echo $this->get_field_name('radius'); ?>" type="text" value="<?php echo $radius; ?>" size="4" maxlength="4" />px</label> <small><a href="http://shailan.com/wordpress/plugins/countdown/help/#border-radius" target="_blank" rel="external">(?)</a></small></p>
  265.  
  266.         <p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('link'); ?>" name="<?php echo $this->get_field_name('link'); ?>"<?php checked( $link ); ?> />
  267.         <label for="<?php echo $this->get_field_id('link'); ?>"><?php _e( 'Remove link' ); ?></label> <small><a href="http://shailan.com/wordpress/plugins/countdown/help/#remove-link" target="_blank" rel="external">(?)</a></small></p>
  268.  
  269.         <div class="widget-control-actions">
  270.             <p><small>Powered by <a href="http://shailan.com/wordpress/plugins/countdown" title="Wordpress Tips and tricks, Freelancing, Web Design">Shailan.com</a> | <a href="http://shailan.com/wordpress/" title="Get more wordpress widgets and themes">Get more..</a></small></p>
  271.         </div>
  272.  
  273. <div class="clear"></div>
  274.         <?php
  275.  
  276.     }
  277.  
  278.     function header($instance){
  279.         $all_widgets = $this->get_settings();
  280.  
  281.         foreach ($all_widgets as $key => $widget){
  282.             $widget_id = $this->id_base . '-' . $key;
  283.             if(is_active_widget(false, $widget_id, $this->id_base)){
  284.                 $countdown = $all_widgets[$key];
  285.  
  286.                 if( preg_match('/^#[a-f0-9]{6}$/i', $countdown['color']) == 0 && preg_match('/^[a-f0-9]{6}$/i', $countdown['color']) ){
  287.                     $color = "#" . $countdown['color'];
  288.                 }
  289.  
  290.                 if( preg_match('/^#[a-f0-9]{6}$/i', $countdown['bgcolor']) == 0 && preg_match('/^[a-f0-9]{6}$/i', $countdown['bgcolor']) ){
  291.                     $bgcolor = "#" . $countdown['bgcolor'];
  292.                 }
  293.  
  294.                 echo "\n<style type=\"text/css\" media=\"screen\">";
  295.                 echo "\n\t #shailan-countdown-".$key.", .shailan-countdown-".$key.".hasCountdown{ ";
  296.                 // Background color
  297.                 if(!empty($countdown['bgcolor'])){
  298.                     echo "\n\tbackground-color: ".$countdown['bgcolor'].";";
  299.                 } else {
  300.                     echo "\n\tbackground-color: transparent;";
  301.                 };
  302.                 // Color
  303.                 if(!empty($countdown['color'])){ echo "\n\tcolor: ".$countdown['color'].";"; };
  304.                 // Width
  305.                 if(!empty($countdown['width']) && $countdown['width']>0){ echo "\n\twidth:".$countdown['width']."px;"; };
  306.                 if(!empty($countdown['radius']) && $countdown['radius']>0){ echo "\n\tborder-radius:".$countdown['radius']."px;"; };
  307.                 echo "\n\tmargin:0px auto;";
  308.                 echo "}";
  309.                 echo "\n</style>\n";
  310.             }
  311.         }
  312.     }
  313.  
  314.     function footer($instance){
  315.  
  316.     }
  317.  
  318.  
  319. } // class shailan_CountdownWidget
  320.  
  321. function shailan_CountdownWidget_shortcode( $atts, $content = null ){
  322.     global $post, $subpages_indexes;
  323.  
  324.     $args = shortcode_atts( array(
  325.             'title'=>'',
  326.             'event'=>'',
  327.             'date'=>false,
  328.             'month'=>'',
  329.             'day'=>'',
  330.             'hour'=>'0',
  331.             'minutes'=>'0',
  332.             'seconds'=>'0',
  333.             'year'=>'',
  334.             'format'=>'yowdHMS',
  335.             'layout' => '',
  336.             'color'=>'',
  337.             'bgcolor'=>'',
  338.             'width'=>'',
  339.             'height' => '',
  340.             'radius' => '',
  341.             'border' => '',
  342.             'link'=>false,
  343.             'timezone' => 'SCW_NONE',
  344.             'direction' => 'down',
  345.             'isWidget' => false
  346.         ), $atts );
  347.  
  348.     if( $args['date'] ){
  349.         if (($timestamp = strtotime( $args['date'] )) !== false) {
  350.             $args['month'] = date("n", $timestamp );
  351.             $args['day'] = date("j", $timestamp );
  352.             $args['year'] = date("Y", $timestamp );
  353.         }
  354.     }
  355.  
  356.     ob_start();
  357.     the_widget( 'shailan_CountdownWidget', $args );
  358.     $cd_code = ob_get_contents();
  359.     ob_end_clean();
  360.  
  361.     return $cd_code;
  362.  
  363. } add_shortcode( 'countdown', 'shailan_CountdownWidget_shortcode');
  364.  
  365. // register widget
  366. add_action('widgets_init', create_function('', 'return register_widget("shailan_CountdownWidget");'));
  367.  
  368. if(!function_exists('get_plugin_path')){
  369.     function get_plugin_path($filepath){
  370.         $plugin_path = preg_replace('/^.*wp-content[\\\\\/]plugins[\\\\\/]/', '', $filepath);
  371.         $plugin_path = str_replace('\\','/',$plugin_path );
  372.         $plugin_dir  = substr($plugin_path ,0,strrpos($plugin_path ,'/'));
  373.         $plugin_realpath = str_replace('\\','/',dirname($filepath));
  374.         $plugin_siteurl  = get_bloginfo('wpurl');
  375.         $plugin_siteurl  = (strpos($plugin_siteurl,'http://') === false) ? get_bloginfo('siteurl') : $plugin_siteurl;
  376.         return $plugin_siteurl.'/wp-content/plugins/'.$plugin_dir.'/';
  377.     }
  378. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement