Completed
Push — add/sync-rest-2 ( 120bfd...3b8bcb )
by
unknown
10:10 queued 46s
created

Jetpack_Sync_Client::switch_theme_handler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
require_once dirname( __FILE__ ) . '/class.jetpack-sync-deflate-codec.php';
3
4
class Jetpack_Sync_Client {
5
	private $sync_queue = array();
6
	private $codec;
7
	private $options_whitelist = array( 'stylesheet', '/^theme_mods_.*$/' );
8
9
	// this is necessary because you can't use "new" when you declare instance properties >:(
10
	function __construct() {
11
		$this->codec = new Jetpack_Sync_Deflate_Codec();
12
	}
13
14
	function init() {
15
		$handler = array( $this, 'action_handler' );
16
		// posts
17
		add_action( 'wp_insert_post', $handler, 10, 3 );
18
		add_action( 'delete_post', $handler, 10 );
19
		// comments
20
		add_action( 'wp_insert_comment', $handler, 10, 2 );
21
		add_action( 'deleted_comment', $handler, 10 );
22
		add_action( 'trashed_comment', $handler, 10 );
23
		add_action( 'spammed_comment', $handler, 10 );
24
		// even though it's messy, we implement these hooks because the edit_comment hook doesn't include the data
25
		foreach ( array( '', 'trackback', 'pingback' ) as $comment_type ) {
26
			foreach ( array( 'unapproved', 'approved' ) as $comment_status ) {
27
				add_action( "comment_{$comment_status}_{$comment_type}", $handler, 10, 2 );
28
			}
29
		}
30
31
		// options
32
		add_action( 'added_option', $handler, 10, 2 );
33
		add_action( 'updated_option', $handler, 10, 3 );
34
		add_action( 'deleted_option', $handler, 10, 1 );
35
36
		// themes
37
		add_action( 'jetpack_sync_current_theme_support', $handler, 10 ); // custom hook, see meta-hooks below
38
39
		/**
40
		 * Meta-hooks - fire synthetic hooks for all the properties we need to sync, 
41
		 * e.g. when a theme changes
42
		 */
43
44
		// themes
45
		add_action( 'switch_theme', array( $this, 'switch_theme_handler' ) );
46
	}
47
48
	function set_options_whitelist( $options ) {
49
		$this->options_whitelist = $options;
50
	}
51
52
	function is_whitelisted_option( $option ) {
53
		foreach ( $this->options_whitelist as $whitelisted_option ) {
54
			if ( $whitelisted_option[0] === '/' && preg_match( $whitelisted_option, $option ) ) {
55
				return true;
56
			} elseif ( $whitelisted_option === $option ) {
57
				return true;
58
			}
59
		}
60
		return false;
61
	}
62
63
	function set_codec( iJetpack_Sync_Codec $codec ) {
64
		$this->codec = $codec;
65
	}
66
67
	function action_handler() {
68
		$current_filter = current_filter();
69
		$args           = func_get_args();
70
71
		if ( $current_filter === 'wp_insert_post' && $args[1]->post_type === 'revision' ) {
72
			return;
73
		}
74
75
		if ( in_array( $current_filter, array( 'deleted_option', 'added_option', 'updated_option' ) ) 
76
			&& 
77
			! $this->is_whitelisted_option( $args[0] ) ) {
78
			return;
79
		}
80
		
81
		Jetpack_Sync::schedule_sync();
82
		$this->sync_queue[] = array(
83
			$current_filter,
84
			$args
85
		);
86
	}
87
88
	function switch_theme_handler() {
89
		global $_wp_theme_features;
90
		
91
		do_action( 'jetpack_sync_current_theme_support', $_wp_theme_features );
92
	}
93
94
	function do_sync() {
95
		$data = $this->codec->encode( $this->sync_queue );
96
97
		/**
98
		 * Fires when data is ready to send to the server
99
		 *
100
		 * @since 4.1
101
		 *
102
		 * @param array $data The action buffer
103
		 */
104
		apply_filters( 'jetpack_sync_client_send_data', $data );
105
	}
106
107
	function get_actions() {
108
		return $this->sync_queue;
109
	}
110
111
	function reset_actions() {
112
		$this->sync_queue = array();
113
	}
114
}
115