|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class Jetpack_Sync_Comments { |
|
4
|
|
|
|
|
5
|
|
|
static $sync = array(); |
|
|
|
|
|
|
6
|
|
|
static $delete = array(); |
|
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
static $max_to_sync = 20; |
|
|
|
|
|
|
9
|
|
|
static $que_option_name = 'jetpack_sync_comment_ids_que'; |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
static function init() { |
|
12
|
|
|
add_action( 'wp_insert_comment', array( __CLASS__, 'wp_insert_comment' ), 10, 2 ); |
|
13
|
|
|
add_action( 'transition_comment_status', array( __CLASS__, 'transition_comment_status' ), 10, 3 ); |
|
14
|
|
|
add_action( 'edit_comment', array( __CLASS__, 'edit_comment' ) ); |
|
15
|
|
|
add_action( 'delete_comment', array( __CLASS__, 'delete_comment' ) ); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
static function sync( $comment_id ) { |
|
19
|
|
|
self::$sync[] = $comment_id; |
|
20
|
|
|
Jetpack_Sync::schedule_sync(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
static function get_comment_ids_to_sync() { |
|
24
|
|
|
return Jetpack_Sync::slice_ids( self::$sync, self::$max_to_sync, self::$que_option_name ); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
static function comments_to_delete() { |
|
28
|
|
|
return array_unique( self::$delete ); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
static function wp_insert_comment( $comment_id, $comment ) { |
|
|
|
|
|
|
32
|
|
|
self::sync( $comment_id ); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
static function transition_comment_status( $new_status, $old_status, $comment ) { |
|
36
|
|
|
self::sync( $comment->comment_ID ); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
static function edit_comment( $comment_id ) { |
|
40
|
|
|
self::sync( $comment_id ); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
static function delete_comment( $comment_id ) { |
|
44
|
|
|
self::$delete[] = $comment_id; |
|
45
|
|
|
Jetpack_Sync::schedule_sync(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
static function comments_to_sync() { |
|
49
|
|
|
// Preserve the global comment |
|
50
|
|
|
$global_comment = isset( $GLOBALS['comment'] ) ? $GLOBALS['comment'] : null; |
|
51
|
|
|
unset( $GLOBALS['comment'] ); |
|
52
|
|
|
|
|
53
|
|
|
$comments = array(); |
|
54
|
|
|
foreach ( self::get_comment_ids_to_sync() as $comment_id ) { |
|
55
|
|
|
$comments[ $comment_id ] = self::get_comment( $comment_id ); |
|
56
|
|
|
} |
|
57
|
|
|
$GLOBALS['comment'] = $global_comment; |
|
58
|
|
|
unset( $global_comment ); |
|
59
|
|
|
return $comments; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
static function get_comment( $comment_id ) { |
|
63
|
|
|
// $defaults = array( |
|
64
|
|
|
// 'post_types' => array( 'post', 'page' ), |
|
65
|
|
|
// // For what post types will we sync comments? |
|
66
|
|
|
// 'post_stati' => array( 'publish' ), |
|
67
|
|
|
// // For what post stati will we sync comments? |
|
68
|
|
|
// 'comment_types' => array( '', 'comment', 'trackback', 'pingback' ), |
|
69
|
|
|
// // What comment types will we sync? |
|
70
|
|
|
// 'comment_stati' => array( 'approved' ), |
|
71
|
|
|
// // What comment stati will we sync? |
|
72
|
|
|
// ); |
|
73
|
|
|
$comment_obj = get_comment( $comment_id ); |
|
74
|
|
|
if ( ! $comment_obj ) { |
|
75
|
|
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
$comment = get_object_vars( $comment_obj ); |
|
78
|
|
|
|
|
79
|
|
|
$meta = get_comment_meta( $comment_id, false ); |
|
80
|
|
|
$comment['meta'] = array(); |
|
81
|
|
|
foreach ( $meta as $key => $value ) { |
|
82
|
|
|
$comment['meta'][ $key ] = array_map( 'maybe_unserialize', $value ); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $comment; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.