Created
March 28, 2013 15:09
-
-
Save nimdraugsael/5263901 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Bezdelniki.org ajax filter | |
Plugin URI: http://vk.com/nimdraug | |
Description: a plugin to get posts through ajax request | |
Version: 0.1 | |
Author: Aleksei Shaikhaleev | |
Author URI: http://vk.com/nimdraug | |
License: GPL2 | |
*/ | |
add_action("wp_ajax_get_events", "bezdelniki_ajax_filter_get_events"); | |
add_action("wp_ajax_nopriv_get_events", "bezdelniki_ajax_filter_get_events"); | |
add_action( 'init', 'bezdelniki_ajax_filter_script_enqueuer' ); | |
function bezdelniki_ajax_filter_script_enqueuer() { | |
wp_register_script( "bezdelniki-filter", WP_PLUGIN_URL.'/bezdelniki-ajax-filter/js/bezdelniki-filter.js', array('jquery') ); | |
wp_register_script( "date_js", WP_PLUGIN_URL.'/bezdelniki-ajax-filter/js/date.js' ); | |
wp_register_script( "waypoints.min", WP_PLUGIN_URL.'/bezdelniki-ajax-filter/js/waypoints.min.js' ); | |
wp_register_script( "waypoints-sticky.min", WP_PLUGIN_URL.'/bezdelniki-ajax-filter/js/waypoints-sticky.min.js' ); | |
wp_register_script( "jquery.simplemodal", WP_PLUGIN_URL.'/bezdelniki-ajax-filter/js/jquery.simplemodal.js' ); | |
wp_localize_script( 'bezdelniki-filter', 'nonce', wp_create_nonce("get_events_nonce") ); | |
wp_enqueue_script( 'jquery' ); | |
wp_enqueue_script( 'date_js' ); | |
wp_enqueue_script( 'waypoints.min' ); | |
wp_enqueue_script( 'waypoints-sticky.min' ); | |
wp_enqueue_script( 'jquery.simplemodal' ); | |
wp_enqueue_script( 'bezdelniki-filter' ); | |
} | |
function filter_where_for_week( $where = '' ) { | |
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-0 day')) . "' AND post_date < '" . date('Y-m-d', strtotime('+7 day')) . "'"; | |
return $where; | |
} | |
function filter_where_for_future( $where = '' ) { | |
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-0 day')) . "'"; | |
return $where; | |
} | |
function filter_where_for_days( $where = '' ) { | |
global $days; | |
$where .= " AND date_format(post_date, '%Y-%m-%d') in ($days)"; | |
// $where .= " AND post_date > '" . date('Y-m-d', strtotime('-0 day')) . "' AND post_date < '" . date('Y-m-d', strtotime('+7 day')) . "'"; | |
return $where; | |
} | |
// function filter_where_for_days( $where = '' ) { | |
// global $days; | |
// $where .= " AND date_format(post_date, '%Y-%m-%d') in ($days)"; | |
// echo $where; | |
// } | |
function bezdelniki_ajax_filter_get_events() { | |
if ( !wp_verify_nonce( $_REQUEST['nonce'], "get_events_nonce")) { | |
exit("No naughty business please"); | |
} | |
$tomorrow = mktime(0, 0, 0, date("m"), date("d")+1, date("y")); | |
switch ($_REQUEST['time']) { | |
case 'today': | |
$args = array( | |
'year' => date('Y'), | |
'monthnum' => date('m'), | |
'day' => date('j'), | |
'post_type' => 'post', | |
'order' => 'ASC' | |
); | |
$custom_query = new WP_Query( $args ); | |
break; | |
case 'tomorrow': | |
$args = array( | |
'year' => date('Y', $tomorrow), | |
'monthnum' => date('m', $tomorrow), | |
'day' => date('j', $tomorrow), | |
'post_type' => 'post', | |
'order' => 'ASC' | |
); | |
$custom_query = new WP_Query( $args ); | |
break; | |
case 'week': | |
add_filter('posts_where', 'filter_where_for_week'); | |
$args = array( | |
'post_type' => 'post', | |
'order' => 'ASC' | |
); | |
$custom_query = new WP_Query( $args ); | |
remove_filter('posts_where', 'filter_where_for_week'); | |
break; | |
case 'future': | |
add_filter('posts_where', 'filter_where_for_future'); | |
$args = array( | |
'post_type' => 'post', | |
'order' => 'ASC' | |
); | |
$custom_query = new WP_Query( $args ); | |
remove_filter('posts_where', 'filter_where_for_future'); | |
break; | |
default: | |
$days = $_REQUEST['time']; | |
$days_array = split(',', $days); | |
for ($i = 0; $i < count($days_array); $i++) { | |
$days_array[$i] = "'" . $days_array[$i] . "'"; | |
} | |
global $days; | |
$days = implode(',', $days_array); | |
add_filter('posts_where', 'filter_where_for_days'); | |
$args = array( | |
'post_type' => 'post', | |
'order' => 'ASC' | |
); | |
$custom_query = new WP_Query( $args ); | |
remove_filter('posts_where', 'filter_where_for_days'); | |
break; | |
} | |
while ( $custom_query->have_posts() ) { | |
$custom_query->the_post(); | |
$result[] = load_template_part( 'content', get_post_format() ); | |
} | |
if (count($result) == 0) { | |
$result[] = "<h2>Ни одного события не найдено, может, выбрать другую дату?</h2>"; | |
} | |
echo json_encode($result); | |
wp_reset_query(); | |
die(); | |
} | |
function load_template_part($template_name, $part_name=null) { | |
ob_start(); | |
get_template_part($template_name, $part_name); | |
$var = ob_get_contents(); | |
ob_end_clean(); | |
return $var; | |
} | |
class AnticaffeeEventsWidget extends WP_Widget | |
{ | |
function AnticaffeeEventsWidget() | |
{ | |
$widget_ops = array('classname' => 'AnticaffeeEventsWidget', 'description' => 'Показывает ближайшие события анти-кафе' ); | |
$this->WP_Widget('AnticaffeeEventsWidget', 'События анти-кафе', $widget_ops); | |
} | |
function form($instance) | |
{ | |
$instance = wp_parse_args( (array) $instance, array( 'title' => '' ) ); | |
$title = $instance['title']; | |
?> | |
<p><label for="<?php echo $this->get_field_id('title'); ?>">Title: <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p> | |
<?php | |
} | |
function update($new_instance, $old_instance) | |
{ | |
$instance = $old_instance; | |
$instance['title'] = $new_instance['title']; | |
return $instance; | |
} | |
function widget($args, $instance) | |
{ | |
extract($args, EXTR_SKIP); | |
echo $before_widget; | |
$title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']); | |
if (!empty($title)) | |
echo $before_title . $title . $after_title;; | |
echo "<div class='anticaffee_widget_container sticky'><img src='/images/anticaffee_title.png'>"; | |
function filter_where( $where = '' ) { | |
$where .= " AND post_date > '" . date('Y-m-d') . "'"; | |
return $where; | |
} | |
add_filter('posts_where', 'filter_where'); | |
$args = array( | |
'posts_per_page' => 20, | |
'tag' => 'антикафе', | |
'order' => 'ASC' | |
); | |
$custom_query = new WP_Query( $args ); | |
if ( $custom_query->have_posts() ): | |
while ( $custom_query->have_posts() ) : | |
$custom_query->the_post(); | |
$magic_days[get_the_time('Y-m-d')][] = "<a href='".get_permalink()."'>".get_the_time('G:i')." ".get_the_title()."</a>"; | |
endwhile; | |
endif; | |
wp_reset_query(); | |
remove_filter('posts_where', 'filter_where'); | |
$magic_days; | |
$month_names_owned = array( 0 => 'января', | |
1 => 'февраля', | |
2 => 'марта', | |
3 => 'апреля', | |
4 => 'мая', | |
5 => 'июня', | |
6 => 'июля', | |
7 => 'августа', | |
8 => 'сентября', | |
9 => 'октября', | |
10 =>'ноября', | |
11 =>'декабря' ); | |
foreach ($magic_days as $day => $events) { | |
$day_splitted = split('-',$day); | |
echo "<p>"; | |
echo intval($day_splitted[2]); | |
echo " "; | |
echo $month_names_owned[intval($day_splitted[1])-1]; | |
echo "</p>"; | |
foreach ($events as $html) { | |
echo "<p>$html</p>"; | |
} | |
} | |
echo "</div>"; | |
echo $after_widget; | |
} | |
} | |
add_action( 'widgets_init', create_function('', 'return register_widget("AnticaffeeEventsWidget");') ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment