options-medialibrary-uploader.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. /**
  3. * WooThemes Media Library-driven AJAX File Uploader Module (2010-11-05)
  4. *
  5. * Slightly modified for use in the Options Framework.
  6. */
  7. if ( is_admin() ) {
  8. // Load additional css and js for image uploads on the Options Framework page
  9. $of_page= 'appearance_page_options-framework';
  10. add_action( "admin_print_styles-$of_page", 'optionsframework_mlu_css', 0 );
  11. add_action( "admin_print_scripts-$of_page", 'optionsframework_mlu_js', 0 );
  12. }
  13. /**
  14. * Sets up a custom post type to attach image to. This allows us to have
  15. * individual galleries for different uploaders.
  16. */
  17. if ( ! function_exists( 'optionsframework_mlu_init' ) ) :
  18. function optionsframework_mlu_init () {
  19. register_post_type( 'optionsframework', array(
  20. 'labels' => array(
  21. 'name' => __( 'Theme Options Media', 'foto' ),
  22. ),
  23. 'public' => true,
  24. 'show_ui' => false,
  25. 'capability_type' => 'post',
  26. 'hierarchical' => false,
  27. 'rewrite' => false,
  28. 'supports' => array( 'title', 'editor' ),
  29. 'query_var' => false,
  30. 'can_export' => true,
  31. 'show_in_nav_menus' => false,
  32. 'public' => false
  33. ) );
  34. }
  35. endif;
  36. /**
  37. * Adds the Thickbox CSS file and specific loading and button images to the header
  38. * on the pages where this function is called.
  39. */
  40. if ( ! function_exists( 'optionsframework_mlu_css' ) ) :
  41. function optionsframework_mlu_css () {
  42. $_html = '';
  43. $_html .= '<link rel="stylesheet" href="' . site_url() . '/' . WPINC . '/js/thickbox/thickbox.css" type="text/css" media="screen" />' . "\n";
  44. $_html .= '<script type="text/javascript">
  45. var tb_pathToImage = "' . site_url() . '/' . WPINC . '/js/thickbox/loadingAnimation.gif";
  46. var tb_closeImage = "' . site_url() . '/' . WPINC . '/js/thickbox/tb-close.png";
  47. </script>' . "\n";
  48. echo $_html;
  49. }
  50. endif;
  51. /**
  52. * Registers and enqueues (loads) the necessary JavaScript file for working with the
  53. * Media Library-driven AJAX File Uploader Module.
  54. */
  55. if ( ! function_exists( 'optionsframework_mlu_js' ) ) :
  56. function optionsframework_mlu_js () {
  57. // Registers custom scripts for the Media Library AJAX uploader.
  58. wp_register_script( 'of-medialibrary-uploader', OPTIONS_FRAMEWORK_DIRECTORY .'js/of-medialibrary-uploader.js', array( 'jquery', 'thickbox' ) );
  59. wp_enqueue_script( 'of-medialibrary-uploader' );
  60. wp_enqueue_script( 'media-upload' );
  61. }
  62. endif;
  63. /**
  64. * Media Uploader Using the WordPress Media Library.
  65. *
  66. * Parameters:
  67. * - string $_id - A token to identify this field (the name).
  68. * - string $_value - The value of the field, if present.
  69. * - string $_mode - The display mode of the field.
  70. * - string $_desc - An optional description of the field.
  71. * - int $_postid - An optional post id (used in the meta boxes).
  72. *
  73. * Dependencies:
  74. * - optionsframework_mlu_get_silentpost()
  75. */
  76. if ( ! function_exists( 'optionsframework_medialibrary_uploader' ) ) :
  77. function optionsframework_medialibrary_uploader( $_id, $_value, $_mode = 'full', $_desc = '', $_postid = 0, $_name = '') {
  78. $optionsframework_settings = get_option('optionsframework');
  79. // Gets the unique option id
  80. $option_name = $optionsframework_settings['id'];
  81. $output = '';
  82. $id = '';
  83. $class = '';
  84. $int = '';
  85. $value = '';
  86. $name = '';
  87. $id = strip_tags( strtolower( $_id ) );
  88. // Change for each field, using a "silent" post. If no post is present, one will be created.
  89. $int = optionsframework_mlu_get_silentpost( $id );
  90. // If a value is passed and we don't have a stored value, use the value that's passed through.
  91. if ( $_value != '' && $value == '' ) {
  92. $value = $_value;
  93. }
  94. if ($_name != '') {
  95. $name = $option_name.'['.$id.']['.$_name.']';
  96. }
  97. else {
  98. $name = $option_name.'['.$id.']';
  99. }
  100. if ( $value ) { $class = ' has-file'; }
  101. $output .= '<input id="' . $id . '" class="upload' . $class . '" type="text" name="'.$name.'" value="' . $value . '" />' . "\n";
  102. $output .= '<input id="upload_' . $id . '" class="upload_button button" type="button" value="' . __( 'Upload', 'foto' ) . '" rel="' . $int . '" />' . "\n";
  103. if ( $_desc != '' ) {
  104. $output .= '<span class="of_metabox_desc">' . $_desc . '</span>' . "\n";
  105. }
  106. $output .= '<div class="screenshot" id="' . $id . '_image">' . "\n";
  107. if ( $value != '' ) {
  108. $remove = '<a href="javascript:(void);" class="mlu_remove button">Remove</a>';
  109. $image = preg_match( '/(^.*\.jpg|jpeg|png|gif|ico*)/i', $value );
  110. if ( $image ) {
  111. $output .= '<img src="' . $value . '" alt="" />'.$remove.'';
  112. } else {
  113. $parts = explode( "/", $value );
  114. for( $i = 0; $i < sizeof( $parts ); ++$i ) {
  115. $title = $parts[$i];
  116. }
  117. // No output preview if it's not an image.
  118. $output .= '';
  119. // Standard generic output if it's not an image.
  120. $title = __( 'View File', 'foto' );
  121. $output .= '<div class="no_image"><span class="file_link"><a href="' . $value . '" target="_blank" rel="external">'.$title.'</a></span>' . $remove . '</div>';
  122. }
  123. }
  124. $output .= '</div>' . "\n";
  125. return $output;
  126. }
  127. endif;
  128. /**
  129. * Uses "silent" posts in the database to store relationships for images.
  130. * This also creates the facility to collect galleries of, for example, logo images.
  131. *
  132. * Return: $_postid.
  133. *
  134. * If no "silent" post is present, one will be created with the type "optionsframework"
  135. * and the post_name of "of-$_token".
  136. *
  137. * Example Usage:
  138. * optionsframework_mlu_get_silentpost ( 'of_logo' );
  139. */
  140. if ( ! function_exists( 'optionsframework_mlu_get_silentpost' ) ) :
  141. function optionsframework_mlu_get_silentpost ( $_token ) {
  142. global $wpdb;
  143. $_id = 0;
  144. // Check if the token is valid against a whitelist.
  145. // $_whitelist = array( 'of_logo', 'of_custom_favicon', 'of_ad_top_image' );
  146. // Sanitise the token.
  147. $_token = strtolower( str_replace( ' ', '_', $_token ) );
  148. // if ( in_array( $_token, $_whitelist ) ) {
  149. if ( $_token ) {
  150. // Tell the function what to look for in a post.
  151. $_args = array( 'post_type' => 'optionsframework', 'post_name' => 'of-' . $_token, 'post_status' => 'draft', 'comment_status' => 'closed', 'ping_status' => 'closed' );
  152. // Look in the database for a "silent" post that meets our criteria.
  153. $query = 'SELECT ID FROM ' . $wpdb->posts . ' WHERE post_parent = 0';
  154. foreach ( $_args as $k => $v ) {
  155. $query .= ' AND ' . $k . ' = "' . $v . '"';
  156. } // End FOREACH Loop
  157. $query .= ' LIMIT 1';
  158. $_posts = $wpdb->get_row( $query );
  159. // If we've got a post, loop through and get it's ID.
  160. if ( count( $_posts ) ) {
  161. $_id = $_posts->ID;
  162. } else {
  163. // If no post is present, insert one.
  164. // Prepare some additional data to go with the post insertion.
  165. $_words = explode( '_', $_token );
  166. $_title = join( ' ', $_words );
  167. $_title = ucwords( $_title );
  168. $_post_data = array( 'post_title' => $_title );
  169. $_post_data = array_merge( $_post_data, $_args );
  170. $_id = wp_insert_post( $_post_data );
  171. }
  172. }
  173. return $_id;
  174. }
  175. endif;
  176. /**
  177. * Trigger code inside the Media Library popup.
  178. */
  179. if ( ! function_exists( 'optionsframework_mlu_insidepopup' ) ) :
  180. function optionsframework_mlu_insidepopup () {
  181. if ( isset( $_REQUEST['is_optionsframework'] ) && $_REQUEST['is_optionsframework'] == 'yes' ) {
  182. add_action( 'admin_head', 'optionsframework_mlu_js_popup' );
  183. add_filter( 'media_upload_tabs', 'optionsframework_mlu_modify_tabs' );
  184. }
  185. }
  186. endif;
  187. if ( ! function_exists( 'optionsframework_mlu_js_popup' ) ) :
  188. function optionsframework_mlu_js_popup () {
  189. $_of_title = $_REQUEST['of_title'];
  190. if ( ! $_of_title ) { $_of_title = 'file'; } // End IF Statement
  191. ?>
  192. <script type="text/javascript">
  193. jQuery(function($) {
  194. jQuery.noConflict();
  195. // Change the title of each tab to use the custom title text instead of "Media File".
  196. $( 'h3.media-title' ).each ( function () {
  197. var current_title = $( this ).html();
  198. var new_title = current_title.replace( 'media file', '<?php echo $_of_title; ?>' );
  199. $( this ).html( new_title );
  200. } );
  201. // Change the text of the "Insert into Post" buttons to read "Use this File".
  202. $( '.savesend input.button[value*="Insert into Post"], .media-item #go_button' ).attr( 'value', 'Use this File' );
  203. // Hide the "Insert Gallery" settings box on the "Gallery" tab.
  204. $( 'div#gallery-settings' ).hide();
  205. // Preserve the "is_optionsframework" parameter on the "delete" confirmation button.
  206. $( '.savesend a.del-link' ).click ( function () {
  207. var continueButton = $( this ).next( '.del-attachment' ).children( 'a.button[id*="del"]' );
  208. var continueHref = continueButton.attr( 'href' );
  209. continueHref = continueHref + '&is_optionsframework=yes';
  210. continueButton.attr( 'href', continueHref );
  211. });
  212. });
  213. </script>
  214. <?php
  215. }
  216. endif;
  217. /**
  218. * Triggered inside the Media Library popup to modify the title of the "Gallery" tab.
  219. */
  220. if ( ! function_exists( 'optionsframework_mlu_modify_tabs' ) ) :
  221. function optionsframework_mlu_modify_tabs ( $tabs ) {
  222. $tabs['gallery'] = str_replace( __( 'Gallery', 'foto' ), __( 'Previously Uploaded', 'foto' ), $tabs['gallery'] );
  223. return $tabs;
  224. }
  225. endif;