|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
require_once dirname( __FILE__ ) . '/class.jetpack-sync-listener.php'; |
|
4
|
|
|
|
|
5
|
|
|
class Jetpack_Sync_Packager { |
|
6
|
|
|
private $actions; |
|
7
|
|
|
private $packages; |
|
8
|
|
|
protected $listener; |
|
9
|
|
|
// singleton functions |
|
10
|
|
|
private static $instance; |
|
11
|
|
|
|
|
12
|
|
|
public static function get_instance() { |
|
13
|
|
|
if ( null === self::$instance ) { |
|
14
|
|
|
self::$instance = new self(); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
return self::$instance; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
// this is necessary because you can't use "new" when you declare instance properties >:( |
|
21
|
|
|
protected function __construct() { |
|
22
|
|
|
$this->set_defaults(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
private function set_defaults() { |
|
26
|
|
|
$this->actions = array(); |
|
27
|
|
|
$this->listener = Jetpack_Sync_Listener::get_instance(); |
|
28
|
|
|
$this->packages = array( |
|
29
|
|
|
'jetpack_published_post' => array(), |
|
30
|
|
|
'jetpack_sync_save_post' => array(), |
|
31
|
|
|
); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
function add( $action, $args ) { |
|
35
|
|
|
$this->actions[] = array( |
|
36
|
|
|
'trigger' => $action, |
|
37
|
|
|
'args' => $args, |
|
38
|
|
|
); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
function did_action( $action ) { |
|
42
|
|
|
return in_array( $action, wp_list_pluck( $this->actions, 'trigger' ) ); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
function create_post_saved_packages( $action ) { |
|
46
|
|
|
if ( 'jetpack_sync_save_post' === $action['trigger'] ) { |
|
47
|
|
|
$post_id = $action['args'][0]; |
|
48
|
|
|
$post = $action['args'][1]; |
|
49
|
|
|
$state = $action['args'][3]; |
|
50
|
|
|
$this->packages['jetpack_sync_save_post'][ $post_id ] = new Jetpack_Sync_Package(); |
|
51
|
|
|
$this->packages['jetpack_sync_save_post'][ $post_id ]->set_state( $state ); |
|
52
|
|
|
$this->packages['jetpack_sync_save_post'][ $post_id ]->set_object( $post ); |
|
53
|
|
|
} |
|
54
|
|
|
return true; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
function add_related_actions_to_post_saved_packages( $action ) { |
|
58
|
|
|
return true; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
function create_post_published_packages( $action ) { |
|
62
|
|
|
if ( 'jetpack_published_post' === $action['trigger'] ) { |
|
63
|
|
|
$post_id = $action['args'][0]; |
|
64
|
|
|
$state = $action['args'][1]; |
|
65
|
|
|
$this->packages['jetpack_published_post'][ $post_id ] = new Jetpack_Sync_Package(); |
|
66
|
|
|
$this->packages['jetpack_published_post'][ $post_id ]->set_state( $state ); |
|
67
|
|
|
} |
|
68
|
|
|
return true; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
function add_related_actions_to_post_published_packages( $action ) { |
|
72
|
|
|
if ( 'jetpack_sync_save_post' === $action['trigger'] ) { |
|
73
|
|
|
$post_id = $action['args'][0]; |
|
74
|
|
|
$post = $action['args'][1]; |
|
75
|
|
|
$state = $action['args'][3]; |
|
76
|
|
|
if ( isset( $this->packages['jetpack_published_post'][ $post_id ] ) ) { |
|
77
|
|
|
$this->packages['jetpack_published_post'][ $post_id ]->set_object( $post ); |
|
78
|
|
|
$this->packages['jetpack_published_post'][ $post_id ]->set_state( array_merge( $state, $this->packages['jetpack_published_post'][ $post_id ]->get_state() ) ); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
return true; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
function package_post_saved() { |
|
85
|
|
|
// Filter out `jetpack_sync_save_post` actions, and create packages. |
|
86
|
|
|
$this->actions = array_filter( $this->actions, array( $this, 'create_post_saved_packages' ) ); |
|
87
|
|
|
// Filter out actions related to `jetpack_sync_save_post`, and add them to packages. |
|
88
|
|
|
$this->actions = array_filter( $this->actions, array( $this, 'add_related_actions_to_post_saved_packages' ) ); |
|
89
|
|
|
// print_r( "\n" ); |
|
90
|
|
|
// print_r( "\n" ); |
|
91
|
|
|
// print_r( 'packageing post saved' ); |
|
92
|
|
|
// print_r( "\n" ); |
|
93
|
|
|
// print_r( "\n" ); |
|
94
|
|
|
// print_r( $this->packages ); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
function package_post_published() { |
|
98
|
|
|
// Filter out `jetpack_published_post` actions, and create packages. |
|
99
|
|
|
$this->actions = array_filter( $this->actions, array( $this, 'create_post_published_packages' ) ); |
|
100
|
|
|
// Filter out actions related to `jetpack_published_post`, and add them to packages. |
|
101
|
|
|
$this->actions = array_filter( $this->actions, array( $this, 'add_related_actions_to_post_published_packages' ) ); |
|
102
|
|
|
// print_r( "\n" ); |
|
103
|
|
|
// print_r( "\n" ); |
|
104
|
|
|
// print_r( 'packageing post published' ); |
|
105
|
|
|
// print_r( "\n" ); |
|
106
|
|
|
// print_r( "\n" ); |
|
107
|
|
|
// print_r( $this->packages ); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
function send_items_to_queue() { |
|
111
|
|
|
if ( empty( $this->actions ) ) { |
|
112
|
|
|
return; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if ( $this->did_action( 'jetpack_published_post' ) ) { |
|
116
|
|
|
$this->package_post_published(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
if ( $this->did_action( 'jetpack_sync_save_post' ) ) { |
|
120
|
|
|
$this->package_post_saved(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
foreach ( $this->actions as $action ) { |
|
124
|
|
|
$this->listener->enqueue_action( $action['trigger'], $action['args'], $this->listener->get_sync_queue() ); |
|
125
|
|
|
} |
|
126
|
|
|
// print_r( "\n" ); |
|
127
|
|
|
// print_r( "\n" ); |
|
128
|
|
|
// print_r( $this->actions ); |
|
129
|
|
|
// Reset Items |
|
130
|
|
|
$this->actions = array(); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
class Jetpack_Sync_Package { |
|
|
|
|
|
|
135
|
|
|
private $object; |
|
136
|
|
|
private $state; |
|
137
|
|
|
private $related_actions; |
|
138
|
|
|
|
|
139
|
|
|
function __construct() { |
|
140
|
|
|
$this->related_actions = array(); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
function set_state( $state ) { |
|
144
|
|
|
$this->state = $state; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
function get_state() { |
|
148
|
|
|
return $this->state; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
function set_object( $object ) { |
|
152
|
|
|
$this->object = $object; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
function add_related_action( $trigger, $args ) { |
|
156
|
|
|
$this->related_actions[] = array( |
|
157
|
|
|
'trigger' => $trigger, |
|
158
|
|
|
'args' => $args, |
|
159
|
|
|
); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.