'', 'url' => '', 'width' => '', 'height' => '', 'thumbnail' => '', 'filter' => array( 'grayscale' => array( 'checked' => false, 'value' => 0, ), 'blur' => array( 'checked' => false, 'value' => 0, ), 'sepia' => array( 'checked' => false, 'value' => 0, ), 'saturate' => array( 'checked' => false, 'value' => 1, ), 'opacity' => array( 'checked' => false, 'value' => 1, ), 'brightness' => array( 'checked' => false, 'value' => 100, ), 'contrast' => array( 'checked' => false, 'value' => 100, ), 'hue-rotate' => array( 'checked' => false, 'value' => 0, ), 'invert' => array( 'checked' => false, 'value' => 0, ), ), ); // Since value sub-arrays do not get parsed in wp_parse_args! $this->value = Redux_Functions::parse_args( $this->value, $defaults ); $defaults = array( 'mode' => 'image', 'preview' => true, 'preview_size' => 'thumbnail', 'url' => true, 'alt' => '', 'placeholder' => esc_html__( 'No media selected', 'redux-framework' ), 'readonly' => true, 'class' => '', 'filter' => array( 'grayscale' => false, 'blur' => false, 'sepia' => false, 'saturate' => false, 'opacity' => false, 'brightness' => false, 'contrast' => false, 'hue-rotate' => false, 'invert' => false, ), ); $this->field = Redux_Functions::parse_args( $this->field, $defaults ); if ( false === $this->field['mode'] ) { $this->field['mode'] = 0; } include_once Redux_Core::$dir . 'inc/lib/image-filters/class-redux-image-filters.php'; if ( in_array( true, $this->field['filter'], true ) ) { $this->filters_enabled = true; include_once Redux_Core::$dir . 'inc/lib/image-filters/class-redux-image-filters.php'; } } /** * Field Render Function. * Takes the vars and outputs the HTML for the field in the settings * * @since 1.0.0 * @access public * @return void */ public function render() { if ( ! isset( $this->field['library_filter'] ) ) { $lib_filter = ''; } else { if ( ! is_array( $this->field['library_filter'] ) ) { $this->field['library_filter'] = array( $this->field['library_filter'] ); } $mime_types = get_allowed_mime_types(); $lib_array = $this->field['library_filter']; $json_arr = array(); // Enum mime types. foreach ( $mime_types as $ext => $type ) { if ( strpos( $ext, '|' ) ) { $ex_arr = explode( '|', $ext ); foreach ( $ex_arr as $ext ) { if ( in_array( $ext, $lib_array, true ) ) { $json_arr[ $ext ] = $type; } } } elseif ( in_array( $ext, $lib_array, true ) ) { $json_arr[ $ext ] = $type; } } $lib_filter = rawurlencode( wp_json_encode( $json_arr ) ); } if ( empty( $this->value ) && ! empty( $this->field['default'] ) ) { // If there are standard values and value is empty. if ( is_array( $this->field['default'] ) ) { if ( ! empty( $this->field['default']['id'] ) ) { $this->value['id'] = $this->field['default']['id']; } if ( ! empty( $this->field['default']['url'] ) ) { $this->value['url'] = $this->field['default']['url']; } } elseif ( is_numeric( $this->field['default'] ) ) { // Check if it's an attachment ID. $this->value['id'] = $this->field['default']; } else { // Must be a URL. $this->value['url'] = $this->field['default']; } } if ( empty( $this->value['url'] ) && ! empty( $this->value['id'] ) ) { $img = wp_get_attachment_image_src( $this->value['id'], 'full' ); $this->value['url'] = $img[0]; $this->value['width'] = $img[1]; $this->value['height'] = $img[2]; } $hide = 'hide '; if ( false === $this->field['preview'] ) { $this->field['class'] .= ' noPreview'; } if ( ( ! empty( $this->field['url'] ) && true === $this->field['url'] ) || false === $this->field['preview'] ) { $hide = ''; } $read_only = ''; if ( $this->field['readonly'] ) { $read_only = ' readonly="readonly"'; } echo ''; echo ''; echo ''; // phpcs:ignore WordPress.Security.EscapeOutput echo ''; echo ''; echo ''; echo ''; // Preview. $hide = ''; if ( ( false === $this->field['preview'] ) || empty( $this->value['url'] ) ) { $hide .= 'display:none;'; } if ( empty( $this->value['thumbnail'] ) && ! empty( $this->value['url'] ) ) { // Just in case. if ( ! empty( $this->value['id'] ) ) { $image = wp_get_attachment_image_src( $this->value['id'], array( 150, 150 ) ); if ( empty( $image[0] ) ) { $this->value['thumbnail'] = $this->value['url']; } else { $this->value['thumbnail'] = $image[0]; } } else { $this->value['thumbnail'] = $this->value['url']; } } $css = $this->get_filter_css( $this->value['filter'] ); $alt = wp_prepare_attachment_for_js( $this->value['id'] ); $alt = $alt['alt'] ?? ''; echo '
'; echo ''; echo '' . esc_attr( $alt ) . ''; // phpcs:ignore WordPress.Security.EscapeOutput echo ''; echo '
'; // Upload controls DIV. echo '
'; // If the user has WP3.5+ show upload/remove button. echo '' . esc_html__( 'Upload', 'redux-framework' ) . ''; $hide = ''; if ( empty( $this->value['url'] ) || '' === $this->value['url'] ) { $hide = ' hide'; } echo '' . esc_html__( 'Remove', 'redux-framework' ) . ''; echo '
'; if ( $this->filters_enabled ) { $data = array( 'parent' => $this->parent, 'field' => $this->field, 'value' => $this->value, 'mode' => 'media', ); echo Redux_Image_Filters::render( $data ); // phpcs:ignore WordPress.Security.EscapeOutput } } /** * Do enqueue for each field instance. * * @return void */ public function always_enqueue() { Redux_Image_Filters::enqueue( $this->filters_enabled ); } /** * Enqueue Function. * If this field requires any scripts, or CSS define this function and register/enqueue the scripts/css * * @since 1.0.0 * @access public * @return void */ public function enqueue() { if ( function_exists( 'wp_enqueue_media' ) ) { wp_enqueue_media(); } else { wp_enqueue_script( 'media-upload' ); } wp_enqueue_script( 'redux-field-media', Redux_Core::$url . 'assets/js/media/media' . Redux_Functions::is_min() . '.js', array( 'jquery', 'redux-js' ), $this->timestamp, true ); if ( $this->parent->args['dev_mode'] ) { wp_enqueue_style( 'redux-field-media' ); } } /** * Compile CSS styles for output. * * @param string $data CSS data. * * @return string|null */ public function css_style( $data ): string { $data = (array) $data; if ( isset( $data['filter'] ) ) { return $this->get_filter_css( $data['filter'] ); } return ''; } /** * Get filter CSS. * * @param array $data Data. * * @return string */ private function get_filter_css( array $data ): string { $css = ''; foreach ( $data as $filter => $values ) { $checked = filter_var( $values['checked'], FILTER_VALIDATE_BOOLEAN ); if ( true === $checked ) { $unit = Redux_Image_Filters::get_filter_unit( $filter ); if ( '°' === $unit ) { $unit = 'deg'; } $css .= ' ' . $filter . '(' . $values['value'] . $unit . ')'; } } if ( '' !== $css ) { return 'filter:' . $css . ';-webkit-filter:' . $css . ';'; } return ''; } } } class_alias( 'Redux_Media', 'ReduxFramework_Media' );