| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\WpRest\Classes; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; // Exit if accessed directly. |
| 7 |
} |
| 8 |
|
| 9 |
class Elementor_User_Meta { |
| 10 |
|
| 11 |
private function get_meta_config(): array { |
| 12 |
return [ |
| 13 |
'elementor_introduction' => [ |
| 14 |
'schema' => [ |
| 15 |
'description' => 'Elementor user meta data', |
| 16 |
'type' => 'object', |
| 17 |
'properties' => [ |
| 18 |
'ai_get_started' => [ |
| 19 |
'type' => 'boolean', |
| 20 |
], |
| 21 |
], |
| 22 |
'additionalProperties' => true, |
| 23 |
'context' => [ 'view', 'edit' ], |
| 24 |
], |
| 25 |
], |
| 26 |
]; |
| 27 |
} |
| 28 |
|
| 29 |
public function register(): void { |
| 30 |
foreach ( $this->get_meta_config() as $key => $config ) { |
| 31 |
$config['get_callback'] = function( $user, $field_name, $request ) { |
| 32 |
return get_user_meta( $user['id'], $field_name, true ); |
| 33 |
}; |
| 34 |
|
| 35 |
$config['update_callback'] = function( $meta_value, \WP_User $user, $field_name, $request ) { |
| 36 |
if ( 'PATCH' === $request->get_method() ) { |
| 37 |
$existing = get_user_meta( $user->ID, $field_name, true ); |
| 38 |
if ( is_array( $existing ) && is_array( $meta_value ) ) { |
| 39 |
$meta_value = array_merge( $existing, $meta_value ); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
return update_user_meta( $user->ID, $field_name, $meta_value ); |
| 44 |
}; |
| 45 |
|
| 46 |
register_rest_field( 'user', $key, $config ); |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
|