| 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 |
$this->register_field( 'docs', 'word_count', [ |
| 32 |
'get_callback' => [$this, 'get_word_count'] |
| 33 |
] ); |
| 34 |
|
| 35 |
$this->register_field( 'docs', 'total_views', [ |
| 36 |
'get_callback' => [$this, 'get_total_views'] |
| 37 |
] ); |
| 38 |
|
| 39 |
$this->register_field( 'docs', 'reactions', [ |
| 40 |
'get_callback' => [$this, 'get_reaction_count'] |
| 41 |
] ); |
| 42 |
|
| 43 |
$this->register_field( 'docs', 'author_info', [ |
| 44 |
'get_callback' => [$this, 'get_author_info'] |
| 45 |
] ); |
| 46 |
|
| 47 |
$this->register_field( 'docs', 'doc_category_info', [ |
| 48 |
'get_callback' => [$this, 'get_doc_category_info'] |
| 49 |
] ); |
| 50 |
|
| 51 |
$this->register_field( 'docs', 'doc_tag_info', [ |
| 52 |
'get_callback' => [$this, 'get_doc_tag_info'] |
| 53 |
] ); |
| 54 |
|
| 55 |
// $this->register_field( 'docs', 'author_list', [ |
| 56 |
// 'get_callback' => [$this, 'get_author_list'] |
| 57 |
// ] ); |
| 58 |
} |
| 59 |
|
| 60 |
public function get_author_list( $object, $field_name, $request ) { |
| 61 |
$args = [ |
| 62 |
'fields' => [ |
| 63 |
'ID', |
| 64 |
'user_login', |
| 65 |
'display_name' |
| 66 |
] |
| 67 |
]; |
| 68 |
$users = get_users( $args ); |
| 69 |
return $users; |
| 70 |
} |
| 71 |
|
| 72 |
public function analytics_by_post_id( $post_id ) { |
| 73 |
global $wpdb; |
| 74 |
|
| 75 |
$where = "WHERE post_id='" . esc_sql( $post_id ) . "'"; |
| 76 |
return $wpdb->get_results( |
| 77 |
"SELECT |
| 78 |
sum(impressions) as totalViews, |
| 79 |
sum(unique_visit) as totalUniqueViews, |
| 80 |
sum(happy + sad + normal) as totalReactions, |
| 81 |
sum(happy) as totalHappy, |
| 82 |
sum(normal) as totalNormal, |
| 83 |
sum(sad) as totalSad |
| 84 |
FROM {$wpdb->prefix}betterdocs_analytics |
| 85 |
$where" |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
public function get_word_count( $object, $field_name, $request ) { |
| 90 |
return str_word_count( trim( strip_tags( get_post_field( 'post_content', $object['id'] ) ) ) ); |
| 91 |
} |
| 92 |
|
| 93 |
public function get_total_views( $object, $field_name, $request ) { |
| 94 |
$analytics = $this->analytics_by_post_id( $object['id'] ); |
| 95 |
|
| 96 |
if ( ! empty( $analytics ) ) { |
| 97 |
return isset( $analytics[0]->totalViews ) ? $analytics[0]->totalViews : 0; |
| 98 |
} else { |
| 99 |
return 0; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
public function get_reaction_count( $object, $field_name, $request ) { |
| 104 |
$analytics = $this->analytics_by_post_id( $object['id'] ); |
| 105 |
|
| 106 |
if ( ! empty( $analytics ) ) { |
| 107 |
return [ |
| 108 |
'happy' => isset( $analytics[0]->totalHappy ) ? $analytics[0]->totalHappy : 0, |
| 109 |
'normal' => isset( $analytics[0]->totalNormal ) ? $analytics[0]->totalNormal : 0, |
| 110 |
'sad' => isset( $analytics[0]->totalSad ) ? $analytics[0]->totalSad : 0 |
| 111 |
]; |
| 112 |
} else { |
| 113 |
return [ |
| 114 |
'happy' => 0, |
| 115 |
'normal' => 0, |
| 116 |
'sad' => 0 |
| 117 |
]; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
public function save( WP_REST_Request $request ) { |
| 122 |
global $wpdb; |
| 123 |
$docs_id = isset( $request['id'] ) ? esc_sql( intval( $request['id'] ) ) : null; |
| 124 |
$feelings = isset( $request['feelings'] ) ? esc_sql( $request['feelings'] ) : 'happy'; |
| 125 |
if ( $docs_id !== null && get_post( $docs_id ) && get_option( 'betterdocs_db_version' ) == true ) { |
| 126 |
$post_id = $wpdb->get_results( |
| 127 |
$wpdb->prepare( |
| 128 |
"SELECT * |
| 129 |
FROM {$wpdb->prefix}betterdocs_analytics |
| 130 |
WHERE created_at = %s AND post_id = %d", |
| 131 |
date( 'Y-m-d' ), |
| 132 |
$docs_id |
| 133 |
) |
| 134 |
); |
| 135 |
|
| 136 |
if ( ! empty( $post_id ) ) { |
| 137 |
$feelings_increment = $post_id[0]->{$feelings}+1; |
| 138 |
|
| 139 |
$insert = $wpdb->query( |
| 140 |
$wpdb->prepare( |
| 141 |
"UPDATE {$wpdb->prefix}betterdocs_analytics |
| 142 |
SET " . $feelings . " = " . $feelings_increment . " |
| 143 |
WHERE created_at = %s AND post_id = %d", |
| 144 |
[ |
| 145 |
date( 'Y-m-d' ), |
| 146 |
$docs_id |
| 147 |
] |
| 148 |
) |
| 149 |
); |
| 150 |
} else { |
| 151 |
$insert = $wpdb->query( |
| 152 |
$wpdb->prepare( |
| 153 |
"INSERT INTO {$wpdb->prefix}betterdocs_analytics |
| 154 |
( post_id, " . $request['feelings'] . ", created_at ) |
| 155 |
VALUES ( %d, %d, %s )", |
| 156 |
[ |
| 157 |
$docs_id, |
| 158 |
1, |
| 159 |
date( 'Y-m-d' ) |
| 160 |
] |
| 161 |
) |
| 162 |
); |
| 163 |
} |
| 164 |
|
| 165 |
if ( $insert == true ) { |
| 166 |
return true; |
| 167 |
} |
| 168 |
} |
| 169 |
return false; |
| 170 |
} |
| 171 |
|
| 172 |
public function get_author_info( $object, $field_name, $request ) { |
| 173 |
$author_id = isset( $object['author'] ) ? $object['author'] : ''; |
| 174 |
if ( ! empty( $author_id ) ) { |
| 175 |
return [ |
| 176 |
'name' => get_the_author_meta( 'display_name', $author_id ), |
| 177 |
'author_nicename' => get_the_author_meta( 'nicename', $author_id ), |
| 178 |
'author_url' => get_author_posts_url( $author_id ) |
| 179 |
]; |
| 180 |
} |
| 181 |
return []; |
| 182 |
} |
| 183 |
|
| 184 |
public function get_doc_category_info( $object, $field_name, $request ) { |
| 185 |
$category_term_names = []; |
| 186 |
$doc_categories = ! empty( $object['doc_category'] ) ? $object['doc_category'] : []; |
| 187 |
foreach ( $doc_categories as $doc_category_id ) { |
| 188 |
array_push( $category_term_names, ['term_name' => get_term( $doc_category_id )->name, 'term_url' => get_term_link( $doc_category_id )] ); |
| 189 |
} |
| 190 |
return $category_term_names; |
| 191 |
} |
| 192 |
|
| 193 |
public function get_doc_tag_info( $object, $field_name, $request ) { |
| 194 |
$doc_tag_term_names = []; |
| 195 |
$doc_tags = ! empty( $object['doc_tag'] ) ? $object['doc_tag'] : []; |
| 196 |
foreach ( $doc_tags as $tag_id ) { |
| 197 |
array_push( $doc_tag_term_names, ['term_name' => get_term( $tag_id )->name, 'term_url' => get_term_link( $tag_id )] ); |
| 198 |
} |
| 199 |
return $doc_tag_term_names; |
| 200 |
} |
| 201 |
} |
| 202 |
|