upload.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
  2. /**
  3. *
  4. * Field: upload
  5. *
  6. * @since 1.0.0
  7. * @version 1.0.0
  8. *
  9. */
  10. if ( ! class_exists( 'CSF_Field_upload' ) ) {
  11. class CSF_Field_upload 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. 'library' => array(),
  18. 'preview' => true,
  19. 'preview_width' => '',
  20. 'preview_height' => '',
  21. 'button_title' => esc_html__( 'Upload', 'sakurairo_csf' ),
  22. 'remove_title' => esc_html__( 'Remove', 'sakurairo_csf' ),
  23. ) );
  24. echo $this->field_before();
  25. $library = ( is_array( $args['library'] ) ) ? $args['library'] : array_filter( (array) $args['library'] );
  26. $library = ( ! empty( $library ) ) ? implode(',', $library ) : '';
  27. $hidden = ( empty( $this->value ) ) ? ' hidden' : '';
  28. if ( ! empty( $args['preview'] ) ) {
  29. $preview_type = ( ! empty( $this->value ) ) ? strtolower( substr( strrchr( $this->value, '.' ), 1 ) ) : '';
  30. $preview_src = ( ! empty( $preview_type ) && in_array( $preview_type, array( 'jpg', 'jpeg', 'gif', 'png', 'svg', 'webp' ) ) ) ? $this->value : '';
  31. $preview_width = ( ! empty( $args['preview_width'] ) ) ? 'max-width:'. esc_attr( $args['preview_width'] ) .'px;' : '';
  32. $preview_height = ( ! empty( $args['preview_height'] ) ) ? 'max-height:'. esc_attr( $args['preview_height'] ) .'px;' : '';
  33. $preview_style = ( ! empty( $preview_width ) || ! empty( $preview_height ) ) ? ' style="'. esc_attr( $preview_width . $preview_height ) .'"': '';
  34. $preview_hidden = ( empty( $preview_src ) ) ? ' hidden' : '';
  35. echo '<div class="csf--preview'. esc_attr( $preview_hidden ) .'">';
  36. echo '<div class="csf-image-preview"'. $preview_style .'>';
  37. echo '<i class="csf--remove fas fa-times"></i><span><img src="'. esc_url( $preview_src ) .'" class="csf--src" /></span>';
  38. echo '</div>';
  39. echo '</div>';
  40. }
  41. echo '<div class="csf--wrap">';
  42. echo '<input type="text" name="'. esc_attr( $this->field_name() ) .'" value="'. esc_attr( $this->value ) .'"'. $this->field_attributes() .'/>';
  43. echo '<a href="#" class="button button-primary csf--button" data-library="'. esc_attr( $library ) .'">'. $args['button_title'] .'</a>';
  44. echo '<a href="#" class="button button-secondary csf-warning-primary csf--remove'. esc_attr( $hidden ) .'">'. $args['remove_title'] .'</a>';
  45. echo '</div>';
  46. echo $this->field_after();
  47. }
  48. }
  49. }