PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/cmb2/includes/types/CMB2_Type_File_Base.php

https://gitlab.com/ebrjose/comcebu
PHP | 221 lines | 109 code | 22 blank | 90 comment | 10 complexity | 2009e5db437bc01cf390eaa8f7f83144 MD5 | raw file
  1. <?php
  2. /**
  3. * CMB File base field type
  4. *
  5. * @since 2.2.2
  6. *
  7. * @category WordPress_Plugin
  8. * @package CMB2
  9. * @author CMB2 team
  10. * @license GPL-2.0+
  11. * @link https://cmb2.io
  12. */
  13. class CMB2_Type_File_Base extends CMB2_Type_Text {
  14. /**
  15. * Determines if a file has a valid image extension
  16. *
  17. * @since 1.0.0
  18. * @param string $file File url
  19. * @return bool Whether file has a valid image extension
  20. */
  21. public function is_valid_img_ext( $file, $blah = false ) {
  22. $file_ext = CMB2_Utils::get_file_ext( $file );
  23. $valid_types = array( 'jpg', 'jpeg', 'jpe', 'png', 'gif', 'ico', 'icon' );
  24. $allowed = get_allowed_mime_types();
  25. if ( ! empty( $allowed ) ) {
  26. foreach ( (array) $allowed as $type => $mime) {
  27. if ( 0 === strpos( $mime, 'image/' ) ) {
  28. $types = explode( '|', $type );
  29. $valid_types = array_merge( $valid_types, $types );
  30. }
  31. }
  32. $valid_types = array_unique( $valid_types );
  33. }
  34. /**
  35. * Which image types are considered valid image file extensions.
  36. *
  37. * @since 2.0.9
  38. *
  39. * @param array $valid_types The valid image file extensions.
  40. */
  41. $is_valid_types = apply_filters( 'cmb2_valid_img_types', $valid_types );
  42. $is_valid = $file_ext && in_array( $file_ext, (array) $is_valid_types );
  43. $field_id = $this->field->id();
  44. /**
  45. * Filter for determining if a field value has a valid image file-type extension.
  46. *
  47. * The dynamic portion of the hook name, $field_id, refers to the field id attribute.
  48. *
  49. * @since 2.0.9
  50. *
  51. * @param bool $is_valid Whether field value has a valid image file-type extension.
  52. * @param string $file File url.
  53. * @param string $file_ext File extension.
  54. */
  55. return (bool) apply_filters( "cmb2_{$field_id}_is_valid_img_ext", $is_valid, $file, $file_ext );
  56. }
  57. /**
  58. * file/file_list image wrap
  59. *
  60. * @since 2.0.2
  61. * @param array $args Array of arguments for output
  62. * @return string Image wrap output
  63. */
  64. public function img_status_output( $args ) {
  65. return sprintf( '<%1$s class="img-status cmb2-media-item">%2$s<p class="cmb2-remove-wrapper"><a href="#" class="cmb2-remove-file-button"%3$s>%4$s</a></p>%5$s</%1$s>',
  66. $args['tag'],
  67. $args['image'],
  68. isset( $args['cached_id'] ) ? ' rel="' . esc_attr( $args['cached_id'] ) . '"' : '',
  69. esc_html( $this->_text( 'remove_image_text', esc_html__( 'Remove Image', 'cmb2' ) ) ),
  70. isset( $args['id_input'] ) ? $args['id_input'] : ''
  71. );
  72. }
  73. /**
  74. * file/file_list file wrap
  75. *
  76. * @since 2.0.2
  77. * @param array $args Array of arguments for output
  78. * @return string File wrap output
  79. */
  80. public function file_status_output( $args ) {
  81. return sprintf( '<%1$s class="file-status cmb2-media-item"><span>%2$s <strong>%3$s</strong></span>&nbsp;&nbsp; (<a href="%4$s" target="_blank" rel="external">%5$s</a> / <a href="#" class="cmb2-remove-file-button"%6$s>%7$s</a>)%8$s</%1$s>',
  82. $args['tag'],
  83. esc_html( $this->_text( 'file_text', esc_html__( 'File:', 'cmb2' ) ) ),
  84. esc_html( CMB2_Utils::get_file_name_from_path( $args['value'] ) ),
  85. esc_url( $args['value'] ),
  86. esc_html( $this->_text( 'file_download_text', esc_html__( 'Download', 'cmb2' ) ) ),
  87. isset( $args['cached_id'] ) ? ' rel="' . esc_attr( $args['cached_id'] ) . '"' : '',
  88. esc_html( $this->_text( 'remove_text', esc_html__( 'Remove', 'cmb2' ) ) ),
  89. isset( $args['id_input'] ) ? $args['id_input'] : ''
  90. );
  91. }
  92. /**
  93. * Outputs the file/file_list underscore Javascript templates in the footer.
  94. *
  95. * @since 2.2.4
  96. * @return void
  97. */
  98. public static function output_js_underscore_templates() {
  99. ?>
  100. <script type="text/html" id="tmpl-cmb2-single-image">
  101. <div class="img-status cmb2-media-item">
  102. <img width="{{ data.sizeWidth }}" height="{{ data.sizeHeight }}" src="{{ data.sizeUrl }}" class="cmb-file-field-image" alt="{{ data.filename }}" title="{{ data.filename }}" />
  103. <p><a href="#" class="cmb2-remove-file-button" rel="{{ data.mediaField }}">{{ data.stringRemoveImage }}</a></p>
  104. </div>
  105. </script>
  106. <script type="text/html" id="tmpl-cmb2-single-file">
  107. <div class="file-status cmb2-media-item">
  108. <span>{{ data.stringFile }} <strong>{{ data.filename }}</strong></span>&nbsp;&nbsp; (<a href="{{ data.url }}" target="_blank" rel="external">{{ data.stringDownload }}</a> / <a href="#" class="cmb2-remove-file-button" rel="{{ data.mediaField }}">{{ data.stringRemoveFile }}</a>)
  109. </div>
  110. </script>
  111. <script type="text/html" id="tmpl-cmb2-list-image">
  112. <li class="img-status cmb2-media-item">
  113. <img width="{{ data.sizeWidth }}" height="{{ data.sizeHeight }}" src="{{ data.sizeUrl }}" class="cmb-file_list-field-image" alt="{{ data.filename }}">
  114. <p><a href="#" class="cmb2-remove-file-button" rel="{{ data.mediaField }}[{{ data.id }}]">{{ data.stringRemoveImage }}</a></p>
  115. <input type="hidden" id="filelist-{{ data.id }}" data-id="{{ data.id }}" name="{{ data.mediaFieldName }}[{{ data.id }}]" value="{{ data.url }}">
  116. </li>
  117. </script>
  118. <script type="text/html" id="tmpl-cmb2-list-file">
  119. <li class="file-status cmb2-media-item">
  120. <span>{{ data.stringFile }} <strong>{{ data.filename }}</strong></span>&nbsp;&nbsp; (<a href="{{ data.url }}" target="_blank" rel="external">{{ data.stringDownload }}</a> / <a href="#" class="cmb2-remove-file-button" rel="{{ data.mediaField }}[{{ data.id }}]">{{ data.stringRemoveFile }}</a>)
  121. <input type="hidden" id="filelist-{{ data.id }}" data-id="{{ data.id }}" name="{{ data.mediaFieldName }}[{{ data.id }}]" value="{{ data.url }}">
  122. </li>
  123. </script>
  124. <?php
  125. }
  126. /**
  127. * Utility method to return an array of meta data for a registered image size
  128. *
  129. * Uses CMB2_Utils::get_named_size() to get the closest available named size
  130. * from an array of width and height values and CMB2_Utils::get_available_image_sizes()
  131. * to get the meta data associated with a named size.
  132. *
  133. * @since 2.2.4
  134. * @param array|string $img_size Image size. Accepts an array of width and height (in that order)
  135. * @param string $fallback Size to use if the supplied named size doesn't exist
  136. * @return array Array containing the image size meta data
  137. * $size = (
  138. * 'width' => (int) image size width
  139. * 'height' => (int) image size height
  140. * 'name' => (string) e.g. 'thumbnail'
  141. * )
  142. */
  143. static function get_image_size_data( $img_size = '', $fallback = 'thumbnail' ) {
  144. $data = array();
  145. if ( is_array( $img_size ) ) {
  146. $data['width'] = intval( $img_size[0] );
  147. $data['height'] = intval( $img_size[1] );
  148. $data['name'] = '';
  149. // Try and get the closest named size from our array of dimensions
  150. if ( $named_size = CMB2_Utils::get_named_size( $img_size ) ) {
  151. $data['name'] = $named_size;
  152. }
  153. } else {
  154. $image_sizes = CMB2_Utils::get_available_image_sizes();
  155. // The 'thumb' alias, which works elsewhere, doesn't work in the wp.media uploader
  156. if ( 'thumb' == $img_size ) {
  157. $img_size = 'thumbnail';
  158. }
  159. // Named size doesn't exist, use $fallback
  160. if ( ! array_key_exists( $img_size, $image_sizes ) ) {
  161. $img_size = $fallback;
  162. }
  163. // Get image dimensions from named sizes
  164. $data['width'] = intval( $image_sizes[ $img_size ]['width'] );
  165. $data['height'] = intval( $image_sizes[ $img_size ]['height'] );
  166. $data['name'] = $img_size;
  167. }
  168. return $data;
  169. }
  170. /**
  171. * Filters attachment data prepared for JavaScript.
  172. *
  173. * Adds the url, width, height, and orientation for custom sizes to the JavaScript
  174. * object returned by the wp.media uploader. Hooked to 'wp_prepare_attachment_for_js'.
  175. *
  176. * @since 2.2.4
  177. * @param array $response Array of prepared attachment data
  178. * @param int|object $attachment Attachment ID or object
  179. * @param array $meta Array of attachment meta data ( from wp_get_attachment_metadata() )
  180. * @return array filtered $response array
  181. */
  182. public static function prepare_image_sizes_for_js( $response, $attachment, $meta ) {
  183. foreach ( CMB2_Utils::get_available_image_sizes() as $size => $info ) {
  184. // registered image size exists for this attachment
  185. if ( isset( $meta['sizes'][ $size ] ) ) {
  186. $attachment_url = wp_get_attachment_url( $attachment->ID );
  187. $base_url = str_replace( wp_basename( $attachment_url ), '', $attachment_url );
  188. $size_meta = $meta['sizes'][ $size ];
  189. $response['sizes'][ $size ] = array(
  190. 'url' => $base_url . $size_meta['file'],
  191. 'height' => $size_meta['height'],
  192. 'width' => $size_meta['width'],
  193. 'orientation' => $size_meta['height'] > $size_meta['width'] ? 'portrait' : 'landscape',
  194. );
  195. }
  196. }
  197. return $response;
  198. }
  199. }