|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Services: Publisher Service. |
|
4
|
|
|
* |
|
5
|
|
|
* The Publisher service provides functions to list potential publishers. |
|
6
|
|
|
* |
|
7
|
|
|
* @since 3.11.0 |
|
8
|
|
|
* @package Wordlift |
|
9
|
|
|
* @subpackage Wordlift/includes |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Define the {@link Wordlift_Publisher_Service} class. |
|
14
|
|
|
* |
|
15
|
|
|
* @since 3.11.0 |
|
16
|
|
|
* @package Wordlift |
|
17
|
|
|
* @subpackage Wordlift/includes |
|
18
|
|
|
*/ |
|
19
|
|
|
class Wordlift_Publisher_Service { |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Counts the number of potential publishers. |
|
23
|
|
|
* |
|
24
|
|
|
* @since 3.11.0 |
|
25
|
|
|
* |
|
26
|
|
|
* @return int The number of potential publishers. |
|
27
|
|
|
*/ |
|
28
|
|
|
public function count() { |
|
29
|
|
|
|
|
30
|
|
|
// Search for entities which are either a Person |
|
31
|
|
|
// or Organization. |
|
32
|
|
|
|
|
33
|
|
|
// Get only the ids as all we need is the count. |
|
34
|
|
|
$entities = get_posts( array( |
|
35
|
|
|
'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
36
|
|
|
'post_status' => 'publish', |
|
37
|
|
|
'posts_per_page' => - 1, |
|
38
|
|
|
'tax_query' => array( |
|
39
|
|
|
array( |
|
40
|
|
|
'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
41
|
|
|
'field' => 'slug', |
|
42
|
|
|
'terms' => array( 'organization', 'person' ), |
|
43
|
|
|
), |
|
44
|
|
|
), |
|
45
|
|
|
'fields' => 'ids', |
|
46
|
|
|
) ); |
|
47
|
|
|
|
|
48
|
|
|
// Finally return the count. |
|
49
|
|
|
return count( $entities ); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Search SQL filter for matching against post title only. |
|
54
|
|
|
* |
|
55
|
|
|
* @link http://wordpress.stackexchange.com/a/11826/1685 |
|
56
|
|
|
* |
|
57
|
|
|
* @since 3.15.0 |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $search The search string. |
|
60
|
|
|
* @param WP_Query $wp_query The {@link WP_Query} instance. |
|
61
|
|
|
* |
|
62
|
|
|
* @return array|string An array of results. |
|
63
|
|
|
*/ |
|
64
|
|
|
public function limit_search_to_title( $search, $wp_query ) { |
|
65
|
|
|
|
|
66
|
|
|
// Bail out if the search or the `search_terms` haven't been set. |
|
67
|
|
|
if ( empty( $search ) || empty( $wp_query->query_vars['search_terms'] ) ) { |
|
68
|
|
|
return $search; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
global $wpdb; |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
$query_vars = $wp_query->query_vars; |
|
74
|
|
|
$percent = ! empty( $query_vars['exact'] ) ? '' : '%'; |
|
75
|
|
|
$search = array(); |
|
76
|
|
|
|
|
77
|
|
|
foreach ( (array) $query_vars['search_terms'] as $term ) { |
|
78
|
|
|
$search[] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $percent . $wpdb->esc_like( $term ) . $percent ); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if ( ! is_user_logged_in() ) { |
|
82
|
|
|
$search[] = "$wpdb->posts.post_password = ''"; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$search = ' AND ' . implode( ' AND ', $search ); |
|
86
|
|
|
|
|
87
|
|
|
return $search; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Query WP for potential publishers, i.e. {@link WP_Post}s which are associated` |
|
92
|
|
|
* with `wl_entity_type` (taxonomy) terms of `Organization` or `Person`. |
|
93
|
|
|
* |
|
94
|
|
|
* @since 3.11.0 |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $filter The title filter. |
|
97
|
|
|
* |
|
98
|
|
|
* @return array An array of results in a select2 friendly format. |
|
99
|
|
|
*/ |
|
100
|
|
|
public function query( $filter = '' ) { |
|
101
|
|
|
|
|
102
|
|
|
// Search for the filter in the titles only. |
|
103
|
|
|
add_filter( 'posts_search', array( |
|
104
|
|
|
$this, |
|
105
|
|
|
'limit_search_to_title', |
|
106
|
|
|
), 10, 2 ); |
|
107
|
|
|
|
|
108
|
|
|
/* |
|
109
|
|
|
* Search for entities which are either a Person |
|
110
|
|
|
* or Organization. Sort the results by title in ascending order. |
|
111
|
|
|
*/ |
|
112
|
|
|
$entities = get_posts( array( |
|
113
|
|
|
'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
114
|
|
|
'post_status' => 'publish', |
|
115
|
|
|
'posts_per_page' => - 1, |
|
116
|
|
|
'tax_query' => array( |
|
117
|
|
|
array( |
|
118
|
|
|
'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
119
|
|
|
'field' => 'slug', |
|
120
|
|
|
'terms' => array( 'organization', 'person' ), |
|
121
|
|
|
), |
|
122
|
|
|
), |
|
123
|
|
|
's' => $filter, |
|
124
|
|
|
'orderby' => 'title', |
|
125
|
|
|
'order' => 'ASC', |
|
126
|
|
|
) ); |
|
127
|
|
|
|
|
128
|
|
|
// Remove the search filter added before the query. |
|
129
|
|
|
remove_filter( 'posts_search', array( |
|
130
|
|
|
$this, |
|
131
|
|
|
'limit_search_to_title', |
|
132
|
|
|
), 10, 2 ); |
|
133
|
|
|
|
|
134
|
|
|
// Set a reference to ourselves to pass to the closure. |
|
135
|
|
|
$publisher_service = $this; |
|
136
|
|
|
|
|
137
|
|
|
// Map the results in a `Select2` compatible array. |
|
138
|
|
|
return array_map( function ( $entity ) use ( $publisher_service ) { |
|
139
|
|
|
$type = wp_get_post_terms( $entity->ID, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
140
|
|
|
$thumb_id = get_post_thumbnail_id( $entity->ID ); |
|
141
|
|
|
|
|
142
|
|
|
return array( |
|
143
|
|
|
'id' => $entity->ID, |
|
144
|
|
|
'text' => $entity->post_title, |
|
145
|
|
|
'type' => $type[0]->name, |
|
146
|
|
|
'thumbnail_url' => $publisher_service->get_attachment_image_url( $thumb_id ), |
|
147
|
|
|
); |
|
148
|
|
|
}, $entities ); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Get the thumbnail's URL. |
|
153
|
|
|
* |
|
154
|
|
|
* @since 3.11.0 |
|
155
|
|
|
* |
|
156
|
|
|
* @param int $attachment_id The attachment id. |
|
157
|
|
|
* @param string $size The attachment size (default = 'thumbnail'). |
|
158
|
|
|
* |
|
159
|
|
|
* @return string|bool The image URL or false if not found. |
|
160
|
|
|
*/ |
|
161
|
|
|
public function get_attachment_image_url( $attachment_id, $size = 'thumbnail' ) { |
|
162
|
|
|
|
|
163
|
|
|
$image = wp_get_attachment_image_src( $attachment_id, $size ); |
|
164
|
|
|
|
|
165
|
|
|
return isset( $image['0'] ) ? $image['0'] : false; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
} |
|
169
|
|
|
|
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state