| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package smart-custom-fields |
| 4 |
* @author inc2734 |
| 5 |
* @license GPL-2.0+ |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Smart_Custom_Fields_Controller_Profile class. |
| 10 |
*/ |
| 11 |
class Smart_Custom_Fields_Controller_Profile extends Smart_Custom_Fields_Controller_Base { |
| 12 |
|
| 13 |
/** |
| 14 |
* __construct |
| 15 |
*/ |
| 16 |
public function __construct() { |
| 17 |
parent::__construct(); |
| 18 |
add_action( 'show_user_profile', array( $this, 'user_profile' ) ); |
| 19 |
add_action( 'edit_user_profile', array( $this, 'user_profile' ) ); |
| 20 |
add_action( 'personal_options_update', array( $this, 'update' ) ); |
| 21 |
add_action( 'edit_user_profile_update', array( $this, 'update' ) ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Loading resources for profile edit page. |
| 26 |
* |
| 27 |
* @param string $hook The current admin page. |
| 28 |
*/ |
| 29 |
public function admin_enqueue_scripts( $hook ) { |
| 30 |
parent::admin_enqueue_scripts( $hook ); |
| 31 |
|
| 32 |
wp_enqueue_style( |
| 33 |
SCF_Config::PREFIX . 'profile', |
| 34 |
SMART_CUSTOM_FIELDS_URL . '/css/profile.css', |
| 35 |
array(), |
| 36 |
filemtime( SMART_CUSTOM_FIELDS_PATH . '/css/profile.css' ) |
| 37 |
); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Displaying custom fields. |
| 42 |
* |
| 43 |
* @param WP_User $user WP_User object. |
| 44 |
*/ |
| 45 |
public function user_profile( $user ) { |
| 46 |
printf( '<h3>%s</h3>', esc_html__( 'Custom Fields', 'smart-custom-fields' ) ); |
| 47 |
$settings = SCF::get_settings( $user ); |
| 48 |
$callback_args = array(); |
| 49 |
foreach ( $settings as $setting ) { |
| 50 |
$callback_args['args'] = $setting->get_groups(); |
| 51 |
?> |
| 52 |
<table class="form-table"> |
| 53 |
<tr> |
| 54 |
<th scope="row"><?php echo esc_html( $setting->get_title() ); ?></th> |
| 55 |
<td><?php $this->display_meta_box( $user, $callback_args ); ?></td> |
| 56 |
</tr> |
| 57 |
</table> |
| 58 |
<?php |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Saving meta data from custom fields in profile edit page. |
| 64 |
* |
| 65 |
* @param int $user_id User id. |
| 66 |
*/ |
| 67 |
public function update( $user_id ) { |
| 68 |
if ( ! current_user_can( 'edit_user', $user_id ) ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
if ( ! filter_input( INPUT_POST, SCF_Config::NAME, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ) ) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
$this->save( filter_input_array( INPUT_POST ), get_userdata( $user_id ) ); |
| 77 |
} |
| 78 |
} |
| 79 |
|