Skip to content

Commit

Permalink
Added an 'order'-option to event and production listings.
Browse files Browse the repository at this point in the history
Closes  #71.
  • Loading branch information
slimndap committed Dec 29, 2014
1 parent 9f8c05d commit 64f5ce9
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 29 deletions.
5 changes: 3 additions & 2 deletions functions/wpt_events.php
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ public function get($filters=array()) {
global $wp_theatre;

$defaults = array(
'order' => 'asc',
'limit' => false,
'upcoming' => false,
'past' => false,
Expand All @@ -554,14 +555,13 @@ public function get($filters=array()) {
'production' => false,
'status' => array('publish'),
);

$filters = wp_parse_args( $filters, $defaults );

$args = array(
'post_type' => WPT_Event::post_type_name,
'post_status' => $filters['status'],
'meta_query' => array(),
'order' => 'asc'
'order' => $filters['order'],
);

/**
Expand Down Expand Up @@ -644,6 +644,7 @@ public function get($filters=array()) {
$args['category__not_in'] = $filters['category__not_in'];
}


if ($filters['limit']) {
$args['posts_per_page'] = $filters['limit'];
$args['numberposts'] = $filters['limit'];
Expand Down
6 changes: 4 additions & 2 deletions functions/wpt_frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,11 @@ function wpt_events($atts, $content=null) {
'end' => false,
'groupby'=>false,
'limit'=>false,
'order'=>'asc',
);

$atts = shortcode_atts( $defaults, $atts );

if (!empty($atts['paginateby'])) {
$fields = explode(',',$atts['paginateby']);
for ($i=0;$i<count($fields);$i++) {
Expand Down Expand Up @@ -278,7 +279,8 @@ function wpt_productions($atts, $content=null) {
'category__in'=>false,
'category__not_in'=>false,
'groupby'=>false,
'limit'=>false
'limit'=>false,
'order'=>'asc'
);

if (!empty($wp_query->query_vars['wpt_category'])) {
Expand Down
34 changes: 20 additions & 14 deletions functions/wpt_productions.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,20 +193,11 @@ private function get_html_grouped($args=array()) {
break;
default:
/*
* No stickies in filtered, paginated or grouped views
* No stickies in paginated or grouped views
*/
if (
!empty($args['paginateby']) ||
!empty($args['groupby']) ||
!empty($args['category']) ||
!empty($args['cat']) ||
!empty($args['category_name']) ||
!empty($args['category__and']) ||
!empty($args['category__in']) ||
!empty($args['category__not_in']) ||
!empty($args['post__in']) ||
!empty($args['post__not_in']) ||
!empty($args['season'])
!empty($args['groupby'])
) {
$args['ignore_sticky_posts'] = true;
}
Expand Down Expand Up @@ -373,6 +364,7 @@ public function get($filters=array()) {
global $wp_theatre;

$defaults = array(
'order' => 'asc',
'limit' => false,
'post__in' => false,
'post__not_in' => false,
Expand All @@ -391,7 +383,7 @@ public function get($filters=array()) {
'post_type' => WPT_Production::post_type_name,
'post_status' => 'publish',
'meta_query' => array(),
'order' => 'asc'
'order' => $filters['order'],
);

if ($filters['post__in']) {
Expand Down Expand Up @@ -456,8 +448,22 @@ public function get($filters=array()) {

$posts = get_posts($args);

// don't forget the stickies!
if (!$filters['ignore_sticky_posts']) {
/*
* Add sticky productions.
* Unless in filtered, paginated or grouped views.
*/
if (
!$filters['ignore_sticky_posts'] &&
empty($args['cat']) &&
empty($args['category_name']) &&
empty($args['category__and']) &&
empty($args['category__in']) &&
empty($args['category__not_in']) &&
empty($args['post__in']) &&
empty($args['post__not_in']) &&
!$filters['season'] &&
$args['posts_per_page'] < 0
) {
$sticky_posts = get_option( 'sticky_posts' );

if (!empty($sticky_posts)) {
Expand Down
60 changes: 49 additions & 11 deletions tests/test_theatre.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,26 @@ function test_shortcode_wpt_events_paginated_with_historic_events() {
$this->assertEquals(1, substr_count($html, '<span class="wpt_listing_filter"><a href="'));
}

function test_shortcode_wpt_events_order() {

/*
* Ascending (default).
* Expect the production with the first upcoming event.
*/
$link = '<a href="'.get_permalink($this->production_with_upcoming_events).'">';
$output = do_shortcode('[wpt_events limit=1]');
$this->assertContains($link,$output);

/*
* Descending.
* Expect the production with the last upcoming event.
*/
$link = '<a href="'.get_permalink($this->production_with_upcoming_and_historic_events).'">';
$output = do_shortcode('[wpt_events limit=1 order=desc]');
$this->assertContains($link,$output);

}

function test_shortcode_wpt_season_production() {
$season = get_post($this->season1);
}
Expand Down Expand Up @@ -520,20 +540,36 @@ function test_order_productions() {
}

$expected = array(
$this->production_with_historic_event,
$this->production_with_historic_event_sticky,
$this->production_with_upcoming_events,
$this->production_with_upcoming_event,
$this->production_with_upcoming_and_historic_events
$this->production_with_historic_event, // no upcoming events, follows creation order.
$this->production_with_historic_event_sticky, // no upcoming events, follows creation order.
$this->production_with_upcoming_events, // tomorrow
$this->production_with_upcoming_event, // in 2 days
$this->production_with_upcoming_and_historic_events // next week
);

$this->assertEquals($expected,$actual);
}

function test_order_events() {

function test_order_productions_desc() {
$actual = array();
$args = array(
'order' => 'desc'
);
$productions = $this->wp_theatre->productions->get($args);
foreach($productions as $production) {
$actual[] = $production->ID;
}

$expected = array(
$this->production_with_upcoming_and_historic_events, // next week
$this->production_with_upcoming_event, // in 2 days
$this->production_with_upcoming_events, // tomorrow
$this->production_with_historic_event, // no upcoming events, follows creation order.
$this->production_with_historic_event_sticky, // no upcoming events, follows creation order.
);
$this->assertEquals($expected,$actual);
}

// Test transients
function test_wpt_transient_productions() {
do_shortcode('[wpt_productions]');
Expand All @@ -551,7 +587,8 @@ function test_wpt_transient_productions() {
'category__in'=>false,
'category__not_in'=>false,
'groupby'=>false,
'limit'=>false
'limit'=>false,
'order'=>'asc',
);

$this->assertEquals(5, substr_count($this->wp_theatre->transient->get('p',$args), '"wp_theatre_prod"'));
Expand Down Expand Up @@ -589,6 +626,7 @@ function test_wpt_transient_events() {
'end' => false,
'groupby'=>false,
'limit'=>false,
'order'=>'asc',
);
$this->assertEquals(4, substr_count($this->wp_theatre->transient->get('e',$args), '"wp_theatre_event"'));
}
Expand Down Expand Up @@ -736,8 +774,8 @@ function test_wpt_productions_load_args_filter() {

add_filter('wpt_productions_load_args', $func);

// Should return 2 productions in the muziek category (+ 2 sticky productions).
$this->assertCount(4, $this->wp_theatre->productions->get());
// Should return 2 productions in the muziek category.
$this->assertCount(2, $this->wp_theatre->productions->get());

}

Expand Down

0 comments on commit 64f5ce9

Please sign in to comment.