public static function check(EasyRecipePlusSettings $settings) { // $log = EasyRecipePlusLogger::getLog('update'); /** * If the settings haven't been updated to show that we've created taxonomies, schedule the taxonomy creation. * Do this in the background because it may take quite some time especially on underpowered shared servers (10+ secs on our dedicated test server for our moderately sized test blog) */ if (!$settings->taxonomiesCreated) { $scheduler = new EasyRecipePlusScheduler(EasyRecipePlusTaxonomies::UPDATE_TAXONOMIES); /** * If we are running in CRON, set up the hook to catch the update event when it fires * Otherwise, get the scheduler to schedule the update via cron right now * Both of these situations might occur multiple times before the taxonomy creation is complete. The scheduler will handle that */ if (defined('DOING_CRON')) { /** * If the job isn't already running, set it so but allow it to timeout after 10 mins so that if it fails, it won't be flagged as running forever * Then setup the hook to actually do the work */ if (!$scheduler->isRunning()) { self::$taxonomies = new EasyRecipePlusTaxonomies($scheduler); add_action(EasyRecipePlusTaxonomies::UPDATE_TAXONOMIES, array(self::$taxonomies, 'updateAll')); } } else { $scheduler->runNow(); } } }
/** * Update all taxonomies. * This should only ever be called from a cron job scheduled by EasyRecipePlusScheduler because it can potentially take quite a while */ function updateAll() { /** @var wpdb $wpdb */ global $wpdb; /** * If we are already running, don't do it again */ if ($this->scheduler->isRunning()) { return; } /** * Set as running * Set a "timeout" of 10 minutes. This will prevent it being re-run for 10 minutes if the current run terminates abnormally for any reason */ $this->scheduler->setRunning(10 * 60); $q = "SELECT ID FROM {$wpdb->posts} WHERE post_type NOT IN ('attachment','index','nav_menu_item')"; $postIDs = $wpdb->get_col($q); $this->countTerms['cuisine'] = array(); $this->countTerms['course'] = array(); foreach ($postIDs as $postID) { $post = WP_Post::get_instance($postID); $this->update($post, false); } /** * Update any term counts that we may have adjusted */ if (count($this->countTerms['cuisine']) > 0) { wp_update_term_count_now(array_unique(array_keys($this->countTerms['cuisine'])), 'cuisine'); } if (count($this->countTerms['course']) > 0) { wp_update_term_count_now(array_unique(array_keys($this->countTerms['course'])), 'course'); } /** * Mark the taxonomies as having been created */ $settings = EasyRecipePlusSettings::getInstance(); $settings->taxonomiesCreated = true; $settings->update(); /** * Mark this job as complete */ $this->scheduler->terminate(); }