PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

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