Created
December 14, 2018 16:40
-
-
Save gthayer/c02ec118afb0370c948e911a9da19c0f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'admin_init', 'auto_distribute_cron' ); | |
add_action( 'mtm_auto_distribute_cron', 'mtm_push_nondistributed_posts' ); | |
if ( class_exists( 'WP_CLI' ) ) { | |
\WP_CLI::add_command( 'mtm_sj', 'mtm_push_nondistributed_posts' ); | |
} | |
/** | |
* Initialize the scheduled 'mtm_auto_distribute_cron' action. | |
* | |
* @return void | |
*/ | |
function auto_distribute_cron() { | |
if ( false !== wp_next_scheduled( 'mtm_auto_distribute_cron' ) ) { | |
return; | |
} | |
wp_schedule_event( time(), 'fifteen_minutes', 'mtm_auto_distribute_cron' ); | |
} | |
/** | |
* Add an option for scheduleing actions every fifteen minutes. | |
* | |
* @param array $schedules An array of available cadances for scheduled actions. | |
* @return array $schedules. | |
*/ | |
function mtm_add_fifteen_minute_schedule( $schedules ) { | |
// Add a 'fifteen_minutes' schedule to the existing set. | |
$schedules['fifteen_minutes'] = array( | |
'interval' => 60 * 15, | |
'display' => __( 'Fifteen Minutes' ), | |
); | |
return $schedules; | |
} | |
add_filter( 'cron_schedules', 'mtm_add_fifteen_minute_schedule' ); | |
/** | |
* Query all posts created within 6 hours, and attempt to Distribute them if not done so already. | |
* | |
* @return void | |
*/ | |
function mtm_push_nondistributed_posts() { | |
$date = new DateTime(); | |
$date->modify( '-6 hour' ); | |
$args = array( | |
'posts_per_page' => 500, | |
'post_type' => 'post', | |
'post_status' => 'publish', | |
'ignore_sticky_posts' => true, | |
'meta_query' => array( | |
array( | |
'key' => 'dt_connection_map', | |
'compare' => 'NOT EXISTS' | |
), | |
), | |
'date_query' => array( | |
'after' => $date->format( 'Y-m-d H:i:s' ), | |
), | |
); | |
$posts = new WP_Query( $args ); | |
if ( $posts->have_posts() ) { | |
while ( $posts->have_posts() ) { | |
$posts->the_post(); | |
auto_distribute( get_the_ID() ); | |
wp_reset_postdata(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment