Completed
Push — fix/send-callables-after-plugi... ( 88eb16...d535ce )
by
unknown
07:50
created

unlock_sync_callable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
require_once dirname( __FILE__ ) . '/class.jetpack-sync-functions.php';
4
5
class Jetpack_Sync_Module_Callables extends Jetpack_Sync_Module {
6
	const CALLABLES_CHECKSUM_OPTION_NAME = 'jetpack_callables_sync_checksum';
7
	const CALLABLES_AWAIT_TRANSIENT_NAME = 'jetpack_sync_callables_await';
8
9
	private $callable_whitelist;
10
11
	public function name() {
12
		return 'functions';
13
	}
14
15
	public function set_defaults() {
16
		if ( is_multisite() ) {
17
			$this->callable_whitelist = array_merge( Jetpack_Sync_Defaults::get_callable_whitelist(), Jetpack_Sync_Defaults::get_multisite_callable_whitelist() );
18
		} else {
19
			$this->callable_whitelist = Jetpack_Sync_Defaults::get_callable_whitelist();
20
		}
21
	}
22
23
	public function init_listeners( $callable ) {
24
		add_action( 'jetpack_sync_callable', $callable, 10, 2 );
25
		add_action( 'admin_init', array( $this, 'set_plugin_action_links' ), 9999 ); // Should happen very late
26
27
		// For some options, we should always send the change right away!
28
		$always_send_updates_to_these_options = array(
29
			'jetpack_active_modules',
30
			'home',
31
			'siteurl',
32
			'jetpack_sync_error_idc',
33
		);
34
		foreach( $always_send_updates_to_these_options as $option ) {
35
			add_action( "update_option_{$option}", array( $this, 'unlock_sync_callable' ) );
36
		}
37
38
		add_action( 'jetpack_pre_plugin_upgrade', array( $this, 'unlock_sync_callable' ) );
39
40
		// Provide a hook so that hosts can send changes to certain callables right away.
41
		// Especially useful when a host uses constants to change home and siteurl.
42
		add_action( 'jetpack_sync_unlock_sync_callable', array( $this, 'unlock_sync_callable' ) );
43
44
		// get_plugins and wp_version
45
		// gets fired when new code gets installed, updates etc.
46
		add_action( 'upgrader_process_complete', array( $this, 'unlock_plugin_action_link_and_callables' ) );
47
		add_action( 'update_option_active_plugins', array( $this, 'unlock_plugin_action_link_and_callables' ) );
48
	}
49
50
	public function init_full_sync_listeners( $callable ) {
51
		add_action( 'jetpack_full_sync_callables', $callable );
52
	}
53
54
	public function init_before_send() {
55
		add_action( 'jetpack_sync_before_send_queue_sync', array( $this, 'maybe_sync_callables' ) );
56
57
		// full sync
58
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_callables', array( $this, 'expand_callables' ) );
59
	}
60
61
	public function reset_data() {
62
		delete_option( self::CALLABLES_CHECKSUM_OPTION_NAME );
63
		delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME );
64
65
		$url_callables = array( 'home_url', 'site_url', 'main_network_site_url' );
66
		foreach( $url_callables as $callable ) {
67
			delete_option( Jetpack_Sync_Functions::HTTPS_CHECK_OPTION_PREFIX . $callable );
68
		}
69
	}
70
71
	function set_callable_whitelist( $callables ) {
72
		$this->callable_whitelist = $callables;
73
	}
74
75
	function get_callable_whitelist() {
76
		return $this->callable_whitelist;
77
	}
78
79
	public function get_all_callables() {
80
		// get_all_callables should run as the master user always.
81
		$current_user_id = get_current_user_id();
82
		wp_set_current_user( Jetpack_Options::get_option( 'master_user' ) );
83
		$callables = array_combine(
84
			array_keys( $this->get_callable_whitelist() ),
85
			array_map( array( $this, 'get_callable' ), array_values( $this->get_callable_whitelist() ) )
86
		);
87
		wp_set_current_user( $current_user_id );
88
		return $callables;
89
	}
90
91
	private function get_callable( $callable ) {
92
		return call_user_func( $callable );
93
	}
94
95
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
96
		/**
97
		 * Tells the client to sync all callables to the server
98
		 *
99
		 * @since 4.2.0
100
		 *
101
		 * @param boolean Whether to expand callables (should always be true)
102
		 */
103
		do_action( 'jetpack_full_sync_callables', true );
104
105
		// The number of actions enqueued, and next module state (true == done)
106
		return array( 1, true );
107
	}
108
109
	public function estimate_full_sync_actions( $config ) {
110
		return 1;
111
	}
112
113
	public function get_full_sync_actions() {
114
		return array( 'jetpack_full_sync_callables' );
115
	}
116
117
	public function unlock_sync_callable() {
118
		add_filter( 'jetpack_check_and_send_callables', '__return_true' );
119
		error_log('unlock_sync_callable    ' . getmypid() );
120
		delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME );
121
	}
122
123
	public function unlock_plugin_action_link_and_callables() {
124
		delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME );
125
		delete_transient( 'jetpack_plugin_api_action_links_refresh' );
126
	}
