示例#1
0
 public static function checkForConnection($api_key)
 {
     global $wpdb;
     // Start - Saving API Key and Connecting to Server
     # On Save button clicking , it should be saved and connected as well.
     $LAIDKey = trim($api_key);
     $testMode = get_option('linksync_test');
     if (isset($testMode) && $testMode == 'on') {
         $testMode = 'on';
     } else {
         $testMode = 'off';
     }
     update_option('linksync_test', $testMode);
     $apicall = new linksync_class($LAIDKey, $testMode);
     $result = $apicall->testConnection();
     if (isset($result) && !empty($result)) {
         if (isset($result['errorCode']) && !empty($result['userMessage'])) {
             mysql_query("UPDATE `" . $wpdb->prefix . "linksync_laidKey` SET status='Invalid' WHERE api_key='{$LAIDKey}'");
             update_option('linksync_status', "Inactive");
             update_option('linksync_last_test_time', current_time('mysql'));
             update_option('linksync_connected_url', "");
             update_option('linksync_connectedto', '');
             update_option('linksync_connectionwith', '');
             update_option('linksync_addedfile', '');
             update_option('linksync_frequency', $result['userMessage']);
             linksync_class::add('checkAPI Key', 'Fail', $result['userMessage'], $LAIDKey);
             $class1 = 'updated';
             $class2 = 'error';
             $response['error'] = 'Connection Not Established because of ' . $result['userMessage'];
         } else {
             if (isset($result['app']) && !empty($result['app'])) {
                 $app_name = self::appid_app($result['app']);
                 if (isset($app_name) && !empty($app_name['success'])) {
                     update_option('linksync_connectionwith', $app_name['success']);
                     $app_name_status = 'Active';
                 } else {
                     update_option('linksync_connectionwith', 'Supplied API Key not valid');
                     $checkKey = 'Supplied API Key not valid';
                     $app_name_status = 'Inactive';
                 }
             }
             if (isset($result['connected_app']) && !empty($result['connected_app'])) {
                 $connected_app = self::appid_app($result['connected_app']);
                 if (isset($connected_app) && !empty($connected_app['success'])) {
                     update_option('linksync_connectedto', $connected_app['success']);
                     $status = 'Active';
                 } else {
                     update_option('linksync_connectedto', "The supplied API Key is not valid for use with linksync for WooCommerce.");
                     $checkKey = 'Supplied API Key not valid for use with WooCommerce';
                     $status = 'Inactive';
                 }
             }
             if (isset($status) && isset($app_name_status) && $status == 'Active' && $app_name_status == 'Active') {
                 // if (isset($result['connected_app_version']) && !empty($result['connected_app_version'])) {woocommerce/woocommerce.php
                 $plugin_file = dirname(__FILE__) . '/linksync.php';
                 $plugin_data = get_plugin_data($plugin_file, $markup = true, $translate = true);
                 $linksync_version = $plugin_data['Version'];
                 update_option('linksync_version', $linksync_version);
                 $webhook = $apicall->webhookConnection(content_url() . '/plugins/linksync/update.php?c=' . get_option('webhook_url_code'), $linksync_version, 'no');
                 if (isset($webhook) && !empty($webhook)) {
                     if (isset($webhook['result']) && $webhook['result'] == 'success') {
                         linksync_class::add('WebHookConnection', 'success', 'Connected to a file ' . content_url() . '/plugins/linksync/update.php?c=' . get_option('webhook_url_code'), $LAIDKey);
                         update_option('linksync_addedfile', '<a href="' . content_url() . '/plugins/linksync/update.php?c=' . get_option('webhook_url_code') . '">' . content_url() . '/linksync/update.php?c=' . get_option('webhook_url_code') . '</a>');
                     }
                 }
                 //}
                 if (isset($result['time']) && !empty($result['time'])) {
                     $server_response = strtotime($result['time']);
                     $server_time = time();
                     $time = $server_response - $server_time;
                     linksync_class::add('Time Offset', 'success', 'Server Response:' . $result['time'] . ' Current Server Time:' . date("Y-m-d H:i:s") . ' Time Offset: ' . date("H:i:s", abs($time)), $LAIDKey);
                     update_option('linksync_time_offset', $time);
                 }
                 if (get_option('linksync_connectionwith') == 'Vend' || get_option('linksync_connectedto') == 'Vend') {
                     // Add Default setting into DB
                     $response_outlets = $apicall->linksync_getOutlets();
                     if (@get_option('ps_outlet') == 'on') {
                         #VEND TO WC
                         if (isset($response_outlets) && !empty($response_outlets)) {
                             if (isset($response_outlets['errorCode']) && !empty($response_outlets['userMessage'])) {
                                 update_option('ps_outlet_details', 'off');
                                 $response_ = $response_outlets['userMessage'];
                                 linksync_class::add('linksync_getOutlets', 'fail', $response_, $LAIDKey);
                             } else {
                                 foreach ($response_outlets['outlets'] as $key => $value) {
                                     $oulets["{$key}"] = $value['id'];
                                 }
                                 $ouletsdb = implode('|', $oulets);
                                 update_option('ps_outlet_details', $ouletsdb);
                                 update_option('ps_outlet', 'on');
                             }
                         } else {
                             $class2 = 'updated';
                             $class1 = 'error';
                             $response_ = 'Error in getting outlets';
                             echo "<br>";
                             echo "<span style='color:red';>" . $response_ . "</span>";
                             echo "<br>";
                         }
                     }
                     if (@get_option('ps_wc_to_vend_outlet') == 'on') {
                         if (isset($response_outlets['errorCode']) && !empty($response_outlets['userMessage'])) {
                             update_option('wc_to_vend_outlet_detail', 'off');
                             $response_ = $response_outlets['userMessage'];
                             linksync_class::add('linksync_getOutlets', 'fail', $response_, $LAIDKey);
                         } else {
                             $two_way_wc_to_vend = $response_outlets['outlets'][0]['name'] . '|' . $response_outlets['outlets'][0]['id'];
                             update_option('wc_to_vend_outlet_detail', $two_way_wc_to_vend);
                             update_option('ps_wc_to_vend_outlet', 'on');
                         }
                     }
                     /*
                      * display_retail_price_tax_inclusive(0 or 1)
                      */
                     $vend_config = $apicall->getVendConfig();
                     if (isset($vend_config) && !empty($vend_config)) {
                         if (!isset($vend_config['errorCode'])) {
                             update_option('linksync_tax_inclusive', $vend_config['display_retail_price_tax_inclusive']);
                         } else {
                             update_option('linksync_tax_inclusive', '');
                             echo "<span style='color:red;font-weight:bold;'>Error in getting VEND Config : {$vend_config['userMessage']}</span><br>";
                         }
                     }
                 }
                 mysql_query("UPDATE `" . $wpdb->prefix . "linksync_laidKey` SET status='connected' WHERE api_key='{$LAIDKey}'");
                 update_option('linksync_status', 'Active');
                 update_option('linksync_last_test_time', current_time('mysql'));
                 update_option('linksync_connected_url', get_option('linksync_connected_url'));
                 update_option('linksync_frequency', 'Valid API Key');
                 update_option('laid_message', isset($result['message']) ? $result['message'] : null);
                 linksync_class::add('isConnected', 'success', 'Connected URL is ' . get_option('linksync_connected_url'), $LAIDKey);
                 $class2 = 'updated';
                 $class1 = 'error';
                 update_option('linksync_laid', $LAIDKey);
                 $response['success'] = 'Connection is established Successfully!!';
             } else {
                 mysql_query("UPDATE `" . $wpdb->prefix . "linksync_laidKey` SET status='Invalid' WHERE api_key='{$LAIDKey}'");
                 update_option('linksync_status', "Inactive");
                 update_option('linksync_last_test_time', current_time('mysql'));
                 update_option('linksync_connected_url', get_option('linksync_connected_url'));
                 update_option('linksync_addedfile', '');
                 update_option('linksync_connectedto', "The supplied API Key is not valid for use with linksync for WooCommerce.");
                 update_option('linksync_connectionwith', 'Supplied API Key not valid');
                 update_option('linksync_frequency', 'Invalid API Key');
                 linksync_class::add('checkAPI Key', 'fail', 'Invalid API Key', '-');
                 $class1 = 'updated';
                 $class2 = 'error';
                 $response['error'] = "The supplied API Key is not valid for use with linksync for WooCommerce.";
             }
         }
     } else {
         mysql_query("UPDATE `" . $wpdb->prefix . "linksync_laidKey` SET status='Invalid' WHERE api_key='{$LAIDKey}'");
         update_option('linksync_status', "Inactive");
         update_option('linksync_last_test_time', current_time('mysql'));
         update_option('linksync_connected_url', get_option('linksync_connected_url'));
         update_option('linksync_addedfile', '');
         update_option('linksync_connectedto', "The supplied API Key is not valid for use with linksync for WooCommerce.");
         update_option('linksync_connectionwith', 'Supplied API Key not valid');
         update_option('linksync_frequency', 'Invalid API Key');
         linksync_class::add('checkAPI Key', 'fail', 'Invalid API Key', '-');
         $class1 = 'updated';
         $class2 = 'error';
         $response['error'] = "The supplied API Key is not valid for use with linksync for WooCommerce.";
     }
     return $response;
 }