Created
November 19, 2018 18:51
-
-
Save campusboy87/005dc9fca78bbfd13f52f1cc77a77d3c to your computer and use it in GitHub Desktop.
Adds a counter to the rest request post. The functionality of the plugin "Post Views Counter" is used.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| add_filter( 'rest_prepare_post', 'add_rest_post_view_counter', 10, 3 ); | |
| /** | |
| * @param WP_REST_Response $response The response object. | |
| * @param WP_Post $post Post object. | |
| * @param WP_REST_Request $request Request object. | |
| * | |
| * @return mixed | |
| */ | |
| function add_rest_post_view_counter( $response, $post, $request ) { | |
| $callback = $request->get_attributes(); | |
| $callback = isset( $callback['callback'][1] ) ? $callback['callback'][1] : ''; | |
| if ( $callback && is_string( $callback ) && 'get_item' === $callback ) { | |
| if ( method_exists( 'Post_Views_Counter_Counter', 'check_post_rest_api' ) ) { | |
| $request_views = new Post_Views_Counter_Counter();; | |
| $request_views->check_post_rest_api( [ 'id' => $post->ID ] ); | |
| } | |
| } | |
| return $response; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment