Automattic /
jetpack
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-deflate-codec.php'; |
||
| 3 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-queue.php'; |
||
| 4 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-functions.php'; |
||
| 5 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-full.php'; |
||
| 6 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-defaults.php'; |
||
| 7 | |||
| 8 | class Jetpack_Sync_Client { |
||
| 9 | |||
| 10 | const CONSTANTS_CHECKSUM_OPTION_NAME = 'jetpack_constants_sync_checksum'; |
||
| 11 | const CALLABLES_CHECKSUM_OPTION_NAME = 'jetpack_callables_sync_checksum'; |
||
| 12 | const SYNC_THROTTLE_OPTION_NAME = 'jetpack_sync_min_wait'; |
||
| 13 | const LAST_SYNC_TIME_OPTION_NAME = 'jetpack_last_sync_time'; |
||
| 14 | const CALLABLES_AWAIT_TRANSIENT_NAME = 'jetpack_sync_callables_await'; |
||
| 15 | const CONSTANTS_AWAIT_TRANSIENT_NAME = 'jetpack_sync_constants_await'; |
||
| 16 | |||
| 17 | private $checkout_memory_size; |
||
| 18 | private $upload_max_bytes; |
||
| 19 | private $upload_max_rows; |
||
| 20 | private $sync_queue; |
||
| 21 | private $full_sync_client; |
||
| 22 | private $codec; |
||
| 23 | private $options_whitelist; |
||
| 24 | private $constants_whitelist; |
||
| 25 | private $meta_types = array( 'post', 'comment' ); |
||
| 26 | private $callable_whitelist; |
||
| 27 | private $network_options_whitelist; |
||
| 28 | private $taxonomy_whitelist; |
||
| 29 | private $is_multisite; |
||
| 30 | |||
| 31 | // singleton functions |
||
| 32 | private static $instance; |
||
| 33 | |||
| 34 | public static function getInstance() { |
||
| 35 | if ( null === self::$instance ) { |
||
| 36 | self::$instance = new self(); |
||
| 37 | } |
||
| 38 | |||
| 39 | return self::$instance; |
||
| 40 | } |
||
| 41 | |||
| 42 | // this is necessary because you can't use "new" when you declare instance properties >:( |
||
| 43 | protected function __construct() { |
||
| 44 | $this->set_defaults(); |
||
| 45 | $this->init(); |
||
| 46 | } |
||
| 47 | |||
| 48 | private function init() { |
||
| 49 | |||
| 50 | $handler = array( $this, 'action_handler' ); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Most of the following hooks are sent to the same $handler |
||
| 54 | * for immediate serialization and queuing be sent to the server. |
||
| 55 | * The only exceptions are actions which need additional processing. |
||
| 56 | */ |
||
| 57 | |||
| 58 | // constants |
||
| 59 | add_action( 'jetpack_sync_constant', $handler, 10, 2 ); |
||
| 60 | |||
| 61 | // callables |
||
| 62 | add_action( 'jetpack_sync_callable', $handler, 10, 2 ); |
||
| 63 | |||
| 64 | // posts |
||
| 65 | add_action( 'wp_insert_post', $handler, 10, 3 ); |
||
| 66 | add_action( 'deleted_post', $handler, 10 ); |
||
| 67 | add_filter( 'jetpack_sync_before_send_wp_insert_post', array( $this, 'expand_wp_insert_post' ) ); |
||
| 68 | |||
| 69 | add_action( 'jetpack_publicize_post', $handler ); |
||
| 70 | |||
| 71 | // attachments |
||
| 72 | |||
| 73 | add_action( 'edit_attachment', array( $this, 'send_attachment_info' ) ); |
||
| 74 | // Once we don't have to support 4.3 we can start using add_action( 'attachment_updated', $handler, 10, 3 ); instead |
||
| 75 | add_action( 'add_attachment', array( $this, 'send_attachment_info' ) ); |
||
| 76 | add_action( 'jetpack_sync_save_add_attachment', $handler, 10, 2 ); |
||
| 77 | |||
| 78 | // comments |
||
| 79 | add_action( 'wp_insert_comment', $handler, 10, 2 ); |
||
| 80 | add_action( 'deleted_comment', $handler, 10 ); |
||
| 81 | add_action( 'trashed_comment', $handler, 10 ); |
||
| 82 | add_action( 'spammed_comment', $handler, 10 ); |
||
| 83 | |||
| 84 | // even though it's messy, we implement these hooks because |
||
| 85 | // the edit_comment hook doesn't include the data |
||
| 86 | // so this saves us a DB read for every comment event |
||
| 87 | foreach ( array( '', 'trackback', 'pingback' ) as $comment_type ) { |
||
| 88 | foreach ( array( 'unapproved', 'approved' ) as $comment_status ) { |
||
| 89 | add_action( "comment_{$comment_status}_{$comment_type}", $handler, 10, 2 ); |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | // options |
||
| 94 | add_action( 'added_option', $handler, 10, 2 ); |
||
| 95 | add_action( 'updated_option', $handler, 10, 3 ); |
||
| 96 | add_action( 'deleted_option', $handler, 10, 1 ); |
||
| 97 | |||
| 98 | // Sync Core Icon: Detect changes in Core's Site Icon and make it syncable. |
||
| 99 | add_action( 'add_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) ); |
||
| 100 | add_action( 'update_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) ); |
||
| 101 | add_action( 'delete_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) ); |
||
| 102 | |||
| 103 | // wordpress version |
||
| 104 | add_action( 'upgrader_process_complete', array( $this, 'send_wp_version' ), 10, 2 ); |
||
| 105 | add_action( 'jetpack_sync_wp_version', $handler ); |
||
| 106 | |||
| 107 | // themes |
||
| 108 | add_action( 'switch_theme', array( $this, 'send_theme_info' ) ); |
||
| 109 | add_action( 'jetpack_sync_current_theme_support', $handler, 10 ); // custom hook, see meta-hooks below |
||
| 110 | |||
| 111 | // post-meta, and in the future - other meta? |
||
| 112 | foreach ( $this->meta_types as $meta_type ) { |
||
| 113 | add_action( "added_{$meta_type}_meta", $handler, 10, 4 ); |
||
| 114 | add_action( "updated_{$meta_type}_meta", $handler, 10, 4 ); |
||
| 115 | add_action( "deleted_{$meta_type}_meta", $handler, 10, 4 ); |
||
| 116 | } |
||
| 117 | |||
| 118 | // terms |
||
| 119 | add_action( 'created_term', array( $this, 'save_term_handler' ), 10, 3 ); |
||
| 120 | add_action( 'edited_term', array( $this, 'save_term_handler' ), 10, 3 ); |
||
| 121 | add_action( 'jetpack_sync_save_term', $handler, 10, 4 ); |
||
| 122 | add_action( 'delete_term', $handler, 10, 4 ); |
||
| 123 | add_action( 'set_object_terms', $handler, 10, 6 ); |
||
| 124 | add_action( 'deleted_term_relationships', $handler, 10, 2 ); |
||
| 125 | |||
| 126 | // users |
||
| 127 | add_action( 'user_register', array( $this, 'save_user_handler' ) ); |
||
| 128 | add_action( 'profile_update', array( $this, 'save_user_handler' ), 10, 2 ); |
||
| 129 | add_action( 'jetpack_sync_save_user', $handler, 10, 2 ); |
||
| 130 | add_action( 'deleted_user', $handler, 10, 2 ); |
||
| 131 | |||
| 132 | // user roles |
||
| 133 | add_action( 'add_user_role', array( $this, 'save_user_role_handler' ), 10, 2 ); |
||
| 134 | add_action( 'set_user_role', array( $this, 'save_user_role_handler' ), 10, 3 ); |
||
| 135 | add_action( 'remove_user_role', array( $this, 'save_user_role_handler' ), 10, 2 ); |
||
| 136 | |||
| 137 | |||
| 138 | // user capabilities |
||
| 139 | add_action( 'added_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 ); |
||
| 140 | add_action( 'updated_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 ); |
||
| 141 | add_action( 'deleted_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 ); |
||
| 142 | |||
| 143 | // themes |
||
| 144 | add_action( 'set_site_transient_update_plugins', $handler, 10, 1 ); |
||
| 145 | add_action( 'set_site_transient_update_themes', $handler, 10, 1 ); |
||
| 146 | add_action( 'set_site_transient_update_core', $handler, 10, 1 ); |
||
| 147 | |||
| 148 | // multi site network options |
||
| 149 | if ( $this->is_multisite ) { |
||
| 150 | add_action( 'add_site_option', $handler, 10, 2 ); |
||
| 151 | add_action( 'update_site_option', $handler, 10, 3 ); |
||
| 152 | add_action( 'delete_site_option', $handler, 10, 1 ); |
||
| 153 | } |
||
| 154 | |||
| 155 | // synthetic actions for full sync |
||
| 156 | add_action( 'jetpack_full_sync_start', $handler ); |
||
| 157 | add_action( 'jetpack_full_sync_end', $handler ); |
||
| 158 | add_action( 'jetpack_full_sync_options', $handler ); |
||
| 159 | add_action( 'jetpack_full_sync_posts', $handler ); // also sends post meta |
||
| 160 | add_action( 'jetpack_full_sync_comments', $handler ); // also send comments meta |
||
| 161 | add_action( 'jetpack_full_sync_users', $handler ); |
||
| 162 | add_action( 'jetpack_full_sync_terms', $handler, 10, 2 ); |
||
| 163 | if ( is_multisite() ) { |
||
| 164 | add_action( 'jetpack_full_sync_network_options', $handler ); |
||
| 165 | } |
||
| 166 | |||
| 167 | |||
| 168 | // TODO: Callables, Constanst, Network Options, Users, Terms |
||
|
0 ignored issues
–
show
Coding Style
Best Practice
introduced
by
Loading history...
|
|||
| 169 | |||
| 170 | /** |
||
| 171 | * Sync all pending actions with server |
||
| 172 | */ |
||
| 173 | add_action( 'jetpack_sync_actions', array( $this, 'do_sync' ) ); |
||
| 174 | } |
||
| 175 | |||
| 176 | // TODO: Refactor to use one set whitelist function, with one is_whitelisted. |
||
|
0 ignored issues
–
show
|
|||
| 177 | function set_options_whitelist( $options ) { |
||
| 178 | $this->options_whitelist = $options; |
||
| 179 | } |
||
| 180 | |||
| 181 | function get_options_whitelist() { |
||
| 182 | return $this->options_whitelist; |
||
| 183 | } |
||
| 184 | |||
| 185 | function set_constants_whitelist( $constants ) { |
||
| 186 | $this->constants_whitelist = $constants; |
||
| 187 | } |
||
| 188 | |||
| 189 | function get_callable_whitelist() { |
||
| 190 | return $this->callable_whitelist; |
||
| 191 | } |
||
| 192 | |||
| 193 | function set_callable_whitelist( $callables ) { |
||
| 194 | $this->callable_whitelist = $callables; |
||
| 195 | } |
||
| 196 | |||
| 197 | function set_network_options_whitelist( $options ) { |
||
| 198 | $this->network_options_whitelist = $options; |
||
| 199 | } |
||
| 200 | |||
| 201 | function set_send_buffer_memory_size( $size ) { |
||
| 202 | $this->checkout_memory_size = $size; |
||
| 203 | } |
||
| 204 | |||
| 205 | // in bytes |
||
| 206 | function set_upload_max_bytes( $max_bytes ) { |
||
| 207 | $this->upload_max_bytes = $max_bytes; |
||
| 208 | } |
||
| 209 | |||
| 210 | // in rows |
||
| 211 | function set_upload_max_rows( $max_rows ) { |
||
| 212 | $this->upload_max_rows = $max_rows; |
||
| 213 | } |
||
| 214 | |||
| 215 | // in seconds |
||
| 216 | function set_min_sync_wait_time( $seconds ) { |
||
| 217 | update_option( self::SYNC_THROTTLE_OPTION_NAME, $seconds, true ); |
||
| 218 | } |
||
| 219 | |||
| 220 | function get_min_sync_wait_time() { |
||
| 221 | return get_option( self::SYNC_THROTTLE_OPTION_NAME ); |
||
| 222 | } |
||
| 223 | |||
| 224 | private function get_last_sync_time() { |
||
| 225 | return (double) get_option( self::LAST_SYNC_TIME_OPTION_NAME ); |
||
| 226 | } |
||
| 227 | |||
| 228 | private function set_last_sync_time() { |
||
| 229 | return update_option( self::LAST_SYNC_TIME_OPTION_NAME, microtime( true ), true ); |
||
| 230 | } |
||
| 231 | |||
| 232 | function set_taxonomy_whitelist( $taxonomies ) { |
||
| 233 | $this->taxonomy_whitelist = $taxonomies; |
||
| 234 | } |
||
| 235 | |||
| 236 | function is_whitelisted_option( $option ) { |
||
| 237 | return in_array( $option, $this->options_whitelist ); |
||
| 238 | } |
||
| 239 | |||
| 240 | function is_whitelisted_network_option( $option ) { |
||
| 241 | return $this->is_multisite && in_array( $option, $this->network_options_whitelist ); |
||
| 242 | } |
||
| 243 | |||
| 244 | function set_codec( iJetpack_Sync_Codec $codec ) { |
||
| 245 | $this->codec = $codec; |
||
| 246 | } |
||
| 247 | |||
| 248 | function set_full_sync_client( $full_sync_client ) { |
||
| 249 | if ( $this->full_sync_client ) { |
||
| 250 | remove_action( 'jetpack_sync_full', array( $this->full_sync_client, 'start' ) ); |
||
| 251 | } |
||
| 252 | |||
| 253 | $this->full_sync_client = $full_sync_client; |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Sync all objects in the database with the server |
||
| 257 | */ |
||
| 258 | add_action( 'jetpack_sync_full', array( $this->full_sync_client, 'start' ) ); |
||
| 259 | } |
||
| 260 | |||
| 261 | function get_full_sync_client() { |
||
| 262 | return $this->full_sync_client; |
||
| 263 | } |
||
| 264 | |||
| 265 | function action_handler() { |
||
| 266 | // TODO: it's really silly to have this function here - it should be |
||
|
0 ignored issues
–
show
|
|||
| 267 | // wherever we initialize the action listeners or we're just wasting cycles |
||
| 268 | if ( Jetpack::is_development_mode() || Jetpack::is_staging_site() ) { |
||
| 269 | return false; |
||
| 270 | } |
||
| 271 | |||
| 272 | $current_filter = current_filter(); |
||
| 273 | $args = func_get_args(); |
||
| 274 | |||
| 275 | if ( $current_filter === 'wp_insert_post' && $args[1]->post_type === 'revision' ) { |
||
| 276 | return; |
||
| 277 | } |
||
| 278 | |||
| 279 | if ( in_array( $current_filter, array( 'deleted_option', 'added_option', 'updated_option' ) ) |
||
| 280 | && |
||
| 281 | ! $this->is_whitelisted_option( $args[0] ) |
||
| 282 | ) { |
||
| 283 | return; |
||
| 284 | } |
||
| 285 | |||
| 286 | if ( in_array( $current_filter, array( 'delete_site_option', 'add_site_option', 'update_site_option' ) ) |
||
| 287 | && |
||
| 288 | ! $this->is_whitelisted_network_option( $args[0] ) |
||
| 289 | ) { |
||
| 290 | return; |
||
| 291 | } |
||
| 292 | |||
| 293 | // don't sync private meta |
||
| 294 | if ( preg_match( '/^(added|updated|deleted)_.*_meta$/', $current_filter ) |
||
| 295 | && $args[2][0] === '_' |
||
| 296 | && ! in_array( $args[2], Jetpack_Sync_Defaults::$default_whitelist_meta_keys ) |
||
|
0 ignored issues
–
show
The property
default_whitelist_meta_keys 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...
|
|||
| 297 | ) { |
||
| 298 | return; |
||
| 299 | } |
||
| 300 | |||
| 301 | $this->sync_queue->add( array( |
||
| 302 | $current_filter, |
||
| 303 | $args, |
||
| 304 | get_current_user_id(), |
||
| 305 | microtime( true ) |
||
| 306 | ) ); |
||
| 307 | } |
||
| 308 | |||
| 309 | function send_theme_info() { |
||
| 310 | global $_wp_theme_features; |
||
| 311 | |||
| 312 | $theme_support = array(); |
||
| 313 | |||
| 314 | foreach ( Jetpack_Sync_Defaults::$default_theme_support_whitelist as $theme_feature ) { |
||
|
0 ignored issues
–
show
The property
default_theme_support_whitelist 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...
|
|||
| 315 | $has_support = current_theme_supports( $theme_feature ); |
||
| 316 | if ( $has_support ) { |
||
| 317 | $theme_support[ $theme_feature ] = $_wp_theme_features[ $theme_feature ]; |
||
| 318 | } |
||
| 319 | |||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Fires when the client needs to sync theme support info |
||
| 324 | * Only sends theme support attributes whitelisted in Jetpack_Sync_Defaults::$default_theme_support_whitelist |
||
| 325 | * |
||
| 326 | * @since 4.1.0 |
||
| 327 | * |
||
| 328 | * @param object the theme support hash |
||
| 329 | */ |
||
| 330 | do_action( 'jetpack_sync_current_theme_support', $theme_support ); |
||
| 331 | } |
||
| 332 | |||
| 333 | function send_wp_version( $update, $meta_data ) { |
||
| 334 | if ( 'update' === $meta_data['action'] && 'core' === $meta_data['type'] ) { |
||
| 335 | global $wp_version; |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Fires when the client needs to sync WordPress version |
||
| 339 | * |
||
| 340 | * @since 4.1.0 |
||
| 341 | * |
||
| 342 | * @param string The WordPress version number |
||
| 343 | */ |
||
| 344 | do_action( 'jetpack_sync_wp_version', $wp_version ); |
||
| 345 | } |
||
| 346 | } |
||
| 347 | |||
| 348 | function save_term_handler( $term_id, $tt_id, $taxonomy ) { |
||
| 349 | if ( class_exists( 'WP_Term' ) ) { |
||
| 350 | $term_object = WP_Term::get_instance( $term_id, $taxonomy ); |
||
| 351 | } else { |
||
| 352 | $term_object = get_term_by( 'id', $term_id, $taxonomy ); |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Fires when the client needs to sync a new term |
||
| 357 | * |
||
| 358 | * @since 4.1.0 |
||
| 359 | * |
||
| 360 | * @param object the Term object |
||
| 361 | */ |
||
| 362 | do_action( 'jetpack_sync_save_term', $term_object ); |
||
| 363 | } |
||
| 364 | |||
| 365 | function send_attachment_info( $attachment_id ) { |
||
| 366 | $attachment = get_post( $attachment_id ); |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Fires when the client needs to sync an attachment for a post |
||
| 370 | * |
||
| 371 | * @since 4.1.0 |
||
| 372 | * |
||
| 373 | * @param int The attachment ID |
||
| 374 | * @param object The attachment |
||
| 375 | */ |
||
| 376 | do_action( 'jetpack_sync_save_add_attachment', $attachment_id, $attachment ); |
||
| 377 | } |
||
| 378 | |||
| 379 | function save_user_handler( $user_id, $old_user_data = null ) { |
||
| 380 | $user = $this->sanitize_user( get_user_by( 'id', $user_id ) ); |
||
| 381 | |||
| 382 | // Older versions of WP don't pass the old_user_data in ->data |
||
| 383 | if ( isset( $old_user_data->data ) ) { |
||
| 384 | $old_user = $old_user_data->data; |
||
| 385 | } else { |
||
| 386 | $old_user = $old_user_data; |
||
| 387 | } |
||
| 388 | |||
| 389 | if ( $old_user !== null ) { |
||
| 390 | unset( $old_user->user_pass ); |
||
| 391 | if ( serialize( $old_user ) === serialize( $user->data ) ) { |
||
| 392 | return; |
||
| 393 | } |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Fires when the client needs to sync an updated user |
||
| 398 | * |
||
| 399 | * @since 4.1.0 |
||
| 400 | * |
||
| 401 | * @param object The WP_User object |
||
| 402 | */ |
||
| 403 | do_action( 'jetpack_sync_save_user', $user ); |
||
| 404 | } |
||
| 405 | |||
| 406 | function save_user_role_handler( $user_id, $role, $old_roles = null ) { |
||
|
0 ignored issues
–
show
|
|||
| 407 | $user = $this->sanitize_user( get_user_by( 'id', $user_id ) ); |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Fires when the client needs to sync an updated user |
||
| 411 | * |
||
| 412 | * @since 4.1.0 |
||
| 413 | * |
||
| 414 | * @param object The WP_User object |
||
| 415 | */ |
||
| 416 | do_action( 'jetpack_sync_save_user', $user ); |
||
| 417 | } |
||
| 418 | |||
| 419 | function save_user_cap_handler( $meta_id, $user_id, $meta_key, $capabilities ) { |
||
|
0 ignored issues
–
show
|
|||
| 420 | $user = $this->sanitize_user( get_user_by( 'id', $user_id ) ); |
||
| 421 | if ( $meta_key === $user->cap_key ) { |
||
| 422 | /** |
||
| 423 | * Fires when the client needs to sync an updated user |
||
| 424 | * |
||
| 425 | * @since 4.1.0 |
||
| 426 | * |
||
| 427 | * @param object The WP_User object |
||
| 428 | */ |
||
| 429 | do_action( 'jetpack_sync_save_user', $user ); |
||
| 430 | } |
||
| 431 | } |
||
| 432 | |||
| 433 | public function sanitize_user( $user ) { |
||
| 434 | unset( $user->data->user_pass ); |
||
| 435 | |||
| 436 | return $user; |
||
| 437 | } |
||
| 438 | |||
| 439 | |||
| 440 | function do_sync() { |
||
| 441 | // don't sync if importing |
||
| 442 | if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { |
||
| 443 | $this->schedule_sync( "+1 minute" ); |
||
| 444 | |||
| 445 | return false; |
||
| 446 | } |
||
| 447 | |||
| 448 | // don't sync if we are throttled |
||
| 449 | $sync_wait = $this->get_min_sync_wait_time(); |
||
| 450 | $last_sync = $this->get_last_sync_time(); |
||
| 451 | |||
| 452 | if ( $last_sync && $sync_wait && $last_sync + $sync_wait > microtime( true ) ) { |
||
| 453 | return false; |
||
| 454 | } |
||
| 455 | |||
| 456 | $this->set_last_sync_time(); |
||
| 457 | $this->maybe_sync_constants(); |
||
| 458 | $this->maybe_sync_callables(); |
||
| 459 | |||
| 460 | if ( $this->sync_queue->size() === 0 ) { |
||
| 461 | return false; |
||
| 462 | } |
||
| 463 | |||
| 464 | $buffer = $this->sync_queue->checkout_with_memory_limit( $this->checkout_memory_size, $this->upload_max_rows ); |
||
| 465 | |||
| 466 | if ( ! $buffer ) { |
||
| 467 | // buffer has no items |
||
| 468 | return false; |
||
| 469 | } |
||
| 470 | |||
| 471 | if ( is_wp_error( $buffer ) ) { |
||
| 472 | // another buffer is currently sending |
||
| 473 | return false; |
||
| 474 | } |
||
| 475 | |||
| 476 | $upload_size = 0; |
||
| 477 | $items_to_send = array(); |
||
| 478 | |||
| 479 | // we estimate the total encoded size as we go by encoding each item individually |
||
| 480 | // this is expensive, but the only way to really know :/ |
||
| 481 | foreach ( $buffer->get_items() as $key => $item ) { |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Modify the data within an action before it is serialized and sent to the server |
||
| 485 | * For example, during full sync this expands Post ID's into full Post objects, |
||
| 486 | * so that we don't have to serialize the whole object into the queue. |
||
| 487 | * |
||
| 488 | * @since 4.1.0 |
||
| 489 | * |
||
| 490 | * @param array The action parameters |
||
| 491 | */ |
||
| 492 | $item[1] = apply_filters( "jetpack_sync_before_send_" . $item[0], $item[1] ); |
||
| 493 | |||
| 494 | $encoded_item = $this->codec->encode( $item ); |
||
| 495 | $upload_size += strlen( $encoded_item ); |
||
| 496 | |||
| 497 | if ( $upload_size > $this->upload_max_bytes && count( $items_to_send ) > 0 ) { |
||
| 498 | break; |
||
| 499 | } |
||
| 500 | |||
| 501 | $items_to_send[ $key ] = $encoded_item; |
||
| 502 | } |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Fires when data is ready to send to the server. |
||
| 506 | * Return false or WP_Error to abort the sync (e.g. if there's an error) |
||
| 507 | * The items will be automatically re-sent later |
||
| 508 | * |
||
| 509 | * @since 4.1 |
||
| 510 | * |
||
| 511 | * @param array $data The action buffer |
||
| 512 | */ |
||
| 513 | $result = apply_filters( 'jetpack_sync_client_send_data', $items_to_send ); |
||
| 514 | |||
| 515 | if ( ! $result || is_wp_error( $result ) ) { |
||
| 516 | // error_log("There was an error sending data:"); |
||
| 517 | // error_log(print_r($result, 1)); |
||
| 518 | $result = $this->sync_queue->checkin( $buffer ); |
||
| 519 | |||
| 520 | if ( is_wp_error( $result ) ) { |
||
| 521 | error_log( "Error checking in buffer: " . $result->get_error_message() ); |
||
| 522 | $this->sync_queue->force_checkin(); |
||
| 523 | } |
||
| 524 | // try again in 1 minute |
||
| 525 | $this->schedule_sync( "+1 minute" ); |
||
| 526 | } else { |
||
| 527 | |||
| 528 | // scan the sent data to see if a full sync started or finished |
||
| 529 | if ( $this->buffer_includes_action( $buffer, 'jetpack_full_sync_start' ) ) { |
||
| 530 | $this->full_sync_client->set_status_sending_started(); |
||
| 531 | } |
||
| 532 | |||
| 533 | if ( $this->buffer_includes_action( $buffer, 'jetpack_full_sync_end' ) ) { |
||
| 534 | $this->full_sync_client->set_status_sending_finished(); |
||
| 535 | } |
||
| 536 | |||
| 537 | $this->sync_queue->close( $buffer, $result ); |
||
| 538 | // check if there are any more events in the buffer |
||
| 539 | // if so, schedule a cron job to happen soon |
||
| 540 | if ( $this->sync_queue->has_any_items() ) { |
||
| 541 | $this->schedule_sync( "+1 minute" ); |
||
| 542 | } |
||
| 543 | } |
||
| 544 | } |
||
| 545 | |||
| 546 | private function buffer_includes_action( $buffer, $action_name ) { |
||
| 547 | foreach ( $buffer->get_items() as $item ) { |
||
| 548 | if ( $item[0] === $action_name ) { |
||
| 549 | return true; |
||
| 550 | } |
||
| 551 | } |
||
| 552 | |||
| 553 | return false; |
||
| 554 | } |
||
| 555 | |||
| 556 | function expand_wp_insert_post( $args ) { |
||
| 557 | // list( $post_ID, $post, $update ) = $args; |
||
| 558 | return array( $args[0], $this->filter_post_content_and_add_links( $args[1] ), $args[2] ); |
||
| 559 | } |
||
| 560 | |||
| 561 | // Expands wp_insert_post to include filtered content |
||
| 562 | function filter_post_content_and_add_links( $post ) { |
||
| 563 | if ( 0 < strlen( $post->post_password ) ) { |
||
| 564 | $post->post_password = 'auto-' . wp_generate_password( 10, false ); |
||
| 565 | } |
||
| 566 | /** This filter is already documented in core. wp-includes/post-template.php */ |
||
| 567 | $post->post_content_filtered = apply_filters( 'the_content', $post->post_content ); |
||
| 568 | $post->permalink = get_permalink( $post->ID ); |
||
| 569 | $post->shortlink = wp_get_shortlink( $post->ID ); |
||
| 570 | |||
| 571 | // legacy fields until we fully sync users |
||
| 572 | $extra = array(); |
||
| 573 | $extra['author_email'] = get_the_author_meta( 'email', $post->post_author ); |
||
| 574 | $extra['author_display_name'] = get_the_author_meta( 'display_name', $post->post_author ); |
||
| 575 | $extra['dont_email_post_to_subs'] = get_post_meta( $post->ID, '_jetpack_dont_email_post_to_subs', true ); |
||
| 576 | $post->extra = $extra; |
||
| 577 | |||
| 578 | return $post; |
||
| 579 | } |
||
| 580 | |||
| 581 | private function schedule_sync( $when ) { |
||
| 582 | wp_schedule_single_event( strtotime( $when ), 'jetpack_sync_actions' ); |
||
| 583 | } |
||
| 584 | |||
| 585 | function force_sync_constants() { |
||
| 586 | foreach ( $this->constants_whitelist as $name ) { |
||
| 587 | delete_option( self::CONSTANTS_CHECKSUM_OPTION_NAME . "_$name" ); |
||
| 588 | } |
||
| 589 | |||
| 590 | delete_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME ); |
||
| 591 | $this->maybe_sync_constants(); |
||
| 592 | } |
||
| 593 | |||
| 594 | function force_sync_options() { |
||
| 595 | /** |
||
| 596 | * Tells the client to sync all options to the server |
||
| 597 | * |
||
| 598 | * @since 4.1 |
||
| 599 | * |
||
| 600 | * @param boolean Whether to expand options (should always be true) |
||
| 601 | */ |
||
| 602 | do_action( 'jetpack_full_sync_options', true ); |
||
| 603 | } |
||
| 604 | |||
| 605 | function force_sync_network_options() { |
||
| 606 | /** |
||
| 607 | * Tells the client to sync all network options to the server |
||
| 608 | * |
||
| 609 | * @since 4.1 |
||
| 610 | * |
||
| 611 | * @param boolean Whether to expand options (should always be true) |
||
| 612 | */ |
||
| 613 | do_action( 'jetpack_full_sync_network_options', true ); |
||
| 614 | } |
||
| 615 | |||
| 616 | View Code Duplication | private function maybe_sync_constants() { |
|
| 617 | if ( get_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME ) ) { |
||
| 618 | return; |
||
| 619 | } |
||
| 620 | |||
| 621 | $constants = $this->get_all_constants(); |
||
| 622 | if ( empty( $constants ) ) { |
||
| 623 | return; |
||
| 624 | } |
||
| 625 | |||
| 626 | set_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME, microtime( true ), Jetpack_Sync_Defaults::$default_sync_constants_wait_time ); |
||
|
0 ignored issues
–
show
The property
default_sync_constants_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...
|
|||
| 627 | |||
| 628 | // only send the constants that have changed |
||
| 629 | foreach ( $constants as $name => $value ) { |
||
| 630 | $checksum = $this->get_check_sum( $value ); |
||
| 631 | |||
| 632 | // explicitly not using Identical comparison as get_option returns a string |
||
| 633 | if ( $checksum != get_option( self::CONSTANTS_CHECKSUM_OPTION_NAME . "_$name" ) ) { |
||
| 634 | /** |
||
| 635 | * Tells the client to sync a constant to the server |
||
| 636 | * |
||
| 637 | * @since 4.1 |
||
| 638 | * |
||
| 639 | * @param string The name of the constant |
||
| 640 | * @param mixed The value of the constant |
||
| 641 | */ |
||
| 642 | do_action( 'jetpack_sync_constant', $name, $value ); |
||
| 643 | update_option( self::CONSTANTS_CHECKSUM_OPTION_NAME . "_$name", $checksum ); |
||
| 644 | } |
||
| 645 | } |
||
| 646 | } |
||
| 647 | |||
| 648 | private function get_all_constants() { |
||
| 649 | return array_combine( |
||
| 650 | $this->constants_whitelist, |
||
| 651 | array_map( array( $this, 'get_constant' ), $this->constants_whitelist ) |
||
| 652 | ); |
||
| 653 | } |
||
| 654 | |||
| 655 | private function get_constant( $constant ) { |
||
| 656 | if ( defined( $constant ) ) { |
||
| 657 | return constant( $constant ); |
||
| 658 | } |
||
| 659 | |||
| 660 | return null; |
||
| 661 | } |
||
| 662 | |||
| 663 | public function force_sync_callables() { |
||
| 664 | foreach ( $this->callable_whitelist as $name => $config ) { |
||
| 665 | delete_option( self::CALLABLES_CHECKSUM_OPTION_NAME . "_$name" ); |
||
| 666 | } |
||
| 667 | |||
| 668 | delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ); |
||
| 669 | $this->maybe_sync_callables(); |
||
| 670 | } |
||
| 671 | |||
| 672 | View Code Duplication | private function maybe_sync_callables() { |
|
| 673 | if ( get_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ) ) { |
||
| 674 | return; |
||
| 675 | } |
||
| 676 | |||
| 677 | $callables = $this->get_all_callables(); |
||
| 678 | if ( empty( $callables ) ) { |
||
| 679 | return; |
||
| 680 | } |
||
| 681 | |||
| 682 | set_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME, microtime( true ), Jetpack_Sync_Defaults::$default_sync_callables_wait_time ); |
||
|
0 ignored issues
–
show
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...
|
|||
| 683 | |||
| 684 | // only send the callables that have changed |
||
| 685 | foreach ( $callables as $name => $value ) { |
||
| 686 | $checksum = $this->get_check_sum( $value ); |
||
| 687 | // explicitly not using Identical comparison as get_option returns a string |
||
| 688 | if ( $checksum != get_option( self::CALLABLES_CHECKSUM_OPTION_NAME . "_$name" ) ) { |
||
| 689 | /** |
||
| 690 | * Tells the client to sync a callable (aka function) to the server |
||
| 691 | * |
||
| 692 | * @since 4.1 |
||
| 693 | * |
||
| 694 | * @param string The name of the callable |
||
| 695 | * @param mixed The value of the callable |
||
| 696 | */ |
||
| 697 | do_action( 'jetpack_sync_callable', $name, $value ); |
||
| 698 | update_option( self::CALLABLES_CHECKSUM_OPTION_NAME . "_$name", $checksum ); |
||
| 699 | } |
||
| 700 | } |
||
| 701 | } |
||
| 702 | |||
| 703 | private function get_all_callables() { |
||
| 704 | return array_combine( |
||
| 705 | array_keys( $this->callable_whitelist ), |
||
| 706 | array_map( array( $this, 'get_callable' ), array_values( $this->callable_whitelist ) ) |
||
| 707 | ); |
||
| 708 | } |
||
| 709 | |||
| 710 | private function get_callable( $callable ) { |
||
| 711 | return call_user_func( $callable ); |
||
| 712 | } |
||
| 713 | |||
| 714 | // Is public so that we don't have to store so much data all the options twice. |
||
| 715 | function get_all_options() { |
||
| 716 | $options = array(); |
||
| 717 | foreach ( $this->options_whitelist as $option ) { |
||
| 718 | $options[ $option ] = get_option( $option ); |
||
| 719 | } |
||
| 720 | |||
| 721 | return $options; |
||
| 722 | } |
||
| 723 | |||
| 724 | function get_all_network_options() { |
||
| 725 | $options = array(); |
||
| 726 | foreach ( $this->network_options_whitelist as $option ) { |
||
| 727 | $options[ $option ] = get_site_option( $option ); |
||
| 728 | } |
||
| 729 | |||
| 730 | return $options; |
||
| 731 | } |
||
| 732 | |||
| 733 | private function get_check_sum( $values ) { |
||
| 734 | return crc32( json_encode( $values ) ); |
||
| 735 | } |
||
| 736 | |||
| 737 | View Code Duplication | function jetpack_sync_core_icon() { |
|
| 738 | if ( function_exists( 'get_site_icon_url' ) ) { |
||
| 739 | $url = get_site_icon_url(); |
||
| 740 | } else { |
||
| 741 | return; |
||
| 742 | } |
||
| 743 | |||
| 744 | require_once( JETPACK__PLUGIN_DIR . 'modules/site-icon/site-icon-functions.php' ); |
||
| 745 | // If there's a core icon, maybe update the option. If not, fall back to Jetpack's. |
||
| 746 | if ( ! empty( $url ) && $url !== jetpack_site_icon_url() ) { |
||
| 747 | // This is the option that is synced with dotcom |
||
| 748 | Jetpack_Options::update_option( 'site_icon_url', $url ); |
||
| 749 | } else if ( empty( $url ) && did_action( 'delete_option_site_icon' ) ) { |
||
| 750 | Jetpack_Options::delete_option( 'site_icon_url' ); |
||
| 751 | } |
||
| 752 | } |
||
| 753 | |||
| 754 | function get_sync_queue() { |
||
| 755 | return $this->sync_queue; |
||
| 756 | } |
||
| 757 | |||
| 758 | function reset_sync_queue() { |
||
| 759 | $this->sync_queue->reset(); |
||
| 760 | } |
||
| 761 | |||
| 762 | function set_defaults() { |
||
| 763 | $this->sync_queue = new Jetpack_Sync_Queue( 'sync' ); |
||
| 764 | $this->set_send_buffer_memory_size( Jetpack_Sync_Defaults::$default_send_buffer_memory_size ); |
||
|
0 ignored issues
–
show
The property
default_send_buffer_memory_size 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...
|
|||
| 765 | $this->set_upload_max_bytes( Jetpack_Sync_Defaults::$default_upload_max_bytes ); |
||
|
0 ignored issues
–
show
The property
default_upload_max_bytes 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...
|
|||
| 766 | $this->set_upload_max_rows( Jetpack_Sync_Defaults::$default_upload_max_rows ); |
||
|
0 ignored issues
–
show
The property
default_upload_max_rows 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...
|
|||
| 767 | |||
| 768 | if ( $this->get_min_sync_wait_time() === false ) { |
||
| 769 | $this->set_min_sync_wait_time( Jetpack_Sync_Defaults::$default_sync_wait_time ); |
||
|
0 ignored issues
–
show
The property
default_sync_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...
|
|||
| 770 | } |
||
| 771 | |||
| 772 | $this->set_full_sync_client( Jetpack_Sync_Full::getInstance() ); |
||
| 773 | $this->codec = new Jetpack_Sync_Deflate_Codec(); |
||
| 774 | $this->constants_whitelist = Jetpack_Sync_Defaults::$default_constants_whitelist; |
||
|
0 ignored issues
–
show
The property
default_constants_whitelist 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...
|
|||
| 775 | /** |
||
| 776 | * This filter is already documented class.wpcom-json-api-get-option-endpoint.php |
||
| 777 | */ |
||
| 778 | $this->options_whitelist = apply_filters( 'jetpack_options_whitelist', Jetpack_Sync_Defaults::$default_options_whitelist ); |
||
|
0 ignored issues
–
show
The property
default_options_whitelist 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...
|
|||
| 779 | $this->network_options_whitelist = Jetpack_Sync_Defaults::$default_network_options_whitelist; |
||
|
0 ignored issues
–
show
The property
default_network_options_whitelist 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...
|
|||
| 780 | $this->taxonomy_whitelist = Jetpack_Sync_Defaults::$default_taxonomy_whitelist; |
||
|
0 ignored issues
–
show
The property
default_taxonomy_whitelist 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...
|
|||
| 781 | $this->is_multisite = is_multisite(); |
||
| 782 | |||
| 783 | // theme mod varies from theme to theme. |
||
| 784 | $this->options_whitelist[] = 'theme_mods_' . get_option( 'stylesheet' ); |
||
| 785 | if ( $this->is_multisite ) { |
||
| 786 | $this->callable_whitelist = array_merge( Jetpack_Sync_Defaults::$default_callable_whitelist, Jetpack_Sync_Defaults::$default_multisite_callable_whitelist ); |
||
|
0 ignored issues
–
show
The property
default_callable_whitelist 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...
The property
default_multisite_callable_whitelist 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...
|
|||
| 787 | } else { |
||
| 788 | $this->callable_whitelist = Jetpack_Sync_Defaults::$default_callable_whitelist; |
||
|
0 ignored issues
–
show
The property
default_callable_whitelist 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...
|
|||
| 789 | } |
||
| 790 | } |
||
| 791 | |||
| 792 | function reset_data() { |
||
| 793 | $this->reset_sync_queue(); |
||
| 794 | } |
||
| 795 | } |
||
| 796 |