register_meta(); } /** * Register meta * * @since 1.15.0 */ public function register_meta(): void { register_meta( 'user', static::OPTIN_META_KEY, [ 'type' => 'boolean', 'sanitize_callback' => 'rest_sanitize_boolean', 'default' => false, 'show_in_rest' => true, 'auth_callback' => [ $this, 'can_edit_current_user' ], 'single' => true, ] ); register_meta( 'user', static::MEDIA_OPTIMIZATION_META_KEY, [ 'type' => 'boolean', 'sanitize_callback' => 'rest_sanitize_boolean', 'default' => true, 'show_in_rest' => true, 'auth_callback' => [ $this, 'can_edit_current_user' ], 'single' => true, ] ); register_meta( 'user', static::ONBOARDING_META_KEY, [ 'type' => 'object', 'default' => [], 'show_in_rest' => [ 'schema' => [ 'properties' => [], 'additionalProperties' => true, ], ], 'auth_callback' => [ $this, 'can_edit_current_user' ], 'single' => true, ] ); } /** * Auth callback. * * @since 1.4.0 * * @param bool $allowed Unused. Whether the user can add the object meta. * @param string $meta_key Unused. The meta key. * @param int $user_id ID of the user being edited. * @param int $current_user_id The currently editing user's ID. */ public function can_edit_current_user( bool $allowed, string $meta_key, int $user_id, int $current_user_id ): bool { return user_can( $current_user_id, 'edit_user', $user_id ); } /** * Returns the specific preference for a givern user. * * @since 1.12.0 * * @param int $user_id User ID. * @param string $key Preference key. * @return mixed User preference value. */ public function get_preference( int $user_id, string $key ) { return get_user_meta( $user_id, $key, true ); } /** * Act on plugin uninstall. * * @since 1.26.0 */ public function on_plugin_uninstall(): void { delete_metadata( 'user', 0, self::OPTIN_META_KEY, '', true ); delete_metadata( 'user', 0, self::ONBOARDING_META_KEY, '', true ); delete_metadata( 'user', 0, self::MEDIA_OPTIMIZATION_META_KEY, '', true ); } }