PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 3.3.4
Pods – Custom Content Types and Fields v3.3.4
trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 3.2.5 3.2.6 3.2.7 3.2.7.1 3.2.8 3.2.8.1 3.2.8.2 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9
pods / src / Pods / Admin / Settings.php
pods / src / Pods / Admin Last commit date
Config 1 year ago Service_Provider.php 3 years ago Settings.php 1 year ago
Settings.php
411 lines
1 <?php
2
3 namespace Pods\Admin;
4
5 use PodsForm;
6
7 /**
8 * Settings specific functionality.
9 *
10 * @since 2.8.0
11 */
12 class Settings {
13
14 const OPTION_NAME = 'pods_settings';
15
16 /**
17 * Add the class hooks.
18 *
19 * @since 2.8.0
20 */
21 public function hook() {
22 add_filter( 'pods_admin_settings_fields', [ $this, 'add_settings_fields' ], 9 );
23 }
24
25 /**
26 * Remove the class hooks.
27 *
28 * @since 2.8.0
29 */
30 public function unhook() {
31 remove_filter( 'pods_admin_settings_fields', [ $this, 'add_settings_fields' ], 9 );
32 }
33
34 /**
35 * Get the value for a Pods setting.
36 *
37 * @since 2.8.0
38 *
39 * @param string $setting_name The setting name.
40 * @param null $default The default value if the setting is not yet set.
41 *
42 * @return mixed The setting value.
43 */
44 public function get_setting( $setting_name, $default = null ) {
45 $settings = $this->get_settings();
46
47 $setting = pods_v( $setting_name, $settings, $default );
48
49 if ( null !== $default && ( null === $setting || '' === $setting ) ) {
50 return $default;
51 }
52
53 return $setting;
54 }
55
56 /**
57 * Get the Pods settings.
58 *
59 * @since 2.8.0
60 *
61 * @return array The setting values.
62 */
63 public function get_settings() {
64 $settings = get_option( self::OPTION_NAME, [] );
65
66 if ( ! $settings ) {
67 $settings = [];
68 }
69
70 // Register settings with Wisdom Tracker.
71 $settings['wisdom_registered_setting'] = 1;
72
73 static $defaults;
74
75 if ( null === $defaults ) {
76 $defaults = $this->get_setting_fields();
77 }
78
79 $layout_field_types = PodsForm::layout_field_types();
80
81 // Set up defaults as needed.
82 foreach ( $defaults as $setting_name => $setting ) {
83 // Skip layout field types.
84 if ( isset( $setting['type'] ) && in_array( $setting['type'], $layout_field_types, true ) ) {
85 continue;
86 }
87
88 // Skip if we do not have a default to set.
89 if ( ! isset( $setting['default'] ) ) {
90 continue;
91 }
92
93 // Skip if we do not
94 if ( isset( $settings[ $setting_name ] ) && ! in_array( $settings[ $setting_name ], [ null, '' ], true ) ) {
95 continue;
96 }
97
98 $settings[ $setting_name ] = $setting['default'];
99 }
100
101 return $settings;
102 }
103
104 /**
105 * Update the value for a Pods setting.
106 *
107 * @since 2.8.0
108 *
109 * @param string $setting_name The setting name.
110 * @param mixed $setting_value The setting value.
111 */
112 public function update_setting( $setting_name, $setting_value ) {
113 $settings = $this->get_settings();
114
115 if ( null !== $setting_value ) {
116 $settings[ $setting_name ] = $setting_value;
117 } elseif ( isset( $settings[ $setting_name ] ) ) {
118 unset( $settings[ $setting_name ] );
119 }
120
121 $this->update_option( $settings );
122 }
123
124 /**
125 * Update the settings for a Pods.
126 *
127 * @since 2.8.0
128 *
129 * @param array $setting_values The list of settings to update, pass null as a value to remove it.
130 */
131 public function update_settings( array $setting_values ) {
132 $settings = $this->get_settings();
133 $settings = array_merge( $settings, $setting_values );
134
135 foreach ( $settings as $setting_name => $setting_value ) {
136 if ( null === $setting_value ) {
137 unset( $settings[ $setting_name ] );
138 }
139 }
140
141 $this->update_option( $settings );
142 }
143
144 /**
145 * Handle saving the Pods settings to the option.
146 *
147 * @param array $settings The Pods settings to be saved.
148 */
149 private function update_option( array $settings ) {
150 /**
151 * Allow filtering whether Pods settings are set to autoload.
152 *
153 * @param string $autoload Whether Pods settings should be saved as autoload, set to 'yes' to autoload (default) and 'no' to not autoload.
154 */
155 $autoload = apply_filters( 'pods_admin_settings_autoload', 'yes' );
156
157 update_option( self::OPTION_NAME, $settings, $autoload );
158 }
159
160 public function __( string $text, string $domain, bool $did_init ): string {
161 return $did_init ? __( $text, $domain ) : $text;
162 }
163
164 /**
165 * Get the list of Pods settings fields.
166 *
167 * @since 2.8.0
168 *
169 * @return array The list of Pods settings fields.
170 */
171 public function get_setting_fields() {
172 // Only use translation functions after `init` to prevent a WP core notice.
173 $did_init = doing_action( 'init' ) || did_action( 'init' );
174
175 $disabled_text = $did_init ? __( 'This setting is disabled because it is forced through the constant/filter elsewhere.', 'pods' ) : '';
176 $current_value = $did_init ? __( 'Current value', 'pods' ) : '';
177
178 $fields['core'] = [
179 'label' => $did_init ? __( 'Core', 'pods' ) : '',
180 'type' => 'heading',
181 ];
182
183 $is_types_only = pods_is_types_only( true );
184 $is_types_only_overridden = null !== $is_types_only;
185
186 $is_types_only_disabled_text = sprintf(
187 '%1$s<br /><strong>%2$s: %3$s</strong>',
188 $disabled_text,
189 $current_value,
190 ! $is_types_only ? ( $did_init ? __( 'Enabled', 'pods' ) : '' ) : ( $did_init ? __( 'Disabled', 'pods' ) : '' )
191 );
192
193 $fields['types_only'] = [
194 'name' => 'types_only',
195 'label' => $did_init ? __( 'Allow Pods to create and manage custom fields on any content type created/extended through Pods', 'pods' ) : '',
196 'help' => $did_init ? __( 'By default, Pods allows you to create custom fields for any content type that you create/extend with Pods. If you only intend to use Pods for content types themselves and not to add custom fields, Disabling Custom Fields can improve performance on your site. When disabled, this is known as the types-only mode feature.', 'pods' ) : '',
197 'type' => 'pick',
198 'default' => '0',
199 'readonly' => $is_types_only_overridden,
200 'description' => $is_types_only_overridden ? $is_types_only_disabled_text : '',
201 'pick_format_type' => 'single',
202 'pick_format_single' => 'radio',
203 'data' => [
204 '0' => $did_init ? __( 'Enable creating custom fields with Pods', 'pods' ) : '',
205 '1' => $did_init ? __( 'Disable creating custom fields with Pods (for when using Pods only for content types)', 'pods' ) : '',
206 ],
207 'site_health_data' => [
208 '0' => $did_init ? __( 'Enable', 'pods' ) : '',
209 '1' => $did_init ? __( 'Disable', 'pods' ) : '',
210 ],
211 'site_health_include_in_info' => true,
212 ];
213
214 $fields['performance'] = [
215 'label' => $did_init ? __( 'Performance', 'pods' ) : '',
216 'type' => 'heading',
217 ];
218
219 $first_pods_version = get_option( 'pods_framework_version_first' );
220 $first_pods_version = '' === $first_pods_version ? PODS_VERSION : $first_pods_version;
221
222 $fields['watch_changed_fields'] = [
223 'name' => 'watch_changed_fields',
224 'label' => $did_init ? __( 'Watch changed fields for use in hooks', 'pods' ) : '',
225 'help' => $did_init ? __( 'By default, Pods does not watch changed fields when a post, term, user, or other Pods items are saved. Enabling this will allow you to use PHP hooks to reference the previous values of those fields after the save has happened.', 'pods' ) : '',
226 'type' => 'pick',
227 'default' => version_compare( $first_pods_version, '2.8.21', '<' ) ? '1' : '0',
228 'pick_format_type' => 'single',
229 'pick_format_single' => 'radio',
230 'data' => [
231 '1' => $did_init ? __( 'Enable watching changed fields (may reduce performance with large processes)', 'pods' ) : '',
232 '0' => $did_init ? __( 'Disable watching changed fields', 'pods' ) : '',
233 ],
234 'site_health_data' => [
235 '1' => $did_init ? __( 'Enable', 'pods' ) : '',
236 '0' => $did_init ? __( 'Disable', 'pods' ) : '',
237 ],
238 'site_health_include_in_info' => true,
239 ];
240
241 $fields['metadata_integration'] = [
242 'name' => 'metadata_integration',
243 'label' => $did_init ? __( 'Watch WP Metadata calls', 'pods' ) : '',
244 'help' => $did_init ? __( 'By default, Pods will watch Metadata calls and send any values to table-based fields as well as index relationship IDs when they are saved. You can disable this if you do not use table-based Pods and you only want to query meta-based Pods or settings.', 'pods' ) : '',
245 'type' => 'pick',
246 'default' => ( function_exists( 'wc_get_product' ) || version_compare( $first_pods_version, '2.9.14', '<' ) ) ? '1' : '0',
247 'pick_format_type' => 'single',
248 'pick_format_single' => 'radio',
249 'data' => [
250 '1' => $did_init ? __( 'Enable watching WP Metadata calls (may reduce performance with large processes)', 'pods' ) : '',
251 '0' => $did_init ? __( 'Disable watching WP Metadata calls', 'pods' ) : '',
252 ],
253 'site_health_data' => [
254 '1' => $did_init ? __( 'Enable', 'pods' ) : '',
255 '0' => $did_init ? __( 'Disable', 'pods' ) : '',
256 ],
257 'dependency' => true,
258 'site_health_include_in_info' => true,
259 ];
260
261 $fields['metadata_override_get'] = [
262 'name' => 'metadata_override_get',
263 'label' => $did_init ? __( 'Override WP Metadata values', 'pods' ) : '',
264 'help' => $did_init ? __( 'By default, Pods will override Metadata values when calling functions like get_post_meta() so that it can provide more Relationship / File field context.', 'pods' ) : '',
265 'type' => 'pick',
266 'default' => version_compare( $first_pods_version, '2.8.21', '<' ) ? '1' : '0',
267 'pick_format_type' => 'single',
268 'pick_format_single' => 'radio',
269 'data' => [
270 '1' => $did_init ? __( 'Enable overriding WP Metadata values (may conflict with certain plugins and decrease performance with large processes)', 'pods' ) : '',
271 '0' => $did_init ? __( 'Disable overriding WP Metadata values', 'pods' ) : '',
272 ],
273 'site_health_data' => [
274 '1' => $did_init ? __( 'Enable', 'pods' ) : '',
275 '0' => $did_init ? __( 'Disable', 'pods' ) : '',
276 ],
277 'depends-on' => [ 'metadata_integration' => '1' ],
278 'site_health_include_in_info' => true,
279 ];
280
281 $fields['register_meta_integration'] = [
282 'name' => 'register_meta_integration',
283 'label' => $did_init ? __( 'Register meta fields', 'pods' ) : '',
284 'help' => [
285 $did_init ? __( 'If you register meta fields within WordPress using the register_meta() API then WordPress and other plugins can be aware of the details of that specific field configuration.', 'pods' ) : '',
286 'https://developer.wordpress.org/reference/functions/register_meta/',
287 ],
288 'type' => 'pick',
289 'default' => '0',
290 'pick_format_type' => 'single',
291 'pick_format_single' => 'radio',
292 'data' => [
293 '1' => $did_init ? __( 'Enable registering meta fields through the WP Meta API (may reduce performance on sites with many Pods and fields)', 'pods' ) : '',
294 '0' => $did_init ? __( 'Disable registering meta fields through the WP Meta API', 'pods' ) : '',
295 ],
296 'site_health_data' => [
297 '1' => $did_init ? __( 'Enable', 'pods' ) : '',
298 '0' => $did_init ? __( 'Disable', 'pods' ) : '',
299 ],
300 'site_health_include_in_info' => true,
301 ];
302
303 $fields['media_modal_fields'] = [
304 'name' => 'media_modal_fields',
305 'label' => $did_init ? __( 'Show Pods fields in Media Library modals', 'pods' ) : '',
306 'help' => $did_init ? __( 'This feature is only used when you have extended the WordPress Media object with Pods', 'pods' ) : '',
307 'type' => 'pick',
308 'default' => version_compare( $first_pods_version, '2.9.16', '<' ) ? '1' : '0',
309 'pick_format_type' => 'single',
310 'pick_format_single' => 'radio',
311 'data' => [
312 '1' => $did_init ? __( 'Enable showing Pods fields in Media Library modals (may decrease performance with large numbers of items on admin screens with media grids)', 'pods' ) : '',
313 '0' => $did_init ? __( 'Disable showing Pods fields in Media Library modals and only show them when in the full edit screen for an attachment', 'pods' ) : '',
314 ],
315 'site_health_data' => [
316 '0' => $did_init ? __( 'Enable', 'pods' ) : '',
317 '1' => $did_init ? __( 'Disable', 'pods' ) : '',
318 ],
319 'site_health_include_in_info' => true,
320 ];
321
322 $session_auto_start = pods_session_auto_start( true );
323 $session_auto_start_overridden = null !== $session_auto_start;
324
325 $fields['security'] = [
326 'label' => $did_init ? __( 'Security', 'pods' ) : '',
327 'type' => 'heading',
328 ];
329
330 $session_auto_start_disabled_text = sprintf(
331 '%1$s<br /><strong>%2$s: %3$s</strong>',
332 $disabled_text,
333 $current_value,
334 $session_auto_start ? ( $did_init ? __( 'Enabled', 'pods' ) : '' ) : ( $did_init ? __( 'Disabled', 'pods' ) : '' )
335 );
336
337 $fields['session_auto_start'] = [
338 'name' => 'session_auto_start',
339 'label' => $did_init ? __( 'Secure anonymous public form submissions using PHP sessions (potential performance impacts)', 'pods' ) : '',
340 'help' => $did_init ? __( 'Sessions will be used to secure submissions from public forms from logged out visitors to ensure they do not submit fields they are not allowed to access. Auto-detecting sessions will automatically turn this setting on the first anonymous submission so that future submissions will be secured going forward.', 'pods' ) : '',
341 'type' => 'pick',
342 'default' => '0',
343 'readonly' => $session_auto_start_overridden,
344 'description' => $session_auto_start_overridden ? $session_auto_start_disabled_text : '',
345 'pick_format_type' => 'single',
346 'pick_format_single' => 'radio',
347 'data' => [
348 'auto' => $did_init ? __( 'Auto-detect sessions (enable on first anonymous submission)', 'pods' ) : '',
349 '1' => $did_init ? __( 'Enable sessions (may decrease performance)', 'pods' ) : '',
350 '0' => $did_init ? __( 'Disable sessions', 'pods' ) : '',
351 ],
352 'site_health_data' => [
353 'auto' => $did_init ? __( 'Auto-detect', 'pods' ) : '',
354 '1' => $did_init ? __( 'Enable', 'pods' ) : '',
355 '0' => $did_init ? __( 'Disable', 'pods' ) : '',
356 ],
357 'site_health_include_in_info' => true,
358 ];
359
360 $access_fields = pods_access_settings_config();
361
362 if ( $access_fields ) {
363 $fields = array_merge( $fields, $access_fields );
364 }
365
366 $pods_init = pods_init();
367
368 $is_wisdom_opted_out = ! $pods_init->stats_tracking || ! $pods_init->stats_tracking->get_is_tracking_allowed();
369
370 $fields['wisdom-opt-in'] = [
371 'label' => $did_init ? __( 'Stats Tracking', 'pods' ) : '',
372 'type' => 'heading',
373 ];
374
375 // Only register if they are already opted-in.
376 $fields['wisdom_opt_out'] = [
377 'name' => 'wisdom_opt_out',
378 'label' => $did_init ? __( 'Would you like to opt-out of tracking?', 'pods' ) : '',
379 'description' => ( $did_init ? __( 'Thank you for installing our plugin. We\'d like your permission to track its usage on your site. We won\'t record any sensitive data, only information regarding the WordPress environment and your plugin settings. We will only use this information help us make improvements to the plugin and provide better support when you reach out. Tracking is completely optional.', 'pods' ) : '' )
380 . "\n\n"
381 . ( $did_init ? __( 'Any information collected is not shared with third-parties and you will not be signed up for mailing lists.', 'pods' ) : '' ),
382 'type' => 'pick',
383 'default' => $is_wisdom_opted_out ? '1' : '',
384 'pick_format_type' => 'single',
385 'pick_format_single' => 'radio',
386 'data' => [
387 '' => $did_init ? __( 'Track usage on my site', 'pods' ) : '',
388 '1' => $did_init ? __( 'DO NOT track usage on my site', 'pods' ) : '',
389 ],
390 ];
391
392 return $fields;
393 }
394
395 /**
396 * Add custom settings fields.
397 *
398 * @since 2.8.0
399 *
400 * @param array $fields List of fields to filter.
401 *
402 * @return array List of filtered fields.
403 */
404 public function add_settings_fields( $fields ) {
405 $setting_fields = $this->get_setting_fields();
406
407 return array_merge( $fields, $setting_fields );
408 }
409
410 }
411