|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Wordlift\Images_Licenses; |
|
7
|
|
|
|
|
8
|
|
|
use Wordlift\Api\Api_Service; |
|
9
|
|
|
|
|
10
|
|
|
class Image_License_Service { |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var Api_Service |
|
14
|
|
|
*/ |
|
15
|
|
|
private $api_service; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var Image_License_Factory |
|
19
|
|
|
*/ |
|
20
|
|
|
private $image_license_factory; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Images_Licenses_Service constructor. |
|
24
|
|
|
* |
|
25
|
|
|
* @param Api_Service $api_service |
|
26
|
|
|
* @param Image_License_Factory $image_license_factory |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct( $api_service, $image_license_factory ) { |
|
29
|
|
|
|
|
30
|
|
|
$this->api_service = $api_service; |
|
31
|
|
|
$this->image_license_factory = $image_license_factory; |
|
32
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @return array |
|
37
|
|
|
*/ |
|
38
|
|
|
public function get_non_public_domain_images() { |
|
39
|
|
|
|
|
40
|
|
|
$response = $this->api_service->get( '/images/GetNonPublicDomainImages', array(), null, 300 ); |
|
41
|
|
|
$response_body = $response->get_body(); |
|
42
|
|
|
|
|
43
|
|
|
if ( empty( $response_body ) ) { |
|
44
|
|
|
return array(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$json_data = json_decode( $response_body, true ); |
|
48
|
|
|
|
|
49
|
|
|
return apply_filters( |
|
50
|
|
|
'wl_post_get_non_public_domain_images', |
|
51
|
|
|
$this->post_process_response_body( $json_data ) |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param $json_data |
|
57
|
|
|
* |
|
58
|
|
|
* @return Image_License_Factory[] |
|
59
|
|
|
*/ |
|
60
|
|
|
private function post_process_response_body( $json_data ) { |
|
61
|
|
|
global $wpdb; |
|
62
|
|
|
|
|
63
|
|
|
$return_data = array(); |
|
64
|
|
|
|
|
65
|
|
|
foreach ( $json_data as $raw_image ) { |
|
66
|
|
|
$filename = $raw_image['filename']; |
|
67
|
|
|
$more_info_link = sprintf( 'https://commons.wikimedia.org/wiki/File:%s', $filename ); |
|
68
|
|
|
|
|
69
|
|
|
$sql = |
|
70
|
|
|
" |
|
71
|
|
|
SELECT pm1.post_id, pm1.meta_value |
|
72
|
|
|
FROM {$wpdb->postmeta} pm1 |
|
73
|
|
|
LEFT OUTER JOIN {$wpdb->postmeta} pm2 |
|
74
|
|
|
ON pm2.post_id = pm1.post_id |
|
75
|
|
|
AND pm2.meta_key = %s |
|
76
|
|
|
WHERE pm1.meta_key = %s |
|
77
|
|
|
AND pm1.meta_value LIKE %s |
|
78
|
|
|
AND pm2.meta_value IS NULL |
|
79
|
|
|
"; |
|
80
|
|
|
|
|
81
|
|
|
$attachments = $wpdb->get_results( $wpdb->prepare( |
|
82
|
|
|
$sql, |
|
83
|
|
|
'_wl_image_license_fixed', |
|
84
|
|
|
'_wp_attached_file', |
|
85
|
|
|
'wl/%' . $wpdb->esc_like( $filename ) |
|
86
|
|
|
) ); |
|
87
|
|
|
|
|
88
|
|
|
// Get tha attachments' post IDs. |
|
89
|
|
|
foreach ( $attachments as $attachment ) { |
|
90
|
|
|
$post_id = (int) $attachment->post_id; |
|
91
|
|
|
|
|
92
|
|
|
$image_license = $this->image_license_factory->create( $post_id, $raw_image, $more_info_link ); |
|
93
|
|
|
|
|
94
|
|
|
// Get the posts referencing the attachments as featured image_license. |
|
95
|
|
|
$image_license['posts_ids_as_featured_image'] = $this->get_posts_ids_as_featured_image( $post_id ); |
|
96
|
|
|
|
|
97
|
|
|
$filename = $attachment->meta_value; |
|
98
|
|
|
|
|
99
|
|
|
$image_license['posts_ids_as_embed'] = $this->get_posts_ids_as_embed( $filename ); |
|
100
|
|
|
|
|
101
|
|
|
$return_data[] = $image_license; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
}; |
|
105
|
|
|
|
|
106
|
|
|
return $return_data; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param int $id |
|
111
|
|
|
* |
|
112
|
|
|
* @return array |
|
113
|
|
|
*/ |
|
114
|
|
|
private function get_posts_ids_as_featured_image( $id ) { |
|
115
|
|
|
|
|
116
|
|
|
// Bail out if there are no attachments post IDs. |
|
117
|
|
|
if ( empty( $id ) ) { |
|
118
|
|
|
return array(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
global $wpdb; |
|
122
|
|
|
|
|
123
|
|
|
return array_map( 'intval', $wpdb->get_col( $wpdb->prepare( |
|
124
|
|
|
" |
|
125
|
|
|
SELECT post_id FROM {$wpdb->postmeta} |
|
126
|
|
|
WHERE meta_key = %s |
|
127
|
|
|
AND meta_value = %d |
|
128
|
|
|
", |
|
129
|
|
|
'_thumbnail_id', |
|
130
|
|
|
$id |
|
131
|
|
|
) ) ); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param string $filename |
|
136
|
|
|
* |
|
137
|
|
|
* @return array |
|
138
|
|
|
*/ |
|
139
|
|
|
private function get_posts_ids_as_embed( $filename ) { |
|
140
|
|
|
|
|
141
|
|
|
if ( empty( $filename ) ) { |
|
142
|
|
|
return array(); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
global $wpdb; |
|
146
|
|
|
|
|
147
|
|
|
return array_map( 'intval', $wpdb->get_col( $wpdb->prepare( |
|
148
|
|
|
" |
|
149
|
|
|
SELECT ID FROM {$wpdb->posts} |
|
150
|
|
|
WHERE post_content LIKE %s |
|
151
|
|
|
AND post_type = %s |
|
152
|
|
|
AND post_status = %s |
|
153
|
|
|
", |
|
154
|
|
|
'%/' . $wpdb->esc_like( $filename ) . '"%', |
|
155
|
|
|
'post', |
|
156
|
|
|
'publish' |
|
157
|
|
|
) ) ); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
} |
|
161
|
|
|
|