\WP_REST_Server::READABLE, 'callback' => array($this, 'handle_batch'), 'permission_callback' => '__return_true', 'args' => array( 'product_ids' => array( 'required' => false, 'sanitize_callback' => static function ($v) { $out = array(); foreach (is_array($v) ? $v : explode(',', (string) $v) as $x) { $i = (int) $x; if ($i > 0) { $out[] = $i; } } return array_slice(array_values(array_unique($out)), 0, 200); }, ), 'object_type' => array('required' => false, 'default' => 'product'), 'kind' => array('required' => false, 'default' => ''), 'keys' => array( 'required' => false, 'sanitize_callback' => static function ($v) { $keys = array_map('trim', is_array($v) ? $v : explode(',', (string) $v)); return array_slice(array_values(array_unique(array_filter($keys, 'strlen'))), 0, 200); }, ), ), )); register_rest_route('aieo/v1', '/ratings/vote', array( 'methods' => \WP_REST_Server::CREATABLE, 'callback' => array($this, 'handle_vote'), 'permission_callback' => '__return_true', )); register_rest_route('aieo/v1', '/ratings/mine', array( 'methods' => \WP_REST_Server::CREATABLE, 'callback' => array($this, 'handle_mine'), 'permission_callback' => '__return_true', )); } public function handle_batch($req) { $kind = (string) $req->get_param('kind'); $keys = (array) $req->get_param('keys'); if ('' !== $kind && $keys && class_exists('AIEO_Ratings') && class_exists('AIEO_Targets')) { $kind = AIEO_Ratings::kind($kind); $ids = AIEO_Targets::ids($kind, $keys, false); $got = $ids ? AIEO_Ratings::instance()->get_stats_batch(array_values($ids), $kind) : array(); $out = array(); foreach ($keys as $key) { $key = (string) $key; if (isset($ids[$key], $got[$ids[$key]])) { $out[$key] = $got[$ids[$key]]; } } return new \WP_REST_Response(array('stats' => $out ? $out : (object) array(), 'object_type' => $kind, 'currency' => null), 200); } $ids = (array) $req->get_param('product_ids'); $object_type = class_exists('AIEO_Ratings') ? AIEO_Ratings::kind($req->get_param('object_type')) : 'product'; if (empty($ids) || !class_exists('AIEO_Ratings')) { return new \WP_REST_Response(array( 'stats' => (object) array(), 'object_type' => $object_type, 'currency' => null, ), 200); } $stats = AIEO_Ratings::instance()->get_stats_batch($ids, $object_type); $out = array(); foreach ($stats as $pid => $row) { $out[(string) $pid] = $row; } return new \WP_REST_Response(array( 'stats' => empty($out) ? (object) array() : $out, 'object_type' => $object_type, 'currency' => null, ), 200); } private function refuse($req, $what = 'vote') { $origin = (string) $req->get_header('origin'); if ('' !== $origin && !in_array(AIEO_Ratings::origin_of($origin), AIEO_Ratings::origins(), true)) { return new \WP_Error('aieo_rating_origin', __('Votes are taken from this site only.', 'ai-eshop-optimizer'), array('status' => 403)); } if (!$this->within_limits($what)) { return new \WP_Error('aieo_rating_flood', __('Too many votes at once. Please try again in a minute.', 'ai-eshop-optimizer'), array('status' => 429)); } return null; } private function within_limits($what) { $what = 'mine' === $what ? 'mine' : 'vote'; $limits = (array) apply_filters('aieo_rating_limits', 'mine' === $what ? array('minute' => 60, 'hour' => 600) : array('minute' => 20, 'hour' => 200), $what); $ip = isset($_SERVER['REMOTE_ADDR']) ? (string) wp_unslash($_SERVER['REMOTE_ADDR']) : ''; $who = substr(hash_hmac('sha256', $ip, wp_salt('nonce')), 0, 20); foreach (array('minute' => MINUTE_IN_SECONDS, 'hour' => HOUR_IN_SECONDS) as $span => $seconds) { $max = isset($limits[$span]) ? (int) $limits[$span] : 0; if ($max <= 0) { continue; } $key = 'aieo_rf_' . $what . '_' . $span . '_' . $who . '_' . (int) floor(time() / $seconds); $n = (int) get_transient($key); if ($n >= $max) { return false; } set_transient($key, $n + 1, $seconds); } return true; } private function voter($token) { list($sid, $born) = AIEO_Ratings::request_voter($token); return array((int) get_current_user_id(), $sid, $born); } public function handle_vote($req) { if (!class_exists('AIEO_Ratings')) { return new \WP_Error('aieo_rating_off', __('Ratings are disabled.', 'ai-eshop-optimizer'), array('status' => 404)); } $refused = $this->refuse($req); if ($refused) { return $refused; } $settings = AIEO_Ratings::get_settings(); if (empty($settings['enabled'])) { return new \WP_Error('aieo_rating_off', __('Ratings are disabled.', 'ai-eshop-optimizer'), array('status' => 404)); } $body = (array) $req->get_json_params() + (array) $req->get_body_params(); $rating = isset($body['rating']) && is_numeric($body['rating']) ? (int) $body['rating'] : -1; if ($rating < 0 || $rating > 5) { return new \WP_Error('aieo_rating_value', __('Invalid rating value.', 'ai-eshop-optimizer'), array('status' => 400)); } $target = AIEO_Ratings::target((string) ($body['kind'] ?? ''), (string) ($body['key'] ?? ''), $rating > 0); if (null === $target) { return new \WP_Error('aieo_rating_target', __('Nothing of that name can be rated here.', 'ai-eshop-optimizer'), array('status' => 404)); } list($kind, $key, $id) = $target; if ($id <= 0) { return new \WP_REST_Response(array('voter' => '', 'kind' => $kind, 'key' => $key, 'variant' => '', 'rating' => 0, 'stats' => array('count' => 0, 'sum' => 0, 'avg' => 0.0, 'distribution' => array(0, 0, 0, 0, 0))), 200); } $raw_options = isset($body['options']) ? $body['options'] : array(); if ((is_array($raw_options) || is_object($raw_options)) && count((array) $raw_options) > AIEO_Ratings::OPTIONS_MAX) { return new \WP_Error('aieo_rating_options', __('Too many options on one vote.', 'ai-eshop-optimizer'), array('status' => 400)); } list($user_id, $sid, $born) = $this->voter($body['voter'] ?? ''); if (!empty($settings['require_login']) && $user_id <= 0) { return new \WP_Error('aieo_rating_login', __('Please sign in to rate.', 'ai-eshop-optimizer'), array('status' => 401, 'login_url' => wp_login_url())); } $options = AIEO_Ratings::clean_options($raw_options, $kind, $key); $invitee = (class_exists('AIEO_Invitees') && method_exists('AIEO_Invitees', 'current')) ? AIEO_Invitees::current() : array(); $stats = AIEO_Ratings::instance()->record_event(array( 'kind' => $kind, 'key' => $key, 'target' => $id, 'rating' => $rating, 'user_id' => $user_id, 'session' => $sid, 'source' => (string) ($body['source'] ?? ''), 'options' => $options, 'page_url' => AIEO_Ratings::clean_page($body['page'] ?? ''), 'visitor_id' => AIEO_Ratings::request_visitor(), 'invitee_id' => isset($invitee['participant_id']) ? (int) $invitee['participant_id'] : 0, )); AIEO_Ratings::keep_sid($sid); $res = new \WP_REST_Response(array( 'voter' => AIEO_Ratings::voter_token($sid, $born), 'kind' => $kind, 'key' => $key, 'variant' => (string) $stats['variant'], 'rating' => $rating, 'stats' => self::stats_out($stats), ), 200); $res->header('Cache-Control', 'no-store'); return $res; } public function handle_mine($req) { if (!class_exists('AIEO_Ratings')) { return new \WP_Error('aieo_rating_off', __('Ratings are disabled.', 'ai-eshop-optimizer'), array('status' => 404)); } $refused = $this->refuse($req, 'mine'); if ($refused) { return $refused; } $body = (array) $req->get_json_params() + (array) $req->get_body_params(); $targets = isset($body['targets']) && is_array($body['targets']) ? array_slice($body['targets'], 0, self::MINE_MAX) : array(); $user_id = get_current_user_id(); list($sid, $born) = AIEO_Ratings::request_voter($body['voter'] ?? '', false); $by_kind = array(); foreach ($targets as $t) { if (!is_array($t)) { continue; } $found = AIEO_Ratings::target((string) ($t['kind'] ?? ''), (string) ($t['key'] ?? ''), false); if (null === $found || $found[2] <= 0) { continue; } $by_kind[$found[0]][$found[2]] = $found[1]; } $store = AIEO_Ratings::instance(); $votes = array(); $stats = array(); foreach ($by_kind as $kind => $names) { $ids = array_keys($names); foreach ($store->get_stats_batch($ids, $kind) as $id => $row) { $stats[$kind . ':' . $names[$id]] = self::stats_out($row); } foreach ($store->viewer_votes($kind, $ids, $user_id, $user_id > 0 ? '' : $sid) as $id => $variants) { foreach ($variants as $variant => $vote) { $votes[] = array( 'kind' => $kind, 'key' => $names[$id], 'variant' => (string) $variant, 'options' => $vote['options'] ? $vote['options'] : (object) array(), 'rating' => (int) $vote['rating'], 'stats' => self::stats_out($store->get_stats($id, $kind, (string) $variant)), ); } } } $res = new \WP_REST_Response(array( 'voter' => '' === $sid ? '' : AIEO_Ratings::voter_token($sid, $born), 'votes' => $votes, 'stats' => $stats ? $stats : (object) array(), ), 200); $res->header('Cache-Control', 'no-store'); return $res; } private static function stats_out(array $stats) { return array( 'count' => (int) ($stats['count'] ?? 0), 'sum' => (int) ($stats['sum'] ?? 0), 'avg' => (float) ($stats['avg'] ?? 0), 'distribution' => array_map('intval', array_values((array) ($stats['distribution'] ?? array(0, 0, 0, 0, 0)))), ); } } }