| 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( 'SP_PC_Field_media' ) ) { |
| 11 |
class SP_PC_Field_media extends SP_PC_Fields { |
| 12 |
|
| 13 |
/** |
| 14 |
* The field constructor. |
| 15 |
* |
| 16 |
* @param string $field The field type. |
| 17 |
* @param string $value The field value. |
| 18 |
* @param string $unique The unique ID. |
| 19 |
* @param string $where The place to show the field. |
| 20 |
* @param string $parent If it has any parent. |
| 21 |
*/ |
| 22 |
public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) { |
| 23 |
parent::__construct( $field, $value, $unique, $where, $parent ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* The render function. |
| 28 |
* |
| 29 |
* @return void |
| 30 |
*/ |
| 31 |
public function render() { |
| 32 |
|
| 33 |
$args = wp_parse_args( |
| 34 |
$this->field, |
| 35 |
array( |
| 36 |
'url' => true, |
| 37 |
'preview' => true, |
| 38 |
'library' => array(), |
| 39 |
'button_title' => esc_html__( 'Upload', 'smart-post-show' ), |
| 40 |
'remove_title' => esc_html__( 'Remove', 'smart-post-show' ), |
| 41 |
'preview_size' => 'thumbnail', |
| 42 |
) |
| 43 |
); |
| 44 |
|
| 45 |
$default_values = array( |
| 46 |
'url' => '', |
| 47 |
'id' => '', |
| 48 |
'width' => '', |
| 49 |
'height' => '', |
| 50 |
'thumbnail' => '', |
| 51 |
'alt' => '', |
| 52 |
'title' => '', |
| 53 |
'description' => '', |
| 54 |
); |
| 55 |
|
| 56 |
// fallback. |
| 57 |
if ( is_numeric( $this->value ) ) { |
| 58 |
$this->value = array( |
| 59 |
'id' => $this->value, |
| 60 |
'url' => wp_get_attachment_url( $this->value ), |
| 61 |
'thumbnail' => wp_get_attachment_image_src( $this->value, 'thumbnail', true )[0], |
| 62 |
); |
| 63 |
} |
| 64 |
$this->value = wp_parse_args( $this->value, $default_values ); |
| 65 |
|
| 66 |
$library = ( is_array( $args['library'] ) ) ? $args['library'] : array_filter( (array) $args['library'] ); |
| 67 |
$library = ( ! empty( $library ) ) ? implode( ',', $library ) : ''; |
| 68 |
$preview_src = ( 'thumbnail' !== $args['preview_size'] ) ? $this->value['url'] : $this->value['thumbnail']; |
| 69 |
$hidden_url = ( empty( $args['url'] ) ) ? ' hidden' : ''; |
| 70 |
$hidden_auto = ( empty( $this->value['url'] ) ) ? ' hidden' : ''; |
| 71 |
$placeholder = ( empty( $this->field['placeholder'] ) ) ? ' placeholder="' . esc_html__( 'No media selected', 'smart-post-show' ) . '"' : ''; |
| 72 |
|
| 73 |
echo wp_kses_post( $this->field_before() ); |
| 74 |
|
| 75 |
if ( ! empty( $args['preview'] ) ) { |
| 76 |
echo '<div class="spf--preview' . esc_attr( $hidden_auto ) . '">'; |
| 77 |
echo '<div class="spf-image-preview"><a href="#" class="spf--remove fa fa-times"></a><img src="' . esc_url( $preview_src ) . '" class="spf--src"/></div>'; |
| 78 |
echo '</div>'; |
| 79 |
} |
| 80 |
|
| 81 |
echo '<div class="spf--placeholder">'; |
| 82 |
echo '<input type="text" name="' . esc_url( $this->field_name( '[url]' ) ) . '" value="' . esc_url( $this->value['url'] ) . '" class="spf--url' . esc_attr( $hidden_url ) . '" readonly="readonly"' . wp_kses_post( $this->field_attributes() . $placeholder ) . ' />'; |
| 83 |
echo '<a href="#" class="button button-primary spf--button" data-library="' . esc_attr( $library ) . '" data-preview-size="' . esc_attr( $args['preview_size'] ) . '">' . esc_html( $args['button_title'] ) . '</a>'; |
| 84 |
echo ( empty( $args['preview'] ) ) ? '<a href="#" class="button button-secondary spf-warning-primary spf--remove' . esc_attr( $hidden_auto ) . '">' . esc_html( $args['remove_title'] ) . '</a>' : ''; |
| 85 |
echo '</div>'; |
| 86 |
|
| 87 |
echo '<input type="hidden" name="' . esc_attr( $this->field_name( '[id]' ) ) . '" value="' . esc_attr( $this->value['id'] ) . '" class="spf--id"/>'; |
| 88 |
echo '<input type="hidden" name="' . esc_attr( $this->field_name( '[width]' ) ) . '" value="' . esc_attr( $this->value['width'] ) . '" class="spf--width"/>'; |
| 89 |
echo '<input type="hidden" name="' . esc_attr( $this->field_name( '[height]' ) ) . '" value="' . esc_attr( $this->value['height'] ) . '" class="spf--height"/>'; |
| 90 |
echo '<input type="hidden" name="' . esc_attr( $this->field_name( '[thumbnail]' ) ) . '" value="' . esc_attr( $this->value['thumbnail'] ) . '" class="spf--thumbnail"/>'; |
| 91 |
echo '<input type="hidden" name="' . esc_attr( $this->field_name( '[alt]' ) ) . '" value="' . esc_attr( $this->value['alt'] ) . '" class="spf--alt"/>'; |
| 92 |
echo '<input type="hidden" name="' . esc_attr( $this->field_name( '[title]' ) ) . '" value="' . esc_attr( $this->value['title'] ) . '" class="spf--title"/>'; |
| 93 |
echo '<input type="hidden" name="' . esc_attr( $this->field_name( '[description]' ) ) . '" value="' . esc_attr( $this->value['description'] ) . '" class="spf--description"/>'; |
| 94 |
|
| 95 |
echo wp_kses_post( $this->field_after() ); |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
} |
| 100 |
} |
| 101 |
|