Completed
Push — update/sync-posts-less-events ( df315a )
by
unknown
09:51
created

Jetpack_Sync_Item::get_object()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
class Jetpack_Sync_Item {
4
	private $object; // The object we are syncing, eg a post
5
	private $items = array(); // Related sync items that we want to send in the same request, eg categories and tags, post meta, revision
6
	private $state; // The state of the object, eg `is_just_published`
7
	private $trigger; // The action that triggers the sync operation, eg `save_post`
8
9
	function __construct( $trigger, $object = null ) {
10
		$this->trigger = $trigger;
11
		if ( $object ) {
12
			$this->set_object( $object );
13
		}
14
	}
15
16
	function add_sync_item( Jetpack_Sync_Item $item ) {
17
		$this->items[] = $item;
18
	}
19
20
	function set_object( $object ) {
21
		$this->object = $object;
22
	}
23
24
	function get_object() {
25
		return $this->object;
26
	}
27
28
	function set_state_value( $key, $value = null ) {
29
		if ( is_array( $key ) ) {
30
			$this->state = array_merge( $this->state, $key );
31
		} else if ( is_string( $key ) && ! is_null( $value ) ) {
32
			$this->state[ $key ] = $value;
33
		}
34
	}
35
36
	function state_isset( $key ) {
37
		return isset( $this->state[ $key ] );
38
	}
39
40
	function get_state() {
41
		return $this->state;
42
	}
43
44
	function set_state( $state ) {
45
		return $this->state = $state;
46
	}
47
48
	function is_state_value_true( $key ) {
49
		return ( $this->state_isset( $key ) && (bool) $this->state[ $key ] );
50
	}
51
52
	function get_payload() {
53
		if ( empty( $this->object ) ) {
54
			return false;
55
		}
56
		$payload = array( 'object' => $this->object, 'trigger' => $this->trigger );
57
		foreach ( $this->items as $item ) {
58
			$payload[ 'items' ][] = $item->get_payload();
59
		}
60
		if ( ! empty( $this->state ) ) {
61
			$payload[ 'state' ] = $this->state;
62
		}
63
64
		return $payload;
65
	}
66
}
67