Advertisement
dgwatkins

Ajax Archive Calendar

Jan 17th, 2017
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.83 KB | None | 0 0
  1. <?php
  2. /*
  3.   Plugin Name: Ajax Archive Calendar
  4.   Plugin URI: http://projapotibd.com
  5.   Description: Ajax Archive Calendar is not only Calendar is also Archive. It is making by customize WordPress default calendar. I hope every body enjoy this plugin.
  6.   Author: osmansorkar
  7.   Version: 2.5
  8.   Author URI: http://www.projapotibd.com/
  9.  */
  10.  
  11. /**
  12.  * Enqueue frontend scripts.
  13.  */
  14. add_action('wp_enqueue_scripts', 'ajax_ac_enqueue_scripts');
  15.  
  16. /**
  17.  * Make sure we have jquery available.
  18.  */
  19. function ajax_ac_enqueue_scripts() {
  20.     wp_enqueue_script('jquery');
  21. }
  22.  
  23. /**
  24.  * Add function to widgets_init that'll load our widget.
  25.  * @since 0.1
  26.  */
  27. add_action('widgets_init', 'ajax_ac_int');
  28.  
  29. /**
  30.  * Register our widget.
  31.  * 'Example_Widget' is the widget class used below.
  32.  *
  33.  * @since 0.1
  34.  */
  35. function ajax_ac_int() {
  36.     register_widget('ajax_ac_widget');
  37. }
  38.  
  39. /* * ******************************************************** */
  40.  
  41. class ajax_ac_widget extends WP_Widget {
  42.  
  43.     function __construct() {
  44.         parent::__construct(
  45.             'ajax_ac_widget', // Base ID
  46.             'Ajax Archive calendar', // Name
  47.             array('description' => 'It is Ajax Archive Calendar', 'text_domain') // Args
  48.         );
  49.     }
  50.  
  51.     /*   * ******************** It will be sow home page**************** */
  52.  
  53.     function widget($args, $instance) {
  54.         global $wp_locale;
  55.  
  56.         extract($args);
  57.         $title = apply_filters('widget_title', $instance['title']);
  58.  
  59.         /* Before widget (defined by themes). */
  60.         echo $before_widget;
  61.  
  62.         /* $title define by from */
  63.         if ($title)
  64.         /* after title and before title defince by thime */
  65.             echo $before_title . $title . $after_title;
  66.         /* end title */
  67.         /* now start your degine */
  68.         ?>
  69.         <div id="ajax_ac_widget">
  70.             <div class="select_ca">
  71.                 <select name="month" id="my_month" >
  72.                     <?php
  73.                     $month = array();
  74.                     for ($i = 1; $i <= 12; $i++) {
  75.                         $monthnum = zeroise($i, 2);
  76.                         $month[$monthnum] = $wp_locale->get_month($i);
  77.                     }
  78.  
  79.                     global $m;
  80.                     if (empty($m) || $m == '') {
  81.                         $nowm = date('m');
  82.                         $nowyear = date('Y');
  83.                     } else {
  84.                         $mmm = str_split($m, 2);
  85.                         $nowm = zeroise(intval(substr($m, 4, 2)), 2);
  86.                         $nowyear = $mmm['0'] . $mmm['1'];
  87.                     }
  88.  
  89.                     foreach ($month as $k => $mu) {
  90.                         if ($k == $nowm) {
  91.                             echo '<option value="' . $k . '" selected="selected" >' . $mu . '</option>';
  92.                         } else {
  93.                             echo '<option value="' . $k . '">' . $mu . '</option>';
  94.                         }
  95.                     }
  96.                     ?>
  97.                 </select>
  98.  
  99.                 <?php
  100.                 $find = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "0",);
  101.                 $replace = array("১", "২", "৩", "৪", "৫", "৬", "৭", "৮", "৯", "০",);
  102.  
  103.  
  104.                 $taryear = $nowyear + 5;
  105.                 $yeararr = array();
  106.                 $lassyear = $nowyear - 5;
  107.                 for ($nowyearrr = $lassyear; $nowyearrr <= $taryear; $nowyearrr++) {
  108.                     $yeararr[$nowyearrr] = $nowyearrr;
  109.                 }
  110.                 ?>
  111.  
  112.                 <select name="Year" id="my_year" >
  113.                     <?php
  114.                     foreach ($yeararr as $k => $year) {
  115.                         if ('bn' === substr(get_locale(), 0, 2)) {
  116.                             $year = str_replace($find, $replace, $year);
  117.                         }
  118.                         if ($k == $nowyear) {
  119.                             echo '<option value="' . $k . '" selected="selected" >' . $year . '</option>';
  120.                         } else {
  121.                             echo '<option value="' . $k . '">' . $year . '</option>';
  122.                         }
  123.                     }
  124.                     ?>
  125.  
  126.                 </select>
  127.             </div><!--select ca -->
  128.             <div class="clear" style="clear:both; margin-bottom: 5px;"></div>
  129.             <div id="my_calender">
  130.  
  131.             </div><!--my_calender -->
  132.             <div class="aj-loging"><img src="<?php $url = plugin_dir_url(__FILE__);
  133.                     echo $url . 'loading.gif';
  134.                     ?>" /></div>
  135.             <script type="text/javascript" >
  136.                 jQuery('#my_month,#my_year').change(function (e) {
  137.                     jQuery(".aj-loging").css("display", "block");
  138.                     jQuery("#my_calender").css("opacity", "0.30");
  139.                     var mon = jQuery('#my_month').val();
  140.                     var year = jQuery('#my_year').val();
  141.                     var to = year + mon;
  142.                     var data = {
  143.                         action: 'ajax_ac',
  144.                         ma: to
  145.                     };
  146.  
  147.                     // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
  148.                     jQuery.get(ajaxurl, data, function (response) {
  149.                         jQuery("#my_calender").html(response);
  150.                         jQuery(".aj-loging").css("display", "none");
  151.                         jQuery("#my_calender").css("opacity", "1.00");
  152.                     });
  153.  
  154.                 });
  155.             </script>
  156.  
  157.             <script type="text/javascript" >
  158.  
  159.  
  160.                 jQuery(document).ready(function (e) {
  161.         <?php
  162.         if (!isset($_GET['m'])) {
  163.             echo 'var a=' . date('Ym');
  164.         } else
  165.             echo 'var a=' . $_GET['m'];
  166.         ?>
  167.  
  168.                     var data = {
  169.                         action: 'ajax_ac',
  170.                         ma: a,
  171.                     };
  172.  
  173.                     // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
  174.                     jQuery.get(ajaxurl, data, function (response) {
  175.                         jQuery("#my_calender").html(response);
  176.                         jQuery(".aj-loging").css("display", "none");
  177.                         jQuery("#my_calender").css("opacity", "1.00");
  178.                     });
  179.                 });
  180.             </script>
  181.  
  182.  
  183.         </div>
  184.  
  185.         <?php
  186.         /* now end your degine
  187.  
  188.           /*arter_widget define by thimed */
  189.         echo $after_widget;
  190.     }
  191.  
  192.     /*   * ****************** It Update widget ****************************** */
  193.  
  194.     function update($new_instance, $old_instance) {
  195.         $instance = $old_instance;
  196.         $instance['title'] = strip_tags($new_instance['title']);
  197.         return $instance;
  198.     }
  199.  
  200.     /*   * ********************** It is sow only admin menu********************************* */
  201.  
  202.     function form($instance) {
  203.         $defaults = array('title' => 'Archive Calendar');
  204.         $instance = wp_parse_args((array) $instance, $defaults);
  205.         ?>
  206.         <p>
  207.             <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'Recent post'); ?></label>
  208.             <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" />
  209.         </p>
  210.  
  211.  
  212.         <?php
  213.     }
  214.  
  215. // end from function
  216. }
  217.  
  218. // end widget class
  219.  
  220.  
  221. add_action('wp_ajax_ajax_ac', 'ajax_ac_callback');
  222. add_action('wp_ajax_nopriv_ajax_ac', 'ajax_ac_callback');
  223.  
  224. function ajax_ac_callback() {
  225.     $ma = $_GET['ma'];
  226.     ajax_ac_calendar($ma);
  227.     die(); // this is required to return a proper result
  228. }
  229.  
  230. function ajax_ac_calendar($ma, $echo = true) {
  231.     global $wpdb, $m, $monthnum, $year, $wp_locale, $posts;
  232.     $m = & $ma;
  233.     $cache = array();
  234.     $key = md5(get_locale() . $m);
  235.  
  236.     if ($cache = wp_cache_get('get_calendar', 'calendar')) {
  237.         if (is_array($cache) && isset($cache[$key])) {
  238.             if ($echo) {
  239.                 echo apply_filters('get_calendar', $cache[$key]);
  240.                 return;
  241.             } else {
  242.                 return apply_filters('get_calendar', $cache[$key]);
  243.             }
  244.         }
  245.     }
  246.  
  247.     if (!is_array($cache))
  248.         $cache = array();
  249.  
  250.     // Quick check. If we have no posts at all, abort!
  251.     if (!$posts) {
  252.         $gotsome = $wpdb->get_var("SELECT 1 as test FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1");
  253.         if (!$gotsome) {
  254.             $cache[$key] = '';
  255.             wp_cache_set('get_calendar', $cache, 'calendar');
  256.             return;
  257.         }
  258.     }
  259.  
  260.     if (isset($_GET['w']))
  261.         $w = '' . intval($_GET['w']);
  262.  
  263.     // week_begins = 0 stands for Sunday
  264.     $week_begins = intval(get_option('start_of_week'));
  265.  
  266.     // Let's figure out when we are
  267.     if (!empty($monthnum) && !empty($year)) {
  268.         $thismonth = '' . zeroise(intval($monthnum), 2);
  269.         $thisyear = '' . intval($year);
  270.     } elseif (!empty($w)) {
  271.         // We need to get the month from MySQL
  272.         $thisyear = '' . intval(substr($m, 0, 4));
  273.         $d = (($w - 1) * 7) + 6; //it seems MySQL's weeks disagree with PHP's
  274.         $thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL $d DAY) ), '%m')");
  275.     } elseif (!empty($m)) {
  276.         $thisyear = '' . intval(substr($m, 0, 4));
  277.         if (strlen($m) < 6)
  278.             $thismonth = '01';
  279.         else
  280.             $thismonth = '' . zeroise(intval(substr($m, 4, 2)), 2);
  281.     } else {
  282.         $thisyear = gmdate('Y', current_time('timestamp'));
  283.         $thismonth = gmdate('m', current_time('timestamp'));
  284.     }
  285.  
  286.     $unixmonth = mktime(0, 0, 0, $thismonth, 1, $thisyear);
  287.     $last_day = date('t', $unixmonth);
  288.  
  289.     $calendar_output = '<table id="my-calendar">
  290. <thead>
  291. <tr>';
  292.  
  293.     $myweek = array();
  294.  
  295.     for ($wdcount = 0; $wdcount <= 6; $wdcount++) {
  296.         $myweek[] = $wp_locale->get_weekday(($wdcount + $week_begins) % 7);
  297.     }
  298.  
  299.     $barr = array('Saturday' => 'শনি', 'Sunday' => 'রবি', 'Monday' => 'সোম', 'Tuesday' => 'মঙ্গল', 'Wednesday' => 'বুধ', 'Thursday' => 'বৃহ', 'Friday' => 'শুক্র');
  300.     foreach ($myweek as $wd) {
  301.         if ('bn' === substr(get_locale(), 0, 2)) {
  302.             $day_name = $barr[$wd];
  303.         } else {
  304.             $day_name = $wp_locale->get_weekday_abbrev($wd);
  305.         }
  306.         $wd = esc_attr($wd);
  307.         $calendar_output .= "\n\t\t<th class=\"$day_name\" scope=\"col\" title=\"$wd\">$day_name</th>";
  308.     }
  309.  
  310.     $calendar_output .= '
  311. </tr>
  312. </thead>
  313.  
  314. <tbody>
  315. <tr>';
  316.  
  317.     // Get days with posts
  318.     $dayswithposts = get_posts(array(
  319.         'suppress_filters' => false,
  320.         'post_type' => 'post',
  321.         'post_status' => 'publish',
  322.         'monthnum' => $thismonth,
  323.         'year' => $thisyear,
  324.     ));
  325.  
  326.     if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'camino') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'safari') !== false)
  327.         $ak_title_separator = "\n";
  328.     else
  329.         $ak_title_separator = ', ';
  330.  
  331.     $daywithpost = array();
  332.     $ak_titles_for_day = array();
  333.     if ($dayswithposts) {
  334.         foreach ((array) $dayswithposts as $ak_post) {
  335.             $daywith = date('d', strtotime($ak_post->post_date));
  336.             if (!in_array($daywith, $daywithpost)) {
  337.                 $daywithpost[] = $daywith;
  338.             }
  339.  
  340.             $post_title = esc_attr(get_the_title($ak_post));
  341.             if (empty($ak_titles_for_day[$daywith])) // first one
  342.                 $ak_titles_for_day[$daywith] = $post_title;
  343.             else
  344.                 $ak_titles_for_day[$daywith] .= $ak_title_separator . $post_title;
  345.         }
  346.     }
  347.  
  348.     // See how much we should pad in the beginning
  349.     $pad = calendar_week_mod(date('w', $unixmonth) - $week_begins);
  350.     if (0 != $pad)
  351.         $calendar_output .= "\n\t\t" . '<td colspan="' . esc_attr($pad) . '" class="pad">&nbsp;</td>';
  352.  
  353.     $daysinmonth = intval(date('t', $unixmonth));
  354.     for ($day = 1; $day <= $daysinmonth; ++$day) {
  355.         if ('bn' === substr(get_locale(), 0, 2)) {
  356.             $dayrrr = array(
  357.                 '1' => '১',
  358.                 '2' => '২',
  359.                 '3' => '৩',
  360.                 '4' => '৪',
  361.                 '5' => '৫',
  362.                 '6' => '৬',
  363.                 '7' => '৭',
  364.                 '8' => '৮',
  365.                 '9' => '৯',
  366.                 '10' => '১০',
  367.                 '11' => '১১',
  368.                 '12' => '১২',
  369.                 '13' => '১৩',
  370.                 '14' => '১৪',
  371.                 '15' => '১৫',
  372.                 '16' => '১৬',
  373.                 '17' => '১৭',
  374.                 '18' => '১৮',
  375.                 '19' => '১৯',
  376.                 '20' => '২০',
  377.                 '21' => '২১',
  378.                 '22' => '২২',
  379.                 '23' => '২৩',
  380.                 '24' => '২৪',
  381.                 '25' => '২৫',
  382.                 '26' => '২৬',
  383.                 '27' => '২৭',
  384.                 '28' => '২৮',
  385.                 '29' => '২৯',
  386.                 '30' => '৩০',
  387.                 '31' => '৩১',
  388.             );
  389.         } else {
  390.             $dayrrr = array(
  391.                 '1' => '1',
  392.                 '2' => '2',
  393.                 '3' => '3',
  394.                 '4' => '4',
  395.                 '5' => '5',
  396.                 '6' => '6',
  397.                 '7' => '7',
  398.                 '8' => '8',
  399.                 '9' => '9',
  400.                 '10' => '10',
  401.                 '11' => '11',
  402.                 '12' => '12',
  403.                 '13' => '13',
  404.                 '14' => '14',
  405.                 '15' => '15',
  406.                 '16' => '16',
  407.                 '17' => '17',
  408.                 '18' => '18',
  409.                 '19' => '19',
  410.                 '20' => '20',
  411.                 '21' => '21',
  412.                 '22' => '22',
  413.                 '23' => '23',
  414.                 '24' => '24',
  415.                 '25' => '25',
  416.                 '26' => '26',
  417.                 '27' => '27',
  418.                 '28' => '28',
  419.                 '29' => '29',
  420.                 '30' => '30',
  421.                 '31' => '31',
  422.             );
  423.         }
  424.         if (isset($newrow) && $newrow)
  425.             $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t";
  426.         $newrow = false;
  427.  
  428.  
  429.  
  430.         if ($day == gmdate('j', current_time('timestamp')) && $thismonth == gmdate('m', current_time('timestamp')) && $thisyear == gmdate('Y', current_time('timestamp')))
  431.             $calendar_output .= '<td id="today"  >';
  432.         else
  433.             $calendar_output .= '<td class="notday">';
  434.  
  435.         if (in_array($day, $daywithpost)) // any posts today?
  436.             $calendar_output .= '<a class="has-post" href="' . get_day_link($thisyear, $thismonth, $day) . '" title="' . esc_attr($ak_titles_for_day[$day]) . "\">$dayrrr[$day]</a>";
  437.         else
  438.             $calendar_output .= '<span class="notpost">' . $dayrrr[$day] . '</span>';
  439.         $calendar_output .= '</td>';
  440.  
  441.         if (6 == calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins))
  442.             $newrow = true;
  443.     }
  444.  
  445.     $pad = 7 - calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins);
  446.     if ($pad != 0 && $pad != 7)
  447.         $calendar_output .= "\n\t\t" . '<td class="pad" colspan="' . esc_attr($pad) . '">&nbsp;</td>';
  448.  
  449.     $calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>";
  450.  
  451.     $cache[$key] = $calendar_output;
  452.     wp_cache_set('get_calendar', $cache, 'calendar');
  453.  
  454.     if ($echo)
  455.         echo apply_filters('get_calendar', $calendar_output);
  456.     else
  457.         return apply_filters('get_calendar', $calendar_output);
  458. }
  459.  
  460. function ajax_ac_head() {
  461.     ?>
  462.     <script type="text/javascript">
  463.         var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
  464.     </script>
  465.  
  466.     <style type="text/css">
  467.  
  468.         #ajax_ac_widget th {
  469.             background: none repeat scroll 0 0 #008000;
  470.             color: #FFFFFF;
  471.             font-weight: normal;
  472.             padding: 5px 1px;
  473.             text-align: center;
  474.             font-size: 16px;
  475.         }
  476.         #ajax_ac_widget {
  477.             padding: 5px;
  478.         }
  479.  
  480.         #ajax_ac_widget td {
  481.             border: 1px solid #CCCCCC;
  482.             text-align: center;
  483.         }
  484.  
  485.         #my-calendar a {
  486.             background: none repeat scroll 0 0 #008000;
  487.             color: #FFFFFF;
  488.             display: block;
  489.             padding: 6px 0;
  490.             width: 100% !important;
  491.             border-radius:20px;
  492.         }
  493.         #my-calendar{
  494.             width:100%;
  495.         }
  496.  
  497.  
  498.         #my_calender span {
  499.             display: block;
  500.             padding: 6px 0;
  501.             width: 100% !important;
  502.             background-color:#999;
  503.             border-radius:20px;
  504.         }
  505.  
  506.         #today a,#today span {
  507.             background: none repeat scroll 0 0 #FF0000 !important;
  508.             color: #FFFFFF;
  509.             border-radius:20px;
  510.         }
  511.         #ajax_ac_widget #my_year {
  512.             float: right;
  513.         }
  514.         .select_ca #my_month {
  515.             float: left;
  516.         }
  517.         .aj-loging > img {
  518.             margin-left: 140px;
  519.             margin-top: -100px;
  520.             position: absolute;
  521.         }
  522.  
  523.     </style>
  524.     <?php
  525. }
  526.  
  527. add_filter('wp_head', 'ajax_ac_head');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement