PageRenderTime 48ms CodeModel.GetById 29ms app.highlight 15ms RepoModel.GetById 0ms app.codeStats 1ms

/inc/metaboxes/tumblog-meta.php

https://github.com/envex/Micro-Theme
PHP | 468 lines | 300 code | 56 blank | 112 comment | 34 complexity | 77bf7d3079bd8670c322724c310181fe MD5 | raw file
  1<?php
  2/**
  3 * Theme Tumblog Metabox Functions file
  4 * 
  5 * The /inc/metaboxes/tumblog-meta.php file includes
  6 * the Theme's Tumblog metabox functions
  7 * 
  8 * @link 		http://codex.wordpress.org/Function_Reference/add_action 	add_action()
  9 * 
 10 * @package 	Micro
 11 * @copyright	Copyright (c) 2011, UpThemes
 12 * @license		http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License, v2 (or newer)
 13 *
 14 * @since 		Micro 1.0
 15 */
 16
 17/**
 18 * Customize Meta Boxes on Post Edit Screen
 19 * 
 20 * Adds the Tumblog metabox to the post-edit
 21 * screen, to allow entry of Tumblog-style 
 22 * post content based on selected Post Format 
 23 * type.
 24 * 
 25 * Removes the standard custom field metabox from 
 26 * the post-edit screen, since custom post meta 
 27 * data will be added via the Tumblog metabox.
 28 * 
 29 * @link 	http://codex.wordpress.org/Function_Reference/add_meta_box 			add_meta_box()
 30 * @link 	http://codex.wordpress.org/Function_Reference/remove_meta_box 		remove_meta_box()
 31 * 
 32 * @param	none
 33 * @return	none
 34 * 
 35 * @since	Micro 1.0
 36 * 
 37 */
 38function tumblog_customize_meta_boxes() {
 39	
 40	// Remove 'postcustom' metabox
 41	remove_meta_box( 'postcustom' , 'post' , 'normal' ); 
 42	// Add Tumblog metabox
 43	add_meta_box( 'tumblog', __( 'Tumblog Options', 'micro' ), 'tumblog_meta_box', 'post', 'normal', 'high' );
 44}
 45// Hook tumblog_add_custom_box() into admin_init action
 46add_action( 'add_meta_boxes_post', 'tumblog_customize_meta_boxes' );
 47
 48/**
 49 * Enqueue Tumblog Scripts
 50 * 
 51 * @link 	http://codex.wordpress.org/Function_Reference/wp_enqueue_script 	wp_enqueue_script()
 52 * 
 53 * @param	none
 54 * @return	none
 55 * 
 56 * @since	Micro 1.0
 57 * 
 58 */
 59function tumblog_enqueue_scripts() {
 60	
 61	global $pagenow;
 62	if ( 'post.php' == $pagenow || 'post-new.php' == $pagenow ) {
 63		$upthemes =  THEME_DIR.'/admin/';
 64		wp_enqueue_script( 'ajaxupload', $upthemes."js/ajaxupload.js", array( 'jquery' ) );
 65	}
 66
 67}
 68// Hook tumblog_enqueue_scripts() into admin_init action
 69add_action( 'admin_enqueue_scripts', 'tumblog_enqueue_scripts' );
 70
 71/**
 72 * Define Markup for the Tumblog Metabox
 73 * 
 74 * Defines the markup for the Tumblog metabox,
 75 * including specific input fields based on the 
 76 * selected Post Format type.
 77 * 
 78 * @link 	http://codex.wordpress.org/Function_Reference/get_post_meta 	get_post_meta()
 79 * @link 	http://codex.wordpress.org/Function_Reference/wp_nonce_field	wp_nonce_field()
 80 * 
 81 * @param	none
 82 * @return	string	Markup for post-edit screen metabox
 83 * 
 84 * @since	Micro 1.0
 85 * 
 86 */
 87function tumblog_meta_box() {
 88
 89	// Use nonce for verification
 90	wp_nonce_field( 'tumblog', 'tumblog-boxes' );
 91
 92	global $post;
 93	
 94	$metaboxes = array(
 95	
 96	    "image" => array (
 97	        "name" => "image",
 98	        "label" => "Image",
 99	        "type" => "image",
100	        "desc" => "Enter path to image file here."
101	    ),
102		 "gallery" => array (
103	        "name" => "gallery",
104	        "label" => "Gallery Style",
105	        "type" => "select",
106			"options" => array(
107				"Slider" => 'slider',
108				"Grid" => 'grid',
109				"Full-Width List" => 'list'
110			),
111	        "desc" => "Enter path to image file here."
112	    ),
113	    "video-embed" => array (
114	        "name" => "video-embed",
115	        "label" => "Embed Code (Videos)",
116	        "type" => "textarea",
117	        "desc" => "Add embed code for video services like Youtube or Vimeo"
118	    ),
119            "video-url" => array (
120	        "name" => "video-url",
121	        "label" => "Video URL (Vimeo or Youtube)",
122	        "type" => "text",
123	        "desc" => "Add a video URL."
124	    ),
125	    "quote-author" => array (
126	        "name"  => "quote-author",
127	        "std"  => "Unknown",
128	        "label" => "Quote Author",
129	        "type" => "text",
130	        "desc" => "Enter the name of the quote author."
131	    ),
132	    "quote-url" => array (
133	        "name"  => "quote-url",
134	        "std"  => "http://",
135	        "label" => "Link to Quote",
136	        "type" => "text",
137	        "desc" => "Enter the url/web address of the quote if available."
138	    ),
139	    "quote-copy" => array (
140	        "name"  => "quote-copy",
141	        "std"  => "Unknown",
142	        "label" => "Quote",
143	        "type" => "textarea",
144	        "desc" => "Enter the Quote."
145	    ),
146	    "audio" => array (
147	        "name"  => "audio",
148	        "std"  => "http://",
149	        "label" => "Audio URL",
150	        "type" => "text",
151	        "desc" => "Enter the url/web address of the audio file."
152	    ),
153        "link-text" => array (
154	        "name"  => "link-text",
155	        "std"  => "Unknown",
156	        "label" => "Link Text",
157	        "type" => "text",
158	        "desc" => "Enter the link text."
159	    ),
160	    "link-url" => array (
161	        "name"  => "link-url",
162	        "std"  => "http://",
163	        "label" => "Link URL",
164	        "type" => "text",
165	        "desc" => "Enter the url/web address of the link."
166	    )
167	
168	);
169
170?>
171<style type="text/css">
172	#tumblog label{ float: left; clear: left; width: 28%; padding-top: 6px; padding-right: 3%; text-align: right; font-weight: bold; }
173	#tumblog input[type="text"],textarea{ float: left; margin-bottom: 10px; width: 68%; }
174	#tumblog img{ max-width: 68%; float: none; }
175	#tumblog .inside div{ float: none; }
176	#tumblog .uploadify{ overflow: auto; margin: 5px 0 10px 31%; width: auto; clear: both; }
177	#tumblog kbd{ margin-left: 31%; background-color: # }
178</style>
179<script type="text/javascript">
180
181jQuery(function($){
182
183	var tumblog = {
184		container 	: $('#tumblog'),
185		inside 		: $('#tumblog').find('.inside'),
186		postarea 	: $('.postarea'),
187		complete 	: false
188	}
189	
190	var go_switch_post_type = function($value){
191		
192		switch($value){
193	
194		case 'image':
195			tumblog.inside.find('.link-url,.gallery,.link-text,.quote-author,.quote-url,.quote-copy,.video-embed,.video-url,.audio').hide();
196			tumblog.container.slideDown('fast');
197			tumblog.postarea.slideDown('fast');
198			tumblog.inside.find('.image').show();
199			break;
200		case 'link':
201			tumblog.inside.find('.image,.gallery,.quote-author,.quote-url,.quote-copy,.video-embed,.video-url,.audio').hide();
202			tumblog.container.slideDown('fast');
203			tumblog.postarea.slideDown('fast');
204			tumblog.inside.find('.link-url,.link-text').show();
205			break;
206		case 'quote':
207			tumblog.inside.find('.image,.gallery,.link-url,.link-text,.video-embed,.video-url,.audio').hide();
208			tumblog.container.slideDown('fast');
209			tumblog.postarea.slideDown('fast');
210			tumblog.inside.find('.quote-author,.quote-url,.quote-copy').show();
211			break;
212		case 'video':
213			tumblog.inside.find('.image,.gallery,.quote-author,.quote-url,.quote-copy,.link-url,.link-text,.audio').hide();
214			tumblog.container.slideDown('fast');
215			tumblog.postarea.slideDown('fast');
216			tumblog.inside.find('.video-embed,.video-url').show();			
217			break;
218		case 'audio':
219			tumblog.inside.find('.image,.gallery,.quote-author,.quote-url,.quote-copy,.link-url,.link-text,.video-embed,.video-url,').hide();
220			tumblog.container.slideDown('fast');
221			tumblog.postarea.slideDown('fast');
222			tumblog.inside.find('.audio').show();			
223			break;
224		case 'gallery':
225			tumblog.inside.find('.image,.audio,.quote-author,.quote-url,.quote-copy,.link-url,.link-text,.video-embed,.video-url,').hide();
226			tumblog.container.slideDown('fast');
227			tumblog.postarea.slideDown('fast');
228			tumblog.inside.find('.gallery').show();			
229			break;
230		case 'status':
231			tumblog.postarea.slideUp('fast');
232			tumblog.container.slideUp('fast');
233			break;
234		default:
235			tumblog.inside.find('.image,.gallery,.quote-author,.quote-url,.quote-copy,.link-url,.link-text,.video-embed,.video-url,.audio').hide();
236			tumblog.container.slideUp('fast');
237			tumblog.postarea.slideDown('fast');
238			break;
239		}
240	
241		tumblog.complete = true;
242	
243	}
244
245	value = $('#post-formats-select').find('[type="radio"]:checked').val();
246	
247	// start it out with the right post type
248	$(document).ready(function(e){
249		
250		if(!tumblog.complete)
251			go_switch_post_type(value);
252
253	});
254	
255	$('#post-formats-select input[type="radio"]').live('change',function(e){
256			
257		$value = $(this).attr('value');
258		go_switch_post_type($value);
259	
260	});
261
262});
263
264</script>
265<?php
266	
267	foreach( $metaboxes as $metabox ) :
268	
269		$type = $metabox['type'];
270		$name = $metabox['name'];
271		$value = get_post_meta($post->ID, $metabox['name'], true);
272
273		echo '<div class="' . $metabox['name'] . '">';
274		echo '<label for="' . $metabox['name'] . '">' . $metabox['label'] . '</label>';
275				
276		switch( $type ) :
277		
278			case 'text' :
279				echo '<input type="text" id="' . $metabox['name'] . '" name="' . $metabox['name'] . '" value="' . esc_attr( $value ) . '">';
280				break;
281			
282			case 'textarea' :
283				echo '<textarea cols="10" id="' . $metabox['name'] . '" name="' . $metabox['name'] . '">' . esc_textarea( $value ) . '</textarea>';
284				break;
285			
286			case 'select' :
287				?>
288				<select id="<?php echo $metabox['name']; ?>" name="<?php echo $metabox['name']; ?>">
289					<?php foreach($metabox['options'] as $name => $val):?>
290						<option value="<?php echo $val;?>" <?php selected( $value == $val ); ?>><?php echo $name;?></option>
291					<?php endforeach;?>
292				</select>
293				<?php 
294				break;
295			
296			case 'image' :
297				?>
298				<script type="text/javascript">
299					jQuery(function($){
300						<?php //Upload Security
301						$upload_security = md5($_SERVER['SERVER_ADDR']); ?>
302						//Upload an Image
303						var <?php echo $metabox['name']; ?>=$('div.uploadify button#<?php echo $metabox['name']; ?>');
304						var status=$('#<?php echo $metabox['name']; ?>status');
305						new AjaxUpload(<?php echo $metabox['name']; ?>, {
306							action: '<?php echo THEME_DIR; ?>/admin/upload-file.php',
307							name: '<?php echo $upload_security?>',
308							data: {
309								upload_path : '<?php echo base64_encode(UPFW_UPLOADS_DIR); ?>'
310							},
311							onSubmit: function(file, ext){
312								//Check if file is an image
313								if (! (ext && /^(JPG|PNG|GIF|jpg|png|jpeg|gif)$/.test(ext))){ 
314								   // extension is not allowed 
315								   status.text('Only JPG, PNG or GIF files are allowed');
316								   return false;
317								}
318								$('#<?php echo $metabox['name']; ?>loader').addClass('activeload');
319							},
320							onComplete: function(file, response){
321								//On completion clear the status
322								status.text('');
323								//Successful upload
324								if(response==="success"){
325									$file = file.toLowerCase().replace(/ /g,'_').replace(/(_)\1+/g, '_').replace(/[^\w\(\).-]/gi,'_').replace(/__/g,'_');
326									//Preview uploaded file
327									$('#<?php echo $metabox['name']; ?>preview').removeClass('uploaderror');
328									$('#<?php echo $metabox['name']; ?>preview').html('<img class="preview" src="<?php echo UPFW_UPLOADS_URL; ?>/'+$file+'" alt="<?php echo $metabox['name']; ?> Image" />').addClass('success');
329									//Add image source to hidden input
330									$('input#<?php echo $metabox['name']; ?>').attr('value', '<?php echo UPFW_UPLOADS_URL; ?>/'+$file);
331									//Save Me Fool
332									$('#button-zone').animate({ 
333										backgroundColor: '#555',
334										borderLeftColor: '#555',
335										borderRightColor: '#555'
336									});
337									$('#button-zone button').addClass('save-me-fool');
338									$('.formState').fadeIn( 400 );
339								} else{
340									//Something went wrong
341									$('#<?php echo $metabox['name']; ?>preview').text(file+' did not upload. Please try again.').addClass('uploaderror');
342								}
343								$('#<?php echo $metabox['name']; ?>loader').removeClass('activeload');
344							}
345						});
346					});
347				</script>
348
349				<!-- Image Preview Input -->
350				<div id="<?php echo $metabox['name']; ?>preview">
351					<?php if($value):
352						echo "<img src='{$value}' alt='Preview Image' />";
353					else:
354						echo "<img src='".THEME_DIR."/admin/images/upfw_noimage.gif' alt='No Image Available' />";
355					endif;?>
356				</div>	
357
358				<div class="uploadify">
359					<button type="button" id="<?php echo $metabox['name']; ?>" class="secondary" <?php global $attr; echo $attr; ?>><?php _e('Upload Image','upfw'); ?></button>
360					<span id="<?php echo $metabox['name']; ?>loader" class="loader"></span>
361				</div>
362
363				<!-- Hidden Input -->
364				<input type="hidden" name="<?php echo $metabox['name']; ?>" id="<?php echo $metabox['name']; ?>" name="<?php echo $metabox['name']; ?>" value="<?php echo $value; ?>" />
365
366				<!-- Upload Status Input -->
367				<div class="status hide" id="<?php echo $metabox['name']; ?>status"></div>
368				<div class="clear"></div>
369				<?php 
370				break;
371			
372			case 'video' :
373				echo '<textarea cols="10" id="' . $metabox['name'] . '" name="' . $metabox['name'] . '">' . esc_textarea( $value ) . '</textarea>';
374				break;
375			
376		endswitch;
377		
378		switch( $name ):
379			
380			case 'link-url' :
381				echo '<kbd>Enter an optional title and description in the standard title and content fields above.</kbd>';
382				break;
383			case 'gallery' :
384				echo '<kbd>Enter an optional title and description in the standard title and content fields above.</kbd>';
385				break;
386			
387		endswitch;?>
388	
389		<div class='clear'></div>
390
391	</div>
392	
393	<?php endforeach;
394}
395
396/* When the post is saved, saves our custom data */
397/**
398 * Save Tumblog Post Custom Meta Data
399 * 
400 * Validates/sanitizes and saves post custom meta 
401 * data entered via the Tumblog metabox.
402 * 
403 * @uses	micro_convert_url_to_embed	Defined in /inc/media.php
404 * 
405 * @link 	http://codex.wordpress.org/Function_Reference/current_user_can 	current_user_can()
406 * @link 	http://codex.wordpress.org/Function_Reference/update_post_meta 	update_post_meta()
407 * @link 	http://codex.wordpress.org/Function_Reference/wp_verify_nonce	wp_verify_nonce()
408 * 
409 * @link 	http://php.net/manual/en/function.defined.php 					defined()
410 * @link 	http://php.net/manual/en/function.function-exists.php 			function_exists()
411 * 
412 * @param	int 	$post_id	Current post ID
413 * @return	none
414 * 
415 * @since	Micro 1.0
416 * 
417 */
418function tumblog_save_postdata( $post_id ) {
419	
420	// verify this came from the our screen and with proper authorization,
421	// because save_post can be triggered at other times
422		
423	if ( ! isset( $_POST['tumblog-boxes'] ) || ! wp_verify_nonce( $_POST['tumblog-boxes'], 'tumblog' ) ) {
424		return $post_id;
425	}
426	
427	// verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
428	// to do anything
429	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
430		return $post_id;
431	
432	// Check permissions
433	if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
434		if ( ! current_user_can( 'edit_page', $post_id ) )
435			return $post_id;
436	} else {
437		if ( ! current_user_can( 'edit_post', $post_id ) )
438			return $post_id;
439	}
440	
441	// OK, we're authenticated: we need to find and save the data	
442	$image = ( isset( $_POST['image'] ) ? esc_url( $_POST['image'] ) : false );
443	$videoembed = ( isset( $_POST['video-embed'] ) ? esc_html( $_POST['video-embed'] ) : false );
444	$videourl = ( isset( $_POST['video-url'] ) ? esc_url( $_POST['video-url'] ) : false );
445	if( $videourl ) $videoembed = micro_convert_url_to_embed( $videourl );
446	$videoembed = $videoembed ? $videoembed : esc_html( $_POST['video-embed'] );
447	$quoteauthor = ( isset( $_POST['quote-author'] ) ? esc_attr( $_POST['quote-author'] ) : false );
448	$quoteurl = ( isset( $_POST['quote-url'] ) ? esc_url( $_POST['quote-url'] ) : false );
449	$quotecopy = ( isset( $_POST['quote-copy'] ) ? wp_kses_post( $_POST['quote-copy'] ) : false );
450	$audio = ( isset( $_POST['audio'] ) ? esc_url( $_POST['audio'] ) : false );
451	$gallery = ( isset( $_POST['gallery'] ) && in_array( $_POST['gallery'], array( 'grid', 'list', 'slider' ) ) ? $_POST['gallery'] : false );
452	$linkurl = ( isset( $_POST['link-url'] ) ? esc_url( $_POST['link-url'] ) : false );
453    $linktext = ( isset( $_POST['link-text'] ) ? esc_attr( $_POST['link-text'] ) : false );
454	if( $image ) update_post_meta( $post_id, 'image', $image );
455	if( $videoembed ) update_post_meta( $post_id, 'video-embed', $videoembed );
456    if( $videourl ) update_post_meta( $post_id, 'video-url', $videourl );
457	if( $quoteauthor ) update_post_meta( $post_id, 'quote-author', $quoteauthor );
458	if( $quotecopy ) update_post_meta( $post_id, 'quote-copy', $quotecopy );
459    if( $quoteurl ) update_post_meta( $post_id, 'quote-url', $quoteurl );
460	if( $audio ) update_post_meta( $post_id, 'audio', $audio );
461	if( $gallery ) update_post_meta( $post_id, 'gallery', $gallery );
462	if( $linkurl ) update_post_meta( $post_id, 'link-url', $linkurl );
463    if( $linktext ) update_post_meta( $post_id, 'link-text', $linktext );
464
465}
466
467/* Do something with the data entered */
468add_action( 'save_post', 'tumblog_save_postdata' );