Changeset 2700245
- Timestamp:
- 03/27/2022 03:30:23 PM (4 years ago)
- File:
-
- 1 edited
-
kitcart/trunk/kitcart.php (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
kitcart/trunk/kitcart.php
r2634464 r2700245 31 31 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 32 32 33 Copyright 2020-202 1Kitcart Technology, Inc.33 Copyright 2020-2022 Kitcart Technology, Inc. 34 34 */ 35 35 … … 46 46 define('KITCART__PLUGIN_DIR', plugin_dir_path(__FILE__)); 47 47 define('KITCART_DELETE_LIMIT', 100000); 48 define('KITCART_API_URI', 'https://kitcart.net/api/ create-order');48 define('KITCART_API_URI', 'https://kitcart.net/api/wordpress/'); 49 49 50 50 // add settings link … … 92 92 93 93 if ($kitcart_wc_active == 'no') { 94 ?>94 ?> 95 95 96 96 <div class="notice notice-error is-dismissible"> 97 97 <p>WooCommerce is not activated, please activate it to use <b>Kitcart Plugin</b></p> 98 98 </div> 99 <?php99 <?php 100 100 101 101 } … … 106 106 add_action('activated_plugin', 'kitcart_activated_action'); 107 107 register_uninstall_hook(__FILE__, 'uninstall_kitcart'); 108 // register deactivation hook 109 register_deactivation_hook(__FILE__, 'deactivate_kitcart'); 110 111 // kitcart plugin action function to report plugin activation 112 function kitcart_plugin_action($action) 113 { 114 $route = 'action'; 115 $body = array( 116 117 'public_key' => get_option('kitcart_public_key'), 118 'secret_key' => get_option('kitcart_secret_key'), 119 'action' => $action, 120 ); 121 122 $req_args = array( 123 'body' => $body, 124 'timeout' => '60', 125 'redirection' => '5', 126 'httpversion' => '1.0', 127 'blocking' => true, 128 'headers' => array( 129 'accept' => "application/json", 130 ), 131 'cookies' => array( 132 'XSRF-TOKEN' => 'woocommerce-api-request', 133 'kitcart_session' => 'woocommerce' 134 ), 135 ); 136 try { 137 $response = wp_remote_post(KITCART_API_URI . $route, $req_args); 138 $body = wp_remote_retrieve_body($response); 139 $body = json_decode($body); 140 if ($body->status == 'success') { 141 return true; 142 } else { 143 return false; 144 } 145 } catch (\Throwable $th) { 146 return false; 147 } 148 } 149 150 function submit_all_successful_orders() 151 { 152 // empty array to store all successful orders 153 $all_order_data = array(); 154 //route 155 $route = 'submit-all-successful-orders'; 156 // get all successful orders 157 $args = array( 158 'post_type' => 'shop_order', 159 'post_status' => 'wc-processing', 160 'posts_per_page' => -1, 161 ); 162 $orders = get_posts($args); 163 // loop through orders 164 foreach ($orders as $order) { 165 $order_id = $order->ID; 166 $order = wc_get_order($order_id); 167 $items = $order->get_items(); 168 $items_data = []; 169 foreach ($items as $item_key => $item) { 170 $items_data[] = $item->get_data(); 171 } 172 173 $order_data = array( 174 'order_id' => $order_id, 175 'product_data' => $items_data, 176 'order_data' => $order->get_data(), 177 ); 178 $all_order_data[] = $order_data; 179 } 180 181 $body = array( 182 'public_key' => get_option('kitcart_public_key'), 183 'secret_key' => get_option('kitcart_secret_key'), 184 'orders' => $all_order_data, 185 ); 186 $req_args = array( 187 'body' => $body, 188 'timeout' => '300', 189 'redirection' => '5', 190 'httpversion' => '1.0', 191 'blocking' => true, 192 'headers' => array( 193 'accept' => "application/json", 194 ), 195 'cookies' => array( 196 'XSRF-TOKEN' => 'woocommerce-api-request', 197 'kitcart_session' => 'woocommerce' 198 ), 199 ); 200 $response = wp_remote_post(KITCART_API_URI . $route, $req_args); 201 } 108 202 109 203 // action functions and datas … … 111 205 { 112 206 //value to be change on first update 113 add_option('kitcart_redirect_to_settings', 'yes'); 207 if (get_option('kitcart_secret_key') && get_option('kitcart_public_key')) { 208 kitcart_plugin_action('activate'); 209 submit_all_successful_orders(); 210 } else { 211 212 add_option('kitcart_redirect_to_settings', 'yes'); 213 } 114 214 } 115 215 116 216 //redirect after activation to settings page 117 function kitcart_activated_action( $plugin ) { 217 function kitcart_activated_action($plugin) 218 { 118 219 //if the user has updated secret keys before, the value will be NO 119 if( get_option('kitcart_redirect_to_settings') === 'yes') { 120 if( $plugin == plugin_basename( __FILE__ ) ) { 121 exit( wp_redirect( admin_url( '/admin.php?page=wc-settings&tab=kitcart' ) ) ); 122 } 123 } 124 220 if (get_option('kitcart_redirect_to_settings') === 'yes') { 221 if ($plugin == plugin_basename(__FILE__)) { 222 exit(wp_redirect(admin_url('/admin.php?page=wc-settings&tab=kitcart'))); 223 } 224 } 225 } 226 227 function deactivate_kitcart() 228 { 229 // let kitcart know that the plugin is deactivated 230 kitcart_plugin_action('deactivate'); 125 231 } 126 232 … … 128 234 function uninstall_kitcart() 129 235 { 236 130 237 if (get_option('kitcart_secret_key')) { 131 delete_option('kitcart_redirect_to_settings', 'yes'); 132 delete_option('kitcart_secret_key'); 133 delete_option('kitcart_public_key'); 238 // delete all the values 239 if (kitcart_plugin_action('uninstall')) { 240 delete_option('kitcart_public_key'); 241 delete_option('kitcart_secret_key'); 242 delete_option('kitcart_redirect_to_settings'); 243 } else { 244 return; 245 } 134 246 } 135 247 } … … 157 269 update_option('kitcart_public_key', sanitize_text_field($_POST['kitcart_public_key'])); 158 270 update_option('kitcart_secret_key', sanitize_text_field($_POST['kitcart_secret_key'])); 271 //make kitcart api call to update the keys 272 if (kitcart_plugin_action('activate')) { 273 $message = '<div class="notice notice-success is-dismissible"><p>' . __('Your keys have been updated successfully', 'woocommerce') . '</p></div>'; 274 } else { 275 $message = '<div class="notice notice-error is-dismissible"><p>' . __('Your keys could not be updated', 'woocommerce') . '</p></div>'; 276 } 159 277 } else { 160 278 update_option('kitcart_redirect_to_settings', 'no'); 161 279 add_option('kitcart_public_key', sanitize_text_field($_POST['kitcart_public_key'])); 162 280 add_option('kitcart_secret_key', sanitize_text_field($_POST['kitcart_secret_key'])); 163 } 164 } 165 $public_key = get_option('kitcart_public_key') ? get_option('kitcart_public_key') : ""; 166 $secret_key = get_option('kitcart_secret_key') ? get_option('kitcart_secret_key') : ''; 167 168 // Styling the table a bit 169 ?> 281 //make kitcart api call to create record of all successful orders 282 submit_all_successful_orders(); 283 // make kitcart aware of the activation 284 if (kitcart_plugin_action('activate')) { 285 $message = '<div class="notice notice-success is-dismissible"><p>' . __('Your keys have been updated successfully', 'woocommerce') . '</p></div>'; 286 } else { 287 $message = '<div class="notice notice-error is-dismissible"><p>' . __('Your keys could not be updated', 'woocommerce') . '</p></div>'; 288 } 289 } 290 } else { 291 $message = '<div class="notice notice-error is-dismissible"><p>' . __('Please enter valid keys', 'woocommerce') . '</p></div>'; 292 } 293 $public_key = get_option('kitcart_public_key') ?? ""; 294 $secret_key = get_option('kitcart_secret_key') ?? ''; 295 echo $message 296 297 ?> 298 299 300 170 301 <h2>Kitcart API Keys</h2> 171 302 <table class="form-table"> … … 189 320 </table> 190 321 191 <?php322 <?php 192 323 } 193 324 194 325 // The Final Order Sending part 195 add_action('woocommerce_thankyou', 'create_new_order_on_kitcart', 10); 326 add_action('woocommerce_payment_complete', 'create_new_order_on_kitcart', 10); 327 add_action('woocommerce_order_status_completed', 'create_new_order_on_kitcart', 10); 328 add_action('woocommerce_order_status_processing', 'create_new_order_on_kitcart', 10); 329 add_action('woocommerce_order_status_on-hold', 'create_new_order_on_kitcart', 10); 330 add_action('woocommerce_order_status_pending', 'create_new_order_on_kitcart', 10); 331 add_action('woocommerce_order_status_failed', 'create_new_order_on_kitcart', 10); 332 add_action('woocommerce_order_status_cancelled', 'create_new_order_on_kitcart', 10); 333 add_action('woocommerce_order_status_refunded', 'create_new_order_on_kitcart', 10); 334 add_action('woocommerce_order_status_processing_to_completed', 'create_new_order_on_kitcart', 10); 335 add_action('woocommerce_order_status_completed_to_processing', 'create_new_order_on_kitcart', 10); 336 add_action('woocommerce_order_status_completed_to_on-hold', 'create_new_order_on_kitcart', 10); 337 add_action('woocommerce_order_status_completed_to_pending', 'create_new_order_on_kitcart', 10); 196 338 function create_new_order_on_kitcart($order_id) 197 339 { 340 $route = "create-order"; 198 341 199 342 if (class_exists('woocommerce')) { … … 228 371 ); 229 372 230 $response = wp_remote_post(KITCART_API_URI, $req_args); 231 $body = wp_remote_retrieve_body($response); 232 } 233 } 373 $response = wp_remote_post(KITCART_API_URI . $route, $req_args); 374 } 375 }
Note: See TracChangeset
for help on using the changeset viewer.