PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 3.3.2
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v3.3.2
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / REST / Feedback.php

Feedback.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 3.3.2, at includes/REST/Feedback.php

83 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPDeveloper\BetterDocs\REST;
4 use WP_REST_Request;
5 use WPDeveloper\BetterDocs\Core\BaseAPI;
6
7 class Feedback extends BaseAPI {
8 /**
9 * @return mixed
10 */
11 public function register() {
12 $this->post( '/feedback/(?P<id>\d+)', [$this, 'save'], [
13 'id' => [
14 'type' => 'integer',
15 'validate_callback' => function ( $param, $request, $key ) {
16 return ! empty( $param ) && is_numeric( $param ) && get_post( $param ) !== null;
17 },
18 'required' => false,
19 'default' => null
20 ],
21 'feelings' => [
22 'type' => 'string',
23 'validate_callback' => function ( $param, $request, $key ) {
24 $allowed_feelings = [ 'happy', 'sad', 'normal' ];
25 return in_array( $param, $allowed_feelings );
26 },
27 'required' => true
28 ]
29 ] );
30 }
31
32 public function save( WP_REST_Request $request ) {
33 global $wpdb;
34 $docs_id = isset( $request['id'] ) ? esc_sql( intval( $request['id'] ) ) : null;
35 $feelings = isset( $request['feelings'] ) ? esc_sql( $request['feelings'] ) : 'happy';
36 if ( $docs_id !== null && get_post( $docs_id ) && get_option( 'betterdocs_pro_db_version' ) == true ) {
37 $post_id = $wpdb->get_results(
38 $wpdb->prepare(
39 "SELECT *
40 FROM {$wpdb->prefix}betterdocs_analytics
41 WHERE created_at = %s AND post_id = %d",
42 date( 'Y-m-d' ),
43 $docs_id
44 )
45 );
46
47 if ( ! empty( $post_id ) ) {
48 $feelings_increment = $post_id[0]->{$feelings}+1;
49
50 $insert = $wpdb->query(
51 $wpdb->prepare(
52 "UPDATE {$wpdb->prefix}betterdocs_analytics
53 SET " . $feelings . " = " . $feelings_increment . "
54 WHERE created_at = %s AND post_id = %d",
55 [
56 date( 'Y-m-d' ),
57 $docs_id
58 ]
59 )
60 );
61 } else {
62 $insert = $wpdb->query(
63 $wpdb->prepare(
64 "INSERT INTO {$wpdb->prefix}betterdocs_analytics
65 ( post_id, " . $request['feelings'] . ", created_at )
66 VALUES ( %d, %d, %s )",
67 [
68 $docs_id,
69 1,
70 date( 'Y-m-d' )
71 ]
72 )
73 );
74 }
75
76 if ( $insert == true ) {
77 return true;
78 }
79 }
80 return false;
81 }
82 }
83