127
128
	public function set_plugin_action_links() {
129
		if (
130
			! class_exists( 'DOMDocument' ) ||
131
			! function_exists ( 'libxml_use_internal_errors' ) ||
132
			! function_exists ( 'mb_convert_encoding' )
133
		) {
134
			return;
135
		}
136
137
		$plugins_action_links = array();
138
		// Is the transient lock in place?
139
		$plugins_lock = get_transient( 'jetpack_plugin_api_action_links_refresh', false );
140
		if ( ! empty( $plugins_lock ) ) {
141
			return;
142
		}
143
		$plugins = array_keys( Jetpack_Sync_Functions::get_plugins() );
144
		foreach ( $plugins as $plugin_file ) {
145
			/**
146
			 *  Plugins often like to unset things but things break if they are not able to.
147
			 */
148
			$action_links = array(
149
				'deactivate' => '',
150
				'activate' => '',
151
				'details' => '',
152
				'delete' => '',
153
				'edit' => ''
154
			);
155
			/** This filter is documented in src/wp-admin/includes/class-wp-plugins-list-table.php */
156
			$action_links = apply_filters( 'plugin_action_links', $action_links, $plugin_file, null, 'all' );
157
			/** This filter is documented in src/wp-admin/includes/class-wp-plugins-list-table.php */
158
			$action_links = apply_filters( "plugin_action_links_{$plugin_file}", $action_links, $plugin_file, null, 'all' );
159
			$action_links = array_filter( $action_links );
160
			$formatted_action_links = null;
161
			if ( ! empty( $action_links ) && count( $action_links ) > 0 ) {
162
				$dom_doc = new DOMDocument;
163
				foreach ( $action_links as $action_link ) {
164
					// The @ is not enough to suppress errors when dealing with libxml,
165
					// we have to tell it directly how we want to handle errors.
166
					libxml_use_internal_errors( true );
167
					$dom_doc->loadHTML( mb_convert_encoding( $action_link, 'HTML-ENTITIES', 'UTF-8' ) );
168
					libxml_use_internal_errors( false );
169
170
					$link_elements = $dom_doc->getElementsByTagName( 'a' );
171
					if ( $link_elements->length == 0 ) {
172
						continue;
173
					}
174
175
					$link_element = $link_elements->item( 0 );
176
					if ( $link_element->hasAttribute( 'href' ) && $link_element->nodeValue ) {
0 ignored issues
show
Bug introduced by
The method hasAttribute() does not exist on DOMNode. Did you maybe mean hasAttributes()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
177
						$link_url = trim( $link_element->getAttribute( 'href' ) );
178
179
						// Add the full admin path to the url if the plugin did not provide it
180
						$link_url_scheme = wp_parse_url( $link_url, PHP_URL_SCHEME );
181
						if ( empty( $link_url_scheme ) ) {
182
							$link_url = admin_url( $link_url );
183
						}
184
185
						$formatted_action_links[ $link_element->nodeValue ] = $link_url;
186
					}
187
				}
188
			}
189
			if ( $formatted_action_links ) {
190
				$plugins_action_links[ $plugin_file ] = $formatted_action_links;
191
			}
192
		}
193
		// Cache things for a long time
194
		set_transient( 'jetpack_plugin_api_action_links_refresh', time(), DAY_IN_SECONDS );
195
		update_option( 'jetpack_plugin_api_action_links', $plugins_action_links );
196
	}
197
198
	public function should_send_callable( $callable_checksums, $name, $checksum ) {
199
		$idc_override_callables = array(
200
			'main_network_site',
201
			'home_url',
202
			'site_url',
203
		);
204
		if ( in_array( $name, $idc_override_callables ) && Jetpack_Options::get_option( 'migrate_for_idc' ) ) {
205
			return true;
206
		}
207
208
		return ! $this->still_valid_checksum( $callable_checksums, $name, $checksum );
209
	}
210
211
	public function maybe_sync_callables() {
212
		if ( ! apply_filters( 'jetpack_check_and_send_callables', false ) ) {
213
			if ( ! is_admin() || Jetpack_Sync_Settings::is_doing_cron() ) {
214
				return;
215
			}
216
217
			if ( get_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ) ) {
218
				return;
219
			}
220
		}
221
222
		set_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME, microtime( true ), Jetpack_Sync_Defaults::$default_sync_callables_wait_time );
0 ignored issues
show
Bug introduced by
The property default_sync_callables_wait_time cannot be accessed from this context as it is declared private in class Jetpack_Sync_Defaults.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
223
224
		$callables = $this->get_all_callables();
225
226
		if ( empty( $callables ) ) {
227
			return;
228
		}
229
230
		$callable_checksums = (array) Jetpack_Options::get_raw_option( self::CALLABLES_CHECKSUM_OPTION_NAME, array() );
231
232
		// only send the callables that have changed
233 View Code Duplication
		foreach ( $callables as $name => $value ) {
234
			$checksum = $this->get_check_sum( $value );
235
			// explicitly not using Identical comparison as get_option returns a string
236
			if ( ! is_null( $value ) && $this->should_send_callable( $callable_checksums, $name, $checksum ) ) {
237
				/**
238
				 * Tells the client to sync a callable (aka function) to the server
239
				 *
240
				 * @since 4.2.0
241
				 *
242
				 * @param string The name of the callable
243
				 * @param mixed The value of the callable
244
				 */
245
				do_action( 'jetpack_sync_callable', $name, $value );
246
				$callable_checksums[ $name ] = $checksum;
247
			} else {
248
				$callable_checksums[ $name ] = $checksum;
249
			}
250
		}
251
		Jetpack_Options::update_raw_option( self::CALLABLES_CHECKSUM_OPTION_NAME, $callable_checksums );
252
	}
253
254
	public function expand_callables( $args ) {
255
		if ( $args[0] ) {
256
			$callables = $this->get_all_callables();
257
			$callables_checksums = array();
258
			foreach ( $callables as $name => $value ) {
259
				$callables_checksums[ $name ] = $this->get_check_sum( $value );
260
			}
261
			Jetpack_Options::update_raw_option( self::CALLABLES_CHECKSUM_OPTION_NAME, $callables_checksums );
262
			return $callables;
263
		}
264
265
		return $args;
266
	}
267
}
268