Headline
CVE-2023-3956: class-instawp-rest-apis.php in instawp-connect/tags/0.0.9.18/includes – WordPress Plugin Repository
The InstaWP Connect plugin for WordPress is vulnerable to unauthorized access of data, modification of data and loss of data due to a missing capability check on the ‘events_receiver’ function in versions up to, and including, 0.0.9.18. This makes it possible for unauthenticated attackers to add, modify or delete post and taxonomy, install, activate or deactivate plugin, change customizer settings, add or modify or delete user including administrator user.
1<?php2/**3 *4 * @link https://instawp.com/5 * @since 1.06 *7 * @package instawp8 * @subpackage instawp/includes9 */1011/**12 *13 * @since 1.014 * @package instawp15 * @subpackage instawp/includes16 * @author instawp team17 */1819if ( ! defined(‘INSTAWP_PLUGIN_DIR’) ) {20 die;21}2223require_once INSTAWP_PLUGIN_DIR . '/includes/class-instawp-db.php’;2425class InstaWP_Rest_Apis{26 27 private $wpdb;2829 private $InstaWP_db;3031 private $tables;3233 public function __construct(){34 global $wpdb;3536 $this->wpdb = $wpdb;3738 $this->InstaWP_db = new InstaWP_DB();3940 $this->tables = $this->InstaWP_db->tables;4142 /*43 * Initiate Sync44 * Endpoint : /wp-json/instawp-connect/v1/sync45 * HOOK - rest_api_init46 */47 48 add_action( 'rest_api_init’, function () {49 register_rest_route( 'instawp-connect/v1’, '/sync’, 50 array(51 ‘methods’ => 'POST’,52 ‘callback’ => [$this, ‘events_receiver’],53 ‘permission_callback’ => [$this, ‘check_permission’],54 ) );55 } );56 }5758 function check_permission(){59 return true;60 } 6162 public function get_post_by_reference_Id($post_type, $reference_id, $post_name) {63 $post = get_posts( array(64 'post_type’=> $post_type,65 ‘meta_key’ => 'instawp_event_sync_reference_id’,66 ‘meta_value’ => $reference_id67 ) );68 if(!empty($post)){69 $post = $post[0];70 }else {71 $post = instawp_get_post_by_name($post_name, $post_type); 72 }73 return $post;74 }7576 public function create_or_update_post($post, $post_meta) {77 $reference_id = isset($post_meta[‘instawp_event_sync_reference_id’][0]) ? $post_meta[‘instawp_event_sync_reference_id’][0] : '’;78 $destination_post = $this->get_post_by_reference_Id($post[‘post_type’], $reference_id, $post[‘post_name’]);79 unset($post[‘ID’]);80 if(!empty($destination_post)){81 #The post exists,Then update82 $post_id = $post[‘ID’] = $destination_post->ID;83 //$post[‘post_parent’] = $destination_post->post_parent;84 $postData = $this->postData($post);85 wp_update_post($postData);86 #post meta87 $this->add_update_postmeta($post_meta, $destination_post->ID);88 }else{89 $postData = $this->postData($post);90 #The post does not exist,Then insert91 $post_id = wp_insert_post($postData); 92 #post meta93 $this->add_update_postmeta( $post_meta, $post_id ); 94 }95 return $post_id;96 }9798 /**99 * Reciver 100 * @param array $data Options for the function.101 * @return string|null 102 */103 public function events_receiver($req) {104 // error_reporting(E_ALL);105 // ini_set('display_errors’, 1);106 $body = $req->get_body();107 $bodyArr = json_decode($body);108 $encrypted_contents = json_decode($bodyArr->encrypted_contents);109 $sync_id = $bodyArr->sync_id;110 $source_connect_id = $bodyArr->source_connect_id;111 $is_enabled = false;112 113 114 if(get_option(‘syncing_enabled_disabled’)){115 $is_enabled = true;116 }117118 #forcely disable the syncing at the destination119 update_option('syncing_enabled_disabled’, 0);120121 if(!empty($encrypted_contents) && is_array($encrypted_contents)){122 $total_op = count($encrypted_contents);123 $count = 1;124 $progress_status = 'pending’;125 $changes = $sync_response = [];126 foreach($encrypted_contents as $v){127 128 $source_id = (isset($v->source_id) && !empty($v->source_id)) ? intval($v->source_id) : null;129130 /*131 *Post Oprations 132 */133 //create and update134 if(isset($v->event_slug) && ($v->event_slug == ‘post_change’ ||$v->event_slug == ‘post_new’) ){135 $posts = isset($v->details->posts) ? (array) $v->details->posts : '’;136 $postmeta = isset($v->details->postmeta) ? (array) $v->details->postmeta : '’;137 $featured_image = isset($v->details->featured_image) ? (array) $v->details->featured_image : '’;138 $media = isset($v->details->media) ? (array) $v->details->media : '’;139 140 $parent_post = isset($v->details->parent->post) ? (array) $v->details->parent->post : [];141 142 #check for the post parent143 if(!empty($parent_post)){144 $parent_post_meta = isset($v->details->parent->post_meta) ? (array) $v->details->parent->post_meta : [];145 $reference_id = isset($parent_post_meta[‘instawp_event_sync_reference_id’][0]) ? $parent_post_meta[‘instawp_event_sync_reference_id’][0] : ‘’;146 $destination_post = $this->get_post_by_reference_Id($parent_post[‘post_type’], $reference_id, $parent_post[‘post_name’]);147 if(!empty($destination_post)){148 $posts[‘post_parent’] = $destination_post->ID;149 }150 }151 152 if($posts[‘post_type’] == ‘attachment’){153 //create or update the attachments154 $posts[‘ID’] = $this->handle_attachments($posts, $postmeta, $posts[‘guid’]);155 #update meta156 $this->add_update_postmeta($postmeta,$posts[‘ID’]);157 }else{158 $posts[‘ID’] = $this->create_or_update_post($posts, $postmeta);159 }160 161 #feature image import 162 if(isset($featured_image[‘media’]) && !empty($featured_image[‘media’])){163 $att_id = $this->handle_attachments((array) $featured_image[‘media’],(array) $featured_image[‘media_meta’], $featured_image[‘featured_image_url’]);164 if(isset($att_id) && !empty($att_id)){165 set_post_thumbnail($posts[‘ID’],$att_id);166 }167 }168169 #if post type is product then set gallery170 if(get_post_type($posts[‘ID’]) == ‘product’){171 if(isset($v->details->product_gallery) && !empty($v->details->product_gallery)){172 $product_gallery = $v->details->product_gallery;173 $gallery_ids = [];174 //pr($product_gallery);175 foreach($product_gallery as $gallery){176 if(isset($gallery->media) && !empty($gallery->media) && isset($gallery->url) && $gallery->url !=’’){177 $gallery_ids[] = $this->handle_attachments((array) $gallery->media,(array) $gallery->media_meta, $gallery->url);178 }179 }180 $this->set_product_gallery($posts[‘ID’],$gallery_ids);181 }182 }183184 #terms in post185 $taxonomies = (array) $v->details->taxonomies;186 $this->reset_post_terms( $posts[‘ID’]); //rest the terms for all taxo187 if(!empty($taxonomies) && is_array($taxonomies)){188 foreach($taxonomies as $taxonomy => $terms){189 $terms = (array) $terms;190 $term_ids = [];191 # if term not exist then create first192 if(!empty($terms) && is_array($terms)){193 foreach($terms as $term){194 $term = (array) $term;195 if(!term_exists($term[‘slug’],$taxonomy)){196 $inserted_term = wp_insert_term(197 $term[‘name’], // the term 198 $taxonomy, // the taxonomy199 array(200 ‘description’ => $term[‘description’],201 ‘slug’ => $term[‘slug’],202 ‘parent’ => 0203 )204 );205 $term_ids[] = $inserted_term[‘term_id’];206 }else{207 $get_term_by =(array) get_term_by('slug’,$term[‘slug’] , $taxonomy);208 $term_ids[] = $get_term_by[‘term_id’];209 }210 }211 }212 #set terms in post213 wp_set_post_terms( $posts[‘ID’], $term_ids, $taxonomy );214 }215 }216217 # media upload from content 218 $this->upload_content_media($media,$posts[‘ID’]);219220 #message 221 $message = 'Sync successfully.’;222 $status = 'completed’;223 $sync_response[] = $this->sync_opration_response($status,$message,$v);224 #changes225 $changes[$v->event_type] = $changes[$v->event_type] + 1;226 }227228 //Post trash229 if(isset($v->event_slug) && $v->event_slug == ‘post_trash’){230 if(isset($source_id)){231 $posts = (array) $v->details->posts;232 $postmeta = (array) $v->details->postmeta;233 $post_by_reference_id = get_posts( array(234 'post_type’=> $posts[‘post_type’],235 ‘meta_key’ => 'instawp_event_sync_reference_id’,236 ‘meta_value’ => isset($postmeta[‘instawp_event_sync_reference_id’][0]) ? $postmeta[‘instawp_event_sync_reference_id’][0] : '’,237 ) );238239 if(!empty($post_by_reference_id)){240 $post_id = $post_by_reference_id[0]->ID;241 $rel = wp_trash_post($post_id); //Post data on success, false or null on failure.242 $status = $this->sync_post_status($rel);243 $message = $this->sync_message($rel);244 }else{245 $post_check_data = instawp_get_post_by_name(str_replace('__trashed’,’’, $posts[‘post_name’]), $posts[‘post_type’]); 246 if(!empty($post_check_data)){247 $rel = wp_trash_post($post_check_data->ID); //Post data on success, false or null on failure.248 $status = $this->sync_post_status($rel);249 $message = $this->sync_message($rel);250 }else{251 $status = 'pending’;252 $message = $this->notExistMsg(); 253 }254 }255 $sync_response[] = $this->sync_opration_response($status,$message,$v);256 #changes257 $changes[$v->event_type] = $changes[$v->event_type] + 1; 258 }259 }260261 //Post permanently delete 262 if(isset($v->event_slug) && $v->event_slug == ‘post_delete’){263 if(isset($source_id)){264 $posts = (array) $v->details->posts;265 $postmeta = (array) $v->details->postmeta;266 $post_by_reference_id = get_posts([267 ‘post_status’ => 'trash’,268 ‘post_type’ => $posts[‘post_type’],269 ‘nopaging’ => TRUE,270 ‘meta_query’ => array(271 array(272 ‘key’ => 'instawp_event_sync_reference_id’,273 ‘value’ => isset($postmeta[‘instawp_event_sync_reference_id’][0]) ? $postmeta[‘instawp_event_sync_reference_id’][0] : '’,274 ‘compare’ => '=’,275 ),276 ),277 ]);278279 if(!empty($post_by_reference_id)){280 $post_id = $post_by_reference_id[0]->ID;281 $rel = wp_delete_post($post_id); //Post data on success, false or null on failure.282 $status = $this->sync_post_status($rel);283 $message = $this->sync_message($rel);284 }else{285 $post_check_data = instawp_get_post_by_name($posts[‘post_name’], $posts[‘post_type’]); 286 if(!empty($post_check_data)){287 $rel = wp_delete_post($post_check_data->ID); //Post data on success, false or null on failure.288 $status = $this->sync_post_status($rel);289 $message = $this->sync_message($rel);290 }else{291 $status = 'pending’;292 $message = $this->notExistMsg(); 293 }294 }295 $sync_response[] = $this->sync_opration_response($status,$message,$v);296 #changes297 $changes[$v->event_type] = $changes[$v->event_type] + 1; 298 }299 }300301 //Post restored 302 if(isset($v->event_slug) && $v->event_slug == ‘untrashed_post’){303 if(isset($source_id)){304 $posts = (array) $v->details->posts;305 $postmeta = (array) $v->details->postmeta;306 $post_by_reference_id = get_posts([307 ‘post_status’ => 'trash’,308 ‘post_type’ => $posts[‘post_type’],309 ‘nopaging’ => TRUE,310 ‘meta_query’ => array(311 array(312 ‘key’ => 'instawp_event_sync_reference_id’,313 ‘value’ => isset($postmeta[‘instawp_event_sync_reference_id’][0]) ? $postmeta[‘instawp_event_sync_reference_id’][0] : '’,314 ‘compare’ => '=’,315 ),316 ),317 ]);318 319320 if(!empty($post_by_reference_id)){321 $post_id = $post_by_reference_id[0]->ID;322 $rel = wp_untrash_post($post_id);323 $status = $this->sync_post_status($rel);324 $message = $this->sync_message($rel);325 }else{326 $post_check_data = instawp_get_post_by_name($posts[‘post_name’].’__trashed’, $posts[‘post_type’]); 327 if(!empty($post_check_data)){328 $rel = wp_untrash_post($post_check_data->ID);329 $status = $this->sync_post_status($rel);330 $message = $this->sync_message($rel);331 }else{332 $status = 'pending’;333 $message = $this->notExistMsg(); 334 }335 }336 $sync_response[] = $this->sync_opration_response($status,$message,$v);337 #changes338 $changes[$v->event_type] = $changes[$v->event_type] + 1; 339 }340 }341 342 /*343 *Plugin Oprations344 */345 //Plugin actiavte 346 if(isset($v->details) && $v->event_slug == ‘activate_plugin’){347 $check_plugin_installed = $this->check_plugin_installed($v->details);348 if($check_plugin_installed != 1){349 $pluginData = get_plugin_data($v->details);350 if(!empty($pluginData[‘TextDomain’])){351 $this->plugin_install($pluginData[‘TextDomain’]);352 } 353 }354355 $this->plugin_activation($v->details);356 #message 357 $message = 'Sync successfully.’;358 $status = 'completed’;359 $sync_response[] = $this->sync_opration_response($status,$message,$v);360 #changes361 $changes[$v->event_type] = $changes[$v->event_type] + 1;362 }363364 //Plugin deactiavte 365 if(isset($v->event_slug) && $v->event_slug == ‘deactivate_plugin’){366 $this->plugin_deactivation($v->details);367 #message 368 $message = 'Sync successfully.’;369 $status = 'completed’;370 $sync_response[] = $this->sync_opration_response($status,$message,$v);371 #changes372 $changes[$v->event_type] = $changes[$v->event_type] + 1;373 } 374 375 /*376 * Taxonomy Oprations377 */378379 //create and update380 if(isset($v->event_slug) && ($v->event_slug == ‘create_taxonomy’ || $v->event_slug == ‘edit_taxonomy’)){381 if(isset($source_id)){382 $details = (array) $v->details;383 $wp_terms = $this->wp_terms_data($source_id,$details);384 $wp_term_taxonomy = $this->wp_term_taxonomy_data($source_id,$details);385 if(!term_exists($source_id,$v->event_type)){386 if($v->event_slug == ‘create_taxonomy’){387 $this->insert_taxonomy($source_id,$wp_terms,$wp_term_taxonomy);388 clean_term_cache($source_id);389 } 390 }391 if(term_exists($source_id,$v->event_type)){392 if($v->event_slug == ‘edit_taxonomy’){393 $this->update_taxonomy($source_id,$wp_terms,$wp_term_taxonomy);394 }395 } 396 397 #message 398 $message = 'Sync successfully.’;399 $status = 'completed’;400 $sync_response[] = $this->sync_opration_response($status,$message,$v);401 #changes402 $changes[$v->event_type] = $changes[$v->event_type] + 1;403 404 }405 }406407 //Delete 408 if(isset($v->event_slug) && $v->event_slug == ‘delete_taxonomy’){409 if(isset($source_id)){410 if(term_exists($source_id,$v->event_type)){411 $rel = wp_delete_term($source_id,$v->event_type);412 $status = $this->sync_post_status($rel);413 $message = $this->sync_message($rel);414 }415 }else{416 $status = 'pending’;417 $message = $this->notExistMsg(); 418 }419 $sync_response[] = $this->sync_opration_response($status,$message,$v);420 #changes421 $changes[$v->event_type] = $changes[$v->event_type] + 1; 422 }423 424 /**425 * Customizer settings update426 */427 428 if(isset($v->event_slug) && $v->event_slug == ‘customizer_changes’){429 $details = isset($v->details) ? $v->details : '’;430 431 #custom logo432 $this->customizer_custom_logo($details->custom_logo);433 434 #background image435 $this->customizer_background_image($details->background_image);436437 #site icon438 $this->customizer_site_icon($details->site_icon);439 440 #background color441 if(isset($details->background_color) && !empty($details->background_color)){442 set_theme_mod( 'background_color’, $details->background_color );443 } 444445 #Site Title446 update_option( 'blogname’, $details->name );447448 #Tagline449 $this->blogDescription($details->description); 450 451 #Homepage Settings452 if(isset($details->show_on_front) && !empty($details->show_on_front)){453 update_option( 'show_on_front’, $details->show_on_front );454 }455 456 #for ‘Astra’ theme457 if( isset($details->astra_settings) && !empty($details->astra_settings) ){458 $astra_settings = $this->object_to_array($details->astra_settings);459 update_option( ‘astra-settings’, $astra_settings );460 }461 462 #nav menu locations463 if(!empty($details->nav_menu_locations)){464 $menu_array = (array) $details->nav_menu_locations;465 set_theme_mod( ‘nav_menu_locations’, $menu_array );466 }467468 #Custom css post id469 $custom_css_post = (array) $details->custom_css_post;470 if(!empty($details->custom_css_post)){471 if (get_post_status($custom_css_post[‘ID’]) ) { 472 #The post exists,Then update473 $postData = $this->postData($custom_css_post,’update’);474 wp_update_post($postData);475 476 } else {477 $postData = $this->postData($custom_css_post,’insert’);478 #The post does not exist,Then insert479 wp_insert_post($postData); 480 }481 set_theme_mod( 'custom_css_post_id’, $custom_css_post[‘ID’] );482 }483 $current_theme = wp_get_theme();484 if($current_theme->Name == ‘Astra’){ #for ‘Astra’ theme485 $astra_theme_setting = isset($details->astra_theme_customizer_settings) ? (array) $details->astra_theme_customizer_settings : '’;486 $this->setAstraCostmizerSetings($astra_theme_setting);487 }else if($current_theme->Name == ‘Divi’){ #for ‘Divi’ theme488 $divi_settings = isset($details->divi_settings) ? (array) $details->divi_settings : '’;489 if(!empty($divi_settings) && is_array($divi_settings)){490 update_option('et_divi’,$divi_settings);491 }492 }493494 #message 495 $message = 'Sync successfully.’;496 $status = 'completed’;497 $sync_response[] = $this->sync_opration_response($status,$message,$v);498 #changes499 $changes[$v->event_type] = $changes[$v->event_type] + 1;500 }501502 /**503 * Woocommerce attributes504 */505 506 #create&upadte woocommerce attribute507 if(isset($v->event_slug) && ($v->event_slug == ‘woocommerce_attribute_added’ || $v->event_slug == ‘woocommerce_attribute_updated’)){508 $details = isset($v->details) ? (array) $v->details : '’;509 if(!empty($details)){510 $attribute = wc_get_attribute(208);511 if(!empty($attribute)){512 unset($details[‘id’]); 513 wc_update_attribute($v->source_id,$attribute);514515 #message 516 $message = 'Sync successfully.’;517 $status = 'completed’;518 $sync_response[] = $this->sync_opration_response($status,$message,$v);519 #changes520 $changes[$v->event_type] = $changes[$v->event_type] + 1;521 }else{522 $this->woocommerce_create_attribute($v->source_id,$details); 523524 #message 525 $message = 'Sync successfully.’;526 $status = 'completed’;527 $sync_response[] = $this->sync_opration_response($status,$message,$v);528 #changes529 $changes[$v->event_type] = $changes[$v->event_type] + 1;530531 } 532 } 533 }534 535 if(isset($v->event_slug) && $v->event_slug == ‘woocommerce_attribute_deleted’){536 wc_delete_attribute($v->source_id);537 #message 538 $message = 'Sync successfully.’;539 $status = 'completed’;540 $sync_response[] = $this->sync_opration_response($status,$message,$v);541 #changes542 $changes[$v->event_type] = $changes[$v->event_type] + 1;543 }544 545 /**546 * Users actions547 */548 if(isset($v->event_type) && $v->event_type == ‘users’){549 $user_data = isset($v->details->user_data) ? (array) $v->details->user_data : '’;550 $user_meta = isset($v->details->user_meta) ? (array) $v->details->user_meta : '’;551 //$user = get_userdata($v->source_id);552 $user_table = $this->wpdb->prefix.’users’;553554 $get_user_by_reference_id = get_users(array(555 ‘meta_key’ => 'instawp_event_user_sync_reference_id’,556 ‘meta_value’ => isset($user_meta[‘instawp_event_sync_reference_id’][0]) ? $user_meta[‘instawp_event_sync_reference_id’][0] : '’,557 ));558559 $user = !empty($get_user_by_reference_id) ? $get_user_by_reference_id[0] : get_user_by('email’, $user_data[‘email’]);560 561 //Create user if not exits562 if( isset($v->event_slug) && ($v->event_slug == ‘user_register’) ){563 if(!$user){564 unset($user_data[‘ID’]);565 array_merge($user_data, ['role’=>$v->details->role ]);566 $user = wp_insert_user($user_data); 567 $this->add_update_usermeta($user_meta,$v->source_id);568 }569 }570571 //Update user572 if( isset($v->event_slug) && ($v->event_slug == ‘profile_update’) ){573 $this->InstaWP_db->update($user_table,$user_data,array( ‘ID’ => $v->source_id ));574 $this->add_update_usermeta($user_meta,$v->source_id);575 $user->add_role( $v->details->role );576 }577578 //Delete user579 if( isset($v->event_slug) && ($v->event_slug == ‘delete_user’) ){580 if(isset($user->data->user_email)){581 if($user->data->user_email == $user_data[‘data’]->user_email){ 582 wp_delete_user($v->source_id);583 }584 }585 }586587 #message 588 $message = 'Sync successfully.’;589 $status = 'completed’;590 $sync_response[] = $this->sync_opration_response($status,$message,$v);591 #changes592 $changes[$v->event_type] = $changes[$v->event_type] + 1;593 }594595 /*596 * widget597 */598 if(isset($v->event_type) && $v->event_type == ‘widget’){599 $widget_block = (array) $v->details->widget_block;600 $appp = (array) $v->details;601 $dataIns = [602 ‘data’ => json_encode($appp)603 ];604 $this->InstaWP_db->insert('wp_testing’,$dataIns);605606 $widget_block_arr = [];607 foreach($widget_block as $widget_key => $widget_val){608 if($widget_key == ‘_multiwidget’){609 $widget_block_arr[$widget_key] = $widget_val;610 }else{611 $widget_val_arr = (array) $widget_val;612 $widget_block_arr[$widget_key] = [‘content’ => $widget_val_arr[‘content’]];613 } 614 }615 update_option('widget_block’,$widget_block_arr);616 #message 617 $message = 'Sync successfully.’;618 $status = 'completed’;619 $sync_response[] = $this->sync_opration_response($status,$message,$v);620 #changes621 $changes[$v->event_type] = $changes[$v->event_type] + 1;622 }623 /*624 * Update api for cloud625 */626 $progress = intval($count/$total_op * 100);627 $progress_status = ($progress > 100 ) ? ‘in_progress’: ‘completed’;628 #Sync update629 $syncUpdate = [630 ‘progress’ => $progress,631 ‘status’ => $progress_status,632 ‘message’ => $message,633 ‘changes’ => [‘changes’ => $changes,’sync_response’ => $sync_response],634 ];635 $this->sync_update($sync_id,$syncUpdate,$source_connect_id);636 $count++; 637 }638 }639 640 #Sync history save641 $this->sync_history_save($body,$changes,’Complete’);642643 #enable is back if syncing already enabled at the destination644 if($is_enabled){645 update_option(‘syncing_enabled_disabled’, 1);646 }647648 return new WP_REST_Response( 649 array(650 ‘encrypted_contents’ => $encrypted_contents,651 ‘source_connect_id’ => $source_connect_id,652 ‘changes’ => [‘changes’ => $changes,’sync_response’ => $sync_response],653 ‘sync_id’ => $sync_id654 ) 655 );656 }657658 /**659 * This function is for upload media which are coming form widgets.660 */661 public function upload_widgets_media($media = null, $content = null){662 $media = json_decode(reset($media));663 $new = $old = []; 664 $newContent = '’; 665 if(!empty($media)){666 foreach($media as $v){667 $v = (array) $v; 668 if(isset($v[‘attachment_id’]) && isset($v[‘attachment_url’])){669 $attachment_id = $this->insert_attachment($v[‘attachment_id’],$v[‘attachment_url’]);670 $new[] = wp_get_attachment_url($attachment_id); 671 $old[] = $v[‘attachment_url’];672 } 673 }674 $newContent = str_replace($old, $new, $content); #str_replace(old,new,str)675 }676 return $newContent;677 }678679 public function user_id_exists($user_id){680 $table_name = $this->wpdb->prefix.’users’;681 $count = $this->wpdb->get_var($this->wpdb->prepare("SELECT COUNT(*) FROM $table_name WHERE ID = %d",$user_id));682 if($count == 1){ return true; }else{ return false; }683 }684 685 public function blogDescription($v = null){686 $this->wpdb->update($this->wpdb->prefix.’options’,[‘option_value’ => $v],array( ‘option_name’ => ‘blogdescription’ ));687 }688689 /**690 * object to array conversation 691 */692 public function object_to_array($data) {693 if ((! is_array($data)) and (! is_object($data))){694 return;695 }696 $result = array();697 $data = (array) $data;698 foreach ($data as $key => $value) {699 if (is_object($value))700 $value = (array) $value;701 if (is_array($value))702 $result[$key] = $this->object_to_array($value);703 else704 $result[$key] = $value;705 }706 return $result;707 }708709 /**710 * Create woocommerce attribute711 */712 public function woocommerce_create_attribute($source_id,$data = null){713 $format = array( '%s’, '%s’, '%s’, '%s’, ‘%d’ );714 $data[‘attribute_id’] = intval($source_id);715 $results = $this->wpdb->insert(716 $this->wpdb->prefix . 'woocommerce_attribute_taxonomies’,717 $data,718 $format719 );720 721 if ( is_wp_error( $results ) ) {722 return new WP_Error( 'cannot_create_attribute’, 'Can not create attribute!’, array( ‘status’ => 400 ) );723 }724 $id = $this->wpdb->insert_id;725 /**726 * Attribute added.727 *728 * @param int $id Added attribute ID.729 * @param array $data Attribute data.730 */731 do_action( 'woocommerce_attribute_added’, $id, $data );732 // Clear cache and flush rewrite rules.733 wp_schedule_single_event( time(), ‘woocommerce_flush_rewrite_rules’ );734 delete_transient( ‘wc_attribute_taxonomies’ );735 WC_Cache_Helper::invalidate_cache_group( ‘woocommerce-attributes’ ); 736 }737738 /**739 * Set product gallery 740 */741 public function set_product_gallery($product_id = null, $gallery_ids = null){742 $product = new WC_product($product_id);743 $product->set_gallery_image_ids( $gallery_ids );744 $product->save();745 }746747 public function customizer_site_icon($data = null){748 $attachment_id = $data->id;749 $url = $data->url;750 if(isset($attachment_id) && !empty($attachment_id)){751 $attUrl = wp_get_attachment_url(intval($attachment_id));752 if(!empty($attUrl)){753 update_option('site_icon’,$attachment_id);754 }else{755 $attachment_id = $this->insert_attachment($attachment_id,$url);756 update_option('site_icon’,$attachment_id);757 }758 }else{759 update_option('site_icon’,$attachment_id);760 }761 }762763 public function customizer_custom_logo($data){764 $attachment_id = $data->id;765 $url = $data->url;766 if(isset($attachment_id) && !empty($attachment_id)){767 $attUrl = wp_get_attachment_url(intval($attachment_id));768 if(!empty($attUrl)){769 set_theme_mod( 'custom_logo’, $attachment_id );770 }else{771 $attachment_id = $this->insert_attachment($attachment_id,$url);772 set_theme_mod( 'custom_logo’, $attachment_id );773 }774 }else{775 set_theme_mod( 'custom_logo’, $attachment_id );776 }777 }778779 public function customizer_background_image($data = null){ 780 $attachment_id = $data->id;781 $url = $data->url;782 if(isset($attachment_id) && !empty($attachment_id)){783 $attUrl = wp_get_attachment_url(intval($attachment_id));784 if(!empty($attUrl)){785 set_theme_mod( 'background_image’, $attUrl );786 }else{787 $attachment_id = $this->insert_attachment($attachment_id,$url);788 $attachment_url = wp_get_attachment_url(intval($attachment_id));789 set_theme_mod( 'background_image’, $attachment_url ); 790 }791 }else{792 set_theme_mod( 'background_image’, $url ); 793 }794 795 if(isset($data->background_preset) && !empty($data->background_preset)){796 set_theme_mod( 'background_preset’, $data->background_preset );797 }798799 if(isset($data->background_size) && !empty($data->background_size)){800 set_theme_mod( 'background_size’, $data->background_size );801 }802 803 if(isset($data->background_repeat) && !empty($data->background_repeat)){804 set_theme_mod( 'background_repeat’, $data->background_repeat );805 }806 807 if(isset($data->background_attachment) && !empty($data->background_attachment)){808 set_theme_mod( 'background_attachment’, $data->background_attachment );809 }810 811 if(isset($data->background_position_x) && !empty($data->background_position_x)){812 set_theme_mod( 'background_position_x’, $data->background_position_x );813 }814 815 if(isset($data->background_position_y) && !empty($data->background_position_y)){816 set_theme_mod( 'background_position_y’, $data->background_position_y );817 }818 } 819820 /**821 * This function is for upload media which are coming form content.822 */823 public function upload_content_media($media = null, $post_id = null){824 $media = json_decode(reset($media));825 $post = get_post($post_id); 826 $content = $post->post_content;827 $new = $old = []; 828 if(!empty($media)){829 foreach($media as $v){830 $v = (array) $v; 831 if(isset($v[‘attachment_id’]) && isset($v[‘attachment_url’])){832 $attachment_id = $this->handle_attachments((array) $v[‘attachment_media’], (array) $v[‘attachment_media_meta’], $v[‘attachment_url’]);833 $new[] = wp_get_attachment_url($attachment_id); 834 $old[] = $v[‘attachment_url’];835 } 836 }837 $newContent = str_replace($old, $new, $content); #str_replace(old,new,str)838 $arg = array(839 ‘ID’ => $post_id,840 ‘post_content’ => $newContent,841 );842 wp_update_post( $arg );843 }844 } 845846 public function notExistMsg(){847 return "ID is not exists.";848 }849850 public function wp_terms_data($term_id = null, $arr = []){851 return [852 ‘term_id’ => $term_id,853 ‘name’ => $arr[‘name’],854 ‘slug’ => $arr[‘slug’]855 ];856 }857 public function wp_term_taxonomy_data($term_id = null, $arr = []){858 return [859 ‘term_taxonomy_id’ => $term_id,860 ‘term_id’ => $term_id,861 ‘taxonomy’ => $arr[‘taxonomy’],862 ‘description’ => $arr[‘description’],863 ‘parent’ => $arr[‘parent’]864 ];865 } 866867 public function insert_taxonomy($term_id = null, $wp_terms = null, $wp_term_taxonomy = null){868 $this->InstaWP_db->insert($this->wpdb->prefix.’terms’,$wp_terms);869 $this->InstaWP_db->insert($this->wpdb->prefix.’term_taxonomy’,$wp_term_taxonomy);870 }871872 public function update_taxonomy($term_id = null, $wp_terms = null, $wp_term_taxonomy = null){873 $this->wpdb->update($this->wpdb->prefix.’terms’,$wp_terms,array( ‘term_id’ => $term_id ));874 $this->wpdb->update($this->wpdb->prefix.’term_taxonomy’,$wp_term_taxonomy,array( ‘term_id’ => $term_id ));875 }876877 public function reset_post_terms($post_id){878 $this->wpdb->query( 879 $this->wpdb->prepare( 880 "DELETE FROM {$this->wpdb->prefix}term_relationships WHERE object_id = %d",881 $post_id,882 )883 );884 }885886 public function add_update_postmeta($meta_data = null, $post_id = null){887 888 if(!empty($meta_data) && is_array($meta_data)){889 foreach($meta_data as $k => $v){890 if(isset($v[0])){891 $checkSerialize = @unserialize($v[0]);892 $metaVal = ($checkSerialize !== false || $v[0] === ‘b:0;’) ? unserialize($v[0]) : $v[0];893 if ( metadata_exists('post’,$post_id,$k) ) {894 update_post_meta($post_id,$k,$metaVal); 895 }else{896 add_post_meta($post_id,$k,$metaVal);897 }898 }899 }900901 //if _elementor_css this key not existing then it’s giving a error.902 if(array_key_exists('_elementor_version’,$meta_data)){903 if(!array_key_exists('_elementor_css’,$meta_data)){904 /*$elementor_css = [905 ‘time’ => time(),906 ‘fonts’ => [],907 ‘icons’ => [],908 ‘dynamic_elements_ids’ => [],909 ‘status’ => ‘empty’,910 ‘css’ => ‘’911 ];912 */913 $elementor_css = [];914 add_post_meta($post_id,’_elementor_css’,$elementor_css);915 }916 }917918 //delete the edit lock post919 delete_post_meta($post_id,’_edit_lock’);920 }921 }922923 public function postData($posts = null, $op = null){924 $args = array(925 ‘post_author’ => $posts[‘post_author’],926 ‘post_date’ => $posts[‘post_date’],927 ‘post_date_gmt’ => $posts[‘post_date_gmt’],928 ‘post_content’ => $posts[‘post_content’],929 ‘post_title’ => $posts[‘post_title’],930 ‘post_excerpt’ => $posts[‘post_excerpt’],931 ‘post_status’ => $posts[‘post_status’],932 ‘comment_status’ => $posts[‘comment_status’],933 ‘ping_status’ => $posts[‘ping_status’],934 ‘post_password’ => $posts[‘post_password’],935 ‘post_name’ => $posts[‘post_name’],936 ‘to_ping’ => $posts[‘to_ping’],937 ‘pinged’ => $posts[‘pinged’],938 ‘post_modified’ => $posts[‘post_modified’],939 ‘post_modified_gmt’ => $posts[‘post_modified_gmt’],940 ‘post_content_filtered’ => $posts[‘post_content_filtered’],941 ‘post_parent’ => $posts[‘post_parent’],942 //’guid’ => $posts[‘guid’],943 ‘menu_order’ => $posts[‘menu_order’],944 ‘post_type’ => $posts[‘post_type’],945 ‘post_mime_type’ => $posts[‘post_mime_type’],946 ‘comment_count’ => $posts[‘comment_count’],947 ‘filter’ => $posts[‘filter’],948 );949 #ID to update existing post950 if(isset($posts[‘ID’]) && $posts[‘ID’] > 0){951 $args = array_merge(['ID’=> $posts[‘ID’] ],$args);952 }953 return $args;954 }955956 # import attechments form source to destination.957 public function handle_attachments($attachment_post, $attachment_post_meta, $file){958 $reference_id = '’;959 if(isset($attachment_post_meta[‘instawp_event_sync_reference_id’][0])){960 $reference_id = $attachment_post_meta[‘instawp_event_sync_reference_id’][0];961 }962 $attachment_id = $this->get_post_by_reference_Id($attachment_post[‘post_type’], $reference_id, $attachment_post[‘post_name’]);963 if(!$attachment_id){964 $filename = basename($file);965 $arrContextOptions=array(966 "ssl"=>array(967 "verify_peer"=>false,968 "verify_peer_name"=>false,969 ),970 );971 $parent_post_id = 0;972 $upload_file = wp_upload_bits($filename, null, file_get_contents($file,false, stream_context_create($arrContextOptions)));973 if (!$upload_file[‘error’]) {974 $wp_filetype = wp_check_filetype($filename, null );975 $attachment = array(976 ‘post_mime_type’ => $wp_filetype[‘type’],977 ‘post_parent’ => $parent_post_id,978 ‘post_title’ => preg_replace('/\.[^.]+$/’, '’, $filename),979 ‘post_content’ => '’,980 ‘post_status’ => 'inherit’981 );982 require_once(ABSPATH . “wp-admin” . ‘/includes/image.php’);983 require_once(ABSPATH . “wp-admin” . ‘/includes/file.php’);984 require_once(ABSPATH . “wp-admin” . ‘/includes/media.php’);985 $attachment_id = wp_insert_attachment( $attachment, $upload_file[‘file’], $parent_post_id );986 if (!is_wp_error($attachment_id)) {987 $attachment_data = wp_generate_attachment_metadata( $attachment_id, $upload_file[‘file’] );988 wp_update_attachment_metadata( $attachment_id, $attachment_data );989 $this->add_update_postmeta([‘instawp_event_sync_reference_id’ =>[$reference_id]], $attachment_id);990 }991 return $attachment_id; 992 }993 return;994 }995 return $attachment_id;996 }997 # import attechments form source to destination.998 public function insert_attachment($attachment_id = null, $file = null){999 $filename = basename($file);1000 $arrContextOptions=array(1001 "ssl"=>array(1002 "verify_peer"=>false,1003 "verify_peer_name"=>false,1004 ),1005 );1006 $parent_post_id = 0;1007 $upload_file = wp_upload_bits($filename, null, file_get_contents($file,false, stream_context_create($arrContextOptions)));1008 if (!$upload_file[‘error’]) {1009 $wp_filetype = wp_check_filetype($filename, null );1010 $attachment = array(1011 ‘import_id’ => $attachment_id,1012 ‘post_mime_type’ => $wp_filetype[‘type’],1013 ‘post_parent’ => $parent_post_id,1014 ‘post_title’ => preg_replace('/\.[^.]+$/’, '’, $filename),1015 ‘post_content’ => '’,1016 ‘post_status’ => 'inherit’1017 );1018 require_once(ABSPATH . “wp-admin” . ‘/includes/image.php’);1019 require_once(ABSPATH . “wp-admin” . ‘/includes/file.php’);1020 require_once(ABSPATH . “wp-admin” . ‘/includes/media.php’);1021 $attachment_id = wp_insert_attachment( $attachment, $upload_file[‘file’], $parent_post_id );1022 if (!is_wp_error($attachment_id)) {1023 $attachment_data = wp_generate_attachment_metadata( $attachment_id, $upload_file[‘file’] );1024 wp_update_attachment_metadata( $attachment_id, $attachment_data );1025 }1026 }1027 return $attachment_id; 1028 }10291030 #Insert history 1031 public function sync_history_save($body = null, $changes = null,$status = null){1032 $dir = 'dev-to-live’;1033 $date = date(‘Y-m-d H:i:s’);1034 $bodyArr = json_decode($body);1035 $message = isset($bodyArr->sync_message) ? $bodyArr->sync_message : '’;1036 $data = [1037 ‘encrypted_contents’ => $bodyArr->encrypted_contents,1038 ‘changes’ => json_encode($changes),1039 ‘sync_response’ => '’,1040 ‘direction’ => $dir,1041 ‘status’ => $status,1042 ‘user_id’ => isset($bodyArr->upload_wp_user) ? $bodyArr->upload_wp_user : '’,1043 ‘changes_sync_id’ => isset($bodyArr->sync_id) ? $bodyArr->sync_id : '’,1044 ‘sync_message’ => $message,1045 ‘source_connect_id’ => '’,1046 ‘source_url’ => isset($bodyArr->source_url) ? $bodyArr->source_url : '’,1047 ‘date’ => $date,1048 ];1049 $this->InstaWP_db->insert($this->tables[‘sh_table’],$data);1050 }10511052 #Plugin activate. 1053 public function plugin_activation( $plugin ) {1054 if( ! function_exists(‘activate_plugin’) ) {1055 require_once ABSPATH . 'wp-admin/includes/plugin.php’;1056 }10571058 if( ! is_plugin_active( $plugin ) ) {1059 activate_plugin( $plugin );1060 }1061 } 10621063 #Plugin deactivate.1064 public function plugin_deactivation( $plugin ) {1065 if( ! function_exists(‘deactivate_plugins’) ) {1066 require_once ABSPATH . 'wp-admin/includes/plugin.php’;1067 }1068 if( is_plugin_active( $plugin ) ) {1069 deactivate_plugins( $plugin );1070 }1071 }10721073 public function sync_message($rel = null){1074 if(isset($rel->ID)){1075 $message = 'Sync successfully.’; 1076 }else{1077 $message = 'Something went wrong.’; 1078 }1079 return $message;1080 }10811082 public function sync_post_status($rel = null){1083 $status = 'in_progress’;1084 if(isset($rel->ID)){1085 $status = 'completed’;1086 }else{1087 $status = 'pending’;1088 }1089 return $status;1090 }10911092 public function sync_opration_response($status = null, $message = null, $v = null){1093 return [ 1094 ‘id’ => $v->id,1095 ‘status’ => $status,1096 ‘message’ => $message1097 ];1098 }1099 1100 public function sync_update($sync_id = null, $data = null, $source_connect_id = null){1101 global $InstaWP_Curl;1102 $api_doamin = InstaWP_Setting::get_api_domain(); 1103 $connect_id = intval($source_connect_id);1104 $endpoint = '/api/v2/connects/’.$connect_id.’/syncs/’.$sync_id;1105 $url = $api_doamin.$endpoint; #https://stage.instawp.io/api/v2/connects/241/syncs/4501106 $api_key = $this->get_api_key(); 11071108 try{ 1109 $curl = curl_init();1110 curl_setopt_array($curl, array(1111 CURLOPT_URL => $url,1112 CURLOPT_RETURNTRANSFER => true,1113 CURLOPT_ENCODING => '’,1114 CURLOPT_MAXREDIRS => 10,1115 CURLOPT_TIMEOUT => 0,1116 CURLOPT_FOLLOWLOCATION => true,1117 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,1118 CURLOPT_CUSTOMREQUEST => 'PATCH’,1119 CURLOPT_POSTFIELDS => json_encode($data),1120 CURLOPT_HTTPHEADER => array(1121 'Authorization: Bearer '.$api_key.’’,1122 'Content-Type: application/json’1123 ),1124 ));11251126 $response = curl_exec($curl);1127 return $response;1128 } catch (Exception $e) {1129 return $e->getMessage();1130 }1131 }11321133 function get_api_key(){1134 $instawp_api_options = get_option(‘instawp_api_options’); 1135 return $instawp_api_options[‘api_key’];1136 }11371138 /*1139 * Create elementor css file 'post-{post_id}.css’1140 */1141 public function create_elementor_css_file($data = null, $post_id = null){1142 $upload_dir = wp_upload_dir();1143 $filename = 'post-'.$post_id.’.css’;1144 $filePath = $upload_dir[‘basedir’].’/elementor/css/’.$filename;1145 $file = fopen($filePath, “w+”);//w+,w1146 fwrite($file, $data);1147 fclose($file);1148 }11491150 /**1151 * Plugin install1152 */1153 public function plugin_install($plugin_slug){1154 include_once( ABSPATH . ‘wp-admin/includes/plugin-install.php’ ); //for plugins_api…1155 $api = plugins_api( 'plugin_information’, array(1156 ‘slug’ => $plugin_slug,1157 ‘fields’ => array(1158 ‘short_description’ => false,1159 ‘sections’ => false,1160 ‘requires’ => false,1161 ‘rating’ => false,1162 ‘ratings’ => false,1163 ‘downloaded’ => false,1164 ‘last_updated’ => false,1165 ‘added’ => false,1166 ‘tags’ => false,1167 ‘compatibility’ => false,1168 ‘homepage’ => false,1169 ‘donate_link’ => false,1170 ),1171 ));1172 //includes necessary for Plugin_Upgrader and Plugin_Installer_Skin1173 include_once( ABSPATH . ‘wp-admin/includes/file.php’ );1174 include_once( ABSPATH . ‘wp-admin/includes/misc.php’ );1175 include_once( ABSPATH . ‘wp-admin/includes/class-wp-upgrader.php’ );1176 $upgrader = new Plugin_Upgrader( new Plugin_Installer_Skin( compact('title’, 'url’, 'nonce’, 'plugin’, ‘api’) ) );1177 $upgrader->install($api->download_link);1178 }11791180 /**1181 * Check if plugin is installed by getting all plugins from the plugins dir1182 *1183 * @param $plugin_slug1184 *1185 * @return bool1186 */1187 public function check_plugin_installed( $plugin_slug ): bool {1188 $installed_plugins = get_plugins();1189 return array_key_exists( $plugin_slug, $installed_plugins ) || in_array( $plugin_slug, $installed_plugins, true );1190 }11911192 //add and update user meta1193 public function add_update_usermeta($user_meta = null, $user_id = null){1194 if(!empty($user_meta) && is_array($user_meta)){1195 foreach($user_meta as $k => $v){1196 if(isset($v[0])){1197 $checkSerialize = @unserialize($v[0]);1198 $metaVal = ($checkSerialize !== false || $v[0] === ‘b:0;’) ? unserialize($v[0]) : $v[0];1199 if ( metadata_exists('user’,$user_id,$k) ) {1200 update_user_meta($user_id,$k,$metaVal); 1201 }else{1202 add_user_meta($user_id,$k,$metaVal);1203 }1204 }1205 }1206 }1207 }12081209 /**1210 * Set Astra Costmizer Setings1211 */1212 function setAstraCostmizerSetings($arr = null){1213 #Checkout1214 update_option('woocommerce_checkout_company_field’,$arr[‘woocommerce_checkout_company_field’]);1215 update_option('woocommerce_checkout_address_2_field’,$arr[‘woocommerce_checkout_address_2_field’]);1216 update_option('woocommerce_checkout_phone_field’,$arr[‘woocommerce_checkout_phone_field’]);1217 update_option('woocommerce_checkout_highlight_required_fields’,$arr[‘woocommerce_checkout_highlight_required_fields’]);1218 update_option('wp_page_for_privacy_policy’,$arr[‘wp_page_for_privacy_policy’]);1219 update_option('woocommerce_terms_page_id’,$arr[‘woocommerce_terms_page_id’]);1220 update_option('woocommerce_checkout_privacy_policy_text’,$arr[‘woocommerce_checkout_privacy_policy_text’]);1221 update_option('woocommerce_checkout_terms_and_conditions_checkbox_text’,$arr[‘woocommerce_checkout_terms_and_conditions_checkbox_text’]);1222 1223 #product catalog1224 update_option('woocommerce_shop_page_display’,$arr[‘woocommerce_shop_page_display’]);1225 update_option('woocommerce_default_catalog_orderby’,$arr[‘woocommerce_default_catalog_orderby’]);1226 update_option('woocommerce_category_archive_display’,$arr[‘woocommerce_category_archive_display’]);1227 1228 #Product Images1229 update_option('woocommerce_single_image_width’,$arr[‘woocommerce_single_image_width’]);1230 update_option('woocommerce_thumbnail_image_width’,$arr[‘woocommerce_thumbnail_image_width’]);1231 update_option('woocommerce_thumbnail_cropping’,$arr[‘woocommerce_thumbnail_cropping’]);1232 update_option('woocommerce_thumbnail_cropping_custom_width’,$arr[‘woocommerce_thumbnail_cropping_custom_width’]);1233 update_option('woocommerce_thumbnail_cropping_custom_height’,$arr[‘woocommerce_thumbnail_cropping_custom_height’]);1234 1235 #Store Notice1236 update_option('woocommerce_demo_store’,$arr[‘woocommerce_demo_store’]);1237 update_option('woocommerce_demo_store_notice’,$arr[‘woocommerce_demo_store_notice’]);1238 }1239}1240new InstaWP_Rest_Apis();