function save_seo_meta($postid) { $bapisync = new BAPISync(); $bapisync->init(); $perma = get_permalink(); $permaPath = parse_url($perma); $relativePerma = get_relative($perma); $pageID = get_post_meta(get_the_ID(), 'bapi_page_id'); if ($relativePerma == '/' && $pageID[0] != 'bapi_home') { return; } $seo = $bapisync->getSEOFromUrl($relativePerma); $meta_words = get_post_custom($post->ID, '', true); $myPageId = $seo['ID']; $myType = $seo['entity']; $myPkId = $seo['pkid']; if ($myType === null) { $myType = 0; } if ($myPageId === null) { $myPageId = 0; } if ($myPkId === null) { $myPkId = 0; } $apiKey = getbapiapikey(); $bapi = getBAPIObj(); if (!$bapi->isvalid()) { return; } $keywor = sanitize_text_field($_POST['bapi_meta_keywords']); $metle = sanitize_text_field($_POST['bapi_meta_title']); $meta_desc = sanitize_text_field($_POST['bapi_meta_description']); // save old value if keyword empty or null if ($metle === null || empty($metle)) { $metle = $meta_words['bapi_meta_title'][0]; } if ($meta_desc === null || empty($meta_desc)) { $meta_desc = $meta_words['bapi_meta_description'][0]; } if ($keywor === null || empty($keywor)) { $keywor = $meta_words['bapi_meta_keywords'][0]; } //saves to wordpress database if (isset($_POST['bapi_meta_keywords'])) { if ($_POST['bapi_meta_keywords'] !== $meta_words['bapi_meta_keywords'][0]) { } update_post_meta($postid, 'bapi_meta_keywords', sanitize_text_field($_POST['bapi_meta_keywords'])); } if (isset($_POST['bapi_meta_title']) && $_POST['bapi_meta_title'] !== $meta_words['bapi_meta_title'][0]) { update_post_meta($postid, 'bapi_meta_title', sanitize_text_field($_POST['bapi_meta_title'])); } if (isset($_POST['bapi_meta_description']) && $_POST['bapi_meta_description'] !== $meta_words['bapi_meta_description'][0]) { update_post_meta($postid, 'bapi_meta_description', sanitize_text_field($_POST['bapi_meta_description'])); } $metaArr = array('MetaKeywords' => $keywor, 'PageTitle' => $metle, 'MetaDescrip' => $meta_desc, 'ID' => $myPageId, 'pkid' => $myPkId, 'Keyword' => $relativePerma, 'entity' => $myType); $jsify = json_encode($metaArr); $jsonObj = 'data=' . (string) $jsify; // entety: tyoe and language needs to be //print_r($jsonObj);exit(); $bapi->save($jsonObj, $apiKey); update_option('bapi_keywords_lastmod', 0); bapi_sync_coredata(); }
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); }