|
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_Entity_Page_Service} class. |
|
14
|
|
|
* |
|
15
|
|
|
* @since 3.12.0 |
|
16
|
|
|
* @package Wordlift |
|
17
|
|
|
*/ |
|
18
|
|
|
class Wordlift_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 entity type 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
|
|
|
// Ignore admin side request, requests for which filters should be |
|
46
|
|
|
// suppressed, and when we are not on a entity type archive page. |
|
47
|
|
|
if ( is_admin() || |
|
48
|
|
|
! is_tax( Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ) || |
|
49
|
|
|
! empty( $query->query_vars['suppress_filters'] ) |
|
50
|
|
|
) { |
|
51
|
|
|
return; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// Events should be sorted by start date in descending order. |
|
55
|
|
|
if ( is_tax( Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, 'event' ) ) { |
|
56
|
|
|
|
|
57
|
|
|
// Update the query to use the start time meta and desc order. |
|
58
|
|
|
$meta_query[] = array( |
|
|
|
|
|
|
59
|
|
|
'key' => Wordlift_Schema_Service::FIELD_DATE_START, |
|
60
|
|
|
); |
|
61
|
|
|
$query->set( 'meta_query', $meta_query ); |
|
62
|
|
|
$query->set( 'orderby', 'meta_value' ); |
|
63
|
|
|
$query->set( 'order', 'DESC' ); |
|
64
|
|
|
} else { |
|
65
|
|
|
/* |
|
66
|
|
|
* All other entity types should be sorted by their connectivity. |
|
67
|
|
|
* For this we need to query the relationship table which has |
|
68
|
|
|
* to be done by manipulating the SQL generated for the query. |
|
69
|
|
|
* As this is impossible to be done by changing the query, we Set |
|
70
|
|
|
* additional filters to handle it. |
|
71
|
|
|
*/ |
|
72
|
|
|
|
|
73
|
|
|
add_filter( 'posts_join', array( $this, 'posts_join' ) ); |
|
74
|
|
|
add_filter( 'posts_groupby', array( $this, 'posts_groupby' ) ); |
|
75
|
|
|
add_filter( 'posts_orderby', array( $this, 'posts_orderby' ) ); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Filter handler that sets the join part of a query to include the |
|
81
|
|
|
* relationship table to be able to use it in the sorting. |
|
82
|
|
|
* |
|
83
|
|
|
* @since 3.15.0 |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $join_statement The join part of the SQL statement which is used for the query. |
|
86
|
|
|
* |
|
87
|
|
|
* @return string An join SQL which add the relationships table to the join. |
|
88
|
|
|
*/ |
|
89
|
|
|
public function posts_join( $join_statement ) { |
|
90
|
|
|
|
|
91
|
|
|
global $wpdb; |
|
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
$join_statement .= " LEFT JOIN {$wpdb->prefix}wl_relation_instances ri " . |
|
94
|
|
|
'ON (ri.object_id = wp_posts.ID)'; |
|
95
|
|
|
|
|
96
|
|
|
// Remove to make sure it will not run agan in other context. |
|
97
|
|
|
remove_filter( 'posts_join', array( $this, 'posts_join' ) ); |
|
98
|
|
|
return $join_statement; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Filter handler that sets the groupby part of a query to include the |
|
103
|
|
|
* relationship table to be able to use it in the sorting. |
|
104
|
|
|
* |
|
105
|
|
|
* @since 3.15.0 |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $groupby_statement The groupby part of the SQL statement which is used for the query. |
|
108
|
|
|
* |
|
109
|
|
|
* @return string A groupby SQL which add the relationships table to the join. |
|
110
|
|
|
*/ |
|
111
|
|
|
public function posts_groupby( $groupby_statement ) { |
|
112
|
|
|
|
|
113
|
|
|
$groupby_statement = 'ri.object_id, ' . $groupby_statement; |
|
114
|
|
|
|
|
115
|
|
|
// Remove to make sure it will not run agan in other context. |
|
116
|
|
|
remove_filter( 'posts_groupby', array( $this, 'posts_groupby' ) ); |
|
117
|
|
|
return $groupby_statement; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Filter handler that sets the orderby part of a query to sort by number of |
|
122
|
|
|
* relationships. |
|
123
|
|
|
* |
|
124
|
|
|
* @since 3.15.0 |
|
125
|
|
|
* |
|
126
|
|
|
* @param string $orderby_statement The orderby part of the SQL statement which is used for the query. |
|
127
|
|
|
* |
|
128
|
|
|
* @return string An orderby SQL which sorts by the number of relationships |
|
129
|
|
|
*/ |
|
130
|
|
|
public function posts_orderby( $orderby_statement ) { |
|
131
|
|
|
|
|
132
|
|
|
$orderby_statement = 'COUNT( ri.object_id ) DESC, ' . $orderby_statement; |
|
133
|
|
|
|
|
134
|
|
|
// Remove to make sure it will not run agan in other context. |
|
135
|
|
|
remove_filter( 'posts_orderby', array( $this, 'posts_orderby' ) ); |
|
136
|
|
|
|
|
137
|
|
|
return $orderby_statement; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
|
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:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.