|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/** |
|
|
|
|
|
|
3
|
|
|
* @package LSX_WETU_Importer_Banner_Integration |
|
4
|
|
|
* @author LightSpeed |
|
5
|
|
|
* @license GPL-2.0+ |
|
6
|
|
|
* @link |
|
7
|
|
|
* @copyright 2016 LightSpeed |
|
8
|
|
|
**/ |
|
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
class LSX_WETU_Importer_Banner_Integration extends LSX_WETU_Importer { |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* The url to list items from WETU |
|
14
|
|
|
* |
|
15
|
|
|
* @since 0.0.1 |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
public $tab_slug = 'banners'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Initialize the plugin by setting localization, filters, and administration functions. |
|
23
|
|
|
* |
|
24
|
|
|
* @since 1.0.0 |
|
25
|
|
|
* |
|
26
|
|
|
* @access private |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct() { |
|
|
|
|
|
|
29
|
|
|
add_action( 'wp_ajax_lsx_import_sync_banners', array( $this, 'sync_new_banner' ) ); |
|
30
|
|
|
add_action( 'wp_ajax_nopriv_lsx_import_sync_banners', array( $this, 'sync_new_banner' ) ); |
|
31
|
|
|
} |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Display the importer administration screen |
|
35
|
|
|
*/ |
|
36
|
|
|
public function display_page() { |
|
37
|
|
|
?> |
|
38
|
|
|
<div class="wrap"> |
|
39
|
|
|
<h2><?php esc_html_e( 'Download new banners straight from WETU', 'lsx-wetu-importer' ); ?></h2> |
|
40
|
|
|
|
|
41
|
|
|
<form method="get" action="" id="banners-filter"> |
|
42
|
|
|
<input type="hidden" name="post_type" class="post_type" value="<?php echo esc_attr( $this->tab_slug ); ?>" /> |
|
43
|
|
|
|
|
44
|
|
|
<div class="ajax-loader-small" style="display:none;width:100%;text-align:center;"> |
|
|
|
|
|
|
45
|
|
|
<img style="width:32px;" src="<?php echo esc_url( LSX_WETU_IMPORTER_URL . 'assets/images/ajaxloader.gif' ); ?>" /> |
|
46
|
|
|
</div> |
|
47
|
|
|
|
|
48
|
|
|
<table class="wp-list-table widefat fixed posts"> |
|
49
|
|
|
<thead> |
|
50
|
|
|
<tr> |
|
51
|
|
|
<th style="" class="manage-column column-cb check-column" id="cb" scope="col"> |
|
52
|
|
|
<label for="cb-select-all-1" class="screen-reader-text">Select All</label> |
|
53
|
|
|
<input type="checkbox" id="cb-select-all-1"> |
|
54
|
|
|
</th> |
|
55
|
|
|
<th style="width:15%" class="manage-column column-title " id="title" scope="col">Title</th> |
|
56
|
|
|
<th style="" class="manage-column column-date" id="date" scope="col">Images</th> |
|
57
|
|
|
</tr> |
|
58
|
|
|
</thead> |
|
59
|
|
|
|
|
60
|
|
|
<?php |
|
61
|
|
|
$accommodation_args = array( |
|
62
|
|
|
'post_type' => 'accommodation', |
|
63
|
|
|
'post_status' => array( 'publish', 'pending', 'draft', 'future', 'private' ), |
|
64
|
|
|
'nopagin' => 'true', |
|
65
|
|
|
'posts_per_page' => '1000', |
|
|
|
|
|
|
66
|
|
|
'meta_query' => array( |
|
|
|
|
|
|
67
|
|
|
'relation' => 'AND', |
|
68
|
|
|
array( |
|
69
|
|
|
'key' => 'lsx_wetu_id', |
|
70
|
|
|
'compare' => 'EXISTS', |
|
71
|
|
|
), |
|
72
|
|
|
array( |
|
73
|
|
|
'key' => 'image_group', |
|
74
|
|
|
'compare' => 'EXISTS', |
|
75
|
|
|
), |
|
76
|
|
|
array( |
|
77
|
|
|
'key' => 'image_group', |
|
78
|
|
|
'value' => 'a:1:{s:12:"banner_image";a:0:{}}', |
|
79
|
|
|
'compare' => '!=', |
|
80
|
|
|
), |
|
81
|
|
|
), |
|
82
|
|
|
); |
|
83
|
|
|
$accommodation = new WP_Query( $accommodation_args ); |
|
84
|
|
|
?> |
|
85
|
|
|
|
|
86
|
|
|
<tbody id="the-list"> |
|
87
|
|
|
<?php |
|
88
|
|
|
if ( $accommodation->have_posts() ) { |
|
|
|
|
|
|
89
|
|
|
while ( $accommodation->have_posts() ) { |
|
|
|
|
|
|
90
|
|
|
$accommodation->the_post(); |
|
91
|
|
|
?> |
|
92
|
|
|
<tr class="post-<?php the_ID(); ?> type-tour status-none" id="post-<?php the_ID(); ?>"> |
|
93
|
|
|
<?php |
|
94
|
|
|
$banner_size_appropriate = false; |
|
95
|
|
|
$min_width = '1920'; |
|
96
|
|
|
$min_height = '500'; |
|
97
|
|
|
|
|
98
|
|
|
$img_group = get_post_meta( get_the_ID(), 'image_group', true ); |
|
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
$thumbnails_html = false; |
|
101
|
|
|
|
|
102
|
|
|
if ( false !== $img_group ) { |
|
|
|
|
|
|
103
|
|
|
foreach ( $img_group['banner_image'] as $banner_image ) { |
|
|
|
|
|
|
104
|
|
|
$large = wp_get_attachment_image_src( $banner_image, 'full' ); |
|
105
|
|
|
$real_width = $large[1]; |
|
106
|
|
|
$real_height = $large[2]; |
|
107
|
|
|
|
|
108
|
|
|
$status = 'optimized'; |
|
109
|
|
|
if ( $real_width < intval( $real_width ) ) { |
|
|
|
|
|
|
110
|
|
|
$status = 'width not enough.'; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$thumbnail = wp_get_attachment_image_src( $banner_image, 'thumbnail' ); |
|
114
|
|
|
$thumbnails_html[] = ' |
|
115
|
|
|
<div style="display:block;float:left;"> |
|
116
|
|
|
<img src="' . $thumbnail[0] . '" /> |
|
117
|
|
|
<p style="text-align:center;">' . $real_width . 'px by ' . $real_height . 'px</p> |
|
118
|
|
|
</div>'; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
?> |
|
122
|
|
|
<th class="check-column" scope="row"> |
|
123
|
|
|
<label for="cb-select-<?php the_ID(); ?>" class="screen-reader-text"></label> |
|
124
|
|
|
<input type="checkbox" data-identifier="<?php the_ID(); ?>" value="<?php the_ID(); ?>" name="post[]" id="cb-select-<?php the_ID(); ?>"> |
|
125
|
|
|
</th> |
|
126
|
|
|
|
|
127
|
|
|
<td class="post-title page-title column-title"> |
|
128
|
|
|
<?php |
|
129
|
|
|
echo '<a href="' . esc_url( admin_url( '/post.php?post=' . get_the_ID() . '&action=edit' ) ) . '" target="_blank">'; |
|
|
|
|
|
|
130
|
|
|
the_title(); |
|
131
|
|
|
echo '</a>'; |
|
132
|
|
|
?> |
|
133
|
|
|
</td> |
|
134
|
|
|
|
|
135
|
|
|
<td colspan="2" class="thumbnails column-thumbnails"> |
|
136
|
|
|
<?php |
|
137
|
|
|
if ( false !== $thumbnails_html ) { |
|
|
|
|
|
|
138
|
|
|
echo wp_kses_post( implode( '', $thumbnails_html ) ); |
|
139
|
|
|
} else { |
|
140
|
|
|
echo '<p>There was an error retrieving your images.</p>'; |
|
141
|
|
|
} |
|
142
|
|
|
?> |
|
143
|
|
|
</td> |
|
144
|
|
|
</tr> |
|
145
|
|
|
<?php |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
?> |
|
149
|
|
|
</tbody> |
|
150
|
|
|
|
|
151
|
|
|
<tfoot> |
|
152
|
|
|
<tr> |
|
153
|
|
|
<th style="" class="manage-column column-cb check-column" id="cb" scope="col"> |
|
154
|
|
|
<label for="cb-select-all-1" class="screen-reader-text">Select All</label> |
|
155
|
|
|
<input type="checkbox" id="cb-select-all-1"> |
|
156
|
|
|
</th> |
|
157
|
|
|
<th style="width:15%;" class="manage-column column-title " id="title" scope="col">Title</th> |
|
158
|
|
|
<th style="" class="manage-column column-date" id="date" scope="col">Images</th> |
|
159
|
|
|
</tr> |
|
160
|
|
|
</tfoot> |
|
161
|
|
|
|
|
162
|
|
|
</table> |
|
163
|
|
|
|
|
164
|
|
|
<p><input class="button button-primary download" type="button" value="<?php esc_html_e( 'Download new Banners', 'lsx-wetu-importer' ); ?>" /> |
|
165
|
|
|
</p> |
|
166
|
|
|
</form> |
|
167
|
|
|
</div> |
|
168
|
|
|
<?php |
|
169
|
|
|
} |
|
|
|
|
|
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Creates the main gallery data |
|
173
|
|
|
*/ |
|
174
|
|
|
public function sync_new_banner() { |
|
175
|
|
|
check_ajax_referer( 'lsx_wetu_ajax_action', 'security' ); |
|
176
|
|
|
if ( isset( $_POST['action'] ) && 'lsx_import_sync_banners' === $_POST['action'] && isset( $_POST['post_id'] ) ) { |
|
|
|
|
|
|
177
|
|
|
|
|
178
|
|
|
$post_id = sanitize_text_field( $_POST['post_id'] ); |
|
|
|
|
|
|
179
|
|
|
$banners = get_post_meta( $post_id, 'image_group', true ); |
|
180
|
|
|
$this->wetu_id = get_post_meta( $post_id, 'lsx_wetu_id', true ); |
|
181
|
|
|
|
|
182
|
|
|
$new_banner_array = false; |
|
183
|
|
|
$array_index = 0; |
|
184
|
|
|
|
|
185
|
|
|
foreach ( $banners['banner_image'] as $banner_image ) { |
|
|
|
|
|
|
186
|
|
|
$image_id = $this->attach_external_image2( $this->format_wetu_url( $banner_image ), array(), $post_id ); |
|
187
|
|
|
if ( null !== $image_id && '' !== $image_id ) { |
|
|
|
|
|
|
188
|
|
|
$new_banner_array['banner_image'][ 'cmb-field-' . $array_index ] = $image_id; |
|
189
|
|
|
$array_index++; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
if ( false !== $new_banner_array ) { |
|
|
|
|
|
|
194
|
|
|
delete_post_meta( $post_id, 'image_group' ); |
|
195
|
|
|
add_post_meta( $post_id, 'image_group', $new_banner_array, true ); |
|
196
|
|
|
echo true; |
|
197
|
|
|
} else { |
|
198
|
|
|
echo false; |
|
199
|
|
|
} |
|
200
|
|
|
} else { |
|
201
|
|
|
echo false; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
die(); |
|
|
|
|
|
|
205
|
|
|
} |
|
|
|
|
|
|
206
|
|
|
|
|
207
|
|
|
/** |
|
|
|
|
|
|
208
|
|
|
* formats the url |
|
|
|
|
|
|
209
|
|
|
*/ |
|
210
|
|
|
public function format_wetu_url( $post_id ) { |
|
211
|
|
|
return 'https://wetu.com/ImageHandler/c1920x800/' . $this->wetu_id . '/' . $this->format_filename( $post_id ); |
|
|
|
|
|
|
212
|
|
|
} |
|
|
|
|
|
|
213
|
|
|
|
|
214
|
|
|
/** |
|
|
|
|
|
|
215
|
|
|
* formats the filename |
|
|
|
|
|
|
216
|
|
|
*/ |
|
217
|
|
|
public function format_filename( $post_id ) { |
|
218
|
|
|
$base = str_replace( '_', ' ', get_the_title( $post_id ) ); |
|
219
|
|
|
$base = rawurlencode( $base ); |
|
220
|
|
|
$type = get_post_mime_type( $post_id ); |
|
221
|
|
|
|
|
222
|
|
|
switch ( $type ) { |
|
|
|
|
|
|
223
|
|
|
case 'image/jpeg': |
|
224
|
|
|
return $base . '.jpg'; |
|
225
|
|
|
break; |
|
226
|
|
|
case 'image/png': |
|
227
|
|
|
return $base . '.png'; |
|
228
|
|
|
break; |
|
229
|
|
|
case 'image/gif': |
|
230
|
|
|
return $base . '.gif'; |
|
231
|
|
|
break; |
|
232
|
|
|
default: |
|
233
|
|
|
return false; |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
|
|
|
|
|
236
|
|
|
|
|
237
|
|
|
public function attach_external_image2( $url = null, $post_data = array(), $post_id = '' ) { |
|
|
|
|
|
|
238
|
|
|
if ( ! $url ) { |
|
|
|
|
|
|
239
|
|
|
return new WP_Error( 'missing', 'Need a valid URL' ); } |
|
|
|
|
|
|
240
|
|
|
$att_id = false; |
|
241
|
|
|
|
|
242
|
|
|
require_once ABSPATH . 'wp-admin/includes/file.php'; |
|
243
|
|
|
require_once ABSPATH . 'wp-admin/includes/media.php'; |
|
244
|
|
|
require_once ABSPATH . 'wp-admin/includes/image.php'; |
|
245
|
|
|
|
|
246
|
|
|
$tmp = tempnam( '/tmp', 'FOO' ); |
|
247
|
|
|
$image = wp_remote_get( $url ); |
|
248
|
|
|
|
|
249
|
|
|
if ( ! empty( $image ) && isset( $image['response'] ) && isset( $image['response']['code'] ) && 200 === $image['response']['code'] ) { |
|
|
|
|
|
|
250
|
|
|
file_put_contents( $tmp, $image['body'] ); |
|
|
|
|
|
|
251
|
|
|
chmod( $tmp, '777' ); |
|
252
|
|
|
|
|
253
|
|
|
preg_match( '/[^\?]+\.(tif|TIFF|jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG|pdf|PDF|bmp|BMP)/', $url, $matches ); |
|
254
|
|
|
$url_filename = basename( $matches[0] ); |
|
255
|
|
|
$url_filename = str_replace( '%20', '_', $url_filename ); |
|
256
|
|
|
// extract filename from url for title. |
|
257
|
|
|
$url_type = wp_check_filetype( $url_filename ); |
|
258
|
|
|
|
|
259
|
|
|
// assemble file data (should be built like $_FILES since wp_handle_sideload() will be using). |
|
260
|
|
|
$file_array['tmp_name'] = $tmp; |
|
261
|
|
|
|
|
262
|
|
|
if ( ! empty( $filename ) && ' ' != $filename ) { |
|
|
|
|
|
|
263
|
|
|
$file_array['name'] = $filename . '.' . $url_type['ext']; |
|
264
|
|
|
} else { |
|
265
|
|
|
$file_array['name'] = $url_filename; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
// set additional wp_posts columns. |
|
269
|
|
|
if ( empty( $post_data['post_title'] ) ) { |
|
|
|
|
|
|
270
|
|
|
$url_filename = str_replace( '%20', ' ', $url_filename ); |
|
271
|
|
|
$post_data['post_title'] = basename( $url_filename, '.' . $url_type['ext'] ); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
// make sure gets tied to parent. |
|
275
|
|
|
if ( empty( $post_data['post_parent'] ) ) { |
|
|
|
|
|
|
276
|
|
|
$post_data['post_parent'] = $post_id; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
// do the validation and storage stuff. |
|
280
|
|
|
$att_id = media_handle_sideload( $file_array, $post_id, null, $post_data ); |
|
281
|
|
|
|
|
282
|
|
|
// If error storing permanently, unlink. |
|
283
|
|
|
if ( is_wp_error( $att_id ) ) { |
|
|
|
|
|
|
284
|
|
|
unlink( $file_array['tmp_name'] ); |
|
285
|
|
|
return false; |
|
286
|
|
|
} |
|
287
|
|
|
} |
|
|
|
|
|
|
288
|
|
|
return $att_id; |
|
289
|
|
|
} |
|
|
|
|
|
|
290
|
|
|
} |
|
291
|
|
|
|