PageRenderTime 59ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/themes/news/library/functions/media.php

https://bitbucket.org/lgorence/quickpress
PHP | 247 lines | 85 code | 39 blank | 123 comment | 20 complexity | a36f7cec395ce3b27ea78113527b3470 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * Functions file for loading scripts and stylesheets. This file also handles the output of attachment files
  4. * by displaying appropriate HTML elements for the attachments.
  5. *
  6. * @package HybridCore
  7. * @subpackage Functions
  8. * @author Justin Tadlock <justin@justintadlock.com>
  9. * @copyright Copyright (c) 2008 - 2012, Justin Tadlock
  10. * @link http://themehybrid.com/hybrid-core
  11. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  12. */
  13. /* Register Hybrid Core scripts. */
  14. add_action( 'wp_enqueue_scripts', 'hybrid_register_scripts', 1 );
  15. /* Load Hybrid Core scripts. */
  16. add_action( 'wp_enqueue_scripts', 'hybrid_enqueue_scripts' );
  17. /* Load the development stylsheet in script debug mode. */
  18. add_filter( 'stylesheet_uri', 'hybrid_debug_stylesheet', 10, 2 );
  19. /* Add all image sizes to the image editor to insert into post. */
  20. add_filter( 'image_size_names_choose', 'hybrid_image_size_names_choose' );
  21. /**
  22. * Registers JavaScript files for the framework. This function merely registers scripts with WordPress using
  23. * the wp_register_script() function. It does not load any script files on the site. If a theme wants to register
  24. * its own custom scripts, it should do so on the 'wp_enqueue_scripts' hook.
  25. *
  26. * @since 1.2.0
  27. * @access private
  28. * @return void
  29. */
  30. function hybrid_register_scripts() {
  31. /* Supported JavaScript. */
  32. $supports = get_theme_support( 'hybrid-core-javascript' );
  33. /* Register the 'drop-downs' script if the current theme supports 'hybrid-core-drop-downs'. */
  34. if ( current_theme_supports( 'hybrid-core-drop-downs' ) || ( isset( $supports[0] ) && in_array( 'drop-downs', $supports[0] ) ) )
  35. wp_register_script( 'drop-downs', esc_url( apply_atomic( 'drop_downs_script', trailingslashit( HYBRID_JS ) . 'drop-downs.js' ) ), array( 'jquery' ), '20110920', true );
  36. /* Register the 'nav-bar' script if the current theme supports 'hybrid-core-nav-bar'. */
  37. if ( isset( $supports[0] ) && in_array( 'nav-bar', $supports[0] ) )
  38. wp_register_script( 'nav-bar', esc_url( apply_atomic( 'nav_bar_script', trailingslashit( HYBRID_JS ) . 'nav-bar.js' ) ), array( 'jquery' ), '20111008', true );
  39. }
  40. /**
  41. * Tells WordPress to load the scripts needed for the framework using the wp_enqueue_script() function.
  42. *
  43. * @since 1.2.0
  44. * @access private
  45. * @return void
  46. */
  47. function hybrid_enqueue_scripts() {
  48. /* Supported JavaScript. */
  49. $supports = get_theme_support( 'hybrid-core-javascript' );
  50. /* Load the comment reply script on singular posts with open comments if threaded comments are supported. */
  51. if ( is_singular() && get_option( 'thread_comments' ) && comments_open() )
  52. wp_enqueue_script( 'comment-reply' );
  53. /* Load the 'drop-downs' script if the current theme supports 'hybrid-core-drop-downs'. */
  54. if ( current_theme_supports( 'hybrid-core-drop-downs' ) || ( isset( $supports[0] ) && in_array( 'drop-downs', $supports[0] ) ) )
  55. wp_enqueue_script( 'drop-downs' );
  56. /* Load the 'nav-bar' script if the current theme supports 'hybrid-core-nav-bar'. */
  57. if ( isset( $supports[0] ) && in_array( 'nav-bar', $supports[0] ) )
  58. wp_enqueue_script( 'nav-bar' );
  59. }
  60. /**
  61. * Function for using a debug stylesheet when developing. To develop with the debug stylesheet,
  62. * SCRIPT_DEBUG must be set to 'true' in the 'wp-config.php' file. This will check if a 'style.dev.css'
  63. * file is present within the theme folder and use it if it exists. Else, it defaults to 'style.css'.
  64. *
  65. * @since 0.9.0
  66. * @access private
  67. * @param string $stylesheet_uri The URI of the active theme's stylesheet.
  68. * @param string $stylesheet_dir_uri The directory URI of the active theme's stylesheet.
  69. * @return string $stylesheet_uri
  70. */
  71. function hybrid_debug_stylesheet( $stylesheet_uri, $stylesheet_dir_uri ) {
  72. /* If SCRIPT_DEBUG is set to true and the theme supports 'dev-stylesheet'. */
  73. if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && current_theme_supports( 'dev-stylesheet' ) ) {
  74. /* Remove the stylesheet directory URI from the file name. */
  75. $stylesheet = str_replace( trailingslashit( $stylesheet_dir_uri ), '', $stylesheet_uri );
  76. /* Change the stylesheet name to 'style.dev.css'. */
  77. $stylesheet = str_replace( '.css', '.dev.css', $stylesheet );
  78. /* If the stylesheet exists in the stylesheet directory, set the stylesheet URI to the dev stylesheet. */
  79. if ( file_exists( trailingslashit( get_stylesheet_directory() ) . $stylesheet ) )
  80. $stylesheet_uri = trailingslashit( $stylesheet_dir_uri ) . $stylesheet;
  81. }
  82. /* Return the theme stylesheet. */
  83. return $stylesheet_uri;
  84. }
  85. /**
  86. * Adds theme/plugin custom images sizes added with add_image_size() to the image uploader/editor. This
  87. * allows users to insert these images within their post content editor.
  88. *
  89. * @since 1.3.0
  90. * @access private
  91. * @param array $sizes Selectable image sizes.
  92. * @return array $sizes
  93. */
  94. function hybrid_image_size_names_choose( $sizes ) {
  95. /* Get all intermediate image sizes. */
  96. $intermediate_sizes = get_intermediate_image_sizes();
  97. $add_sizes = array();
  98. /* Loop through each of the intermediate sizes, adding them to the $add_sizes array. */
  99. foreach ( $intermediate_sizes as $size )
  100. $add_sizes[$size] = $size;
  101. /* Merge the original array, keeping it intact, with the new array of image sizes. */
  102. $sizes = array_merge( $add_sizes, $sizes );
  103. /* Return the new sizes plus the old sizes back. */
  104. return $sizes;
  105. }
  106. /**
  107. * Loads the correct function for handling attachments. Checks the attachment mime type to call
  108. * correct function. Image attachments are not loaded with this function. The functionality for them
  109. * should be handled by the theme's attachment or image attachment file.
  110. *
  111. * Ideally, all attachments would be appropriately handled within their templates. However, this could
  112. * lead to messy template files.
  113. *
  114. * @since 0.5.0
  115. * @access public
  116. * @uses get_post_mime_type() Gets the mime type of the attachment.
  117. * @uses wp_get_attachment_url() Gets the URL of the attachment file.
  118. * @return void
  119. */
  120. function hybrid_attachment() {
  121. $file = wp_get_attachment_url();
  122. $mime = get_post_mime_type();
  123. $mime_type = explode( '/', $mime );
  124. /* Loop through each mime type. If a function exists for it, call it. Allow users to filter the display. */
  125. foreach ( $mime_type as $type ) {
  126. if ( function_exists( "hybrid_{$type}_attachment" ) )
  127. $attachment = call_user_func( "hybrid_{$type}_attachment", $mime, $file );
  128. $attachment = apply_atomic( "{$type}_attachment", $attachment );
  129. }
  130. echo apply_atomic( 'attachment', $attachment );
  131. }
  132. /**
  133. * Handles application attachments on their attachment pages. Uses the <object> tag to embed media
  134. * on those pages.
  135. *
  136. * @since 0.3.0
  137. * @access public
  138. * @param string $mime attachment mime type
  139. * @param string $file attachment file URL
  140. * @return string
  141. */
  142. function hybrid_application_attachment( $mime = '', $file = '' ) {
  143. $embed_defaults = wp_embed_defaults();
  144. $application = '<object class="text" type="' . esc_attr( $mime ) . '" data="' . esc_url( $file ) . '" width="' . esc_attr( $embed_defaults['width'] ) . '" height="' . esc_attr( $embed_defaults['height'] ) . '">';
  145. $application .= '<param name="src" value="' . esc_url( $file ) . '" />';
  146. $application .= '</object>';
  147. return $application;
  148. }
  149. /**
  150. * Handles text attachments on their attachment pages. Uses the <object> element to embed media
  151. * in the pages.
  152. *
  153. * @since 0.3.0
  154. * @access public
  155. * @param string $mime attachment mime type
  156. * @param string $file attachment file URL
  157. * @return string
  158. */
  159. function hybrid_text_attachment( $mime = '', $file = '' ) {
  160. $embed_defaults = wp_embed_defaults();
  161. $text = '<object class="text" type="' . esc_attr( $mime ) . '" data="' . esc_url( $file ) . '" width="' . esc_attr( $embed_defaults['width'] ) . '" height="' . esc_attr( $embed_defaults['height'] ) . '">';
  162. $text .= '<param name="src" value="' . esc_url( $file ) . '" />';
  163. $text .= '</object>';
  164. return $text;
  165. }
  166. /**
  167. * Handles audio attachments on their attachment pages. Puts audio/mpeg and audio/wma files into
  168. * an <object> element.
  169. *
  170. * @todo Test out and support more audio types.
  171. *
  172. * @since 0.2.2
  173. * @access public
  174. * @param string $mime attachment mime type
  175. * @param string $file attachment file URL
  176. * @return string
  177. */
  178. function hybrid_audio_attachment( $mime = '', $file = '' ) {
  179. $embed_defaults = wp_embed_defaults();
  180. $audio = '<object type="' . esc_attr( $mime ) . '" class="player audio" data="' . esc_url( $file ) . '" width="' . esc_attr( $embed_defaults['width'] ) . '" height="' . esc_attr( $embed_defaults['height'] ) . '">';
  181. $audio .= '<param name="src" value="' . esc_url( $file ) . '" />';
  182. $audio .= '<param name="autostart" value="false" />';
  183. $audio .= '<param name="controller" value="true" />';
  184. $audio .= '</object>';
  185. return $audio;
  186. }
  187. /**
  188. * Handles video attachments on attachment pages. Add other video types to the <object> element.
  189. *
  190. * @since 0.2.2
  191. * @access public
  192. * @param string $mime attachment mime type
  193. * @param string $file attachment file URL
  194. * @return string
  195. */
  196. function hybrid_video_attachment( $mime = false, $file = false ) {
  197. $embed_defaults = wp_embed_defaults();
  198. if ( $mime == 'video/asf' )
  199. $mime = 'video/x-ms-wmv';
  200. $video = '<object type="' . esc_attr( $mime ) . '" class="player video" data="' . esc_url( $file ) . '" width="' . esc_attr( $embed_defaults['width'] ) . '" height="' . esc_attr( $embed_defaults['height'] ) . '">';
  201. $video .= '<param name="src" value="' . esc_url( $file ) . '" />';
  202. $video .= '<param name="autoplay" value="false" />';
  203. $video .= '<param name="allowfullscreen" value="true" />';
  204. $video .= '<param name="controller" value="true" />';
  205. $video .= '</object>';
  206. return $video;
  207. }
  208. ?>