| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* LP_Meta_Box_Duration_Attribute |
| 5 |
* |
| 6 |
* @author Nhamdv |
| 7 |
* @version 1.0.0 |
| 8 |
* @since 4.0.0 |
| 9 |
*/ |
| 10 |
class LP_Meta_Box_File_Field extends LP_Meta_Box_Field { |
| 11 |
|
| 12 |
/** |
| 13 |
* Constructor. |
| 14 |
* |
| 15 |
* @param string $id |
| 16 |
* @param string $label |
| 17 |
* @param string $description |
| 18 |
* @param mixed $default |
| 19 |
* @param array $extra |
| 20 |
*/ |
| 21 |
public function __construct( $label = '', $description = '', $default = '', $extra = array() ) { |
| 22 |
parent::__construct( $label, $description, $default, $extra ); |
| 23 |
} |
| 24 |
|
| 25 |
public function output( $thepostid ) { |
| 26 |
if ( empty( $this->id ) ) { |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
$field = $this->extra; |
| 31 |
$field['id'] = $this->id; |
| 32 |
$field['default'] = $this->default; |
| 33 |
$field['description'] = $this->description; |
| 34 |
$field['label'] = $this->label; |
| 35 |
|
| 36 |
$field['class'] = $field['class'] ?? 'short'; |
| 37 |
$field['style'] = $field['style'] ?? ''; |
| 38 |
$field['wrapper_class'] = $field['wrapper_class'] ?? ''; |
| 39 |
$field['default'] = ( ! $this->meta_value( $thepostid ) && isset( $field['default'] ) ) ? $field['default'] : $this->meta_value( $thepostid ); |
| 40 |
$field['value'] = $field['value'] ?? $field['default']; |
| 41 |
$field['name'] = $field['name'] ?? $field['id']; |
| 42 |
$field['mime_type'] = isset( $field['mime_type'] ) ? implode( ',', $field['mime_type'] ) : ''; |
| 43 |
$field['multil'] = isset( $field['multil'] ) && $field['multil']; |
| 44 |
$field['desc_tip'] = $field['desc_tip'] ?? false; |
| 45 |
|
| 46 |
// Custom attribute handling |
| 47 |
$custom_attributes = array(); |
| 48 |
|
| 49 |
if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) { |
| 50 |
foreach ( $field['custom_attributes'] as $attribute => $custom_attribute ) { |
| 51 |
$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $custom_attribute ) . '"'; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
echo '<div class="form-field ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '"> |
| 56 |
<label for="' . esc_attr( $field['id'] ) . '">' . wp_kses_post( $field['label'] ) . '</label>'; |
| 57 |
|
| 58 |
echo '<div id="' . esc_attr( $field['id'] ) . '" class="lp-meta-box__file ' . esc_attr( $field['class'] ) . '" data-mime="' . $field['mime_type'] . '" data-multil="' . $field['multil'] . '" style="' . esc_attr( $field['style'] ) . '" ' . implode( |
| 59 |
' ', |
| 60 |
$custom_attributes |
| 61 |
) . '>'; |
| 62 |
echo '<ul class="lp-meta-box__file_list">'; |
| 63 |
|
| 64 |
$value = (array) $field['value']; |
| 65 |
if ( ! empty( $field['value'] ) ) { |
| 66 |
$value = array_map( 'absint', $value ); |
| 67 |
foreach ( $value as $attachment_id ) { |
| 68 |
$url = wp_get_attachment_url( $attachment_id ); |
| 69 |
|
| 70 |
if ( $url ) { |
| 71 |
$check_file = wp_check_filetype( $url ); |
| 72 |
|
| 73 |
echo '<li class="lp-meta-box__file_list-item image" data-attachment_id="' . $attachment_id . '">'; |
| 74 |
echo sprintf( '<img class="is_file" src="%s" />', wp_mime_type_icon( $check_file['type'] ) ); |
| 75 |
echo sprintf( '<span>%s</span>', wp_basename( get_attached_file( $attachment_id ) ) ); |
| 76 |
echo '<ul class="actions"><li><a href="#" class="delete"></a></li></ul>'; |
| 77 |
echo '</li>'; |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
echo '</ul>'; |
| 83 |
echo '<input class="lp-meta-box__file_input" type="hidden" name="' . esc_attr( $field['id'] ) . '" value="' . esc_attr( ( ! empty( $field['value'] ) && is_array( $field['value'] ) ) ? implode( ',', $field['value'] ) : $field['value'] ) . '" />'; |
| 84 |
echo '<p>'; |
| 85 |
echo '<a href="#" class="button btn-upload">' . esc_html__( '+ Add media', 'learnpress' ) . '</a>'; |
| 86 |
if ( ! empty( $field['description'] ) && false !== $field['desc_tip'] ) { |
| 87 |
learn_press_quick_tip( $field['description'] ); |
| 88 |
} |
| 89 |
echo '</p>'; |
| 90 |
|
| 91 |
if ( ! empty( $field['description'] ) && false === $field['desc_tip'] ) { |
| 92 |
echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>'; |
| 93 |
} |
| 94 |
echo '</div>'; |
| 95 |
echo '</div>'; |
| 96 |
} |
| 97 |
|
| 98 |
public function save( $post_id ) { |
| 99 |
$value = LP_Request::get_param( $this->id ); |
| 100 |
if ( ! empty( $value ) ) { |
| 101 |
$value = explode( ',', $value ); |
| 102 |
if ( ! empty( $value ) ) { |
| 103 |
$value = array_map( 'absint', $value ); |
| 104 |
} |
| 105 |
} else { |
| 106 |
$value = $this->default ?? ''; |
| 107 |
} |
| 108 |
|
| 109 |
update_post_meta( $post_id, $this->id, $value ); |
| 110 |
|
| 111 |
return $value; |
| 112 |
} |
| 113 |
} |
| 114 |
|