-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadmin-edit-comment.php
More file actions
271 lines (239 loc) · 7.85 KB
/
Copy pathadmin-edit-comment.php
File metadata and controls
271 lines (239 loc) · 7.85 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
/*
* Plugin Name: Admin Edit Comment
* Description: Adding an extra comment functionality in post screen exclusively for your editorial team.
* Version: 1.2.0
* Author: PRESSMAN
* Author URI: https://www.pressman.ne.jp/
* License: GNU GPL v2 or higher
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*
* @author PRESSMAN
* @link https://www.pressman.ne.jp/
* @copyright Copyright (c) 2018, PRESSMAN
*/
// Deny accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Main Admin Edit Comment Class.
*
* @class Admin_Edit_Comment
*/
class Admin_Edit_Comment {
const POST_TYPE_NAME = 'admin_edit_comment';
const ADMIN_EDIT_COMMENT_OPTIONS = 'admin_edit_comment_options';
/**
* This plugin is enabled by default on 'post' and 'page'.
*/
const DEFAULT_ACTIVE_POST_TYPE = [ 'post', 'page' ];
/**
* Version of this plugin.
*
* @var string
*/
public $version;
/**
* The single instance of the class.
*
* @var Admin_Edit_Comment
*/
protected static $instance = null;
/**
* Ensures only one instance of this class.
*
* @return Admin_Edit_Comment
*/
public static function instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Admin_Edit_Comment constructor.
*/
public function __construct() {
require_once plugin_dir_path( __FILE__ ) . 'admin/admin.php';
register_uninstall_hook( __FILE__, 'aec_uninstall' );
$plugin_data = get_file_data( __FILE__, [ 'version' => 'Version' ] );
$this->version = $plugin_data['version'];
add_action( 'init', [ $this, 'register_post_type' ] );
add_action( 'admin_head', [ $this, 'add_comment_box' ] );
add_action( 'wp_ajax_aec_insert_comment', [ $this, 'insert_comment' ] );
add_action( 'wp_ajax_aec_delete_comment', [ $this, 'delete_comment' ] );
add_action( 'plugins_loaded', [ $this, 'load_text_domain' ] );
}
/**
* Loads translated strings.
*/
public function load_text_domain() {
load_plugin_textdomain( 'admin-edit-comment', false, plugin_basename( plugin_dir_path( __FILE__ ) ) . '/languages' );
}
/**
* Register post type used by this plugin.
*/
public function register_post_type() {
register_post_type(
self::POST_TYPE_NAME,
apply_filters( 'aec_register_post_type_args', [
'label' => 'AEC',
'public' => false,
'hierarchical' => false,
'supports' => false,
'rewrite' => false,
] )
);
}
/**
* Adding comment box at editing screen.
*/
public function add_comment_box() {
$screen = get_current_screen();
$active_post_types = apply_filters( 'aec_activate_post_types', get_option( self::ADMIN_EDIT_COMMENT_OPTIONS, self::DEFAULT_ACTIVE_POST_TYPE ) );
if ( $screen->base !== 'post' || ! in_array( $screen->post_type, $active_post_types ) ) {
return;
}
add_meta_box( 'admin_edit_comment', 'Admin Edit Comment', [ $this, 'add_meta_box' ], $active_post_types, 'side' );
wp_enqueue_style( 'aec_edit.css', plugin_dir_url( __FILE__ ) . 'assets/css/edit.css', [], $this->version );
wp_enqueue_script( 'aec_edit.js', plugin_dir_url( __FILE__ ) . 'assets/js/edit.js', [ 'jquery' ], $this->version, true );
wp_localize_script(
'aec_edit.js',
'localize',
[
'ajax_url' => admin_url( 'admin-ajax.php' ),
'delete_failed_msg' => __( 'Delete failed.', 'admin-edit-comment' ),
'update_failed_msg' => __( 'Update failed.', 'admin-edit-comment' ),
'comments_limit_msg' => __( 'The number of comments exceeds the limit.', 'admin-edit-comment' ),
'no_empty_msg' => __( 'No empty.', 'admin-edit-comment' ),
]
);
}
/**
* Create meta box.
*
* @param WP_Post $post
*/
public function add_meta_box( WP_Post $post ) {
?>
<div id="aec_comment_wrap">
<?php echo $this->get_content_html( $post->ID ); ?>
</div>
<div id='aec_text_area_wrap'>
<textarea name='aec_comment_text_area' placeholder='' rows='3'></textarea>
</div>
<div id='aec_submit_wrap'>
<input class='button button-primary' type='button' name='aec_submit' value='Send'>
</div>
<?php
}
/**
* Get comments content.
*
* @param int $post_id
*
* @return string
*/
private function get_content_html( $post_id ) {
$comments = get_posts( apply_filters( 'aec_get_post_args', [
'posts_per_page' => - 1,
'post_type' => self::POST_TYPE_NAME,
'post_parent' => $post_id,
] ) );
if ( ! $comments ) {
return __( 'No comments yet.', 'admin-edit-comment' );
}
$limit = ( count( $comments ) >= apply_filters( 'aec_limit_per_post', 20 ) ) ? 'exceeds' : '';
$content_content = '<input type="hidden" name="aec_limit" value="' . $limit . '">';
foreach ( $comments as $comment ) {
$comment_text = nl2br( htmlspecialchars( $comment->post_content, ENT_QUOTES, 'UTF-8' ) );
if ( (int) wp_get_current_user()->ID === (int) $comment->post_author ) {
$delete_button = '<span class="aec_delete dashicons dashicons-trash" href=\'javascript:void(0)\' comment_id="' . $comment->ID . '"></span>';
$is_others = '';
$author_name = wp_get_current_user()->display_name;
$avatar = get_avatar( wp_get_current_user()->ID, 18 );
} else {
$delete_button = '';
$is_others = 'others';
$author = get_user_by( 'id', $comment->post_author );
$author_name = ( $author ) ? $author->display_name : '';
$avatar = get_avatar( $comment->post_author, 18 );
}
$content_content
.= <<<HTML
<div id="admin_edit_comment-{$comment->ID}">
<div class="comment_head {$is_others}">
<div class="comment_author">
{$avatar}<strong class="comment_author_name">{$author_name}</strong>
</div>
</div>
<div class="comment_body {$is_others}">{$comment_text}</div>
<div class="comment_date">{$comment->post_date}{$delete_button}</div>
</div>
HTML;
}
return $content_content;
}
/**
* Insert comment.
*/
public function insert_comment() {
$post_id = filter_input( INPUT_POST, 'post_id' );
$comment = filter_input( INPUT_POST, 'comment' );
if ( ! $post_id || ! $comment ) {
wp_send_json_error( [ 'message' => __( 'Oops! Failed to get necessary parameter.', 'admin-edit-comment' ) ] );
}
$user = wp_get_current_user();
if ( ! $insert_post_id = wp_insert_post( apply_filters( 'aec_insert_post_args', [
'post_author' => $user->ID,
'post_content' => $comment,
'post_status' => 'publish',
'post_parent' => $post_id,
'post_type' => self::POST_TYPE_NAME,
] ) ) ) {
wp_send_json_error( [ 'message' => __( 'Insert comment refused.', 'admin-edit-comment' ) ] );
}
/**
* Fires immediately after a comment is registered.
*
* @since 1.0.0
*
* @param string $post_id
* @param WP_User $user
* @param int $insert_post_id
*/
do_action( 'aec_after_insert_comment', $post_id, $user, $insert_post_id );
wp_send_json_success( [ 'comments' => $this->get_content_html( $post_id ) ] );
}
/**
* Delete comment.
*/
public function delete_comment() {
$post_id = filter_input( INPUT_POST, 'post_id' );
$comment_post_id = filter_input( INPUT_POST, 'comment_id' );
if ( ! $post_id || ! $comment_post_id ) {
wp_send_json_error( [ 'message' => __( 'WTH! Failed to get necessary parameter.', 'admin-edit-comment' ) ] );
}
if ( ! wp_delete_post( $comment_post_id, true ) ) {
wp_send_json_error( [ 'message' => __( 'Failed to delete comment.', 'admin-edit-comment' ) ] );
}
wp_send_json_success( [ 'comments' => $this->get_content_html( $post_id ) ] );
}
}
Admin_Edit_Comment::instance();
/**
* Uninstalls Admin Edit Comment.
*/
function aec_uninstall() {
if ( is_multisite() ) {
$sites = get_sites();
foreach ( $sites as $site ) {
switch_to_blog( $site->blog_id );
delete_option( Admin_Edit_Comment::ADMIN_EDIT_COMMENT_OPTIONS );
restore_current_blog();
}
} else {
delete_option( Admin_Edit_Comment::ADMIN_EDIT_COMMENT_OPTIONS );
}
}