Closed vinaysamant closed 6 years ago
Hi Vinay,
I have an endpoint for PUT, where i will like to increment view counter of post as soon as user actually views my article in mobile app. (...) I want to call this POST endpoint when the details page is displayed, that should actually update view-counter of that particular article. Which function/action should i call for this?
At the moment, the only way to update the views count of a post is by sending a POST request to admin-ajax.php
(see relevant code). The parameters sent in this call are:
Or can you provide REST endpoint for this where we may POST post-id and the counter is incremented by 1...
A few days ago, another user sent a PR implementing WP REST API support into the plugin so a big part of the job has been done already.
However, I'm not familiar with this API yet so implementing this might take a while.
Also if i want list of most popular articles ordered by views, do i have some function like get_posts() that will return me pure "posts" object? this will help me to send REST data to app and show most popular articles..
Well, you could:
popular-posts
endpoint implemented by @johnwatkins0 in the PR I mentioned above (but then you'll have to install the GitHub version of the plugin as this feature hasn't been pushed to the official WordPress repo yet.)One important thing, i must say, this is great module by you! works like charm. helped me a lot. Many many thanks and keep up great job Sir!
Thank you! I'm glad you like my plugin :)
Hii...
i have seen both the pages and understood none. I admit i am very dumb :(
please dont give up on me..
your 1st para makes it clear that it's possible by making a ajax request. But it requires a Nonce. It's not possible to get it on Cordova App. So generally we use REST EndPoints to send receive data. And those EndPoints call our function to do some job...
So in this function of my POST endpoint,
Please correct me if i am wrong. And how can i check this??
Isn't it possible to call a function with these parameters or something from my endpoint to increment the counter by passing ID & date etc?
For general infromation, this is code of my 1endpoint :
add_action('init', 'ananya_init'); function ananya_init(){ register_rest_route('ananya/posts', '/all', array('methods' => 'GET', 'callback' => 'ananya_posts',)); }
function ananya_posts($request){//get SWPM user profile $args = array( 'numberposts' => $request['count'] ? $request['count'] : 5, 'offset' => $request['offset'] ? $request['offset'] : 0, 'orderby' => $request['orderby'] ? $request['orderby'] : 'date', 'order' => $request['order'] ? $request['order'] : 'desc', ); if($request['categories']) $args['cat'] = $request['categories']; if($request['tags']) $args['tag__in'] = $request['tags'].split(','); if($request['search']) $args['s'] = $request['search'];
$allposts = get_posts($args);
foreach ( $allposts as $post ) {
$post->post_content = nl2br($post->post_content);
$post->featured = get_the_post_thumbnail_url($post->ID, 'full');
$post->categories = get_the_terms( $post->ID, 'category');
$post->tags = get_the_terms( $post->ID, 'post_tag' );
$post->comments = get_comments(array('post_id' => $post->ID));
You can see wpp_get_views() used in this endpoint to get views. This JSON response is returned to my mobile app and used in creating cards..
Thanks for your time and understanding..
regards, Vinay Samant, Managing Director & CEO, Ananya MultiTech Private Limited (www.ananyamultitech.com) At Post Wadi-Warawde, Taluka : Kudal, District : Sindhudurga, Maharashtra, India. Phone : +91 93252 62692, +91 98202 62692 E-Mail : mail@vinaysamant.com
PLEASE DO NOT PRINT THIS MAIL UNLESS GENUINELY REQUIRED.SUPPORT CAUSE, CONTRIBUTE TO GREENER EARTH!
This is official communication of Mr. Vinay Samant. The message is for intended recipient/s only. Any unauthorized viewing or use of this message is strictly prohibited. Please delete this mail if you are not the intended recipient/s.
सुखी संतोषा न यावे । दु:खी विषादा न भजावे । लाभालाभ न धरावे । मनामाजी ।। एथ विजय होइल । की सर्वथा देह जाइल । हे अाधीची कां पुढील । चिंतावेना ।। अापणयां उचीता । स्वधर्मातेंची रहाटता । जे पावे ते निवांता । साहोनी जावे ।। ऐेसेया मनें होअावे । तरी दोष न घडू स्वभावे । म्हणोनी अतां झुंजावे । निभ्रांत तुवां ।। ज्ञानेश्वरी अध्याय २, अोवी २२६-२२९.
On Tue, Feb 27, 2018 at 6:54 PM, Héctor Cabrera notifications@github.com wrote:
Hi Vinay,
I have an endpoint for PUT, where i will like to increment view counter of post as soon as user actually views my article in mobile app. (...) I want to call this POST endpoint when the details page is displayed, that should actually update view-counter of that particular article. Which function/action should i call for this?
At the moment, the only way to update the views count of a post is by sending a POST request to admin-ajax.php (see relevant code https://github.com/cabrerahector/wordpress-popular-posts/blob/master/public/js/wpp.js#L62-L68). The parameters sent in this call are:
- action: "update_views_ajax"
- token: a WordPress Nonce https://codex.wordpress.org/WordPress_Nonces named "wpp-token"
- wpp_id: the post ID
Or can you provide REST endpoint for this where we may POST post-id and the counter is incremented by 1...
A few days ago, another user sent a PR implementing WP REST API support into the plugin https://github.com/cabrerahector/wordpress-popular-posts/pull/164 so a big part of the job has been done already.
However, I'm not familiar with this API yet so implementing this might take a while.
Also if i want list of most popular articles ordered by views, do i have some function like get_posts() that will return me pure "posts" object? this will help me to send REST data to app and show most popular articles..
Well, you could:
- use the WPP_Query class https://github.com/cabrerahector/wordpress-popular-posts/blob/master/includes/class-wordpress-popular-posts-query.php to fetch an array of popular post objects, or
- just use the popular-posts endpoint implemented by @johnwatkins0 https://github.com/johnwatkins0 in the PR I mentioned above (but then you'll have to install the GitHub version of the plugin as this feature hasn't been pushed to the official WordPress repo yet.)
One important thing, i must say, this is great module by you! works like charm. helped me a lot. Many many thanks and keep up great job Sir!
Thank you! I'm glad you like my plugin :)
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/cabrerahector/wordpress-popular-posts/issues/178#issuecomment-368871631, or mute the thread https://github.com/notifications/unsubscribe-auth/ALcxK-kPjCms1VG5K4nvRO1YwNBfPQhvks5tZAHvgaJpZM4SUj3n .
Hey Vinay,
Let me begin by reiterating that I'm not familiar with the WP REST API, so chances are there are better ways to do this.
With that being said:
your 1st para makes it clear that it's possible by making a ajax request. But it requires a Nonce. It's not possible to get it on Cordova App.
You could set up a GET endpoint that returns the Nonce, call it from your Cordova App and store it in a (global?) variable for later use.
So in this function of my POST endpoint,
- i will create Nonce for "update_views_ajax" like $nonce = wp_create_nonce( 'update_views_ajax'.$post->ID );
- i will generate a ajax request to admin-ajax.php with that Nonce and other 2 parameters.
- And this should take care of my counter increment?
Please correct me if i am wrong. And how can i check this??
Change wp_create_nonce( 'update_views_ajax'.$post->ID )
to wp_create_nonce( 'wpp-token' )
.
If it worked, you'll get a response like "WPP: OK. Execution time: X.XXXXX seconds".
Hi...
sorry, but i am writing without actually trying it. I will surely try your trick for Nonce and it might help me in other things as well..
But this makes 2 ajax calls.. 1 to get Nonce and 2nd to update view.. Isn't it possible that i make just one call and in that end-point function i just call 1function to pass post id that takes of everything. This will be not only simple but also easily replicable to any other developer also..
regards, Vinay Samant, Managing Director & CEO, Ananya MultiTech Private Limited (www.ananyamultitech.com) At Post Wadi-Warawde, Taluka : Kudal, District : Sindhudurga, Maharashtra, India. Phone : +91 93252 62692, +91 98202 62692 E-Mail : mail@vinaysamant.com
PLEASE DO NOT PRINT THIS MAIL UNLESS GENUINELY REQUIRED.SUPPORT CAUSE, CONTRIBUTE TO GREENER EARTH!
This is official communication of Mr. Vinay Samant. The message is for intended recipient/s only. Any unauthorized viewing or use of this message is strictly prohibited. Please delete this mail if you are not the intended recipient/s.
सुखी संतोषा न यावे । दु:खी विषादा न भजावे । लाभालाभ न धरावे । मनामाजी ।। एथ विजय होइल । की सर्वथा देह जाइल । हे अाधीची कां पुढील । चिंतावेना ।। अापणयां उचीता । स्वधर्मातेंची रहाटता । जे पावे ते निवांता । साहोनी जावे ।। ऐेसेया मनें होअावे । तरी दोष न घडू स्वभावे । म्हणोनी अतां झुंजावे । निभ्रांत तुवां ।। ज्ञानेश्वरी अध्याय २, अोवी २२६-२२९.
On Tue, Feb 27, 2018 at 7:54 PM, Héctor Cabrera notifications@github.com wrote:
Hey Vinay,
Let me begin by reiterating that I'm not familiar with the WP REST API, so chances are there are better ways to do this.
With that being said:
your 1st para makes it clear that it's possible by making a ajax request. But it requires a Nonce. It's not possible to get it on Cordova App.
You could set up a GET endpoint that returns the Nonce, call it from your Cordova App and store it in a (global?) variable for later use.
So in this function of my POST endpoint,
- i will create Nonce for "update_views_ajax" like $nonce = wp_create_nonce( 'update_views_ajax'.$post->ID );
- i will generate a ajax request to admin-ajax.php with that Nonce and other 2 parameters.
- And this should take care of my counter increment?
Please correct me if i am wrong. And how can i check this??
Change wp_create_nonce( 'update_views_ajax'.$post->ID ) to wp_create_nonce( 'wpp-token' ).
If it worked, you'll get a response like "WPP: OK. Execution time: X.XXXXX seconds".
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/cabrerahector/wordpress-popular-posts/issues/178#issuecomment-368894438, or mute the thread https://github.com/notifications/unsubscribe-auth/ALcxKxaouXUBD1K86ER3BVp8Ox1zJy5uks5tZBA1gaJpZM4SUj3n .
You don't really need to get the nonce key every time you update the views count. You could store it somewhere for a few hours (session, local database, etc) and use it.
Do make sure to update it at least once a day though to make sure you're using a valid, non-expired nonce.
Ok boss,
Let me try..
On Feb 27, 2018 10:11 PM, "Héctor Cabrera" notifications@github.com wrote:
You don't really need to get the nonce key every time you update the views count. You could store it somewhere for a few hours (session, local database, etc) and use it.
Do make sure to update it at least once a day though to make sure you're using a valid, non-expired nonce.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/cabrerahector/wordpress-popular-posts/issues/178#issuecomment-368942541, or mute the thread https://github.com/notifications/unsubscribe-auth/ALcxK4B-JQ6Xi8ESpxFyrIjCL-bwT_KIks5tZDBAgaJpZM4SUj3n .
Closing due to inactivity.
i have Cordova based android mobile application where i get posts from REST endpoints. I have custom endpoint where i used "$post->views = wpp_get_views($post->ID);" to collect number of views per post in my REST output. This works perfect & i am really thankful to you for this.
I have an endpoint for PUT, where i will like to increment view counter of post as soon as user actually views my article in mobile app. Initially i show him list of 20 articles, and when he clicks specific then i show him details. I want to call this POST endpoint when the details page is displayed, that should actually update view-counter of that particular article. Which function/action should i call for this?
Or can you provide REST endpoint for this where we may POST post-id and the counter is incremented by 1...
Also if i want list of most popular articles ordered by views, do i have some function like get_posts() that will return me pure "posts" object? this will help me to send REST data to app and show most popular articles..
One important thing, i must say, this is great module by you! works like charm. helped me a lot. Many many thanks and keep up great job Sir!