|
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
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Define the {@link Wordlift_Publisher_Service} class. |
|
13
|
|
|
* |
|
14
|
|
|
* @since 3.11.0 |
|
15
|
|
|
* @package Wordlift |
|
16
|
|
|
*/ |
|
17
|
|
|
class Wordlift_Publisher_Service { |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Counts the number of potential publishers. |
|
21
|
|
|
* |
|
22
|
|
|
* @since 3.11.0 |
|
23
|
|
|
* |
|
24
|
|
|
* @return int The number of potential publishers. |
|
25
|
|
|
*/ |
|
26
|
|
|
public function count() { |
|
27
|
|
|
|
|
28
|
|
|
// Get the global `wpdb` instance. |
|
29
|
|
|
global $wpdb; |
|
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
// Run the query and get the count. |
|
32
|
|
|
$count = $wpdb->get_var( $wpdb->prepare( |
|
33
|
|
|
'SELECT COUNT( p.id )' . |
|
34
|
|
|
" FROM $wpdb->posts p" . |
|
35
|
|
|
" LEFT JOIN $wpdb->term_relationships tr" . |
|
36
|
|
|
' ON tr.object_id = p.id' . |
|
37
|
|
|
" LEFT JOIN $wpdb->term_taxonomy tt" . |
|
38
|
|
|
' ON tt.term_taxonomy_id = tr.term_taxonomy_id' . |
|
39
|
|
|
" LEFT JOIN $wpdb->terms t" . |
|
40
|
|
|
' ON t.term_id = tt.term_id' . |
|
41
|
|
|
" LEFT JOIN $wpdb->postmeta m" . |
|
42
|
|
|
" ON m.post_id = p.id AND m.meta_key = '_thumbnail_id'" . |
|
43
|
|
|
' WHERE p.post_type = %s' . |
|
44
|
|
|
" AND t.name IN ( 'Organization', 'Person' )" . |
|
45
|
|
|
' AND tt.taxonomy = %s' . |
|
46
|
|
|
' ORDER BY p.post_title', |
|
47
|
|
|
Wordlift_Entity_Service::TYPE_NAME, |
|
48
|
|
|
Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME |
|
49
|
|
|
) ); |
|
50
|
|
|
|
|
51
|
|
|
// Finally return the count. |
|
52
|
|
|
return (int) $count; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Query WP for potential publishers, i.e. {@link WP_Post}s of type `entity` |
|
57
|
|
|
* and of `wl_entity_type` (taxonomy) `Organization` or `Person`. |
|
58
|
|
|
* |
|
59
|
|
|
* @since 3.11.0 |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $filter The title filter. |
|
62
|
|
|
* |
|
63
|
|
|
* @return array An array of results. |
|
64
|
|
|
*/ |
|
65
|
|
|
public function query( $filter = '' ) { |
|
66
|
|
|
|
|
67
|
|
|
// Get the global `wpdb` instance. |
|
68
|
|
|
global $wpdb; |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
// Run the query and get the results. |
|
71
|
|
|
$results = $wpdb->get_results( $wpdb->prepare( |
|
72
|
|
|
'SELECT p.id, p.post_title, t.name AS type, m.meta_value AS thumbnail_id' . |
|
73
|
|
|
" FROM $wpdb->posts p" . |
|
74
|
|
|
" LEFT JOIN $wpdb->term_relationships tr" . |
|
75
|
|
|
' ON tr.object_id = p.id' . |
|
76
|
|
|
" LEFT JOIN $wpdb->term_taxonomy tt" . |
|
77
|
|
|
' ON tt.term_taxonomy_id = tr.term_taxonomy_id' . |
|
78
|
|
|
" LEFT JOIN $wpdb->terms t" . |
|
79
|
|
|
' ON t.term_id = tt.term_id' . |
|
80
|
|
|
" LEFT JOIN $wpdb->postmeta m" . |
|
81
|
|
|
" ON m.post_id = p.id AND m.meta_key = '_thumbnail_id'" . |
|
82
|
|
|
' WHERE p.post_type = %s' . |
|
83
|
|
|
" AND t.name IN ( 'Organization', 'Person' )" . |
|
84
|
|
|
' AND tt.taxonomy = %s' . |
|
85
|
|
|
' AND p.post_title LIKE %s' . |
|
86
|
|
|
' ORDER BY p.post_title', |
|
87
|
|
|
Wordlift_Entity_Service::TYPE_NAME, |
|
88
|
|
|
Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
89
|
|
|
'%' . $wpdb->esc_like( $filter ) . '%' |
|
90
|
|
|
) ); |
|
91
|
|
|
|
|
92
|
|
|
// Set a reference to ourselves to pass to the closure. |
|
93
|
|
|
$publisher_service = $this; |
|
94
|
|
|
|
|
95
|
|
|
// Map the results in a `Select2` compatible array. |
|
96
|
|
|
return array_map( function ( $item ) use ( $publisher_service ) { |
|
97
|
|
|
return array( |
|
98
|
|
|
'id' => $item->id, |
|
99
|
|
|
'text' => $item->post_title, |
|
100
|
|
|
'type' => $item->type, |
|
101
|
|
|
'thumbnail_url' => $publisher_service->get_attachment_image_url( $item->thumbnail_id ), |
|
102
|
|
|
); |
|
103
|
|
|
}, $results ); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Get the thumbnail's URL. |
|
108
|
|
|
* |
|
109
|
|
|
* @since 3.11.0 |
|
110
|
|
|
* |
|
111
|
|
|
* @param int $attachment_id The attachment id. |
|
112
|
|
|
* @param string $size The attachment size (default = 'thumbnail'). |
|
113
|
|
|
* |
|
114
|
|
|
* @return string|bool The image URL or false if not found. |
|
115
|
|
|
*/ |
|
116
|
|
|
public function get_attachment_image_url( $attachment_id, $size = 'thumbnail' ) { |
|
117
|
|
|
|
|
118
|
|
|
$image = wp_get_attachment_image_src( $attachment_id, $size ); |
|
119
|
|
|
|
|
120
|
|
|
return isset( $image['0'] ) ? $image['0'] : false; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|
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