Created
June 7, 2022 10:55
-
-
Save wolfcoder/cd5a11f291ae5adc47e2fbdfaebc4db2 to your computer and use it in GitHub Desktop.
inject new category to wordpress with json data
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 | |
| class RT_Sync { | |
| function __construct() { | |
| $rt_settings = get_option( 'rentalist_setting' ); | |
| $this->api_url = isset( $rt_settings['rt_api_url'] ) ? $rt_settings['rt_api_url'] : ''; | |
| $this->api_key = isset( $rt_settings['rt_api_key'] ) ? $rt_settings['rt_api_key'] : ''; | |
| $this->project_id = isset( $rt_settings['rt_project_id'] ) ? $rt_settings['rt_project_id'] : ''; | |
| add_action( 'wp_ajax_rt_sync_categories', array( $this, 'rt_sync_categories' ) ); | |
| } | |
| function rt_sync_categories() { | |
| $data_json = '[{ | |
| "projectCategoryId":170425,"parentProjectCategoryId":null,"projectId":26671,"name":"General Information","shortName":"General Information","isDefault":false,"ordinal":10,"typeId":1,"description":null,"isVirtual":false,"isVisible":true,"oldCategoryId":null,"dateEntered":"2021-07-14T14:15:22.507","dateModified":"2021-07-14T14:15:22.507","isVisiblePdf":true,"parentProjectCategory":null,"project":null,"inverseParentProjectCategory":[]},{ | |
| "projectCategoryId":170430,"parentProjectCategoryId":null,"projectId":26671,"name":"I am a...","shortName":"I am a...","isDefault":false,"ordinal":50,"typeId":1,"description":null,"isVirtual":false,"isVisible":true,"oldCategoryId":null,"dateEntered":"2021-07-14T14:16:52.11","dateModified":"2021-07-14T14:17:15.843","isVisiblePdf":true,"parentProjectCategory":null,"project":null,"inverseParentProjectCategory":[]},{ | |
| "projectCategoryId":170431,"parentProjectCategoryId":170430,"projectId":26671,"name":"Veterinary Professional","shortName":"Veterinary Professional","isDefault":false,"ordinal":1,"typeId":1,"description":null,"isVirtual":false,"isVisible":true,"oldCategoryId":null,"dateEntered":"2021-07-14T14:17:42.91","dateModified":"2021-07-14T14:17:42.91","isVisiblePdf":true,"parentProjectCategory":null,"project":null,"inverseParentProjectCategory":[]},{ | |
| "projectCategoryId":170432,"parentProjectCategoryId":170430,"projectId":26671,"name":"Student","shortName":"Student","isDefault":false,"ordinal":2,"typeId":1,"description":null,"isVirtual":false,"isVisible":true,"oldCategoryId":null,"dateEntered":"2021-07-14T14:18:41.04","dateModified":"2021-07-14T14:18:41.04","isVisiblePdf":true,"parentProjectCategory":null,"project":null,"inverseParentProjectCategory":[]},{ | |
| "projectCategoryId":170435,"parentProjectCategoryId":170430,"projectId":26671,"name":"Pet Owner","shortName":"Pet Owner","isDefault":false,"ordinal":5,"typeId":1,"description":null,"isVirtual":false,"isVisible":true,"oldCategoryId":null,"dateEntered":"2021-07-14T14:19:36.387","dateModified":"2021-07-14T14:19:36.387","isVisiblePdf":true,"parentProjectCategory":null,"project":null,"inverseParentProjectCategory":[]},{ | |
| "projectCategoryId":170450,"parentProjectCategoryId":170425,"projectId":26671,"name":"Book and Product Reviews","shortName":"Book and Product Reviews","isDefault":false,"ordinal":1,"typeId":1,"description":null,"isVirtual":false,"isVisible":true,"oldCategoryId":null,"dateEntered":"2021-07-14T14:24:53.96","dateModified":"2021-07-14T14:24:53.96","isVisiblePdf":true,"parentProjectCategory":null,"project":null,"inverseParentProjectCategory":[]},{ | |
| "projectCategoryId":170451,"parentProjectCategoryId":170425,"projectId":26671,"name":"Lectures and Seminars","shortName":"Lectures and Seminars","isDefault":false,"ordinal":1,"typeId":1,"description":null,"isVirtual":false,"isVisible":true,"oldCategoryId":null,"dateEntered":"2021-07-14T14:25:06.413","dateModified":"2021-07-14T14:25:06.413","isVisiblePdf":true,"parentProjectCategory":null,"project":null,"inverseParentProjectCategory":[]},{ | |
| "projectCategoryId":170452,"parentProjectCategoryId":170425,"projectId":26671,"name":"Reader Q&A","shortName":"Reader Q&A","isDefault":false,"ordinal":1,"typeId":1,"description":null,"isVirtual":false,"isVisible":true,"oldCategoryId":null,"dateEntered":"2021-07-14T14:25:20.897","dateModified":"2021-07-14T14:25:20.897","isVisiblePdf":true,"parentProjectCategory":null,"project":null,"inverseParentProjectCategory":[]},{ | |
| "projectCategoryId":193716,"parentProjectCategoryId":null,"projectId":26671,"name":"Ask LSH","shortName":"Ask LSH","isDefault":false,"ordinal":40,"typeId":1,"description":null,"isVirtual":false,"isVisible":true,"oldCategoryId":null,"dateEntered":"2022-06-02T17:26:05.283","dateModified":"2022-06-02T17:26:05.283","isVisiblePdf":true,"parentProjectCategory":null,"project":null,"inverseParentProjectCategory":[]}]'; | |
| // $categories = $this->rt_get_categories(); | |
| $categories = json_decode( $data_json ); | |
| $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_ID' => $category->projectCategoryId, | |
| 'cat_name' => $category->name, | |
| // 'category_nicename' => $this->rt_generate_slug( $category->name ), | |
| // 'category_parent' => empty( $category->parentProjectCategoryId ) ? '' : ( empty( $hash_cat[ $category->parentProjectCategoryId ] ) ? '' : $hash_cat[ $category->parentProjectCategoryId ] ); | |
| // 'category_parent' => $category->parentProjectCategoryId ? ( $hash_cat[ $category->parentProjectCategoryId ] ? $hash_cat[ $category->parentProjectCategoryId ] : '' ) : '' | |
| 'category_parent' => $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, 'data' => $categories ) ); | |
| wp_die(); | |
| } | |
| private function rt_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 ); | |
| print_r( $response ); | |
| $categories = json_decode( $response->body ); | |
| return $categories; | |
| } | |
| } | |
| //global $rt_sync; | |
| $rt_sync = new RT_Sync(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment