PluginProbe
WP Console – WordPress PHP Console powered by PsySH / trunk
WP Console – WordPress PHP Console powered by PsySH vtrunk
trunk 2.4.0 2.4.1 2.5.0 2.5.1 2.6.0
wp-console / includes / Core / UserSettings / Controller.php

Controller.php in WP Console – WordPress PHP Console powered by PsySH trunk, at includes/Core/UserSettings/Controller.php

79 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPConsole\Core\UserSettings;
4
5 class Controller {
6
7 /**
8 * Settings schema
9 *
10 * @since 2.0.0
11 *
12 * @return array
13 */
14 public function get_settings_schema() {
15 return apply_filters( 'wp_console_user_settings_schema', [] );
16 }
17
18 /**
19 * Get user settings
20 *
21 * @since 2.0.0
22 *
23 * @param int $user_id
24 *
25 * @return array
26 */
27 public function get( $user_id ) {
28 $user_settings = [];
29 $saved_settings = get_user_meta( $user_id, 'wp_console_user_settings', true );
30 $saved_settings = is_array( $saved_settings ) ? $saved_settings : [];
31
32 $settings_schema = $this->get_settings_schema();
33
34 foreach ( $settings_schema as $section => $section_props ) {
35 $options = $section_props['properties'];
36
37 foreach ( $options as $option => $schema_value ) {
38 $value = $schema_value['default'];
39
40 if ( isset( $saved_settings[ $section ][ $option ] ) ) {
41 $value = $saved_settings[ $section ][ $option ];
42 }
43
44 $user_settings[ $section ][ $option ] = $value;
45 }
46 }
47
48 return $user_settings;
49 }
50
51 /**
52 * Save user settings
53 *
54 * @since 2.0.0
55 *
56 * @param int $user_id
57 * @param array $settings
58 *
59 * @return array
60 */
61 public function save( $user_id, $settings ) {
62 $updated_settings = [];
63 $current_settings = wp_console()->user_settings->get( $user_id );
64
65 foreach ( $current_settings as $section => $options ) {
66 foreach ( $options as $option => $value) {
67 $updated_settings[ $section ][ $option ]
68 = isset( $settings[ $section ][ $option ] )
69 ? $settings[ $section ][ $option ]
70 : $value;
71 }
72 }
73
74 update_user_meta( $user_id, 'wp_console_user_settings', $updated_settings );
75
76 return $updated_settings;
77 }
78 }
79