| 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 |
|
| 13 |
public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) { |
| 14 |
parent::__construct( $field, $value, $unique, $where, $parent ); |
| 15 |
} |
| 16 |
|
| 17 |
public function render() { |
| 18 |
|
| 19 |
$args = wp_parse_args( $this->field, array( |
| 20 |
'url' => true, |
| 21 |
'preview' => true, |
| 22 |
'preview_width' => '', |
| 23 |
'preview_height' => '', |
| 24 |
'library' => array(), |
| 25 |
'button_title' => esc_html__( 'Upload', 'csf' ), |
| 26 |
'remove_title' => esc_html__( 'Remove', 'csf' ), |
| 27 |
'preview_size' => 'thumbnail', |
| 28 |
) ); |
| 29 |
|
| 30 |
$default_values = array( |
| 31 |
'url' => '', |
| 32 |
'id' => '', |
| 33 |
'width' => '', |
| 34 |
'height' => '', |
| 35 |
'thumbnail' => '', |
| 36 |
'alt' => '', |
| 37 |
'title' => '', |
| 38 |
'description' => '' |
| 39 |
); |
| 40 |
|
| 41 |
// fallback |
| 42 |
if ( is_numeric( $this->value ) ) { |
| 43 |
|
| 44 |
$this->value = array( |
| 45 |
'id' => $this->value, |
| 46 |
'url' => wp_get_attachment_url( $this->value ), |
| 47 |
'thumbnail' => wp_get_attachment_image_src( $this->value, 'thumbnail', true )[0], |
| 48 |
); |
| 49 |
|
| 50 |
} |
| 51 |
|
| 52 |
$this->value = wp_parse_args( $this->value, $default_values ); |
| 53 |
|
| 54 |
$library = ( is_array( $args['library'] ) ) ? $args['library'] : array_filter( (array) $args['library'] ); |
| 55 |
$library = ( ! empty( $library ) ) ? implode(',', $library ) : ''; |
| 56 |
$preview_src = ( $args['preview_size'] !== 'thumbnail' ) ? $this->value['url'] : $this->value['thumbnail']; |
| 57 |
$hidden_url = ( empty( $args['url'] ) ) ? ' hidden' : ''; |
| 58 |
$hidden_auto = ( empty( $this->value['url'] ) ) ? ' hidden' : ''; |
| 59 |
$placeholder = ( empty( $this->field['placeholder'] ) ) ? ' placeholder="'. esc_html__( 'Not selected', 'csf' ) .'"' : ''; |
| 60 |
|
| 61 |
echo $this->field_before(); |
| 62 |
|
| 63 |
if ( ! empty( $args['preview'] ) ) { |
| 64 |
|
| 65 |
$preview_width = ( ! empty( $args['preview_width'] ) ) ? 'max-width:'. esc_attr( $args['preview_width'] ) .'px;' : ''; |
| 66 |
$preview_height = ( ! empty( $args['preview_height'] ) ) ? 'max-height:'. esc_attr( $args['preview_height'] ) .'px;' : ''; |
| 67 |
$preview_style = ( ! empty( $preview_width ) || ! empty( $preview_height ) ) ? ' style="'. esc_attr( $preview_width . $preview_height ) .'"': ''; |
| 68 |
|
| 69 |
echo '<div class="csf--preview'. esc_attr( $hidden_auto ) .'">'; |
| 70 |
echo '<div class="csf-image-preview"'. $preview_style .'>'; |
| 71 |
echo '<i class="csf--remove fas fa-times"></i><span><img src="'. esc_url( $preview_src ) .'" class="csf--src" /></span>'; |
| 72 |
echo '</div>'; |
| 73 |
echo '</div>'; |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
echo '<div class="csf--placeholder">'; |
| 78 |
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 .' />'; |
| 79 |
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>'; |
| 80 |
echo ( empty( $args['preview'] ) ) ? '<a href="#" class="button button-secondary csf-warning-primary csf--remove'. esc_attr( $hidden_auto ) .'">'. $args['remove_title'] .'</a>' : ''; |
| 81 |
echo '</div>'; |
| 82 |
|
| 83 |
echo '<input type="hidden" name="'. esc_attr( $this->field_name( '[id]' ) ) .'" value="'. esc_attr( $this->value['id'] ) .'" class="csf--id"/>'; |
| 84 |
echo '<input type="hidden" name="'. esc_attr( $this->field_name( '[width]' ) ) .'" value="'. esc_attr( $this->value['width'] ) .'" class="csf--width"/>'; |
| 85 |
echo '<input type="hidden" name="'. esc_attr( $this->field_name( '[height]' ) ) .'" value="'. esc_attr( $this->value['height'] ) .'" class="csf--height"/>'; |
| 86 |
echo '<input type="hidden" name="'. esc_attr( $this->field_name( '[thumbnail]' ) ) .'" value="'. esc_attr( $this->value['thumbnail'] ) .'" class="csf--thumbnail"/>'; |
| 87 |
echo '<input type="hidden" name="'. esc_attr( $this->field_name( '[alt]' ) ) .'" value="'. esc_attr( $this->value['alt'] ) .'" class="csf--alt"/>'; |
| 88 |
echo '<input type="hidden" name="'. esc_attr( $this->field_name( '[title]' ) ) .'" value="'. esc_attr( $this->value['title'] ) .'" class="csf--title"/>'; |
| 89 |
echo '<input type="hidden" name="'. esc_attr( $this->field_name( '[description]' ) ) .'" value="'. esc_attr( $this->value['description'] ) .'" class="csf--description"/>'; |
| 90 |
|
| 91 |
echo $this->field_after(); |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
} |
| 96 |
} |
| 97 |
|