| | |
| | | namespace JVBase\rest\routes; |
| | | |
| | | use JVBase\JVB; |
| | | use JVBase\rest\RestRouteManager; |
| | | use JVBase\rest\PermissionHandler; |
| | | use JVBase\rest\Rest; |
| | | use JVBase\managers\Cache; |
| | | use JVBase\meta\MetaManager; |
| | | use JVBase\meta\MetaSanitizer; |
| | | use JVBase\meta\Meta; |
| | | use JVBase\meta\Sanitizer; |
| | | use JVBase\rest\Route; |
| | | use WP_REST_Request; |
| | | use WP_REST_Response; |
| | | use WP_Error; |
| | |
| | | if (!defined('ABSPATH')) { |
| | | exit; // Exit if accessed directly |
| | | } |
| | | class SettingsRoutes extends RestRouteManager |
| | | class SettingsRoutes extends Rest |
| | | { |
| | | protected int $count; |
| | | public function __construct() |
| | | { |
| | | $this->cache_name = 'user_settings'; |
| | | $this->cacheName = 'user_settings'; |
| | | parent::__construct(); |
| | | $this->action = 'dash-'; |
| | | $this->operation_type = 'user_settings'; |
| | | // $this->action = 'dash-'; |
| | | // $this->operation_type = 'user_settings'; |
| | | $this->count = 1; |
| | | add_filter(BASE.'handle_bulk_operation', [$this, 'processOperation'], 10, 3); |
| | | } |
| | |
| | | */ |
| | | public function registerRoutes():void |
| | | { |
| | | register_rest_route($this->namespace, '/settings', [ |
| | | [ |
| | | 'methods' => 'POST', |
| | | 'callback' => [$this, 'saveSettings'], |
| | | 'permission_callback' => [$this, 'checkPermission'] |
| | | ]]); |
| | | Route::for('settings') |
| | | ->post([$this, 'saveSettings']) |
| | | ->auth(PermissionHandler::combine(['admin', ['actionNonce' => 'dash-']])) |
| | | ->rateLimit(20) |
| | | ->register(); |
| | | } |
| | | |
| | | /** |
| | |
| | | $data = $request->get_params(); |
| | | error_log('User: '.print_r($data['user'], true)); |
| | | error_log('Settings routes data: '.print_r($data, true)); |
| | | $user_id = (int)$data['user']; |
| | | if (!$this->userCheck($user_id)) { |
| | | return new WP_REST_Response([ |
| | | 'success' => false, |
| | | 'message' => 'Looks like you are not who you say you are. Bot?' |
| | | ]); |
| | | $user_id = absint($data['user']??0); |
| | | if ($user_id === 0) { |
| | | return $this->unauthorized(); |
| | | } |
| | | |
| | | |
| | | $fields = JVB()->getFields('user'); |
| | | $meta = new MetaSanitizer(); |
| | | $meta = new Sanitizer(); |
| | | $add = []; |
| | | global $types; |
| | | foreach ($data as $name => $value) { |
| | |
| | | ] |
| | | ); |
| | | |
| | | // Return standardized response |
| | | return new WP_REST_Response([ |
| | | 'success' =>true, |
| | | return $this->success([ |
| | | 'message' => 'Request received and queued', |
| | | 'status' => 'queued' |
| | | ]); |
| | |
| | | |
| | | |
| | | $user_id = $operation->user_id; |
| | | $meta = new MetaManager($user_id, 'user'); |
| | | $meta = Meta::forUser($user_id); |
| | | $results = []; |
| | | $tempMeta = new MetaManager(null, 'options'); |
| | | $tempMeta = Meta::forOptions('options'); |
| | | foreach ($data as $name => $value) { |
| | | if ($name == 'notification_preferences') { |
| | | return $this->saveNotificationPreferences($user_id, $value); |
| | |
| | | |
| | | if ($name !== 'digest_override') { // digest_override should always reset to 'off' |
| | | if(array_key_exists($name, JVB_OPTIONS)) { |
| | | $results[] = $tempMeta->updateValue($name, $value); |
| | | $results[] = $tempMeta->set($name, $value); |
| | | } else { |
| | | $results[] = $meta->updateValue($name, $value); |
| | | $results[] = $meta->set($name, $value); |
| | | } |
| | | |
| | | } |
| | | $this->cache->flush(); |
| | | } |
| | | |
| | | return [ |
| | | 'success' => true, |
| | | 'result' => $results, |
| | |
| | | */ |
| | | protected function saveNotificationPreferences(int $user_id, array $data):array|WP_Error |
| | | { |
| | | //TODO: This should be in with Notifications.php manager |
| | | //TODO: Check what cache we are using to store user's preferences for the daily/weekly/monthly sendouts |
| | | global $wpdb; |
| | | $table = $wpdb->prefix . BASE . 'notification_preferences'; |