Completed
Push — develop ( 1753bd...a73cd0 )
by David
02:54
created

Wordlift_Event_Entity_Page_Service   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B pre_get_posts() 0 32 5
1
<?php
2
/**
3
 * Services: Set the order in which entities are displayed on the archive
4
 * page of the event entity.
5
 *
6
 * Sorts the order of the entities being displayed by reverse event start time
7
 *
8
 * @since   3.12.0
9
 * @package Wordlift
10
 */
11
12
/**
13
 * Define the {@link Wordlift_Event_Entity_Page_Service} class.
14
 *
15
 * @since   3.12.0
16
 * @package Wordlift
17
 */
18
class Wordlift_Event_Entity_Page_Service {
19
20
	/**
21
	 * Set the entity post types as one to be included in archive pages.
22
	 *
23
	 * In order to have entities show up in standard WP categories (Posts categories)
24
	 * we configure the `entity` post type, but we also need to alter the main
25
	 * WP query (which by default queries posts only) to include the `entities`.
26
	 *
27
	 * @since 3.12.0
28
	 *
29
	 * @param WP_Query $query WP's {@link WP_Query} instance.
30
	 */
31
	public function pre_get_posts( $query ) {
32
33
		// Only for the main query, avoid problems with widgets and what not.
34
		if ( ! $query->is_main_query() ) {
35
			return;
36
		}
37
38
		// We don't want to alter the query if we're in the admin UI, if this is
39
		// not a event achieve query, or if the `suppress_filters` is set.
40
		//
41
		// Note that it is unlikely for `suppress_filter` to be set on the front
42
		// end, but let's be safe if it is set the calling code assumes no
43
		// modifications of queries.
44
		//
45
		// is_admin is needed, otherwise category based post filters will show
46
		// both types and at the current release (4.7) it causes PHP errors.
47
		if ( is_admin() ||
48
		     ! is_tax( Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, 'event' ) ||
49
		     ! empty( $query->query_vars['suppress_filters'] )
50
		) {
51
			return;
52
		}
53
54
		// Update the query to use the start time meta and desc order.
55
		$meta_query[] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$meta_query was never initialized. Although not strictly required by PHP, it is generally a good practice to add $meta_query = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
56
			'key' => Wordlift_Schema_Service::FIELD_DATE_START,
57
		);
58
		$query->set( 'meta_query', $meta_query );
59
		$query->set( 'orderby', 'meta_value' );
60
		$query->set( 'order', 'DESC' );
61
		
62
	}
63
64
}
65