| 1 |
<?php |
| 2 |
namespace OACS\SolidPostLikes\Controllers; |
| 3 |
|
| 4 |
use OACS\SolidPostLikes\Controllers\SolidPostLikesChecker as Checker; |
| 5 |
use OACS\SolidPostLikes\Controllers\SolidPostLikesIcon as Icon; |
| 6 |
use OACS\SolidPostLikes\Controllers\SolidPostLikesCounter as Counter; |
| 7 |
use OACS\SolidPostLikes\Controllers\SolidPostLikesText as Text; |
| 8 |
use OACS\SolidPostLikes\Controllers\SolidPostLikesStore as Store; |
| 9 |
|
| 10 |
if ( ! defined( 'WPINC' ) ) { die; } |
| 11 |
/** Read-only like info for pages served from a full page cache. */ |
| 12 |
|
| 13 |
class SolidPostLikesGetter |
| 14 |
{ |
| 15 |
function oacs_spl_get_like_info() |
| 16 |
{ |
| 17 |
// Read-only endpoint: deliberately no nonce check — pages served from a full page |
| 18 |
// cache carry stale nonces, which is the very reason this endpoint exists. |
| 19 |
// Test if this is a comment |
| 20 |
$is_comment = (isset($_REQUEST['is_comment']) && $_REQUEST['is_comment'] == 1) ? 1 : 0; |
| 21 |
|
| 22 |
$post_id = (isset($_REQUEST['post_id']) && is_numeric($_REQUEST['post_id'])) ? (int) $_REQUEST['post_id'] : 0; |
| 23 |
if (!$post_id) { |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
$checker = new Checker; |
| 28 |
$store = new Store; |
| 29 |
$post_like_icon = new Icon; |
| 30 |
$post_like_text = new Text; |
| 31 |
$post_like_counter = new Counter; |
| 32 |
|
| 33 |
if ($checker->oacs_spl_already_liked($post_id, $is_comment)) { |
| 34 |
$response['status'] = "liked"; |
| 35 |
$response['icon'] = $post_like_icon->oacs_spl_get_liked_icon(); |
| 36 |
$response['text'] = $post_like_text->oacs_spl_get_unlike_text(); |
| 37 |
} else { |
| 38 |
$response['status'] = "unliked"; |
| 39 |
$response['icon'] = $post_like_icon->oacs_spl_get_unliked_icon(); |
| 40 |
$response['text'] = $post_like_text->oacs_spl_get_like_text(); |
| 41 |
} |
| 42 |
|
| 43 |
// insert a space so the button stays the same. |
| 44 |
$response['count'] = $post_like_counter->oacs_spl_get_like_count($store->display_count($post_id, $is_comment)) . ' '; |
| 45 |
$response['comment'] = $is_comment; |
| 46 |
// Fresh nonce so like clicks keep working on pages served from a stale full page cache. |
| 47 |
$response['nonce'] = wp_create_nonce('oacs_spl_likes_nonce'); |
| 48 |
wp_send_json($response); |
| 49 |
} |
| 50 |
} |
| 51 |
|