Ejemplo n.º 1
0
function kigo_sync_entity($post, $seo, $force_sync = false)
{
    global $bapisync;
    if (empty($bapisync)) {
        $bapisync = new BAPISync();
        $bapisync->init();
    }
    $t = BAPISync::getSolutionData();
    $maEnabled = $t['BizRules']['Has Market Area Landing Pages'];
    // parse out the meta attributes for the current post
    $page_exists_in_wp = !empty($post);
    $meta = $page_exists_in_wp ? get_post_custom($post->ID) : null;
    $last_update = !empty($meta) ? $meta['bapi_last_update'][0] : null;
    $staticpagekey = !empty($meta) ? $meta['bapi_page_id'][0] : null;
    $pagekey = !empty($meta) ? $meta['bapikey'][0] : null;
    $meta_keywords = !empty($meta) ? $meta['bapi_meta_keywords'][0] : null;
    $meta_description = !empty($meta) ? $meta['bapi_meta_description'][0] : null;
    $meta_title = !empty($meta) ? $meta['bapi_meta_title'][0] : null;
    //Only for properties pages: Retrieve the timestamp of first cron call and the latest successful diff call on this website
    if (!is_array($seo) || !isset($seo['entity']) || 'property' !== $seo['entity'] || !is_array($cron_info = Kigo_Site_Cron::get_cron_info_option('property'))) {
        $last_successful_diff = $first_cron_execution = 0;
    } else {
        $last_successful_diff = $cron_info['last_update_timestamp'];
        $first_cron_execution = $cron_info['first_cron_execution'];
    }
    /*we get the property headline*/
    $page_title = !is_array($seo) || !is_string($seo["PageTitle"]) ? '' : $seo["PageTitle"];
    $do_page_update = false;
    $do_meta_update = false;
    $do_market_update = false;
    $changes = "";
    if (!empty($seo) && ($seo["entity"] == 'property' || $seo["entity"] == 'marketarea') && $maEnabled) {
        $do_market_update = true;
    }
    // Static pages (entity = system ?) are not updated, but their meta data are updated
    if ($page_exists_in_wp && !empty($staticpagekey)) {
        // update the meta tags
        if (empty($meta['bapi_last_update']) || time() - $meta['bapi_last_update'][0] > 300) {
            does_meta_exist("bapi_last_update", $meta) ? update_post_meta($post->ID, 'bapi_last_update', time()) : add_post_meta($post->ID, 'bapi_last_update', time(), true);
            if (!empty($seo)) {
                if ($meta['bapi_meta_description'][0] != $seo["MetaDescrip"]) {
                    update_post_meta($post->ID, 'bapi_meta_description', $seo["MetaDescrip"]);
                }
                if ($meta['bapi_meta_title'][0] != $seo["PageTitle"]) {
                    update_post_meta($post->ID, 'bapi_meta_title', $seo["PageTitle"]);
                }
                if ($meta['bapi_meta_keywords'][0] != $seo["MetaKeywords"]) {
                    update_post_meta($post->ID, 'bapi_meta_keywords', $seo["MetaKeywords"]);
                }
                //does_meta_exist("bapi_meta_description", $meta) ? update_post_meta($post->ID, 'bapi_meta_description', $seo["MetaDescrip"]) : add_post_meta($post->ID, 'bapi_meta_description', $seo["MetaDescrip"], true);
                //does_meta_exist("bapi_meta_keywords", $meta) ? update_post_meta($post->ID, 'bapi_meta_keywords', $seo["MetaKeywords"]) : add_post_meta($post->ID, 'bapi_meta_keywords', $seo["MetaKeywords"], true);
            }
        }
        return true;
    }
    //catch bad bapikey
    if ($page_exists_in_wp && !empty($pagekey)) {
        $pktest = explode(":", $pagekey);
        //print_r($pktest); exit();
        if (strlen($pktest[0]) == 0 || strlen($pktest[1]) == 0) {
            //To Delete Meta or Page, that is the question.
            //wp_delete_post($post->ID,true);  //Going w/ deleting post for now - I think this will work because if page should exist it will ge recreated.
            //delete_post_meta($post->ID,'bapikey');
        }
        //Check for non-initialized market area page (-1) and set correct bapikey
        if ($pktest[1] == -1 && $pktest[0] == 'marketarea') {
            //print_r($post); exit();
            update_post_meta($post->ID, "bapikey", 'marketarea:' . $seo['pkid']);
        }
    }
    // case 1: page exists in wp and is marked for syncing on wp but, it no longer exists in Bookt
    if ($page_exists_in_wp && empty($seo) && !empty($pagekey)) {
        //echo $post->ID; exit();
        //print_r("case 1");
        // Action: Set current page to "unpublished"
        // $post->post_status = "unpublish";
        //wp_delete_post($post->ID,true); //optional 2nd parameter can be added -> if true then page will be deleted immediately instead of going to trash.
    } else {
        if ($page_exists_in_wp && !empty($seo)) {
            //Move from trashcan to publish if exists and no published
            if ($post->post_status == 'trash') {
                $post->post_status = 'publish';
                $do_page_update = true;
            }
            $do_page_update = empty($meta['bapi_last_update']) || $meta['bapi_last_update'][0] <= $first_cron_execution || time() - $last_successful_diff > 900 && time() - $meta['bapi_last_update'][0] > 300;
            // check for difference in meta description
            if ($meta['bapi_meta_description'][0] != stripslashes($seo["MetaDescrip"])) {
                $changes = $changes . "|meta_description";
                $do_meta_update = true;
            }
            if ($meta['bapi_meta_title'][0] != $seo["PageTitle"]) {
                $changes = $changes . "|meta_title";
                $do_meta_update = true;
            }
            // check for difference in meta keywords
            if ($meta['bapi_meta_keywords'][0] != $seo["MetaKeywords"]) {
                $changes = $changes . "|meta_keywords";
                $do_meta_update = true;
            }
            /*check if this is a bapi defined page*/
            if (!empty($pagekey) || $pagekey != null) {
                $dom = new DomDocument();
                libxml_use_internal_errors(true);
                $dom->loadHTML("<!DOCTYPE html><html><head></head><body>" . $post->post_content . "</body></html>");
                libxml_use_internal_errors(false);
                $xpath = new DOMXpath($dom);
                $xpathq = "//h2[@class='title']";
                $elements = $xpath->query($xpathq);
                if (!is_null($elements)) {
                    $resultarray = array();
                    foreach ($elements as $element) {
                        $nodes = $element->childNodes;
                        foreach ($nodes as $node) {
                            $resultarray[] = $node->nodeValue;
                        }
                    }
                    $entity_title = $resultarray;
                    if (strlen($entity_title[0]) > 0) {
                        $page_title = $entity_title[0];
                    }
                }
            }
            // check for different in title
            if ($post->post_title != $page_title) {
                $changes = $changes . "|post_title";
                $do_page_update = true;
            }
            // check for difference in post name
            if ($post->post_name != BAPISync::clean_post_name($seo["DetailURL"])) {
                $changes = $changes . "|post_name";
                $do_page_update = true;
            }
        } else {
            if (!$page_exists_in_wp && empty($seo)) {
                // Action: Do nothing and let wp generate a 404
                //print_r("case 3");
            } else {
                if (!$page_exists_in_wp && !empty($seo)) {
                    //print_r("case 4".$do_market_update);exit();
                    // Result-> Need to create the page
                    $changes = "create new page";
                    $tempPost = new stdClass();
                    $post = new WP_Post($tempPost);
                    $do_page_update = true;
                    $do_meta_update = true;
                }
            }
        }
    }
    //Check if developer is using debugmode and force entity sync
    if (isset($_GET['debugmode']) && $_GET['debugmode']) {
        $do_page_update = true;
    }
    if ($do_page_update || $force_sync) {
        // do page update
        $post->comment_status = "close";
        if (!is_string($s2s_success = $bapisync->getMustache($seo["entity"], $seo["pkid"]))) {
            // by "trash"ing the post, WP will display a nice 404.
            // next time we try to sync and the property shows up, it will be reverted to an active page.
            $post->post_status = 'trash';
        } else {
            $post->post_content = $s2s_success;
            //if we have a bapikey
            if (!empty($pagekey) || $pagekey != null) {
                $dom = new DomDocument();
                libxml_use_internal_errors(true);
                $dom->loadHTML("<!DOCTYPE html><html><head></head><body>" . $post->post_content . "</body></html>");
                libxml_use_internal_errors(false);
                $xpath = new DOMXpath($dom);
                $xpathq = "//h2[@class='title']";
                $elements = $xpath->query($xpathq);
                if (!is_null($elements)) {
                    $resultarray = array();
                    foreach ($elements as $element) {
                        $nodes = $element->childNodes;
                        foreach ($nodes as $node) {
                            $resultarray[] = $node->nodeValue;
                        }
                    }
                    $entity_title = $resultarray;
                    if (strlen($entity_title[0]) > 0) {
                        $page_title = $entity_title[0];
                    }
                }
            }
            $post->post_title = $page_title;
            $post->post_name = BAPISync::clean_post_name($seo["DetailURL"]);
            $post->post_parent = get_page_by_path(BAPISync::getRootPath($seo["entity"]))->ID;
            if ($do_market_update) {
                $post->post_parent = ensure_ma_landing_pages($seo["DetailURL"]);
            }
            $post->post_type = "page";
        }
        remove_filter('content_save_pre', 'wp_filter_post_kses');
        if (empty($post->ID)) {
            $post->ID = wp_insert_post($post, $wp_error);
        } else {
            wp_update_post($post);
        }
        add_filter('content_save_pre', 'wp_filter_post_kses');
    }
    if ($do_meta_update || $do_page_update || $force_sync) {
        // update the meta tags
        does_meta_exist("bapi_last_update", $meta) ? update_post_meta($post->ID, 'bapi_last_update', time()) : add_post_meta($post->ID, 'bapi_last_update', time(), true);
        does_meta_exist("bapi_meta_description", $meta) ? update_post_meta($post->ID, 'bapi_meta_description', $seo["MetaDescrip"]) : add_post_meta($post->ID, 'bapi_meta_description', $seo["MetaDescrip"], true);
        does_meta_exist("bapi_meta_keywords", $meta) ? update_post_meta($post->ID, 'bapi_meta_keywords', $seo["MetaKeywords"]) : add_post_meta($post->ID, 'bapi_meta_keywords', $seo["MetaKeywords"], true);
        does_meta_exist("_wp_page_template", $meta) ? update_post_meta($post->ID, "_wp_page_template", BAPISync::getPageTemplate($seo["entity"])) : add_post_meta($post->ID, "_wp_page_template", BAPISync::getPageTemplate($seo["entity"]), true);
        does_meta_exist("bapikey", $meta) ? update_post_meta($post->ID, "bapikey", BAPISync::getPageKey($seo["entity"], $seo["pkid"])) : add_post_meta($post->ID, "bapikey", BAPISync::getPageKey($seo["entity"], $seo["pkid"]), true);
        does_meta_exist("bapi_meta_title", $meta) ? update_post_meta($post->ID, 'bapi_meta_title', $seo["PageTitle"]) : add_post_meta($post->ID, 'bapi_meta_title', $seo["PageTitle"], true);
    }
    return true;
}