PluginProbe
Stripe Payment Forms by WP Simple Pay – Accept Credit Card Payments + Subscriptions with Stripe / trunk
Stripe Payment Forms by WP Simple Pay – Accept Credit Card Payments + Subscriptions with Stripe vtrunk
4.17.3 trunk 2.2.0 2.3.0 2.3.1 2.3.2 2.3.3 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 4.10.0 4.11.1 4.12.2 4.14.1 4.14.2 4.14.3 4.15.0 4.16.0 All 59 releases
stripe / src / Admin / FormBuilder / FormStyle / Settings.php

Settings.php in Stripe Payment Forms by WP Simple Pay – Accept Credit Card Payments + Subscriptions with Stripe trunk, at src/Admin/FormBuilder/FormStyle/Settings.php

235 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles getting and setting style meta values.
4 *
5 * @package SimplePay\Core\Admin\FormBuilder\FormStyle
6 */
7
8 namespace SimplePay\Core\Admin\FormBuilder\FormStyle;
9
10 // Exit if accessed directly.
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13 }
14
15 /**
16 * Settings handler class.
17 *
18 * Handles getting, setting, and managing style meta values for forms.
19 * Provides methods for retrieving, saving, and deleting style settings,
20 * as well as checking if settings exist and sanitizing values.
21 *
22 * @since 4.17.0
23 */
24 class Settings {
25
26 /**
27 * The prefix for all style meta keys.
28 *
29 * @var string
30 */
31 private static $meta_prefix = '_wpsp_style_';
32
33 /**
34 * Get a specific style setting for a form.
35 *
36 * @since 4.17.0
37 *
38 * @param int $form_id The WP Simple Pay form ID (Post ID).
39 * @param string $key The setting key (e.g., 'primary_color').
40 * @param string $default Optional. The default value if the setting is not found.
41 * @return string The setting value.
42 */
43 public static function get_setting( $form_id, $key, $default = '' ) {
44 $meta_key = self::$meta_prefix . sanitize_key( $key );
45 $value = get_post_meta( $form_id, $meta_key, true );
46
47 $value = self::sanitize_setting( $key, $value );
48
49 // For border radius settings, treat empty and '0' consistently.
50 if ( in_array( $key, array( 'border_radius', 'form_border_radius' ), true ) && '0' === $value && '' === $default ) {
51 $default = '0';
52 }
53
54 return ( '' !== $value ) ? $value : $default;
55 }
56
57 /**
58 * Get the raw setting value without default fallback.
59 *
60 * @since 4.17.0
61 *
62 * @param int $form_id The WP Simple Pay form ID (Post ID).
63 * @param string $key The setting key (e.g., 'primary_color').
64 * @return string The raw setting value (can be empty string).
65 */
66 public static function get_raw_setting( $form_id, $key ) {
67 $meta_key = self::$meta_prefix . sanitize_key( $key );
68 $value = get_post_meta( $form_id, $meta_key, true );
69
70 if ( '' === $value || false === $value ) {
71 return '';
72 }
73
74 return self::sanitize_setting( $key, $value );
75 }
76
77 /**
78 * Save a specific style setting for a form.
79 *
80 * @since 4.17.0
81 *
82 * @param int $form_id The WP Simple Pay form ID (Post ID).
83 * @param string $key The setting key (e.g., 'primary_color').
84 * @param mixed $value The value to save.
85 * @return bool|int Meta ID if the key didn't exist, true on successful update, false on failure.
86 */
87 public static function save_setting( $form_id, $key, $value ) {
88 $meta_key = self::$meta_prefix . sanitize_key( $key );
89
90 $value = self::sanitize_setting( $key, $value );
91
92 return update_post_meta( $form_id, $meta_key, $value );
93 }
94
95 /**
96 * Get all style setting keys.
97 *
98 * Returns an array of all available style setting keys that can be
99 * used with get_setting() and save_setting() methods.
100 *
101 * @since 4.17.0
102 *
103 * @return array<string> Array of style setting keys.
104 */
105 public static function get_style_keys(): array {
106 return array(
107 'selected_theme',
108 'form_container_background_color',
109 'background_color',
110 'text_color',
111 'label_text_color',
112 'input_text_color',
113 'border_color',
114 'primary_color',
115 'button_background_color',
116 'button_text_color',
117 'button_hover_background_color',
118 'border_radius',
119 'form_border_radius',
120 'form_padding',
121 'label_font_size',
122 'label_font_weight',
123 'input_font_size',
124 'error_border_color',
125 'error_text_color',
126 'title_color',
127 'title_font_size',
128 'title_font_weight',
129 'description_color',
130 'description_font_size',
131 'description_font_weight',
132 );
133 }
134
135 /**
136 * Delete a specific style setting for a post.
137 *
138 * Removes a style setting from the database for the specified post.
139 *
140 * @since 4.17.0
141 *
142 * @param int $post_id The post ID.
143 * @param string $key The setting key (without prefix).
144 * @return void
145 */
146 public static function delete_setting( $post_id, $key ) {
147 delete_post_meta( $post_id, self::$meta_prefix . sanitize_key( $key ) );
148 }
149
150 /**
151 * Check if a specific style setting exists for a post.
152 *
153 * Determines if a style setting has been saved for the specified post.
154 *
155 * @since 1.0.0
156 *
157 * @param int $post_id The post ID.
158 * @param string $key The setting key (without prefix).
159 * @return bool True if the meta key exists, false otherwise.
160 */
161 public static function setting_exists( $post_id, $key ) {
162 return metadata_exists( 'post', $post_id, self::$meta_prefix . sanitize_key( $key ) );
163 }
164
165 /**
166 * Sanitize a setting value.
167 *
168 * Sanitizes a setting value based on its key to ensure data integrity
169 * and security before use.
170 *
171 * @since 4.17.0
172 *
173 * @param string $key Setting key.
174 * @param mixed $value Setting value.
175 * @return string Sanitized setting value.
176 */
177 public static function sanitize_setting( $key, $value ) {
178 $value = is_scalar( $value ) ? (string) $value : '';
179
180 switch ( $key ) {
181 case 'selected_theme':
182 return sanitize_text_field( $value );
183 case 'form_container_background_color':
184 case 'background_color':
185 case 'text_color':
186 case 'label_text_color':
187 case 'input_text_color':
188 case 'border_color':
189 case 'primary_color':
190 case 'button_background_color':
191 case 'button_text_color':
192 case 'button_hover_background_color':
193 case 'error_border_color':
194 case 'error_text_color':
195 case 'title_color':
196 case 'description_color':
197 // Handle empty values.
198 if ( empty( $value ) || '' === trim( $value ) ) {
199 return '';
200 }
201 // Try to sanitize as hex color first.
202 $hex_sanitized = sanitize_hex_color( $value );
203 // If sanitize_hex_color returns a value, use it.
204 if ( ! empty( $hex_sanitized ) ) {
205 return $hex_sanitized;
206 }
207 // Validate rgba/rgb/hsla/hsl color formats to prevent CSS injection.
208 if ( preg_match( '/^(rgba?|hsla?)\(\s*[\d.,\s%]+\)$/', $value ) ) {
209 return sanitize_text_field( $value );
210 }
211 // Reject any other value to prevent CSS injection.
212 return '';
213 case 'border_radius':
214 case 'form_border_radius':
215 case 'form_padding':
216 case 'label_font_size':
217 case 'input_font_size':
218 case 'title_font_size':
219 case 'description_font_size':
220 if ( '' === trim( $value ) ) {
221 return '';
222 }
223
224 return (string) absint( $value );
225 case 'label_font_weight':
226 case 'title_font_weight':
227 case 'description_font_weight':
228 $allowed_values = array( '', 'normal', 'bold', '100', '200', '300', '400', '500', '600', '700', '800', '900' );
229 return in_array( $value, $allowed_values, true ) ? $value : '';
230 default:
231 return $value;
232 }
233 }
234 }
235