PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / trunk
Pods – Custom Content Types and Fields vtrunk
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 4 months ago Service_Provider.php 4 months ago Settings.php 4 months ago
Settings.php
425 lines
1 <?php
2
3 namespace Pods\Admin;
4
5 // Don't load directly.
6 if ( ! defined( 'ABSPATH' ) ) {
7 die( '-1' );
8 }
9
10 use PodsForm;
11
12 /**
13 * Settings specific functionality.
14 *
15 * @since 2.8.0
16 */
17 class Settings {
18
19 const OPTION_NAME = 'pods_settings';
20
21 /**
22 * Add the class hooks.
23 *
24 * @since 2.8.0
25 */
26 public function hook() {
27 add_filter( 'pods_admin_settings_fields', [ $this, 'add_settings_fields' ], 9 );
28 }
29
30 /**
31 * Remove the class hooks.
32 *
33 * @since 2.8.0
34 */
35 public function unhook() {
36 remove_filter( 'pods_admin_settings_fields', [ $this, 'add_settings_fields' ], 9 );
37 }
38
39 /**
40 * Get the value for a Pods setting.
41 *
42 * @since 2.8.0
43 *
44 * @param string $setting_name The setting name.
45 * @param null $default The default value if the setting is not yet set.
46 *
47 * @return mixed The setting value.
48 */
49 public function get_setting( $setting_name, $default = null ) {
50 $settings = $this->get_settings();
51
52 $setting = pods_v( $setting_name, $settings, $default );
53
54 if ( null !== $default && ( null === $setting || '' === $setting ) ) {
55 return $default;
56 }
57
58 return $setting;
59 }
60
61 /**
62 * Get the Pods settings.
63 *
64 * @since 2.8.0
65 *
66 * @return array The setting values.
67 */
68 public function get_settings() {
69 $settings = get_option( self::OPTION_NAME, [] );
70
71 if ( ! $settings ) {
72 $settings = [];
73 }
74
75 // Register settings with Wisdom Tracker.
76 $settings['wisdom_registered_setting'] = 1;
77
78 static $defaults;
79
80 if ( null === $defaults ) {
81 $defaults = $this->get_setting_fields();
82 }
83
84 $layout_field_types = PodsForm::layout_field_types();
85
86 // Set up defaults as needed.
87 foreach ( $defaults as $setting_name => $setting ) {
88 // Skip layout field types.
89 if ( isset( $setting['type'] ) && in_array( $setting['type'], $layout_field_types, true ) ) {
90 continue;
91 }
92
93 // Skip if we do not have a default to set.
94 if ( ! isset( $setting['default'] ) ) {
95 continue;
96 }
97
98 // Skip if we do not
99 if ( isset( $settings[ $setting_name ] ) && ! in_array( $settings[ $setting_name ], [ null, '' ], true ) ) {
100 continue;
101 }
102
103 $settings[ $setting_name ] = $setting['default'];
104 }
105
106 return $settings;
107 }
108
109 /**
110 * Update the value for a Pods setting.
111 *
112 * @since 2.8.0
113 *
114 * @param string $setting_name The setting name.
115 * @param mixed $setting_value The setting value.
116 */
117 public function update_setting( $setting_name, $setting_value ) {
118 $settings = $this->get_settings();
119
120 if ( null !== $setting_value ) {
121 $settings[ $setting_name ] = $setting_value;
122 } elseif ( isset( $settings[ $setting_name ] ) ) {
123 unset( $settings[ $setting_name ] );
124 }
125
126 $this->update_option( $settings );
127 }
128
129 /**
130 * Update the settings for a Pods.
131 *
132 * @since 2.8.0
133 *
134 * @param array $setting_values The list of settings to update, pass null as a value to remove it.
135 */
136 public function update_settings( array $setting_values ) {
137 $settings = $this->get_settings();
138 $settings = array_merge( $settings, $setting_values );
139
140 foreach ( $settings as $setting_name => $setting_value ) {
141 if ( null === $setting_value ) {
142 unset( $settings[ $setting_name ] );
143 }
144 }
145
146 $this->update_option( $settings );
147 }
148
149 /**
150 * Handle saving the Pods settings to the option.
151 *
152 * @param array $settings The Pods settings to be saved.
153 */
154 private function update_option( array $settings ) {
155 /**
156 * Allow filtering whether Pods settings are set to autoload.
157 *
158 * @param string $autoload Whether Pods settings should be saved as autoload, set to 'yes' to autoload (default) and 'no' to not autoload.
159 */
160 $autoload = apply_filters( 'pods_admin_settings_autoload', 'yes' );
161
162 update_option( self::OPTION_NAME, $settings, $autoload );
163 }
164
165 /**
166 * Get the list of Pods settings fields.
167 *
168 * @since 2.8.0
169 *
170 * @return array The list of Pods settings fields.
171 */
172 public function get_setting_fields() {
173 // Only use translation functions after `init` to prevent a WP core notice.
174 $did_init = doing_action( 'init' ) || did_action( 'init' );
175
176 $disabled_text = $did_init ? __( 'This setting is disabled because it is forced through the constant/filter elsewhere.', 'pods' ) : '';
177 $current_value = $did_init ? __( 'Current value', 'pods' ) : '';
178
179 $fields['core'] = [
180 'label' => $did_init ? __( 'Core', 'pods' ) : '',
181 'type' => 'heading',
182 ];
183
184 $is_types_only = pods_is_types_only( true );
185 $is_types_only_overridden = null !== $is_types_only;
186
187 $is_types_only_disabled_text = sprintf(
188 '%1$s<br /><strong>%2$s: %3$s</strong>',
189 $disabled_text,
190 $current_value,
191 ! $is_types_only ? ( $did_init ? __( 'Enabled', 'pods' ) : '' ) : ( $did_init ? __( 'Disabled', 'pods' ) : '' )
192 );
193
194 $fields['types_only'] = [
195 'name' => 'types_only',
196 'label' => $did_init ? __( 'Allow Pods to create and manage custom fields on any content type created/extended through Pods', 'pods' ) : '',
197 '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' ) : '',
198 'type' => 'pick',
199 'default' => '0',
200 'readonly' => $is_types_only_overridden,
201 'description' => $is_types_only_overridden ? $is_types_only_disabled_text : '',
202 'pick_format_type' => 'single',
203 'pick_format_single' => 'radio',
204 'data' => [
205 '0' => $did_init ? __( 'Enable creating custom fields with Pods', 'pods' ) : '',
206 '1' => $did_init ? __( 'Disable creating custom fields with Pods (for when using Pods only for content types)', 'pods' ) : '',
207 ],
208 'site_health_data' => [
209 '0' => $did_init ? __( 'Enable', 'pods' ) : '',
210 '1' => $did_init ? __( 'Disable', 'pods' ) : '',
211 ],
212 'site_health_include_in_info' => true,
213 ];
214
215 $fields['performance'] = [
216 'label' => $did_init ? __( 'Performance', 'pods' ) : '',
217 'type' => 'heading',
218 ];
219
220 $first_pods_version = get_option( 'pods_framework_version_first' );
221 $first_pods_version = '' === $first_pods_version ? PODS_VERSION : $first_pods_version;
222
223 $fields['watch_changed_fields'] = [
224 'name' => 'watch_changed_fields',
225 'label' => $did_init ? __( 'Watch changed fields for use in hooks', 'pods' ) : '',
226 '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' ) : '',
227 'type' => 'pick',
228 'default' => version_compare( $first_pods_version, '2.8.21', '<' ) ? '1' : '0',
229 'pick_format_type' => 'single',
230 'pick_format_single' => 'radio',
231 'data' => [
232 '1' => $did_init ? __( 'Enable watching changed fields (may reduce performance with large processes)', 'pods' ) : '',
233 '0' => $did_init ? __( 'Disable watching changed fields', 'pods' ) : '',
234 ],
235 'site_health_data' => [
236 '1' => $did_init ? __( 'Enable', 'pods' ) : '',
237 '0' => $did_init ? __( 'Disable', 'pods' ) : '',
238 ],
239 'site_health_include_in_info' => true,
240 ];
241
242 $fields['metadata_integration'] = [
243 'name' => 'metadata_integration',
244 'label' => $did_init ? __( 'Watch WP Metadata calls', 'pods' ) : '',
245 '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' ) : '',
246 'type' => 'pick',
247 'default' => ( function_exists( 'wc_get_product' ) || version_compare( $first_pods_version, '2.9.14', '<' ) ) ? '1' : '0',
248 'pick_format_type' => 'single',
249 'pick_format_single' => 'radio',
250 'data' => [
251 '1' => $did_init ? __( 'Enable watching WP Metadata calls (may reduce performance with large processes)', 'pods' ) : '',
252 '0' => $did_init ? __( 'Disable watching WP Metadata calls', 'pods' ) : '',
253 ],
254 'site_health_data' => [
255 '1' => $did_init ? __( 'Enable', 'pods' ) : '',
256 '0' => $did_init ? __( 'Disable', 'pods' ) : '',
257 ],
258 'dependency' => true,
259 'site_health_include_in_info' => true,
260 ];
261
262 $fields['metadata_override_get'] = [
263 'name' => 'metadata_override_get',
264 'label' => $did_init ? __( 'Override WP Metadata values', 'pods' ) : '',
265 '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' ) : '',
266 'type' => 'pick',
267 'default' => version_compare( $first_pods_version, '2.8.21', '<' ) ? '1' : '0',
268 'pick_format_type' => 'single',
269 'pick_format_single' => 'radio',
270 'data' => [
271 '1' => $did_init ? __( 'Enable overriding WP Metadata values (may conflict with certain plugins and decrease performance with large processes)', 'pods' ) : '',
272 '0' => $did_init ? __( 'Disable overriding WP Metadata values', 'pods' ) : '',
273 ],
274 'site_health_data' => [
275 '1' => $did_init ? __( 'Enable', 'pods' ) : '',
276 '0' => $did_init ? __( 'Disable', 'pods' ) : '',
277 ],
278 'depends-on' => [ 'metadata_integration' => '1' ],
279 'site_health_include_in_info' => true,
280 ];
281
282 $fields['register_meta_integration'] = [
283 'name' => 'register_meta_integration',
284 'label' => $did_init ? __( 'Register meta fields', 'pods' ) : '',
285 'help' => [
286 $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' ) : '',
287 'https://developer.wordpress.org/reference/functions/register_meta/',
288 ],
289 'type' => 'pick',
290 'default' => '0',
291 'pick_format_type' => 'single',
292 'pick_format_single' => 'radio',
293 'data' => [
294 '1' => $did_init ? __( 'Enable registering meta fields through the WP Meta API (may reduce performance on sites with many Pods and fields)', 'pods' ) : '',
295 '0' => $did_init ? __( 'Disable registering meta fields through the WP Meta API', 'pods' ) : '',
296 ],
297 'site_health_data' => [
298 '1' => $did_init ? __( 'Enable', 'pods' ) : '',
299 '0' => $did_init ? __( 'Disable', 'pods' ) : '',
300 ],
301 'site_health_include_in_info' => true,
302 ];
303
304 $fields['media_modal_fields'] = [
305 'name' => 'media_modal_fields',
306 'label' => $did_init ? __( 'Show Pods fields in Media Library modals', 'pods' ) : '',
307 'help' => $did_init ? __( 'This feature is only used when you have extended the WordPress Media object with Pods', 'pods' ) : '',
308 'type' => 'pick',
309 'default' => version_compare( $first_pods_version, '2.9.16', '<' ) ? '1' : '0',
310 'pick_format_type' => 'single',
311 'pick_format_single' => 'radio',
312 'data' => [
313 '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' ) : '',
314 '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' ) : '',
315 ],
316 'site_health_data' => [
317 '0' => $did_init ? __( 'Enable', 'pods' ) : '',
318 '1' => $did_init ? __( 'Disable', 'pods' ) : '',
319 ],
320 'site_health_include_in_info' => true,
321 ];
322
323 $fields['autocomplete_limit'] = [
324 'name' => 'autocomplete_limit',
325 'label' => $did_init ? __( 'Limit autocomplete search results in Relationship fields', 'pods' ) : '',
326 'help' => $did_init ? __( 'This is the default result limit to use for Relationship fields when doing autocomplete searches. For performance reasons, you will want to keep this number low. To remove the limit, set the value to "-1".', 'pods' ) : '',
327 'type' => 'number',
328 'default' => '15',
329 'number_format_type' => 'number',
330 'number_format' => '9999.99',
331 'number_min' => -1,
332 'number_html5' => true,
333 'site_health_include_in_info' => true,
334 ];
335
336 $session_auto_start = pods_session_auto_start( true );
337 $session_auto_start_overridden = null !== $session_auto_start;
338
339 $fields['security'] = [
340 'label' => $did_init ? __( 'Security', 'pods' ) : '',
341 'type' => 'heading',
342 ];
343
344 $session_auto_start_disabled_text = sprintf(
345 '%1$s<br /><strong>%2$s: %3$s</strong>',
346 $disabled_text,
347 $current_value,
348 $session_auto_start ? ( $did_init ? __( 'Enabled', 'pods' ) : '' ) : ( $did_init ? __( 'Disabled', 'pods' ) : '' )
349 );
350
351 $fields['session_auto_start'] = [
352 'name' => 'session_auto_start',
353 'label' => $did_init ? __( 'Secure anonymous public form submissions using PHP sessions (potential performance impacts)', 'pods' ) : '',
354 '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' ) : '',
355 'type' => 'pick',
356 'default' => '0',
357 'readonly' => $session_auto_start_overridden,
358 'description' => $session_auto_start_overridden ? $session_auto_start_disabled_text : '',
359 'pick_format_type' => 'single',
360 'pick_format_single' => 'radio',
361 'data' => [
362 'auto' => $did_init ? __( 'Auto-detect sessions (enable on first anonymous submission)', 'pods' ) : '',
363 '1' => $did_init ? __( 'Enable sessions (may decrease performance)', 'pods' ) : '',
364 '0' => $did_init ? __( 'Disable sessions', 'pods' ) : '',
365 ],
366 'site_health_data' => [
367 'auto' => $did_init ? __( 'Auto-detect', 'pods' ) : '',
368 '1' => $did_init ? __( 'Enable', 'pods' ) : '',
369 '0' => $did_init ? __( 'Disable', 'pods' ) : '',
370 ],
371 'site_health_include_in_info' => true,
372 ];
373
374 $access_fields = pods_access_settings_config();
375
376 if ( $access_fields ) {
377 $fields = array_merge( $fields, $access_fields );
378 }
379
380 $pods_init = pods_init();
381
382 $is_wisdom_opted_out = ! $pods_init->stats_tracking || ! $pods_init->stats_tracking->get_is_tracking_allowed();
383
384 $fields['wisdom-opt-in'] = [
385 'label' => $did_init ? __( 'Stats Tracking', 'pods' ) : '',
386 'type' => 'heading',
387 ];
388
389 // Only register if they are already opted-in.
390 $fields['wisdom_opt_out'] = [
391 'name' => 'wisdom_opt_out',
392 'label' => $did_init ? __( 'Would you like to opt-out of tracking?', 'pods' ) : '',
393 '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' ) : '' )
394 . "\n\n"
395 . ( $did_init ? __( 'Any information collected is not shared with third-parties and you will not be signed up for mailing lists.', 'pods' ) : '' ),
396 'type' => 'pick',
397 'default' => $is_wisdom_opted_out ? '1' : '',
398 'pick_format_type' => 'single',
399 'pick_format_single' => 'radio',
400 'data' => [
401 '' => $did_init ? __( 'Track usage on my site', 'pods' ) : '',
402 '1' => $did_init ? __( 'DO NOT track usage on my site', 'pods' ) : '',
403 ],
404 ];
405
406 return $fields;
407 }
408
409 /**
410 * Add custom settings fields.
411 *
412 * @since 2.8.0
413 *
414 * @param array $fields List of fields to filter.
415 *
416 * @return array List of filtered fields.
417 */
418 public function add_settings_fields( $fields ) {
419 $setting_fields = $this->get_setting_fields();
420
421 return array_merge( $fields, $setting_fields );
422 }
423
424 }
425