media.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
  2. /**
  3. *
  4. * Field: media
  5. *
  6. * @since 1.0.0
  7. * @version 1.0.0
  8. *
  9. */
  10. if ( ! class_exists( 'CSF_Field_media' ) ) {
  11. class CSF_Field_media extends CSF_Fields {
  12. public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) {
  13. parent::__construct( $field, $value, $unique, $where, $parent );
  14. }
  15. public function render() {
  16. $args = wp_parse_args( $this->field, array(
  17. 'url' => true,
  18. 'preview' => true,
  19. 'preview_width' => '',
  20. 'preview_height' => '',
  21. 'library' => array(),
  22. 'button_title' => esc_html__( 'Upload', 'sakurairo_csf' ),
  23. 'remove_title' => esc_html__( 'Remove', 'sakurairo_csf' ),
  24. 'preview_size' => 'thumbnail',
  25. ) );
  26. $default_values = array(
  27. 'url' => '',
  28. 'id' => '',
  29. 'width' => '',
  30. 'height' => '',
  31. 'thumbnail' => '',
  32. 'alt' => '',
  33. 'title' => '',
  34. 'description' => ''
  35. );
  36. // fallback
  37. if ( is_numeric( $this->value ) ) {
  38. $this->value = array(
  39. 'id' => $this->value,
  40. 'url' => wp_get_attachment_url( $this->value ),
  41. 'thumbnail' => wp_get_attachment_image_src( $this->value, 'thumbnail', true )[0],
  42. );
  43. }
  44. $this->value = wp_parse_args( $this->value, $default_values );
  45. $library = ( is_array( $args['library'] ) ) ? $args['library'] : array_filter( (array) $args['library'] );
  46. $library = ( ! empty( $library ) ) ? implode(',', $library ) : '';
  47. $preview_src = ( $args['preview_size'] !== 'thumbnail' ) ? $this->value['url'] : $this->value['thumbnail'];
  48. $hidden_url = ( empty( $args['url'] ) ) ? ' hidden' : '';
  49. $hidden_auto = ( empty( $this->value['url'] ) ) ? ' hidden' : '';
  50. $placeholder = ( empty( $this->field['placeholder'] ) ) ? ' placeholder="'. esc_html__( 'Not selected', 'sakurairo_csf' ) .'"' : '';
  51. echo $this->field_before();
  52. if ( ! empty( $args['preview'] ) ) {
  53. $preview_width = ( ! empty( $args['preview_width'] ) ) ? 'max-width:'. esc_attr( $args['preview_width'] ) .'px;' : '';
  54. $preview_height = ( ! empty( $args['preview_height'] ) ) ? 'max-height:'. esc_attr( $args['preview_height'] ) .'px;' : '';
  55. $preview_style = ( ! empty( $preview_width ) || ! empty( $preview_height ) ) ? ' style="'. esc_attr( $preview_width . $preview_height ) .'"': '';
  56. echo '<div class="csf--preview'. esc_attr( $hidden_auto ) .'">';
  57. echo '<div class="csf-image-preview"'. $preview_style .'>';
  58. echo '<i class="csf--remove fas fa-times"></i><span><img src="'. esc_url( $preview_src ) .'" class="csf--src" /></span>';
  59. echo '</div>';
  60. echo '</div>';
  61. }
  62. echo '<div class="csf--placeholder">';
  63. echo '<input type="text" name="'. esc_attr( $this->field_name( '[url]' ) ) .'" value="'. esc_attr( $this->value['url'] ) .'" class="csf--url'. esc_attr( $hidden_url ) .'" readonly="readonly"'. $this->field_attributes() . $placeholder .' />';
  64. echo '<a href="#" class="button button-primary csf--button" data-library="'. esc_attr( $library ) .'" data-preview-size="'. esc_attr( $args['preview_size'] ) .'">'. $args['button_title'] .'</a>';
  65. echo ( empty( $args['preview'] ) ) ? '<a href="#" class="button button-secondary csf-warning-primary csf--remove'. esc_attr( $hidden_auto ) .'">'. $args['remove_title'] .'</a>' : '';
  66. echo '</div>';
  67. echo '<input type="hidden" name="'. esc_attr( $this->field_name( '[id]' ) ) .'" value="'. esc_attr( $this->value['id'] ) .'" class="csf--id"/>';
  68. echo '<input type="hidden" name="'. esc_attr( $this->field_name( '[width]' ) ) .'" value="'. esc_attr( $this->value['width'] ) .'" class="csf--width"/>';
  69. echo '<input type="hidden" name="'. esc_attr( $this->field_name( '[height]' ) ) .'" value="'. esc_attr( $this->value['height'] ) .'" class="csf--height"/>';
  70. echo '<input type="hidden" name="'. esc_attr( $this->field_name( '[thumbnail]' ) ) .'" value="'. esc_attr( $this->value['thumbnail'] ) .'" class="csf--thumbnail"/>';
  71. echo '<input type="hidden" name="'. esc_attr( $this->field_name( '[alt]' ) ) .'" value="'. esc_attr( $this->value['alt'] ) .'" class="csf--alt"/>';
  72. echo '<input type="hidden" name="'. esc_attr( $this->field_name( '[title]' ) ) .'" value="'. esc_attr( $this->value['title'] ) .'" class="csf--title"/>';
  73. echo '<input type="hidden" name="'. esc_attr( $this->field_name( '[description]' ) ) .'" value="'. esc_attr( $this->value['description'] ) .'" class="csf--description"/>';
  74. echo $this->field_after();
  75. }
  76. }
  77. }