-
Notifications
You must be signed in to change notification settings - Fork 3
/
internal-comments-class.php
476 lines (407 loc) · 13.6 KB
/
internal-comments-class.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
<?php
! defined( 'ABSPATH' ) AND exit;
/*
Plugin Name: Private Comments for CPT
Plugin URI: https://github.com/baden03/Private-Comments-in-CPT
Description: Enables internal comments for a given Custom Post Type when Editing Draft or Pending posts.
Author: Rodolfo Buaiz, Twinpictures
Author URI: https://rodbuaiz.com/
Version: 2023.01.25.01
License: GPL
*/
class InternalComments {
private $cpt_include = array( 'draft', 'pending' ); // Where to enable the comments
private $cpt_exclude = array( 'trash' ); // Deny only if cpt comment in trash
private $other_exclude = array('draft','pending','trash'); // Deny to the rest of post_types
private $row_id = 'inner_msgs';
public function __construct() {
add_action( 'admin_init', array( $this, 'text_domain' ) );
add_action( 'admin_init', array( $this, 'init_admin') );
/*
if (!is_admin()) {
add_action( 'init', array( $this, 'init_front') );
}
*/
}
function init_admin(){
add_action( 'current_screen', array( $this, 'exclude_lazy_hook' ), 10, 2 );
add_action( 'wp_ajax_replyto-comment', array( $this, 'ajax_replyto_comment' ), 0 );
if( isset( $_GET['post'] ) ){
$this->add_comment_metabox();
}
add_action( 'admin_footer-edit-comments.php', array( $this, 'karma_row_bg_color' ) );
add_action( 'load-edit-comments.php', array( $this, 'wpse_64973_load' ) );
add_action( 'manage_comments_custom_column', array( $this, 'wpse_64973_column_cb' ), 10, 2 );
}
function is_iccpt( $comment_post_id = null ){
// Allow cpts to be filtered
$cpt = apply_filters( 'internal_comments_cpt', array( 'portfolio' ) );
return in_array( get_post_type($comment_post_id), $cpt, true );
}
function init_front() {
add_filter( 'comments_array', array( $this, 'remove_karmic_comments' ), 20, 2 );
}
function text_domain(){
load_plugin_textdomain(
'iccpt',
FALSE,
dirname( plugin_basename(__FILE__) ) . '/languages/'
);
}
/**
* Delay hooking our clauses filter to ensure it's only applied when needed.
*
* @param object $screen
* @return void
**/
function exclude_lazy_hook( $screen ){
if ($screen->id != 'edit-comments') {
return;
}
// Check if our Query Var is defined
if (isset($_GET['internal_messages'])) {
add_action( 'pre_get_comments', array( $this, 'list_only_internal_messages' ), 10, 1 );
}
add_filter( 'comment_status_links', array( $this, 'link_to_internal_messages' ) );
}
/**
* Only display comments of specific karma
*
* @param integer $cols
* @return object WP_Comment_Query
**/
function list_only_internal_messages( $clauses ){
$clauses->query_vars['karma'] = 3;
return $clauses;
}
/**
* Add link to custom comments karma with counter
*
* @param integer $cols
* @return integer
**/
function link_to_internal_messages( $status_links ){
global $wpdb;
$count = count(
$wpdb->get_results(
$wpdb->prepare(
"SELECT comment_ID FROM $wpdb->comments
WHERE comment_karma = %d
AND comment_approved != %s",
3,
'trash'
)
)
);
if( isset( $_GET['internal_messages'] ) ) {
$status_links['all'] =
'<a href="edit-comments.php?comment_status=all">'
. __('All')
. '</a>';
$status_links['internal_messages'] =
'<a href="edit-comments.php?comment_status=all&internal_messages=1" class="current" style="margin-leff:60px">'
. __( 'Internal Comments', 'iccpt' )
. ' <span class="count">('
. $count
. ')</a></span>';
}
else {
$status_links['internal_messages'] =
'<a href="edit-comments.php?comment_status=all&internal_messages=1" style="margin-leff:60px">'
. __( 'Internal Comments', 'iccpt' )
. ' <span class="count">('
. $count
. ')</span></a>';
}
return $status_links;
}
/**
* Modified copy of wp_ajax_replyto_comment()
* /wp-admin/includes/ajax-actions.php
*
* Adjust the CPT that defines $diff_status
* TODO: permissions should be cpt caps and defined in a plugin settings page
*/
function ajax_replyto_comment( $action ) {
global $wp_list_table, $wpdb;
$comment_post_id = (int) $_POST['comment_post_ID'];
if ( !current_user_can( 'edit_post', $comment_post_id ) )
wp_die( -1 );
if ( empty( $action ) )
$action = 'replyto-comment';
check_ajax_referer( $action, '_ajax_nonce-replyto-comment' );
set_current_screen( 'edit-comments' );
$status = $wpdb->get_var( $wpdb->prepare( "SELECT post_status FROM $wpdb->posts WHERE ID = %d", $comment_post_id ) );
if( !empty($comment_post_id) && $this->is_iccpt($comment_post_id) )
$diff_status = $this->cpt_exclude;
else
$diff_status = $this->other_exclude;
if ( empty( $status ) )
wp_die( 1 );
elseif ( in_array( $status, $diff_status ) )
wp_die( __('ERROR: you are replying to a comment on a draft post.', 'iccpt' ) );
$user = wp_get_current_user();
if ( $user->exists() ) {
$comment_author = wp_slash( $user->display_name );
$comment_author_email = wp_slash( $user->user_email );
$comment_author_url = wp_slash( $user->user_url );
$user_id = $user->ID;
$comment_content = trim( $_POST['content'] );
if ( current_user_can( 'unfiltered_html' ) ) {
if ( wp_create_nonce( 'unfiltered-html-comment' ) != $_POST['_wp_unfiltered_html_comment'] ) {
kses_remove_filters(); // start with a clean slate
kses_init_filters(); // set up the filters
}
}
}
else {
wp_die( __( 'Sorry, you must be logged in to reply to a comment.', 'iccpt' ) );
}
if ( '' == $comment_content ){
wp_die( __( 'ERROR: please type a comment.', 'iccpt' ) );
}
$comment_parent = absint($_POST['comment_ID']);
$comment_auto_approved = false;
//$commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'comment_parent', 'user_ID');
$commentdata = array(
'comment_post_ID' => $comment_post_id,
);
$commentdata += compact(
'comment_author',
'comment_author_email',
'comment_author_url',
'comment_content',
'comment_type',
'comment_parent',
'user_id'
);
$commentdata['comment_karma'] =
( $diff_status == $this->other_exclude )
? $commentdata['comment_karma']
: 3;
$comment_id = wp_new_comment( $commentdata );
$comment = get_comment($comment_id);
if (!$comment) {
wp_die( 1 );
}
if (!in_array('draft', $diff_status)) {
update_comment_meta( $comment_id, $this->row_id, 'yes' );
}
$position = ( isset($_POST['position']) && (int) $_POST['position'] ) ? (int) $_POST['position'] : '-1';
// automatically approve parent comment
if ( !empty($_POST['approve_parent']) ) {
$parent = get_comment( $comment_parent );
if ( $parent && $parent->comment_approved === '0' && $parent->comment_post_ID == $comment_post_id ) {
if ( wp_set_comment_status( $parent->comment_ID, 'approve' ) ){
$comment_auto_approved = true;
}
}
}
ob_start();
if ( 'dashboard' == $_REQUEST['mode'] ) {
require_once( ABSPATH . 'wp-admin/includes/dashboard.php' );
_wp_dashboard_recent_comments_row( $comment );
}
else {
if ( 'single' == $_REQUEST['mode'] ) {
$wp_list_table = _get_list_table('WP_Post_Comments_List_Table');
}
else {
$wp_list_table = _get_list_table('WP_Comments_List_Table');
}
$wp_list_table->single_row( $comment );
}
$comment_list_item = ob_get_contents();
ob_end_clean();
$response = array(
'what' => 'comment',
'id' => $comment->comment_ID,
'data' => $comment_list_item,
'position' => $position
);
if ($comment_auto_approved) {
$response['supplemental'] = array( 'parent_approved' => $parent->comment_ID );
}
$x = new WP_Ajax_Response();
$x->add( $response );
$x->send();
}
/**
* Enables the default Comments Meta Box
*
* @return void
*/
function add_comment_metabox()
{
$post_id = absint( $_GET['post'] );
$post = get_post( $post_id );
if ( in_array( $post->post_status, $this->cpt_include ) )
{
add_meta_box(
'commentsdiv'
, __( 'Offline Comments', 'iccpt' )
, 'post_comment_meta_box'
, apply_filters( 'internal_comments_cpt', array( 'portfolio' ) )
, 'normal'
, 'core'
);
}
}
/**
* Removes comments with Karma from frontend
*
* @param object $comments
* @param int $post_id
* @return object
**/
function remove_karmic_comments( $comments, $post_id ){
foreach ( $comments as $index => $c )
{
if ( $c->comment_karma == 3 )
unset( $comments[ $index ] );
}
return $comments;
}
//TODO: this should be moved to external files / plugin options page
function karma_row_bg_color(){
if( isset( $_GET['internal_messages'] ) )
return;
?>
<script type="text/javascript">
jQuery(document).ready( function($) {
//$('.inner-msgs-span').parent().parent().css('background-color','#858585').fadeTo('slow', 1);
$('.inner-msgs-span').parent().parent().fadeTo('fast', .3, function()
{
$(this).css('background-color', '#E3E3E3');
}).fadeTo('slow', 1);
});
</script>
<?php
}
/*
Plugin Name. Add Extra Comment Columns for Comment_Meta
Author URI. http://pmg.co/people/chris
License. MIT
*/
function wpse_64973_load(){
if (isset($_GET['internal_messages'])) {
return;
}
$screen = get_current_screen();
add_filter( "manage_{$screen->id}_columns", array( $this, 'wpse_64973_add_columns' ) );
}
function wpse_64973_add_columns( $cols ){
$cols[$this->row_id] = __( 'Internal Comments', 'iccpt' );
return $cols;
}
function wpse_64973_column_cb( $col, $comment_id ){
switch( $col ){
case $this->row_id:
if( 'yes' == get_comment_meta( $comment_id, $this->row_id, true ) )
{
echo '<span style="color:#f00;font-size:7em;line-height:.5em;margin-left:15%" class="inner-msgs-span"><sub>•</sub></span>';
}
break;
}
} /* end plugin */
} /* end class */
$WP_InternalComments = new InternalComments;
// http://wordpress.stackexchange.com/questions/25910
if ( ! class_exists('InternalCommentsInit' ) ) :
/**
* This class triggers functions that run during activation/deactivation & uninstallation
*/
class InternalCommentsInit
{
// Set this to true to get the state of origin, so you don't need to always uninstall during development.
const STATE_OF_ORIGIN = false;
function __construct( $case = false )
{
if ( ! $case )
wp_die( 'Busted! You should not call this class directly', 'Doing it wrong!' );
switch( $case )
{
case 'activate' :
add_action( 'init', array( &$this, 'activate_cb' ) );
break;
case 'deactivate' :
add_action( 'init', array( &$this, 'deactivate_cb' ) );
break;
case 'uninstall' :
add_action( 'init', array( &$this, 'uninstall_cb' ) );
break;
}
}
/**
* Set up tables, add options, etc. - All preparation that only needs to be done once
*/
function on_activate()
{
new InternalCommentsInit( 'activate' );
}
/**
* Do nothing like removing settings, etc.
* The user could reactivate the plugin and wants everything in the state before activation.
* Take a constant to remove everything, so you can develop & test easier.
*/
function on_deactivate()
{
$case = 'deactivate';
if ( STATE_OF_ORIGIN )
$case = 'uninstall';
new InternalCommentsInit( $case );
}
/**
* Remove/Delete everything - If the user wants to uninstall, then he wants the state of origin.
*
* Will be called when the user clicks on the uninstall link that calls for the plugin to uninstall itself
*/
function on_uninstall()
{
// important: check if the file is the one that was registered with the uninstall hook (function)
if ( __FILE__ != WP_UNINSTALL_PLUGIN )
return;
new InternalCommentsInit( 'uninstall' );
}
function activate_cb()
{
// Stuff like adding default option values to the DB
wp_die( '<h1>This is run on <code>init</code> during activation.</h1>', 'Activation hook example' );
}
function deactivate_cb()
{
// if you need to output messages in the 'admin_notices' field, do it like this:
$this->error( "Some message.<br />" );
// if you need to output messages in the 'admin_notices' field AND stop further processing, do it like this:
$this->error( "Some message.<br />", TRUE );
// Stuff like remove_option(); etc.
wp_die( '<h1>This is run on <code>init</code> during deactivation.</h1>', 'Deactivation hook example' );
}
function uninstall_cb()
{
// Stuff like delete tables, etc.
wp_die( '<h1>This is run on <code>init</code> during uninstallation</h1>', 'Uninstallation hook example' );
}
/**
* trigger_error()
*
* @param (string) $error_msg
* @param (boolean) $fatal_error | catched a fatal error - when we exit, then we can't go further than this point
* @param unknown_type $error_type
* @return void
*/
function error( $error_msg, $fatal_error = false, $error_type = E_USER_ERROR )
{
if( isset( $_GET['action'] ) && 'error_scrape' == $_GET['action'] )
{
echo "{$error_msg}\n";
if ( $fatal_error )
exit;
}
else
{
trigger_error( $error_msg, $error_type );
}
}
}
endif;