-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest-likes.php
183 lines (152 loc) · 3.91 KB
/
rest-likes.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* Plugin Name: REST Likes
* Plugin URI: https://required.com
* Description: Like posts and comments using the REST API.
* Version: 3.0.0-beta
* Author: required
* Requires at least: 6.6
* Author URI: https://required.com
* Text Domain: rest-likes
* Domain Path: /languages
* License: GPL-2.0-or-later
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
// phpcs:disable PSR1.Files.SideEffects
if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
include __DIR__ . '/vendor/autoload.php';
}
if ( ! class_exists( 'WP_REST_Controller' ) ) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error -- Debugging info.
trigger_error( sprintf( '%s does not exist. Update WordPress or activate the REST API plugin.', 'WP_REST_Controller' ) );
return;
}
const REST_LIKES_PLUGIN_FILE = __FILE__;
const REST_LIKES_PLUGIN_DIR = __DIR__;
require_once __DIR__ . '/inc/Blocks/namespace.php';
/**
* Returns an instance of the main plugin class.
*
* @return \Required\RestLikes\Plugin
*/
function rest_likes() {
static $controller = null;
if ( null === $controller ) {
$controller = new \Required\RestLikes\Plugin();
}
return $controller;
}
add_action( 'plugins_loaded', [ rest_likes(), 'add_hooks' ] );
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
/**
* Returns a post's like count.
*
* @since 1.0.0
*
* @param int|\WP_Post $post Optional. Post ID or object. Default global $post.
*
* @return int The post like count.
*/
function get_rest_post_like_count( $post = null ) {
$post = get_post( $post );
if ( ! $post ) {
return 0;
}
return rest_likes()->get_like_count( 'post', $post->ID );
}
/**
* Prints the post like count markup.
*
* @since 1.0.0
*
* @return void
*/
function the_rest_post_like_count() {
$post = get_post();
if ( ! $post ) {
return;
}
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Already escaped.
echo rest_likes()->get_like_count_html( 'post', $post->ID );
}
/**
* Returns the post like button markup.
*
* @since 1.0.0
*
* @param int|\WP_Post $post Optional. Post ID or object. Default global $post.
* @return string The like button markup.
*/
function get_rest_post_like_button( $post = null ) {
$post = get_post( $post );
if ( ! $post ) {
return '';
}
return rest_likes()->get_like_button( 'post', $post->ID );
}
/**
* Prints the post like button markup.
*
* @since 1.0.0
*
* @return void
*/
function the_rest_post_like_button() {
echo get_rest_post_like_button();
}
/**
* Returns a comments's like count.
*
* @since 1.0.0
*
* @param int|\WP_Comment $comment Optional. Comment ID or object. Default global $comment.
* @return int The comment like count.
*/
function get_rest_comment_like_count( $comment = null ) {
$comment = get_comment( $comment );
if ( ! $comment ) {
return 0;
}
return rest_likes()->get_like_count( 'comment', $comment->comment_ID );
}
/**
* Prints the comment like count markup.
*
* @since 1.0.0
*
* @return void
*/
function the_rest_comment_like_count() {
$comment = get_comment();
if ( ! $comment ) {
return;
}
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Already escaped.
echo rest_likes()->get_like_count_html( 'comment', $comment->comment_ID );
}
/**
* Returns the comment like button markup.
*
* @since 1.0.0
*
* @param int|\WP_Comment $comment Optional. Comment ID or object. Default global $comment.
* @return string The like button markup.
*/
function get_rest_comment_like_button( $comment = null ) {
$comment = get_comment( $comment );
if ( ! $comment ) {
return '';
}
return rest_likes()->get_like_button( 'comment', $comment->comment_ID );
}
/**
* Prints the comment like button markup.
*
* @since 1.0.0
*
* @return void
*/
function the_rest_comment_like_button() {
echo get_rest_comment_like_button();
}
// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound