PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.2.0
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.2.0
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 4.2.0, at includes/REST/Feedback.php

244 lines 6.1 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
5 use WP_REST_Request;
6 use WPDeveloper\BetterDocs\Core\BaseAPI;
7
8 class Feedback extends BaseAPI {
9 /**
10 * @return mixed
11 */
12 public function register() {
13 $this->post(
14 '/feedback/(?P<id>\d+)',
15 [ $this, 'save' ],
16 [
17 'id' => [
18 'type' => 'integer',
19 'validate_callback' => function ( $param, $request, $key ) {
20 return ! empty( $param ) && is_numeric( $param ) && get_post( $param ) !== null;
21 },
22 'required' => false,
23 'default' => null
24 ],
25 'feelings' => [
26 'type' => 'string',
27 'validate_callback' => function ( $param, $request, $key ) {
28 $allowed_feelings = [ 'happy', 'sad', 'normal' ];
29 return in_array( $param, $allowed_feelings );
30 },
31 'required' => true
32 ]
33 ]
34 );
35
36 $this->register_field(
37 'docs',
38 'word_count',
39 [
40 'get_callback' => [ $this, 'get_word_count' ]
41 ]
42 );
43
44 $this->register_field(
45 'docs',
46 'total_views',
47 [
48 'get_callback' => [ $this, 'get_total_views' ]
49 ]
50 );
51
52 $this->register_field(
53 'docs',
54 'reactions',
55 [
56 'get_callback' => [ $this, 'get_reaction_count' ]
57 ]
58 );
59
60 $this->register_field(
61 'docs',
62 'author_info',
63 [
64 'get_callback' => [ $this, 'get_author_info' ]
65 ]
66 );
67
68 $this->register_field(
69 'docs',
70 'doc_category_info',
71 [
72 'get_callback' => [ $this, 'get_doc_category_info' ]
73 ]
74 );
75
76 $this->register_field(
77 'docs',
78 'doc_tag_info',
79 [
80 'get_callback' => [ $this, 'get_doc_tag_info' ]
81 ]
82 );
83
84 // $this->register_field( 'docs', 'author_list', [
85 // 'get_callback' => [$this, 'get_author_list']
86 // ] );
87 }
88
89 public function get_author_list( $object, $field_name, $request ) {
90 $args = [
91 'fields' => [
92 'ID',
93 'user_login',
94 'display_name'
95 ]
96 ];
97 $users = get_users( $args );
98 return $users;
99 }
100
101 public function analytics_by_post_id( $post_id ) {
102 global $wpdb;
103
104 $where = "WHERE post_id='" . esc_sql( $post_id ) . "'";
105 return $wpdb->get_results(
106 "SELECT
107 sum(impressions) as totalViews,
108 sum(unique_visit) as totalUniqueViews,
109 sum(happy + sad + normal) as totalReactions,
110 sum(happy) as totalHappy,
111 sum(normal) as totalNormal,
112 sum(sad) as totalSad
113 FROM {$wpdb->prefix}betterdocs_analytics
114 $where"
115 );
116 }
117
118 public function get_word_count( $object, $field_name, $request ) {
119 return str_word_count( trim( strip_tags( get_post_field( 'post_content', $object['id'] ) ) ) );
120 }
121
122 public function get_total_views( $object, $field_name, $request ) {
123 $analytics = $this->analytics_by_post_id( $object['id'] );
124
125 if ( ! empty( $analytics ) ) {
126 return isset( $analytics[0]->totalViews ) ? $analytics[0]->totalViews : 0;
127 } else {
128 return 0;
129 }
130 }
131
132 public function get_reaction_count( $object, $field_name, $request ) {
133 $analytics = $this->analytics_by_post_id( $object['id'] );
134
135 if ( ! empty( $analytics ) ) {
136 return [
137 'happy' => isset( $analytics[0]->totalHappy ) ? $analytics[0]->totalHappy : 0,
138 'normal' => isset( $analytics[0]->totalNormal ) ? $analytics[0]->totalNormal : 0,
139 'sad' => isset( $analytics[0]->totalSad ) ? $analytics[0]->totalSad : 0
140 ];
141 } else {
142 return [
143 'happy' => 0,
144 'normal' => 0,
145 'sad' => 0
146 ];
147 }
148 }
149
150 public function save( WP_REST_Request $request ) {
151 global $wpdb;
152 $docs_id = isset( $request['id'] ) ? esc_sql( intval( $request['id'] ) ) : null;
153 $feelings = isset( $request['feelings'] ) ? esc_sql( $request['feelings'] ) : 'happy';
154 if ( $docs_id !== null && get_post( $docs_id ) && get_option( 'betterdocs_db_version' ) == true ) {
155 $post_id = $wpdb->get_results(
156 $wpdb->prepare(
157 "SELECT *
158 FROM {$wpdb->prefix}betterdocs_analytics
159 WHERE created_at = %s AND post_id = %d",
160 date( 'Y-m-d' ),
161 $docs_id
162 )
163 );
164
165 if ( ! empty( $post_id ) ) {
166 $feelings_increment = $post_id[0]->{$feelings} + 1;
167 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
168 $insert = $wpdb->query(
169 $wpdb->prepare(
170 "UPDATE {$wpdb->prefix}betterdocs_analytics
171 SET " . $feelings . ' = ' . $feelings_increment . '
172 WHERE created_at = %s AND post_id = %d',
173 [
174 date( 'Y-m-d' ),
175 $docs_id
176 ]
177 )
178 );
179 } else {
180 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
181 $insert = $wpdb->query(
182 $wpdb->prepare(
183 "INSERT INTO {$wpdb->prefix}betterdocs_analytics
184 ( post_id, " . $request['feelings'] . ', created_at )
185 VALUES ( %d, %d, %s )',
186 [
187 $docs_id,
188 1,
189 date( 'Y-m-d' )
190 ]
191 )
192 );
193 }
194
195 if ( $insert == true ) {
196 return true;
197 }
198 }
199 return false;
200 }
201
202 public function get_author_info( $object, $field_name, $request ) {
203 $author_id = isset( $object['author'] ) ? $object['author'] : '';
204 if ( ! empty( $author_id ) ) {
205 return [
206 'name' => get_the_author_meta( 'display_name', $author_id ),
207 'author_nicename' => get_the_author_meta( 'nicename', $author_id ),
208 'author_url' => get_author_posts_url( $author_id )
209 ];
210 }
211 return [];
212 }
213
214 public function get_doc_category_info( $object, $field_name, $request ) {
215 $category_term_names = [];
216 $doc_categories = ! empty( $object['doc_category'] ) ? $object['doc_category'] : [];
217 foreach ( $doc_categories as $doc_category_id ) {
218 array_push(
219 $category_term_names,
220 [
221 'term_name' => get_term( $doc_category_id )->name,
222 'term_url' => get_term_link( $doc_category_id )
223 ]
224 );
225 }
226 return $category_term_names;
227 }
228
229 public function get_doc_tag_info( $object, $field_name, $request ) {
230 $doc_tag_term_names = [];
231 $doc_tags = ! empty( $object['doc_tag'] ) ? $object['doc_tag'] : [];
232 foreach ( $doc_tags as $tag_id ) {
233 array_push(
234 $doc_tag_term_names,
235 [
236 'term_name' => get_term( $tag_id )->name,
237 'term_url' => get_term_link( $tag_id )
238 ]
239 );
240 }
241 return $doc_tag_term_names;
242 }
243 }
244