forked from pontusab/WordPress-Post-Gallery-Class
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
371 lines (287 loc) · 9.18 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php
/*
Plugin Name: WordPress-Post-Gallery-Class
Plugin URI: https://github.com/pontusab/WordPress-Post-Gallery-Class
Version: 0.2
Author: Pontus Abrahamsson & Jonas Skoogh
*/
class WPgallery
{
const AJAX_ACTION = 'post_gallery_upload';
private $post_type;
private $_key;
public function __construct( $post_type, $_key = '_gallery' )
{
$this->post_type = $post_type;
$this->_key = $_key;
add_action( 'add_meta_boxes', array( &$this, 'add_meta_boxes' ) );
add_action( 'admin_enqueue_scripts', array( &$this, 'enqueue_scripts') );
add_action( 'wp_ajax_' . self::AJAX_ACTION , array( &$this, 'handle_upload' ) );
add_action( 'wp_ajax_remove_attachment', array( &$this, 'remove_attacment' ) );
add_action( 'wp_ajax_add_attachment', array( &$this, 'add_attachment' ) );
}
public static function upload_script()
{
global $post;
$plupload = array(
'runtimes' => 'html5,silverlight,flash,html4',
'browse_button' => 'plupload-browse-button',
'container' => 'plupload-upload-ui',
'drop_element' => 'drag-drop-area',
'file_data_name' => 'file-input',
//'multiple_queues' => true,
'url' => admin_url ('admin-ajax.php'),
'flash_swf_url' => includes_url( 'js/plupload/plupload.flash.swf' ),
'silverlight_xap_url' => includes_url( 'js/plupload/plupload.silverlight.xap' ),
'filters' => array(
'mime_types' => array( array( 'title' => __( 'Allowed Files' ), 'extensions' => '*' ) ),
'max_file_size' => wp_max_upload_size() . 'b'
),
'multipart' => true,
'urlstream_upload' => true,
// additional post data to send to our ajax hook
'multipart_params' => array(
'_ajax_nonce' => wp_create_nonce( 'photo-upload' ),
'action' => self::AJAX_ACTION, // the ajax action name,
'post_id' => $post->ID
),
);
return $plupload;
}
public function enqueue_scripts()
{
if($this->is_edit_screen())
{
wp_enqueue_script( 'plupload' );
wp_register_style( 'gallery-css', plugins_url( '/assets/css/gallery.css', __FILE__ ) );
wp_register_script( 'gallery-js', plugins_url( '/assets/js/gallery-min.js', __FILE__ ) );
wp_enqueue_script( 'jquery-ui-draggable' );
wp_enqueue_script( 'jquery-ui-sortable' );
wp_enqueue_style( 'gallery-css' );
wp_enqueue_script( 'gallery-js' );
?>
<script type="text/javascript">
var gallery_settings = <?php echo json_encode( self::upload_script() ); ?>;
</script>
<?php
}
}
public function add_meta_boxes()
{
if($this->is_edit_screen())
{
add_meta_box( 'gallery', __( 'Gallery' ), array( &$this, 'meta_data' ), $this->post_type, 'normal', 'low' );
}
}
private function is_edit_screen()
{
$screen = get_current_screen();
return $screen->post_type == $this->post_type;
}
public function meta_data()
{
global $post;
$attachments = get_post_meta( $post->ID, $this->_key, true );
$upploaded = get_posts( array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'exclude' => $attachments
));
if( !$attachments )
{
$attachments = array();
}
$output = '<div id="plupload-upload-ui" class="hide-if-no-js clearfix">';
$output .= '<div class="header">';
$output .= '<div class="media-router">';
$output .= '<a href="#" data-click="1" class="media-menu-item '.( empty( $attachments ) ? 'active' : null ).'">'. __('Upload Files') .'</a>';
$output .= '<a href="#" data-click="2" class="media-menu-item '.( !empty( $attachments ) ? 'active' : null ).'">'. __('Attached Images') .'</a>';
$output .= '<a href="#" data-click="3" class="media-menu-item">'. __('Media Library') .'</a>';
$output .= '</div>';
$output .= '</div>';
$output .= '<div id="drag-drop-area">';
// Start Upload
$output .= '<div data-panel="1" class="panel upload-wrap '.( empty( $attachments ) ? 'active' : null ).'">';
$output .= '<div class="drag-drop-inside">';
$output .= '<h3>'. __( 'Drop files anywhere to upload' ) .'</h3>';
$output .= '<p class="drag-drop-buttons">';
$output .= '<input id="plupload-browse-button" type="button" value="'. __('Select Files') .'" class="browser button button-hero" />';
$output .= '</p>';
$output .= '</div>';
$output .= '</div>';
// End Upload
// Start Delete
$output .= '<div data-panel="2" class="panel scrollable delete-wrap '.( !empty( $attachments ) ? 'active' : null ).'">';
$output .= '<ul>';
foreach( $attachments as $key => $value )
{
$output .= '<li data-id="'. $value .'">';
$output .= wp_get_attachment_image( $value, array( 120, 120 ) );
$output .= '</li>';
}
$output .= '</ul>';
$output .= '<div class="delete">';
$output .= '<a class="button button-primary button-large" href="#">'. __('Delete Image') .'</a>';
$output .= '</div>';
$output .= '</div>';
// End Delete
// Start add
$output .= '<div data-panel="3" class="panel scrollable add-wrap">';
$output .= '<ul>';
foreach( $upploaded as $image )
{
$output .= '<li data-id="'. $image->ID .'">';
$output .= wp_get_attachment_image( $image->ID, array( 120, 120 ) );
$output .= '</li>';
}
$output .= '</ul>';
$output .= '<div class="add">';
$output .= '<a class="button button-primary button-large" href="#">'. __('Attatch Image(s)') .'</a>';
$output .= '</div>';
$output .= '</div>';
// End add
$output .= '</div>';
$output .= '</div>';
echo $output;
}
public function remove_attacment()
{
$post_id = $_REQUEST['post_id'];
$attachments = isset( $_POST['ids'] ) ? $_POST['ids'] : null;
$saved = get_post_meta( $post_id, $this->_key, true );
if( count( $attachments ) > 0 )
{
$attachments = explode( ',', $attachments );
foreach( $attachments as $attachment )
{
unset( $saved[ array_search( $attachment, $saved ) ] );
}
if( count( $saved ) > 0 )
{
$this->update_meta( $post_id, $this->_key, $saved );
}
else
{
$this->delete_meta( $post_id, $this->_key );
}
}
exit;
}
static public function images( $size, $li = true )
{
global $post;
$attachments = get_post_meta( $post->ID, $this->_key, true );
if( !$attachments )
{
$attachments = array();
}
$output = '';
if( $li )
{
foreach( $attachments as $attachment )
{
$output .= '<li>';
$output .= wp_get_attachment_image( $attachment, $size );
$output .= '</li>';
}
}
return $output;
}
public function update_meta( $post_id, $key, $saved )
{
if( class_exists('Polylang_Base') )
{
global $polylang;
foreach( $polylang->get_languages_list() as $language )
{
$id = $polylang->get_translation( 'post', $post_id, $language );
update_post_meta( $id, $key, $saved );
}
}
else
{
update_post_meta( $post_id, $key, $saved );
}
}
public function delete_meta( $post_id, $key )
{
if( class_exists('Polylang_Base') )
{
global $polylang;
foreach( $polylang->get_languages_list() as $language )
{
$id = $polylang->get_translation( 'post', $post_id, $language );
delete_post_meta( $id, $key );
}
}
else
{
delete_post_meta( $post_id, $key );
}
}
public function add_attachment()
{
$order = isset( $_POST['order'] ) ? $_POST['order'] : null;
$post_id = $_REQUEST['post_id'];
$saved = get_post_meta( $post_id, $this->_key, true );
if( !$saved )
{
$saved = array();
}
$attachments = isset( $_POST['ids'] ) ? $_POST['ids'] : null;
if( count( $order ) > 0 )
{
$this->update_meta( $post_id, $this->_key, $order );
}
else
{
if( $attachments )
{
$attachments = explode( ',', $attachments );
foreach( $attachments as $attachment )
{
array_unshift( $saved, $attachment );
}
}
if( count( $saved ) > 0 )
{
$this->update_meta( $post_id, $this->_key, $saved );
}
else
{
$this->delete_meta( $post_id, $this->_key );
}
}
exit;
}
public function handle_upload()
{
$post_id = $_REQUEST['post_id'];
$saved = get_post_meta( $post_id, $this->_key, true );
if( empty( $saved ) )
{
$saved = array();
}
$file = $_FILES['file-input'];
$file_attr = wp_handle_upload( $file, array(
'test_form' => false,
'action' => self::AJAX_ACTION
));
$attachment = array(
'post_mime_type' => $file_attr['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file['name'] ) ),
'post_content' => '',
'post_status' => 'inherit'
);
// Adds file as attachment to WordPress
$id = wp_insert_attachment( $attachment, $file_attr['file'], $post_id );
if( !is_wp_error( $id ) )
{
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file_attr['file'] ) );
array_unshift( $saved, $id );
$this->update_meta( $post_id, $this->_key, $saved );
echo $file_attr['url'];
}
exit;
}
}