PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.0.13
Kirki – Freeform Page Builder, Website Builder & Customizer v6.0.13
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 / upload / src / Field / Upload.php
kirki / customizer / packages / controls / upload / src / Field Last commit date
Upload.php 5 months ago
Upload.php
153 lines
1 <?php
2 /**
3 * Override field methods
4 *
5 * @package kirki-framework/control-typography
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 use Kirki\Field;
14
15 /**
16 * Field overrides.
17 *
18 * @since 1.0
19 */
20 class Upload extends Field {
21
22 /**
23 * The field type.
24 *
25 * @access public
26 * @since 1.0
27 * @var string
28 */
29 public $type = 'kirki-upload';
30
31 /**
32 * The control class-name.
33 *
34 * @access protected
35 * @since 0.1
36 * @var string
37 */
38 protected $control_class = '\Kirki\Control\Upload';
39
40 /**
41 * Filter arguments before creating the setting.
42 *
43 * @access public
44 * @since 0.1
45 * @param array $args The field arguments.
46 * @param WP_Customize_Manager $wp_customize The customizer instance.
47 * @return array
48 */
49 public function filter_setting_args( $args, $wp_customize ) {
50 if ( $args['settings'] === $this->args['settings'] ) {
51 $args = parent::filter_setting_args( $args, $wp_customize );
52
53 // Set the sanitize-callback if none is defined.
54 if ( ! isset( $args['sanitize_callback'] ) || ! $args['sanitize_callback'] ) {
55
56 $args['sanitize_callback'] = function( $value ) {
57 $save_as = isset( $this->args['choices']['save_as'] ) ? $this->args['choices']['save_as'] : 'url';
58
59 return self::sanitize( $value, $save_as );
60 };
61
62 }
63 }
64
65 return $args;
66 }
67
68 /**
69 * Filter arguments before creating the control.
70 *
71 * @access public
72 * @since 0.1
73 * @param array $args The field arguments.
74 * @param WP_Customize_Manager $wp_customize The customizer instance.
75 * @return array
76 */
77 public function filter_control_args( $args, $wp_customize ) {
78 if ( $args['settings'] === $this->args['settings'] ) {
79 $args = parent::filter_control_args( $args, $wp_customize );
80 $args['type'] = 'upload';
81 }
82 return $args;
83 }
84
85 /**
86 * Sanitizes the field value.
87 *
88 * @since 1.0.2
89 *
90 * @param mixed $value The field value.
91 * @param string $save_as The expected saving format.
92 *
93 * @return mixed
94 */
95 public static function sanitize( $value, $save_as = 'url' ) {
96
97 if ( 'array' === $save_as ) {
98
99 if ( is_array( $value ) ) {
100 return [
101 'id' => ( isset( $value['id'] ) && '' !== $value['id'] ) ? (int) $value['id'] : '',
102 'url' => ( isset( $value['url'] ) && '' !== $value['url'] ) ? esc_url_raw( $value['url'] ) : '',
103 'filename' => ( isset( $value['filename'] ) && '' !== $value['filename'] ) ? sanitize_text_field( $value['filename'] ) : '',
104 ];
105 } elseif ( is_string( $value ) && ! is_numeric( $value ) ) {
106 // Here, we assume that the value is the URL.
107 $attachment_id = attachment_url_to_postid( $value );
108
109 return [
110 'id' => $attachment_id,
111 'url' => $value,
112 'filename' => basename( get_attached_file( $attachment_id ) ),
113 ];
114 } else {
115 // Here, we assume that the value is int or numeric (the attachment ID).
116 $value = absint( $value );
117
118 return [
119 'id' => $value,
120 'url' => wp_get_attachment_url( $value ),
121 'filename' => basename( get_attached_file( $value ) ),
122 ];
123 }
124 } elseif ( 'id' === $save_as ) {
125
126 if ( is_string( $value ) && ! is_numeric( $value ) ) {
127 // Here, we assume that the value is the URL.
128 return attachment_url_to_postid( $value );
129 } elseif ( is_array( $value ) && isset( $value['id'] ) ) {
130 return absint( $value['id'] );
131 }
132
133 // Here, we assume that the value is int or numeric (the attachment ID).
134 return absint( $value );
135
136 }
137
138 // If we're reaching this point, then we're saving the URL.
139 if ( is_array( $value ) && isset( $value['url'] ) ) {
140 $value = $value['url'];
141 } elseif ( is_numeric( $value ) ) {
142 $value = absint( $value );
143 $value = wp_get_attachment_url( $value );
144 } else {
145 $value = esc_url_raw( $value );
146 }
147
148 return $value;
149
150 }
151
152 }
153