forked from devinsays/options-framework-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions-medialibrary-uploader.php
291 lines (226 loc) · 8.87 KB
/
options-medialibrary-uploader.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
<?php
/**
* WooThemes Media Library-driven AJAX File Uploader Module (2010-11-05)
*
* Slightly modified for use in the Options Framework.
*/
if ( is_admin() ) {
// Load additional css and js for image uploads on the Options Framework page
$of_page= 'appearance_page_options-framework';
add_action( "admin_print_styles-$of_page", 'optionsframework_mlu_css', 0 );
add_action( "admin_print_scripts-$of_page", 'optionsframework_mlu_js', 0 );
}
/**
* Sets up a custom post type to attach image to. This allows us to have
* individual galleries for different uploaders.
*/
if ( ! function_exists( 'optionsframework_mlu_init' ) ) {
function optionsframework_mlu_init () {
register_post_type( 'optionsframework', array(
'labels' => array(
'name' => __( 'Options Framework Internal Container' ),
),
'public' => true,
'show_ui' => false,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => false,
'supports' => array( 'title', 'editor' ),
'query_var' => false,
'can_export' => true,
'show_in_nav_menus' => false
) );
}
}
/**
* Adds the Thickbox CSS file and specific loading and button images to the header
* on the pages where this function is called.
*/
if ( ! function_exists( 'optionsframework_mlu_css' ) ) {
function optionsframework_mlu_css () {
$_html = '';
$_html .= '<link rel="stylesheet" href="' . site_url() . '/' . WPINC . '/js/thickbox/thickbox.css" type="text/css" media="screen" />' . "\n";
$_html .= '<script type="text/javascript">
var tb_pathToImage = "' . site_url() . '/' . WPINC . '/js/thickbox/loadingAnimation.gif";
var tb_closeImage = "' . site_url() . '/' . WPINC . '/js/thickbox/tb-close.png";
</script>' . "\n";
echo $_html;
}
}
/**
* Registers and enqueues (loads) the necessary JavaScript file for working with the
* Media Library-driven AJAX File Uploader Module.
*/
if ( ! function_exists( 'optionsframework_mlu_js' ) ) {
function optionsframework_mlu_js () {
// Registers custom scripts for the Media Library AJAX uploader.
wp_register_script( 'of-medialibrary-uploader', OPTIONS_FRAMEWORK_URL .'js/of-medialibrary-uploader.js', array( 'jquery', 'thickbox' ) );
wp_enqueue_script( 'of-medialibrary-uploader' );
wp_enqueue_script( 'media-upload' );
}
}
/**
* Media Uploader Using the WordPress Media Library.
*
* Parameters:
* - string $_id - A token to identify this field (the name).
* - string $_value - The value of the field, if present.
* - string $_mode - The display mode of the field.
* - string $_desc - An optional description of the field.
* - int $_postid - An optional post id (used in the meta boxes).
*
* Dependencies:
* - optionsframework_mlu_get_silentpost()
*/
if ( ! function_exists( 'optionsframework_medialibrary_uploader' ) ) {
function optionsframework_medialibrary_uploader( $_id, $_value, $_mode = 'full', $_desc = '', $_postid = 0, $_name = '') {
$optionsframework_settings = get_option('optionsframework');
// Gets the unique option id
$option_name = $optionsframework_settings['id'];
$output = '';
$id = '';
$class = '';
$int = '';
$value = '';
$name = '';
$id = strip_tags( strtolower( $_id ) );
// Change for each field, using a "silent" post. If no post is present, one will be created.
$int = optionsframework_mlu_get_silentpost( $id );
// If a value is passed and we don't have a stored value, use the value that's passed through.
if ( $_value != '' && $value == '' ) {
$value = $_value;
}
if ($_name != '') {
$name = $option_name.'['.$id.']['.$_name.']';
}
else {
$name = $option_name.'['.$id.']';
}
if ( $value ) { $class = ' has-file'; }
$output .= '<input id="' . $id . '" class="upload' . $class . '" type="text" name="'.$name.'" value="' . $value . '" />' . "\n";
$output .= '<input id="upload_' . $id . '" class="upload_button button" type="button" value="' . __( 'Upload' ) . '" rel="' . $int . '" />' . "\n";
if ( $_desc != '' ) {
$output .= '<span class="of_metabox_desc">' . $_desc . '</span>' . "\n";
}
$output .= '<div class="screenshot" id="' . $id . '_image">' . "\n";
if ( $value != '' ) {
$remove = '<a href="javascript:(void);" class="mlu_remove button">Remove</a>';
$image = preg_match( '/(^.*\.jpg|jpeg|png|gif|ico*)/i', $value );
if ( $image ) {
$output .= '<img src="' . $value . '" alt="" />'.$remove.'';
} else {
$parts = explode( "/", $value );
for( $i = 0; $i < sizeof( $parts ); ++$i ) {
$title = $parts[$i];
}
// No output preview if it's not an image.
$output .= '';
// Standard generic output if it's not an image.
$title = __( 'View File', 'optionsframework' );
$output .= '<div class="no_image"><span class="file_link"><a href="' . $value . '" target="_blank" rel="external">'.$title.'</a></span>' . $remove . '</div>';
}
}
$output .= '</div>' . "\n";
return $output;
}
}
/**
* Uses "silent" posts in the database to store relationships for images.
* This also creates the facility to collect galleries of, for example, logo images.
*
* Return: $_postid.
*
* If no "silent" post is present, one will be created with the type "optionsframework"
* and the post_name of "of-$_token".
*
* Example Usage:
* optionsframework_mlu_get_silentpost ( 'of_logo' );
*/
if ( ! function_exists( 'optionsframework_mlu_get_silentpost' ) ) {
function optionsframework_mlu_get_silentpost ( $_token ) {
global $wpdb;
$_id = 0;
// Check if the token is valid against a whitelist.
// $_whitelist = array( 'of_logo', 'of_custom_favicon', 'of_ad_top_image' );
// Sanitise the token.
$_token = strtolower( str_replace( ' ', '_', $_token ) );
// if ( in_array( $_token, $_whitelist ) ) {
if ( $_token ) {
// Tell the function what to look for in a post.
$_args = array( 'post_type' => 'optionsframework', 'post_name' => 'of-' . $_token, 'post_status' => 'draft', 'comment_status' => 'closed', 'ping_status' => 'closed' );
// Look in the database for a "silent" post that meets our criteria.
$query = 'SELECT ID FROM ' . $wpdb->posts . ' WHERE post_parent = 0';
foreach ( $_args as $k => $v ) {
$query .= ' AND ' . $k . ' = "' . $v . '"';
} // End FOREACH Loop
$query .= ' LIMIT 1';
$_posts = $wpdb->get_row( $query );
// If we've got a post, loop through and get it's ID.
if ( count( $_posts ) ) {
$_id = $_posts->ID;
} else {
// If no post is present, insert one.
// Prepare some additional data to go with the post insertion.
$_words = explode( '_', $_token );
$_title = join( ' ', $_words );
$_title = ucwords( $_title );
$_post_data = array( 'post_title' => $_title );
$_post_data = array_merge( $_post_data, $_args );
$_id = wp_insert_post( $_post_data );
}
}
return $_id;
}
}
/**
* Trigger code inside the Media Library popup.
*/
if ( ! function_exists( 'optionsframework_mlu_insidepopup' ) ) {
function optionsframework_mlu_insidepopup () {
if ( isset( $_REQUEST['is_optionsframework'] ) && $_REQUEST['is_optionsframework'] == 'yes' ) {
add_action( 'admin_head', 'optionsframework_mlu_js_popup' );
add_filter( 'media_upload_tabs', 'optionsframework_mlu_modify_tabs' );
}
}
}
if ( ! function_exists( 'optionsframework_mlu_js_popup' ) ) {
function optionsframework_mlu_js_popup () {
$_of_title = $_REQUEST['of_title'];
if ( ! $_of_title ) { $_of_title = 'file'; } // End IF Statement
?>
<script type="text/javascript">
<!--
jQuery(function($) {
jQuery.noConflict();
// Change the title of each tab to use the custom title text instead of "Media File".
$( 'h3.media-title' ).each ( function () {
var current_title = $( this ).html();
var new_title = current_title.replace( 'media file', '<?php echo $_of_title; ?>' );
$( this ).html( new_title );
} );
// Change the text of the "Insert into Post" buttons to read "Use this File".
$( '.savesend input.button[value*="Insert into Post"], .media-item #go_button' ).attr( 'value', 'Use this File' );
// Hide the "Insert Gallery" settings box on the "Gallery" tab.
$( 'div#gallery-settings' ).hide();
// Preserve the "is_optionsframework" parameter on the "delete" confirmation button.
$( '.savesend a.del-link' ).click ( function () {
var continueButton = $( this ).next( '.del-attachment' ).children( 'a.button[id*="del"]' );
var continueHref = continueButton.attr( 'href' );
continueHref = continueHref + '&is_optionsframework=yes';
continueButton.attr( 'href', continueHref );
} );
});
-->
</script>
<?php
}
}
/**
* Triggered inside the Media Library popup to modify the title of the "Gallery" tab.
*/
if ( ! function_exists( 'optionsframework_mlu_modify_tabs' ) ) {
function optionsframework_mlu_modify_tabs ( $tabs ) {
$tabs['gallery'] = str_replace( __( 'Gallery', 'optionsframework' ), __( 'Previously Uploaded', 'optionsframework' ), $tabs['gallery'] );
return $tabs;
}
}