PluginProbe
Property Hive / trunk
Property Hive vtrunk
2.2.6 2.2.5 2.2.4 2.2.3 2.2.2 1.4.46 1.4.47 1.4.48 1.4.49 1.4.5 1.4.50 1.4.51 1.4.52 1.4.53 1.4.54 1.4.55 1.4.56 1.4.57 1.4.58 1.4.59 1.4.6 1.4.60 1.4.61 1.4.62 1.4.63 All 259 releases
propertyhive / includes / admin / class-ph-admin-profile.php

class-ph-admin-profile.php in Property Hive trunk, at includes/admin/class-ph-admin-profile.php

272 lines 10.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Add extra profile fields for users in admin
4 *
5 * @author PropertyHive
6 * @category Admin
7 * @package PropertyHive/Admin
8 * @version 1.0.0
9 */
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit; // Exit if accessed directly
13 }
14
15 if ( ! class_exists( 'PH_Admin_Profile', false ) ) :
16
17 /**
18 * PH_Admin_Profile Class.
19 */
20 class PH_Admin_Profile {
21
22 /**
23 * Hook in tabs.
24 */
25 public function __construct() {
26 add_action( 'show_user_profile', array( $this, 'extra_user_meta_fields' ) );
27 add_action( 'edit_user_profile', array( $this, 'extra_user_meta_fields' ) );
28
29 add_action( 'personal_options_update', array( $this, 'save_extra_user_meta_fields' ) );
30 add_action( 'edit_user_profile_update', array( $this, 'save_extra_user_meta_fields' ) );
31 }
32
33 /**
34 * Get fields for the edit user pages.
35 *
36 * @return array Fields to display which are filtered through propertyhive_user_meta_fields before being returned
37 */
38 public function get_user_meta_fields() {
39
40 $args = array(
41 'post_type' => 'office',
42 'nopaging' => true,
43 'orderby' => 'post_title',
44 'order' => 'ASC',
45 );
46
47 $offices = array();
48
49 $offices_query = new WP_Query( $args );
50
51 if ( $offices_query->have_posts() )
52 {
53 while ( $offices_query->have_posts() )
54 {
55 $offices_query->the_post();
56
57 $offices[get_the_ID()] = get_the_title();
58 }
59 }
60 wp_reset_postdata();
61
62 $fields = array(
63 'office_id' => array(
64 'label' => __( 'Office', 'propertyhive' ),
65 'description' => '',
66 'type' => 'select',
67 'options' => array( '' => __( 'Select an office', 'propertyhive' ) ) + $offices,
68 ),
69 'telephone_number' => array(
70 'label' => __( 'Telephone Number', 'propertyhive' ),
71 'description' => '',
72 'type' => 'text',
73 ),
74 'photo_attachment_id' => array(
75 'label' => __( 'Photo', 'propertyhive' ),
76 'description' => '',
77 'type' => 'image',
78 ),
79 );
80
81 $user = wp_get_current_user();
82 $roles = (array)$user->roles;
83
84 $fields['crm_only_mode'] = array(
85 'label' => __( 'Property Hive-Only Mode', 'propertyhive' ),
86 'description' => __( 'Enabling this option will remove all top level WordPress menu items leaving just Property Hive options making it easier to navigate and use as a CRM', 'propertyhive' ),
87 'type' => in_array('administrator', $roles) ? 'checkbox' : 'hidden',
88 );
89
90 $show_fields = array(
91 'negotiator' => array(
92 'title' => __( 'Property Hive Negotiator Details', 'propertyhive' ),
93 'fields' => $fields,
94 ),
95 );
96
97 $show_fields = apply_filters( 'propertyhive_user_meta_fields', $show_fields );
98
99 return $show_fields;
100 }
101
102 /**
103 * Show fields on edit user pages.
104 *
105 * @param WP_User $user
106 */
107 public function extra_user_meta_fields( $user ) {
108
109 if ( ! current_user_can( 'manage_propertyhive' ) ) {
110 return;
111 }
112
113 $user_meta = get_userdata($user->ID);
114 $user_roles = $user_meta->roles;
115
116 if ( ! in_array("administrator", $user_roles) && ! in_array("editor", $user_roles) ) {
117 return;
118 }
119
120 $show_fields = $this->get_user_meta_fields();
121
122 foreach ( $show_fields as $fieldset_key => $fieldset ) :
123 ?>
124 <h2><?php echo esc_html($fieldset['title']); ?></h2>
125 <table class="form-table" id="<?php echo esc_attr( 'fieldset-' . $fieldset_key ); ?>">
126 <?php foreach ( $fieldset['fields'] as $key => $field ) : ?>
127 <?php
128 if ( ! empty( $field['type'] ) && 'hidden' === $field['type'] )
129 {
130 ?>
131 <input type="hidden" name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" value="<?php echo esc_attr( $this->get_user_meta( $user->ID, $key ) != '' ? $this->get_user_meta( $user->ID, $key ) : ( isset( $field['default'] ) ? $field['default'] : '' ) ); ?>" />
132 <?php
133 }
134 else
135 {
136 ?>
137 <tr>
138 <th>
139 <label for="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $field['label'] ); ?></label>
140 </th>
141 <td>
142 <?php if ( ! empty( $field['type'] ) && 'select' === $field['type'] ) : ?>
143 <select name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" class="<?php echo ( isset($field['class']) ? esc_attr($field['class']) : '' ); ?>" style="width: 25em;">
144 <?php
145 $selected = esc_attr( get_user_meta( $user->ID, $key, true ) );
146 foreach ( $field['options'] as $option_key => $option_value ) :
147 ?>
148 <option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $selected, $option_key, true ); ?>><?php echo esc_html( $option_value ); ?></option>
149 <?php endforeach; ?>
150 </select>
151 <?php elseif ( ! empty( $field['type'] ) && 'checkbox' === $field['type'] ) : ?>
152 <input type="checkbox" name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" value="1" class="<?php echo isset($field['class']) ? esc_attr( $field['class'] ) : ''; ?>" <?php checked( (int) get_user_meta( $user->ID, $key, true ), 1, true ); ?> />
153 <?php elseif ( ! empty( $field['type'] ) && 'color' === $field['type'] ) : ?>
154 <input type="color" name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" value="<?php echo esc_attr( $this->get_user_meta( $user->ID, $key ) != '' ? $this->get_user_meta( $user->ID, $key ) : ( isset( $field['default'] ) ? $field['default'] : '' ) ); ?>" class="<?php echo esc_attr( $field['class'] ); ?>" />
155 <?php elseif ( ! empty( $field['type'] ) && 'button' === $field['type'] ) : ?>
156 <button type="button" id="<?php echo esc_attr( $key ); ?>" class="button <?php echo esc_attr( $field['class'] ); ?>"><?php echo esc_html( $field['text'] ); ?></button>
157 <?php elseif ( ! empty( $field['type'] ) && 'image' === $field['type'] ) : ?>
158 <input type="hidden" name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" class="photo-attachment-id" value="<?php echo esc_attr( $this->get_user_meta( $user->ID, $key ) ); ?>" />
159 <div class="photo-attachment-image">
160 <?php if ( !empty($this->get_user_meta( $user->ID, $key )) ) { echo wp_get_attachment_image( $this->get_user_meta( $user->ID, $key ), 'thumbnail'); } ?>
161 </div>
162 <div class="wp-media-buttons">
163 <button class="button propertyhive-add-media" id="propertyhive-add-media"><?php echo esc_html(__('Select', 'propertyhive')); ?></button>
164 </div>
165 <script>
166
167 var file_frame;
168
169 jQuery(document).ready(function()
170 {
171 jQuery('body').on('click', '.propertyhive-add-media', function( event ){
172
173 event.preventDefault();
174
175 // If the media frame already exists, reopen it.
176 if ( file_frame ) {
177 file_frame.open();
178 return;
179 }
180
181 // Create the media frame.
182 file_frame = wp.media.frames.file_frame = wp.media({
183 multiple: false
184 });
185
186 // When an image is selected, run a callback.
187 file_frame.on( 'select', function() {
188 var selection = file_frame.state().get('selection');
189
190 selection.map( function( attachment ) {
191
192 attachment = attachment.toJSON();
193
194 jQuery('#<?php echo esc_attr( $key ); ?>').val(attachment.id);
195
196 var photo_html = '<img src="' + attachment.url + '" style="max-width:150px; max-height:150px;" alt=""></li>';
197
198 jQuery('.photo-attachment-image').html(photo_html);
199 });
200 });
201
202 // Finally, open the modal
203 file_frame.open();
204 });
205 });
206 </script>
207 <?php else: ?>
208 <input type="text" name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" value="<?php echo esc_attr( $this->get_user_meta( $user->ID, $key ) ); ?>" class="<?php echo ( ! empty( $field['class'] ) ? esc_attr( $field['class'] ) : 'regular-text' ); ?>" />
209 <?php endif; ?>
210 <p class="description"><?php echo wp_kses_post( $field['description'] ); ?></p>
211 </td>
212 </tr>
213 <?php
214 }
215 ?>
216 <?php endforeach; ?>
217 </table>
218 <?php
219 endforeach;
220 }
221
222 /**
223 * Save Address Fields on edit user pages.
224 *
225 * @param int $user_id User ID of the user being saved
226 */
227 public function save_extra_user_meta_fields( $user_id ) {
228
229 if ( ! current_user_can( 'manage_propertyhive' ) ) {
230 return;
231 }
232
233 $user_meta = get_userdata($user_id);
234 $user_roles = $user_meta->roles;
235
236 if ( ! in_array("administrator", $user_roles) && ! in_array("editor", $user_roles) ) {
237 return;
238 }
239
240 $save_fields = $this->get_user_meta_fields();
241
242 foreach ( $save_fields as $fieldset ) {
243
244 foreach ( $fieldset['fields'] as $key => $field ) {
245
246 if ( isset( $field['type'] ) && 'checkbox' === $field['type'] ) {
247 update_user_meta( $user_id, $key, isset( $_POST[ $key ] ) );
248 } elseif ( isset( $_POST[ $key ] ) ) {
249 update_user_meta( $user_id, $key, ph_clean( $_POST[ $key ] ) );
250 }
251 }
252 }
253 }
254
255 /**
256 * Get user meta for a given key, with fallbacks to core user info for pre-existing fields.
257 *
258 * @param int $user_id User ID of the user being edited
259 * @param string $key Key for user meta field
260 * @return string
261 */
262 protected function get_user_meta( $user_id, $key ) {
263 $value = get_user_meta( $user_id, $key, true );
264
265 return $value;
266 }
267 }
268
269 endif;
270
271 return new PH_Admin_Profile();
272