Skip to content

Instantly share code, notes, and snippets.

@paulschreiber
Created February 26, 2020 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulschreiber/06478069a4fc865b759ce3a14ea3c035 to your computer and use it in GitHub Desktop.
Save paulschreiber/06478069a4fc865b759ce3a14ea3c035 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: VIP Jetpack Sync Cron
Description: This drop-in plugin ensures that Jetpack only syncs on a dedicated cron task and schedules a single cron event upon a whitelist execution with a delay of 30s.
Version: 1.0
Author: Rebecca Hum, Automattic
*/
class VIP_Jetpack_Cron_Sync {
/**
* __construct()
*/
public function __construct() {
if ( class_exists( 'Jetpack' ) ) {
add_filter( 'jetpack_sync_sender_should_load', [ $this, 'vip_jp_sync_cron_only' ], 99 );
add_action( 'jetpack_sync_action_before_enqueue', [ $this, 'vip_jp_schedule_sync_cron' ] );
register_deactivation_hook( __FILE__, [ $this, 'vip_jp_cron_sync_deactivate' ] );
}
}
/**
* Filter for JP to short circuit loading of sender unless on a cron job.
*/
public function vip_jp_sync_cron_only( $bool ) {
return $bool = defined( 'DOING_CRON' ) && DOING_CRON ? true : false;
}
/**
* Schedule a single event for JP sync on cron job.
*/
public function vip_jp_schedule_sync_cron() {
wp_schedule_single_event( time() + 30, 'vip_jp_sync_event' );
}
/**
* Do the actual JP syncing.
*/
public function vip_jp_sync_event() {
global $jetpack_sync_sender;
$jetpack_sync_sender->do_sync();
}
/**
* Register de-activation hook to remove filters.
*/
public function vip_jp_cron_sync_deactivate() {
remove_filter( 'jetpack_sync_sender_should_load', [ $this, 'vip_jp_sync_cron_only' ] );
}
}
$vip_jetpack_cron_sync = new VIP_Jetpack_Cron_Sync();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment