PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.1.1
Kirki – Freeform Page Builder, Website Builder & Customizer v6.1.1
6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / customizer / packages / controls / image / src / Field / Image.php
kirki / customizer / packages / controls / image / src / Field Last commit date
CSS 5 months ago Image.php 2 months ago
Image.php
170 lines
1 <?php // phpcs:disable PHPCompatibility.FunctionDeclarations.NewClosure
2 /**
3 * Override field methods
4 *
5 * @package kirki-framework/control-image
6 * @copyright Copyright (c) 2023, Themeum
7 * @license https://opensource.org/licenses/MIT
8 * @since 1.0
9 */
10
11 namespace Kirki\Field;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 use Kirki\Field;
18
19 /**
20 * Field overrides.
21 */
22 class Image extends Field {
23
24 /**
25 * The field type.
26 *
27 * @access public
28 * @since 1.0
29 * @var string
30 */
31 public $type = 'kirki-image';
32
33 /**
34 * The control class-name.
35 *
36 * @access protected
37 * @since 0.1
38 * @var string
39 */
40 protected $control_class = '\Kirki\Control\Image';
41
42 /**
43 * Whether we should register the control class for JS-templating or not.
44 *
45 * @access protected
46 * @since 0.1
47 * @var bool
48 */
49 protected $control_has_js_template = true;
50
51 /**
52 * Additional logic for this field.
53 *
54 * @access protected
55 * @since 0.1
56 * @param array $args The field arguments.
57 * @return void
58 */
59 protected function init( $args ) {
60 add_filter( 'kirki_output_item_args', [ $this, 'output_item_args' ], 10, 4 );
61 add_filter( 'kirki_output_control_classnames', [ $this, 'output_control_classnames' ] );
62 }
63
64 /**
65 * Filter arguments before creating the setting.
66 *
67 * @access public
68 * @since 0.1
69 * @param array $args The field arguments.
70 * @param WP_Customize_Manager $wp_customize The customizer instance.
71 * @return array
72 */
73 public function filter_setting_args( $args, $wp_customize ) {
74 if ( $args['settings'] === $this->args['settings'] ) {
75 $args = parent::filter_setting_args( $args, $wp_customize );
76
77 // Set the sanitize-callback if none is defined.
78 if ( ! isset( $args['sanitize_callback'] ) || ! $args['sanitize_callback'] ) {
79 $args['sanitize_callback'] = function( $value ) {
80 if ( isset( $this->args['choices']['save_as'] ) && 'array' === $this->args['choices']['save_as'] ) {
81 return [
82 'id' => ( isset( $value['id'] ) && '' !== $value['id'] ) ? (int) $value['id'] : '',
83 'url' => ( isset( $value['url'] ) && '' !== $value['url'] ) ? esc_url_raw( $value['url'] ) : '',
84 'width' => ( isset( $value['width'] ) && '' !== $value['width'] ) ? (int) $value['width'] : '',
85 'height' => ( isset( $value['height'] ) && '' !== $value['height'] ) ? (int) $value['height'] : '',
86 ];
87 }
88 if ( isset( $this->args['choices']['save_as'] ) && 'id' === $this->args['choices']['save_as'] ) {
89 return absint( $value );
90 }
91 return ( is_string( $value ) ) ? esc_url_raw( $value ) : $value;
92 };
93 }
94 }
95 return $args;
96 }
97
98 /**
99 * Filter arguments before creating the control.
100 *
101 * @access public
102 * @since 0.1
103 * @param array $args The field arguments.
104 * @param WP_Customize_Manager $wp_customize The customizer instance.
105 * @return array
106 */
107 public function filter_control_args( $args, $wp_customize ) {
108 if ( $args['settings'] === $this->args['settings'] ) {
109 $args = parent::filter_control_args( $args, $wp_customize );
110
111 $args['button_labels'] = isset( $args['button_labels'] ) ? $args['button_labels'] : [];
112 $args['button_labels'] = wp_parse_args(
113 $args['button_labels'],
114 [
115 'select' => esc_html__( 'Select image', 'kirki' ),
116 'change' => esc_html__( 'Change image', 'kirki' ),
117 'default' => esc_html__( 'Default', 'kirki' ),
118 'remove' => esc_html__( 'Remove', 'kirki' ),
119 'placeholder' => esc_html__( 'No image selected', 'kirki' ),
120 'frame_title' => esc_html__( 'Select image', 'kirki' ),
121 'frame_button' => esc_html__( 'Choose image', 'kirki' ),
122 ]
123 );
124
125 $args['choices'] = isset( $args['choices'] ) ? (array) $args['choices'] : [];
126 $args['choices']['save_as'] = isset( $args['choices']['save_as'] ) ? $args['choices']['save_as'] : 'url';
127 $args['choices']['labels'] = isset( $args['choices']['labels'] ) ? $args['choices']['labels'] : [];
128 $args['choices']['labels'] = wp_parse_args( $args['choices']['labels'], $args['button_labels'] );
129
130 // Set the control-type.
131 $args['type'] = 'kirki-image';
132 }
133 return $args;
134 }
135
136 /**
137 * Filter for output argument used by the kirki-framework/module-css module.
138 *
139 * @access public
140 * @since 1.0
141 * @param array $output A single output item.
142 * @param mixed $value The value.
143 * @param array $all_outputs All field output args.
144 * @param array $field The field arguments.
145 * @return array
146 */
147 public function output_item_args( $output, $value, $all_outputs, $field ) {
148 if ( $field['settings'] === $this->args['settings'] ) {
149 if ( isset( $output['property'] ) && in_array( [ 'background', 'background-image' ], $output['property'], true ) ) {
150 if ( ! isset( $output['value_pattern'] ) || empty( $output['value_pattern'] ) || '$' === $output['value_pattern'] ) {
151 $output['value_pattern'] = 'url("$")';
152 }
153 }
154 }
155 return $output;
156 }
157
158 /**
159 * Adds a custom output class for typography fields.
160 *
161 * @access public
162 * @since 1.0
163 * @param array $classnames The array of classnames.
164 * @return array
165 */
166 public function output_control_classnames( $classnames ) {
167 $classnames['kirki-image'] = '\Kirki\Field\CSS\Image';
168 return $classnames;
169 }
170 }