1 <?php
2
3 /**
4 * Use fieldmanager on the user profile screen and save data primarily to user
5 * meta.
6 *
7 * @package Fieldmanager_Context
8 */
9 class Fieldmanager_Context_User extends Fieldmanager_Context_Storable {
10
11 /**
12 * Group Title
13 * @var string
14 */
15 public $title;
16
17 /**
18 * Add fieldmanager to user form
19 * @param string $title
20 * @param Fieldmanager_Field $fm
21 */
22 public function __construct( $title = '', $fm = null ) {
23 $this->title = $title;
24 $this->fm = $fm;
25 add_action( 'show_user_profile', array( $this, 'render_user_form' ) );
26 add_action( 'edit_user_profile', array( $this, 'render_user_form' ) );
27 add_action( 'personal_options_update', array( $this, 'save_user_form' ) );
28 add_action( 'edit_user_profile_update', array( $this, 'save_user_form' ) );
29 add_filter( 'fm_context_after_presave_data', array( $this, 'legacy_presave_filter' ) );
30 }
31
32 /**
33 * Maintain legacy support for custom filter.
34 *
35 * @deprecated
36 *
37 * @param mixed $data Data being saved.
38 * @return mixed
39 */
40 public function legacy_presave_filter( $data ) {
41 return apply_filters( 'fm_user_presave_data', $data, $this->fm );
42 }
43
44 /**
45 * Render the form on the user profile page
46 * @return void
47 */
48 public function render_user_form( $user ) {
49 $this->fm->data_id = $user->ID;
50 $this->fm->data_type = 'user';
51
52 if ( !empty( $this->title ) ) {
53 echo '<h3>' . esc_html( $this->title ) . '</h3>';
54 }
55 echo '<div class="fm-user-form-wrapper">';
56 $this->render_field();
57 echo '</div>';
58
59 // Check if any validation is required
60 $fm_validation = Fieldmanager_Util_Validation( 'your-profile', 'user' );
61 $fm_validation->add_field( $this->fm );
62 }
63
64 /**
65 * Save user form.
66 *
67 * @param int $user_id
68 */
69 public function save_user_form( $user_id ) {
70 if ( ! $this->is_valid_nonce() ) {
71 return;
72 }
73
74 if ( ! current_user_can( 'edit_user', $user_id ) ) {
75 $this->fm->_unauthorized_access( __( 'Current user cannot edit this user', 'fieldmanager' ) );
76 return;
77 }
78
79 $this->save_to_user_meta( $user_id );
80 }
81
82 /**
83 * Save data to user meta.
84 *
85 * @param int $user_id
86 */
87 public function save_to_user_meta( $user_id, $data = null ) {
88 $this->fm->data_id = $user_id;
89 $this->fm->data_type = 'user';
90
91 $this->save( $data );
92 }
93
94 /**
95 * Get user meta.
96 *
97 * @see get_user_meta().
98 */
99 protected function get_data( $user_id, $meta_key, $single = false ) {
100 return call_user_func(
101 /**
102 * Allow control over retrieving from the user data storage.
103 * This improves compatibility with WordPress.com.
104 *
105 * @see get_user_meta() for more details about each param.
106 *
107 * @param string $function_name The function to call to get user
108 * data. Default is 'get_user_meta'.
109 * @param int $user_id User ID.
110 * @param string $meta_key Meta key to retrieve.
111 * @param boolean $single Get single value (true) or array of values
112 * (false). Default is false.
113 */
114 apply_filters( 'fm_user_context_get_data', 'get_user_meta' ),
115 $user_id,
116 $meta_key,
117 $single
118 );
119 }
120
121 /**
122 * Add user meta.
123 *
124 * @see add_user_meta().
125 */
126 protected function add_data( $user_id, $meta_key, $meta_value, $unique = false ) {
127 return call_user_func(
128 /**
129 * Allow control over adding to the user data storage.
130 * This improves compatibility with WordPress.com.
131 *
132 * @see add_user_meta() for more details about each param.
133 *
134 * @param string $function_name The function to call to get user
135 * data. Default is 'add_user_meta'.
136 * @param int $user_id User ID.
137 * @param string $meta_key Meta key to add.
138 * @param mixed $meta_value The meta value to store.
139 * @param boolean $unique If true, only add if key is unique.
140 * Default is false.
141 */
142 apply_filters( 'fm_user_context_add_data', 'add_user_meta' ),
143 $user_id,
144 $meta_key,
145 $meta_value,
146 $unique
147 );
148 }
149
150 /**
151 * Update user meta.
152 *
153 * @see update_user_meta().
154 */
155 protected function update_data( $user_id, $meta_key, $meta_value, $data_prev_value = '' ) {
156 return call_user_func(
157 /**
158 * Allow control over updating the user data storage.
159 * This improves compatibility with WordPress.com.
160 *
161 * @see update_user_meta() for more details about each param.
162 *
163 * @param string $function_name The function to call to get user
164 * data. Default is 'update_user_meta'.
165 * @param int $user_id User ID.
166 * @param string $meta_key Meta key to update.
167 * @param mixed $meta_value The meta value to store.
168 * @param mixed $data_prev_value Only update if the previous value
169 * matches $data_prev_value.
170 */
171 apply_filters( 'fm_user_context_update_data', 'update_user_meta' ),
172 $user_id,
173 $meta_key,
174 $meta_value,
175 $data_prev_value
176 );
177 }
178
179 /**
180 * Delete user meta.
181 *
182 * @see delete_user_meta().
183 */
184 protected function delete_data( $user_id, $meta_key, $meta_value = '' ) {
185 return call_user_func(
186 /**
187 * Allow control over deleting from the user data storage.
188 * This improves compatibility with WordPress.com.
189 *
190 * @see delete_user_meta() for more details about each param.
191 *
192 * @param string $function_name The function to call to get user
193 * data. Default is 'delete_user_meta'.
194 * @param int $user_id User ID.
195 * @param string $meta_key Meta key to retrieve.
196 * @param mixed $meta_value Only delete if the current value matches.
197 */
198 apply_filters( 'fm_user_context_delete_data', 'delete_user_meta' ),
199 $user_id,
200 $meta_key,
201 $meta_value
202 );
203 }
204 }