Created
June 8, 2022 05:38
-
-
Save wolfcoder/9c2a6a2d943c58b7515107174a2b2af6 to your computer and use it in GitHub Desktop.
VIN article syn class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| if( !class_exists ( 'VAP_Sync' ) ) { | |
| class VAP_Sync { | |
| function __construct() { | |
| $vap_settings = get_option( 'vap_setting' ); | |
| $this->api_url = isset( $vap_settings['vap_api_url'] ) ? $vap_settings['vap_api_url'] : ''; | |
| $this->api_key = isset( $vap_settings['vap_api_key'] ) ? $vap_settings['vap_api_key'] : ''; | |
| $this->project_id = isset( $vap_settings['vap_project_id'] ) ? $vap_settings['vap_project_id'] : ''; | |
| $this->append_post = isset( $vap_settings['vap_append_post'] ) ? $vap_settings['vap_append_post'] : 1; | |
| $this->auto_publish = isset( $vap_settings['vap_auto_publish'] ); | |
| $this->default_page_size = 15; | |
| add_action( 'wp_ajax_vap_sync_categories', array( $this, 'vap_get_sync_categories' ) ); | |
| add_action( 'wp_ajax_vap_sync_start', array( $this, 'vap_start_sync' ) ); | |
| add_action( 'wp_ajax_vap_sync_final_stop', array( $this, 'vap_final_stop_sync' ) ); | |
| } | |
| function vap_display_settings() { | |
| if( file_exists( VAP_INCLUDES_DIR . "vap_sync.view.php" ) ) { | |
| include_once( VAP_INCLUDES_DIR . "vap_sync.view.php" ); | |
| } | |
| } | |
| function vap_get_sync_categories() { | |
| $categories = $this->vap_get_categories(); | |
| $output = array(); | |
| if ( !empty( $categories ) ) { | |
| $hash_cat = array(); | |
| foreach ( $categories as $category ) { | |
| $category_id = category_exists( $category->name ); | |
| if ( !isset( $category_id ) ) { | |
| $new_category = array( | |
| 'cat_name' => $category->name, | |
| 'category_nicename' => $this->vap_generate_slug( $category->name ), | |
| 'category_parent' => empty( $category->parentProjectCategoryId ) ? '' : ( empty( $hash_cat[$category->parentProjectCategoryId] ) ? '' : $hash_cat[$category->parentProjectCategoryId] ) | |
| ); | |
| $category_id = wp_insert_category( $new_category ); | |
| } | |
| $hash_cat[$category->projectCategoryId] = $category_id; | |
| $output[] = array( | |
| 'id' => $category->projectCategoryId, | |
| 'wpid' => $category_id, | |
| 'name' => $category->name | |
| ); | |
| } | |
| } | |
| echo json_encode( array( 'success' => true, 'result' => $output ) ); | |
| wp_die(); | |
| } | |
| function vap_get_lastsync() { | |
| $vap_lastsync = get_option( 'vap_lastsync' ); | |
| $output = array( | |
| 'timestamp' => isset( $vap_lastsync['timestamp'] ) ? $vap_lastsync['timestamp'] : 0, | |
| 'category' => isset( $vap_lastsync['category'] ) ? $vap_lastsync['category'] : 0, | |
| 'page' => isset( $vap_lastsync['page'] ) ? $vap_lastsync['page'] : 1, | |
| ); | |
| return $output; | |
| } | |
| function vap_start_sync () { | |
| $processed = 0; | |
| if ( !empty( $_POST['on_sync'] ) ) { | |
| $timestamp = ($_POST['timestamp'] + 0); | |
| $processed = $this->vap_sync_process( $_POST['category_id'], $_POST['category_wpid'], $_POST['page_index'], $timestamp ); | |
| } | |
| echo json_encode( array( 'success' => true, 'result' => array( 'processed' => $processed ) ) ); | |
| wp_die(); | |
| } | |
| function vap_final_stop_sync() { | |
| $this->vap_save_lastsync( 0, 1, time() ); | |
| echo json_encode( array( 'success' => true ) ); | |
| wp_die(); | |
| } | |
| function vap_sync_process( $category_id, $category_wpid, $page_index, $timestamp ) { | |
| $processed = 0; | |
| if ( !empty( $this->api_url ) && !empty( $this->api_key ) && !empty( $this->project_id ) ) { | |
| $articles = $this->vap_get_articles( $category_id, $page_index, $this->default_page_size, $timestamp ); | |
| if ( !empty( $articles ) ) { | |
| foreach( $articles as $article ) { | |
| if ( $article->nodeTypeID == 1 ) { | |
| $documents = $this->vap_get_article_subdocuments( $article->nodeID, $article->nodeObjectID, $article->nodeObjectTypeID ); | |
| if ( !empty( $documents ) ) { | |
| $content = $this->vap_get_document_by_type( $documents ); | |
| if ( !empty( $content ) ) { | |
| $article_info = $this->vap_get_article_info( $article->nodeID ); | |
| $fixed_content = $this->vap_fix_media_url( $content->content ); | |
| $content_checksum = $this->vap_generate_checksum( $fixed_content ); | |
| $post_id = $this->vap_check_existing_post_by_meta( $article->nodeID ); //post_exists( $post_title ); | |
| if ($post_id) { | |
| $post_categories = get_the_category( $post_id ); | |
| $category_ids = array_map( function( $item ) { | |
| return $item->term_id; | |
| }, $post_categories ); | |
| if ( !in_array( $category_wpid, $category_ids ) ) { | |
| wp_set_post_categories( $post_id, array( $category_wpid ), true ); | |
| } | |
| if ( $this->append_post == 1 ) { | |
| $existed_checksum = get_post_meta( $post_id, '_vinarticlechecksum', true ); | |
| if ( $existed_checksum != $content_checksum ) { | |
| $modified_post = array( | |
| 'ID' => $post_id, | |
| 'post_title' => $article->nodeTitle, | |
| 'post_content' => $fixed_content, | |
| 'post_excerpt' => ( !empty( $article->nodeDescription ) ? $article->nodeDescription : '' ), | |
| 'post_date' => date( 'Y-m-d H:i:s', strtotime( $article_info->dateAuthorCreated ) ), | |
| ); | |
| wp_update_post( $modified_post ); | |
| } | |
| } | |
| } else { | |
| $new_post = array( | |
| 'post_title' => $article->nodeTitle, | |
| 'post_content' => $fixed_content, | |
| 'post_excerpt' => ( !empty( $article->nodeDescription ) ? $article->nodeDescription : '' ), | |
| 'post_status' => ( $this->auto_publish ? 'publish' : 'draft' ), | |
| 'post_date' => date( 'Y-m-d H:i:s', strtotime( $article_info->dateAuthorCreated ) ), | |
| 'post_category' => array( $category_wpid ) | |
| ); | |
| $post_id = wp_insert_post( $new_post ); | |
| } | |
| update_post_meta( $post_id, '_vinarticleid', $article->nodeID ); | |
| update_post_meta( $post_id, '_vinarticlechecksum', $content_checksum ); | |
| update_post_meta( $post_id, '_vinarticleauthordesc', $this->vap_get_author( $documents ) ); | |
| // records vin category ids for future checking | |
| $vincat_ids = get_post_meta( $post_id, '_vinarticlecatid', true ); | |
| if ( empty( $vincat_ids ) ) { | |
| $vincat_ids = array(); | |
| $vincat_ids[] = $category_id; | |
| } else { | |
| if ( !in_array( $category_id, $vincat_ids ) ) { | |
| $vincat_ids[] = $category_id; | |
| } | |
| } | |
| update_post_meta( $post_id, '_vinarticlecatid', $vincat_ids ); | |
| $processed++; | |
| } | |
| } | |
| } | |
| } | |
| $this->vap_save_lastsync( $category_id, $page_index, $timestamp ); | |
| } | |
| } | |
| return $processed; | |
| } | |
| private function vap_get_categories() { | |
| $categories = null; | |
| $headers = array( | |
| 'ApiKey' => $this->api_key, | |
| 'Accept' => 'application/json; version=1', | |
| ); | |
| $path = $this->api_url . '/articlecategory/' . $this->project_id; | |
| $response = Requests::get( $path, $headers ); | |
| $categories = json_decode( $response->body ); | |
| return $categories; | |
| } | |
| private function vap_get_articles( $category_id, $page_index, $page_size, $timestamp ) { | |
| $articles = null; | |
| $path = ''; | |
| $headers = array( | |
| 'ApiKey' => $this->api_key, | |
| 'Accept' => 'application/json; version=1', | |
| ); | |
| if ( $timestamp != 0 ) { | |
| $start_date = date( 'm/d/Y', strtotime( '-1 day', $timestamp ) ); | |
| $end_date = date( 'm/d/Y', strtotime( '+1 day ' ) ); | |
| $path = $this->api_url . '/article/filter/' . $this->project_id . '/category/' . $category_id . '/'. $page_index .'/' . $page_size; | |
| $path = $path . '?startdate=' . $start_date . '&enddate=' . $end_date; | |
| } else { | |
| $path = $this->api_url . '/article/' . $this->project_id . '/category/' . $category_id . '/'. $page_index .'/' . $page_size; | |
| } | |
| error_log( $path ); | |
| $response = Requests::get( $path, $headers ); | |
| $articles = json_decode( $response->body ); | |
| return $articles; | |
| } | |
| private function vap_get_article_info( $article_id ) { | |
| $headers = array( | |
| 'ApiKey' => $this->api_key, | |
| 'Accept' => 'application/json; version=1', | |
| ); | |
| $path = $this->api_url . '/article/detail?id=' . $article_id . ''; | |
| error_log( $path ); | |
| $response = Requests::get( $path, $headers ); | |
| $info = json_decode( $response->body ); | |
| return $info; | |
| } | |
| private function vap_get_article_subdocuments( $article_id, $object_id, $object_type ) { | |
| $headers = array( | |
| 'ApiKey' => $this->api_key, | |
| 'Accept' => 'application/json; version=1', | |
| ); | |
| $path = $this->api_url . '/article/subdocument/' . $article_id . '?objectId=' . $object_id . '&objectTypeId=' . $object_type . '&deleted=false'; | |
| error_log( $path ); | |
| $response = Requests::get( $path, $headers ); | |
| $sub_details = json_decode( $response->body ); | |
| return $sub_details; | |
| } | |
| private function vap_get_document_by_type( $sub_documents, $type_name = 'Article' ) { | |
| $content = null; | |
| foreach($sub_documents as $sub_document) { | |
| if ( $sub_document->subDocumentType == $type_name ) { | |
| $content = $sub_document; | |
| break; | |
| } | |
| } | |
| return $content; | |
| } | |
| private function vap_get_author( $sub_documents ) { | |
| $author_doc = $this->vap_get_document_by_type( $sub_documents, 'Authors' ); | |
| if ( !empty( $author_doc )) { | |
| $author_desc = $author_doc->content; | |
| $pattern = '/href=\x22default.aspx/i'; | |
| return preg_replace($pattern, 'href="https://veterinarypartner.vin.com/default.aspx', $author_desc); | |
| } | |
| return ''; | |
| } | |
| private function vap_check_existing_post_by_meta( $vin_article_id ) { | |
| global $wpdb; | |
| $tbl = $wpdb->prefix.'postmeta'; | |
| $prepare_guery = $wpdb->prepare( "SELECT post_id FROM $tbl where meta_key = '_vinarticleid' and meta_value = '%d'", $vin_article_id ); | |
| $ids = $wpdb->get_col( $prepare_guery ); | |
| return empty( $ids ) ? false : reset( $ids ); | |
| } | |
| private function vap_save_lastsync( $category_id, $page_index, $timestamp ) { | |
| update_option('vap_lastsync', array( | |
| 'timestamp' => $timestamp, | |
| 'category' => $category_id, | |
| 'page' => $page_index, | |
| )); | |
| } | |
| private function vap_generate_checksum( $content ) { | |
| return crc32( $content ); | |
| } | |
| private function vap_fix_media_url( $content ) { | |
| $pattern = '/\/AppUtil\/Image\/handler.ashx/i'; | |
| return preg_replace($pattern, 'https://beta.vin.com/AppUtil/Image/handler.ashx', $content); | |
| } | |
| private function vap_generate_slug( $name ) { | |
| $slug = strtolower( trim( preg_replace( '/[^A-Za-z0-9-]+/', '-', $name ) ) ); | |
| return $slug; | |
| } | |
| } | |
| global $vap_sync; | |
| $vap_sync = new VAP_Sync(); | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment