/**
  * @param WP_Post $post
  * @param bool    $updateAll TRUE if the update is part of updateAll() - if so, defer the term count
  */
 public function update($post, $updateAll = false)
 {
     /** @var wpdb $wpdb */
     global $wpdb;
     $content = $post->post_content;
     /**
      * Do a quick check that there IS a recipe before we go to the expense of instantiating a DOMDocument
      */
     if (strpos($content, 'endeasyrecipe') === false) {
         return;
     }
     $dom = new EasyRecipeDocument($content);
     if (!$dom->isEasyRecipe) {
         return;
     }
     $cuisineTerms = array();
     $courseTerms = array();
     $postID = $post->ID;
     if (!$updateAll) {
         $this->countTerms['cuisine'] = array();
         $this->countTerms['course'] = array();
     }
     /**
      * Read all the current cuisine/course terms for this post
      * We need this to decide if we need to insert a new relationship and whether we need to remove any old relationships no longer used
      */
     $q = "SELECT {$wpdb->term_taxonomy}.term_taxonomy_id, taxonomy FROM {$wpdb->terms} JOIN {$wpdb->term_taxonomy} ON {$wpdb->terms}.term_id = {$wpdb->term_taxonomy}.term_id JOIN ";
     $q .= "{$wpdb->term_relationships} on {$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id ";
     $q .= "WHERE object_id = {$postID} AND taxonomy in ('course','cuisine')";
     $existing = $wpdb->get_results($q);
     $existingUnused = array();
     foreach ($existing as $exist) {
         $existingUnused[$exist->term_taxonomy_id] = true;
         $this->countTerms[$exist->taxonomy][] = $exist->term_taxonomy_id;
     }
     /**
      * Get the course(s) and cuisine(s) from the recipe
      */
     $cuisines = $dom->getElementsByClassName('cuisine');
     $courses = $dom->getElementsByClassName('type');
     if (count($cuisines) > 0) {
         /** @var DOMElement $cuisine */
         foreach ($cuisines as $cuisine) {
             $term = $cuisine->nodeValue;
             /**
              * Get the term info for the recipe's cuisine(s) - insert one of it doesn't yet exist
              */
             if (empty($cuisineTerms[$term])) {
                 $termInfo = term_exists($term, 'cuisine');
                 if (!$termInfo) {
                     $termInfo = wp_insert_term($term, 'cuisine');
                 }
                 $cuisineTerms[$term] = $termInfo['term_taxonomy_id'];
             }
             /**
              * Check to see if we already have the correct relationship for this cuisine term for this post
              */
             $ttID = $cuisineTerms[$term];
             if (!empty($existingUnused[$ttID])) {
                 $existingUnused[$ttID] = false;
                 continue;
             }
             /**
              * The relationship didn't exist so insert it
              */
             $this->countTerms['cuisine'][] = $ttID;
             $wpdb->insert($wpdb->term_relationships, array('object_id' => $postID, 'term_taxonomy_id' => $ttID));
         }
     }
     if (count($courses) > 0) {
         /** @var DOMElement $course */
         foreach ($courses as $course) {
             $term = $course->nodeValue;
             /**
              * Get the term info for the recipe's course(s) - insert one of it doesn't yet exist
              */
             if (empty($courseTerms[$term])) {
                 $termInfo = term_exists($term, 'course');
                 if (!$termInfo) {
                     $termInfo = wp_insert_term($term, 'course');
                 }
                 $courseTerms[$term] = $termInfo['term_taxonomy_id'];
             }
             /**
              * Check to see if we already have the correct relationship for this course term for this post
              */
             $ttID = $courseTerms[$term];
             if (!empty($existingUnused[$ttID])) {
                 $existingUnused[$ttID] = false;
                 continue;
             }
             /**
              * The relationship didn't exist so insert it and mark the ttID to be counted
              */
             $this->countTerms['course'][] = $ttID;
             $wpdb->insert($wpdb->term_relationships, array('object_id' => $postID, 'term_taxonomy_id' => $ttID));
         }
     }
     /**
      * Remove any existing term relationships that are now no longer used and adjust the list of terms that need to be updated
      */
     foreach ($existingUnused as $ttID => $unused) {
         if ($unused) {
             $wpdb->delete($wpdb->term_relationships, array('object_id' => $postID, 'term_taxonomy_id' => $ttID));
         } else {
             if (in_array($ttID, $this->countTerms['course'])) {
             }
         }
     }
     /**
      * Update any term counts that we may have adjusted unless this is part of an updateAll() run
      */
     if (!$updateAll) {
         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');
         }
     }
 }