-
Notifications
You must be signed in to change notification settings - Fork 0
/
Image_Upload_Helper.php
170 lines (155 loc) · 5.37 KB
/
Image_Upload_Helper.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
<?php
class Image_Upload_Helper {
const TEXT_DOMAIN = 'Image_Upload_Helper';
const VERSION = 1.4;
public static function init() {
add_action('admin_enqueue_scripts', array(__CLASS__, 'register_scripts'), 0, 1);
add_action('wp_ajax_image_upload_helper_image', array(__CLASS__, 'print_image_html'), 10, 0);
add_filter('get_media_item_args', array(__CLASS__, 'filter_media_item_args'), 10, 1);
}
public static function register_scripts( $context = '') {
wp_register_script('image-upload-helper', self::plugin_url('resources/admin-scripts.js'), array('jquery', 'thickbox', 'media-upload'), self::VERSION, TRUE);
wp_register_script('image-upload-helper-popup', self::plugin_url('resources/media-upload-popup.js'), array('jquery'), self::VERSION, TRUE);
add_action('admin_print_scripts', array(__CLASS__, 'print_js_settings'), 0, 0);
if ( $context == 'media-upload-popup' ) {
if ( isset($_REQUEST['image-upload-helper']) ) {
wp_enqueue_script('image-upload-helper-popup');
}
} else {
wp_enqueue_script('image-upload-helper');
wp_enqueue_style('thickbox');
}
}
public static function print_js_settings() {
$short_label = !empty($_REQUEST['image-upload-helper'])?$_REQUEST['image-upload-helper']:self::__('thumbnail image');
$label = sprintf(self::__('Use as %s'), $short_label);
echo '<script type="text/javascript">';
echo 'ImageUploadHelper = {
label: "'.$label.'",
short_label: "'.$short_label.'"
};';
echo '</script>';
}
public static function print_image_html() {
global $post_ID;
$helper = new self();
$args = array();
foreach ( array('thumbnail_id', 'label', 'size', 'field_name', 'type') as $arg ) {
if ( !empty($_REQUEST[$arg]) ) {
$args[$arg] = $_REQUEST[$arg];
}
}
if ( empty($args['thumbnail_id']) ) {
status_header(404);
exit();
}
if ( !empty($_REQUEST['post_ID']) ) {
$post_ID = $_REQUEST['post_ID'];
}
echo $helper->thumbnail_html($args);
exit();
}
public static function filter_media_item_args( $args ) {
if ( !empty($_REQUEST['image-upload-helper']) ) {
$args['send'] = TRUE;
}
return $args;
}
/**
* Return $string after translating it with the plugin's text domain
* @static
* @param string $string
* @return string|void
*/
protected static function __( $string ) {
return __( $string, self::TEXT_DOMAIN );
}
/**
* Echo $string after translating it with the plugin's text domain
*
* @static
* @param string $string
* @return void
*/
protected static function _e( $string ) {
_e( $string, self::TEXT_DOMAIN );
}
/**
* Get the absolute system path to the plugin directory, or a file therein
* @static
* @param string $path
* @return string
*/
protected static function plugin_path( $path ) {
$base = dirname( __FILE__ );
if ( $path ) {
return trailingslashit( $base ) . $path;
} else {
return untrailingslashit( $base );
}
}
/**
* Get the absolute URL to the plugin directory, or a file therein
* @static
* @param string $path
* @return string
*/
protected static function plugin_url( $path ) {
return plugins_url( $path, __FILE__ );
}
public function __construct() {
self::init();
}
/**
* Output HTML for the image helper.
*
* @since 2.9.0
*
* @param array $args
* @return string html
*/
public function thumbnail_html( $args = array() ) {
global $content_width;
$defaults = array(
'label' => 'thumbnail image',
'thumbnail_id' => NULL,
'size' => 'thumbnail',
'field_name' => 'image-upload-helper',
'type' => 'image',
);
$args = wp_parse_args($args, $defaults);
/**
* @var string $thumbnail_id
* @var string $label
* @var string $size
* @var string $field_name
* @var string $type
*/
extract($args);
// hack to make sure our query args get added before TB_iframe so they get passed on
$url = remove_query_arg('TB_iframe', get_upload_iframe_src($type));
$url = add_query_arg(array('image-upload-helper' => urlencode($label)), $url);
$url = add_query_arg(array('TB_iframe' => true), $url);
$set_thumbnail_link = '<p class="hide-if-no-js"><a title="' . esc_attr__( sprintf(self::__('Set %s'), $label) ) . '" href="' . esc_url( $url ) . '" class="thickbox image-upload-helper-set">%s</a></p>';
$content = sprintf($set_thumbnail_link, sprintf(self::__('Set <span class="image-upload-helper-label">%s</span>'), esc_html($label)) );
if ( $thumbnail_id && get_post( $thumbnail_id ) ) {
if ( $type == 'image' ) {
$old_content_width = $content_width;
$content_width = 266;
$thumbnail_html = wp_get_attachment_image( $thumbnail_id, $size );
if ( !empty( $thumbnail_html ) ) {
$content = sprintf($set_thumbnail_link, $thumbnail_html);
}
$content_width = $old_content_width;
} else {
$attachment_url = wp_get_attachment_url($thumbnail_id);
$filename = basename($attachment_url);
$content = sprintf($set_thumbnail_link, '<span>'.$filename.'</span>');
}
$content .= '<p class="hide-if-no-js"><a href="#" class="image-upload-helper-remove">' . sprintf(self::__('Remove <span class="image-upload-helper-label">%s</span>'), esc_html($label)) . '</a></p>';
}
$content .= sprintf('<input type="hidden" name="%s" value="%d" class="image-upload-helper-input" data-type="%s" data-size="%s" />', $field_name, $thumbnail_id, $type, $size);
$content = '<div class="image-upload-helper">'.$content.'</div>';
return apply_filters( 'image_upload_helper_html', $content );
}
}