-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-reorder-entry-images.php
executable file
·531 lines (447 loc) · 14.8 KB
/
class-reorder-entry-images.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
<?php
/**
* Reorder Entry images
*
* @package ReorderEntryImages
* @author Vayu Robins <[email protected]>
* @license GPL-2.0+
* @link http://vayu.dk/reorder-entry-images/
* @copyright 2013 Vayu Robins
*/
/**
* Plugin class.
*
* @package ReorderEntryImages
* @author Vayu Robins <[email protected]>
*/
class ReorderEntryImages {
/**
* Plugin version, used for cache-busting of style and script file references.
*
* @since 1.0.0
*
* @var string
*/
protected $version = '1.0.8';
/**
* Unique identifier for your plugin.
*
* Use this value (not the variable name) as the text domain when internationalizing strings of text. It should
* match the Text Domain file header in the main plugin file.
*
* @since 1.0.0
*
* @var string
*/
protected $plugin_slug = 'reorder-entry-images';
/**
* Instance of this class.
*
* @since 1.0.0
*
* @var object
*/
protected static $instance = null;
/**
* Slug of the plugin screen.
*
* @since 1.0.0
*
* @var string
*/
protected $plugin_screen_hook_suffix = 'reorder-entry-images';
/**
* Entry type to add the metabox to.
*
* @since 1.0.0
*
* @var string
*/
protected $the_post_type = array();
/**
* Initialize the plugin by setting localization, filters, and administration functions.
*
* @since 1.0.0
*/
private function __construct() {
if( is_admin() ) :
// Load plugin text domain
add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
// Add the options page and menu item.
add_action( 'admin_menu', array( $this, 'add_plugin_admin_menu' ) );
// Register plugin settings.
add_action( 'admin_init', array( $this, 'register_plugin_settings' ) );
// Load admin style sheet and JavaScript.
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
// Get the post type from options settings and save in variable
$this->the_post_type = is_array( get_option( 'rei-options' ) ) ? get_option( 'rei-options' ) : $this->the_post_type;
// Add metabox on the proper metabox hook
if( is_array( $this->the_post_type ) ) {
foreach ( $this->the_post_type as $type ) {
add_action( 'add_meta_boxes_' . $type, array( $this, 'add_image_sortable_box' ) );
}
}
// Updates the attachments when saving
add_filter( 'wp_insert_post_data', array( $this, 'sort_images_meta_save' ), 99, 2 );
// This function is called with ajax, to get the products under the chosen category and display them in the form.
add_action( 'wp_ajax_delete_attachment', array( $this, 'delete_attachment' ) );
endif;
// Add list attached images shortcode
add_shortcode( 'list_attached_images', array( $this, 'list_attached_images_shortcode' ) );
}
/**
* Return an instance of this class.
*
* @since 1.0.0
*
* @return object A single instance of this class.
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Fired when the plugin is activated.
*
* @since 1.0.0
*
* @param boolean $network_wide True if WPMU superadmin uses "Network Activate" action, false if WPMU is disabled or plugin is activated on an individual blog.
*/
public static function activate( $network_wide ) {
if ( ! current_user_can( 'activate_plugins' ) ) { return; }
$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
check_admin_referer( "activate-plugin_{$plugin}" );
}
/**
* Fired when the plugin is deactivated.
*
* @since 1.0.0
*
* @param boolean $network_wide True if WPMU superadmin uses "Network Deactivate" action, false if WPMU is disabled or plugin is deactivated on an individual blog.
*/
public static function deactivate( $network_wide ) {
if ( ! current_user_can( 'activate_plugins' ) ) { return; }
$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
check_admin_referer( "deactivate-plugin_{$plugin}" );
}
/**
* Load the plugin text domain for translation.
*
* @since 1.0.0
*/
public function load_plugin_textdomain() {
$domain = $this->plugin_slug;
$locale = apply_filters( 'plugin_locale', get_locale(), $domain );
load_textdomain( $domain, WP_LANG_DIR . '/' . $domain . '/' . $domain . '-' . $locale . '.mo' );
load_plugin_textdomain( $domain, FALSE, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
}
/**
* Register and enqueue admin-specific style sheet.
*
* @since 1.0.0
*
* @return null Return early if no settings page is registered.
*/
public function enqueue_admin_styles() {
$screen = get_current_screen();
if ( in_array( $screen->id, $this->the_post_type ) ) {
wp_enqueue_style( $this->plugin_slug .'-admin-styles', plugins_url( 'css/admin.css', __FILE__ ), array(), $this->version );
}
}
/**
* Register and enqueue admin-specific JavaScript.
*
* @since 1.0.0
*
* @return null Return early if no settings page is registered.
*/
public function enqueue_admin_scripts() {
$screen = get_current_screen();
if ( in_array( $screen->id, $this->the_post_type ) ) {
wp_enqueue_script( $this->plugin_slug . '-admin-script', plugins_url( 'js/admin.js', __FILE__ ), array( 'jquery' ), $this->version );
}
}
/**
* Register the administration menu for this plugin into the WordPress Dashboard menu.
*
* @since 1.0.0
*/
public function add_plugin_admin_menu() {
$this->pagehook = add_options_page(
__( 'Reorder entry images', $this->plugin_slug ),
__( 'Reorder images', $this->plugin_slug ),
'manage_options',
$this->plugin_slug,
array( $this, 'display_plugin_admin_page' )
);
}
/**
* Render the settings page for this plugin.
*
* @since 1.0.0
*/
public function display_plugin_admin_page() {
include_once( 'views/admin.php' );
}
/**
* Register plugin settings
*
* @since 1.0.0
*/
public function register_plugin_settings() {
register_setting( 'rei_the_settings_group', 'rei-options' );
add_settings_section(
'rei-section-general-options',
__( 'Post type', 'reorder-entry-images' ),
array( $this, 'rei_general_settings_callback'),
$this->plugin_slug
);
add_settings_field(
'rei-posttype',
__( 'Post type', 'reorder-entry-images' ),
array( $this, 'rei_option_posttype'),
$this->plugin_slug,
'rei-section-general-options'
);
add_settings_field(
'rei-feature-image',
__( 'Feature image', 'reorder-entry-images' ),
array( $this, 'rei_option_feature_image'),
$this->plugin_slug,
'rei-section-general-options'
);
}
/**
* Render the settings section page for this plugin.
*
* @since 1.0.0
*/
public function rei_general_settings_callback() {
_e( 'Choose which post type you would like to use the reorder images functionality.', 'reorder-entry-images' );
}
/**
* Render the settings posttype field for this plugin.
*
* @since 1.0.0
*/
public function rei_option_posttype() {
$settings = (array) get_option( 'rei-options' );
foreach( self::getObjectTypes() as $type ) {
if( $type->name != 'attachment' ) {
echo '<label class="inline"><input type="checkbox" name="rei-options['.$type->name.']" value="'.esc_attr($type->name).'" '.checked( $type->name, isset( $settings[$type->name] ) ? $settings[$type->name] : 0, false ).' /> '.esc_html($type->label).'</label>';
}
}
?>
<span class="description"><?php _e( 'You can use this builtin-in or custom post types.', 'reorder-entry-images' ); ?></span>
<?php
}
/**
* Render the settings feature image field for this plugin.
*
* @since 1.0.4
*/
public function rei_option_feature_image() {
$settings = (array) get_option( 'rei-options' );
echo '<label class="inline"><input type="checkbox" name="rei-options[feature-image]" value="true" '.checked( 'true', $settings['feature-image'], false ).' />'.__( 'Yes, include feature image', 'reorder-entry-images' ).'</label>';
?>
<span class="description"><?php _e( 'Include/exclude feature image in the sort order list.', 'reorder-entry-images' ); ?></span>
<?php
}
/**
* Add a custom metabox to post, page or cpt, that displays the attachments in a list.
*
* @since 1.0.0
*/
public function add_image_sortable_box() {
$images = get_children(
array(
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'post_mime_type' => 'image'
)
);
if ( $images ) {
if( is_array( $this->the_post_type ) ) {
foreach( $this->the_post_type as $type ) {
add_meta_box(
'sort-entry-images',
__( 'Reorder images with drag & drop', 'reorder-entry-images' ),
array( $this, 'add_image_metabox_sorter' ),
$type,
'normal',
'default'
);
}
}
}
}
/**
* Gets all attachments and displays them in a sortable list on admin pages.
*
* @param array|object $p
* @since 1.0.0
*/
public function add_image_metabox_sorter( $p ) {
$settings = (array) get_option( 'rei-options' );
$thumb_id = $settings['feature-image'] ? 0 : get_post_thumbnail_id( get_the_ID() );
$args = array(
'order' => 'ASC',
'orderby' => 'menu_order',
'post_type' => 'attachment',
'post_parent' => get_the_ID(),
'post_mime_type' => array( 'image/jpeg', 'image/png', 'image/gif' ),
'post_status' => null,
'numberposts' => -1,
'exclude' => $thumb_id // Exclude featured thumbnail
);
$attachments = get_posts( $args );
if( $attachments ) :
wp_nonce_field( 'custom_images_sort', 'images_sort_nonce' ); ?>
<div class="imageuploader">
<div id="attachmentcontainer">
<?php $i = 0; foreach( $attachments as $attachment ) :
$editorimage = wp_get_attachment_image_src( intval( $attachment->ID ), 'thumbnail', false, false);
$i++;
?>
<div class="attachment" id="image-<?php echo intval( $attachment->ID ); ?>" aria-label="<?php echo esc_attr( $attachment->post_title ); ?>" data-id="<?php echo intval( $attachment->ID ); ?>">
<div class="image">
<a href="<?php echo esc_url( get_admin_url( '', 'post.php?post='. intval( $attachment->ID ) .'&action=edit' ) ); ?>" title="<?php echo esc_attr( $attachment->post_title ); ?>">
<img width="100" height="auto" src="<?php echo esc_url( $editorimage[0] ); ?>" />
</a>
<input type="hidden" name="att_id[]" id="att_id" value="<?php echo intval( $attachment->ID ); ?>" />
</div>
<div class="title"><?php echo esc_attr( $attachment->post_title ); ?></div>
<span class="number"><?php echo esc_attr( intval( $i ) ); ?></span>
<a class="button-link delete-attachment" data-name="remove" data-nonce="<?php echo wp_create_nonce( 'delete-attachment-nonce' ); ?>" href="#">Delete</a>
</div>
<?php endforeach; ?>
<div style="clear: both;"></div>
</div>
</div>
<?php
endif;
}
/**
* Delete attachment ajax function
*
*/
function delete_attachment() {
$nonce = $_GET['_ajax_nonce'];
// check to see if the submitted nonce matches with the generated nonce we created earlier
if ( ! wp_verify_nonce( $nonce, 'delete-attachment-nonce' ) ) { die ( 'Busted!'); }
$json_array = array();
$attachment_id = isset( $_GET['attachment_id'] ) ? absint( $_GET['attachment_id'] ) : '';
if( ! empty( $attachment_id ) ) {
if ( false === wp_delete_attachment( (int) $attachment_id ) ) {
$json_array['attachment_id'] = 0;
}else{
$json_array['attachment_id'] = (int) $attachment_id;
}
}
echo json_encode( $json_array );
// IMPORTANT: don't forget to "exit"
exit;
}
/**
* Saves the data to the post
*
* @param array $data Sinitized post data
* @param array $_post_vars Raw post data
* @return $data
* @since 1.0.0
*/
public function sort_images_meta_save( $data, $_post_vars ) {
$post_ID = $_post_vars['ID'];
if( !in_array( $data['post_type'], $this->the_post_type ) || !isset( $_post_vars['images_sort_nonce'] ) ) {
return $data;
}
if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return $data;
}
if( !wp_verify_nonce( $_post_vars['images_sort_nonce'], 'custom_images_sort' ) ) {
return $data;
}
if( !current_user_can( 'edit_post', $post_ID ) ) {
return $data;
}
if( isset( $_post_vars['att_id'] ) ) {
foreach( $_post_vars['att_id'] as $img_index => $img_id ) {
$a = array(
'ID' => $img_id,
'menu_order' => $img_index
);
wp_update_post( $a );
}
}
return $data;
}
/**
* Use to get post types
*
* @since 1.0.0
*
* @param string $key
* @return array|object
*/
private static function getObjectTypes( $key = '' ) {
// Get all post types registered.
$object_types = get_post_types( array('public' => true), 'objects' );
$object_types = apply_filters( 'rei-post-object-types', $object_types, $key );
if ( isset( $object_types[$key] ) ) {
return $object_types[$key];
}
return $object_types;
}
/**
* Add shortcode to show image attachements in a post or page.
*
* @since 1.0.1
*
* @param array|string $attr Shortcode attributes. Empty string if no attributes.
* @return string Shortcode output
*/
function list_attached_images_shortcode( $attr ) {
$defaults = array(
'imagesize' => 'thumbnail',
'numberimages' => 0,
'order' => 'desc',
'listclass' => 'list-images',
'before' => '<li class="image-item %s">',
'after' => '</li>',
'imagelink' => false,
'items_wrap' => '<ul id="list-attached-images" class="%1$s">%2$s</ul>',
);
/* Merge the input attributes and the defaults. */
extract( shortcode_atts( $defaults, $attr ) );
$wrap_class = $listclass ? $listclass : '';
$settings = (array) get_option( 'rei-options' );
$thumb_id = $settings['feature-image'] ? 0 : get_post_thumbnail_id( get_the_ID() );
$args = array(
'post_type' => 'attachment',
'post_mime_type' => array('image/jpeg', 'image/png', 'image/gif'),
'orderby' => 'menu_order',
'numberposts' => $numberimages,
'order' => $order,
'post_parent' => get_the_ID(),
'exclude' => $thumb_id // Exclude featured thumbnail
);
$attachments = get_posts($args);
$images_count = count( $attachments );
if ( $attachments ) :
foreach ( $attachments as $key => $attachment ) :
if( $imagelink == 'true' ) {
$link_before = sprintf( '<a href="%s" title="%s">', esc_attr( $attachment->guid ), esc_attr( $attachment->post_title ) );
$link_after = '</a>';
}
$last_child = $key % $images_count == ( $images_count-1 ) ? 'last-child' : '';
$items .= sprintf( $before, esc_attr( $last_child ) ) . $link_before . wp_get_attachment_image( intval( $attachment->ID ), $imagesize ) .$link_after . $after;
endforeach;
endif;
$output .= sprintf( $items_wrap, esc_attr( $wrap_class ), $items );
return apply_filters( 'rei_shortcode', $output, $attr );
}
}