| 1 |
<?php if ( ! defined( 'ABSPATH' ) ) { |
| 2 |
die; } // Cannot access directly. |
| 3 |
/** |
| 4 |
* |
| 5 |
* Field: media |
| 6 |
* |
| 7 |
* @since 1.0.0 |
| 8 |
* @version 1.0.0 |
| 9 |
*/ |
| 10 |
if ( ! class_exists( 'ADMINIFY_Field_media' ) ) { |
| 11 |
class ADMINIFY_Field_media extends ADMINIFY_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 |
$args = wp_parse_args( |
| 19 |
$this->field, |
| 20 |
[ |
| 21 |
'url' => true, |
| 22 |
'preview' => true, |
| 23 |
'preview_width' => '', |
| 24 |
'preview_height' => '', |
| 25 |
'library' => [], |
| 26 |
'button_title' => esc_html__( 'Upload', 'adminify' ), |
| 27 |
'remove_title' => esc_html__( 'Remove', 'adminify' ), |
| 28 |
'preview_size' => 'thumbnail', |
| 29 |
] |
| 30 |
); |
| 31 |
|
| 32 |
$default_values = [ |
| 33 |
'url' => '', |
| 34 |
'id' => '', |
| 35 |
'width' => '', |
| 36 |
'height' => '', |
| 37 |
'thumbnail' => '', |
| 38 |
'alt' => '', |
| 39 |
'title' => '', |
| 40 |
'description' => '', |
| 41 |
]; |
| 42 |
|
| 43 |
// fallback |
| 44 |
if ( is_numeric( $this->value ) ) { |
| 45 |
$this->value = [ |
| 46 |
'id' => $this->value, |
| 47 |
'url' => wp_get_attachment_url( $this->value ), |
| 48 |
'thumbnail' => wp_get_attachment_image_src( $this->value, 'thumbnail', true )[0], |
| 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', 'adminify' ) . '"' : ''; |
| 60 |
|
| 61 |
echo wp_kses_post( $this->field_before() ); |
| 62 |
|
| 63 |
if ( ! empty( $args['preview'] ) ) { |
| 64 |
$preview_width = ( ! empty( $args['preview_width'] ) ) ? 'max-width:' . esc_attr( $args['preview_width'] ) . 'px;' : ''; |
| 65 |
$preview_height = ( ! empty( $args['preview_height'] ) ) ? 'max-height:' . esc_attr( $args['preview_height'] ) . 'px;' : ''; |
| 66 |
$preview_style = ( ! empty( $preview_width ) || ! empty( $preview_height ) ) ? ' style="' . esc_attr( $preview_width . $preview_height ) . '"' : ''; |
| 67 |
|
| 68 |
echo '<div class="adminify--preview' . esc_attr( $hidden_auto ) . '">'; |
| 69 |
echo '<div class="adminify-image-preview"' . esc_attr( $preview_style ) . '>'; |
| 70 |
echo '<i class="adminify--remove fas fa-times"></i><span><img src="' . esc_url( $preview_src ) . '" class="adminify--src" /></span>'; |
| 71 |
echo '</div>'; |
| 72 |
echo '</div>'; |
| 73 |
} |
| 74 |
|
| 75 |
echo '<div class="adminify--placeholder">'; |
| 76 |
echo '<input type="text" name="' . esc_attr( $this->field_name( '[url]' ) ) . '" value="' . esc_attr( $this->value['url'] ) . '" class="adminify--url' . esc_attr( $hidden_url ) . '" readonly="readonly"' . wp_kses_post( $this->field_attributes() ) . wp_kses_post( $placeholder ) . ' />'; |
| 77 |
echo '<a href="#" class="button button-primary adminify--button" data-library="' . esc_attr( $library ) . '" data-preview-size="' . esc_attr( $args['preview_size'] ) . '">' . wp_kses_post( $args['button_title'] ) . '</a>'; |
| 78 |
echo ( empty( $args['preview'] ) ) ? '<a href="#" class="button button-secondary adminify-warning-primary adminify--remove' . esc_attr( $hidden_auto ) . '">' . wp_kses_post( $args['remove_title'] ) . '</a>' : ''; |
| 79 |
echo '</div>'; |
| 80 |
|
| 81 |
echo '<input type="hidden" name="' . esc_attr( $this->field_name( '[id]' ) ) . '" value="' . esc_attr( $this->value['id'] ) . '" class="adminify--id"/>'; |
| 82 |
echo '<input type="hidden" name="' . esc_attr( $this->field_name( '[width]' ) ) . '" value="' . esc_attr( $this->value['width'] ) . '" class="adminify--width"/>'; |
| 83 |
echo '<input type="hidden" name="' . esc_attr( $this->field_name( '[height]' ) ) . '" value="' . esc_attr( $this->value['height'] ) . '" class="adminify--height"/>'; |
| 84 |
echo '<input type="hidden" name="' . esc_attr( $this->field_name( '[thumbnail]' ) ) . '" value="' . esc_attr( $this->value['thumbnail'] ) . '" class="adminify--thumbnail"/>'; |
| 85 |
echo '<input type="hidden" name="' . esc_attr( $this->field_name( '[alt]' ) ) . '" value="' . esc_attr( $this->value['alt'] ) . '" class="adminify--alt"/>'; |
| 86 |
echo '<input type="hidden" name="' . esc_attr( $this->field_name( '[title]' ) ) . '" value="' . esc_attr( $this->value['title'] ) . '" class="adminify--title"/>'; |
| 87 |
echo '<input type="hidden" name="' . esc_attr( $this->field_name( '[description]' ) ) . '" value="' . esc_attr( $this->value['description'] ) . '" class="adminify--description"/>'; |
| 88 |
|
| 89 |
echo wp_kses_post( $this->field_after() ); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|