PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/themes/gentle/massive-panel/mpc-uploader.php

https://github.com/kikaendeavor/wordpress
PHP | 372 lines | 219 code | 84 blank | 69 comment | 55 complexity | 7d928dc934d5f307c519673ce7332314 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. global $shortname;
  3. $shortname = "gentle";
  4. if ( is_admin() ) {
  5. // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  6. // ---->>>> IMPORTANT THE DEFAULT MASSIVE PANEL NAME IS mp-settings !!!
  7. // Load additional css and js for image uploads on the Options Framework page
  8. $mp_page = 'toplevel_page_mp-settings';
  9. add_action( "admin_print_styles-$mp_page", 'mp_mlu_css', 0 );
  10. add_action( "admin_print_scripts-$mp_page", 'mp_mlu_js', 0 );
  11. }
  12. /**
  13. * Sets up a custom post type to attach image to. This allows us to have
  14. * individual galleries for different uploaders.
  15. */
  16. if ( ! function_exists( 'mp_mlu_init' ) ) {
  17. function mp_mlu_init () {
  18. register_post_type( 'mp', array(
  19. 'labels' => array(
  20. 'name' => __( 'Massive Panel Internal Container', 'agera' ),
  21. ),
  22. 'public' => true,
  23. 'show_ui' => false,
  24. 'capability_type' => 'post',
  25. 'hierarchical' => false,
  26. 'rewrite' => false,
  27. 'supports' => array( 'title', 'editor' ),
  28. 'query_var' => false,
  29. 'can_export' => true,
  30. 'show_in_nav_menus' => false
  31. ) );
  32. }
  33. }
  34. /**
  35. * Adds the Thickbox CSS file and specific loading and button images to the header
  36. * on the pages where this function is called.
  37. */
  38. if ( ! function_exists( 'mp_mlu_css' ) ) {
  39. function mp_mlu_css () {
  40. $_html = '';
  41. $_html .= '<link rel="stylesheet" href="' . site_url() . '/' . WPINC . '/js/thickbox/thickbox.css" type="text/css" media="screen" />' . "\n";
  42. $_html .= '<script type="text/javascript">
  43. var tb_pathToImage = "' . site_url() . '/' . WPINC . '/js/thickbox/loadingAnimation.gif";
  44. var tb_closeImage = "' . site_url() . '/' . WPINC . '/js/thickbox/tb-close.png";
  45. </script>' . "\n";
  46. echo $_html;
  47. }
  48. }
  49. /**
  50. * Registers and enqueues (loads) the necessary JavaScript file for working with the
  51. * Media Library-driven AJAX File Uploader Module.
  52. */
  53. if ( ! function_exists( 'mp_mlu_js' ) ) {
  54. function mp_mlu_js () {
  55. // Registers custom scripts for the Media Library AJAX uploader.
  56. wp_register_script( 'mp-medialibrary-uploader', get_template_directory_uri().'/massive-panel/js/mp-uploader.js', array( 'jquery', 'thickbox' ) );
  57. wp_enqueue_script( 'mp-medialibrary-uploader' );
  58. wp_enqueue_script( 'media-upload' );
  59. }
  60. }
  61. /**
  62. * Media Uploader Using the WordPress Media Library.
  63. *
  64. * Parameters:
  65. * - string $_id - A token to identify this field (the name).
  66. * - string $_value - The value of the field, if present.
  67. * - string $_mode - The display mode of the field.
  68. * - string $_desc - An optional description of the field.
  69. * - int $_postid - An optional post id (used in the meta boxes).
  70. *
  71. * Dependencies:
  72. * - mp_mlu_get_silentpost()
  73. */
  74. if ( ! function_exists( 'mp_medialibrary_uploader' ) ) {
  75. function mp_medialibrary_uploader( $_id, $_value, $_mode = 'full', $_desc = '', $_postid = 0, $_name = '') {
  76. global $shortname;
  77. $mp_settings = get_option($shortname.'_options');
  78. // Gets the unique option id
  79. $option_name = $shortname.'_options';
  80. $output = '';
  81. $id = '';
  82. $class = '';
  83. $int = '';
  84. $value = '';
  85. $name = '';
  86. $id = strip_tags( strtolower( $_id ) );
  87. // Change for each field, using a "silent" post. If no post is present, one will be created.
  88. $int = mp_mlu_get_silentpost( $id );
  89. // If a value is passed and we don't have a stored value, use the value that's passed through.
  90. if ( $_value != '' && $value == '' ) {
  91. $value = $_value;
  92. }
  93. if ($_name != '') {
  94. $name = $option_name.'['.$id.']';
  95. } else {
  96. $name = $option_name.'['.$id.']';
  97. }
  98. if ( $value ) { $class = ' has-file'; }
  99. $output .= '<input id="' . $id . '" class="upload' . $class . ' mp-input-big mp-input-border" type="text" name="'.$name.'" value="' . $value . '" />' . "\n";
  100. $output .= '<input id="upload_' . $id . '" class="upload_button button" type="button" value="' . __( 'Upload', 'agera' ) . '" rel="' . $int . '" />' . "\n";
  101. if ( $_desc != '' ) {
  102. $output .= '<span class="of_metabox_desc">' . $_desc . '</span>' . "\n";
  103. }
  104. $output .= '<div class="screenshot" id="'.$id.'_image"><div class="mp-image-frame" id="' . $id . '_frame"></div>';
  105. if ( $value != '' ) {
  106. $remove = '<a href="javascript:(void);" class="mlu_remove button">Remove</a>';
  107. $image = preg_match( '/.jpg|.jpeg|.png|.gif|.ico/', $value );
  108. //echo "value = ".$image;
  109. if ( $image ) {
  110. $output .= '<img src="' . $value . '" alt="" class="mp-image-border"/>'.$remove;
  111. } else {
  112. $parts = explode( "/", $value );
  113. for( $i = 0; $i < sizeof( $parts ); ++$i ) {
  114. $title = $parts[$i];
  115. }
  116. $output .= '<div class="no-preview"></div>'.$remove;
  117. }
  118. }
  119. $output .= '</div>';
  120. return $output;
  121. }
  122. }
  123. /* Multi Uploader */
  124. if ( ! function_exists( 'mp_medialibrary_multi_uploader' ) ) {
  125. function mp_medialibrary_multi_uploader( $_id, $_value, $_mode = 'full', $_desc = '', $_postid = 0, $_name = '') {
  126. $mp_settings = get_option($shortname.'_options');
  127. // Gets the unique option id
  128. $option_name = $shortname.'_options';
  129. $output = '';
  130. $id = '';
  131. $class = '';
  132. $int = '';
  133. $value = '';
  134. $name = '';
  135. $id = strip_tags( strtolower( $_id ) );
  136. // Change for each field, using a "silent" post. If no post is present, one will be created.
  137. $int = mp_mlu_get_silentpost( $id );
  138. // If a value is passed and we don't have a stored value, use the value that's passed through.
  139. if ( $_value != '' && $value == '' ) {
  140. $value = $_value;
  141. }
  142. if ($_name != '') {
  143. $name = $option_name.'['.$id.']';
  144. } else {
  145. $name = $option_name.'['.$id.']';
  146. }
  147. if ($value) {
  148. $class = ' has-file';
  149. }
  150. $value = trim($value);
  151. $output .= '<textarea rows="8" cols="86" id="' . $id . '" class="upload displayall-upload ' . $class . ' mp-textarea mp-input-border" name="'.$name.'">'.($value).'</textarea>'. "\n";
  152. $output .= '<input id="upload_' . $id . '" class="multi_upload_button button" type="button" value="' . __( 'Upload', 'agera' ) . '" rel="' . $int . '" />' . "\n";
  153. if ( $_desc != '' ) {
  154. $output .= '<span class="of_metabox_desc">' . $_desc . '</span>' . "\n";
  155. }
  156. $tempArray = split('http', $value);
  157. $ind = 0;
  158. foreach($tempArray as $key => $val){
  159. if($ind == 1){
  160. $output .= '<div class="multi-screenshot firstshot" >' . "\n";
  161. } elseif($ind == 6 || $ind == 11 || $ind == 16) {
  162. $output .= '<div class="multi-screenshot columnshot-first" >' . "\n";
  163. } elseif($ind > 6) {
  164. $output .= '<div class="multi-screenshot columnshot" >' . "\n";
  165. } else {
  166. $output .= '<div class="multi-screenshot" >' . "\n";
  167. }
  168. $output .= '<div class="mp-image-frame" ></div>';
  169. if ( $val != '' ) {
  170. $remove = '<a href="javascript:(void);" class="mlu_remove_multi button">Remove</a>';
  171. $image = preg_match('/.jpg|.jpeg|.png|.gif|.ico/', $val);
  172. if ( $image ) {
  173. $output .= '<img src="http' . $val . '" alt="" class="mp-image-border"/>'.$remove.'';
  174. } else {
  175. $parts = explode( "/", $value );
  176. for( $i = 0; $i < sizeof( $parts ); ++$i ) {
  177. $title = $parts[$i];
  178. }
  179. // No output preview if it's not an image.
  180. $output .= '<div class="no-preview-small">http'. $val .'</div>'.$remove.'';
  181. }
  182. }
  183. $output .= '</div>';
  184. $ind++;
  185. }
  186. return $output;
  187. }
  188. }
  189. /**
  190. * Uses "silent" posts in the database to store relationships for images.
  191. * This also creates the facility to collect galleries of, for example, logo images.
  192. *
  193. * Return: $_postid.
  194. *
  195. * If no "silent" post is present, one will be created with the type "mp"
  196. * and the post_name of "mp-$_token".
  197. *
  198. * Example Usage:
  199. * mp_mlu_get_silentpost ( 'mp_logo' );
  200. */
  201. if ( ! function_exists( 'mp_mlu_get_silentpost' ) ) {
  202. function mp_mlu_get_silentpost ( $_token ) {
  203. global $wpdb;
  204. $_id = 0;
  205. // Check if the token is valid against a whitelist.
  206. // $_whitelist = array( 'of_logo', 'of_custom_favicon', 'of_ad_top_image' );
  207. // Sanitise the token.
  208. $_token = strtolower( str_replace( ' ', '_', $_token ) );
  209. // if ( in_array( $_token, $_whitelist ) ) {
  210. if ( $_token ) {
  211. // Tell the function what to look for in a post.
  212. $_args = array( 'post_type' => 'mp', 'post_name' => 'of-' . $_token, 'post_status' => 'draft', 'comment_status' => 'closed', 'ping_status' => 'closed' );
  213. // Look in the database for a "silent" post that meets our criteria.
  214. $query = 'SELECT ID FROM ' . $wpdb->posts . ' WHERE post_parent = 0';
  215. foreach ( $_args as $k => $v ) {
  216. $query .= ' AND ' . $k . ' = "' . $v . '"';
  217. } // End FOREACH Loop
  218. $query .= ' LIMIT 1';
  219. $_posts = $wpdb->get_row( $query );
  220. // If we've got a post, loop through and get it's ID.
  221. if ( count( $_posts ) ) {
  222. $_id = $_posts->ID;
  223. } else {
  224. // If no post is present, insert one.
  225. // Prepare some additional data to go with the post insertion.
  226. $_words = explode( '_', $_token );
  227. $_title = join( ' ', $_words );
  228. $_title = ucwords( $_title );
  229. $_post_data = array( 'post_title' => $_title );
  230. $_post_data = array_merge( $_post_data, $_args );
  231. $_id = wp_insert_post( $_post_data );
  232. }
  233. }
  234. return $_id;
  235. }
  236. }
  237. /**
  238. * Trigger code inside the Media Library popup.
  239. */
  240. if ( ! function_exists( 'mp_mlu_insidepopup' ) ) {
  241. function mp_mlu_insidepopup () {
  242. if ( isset( $_REQUEST['is_mp'] ) && $_REQUEST['is_mp'] == 'yes' ) {
  243. add_action( 'admin_head', 'mp_mlu_js_popup' );
  244. add_filter( 'media_upload_tabs', 'mp_mlu_modify_tabs' );
  245. }
  246. }
  247. }
  248. if ( ! function_exists( 'mp_mlu_js_popup' ) ) {
  249. function mp_mlu_js_popup () {
  250. $_of_title = $_REQUEST['of_title'];
  251. if ( ! $_of_title ) { $_of_title = 'file'; } // End IF Statement
  252. ?>
  253. <script type="text/javascript">
  254. <!--
  255. jQuery(function($) {
  256. jQuery.noConflict();
  257. // Change the title of each tab to use the custom title text instead of "Media File".
  258. $( 'h3.media-title' ).each ( function () {
  259. var current_title = $( this ).html();
  260. var new_title = current_title.replace( 'media file', '<?php echo $_of_title; ?>' );
  261. $( this ).html( new_title );
  262. } );
  263. // Change the text of the "Insert into Post" buttons to read "Use this File".
  264. $( '.savesend input.button[value*="Insert into Post"], .media-item #go_button' ).attr( 'value', 'Use this File' );
  265. // Hide the "Insert Gallery" settings box on the "Gallery" tab.
  266. $( 'div#gallery-settings' ).hide();
  267. // Preserve the "is_mp" parameter on the "delete" confirmation button.
  268. $( '.savesend a.del-link' ).click ( function () {
  269. var continueButton = $( this ).next( '.del-attachment' ).children( 'a.button[id*="del"]' );
  270. var continueHref = continueButton.attr( 'href' );
  271. continueHref = continueHref + '&is_mp=yes';
  272. continueButton.attr( 'href', continueHref );
  273. } );
  274. });
  275. -->
  276. </script>
  277. <?php
  278. }
  279. }
  280. /**
  281. * Triggered inside the Media Library popup to modify the title of the "Gallery" tab.
  282. */
  283. if ( ! function_exists( 'mp_mlu_modify_tabs' ) ) {
  284. function mp_mlu_modify_tabs ( $tabs ) {
  285. $tabs['gallery'] = str_replace( __( 'Gallery', 'mp' ), __( 'Previously Uploaded', 'mp' ), $tabs['gallery'] );
  286. return $tabs;
  287. }
  288. }