Completed
Push — add/sync-rest-2 ( 0f1049...2638b5 )
by
unknown
09:02
created

Jetpack_Sync_Comments::slice_ids()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 11
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 11
loc 11
rs 9.4285
cc 2
eloc 8
nc 2
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A Jetpack_Sync_Comments::delete_comment() 0 4 1
1
<?php
2
3
class Jetpack_Sync_Comments {
4
5
	static $sync = array();
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $sync.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
6
	static $delete = array();
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $delete.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
7
8
	static $max_to_sync = 20;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $max_to_sync.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
9
	static $que_option_name = 'jetpack_sync_comment_ids_que';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $que_option_name.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
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 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $comment is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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