PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 3.8.9
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v3.8.9
4.9.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 All 200 releases
← All changes | includes/REST/Feedback.php +229 -68 3.5.03.8.9 View file →
@@ -1,82 +1,243 @@
1 1 <?php
2 2
3 3 namespace WPDeveloper\BetterDocs\REST;
4 +
4 5 use WP_REST_Request;
5 6 use WPDeveloper\BetterDocs\Core\BaseAPI;
6 7
7 8 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 - }
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 + );
31 35
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_db_version' ) == true ) {
37 - $post_id = $wpdb->get_results(
38 - $wpdb->prepare(
39 - "SELECT *
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 *
40 158 FROM {$wpdb->prefix}betterdocs_analytics
41 159 WHERE created_at = %s AND post_id = %d",
42 - date( 'Y-m-d' ),
43 - $docs_id
44 - )
45 - );
160 + date( 'Y-m-d' ),
161 + $docs_id
162 + )
163 + );
46 164
47 - if ( ! empty( $post_id ) ) {
48 - $feelings_increment = $post_id[0]->{$feelings}+1;
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 + }
49 194
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 - }
195 + if ( $insert == true ) {
196 + return true;
197 + }
198 + }
199 + return false;
200 + }
75 201
76 - if ( $insert == true ) {
77 - return true;
78 - }
79 - }
80 - return false;
81 - }
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 + }
82 243 }