PluginProbe
Solid Post Likes / trunk
Solid Post Likes vtrunk
trunk 1.0.8 1.1.0 1.2.0
solid-post-likes / controllers / SolidPostLikesProcess.php

SolidPostLikesProcess.php in Solid Post Likes trunk, at controllers/SolidPostLikesProcess.php

71 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace OACS\SolidPostLikes\Controllers;
3
4 use OACS\SolidPostLikes\Controllers\SolidPostLikesIcon as Icon;
5 use OACS\SolidPostLikes\Controllers\SolidPostLikesCounter as Counter;
6 use OACS\SolidPostLikes\Controllers\SolidPostLikesText as Text;
7 use OACS\SolidPostLikes\Controllers\SolidPostLikesStore as Store;
8
9 if ( ! defined( 'WPINC' ) ) { die; }
10 /** Processes request when the like button is clicked. */
11
12 class SolidPostLikesProcess
13 {
14 function oacs_spl_process_like()
15 {
16 $nonce = isset($_REQUEST['nonce']) ? sanitize_text_field($_REQUEST['nonce']) : 0;
17
18 if (!wp_verify_nonce($nonce, 'oacs_spl_likes_nonce')) {
19 wp_die(esc_html__('Session expired. Please reload the page and try again.', 'solid-post-likes'), 403);
20 }
21
22 // Only logged-in users may like when the setting is active (server-side guard;
23 // the button itself links to the login page in that case).
24 if (!is_user_logged_in() && get_option('_oacs_spl_logged_in_only')) {
25 wp_send_json(array('status' => 'login_required'));
26 }
27 // Javascript tester: Did my hardcoded attribute make it over?
28 // If it did, there was no ajax call to absorb it so Javascript is off.
29 $disabled = isset($_REQUEST['disabled']) ? 1 : 0;
30 // Test if this is a comment
31 $is_comment = (isset($_REQUEST['is_comment']) && $_REQUEST['is_comment'] == 1) ? 1 : 0;
32
33 $post_id = (isset($_REQUEST['post_id']) && is_numeric($_REQUEST['post_id'])) ? (int) $_REQUEST['post_id'] : 0;
34 if (!$post_id) {
35 return;
36 }
37
38 $store = new Store;
39 $result = $store->toggle($post_id, $is_comment);
40
41 $post_like_icon = new Icon;
42 $post_like_text = new Text;
43 $post_like_counter = new Counter;
44
45 $response = array(
46 'status' => $result['status'],
47 'icon' => ($result['status'] === 'liked') ? $post_like_icon->oacs_spl_get_liked_icon() : $post_like_icon->oacs_spl_get_unliked_icon(),
48 'text' => ($result['status'] === 'liked') ? $post_like_text->oacs_spl_get_unlike_text() : $post_like_text->oacs_spl_get_like_text(),
49 // insert a space so the button stays the same.
50 'count' => $post_like_counter->oacs_spl_get_like_count($result['count']) . ' ',
51 'comment' => $is_comment,
52 );
53
54 if ($disabled) {
55 $this->oacs_spl_nojs_redirect($post_id, $is_comment);
56 }
57 wp_send_json($response);
58 }
59
60 /** No-JS fallback: send the visitor back to the (comment's) post after processing. */
61 private function oacs_spl_nojs_redirect($post_id, $is_comment)
62 {
63 if ($is_comment == 1) {
64 $comment = get_comment($post_id);
65 $post_id = $comment ? $comment->comment_post_ID : 0;
66 }
67 wp_safe_redirect(get_permalink($post_id) ?: home_url('/'));
68 exit();
69 }
70 }
71