| | |
| | | namespace JVBase\rest\routes; |
| | | |
| | | use JVBase\managers\CustomTable; |
| | | use JVBase\registrar\Registrar; |
| | | use JVBase\rest\Response; |
| | | use JVBase\rest\Rest; |
| | | use JVBase\rest\Route; |
| | | use JVBase\utility\Features; |
| | | use WP_REST_Request; |
| | | use WP_REST_Response; |
| | | use WP_Error; |
| | |
| | | public function handleVote(WP_REST_Request $request):WP_REST_Response |
| | | { |
| | | $content = sanitize_text_field($request->get_param('content')??''); |
| | | if ((!Features::forContent($content)->has('karma') && !Features::forTaxonomy($content)->has('karma') && !Features::forUser($content)->has('karma'))) { |
| | | $registrar = Registrar::getInstance($content); |
| | | if (!$registrar || !$registrar->hasFeature('karma')) { |
| | | return Response::validationError(['message' => __('Invalid content', 'jvb')]); |
| | | } |
| | | |
| | |
| | | |
| | | $operation = sanitize_text_field($request->get_param('id')); |
| | | |
| | | $type = match(true) { |
| | | array_key_exists($content, JVB_CONTENT) => 'post', |
| | | array_key_exists($content, JVB_TAXONOMY) => 'term', |
| | | array_key_exists($content, JVB_USER) => 'user', |
| | | default => false |
| | | }; |
| | | $type = Registrar::getInstance($content)->getType()??false; |
| | | if (!$type) { |
| | | return Response::validationError(['message' => __('Invalid content type', 'jvb')]); |
| | | } |
| | |
| | | |
| | | $votes = []; |
| | | |
| | | foreach (jvbGlobalKarma() as $type => $content_types) { |
| | | foreach ($content_types as $content_type) { |
| | | $table = CustomTable::for('karma_' . $content_type); |
| | | foreach (Registrar::getFeatured('has_karma') as $type) { |
| | | $table = CustomTable::for('karma_' . $type); |
| | | |
| | | // Skip if table doesn't exist |
| | | global $wpdb; |
| | | if ($wpdb->get_var("SHOW TABLES LIKE '{$table->getFullTableName()}'") != $table->getFullTableName()) { |
| | | continue; |
| | | } |
| | | // Skip if table doesn't exist |
| | | global $wpdb; |
| | | if ($wpdb->get_var("SHOW TABLES LIKE '{$table->getFullTableName()}'") != $table->getFullTableName()) { |
| | | continue; |
| | | } |
| | | |
| | | $results = $table->where(['user_id' => $user])->getResults(); |
| | | $results = $table->where(['user_id' => $user])->getResults(); |
| | | |
| | | if (!empty($results)) { |
| | | foreach ($results as $vote) { |
| | | $votes[$content_type][$vote->item_id] = $vote->vote; |
| | | } |
| | | if (!empty($results)) { |
| | | foreach ($results as $vote) { |
| | | $votes[$type][$vote->item_id] = $vote->vote; |
| | | } |
| | | } |
| | | } |