/**
  * Loop on each syncable entities present in $_default_entity_diff_meth_ids, call the diff method associated and do the sync on each entity 
  * In case of error (return false), error information can be found in $this->_errors array.
  * 
  * @return bool
  */
 public function sync_entities()
 {
     if (!is_string($this->_api_key) || !strlen($this->_api_key)) {
         $this->log_error(0, 'Invalid API key');
         return false;
     }
     $success_log = array();
     foreach ($this->_entity_diff_meth_ids as $entity => $options) {
         // In case of error propagate, the error, don't update the diff_id and continue with the next entity
         // Call the diff method to get the changed entity's ids
         if (!is_array($ids_to_update = $this->get_entity_diff($entity, array($options['diff_method_name'] => $options['diff_id']), $new_diff_id))) {
             $this->log_error(1, 'Unable to process diff method', array('entity' => $entity, $options['diff_method_name'] => $options['diff_id'], 'url' => $this->url, 'cron_endpoint' => BAPI_CRON_ENDPOINT));
             continue;
         }
         // First time the cron is executed, we just save the returned diff id and the first execution timestamp, without syncing anything
         if (null === $this->_entity_diff_meth_ids[$entity]['first_cron_execution']) {
             $this->_entity_diff_meth_ids[$entity]['diff_id'] = $new_diff_id;
             $this->_entity_diff_meth_ids[$entity]['first_cron_execution'] = time();
             continue;
         }
         if (count($ids_to_update) > 0) {
             // Initialize the "cache" for get call (this reduce the number of calls by doing bulk calls of ids and caching the result
             $cache_options = array();
             // Taken from getMustache() function
             if ($entity == "property") {
                 $cache_options = array("seo" => 1, "descrip" => 1, "avail" => 1, "rates" => 1, "reviews" => 1, "poi" => 1);
             } else {
                 if ($entity == "poi") {
                     // Taken from getMustache() function
                     $cache_options = array("nearbyprops" => 1, "seo" => 1);
                 }
             }
             // Initialize the "cache" of get calls, the return value is not checked because if it didn't worked, then get calls won't use the cache.
             $this->_bapi->init_get_cache($entity, $ids_to_update, $cache_options);
             foreach ($ids_to_update as $id) {
                 if (!is_array($seo = $this->get_seo_from_bapi_cache($entity, $id))) {
                     $this->log_error(3, 'Unable to retrieve the SEO', array('entity' => $entity, 'entity_id' => $id));
                     continue 2;
                 }
                 if (!is_a($post = get_page_by_path(BAPISync::cleanurl($seo["DetailURL"])), 'WP_Post')) {
                     continue 1;
                 }
                 if (!kigo_sync_entity($post, $seo, true)) {
                     $this->log_error(4, 'Unable to process the sync', array('entity' => $entity, 'entity_id' => $id, 'SEO' => $seo));
                     continue 2;
                 }
             }
             $success_log[] = array('entity' => $entity, 'nb_of_updates' => count($ids_to_update));
         }
         // If this point is reached that means the sync has been done without error, we can update the diff_id and save the timestamp
         $this->_entity_diff_meth_ids[$entity]['diff_id'] = $new_diff_id;
         $this->_entity_diff_meth_ids[$entity]['last_update_timestamp'] = time();
     }
     if (!count($this->_errors) && (!is_string($json_entity_diff_meth_ids = json_encode($this->_entity_diff_meth_ids)) || !update_option(self::KIGO_CRON_DIFF_OPTION, $json_entity_diff_meth_ids))) {
         $this->log_error(5, 'Unable to update the option', array('entity_diff_meth_ids' => $this->_entity_diff_meth_ids));
     }
     if (count($success_log)) {
         $this->log_error(10, 'Correct update', $success_log);
     }
     return 0 === count($this->_errors);
 }
Exemple #2
0
function bapi_sync_entity()
{
    if (!(strpos($_SERVER['REQUEST_URI'], 'wp-admin') === false) || !(strpos($_SERVER['REQUEST_URI'], 'wp-login') === false)) {
        return false;
    }
    //getting the page by URL
    $post = get_page_by_path($_SERVER['REDIRECT_URL']);
    //if the URL is empty this is the Home Page
    if (empty($_SERVER['REDIRECT_URL'])) {
        $home_id = get_option('page_on_front');
        $post = get_post($home_id);
    }
    global $bapisync;
    if (empty($bapisync)) {
        $bapisync = new BAPISync();
        $bapisync->init();
    }
    // locate the SEO data stored in Bookt from the requested URL
    if (!is_array($seo = $bapisync->getSEOFromUrl(str_replace("?" . $_SERVER['QUERY_STRING'], '', $_SERVER['REQUEST_URI']))) || !isset($seo["entity"]) || !strlen($seo["entity"]) || !isset($seo["pkid"]) || empty($seo["pkid"]) && 'system' !== $seo["entity"]) {
        // ignore seo info if it doesn't point to a valid entity
        $seo = null;
    }
    // if we didnt find the page using the URL maybe we can search by entity:ID
    // we can only do that if we have the entity and ID from the SEO
    if ($post == null && $seo != null) {
        //we concat the custom field
        $pageBapiKey = $seo['entity'] . ':' . $seo['pkid'];
        //we form the search criteria
        $args = array('post_type' => 'page', 'post_status' => 'publish', 'meta_query' => array(array('key' => 'bapikey', 'value' => $pageBapiKey)));
        //we search a post with the entity_ID
        $result = get_posts($args);
        //it will be null if there is no result
        $post = $result[0];
    }
    return kigo_sync_entity($post, $seo);
}