* @copyright Copyright (C) 2019 wpopal.com. All Rights Reserved.
* @license GNU/GPL v2 or later http://www.gnu.org/licenses/gpl-2.0.html
*
* @website http://www.wpopal.com
* @support http://www.wpopal.com/support/forum.html
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if( !class_exists( 'CMB2_Uploader_Button' ) ) {
/**
* Class CMB2_Uploader_Button
*/
class CMB2_Uploader_Button {
/**
* Constructor
*/
public function __construct() {
add_action( 'cmb2_render_uploader', array( $this, 'callback' ), 10, 5 );
add_action( 'admin_head', array( $this, 'admin_head' ) );
add_action( 'wp_enqueue_scripts', [$this,'scripts_styles'], 99 );
}
/**
* Register javascript file for processing upload images/files
*/
public function scripts_styles () {
wp_register_script(
'cmb2-uploader',
OPALESTATE_PLUGIN_URL . 'assets/js/frontend/uploader.js',
[
'jquery',
],
'4.4.3',
true
);
}
/**
* Render Preview is image or icon with its name
*/
private function render_image_or_icon ( $escaped_value , $show_icon ) {
$cls = $show_icon ? "preview-icon" : "preview-image";
echo '
';
echo ' ';
if( $show_icon ){
echo ' '. basename ( get_attached_file( $escaped_value ) );
} else {
echo wp_get_attachment_image( $escaped_value, 'thumbnail' );
}
echo '
';
}
/**
* Render content input field.
*/
public function callback( $field, $escaped_value, $object_id, $object_type, $field_type_object) {
wp_enqueue_script( 'cmb2-uploader');
$field_name = $field->_name();
$args = array(
'type' => 'checkbox',
'id' => $field_name,
'name' => $field_name,
'desc' => '',
'value' => 'on',
);
if( $escaped_value == 'on' || $escaped_value == 1 ){
$args['checked'] = 'checked';
}
$single = isset( $field->args['single'] ) && $field->args['single'];
$attrs = $single ? "" : 'multiple="multiple"';
$size = '';
if( isset($field->args['accept']) && $field->args['accept'] ){
$attrs .= ' accept="'.$field->args['accept'].'" ';
$info = array(
'size' => opalestate_options( 'upload_other_max_size', 0.5),
'number' => opalestate_options( 'upload_other_max_files', 10)
);
$class = 'upload-file-wrap';
} else {
$attrs .= ' accept="image/*" ';
$class = 'upload-image-wrap';
$info = array(
'size' => opalestate_options( 'upload_image_max_size', 0.5),
'number' => opalestate_options( 'upload_image_max_files', 10)
);
}
if( $single ){
$info['number'] = 1;
}
$show_icon = isset($field->args['show_icon']) && $field->args['show_icon'] ? $field->args['show_icon']: false;
?>
$url ): ?>
render_image_or_icon( $key, $show_icon ); ?>
render_image_or_icon( $escaped_value, $show_icon ); ?>
args['value']) && (int)$field->args['value'] ):
$image_id = $field->args['value'];
?>
render_image_or_icon( $image_id , $show_icon ); ?>
class="select-file" style="visibility: hidden;">
'.$info['size'].'', ''.$info['number'].'' ); ?>