public static function init() { //catch new posts add_filter('wp_insert_post_data', function ($data, $postarr) { if (Lift_Post_Update_Watcher::check_post_validity($data['post_status'], $data['post_type'])) { if (empty($postarr['ID'])) { //new post, so lets queue the entire post to be saved, since there is no //specific hook in wp to know it is an insert, we have to do some trickery add_action('save_post', function ($post_id, $post) { //check to make sure this isn't a revision insert $_post = $post; if (in_array(get_post_type($post_id), Lift_Post_Update_Watcher::get_watched_post_types())) { Lift_Post_Update_Watcher::queue_entire_post($post_id); remove_action('save_post', __FUNCTION__); } }, 1, 2); } } return $data; }, 10, 2); //catch updates add_action('post_updated', function ($post_id, $post_after, $post_before) { $post_status = get_post_status($post_id); $post_type = get_post_type($post_id); if (Lift_Post_Update_Watcher::check_post_validity($post_status, $post_type)) { if ($post_before->post_status == 'auto-draft') { Lift_Post_Update_Watcher::queue_entire_post($post_id); } else { foreach (Lift_Post_Update_Watcher::get_watched_post_fields($post_type) as $field_name) { if (isset($post_after->{$field_name}) && isset($post_before->{$field_name}) && $post_after->{$field_name} != $post_before->{$field_name} || (empty($post_after->{$field_name}) xor empty($post_before->{$field_name}))) { lift_queue_field_update($post_id, $field_name, 'post'); } } } } }, 10, 3); add_action('delete_post', function ($post_id) { $post_status = get_post_status($post_id); $post_type = get_post_type($post_id); if (Lift_Post_Update_Watcher::check_post_validity($post_status, $post_type)) { lift_queue_deletion($post_id, 'post'); } }, 10, 2); }
/** * used by queue_all cron job to process the queue of all posts * * @global object $wpdb */ public static function process_queue_all() { global $wpdb; $id_from = get_option(self::QUEUE_ALL_MARKER_OPTION); if (!$id_from) { wp_clear_scheduled_hook(self::QUEUE_ALL_CRON_HOOK); return; } $post_types = Lift_Search::get_indexed_post_types(); $query = new WP_Query(); $alter_query = function ($where, $wp_query) use($query, $id_from) { global $wpdb; if ($wp_query === $query) { //make sure we're not messing with any other queries //making sure all post_statii are used since wp_query overrides the requested statii $where = $wpdb->prepare(" AND {$wpdb->posts}.post_type in ('" . implode("','", $wp_query->get('post_type')) . "') " . "AND {$wpdb->posts}.ID > %d " . "AND {$wpdb->posts}.post_status <> 'auto-draft'", $id_from); } return $where; }; add_filter('posts_where', $alter_query, 10, 2); $posts = $query->query(array('suppress_filters' => false, 'post_type' => $post_types, 'orderby' => 'ID', 'order' => 'ASC', 'post_status' => array_diff(get_post_stati(), array('auto-draft')), 'posts_per_page' => self::get_queue_all_set_size())); remove_filter('posts_where', $alter_query); if (empty($posts)) { wp_clear_scheduled_hook(self::QUEUE_ALL_CRON_HOOK); delete_option(self::QUEUE_ALL_MARKER_OPTION); return; } foreach ($posts as $post) { Lift_Post_Update_Watcher::queue_entire_post($post->ID); } $new_id_from = end($posts)->ID; update_option(self::QUEUE_ALL_MARKER_OPTION, $new_id_from); }