Plugin Directory

Changeset 2010190


Ignore:
Timestamp:
01/10/2019 07:24:49 PM (7 years ago)
Author:
ShopSite
Message:

Switch cURL calls to WP HTTP API. version is now 1.5.7

Location:
shopsite-plugin/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • shopsite-plugin/trunk/oauth.php

    r1079131 r2010190  
    1818  $nonce2 = mt_rand(10000000,99999999); # nonce for download request
    1919  $timestamp = time(); # in UNIX time
    20  
    2120
    2221  # create the data string for the authorization request and get length for Content-Length
    2322  $request = "grant_type=authorization_code&code=$code&client_credentials=$encodedcredentials&signature=$signature1";
    2423  $length = strlen($request);
    25  
    2624
     25  $response = wp_remote_post($authorizationurl, array(
     26    'headers' => array('Content-Type: application/x-www-form-urlencoded', "Content-Length: $length"),
     27    'body'    => $request,
     28    'timeout' => 30
     29  ));
    2730
     31  if (wp_remote_retrieve_response_code($response) != 200) {
     32    return array('success'=>false, 'error'=>'OAUTH call failed. Error: '.wp_remote_retrieve_response_message($response));
     33  } else
     34    $json = wp_remote_retrieve_body($response);
    2835
    29   $ch = curl_init();
    30   curl_setopt($ch, CURLOPT_URL, $authorizationurl);
    31  
    32   curl_setopt($ch, CURLOPT_HEADER, false);
    33   curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    34     "Content-Type: application/x-www-form-urlencoded",
    35     "Content-Length: $length"
    36   ));
    37   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    38   curl_setopt($ch, CURLOPT_POST, true);
    39   curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
    40  
    41   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verify_peer);
    42   curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    43  
    44   $json = curl_exec($ch);
    45  
    46  
    47   if ($json == false) {
    48     return array('success'=>false, 'error'=>'Curl OAUTH call failed. Curl error: '.curl_error($ch));
    49   }
    50  
    51   curl_close($ch);
    52  
    5336  //debug_print($json);
    54  
    55  
     37
    5638  $json = json_decode($json, true);
    5739  if (!is_array($json))
    5840    return array('success'=>false, 'error'=>'Malformed response or bad URL');
    59  
     41
    6042  if (array_key_exists("error", $json)) {
    6143    return array('success'=>false, 'error'=>"OAUTH request error: ".$json['error_description']);
    6244  }
    63  
    64   //var_dump($json);
    6545
    6646  switch ($request_type) {
    6747    case DOWNLOAD:
    6848      $endpointurl = $json['download_url'];
    69      
    7049      break;
    7150    case UPLOAD:
     
    7756  }
    7857  //debug_print("endpoint: ".$endpointurl);
    79  
     58
    8059  $url_stuff = parse_url($endpointurl);
    8160  $endpoint = $url_stuff['path'];
     
    8564  else
    8665    $port = 80;
    87  
    88 
    8966
    9067  # get query parameters into array, and sort alphabetically ascending (not up to spec)
    9168  $data = $options;
    92   /*if ($request_type == UPLOAD) {
    93     if (!array_key_exists('filename', $data))
    94       $data['filename'] = $options['dbname'].".xml";
    95   }*/
    9669  ksort($data);
    97  
     70
    9871  # should also percent encode names and values according to http://tools.ietf.org/html/draft-hammer-oauth-v2-mac-token-02#section-3.3.1.1
    9972  $token = $json['access_token'];
     
    11487  $data['timestamp']=$timestamp;
    11588  $data['nonce']=$nonce2;
    116  
     89
     90  if ($request_type == UPLOAD && !array_key_exists('filename', $data)) {
     91    $data['UploadFile'] = "@$xml_file;type=text/xml";
     92  }
     93
    11794  $db_request = "";
    11895  foreach ($data as $k=>$v) {
     
    12299
    123100  //debug_print("db_request: $db_request");
    124  
    125   $ch = curl_init();
    126   curl_setopt($ch, CURLOPT_URL, $endpointurl);
    127  
    128   curl_setopt($ch, CURLOPT_HEADER, false);
    129   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    130   curl_setopt($ch, CURLOPT_POST, true);
    131  
    132   if ($request_type != UPLOAD)
    133     curl_setopt($ch, CURLOPT_POSTFIELDS, $db_request);
    134   else {
    135     if (array_key_exists('filename', $data)) {
    136       curl_setopt($ch, CURLOPT_POSTFIELDS, $db_request);
    137     } else {
    138       $data['UploadFile'] = "@$xml_file;type=text/xml";
    139       curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    140     }
     101
     102  $response = wp_remote_post($endpointurl, array('body' => $db_request));
     103  if (wp_remote_retrieve_response_code($response) != 200) {
     104    return array('success'=>false, 'error'=>'Data request failed. Error: '.wp_remote_retrieve_response_message($response));
    141105  }
    142  
    143   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verify_peer);
    144   curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    145  
    146   $downloaddata = curl_exec($ch);
    147   if (!$downloaddata) {
    148     return array('success'=>false, 'error'=>'Curl data call failed. Curl error: '.curl_error($ch));
    149   }
    150  
    151   curl_close($ch);
    152  
    153   //debug_print($downloaddata);
     106  $downloaddata = wp_remote_retrieve_body($response);
     107
    154108  //debug_print("downloaddata: $downloaddata");
    155  
     109
    156110  if (($json2 = json_decode($downloaddata, true)) != NULL) {
    157111    if (array_key_exists('error', $json2)) {
     
    174128    $timestamp3 = time();
    175129    //$db_request = $downloaddata."&token=$token&timestamp=$timestamp3&nonce=$nonce3";
    176    
     130
    177131    $pieces = explode("&", $downloaddata);
    178132    sort($pieces);
    179133    $imploded = implode("\n",$pieces);
    180    
     134
    181135    $url_stuff = parse_url($endpointurl);
    182136    $endpoint = $url_stuff['path'];
     
    186140    else
    187141      $port = 80;
    188    
     142
    189143    $macdigest = "$token\n$timestamp3\n$nonce3\n\nPOST\n$domain\n$port\n$endpoint\n$imploded\n";
    190144    $macdigesthash = hash_hmac("sha1", $macdigest, $secretkey, true);
    191145    $signature3 = base64_encode($macdigesthash);
    192146    $db_request = $downloaddata."&token=$token&timestamp=$timestamp3&nonce=$nonce3&signature=$signature3";
    193    
    194    
    195      
    196     $ch = curl_init();
    197     curl_setopt($ch, CURLOPT_URL, $endpointurl);
    198     curl_setopt($ch, CURLOPT_HEADER, false);
    199     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    200     curl_setopt($ch, CURLOPT_POST, true);   
    201     curl_setopt($ch, CURLOPT_POSTFIELDS, $db_request);
    202     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verify_peer);
    203     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    204     $downloaddata = curl_exec($ch);
    205    
    206    
    207    
    208     if (!$downloaddata) {
    209       return array('success'=>false, 'error'=>'Curl dbmake call failed. Curl error: '.curl_error($ch));
     147
     148    $response = wp_remote_post($endpointurl, array('body' => $db_request));
     149    if (wp_remote_retrieve_response_code($response) != 200) {
     150      return array('success'=>false, 'error'=>'DBmake request failed. Error: '.wp_remote_retrieve_response_message($response));
    210151    }
    211    
     152    $downloaddata = wp_remote_retrieve_body($response);
     153
    212154    if (($json2 = json_decode($downloaddata, true)) != NULL) {
    213155      if (array_key_exists('error', $json2)) {
     
    215157      }
    216158    }
    217    
    218     curl_close($ch);
    219    
    220      
    221    
     159
    222160    return array('success'=>true);
    223161  }
    224  
     162
    225163  if ($request_type == DOWNLOAD) {
    226164    return array('success'=>true, 'data'=>$downloaddata);
    227165  }
    228  
     166
    229167  if ($request_type == PUBLISH) {
    230168    return array('success'=>true);
     
    232170}
    233171
    234 ?>
  • shopsite-plugin/trunk/shopsite.php

    r1707265 r2010190  
    22/**
    33 * @package ShopSite
    4  * @version 1.5.6
     4 * @version 1.5.7
    55 */
    66/*
     
    99Description: ShopSite plugin to put products into your WordPress blog
    1010Author: ShopSite
    11 Version: 1.5.6
     11Version: 1.5.7
    1212Author URI: http://shopsite.com/
    13 */
    14 
    15 
     13 */
    1614
    1715if (isset($_REQUEST['ss_action'])) {
     
    2523  }
    2624  require_once(WP_LOAD_PATH.'wp-load.php');
    27  
    28   //echo "<script>alert('".$_REQUEST['shopsite_url']."')</script>";
    29   ///debug_print("Called with action ".$_REQUEST['ss_action']);
     25
     26  //debug_print("Called with action ".$_REQUEST['ss_action']);
    3027  if ($_REQUEST['ss_action'] == 'insert')
    3128    show_search_form();
     
    3734}
    3835
    39 
    40 
    4136$product_list;
    4237$wp_id;
    43 
    4438
    4539register_uninstall_hook(__FILE__, 'on_uninstall');
     
    5852
    5953function load_plugin() {
    60     $version = "1.5.6";
    61     if ( is_admin() ) { 
    62       $running_version = get_option('ss_version');
    63       if (!$running_version)
    64         $running_version = "1";
    65        
    66       update_option('ss_version', $version);
    67       if ($running_version < "1.5.1") {
    68        
    69         //updating, rename to ss_ options to prevent conflicts with other plugins
    70         {
    71           $option_list = array('shopsite_url', 'config_dump', 'config_type', 'clientid', 'secretkey', 'code', 'authorizationurl', 'identifier', 'remember_search', 'remembered_search_string', 'media_url');
    72           foreach ($option_list as $option) {
    73             $option_val = get_option($option);
    74             if ($option_val) {
    75               update_option('ss_'.$option, $option_val);
    76               delete_option($option);
    77             } else
    78               add_option('ss_'+$option);
    79           }
    80         }
     54  $version = "1.5.7";
     55  if ( is_admin() ) { 
     56    $running_version = get_option('ss_version');
     57    if (!$running_version)
     58      $running_version = "1";
     59
     60    update_option('ss_version', $version);
     61    if ($running_version < "1.5.1") {
     62          //updating, rename to ss_ options to prevent conflicts with other plugins
     63      $option_list = array('shopsite_url', 'config_dump', 'config_type', 'clientid', 'secretkey', 'code', 'authorizationurl', 'identifier', 'remember_search', 'remembered_search_string', 'media_url');
     64      foreach ($option_list as $option) {
     65        $option_val = get_option($option);
     66        if ($option_val) {
     67          update_option('ss_'.$option, $option_val);
     68          delete_option($option);
     69        } else
     70          add_option('ss_'+$option);
    8171      }
    82      
    83       if (get_option('ss_just_activated')) {
    84         delete_option( 'ss_just_activated' );
    85         $shopsite_url = get_option('ss_shopsite_url');
    86         if ($shopsite_url == false || strlen($shopsite_url) < 10) {
    87           add_action( 'admin_head', 'start_tutorial');
    88         }
     72    }
     73
     74    if (get_option('ss_just_activated')) {
     75      delete_option( 'ss_just_activated' );
     76      $shopsite_url = get_option('ss_shopsite_url');
     77      if ($shopsite_url == false || strlen($shopsite_url) < 10) {
     78        add_action( 'admin_head', 'start_tutorial');
    8979      }
    90 
    91      
    9280    }
     81  }
    9382}
    9483
     
    10089  echo "<script>request_output_url();</script>"; 
    10190}
    102 
    103 /*function link_jquery() {
    104   $jquery_url = includes_url()."js/jquery/jquery.js";
    105   //echo "<script type='text/javascript' src='".$jquery_url."'></script>";
    106   echo "<script>
    107     if (typeof $ !== \"undefined\")  jQuery = $;
    108     if (typeof jQuery !== \"undefined\")  $ = jQuery;
    109     </script>";
    110 }*/
    11191
    11292function link_tutorial() {
     
    11595  echo "<script type='text/javascript' src=".plugin_dir_url(__FILE__)."tutorial_driver.js></script>";
    11696  echo "<link rel='stylesheet' href=".plugin_dir_url(__FILE__)."shopsite.css type='text/css' />";
    117  
    11897}
    11998
     
    121100  echo "<script>var ss_path='".plugin_dir_url(__FILE__)."';</script>";
    122101}
    123 
    124102
    125103//add_action( 'admin_enqueue_scripts', 'link_jquery' );
     
    136114
    137115function add_shopsite_menu() {
    138     add_submenu_page( "options-general.php", "ShopSite", "ShopSite", "manage_options", "shopsite_menu", "show_shopsite_menu" );
     116  add_submenu_page( "options-general.php", "ShopSite", "ShopSite", "manage_options", "shopsite_menu", "show_shopsite_menu" );
    139117}
    140118
    141119function show_shopsite_menu() {
    142     global $wpdb;
     120  global $wpdb;
    143121  include_once "oauth.php";
    144122  $testing = false;
    145123  $state = 'new_config';
    146  
     124
    147125  /*$option_list = array('shopsite_url', 'config_dump', 'config_type', 'clientid', 'secretkey', 'code', 'authorizationurl', 'identifier', 'remember_search', 'remembered_search_string', 'media_url');
    148126  foreach ($option_list as $option) {
    149127    echo "<br>$option  |".get_option($option)."| |".get_option('ss_'.$option)."|";
    150128  }*/
    151  
    152  
     129
     130
    153131  if (isset($_REQUEST['config_type'])) {
    154  
     132
    155133    $config_type = trim($_REQUEST['config_type']);
    156134    //echo "<script>alert('$config_type');</script>";
     
    158136    update_option('ss_config_type', $config_type);
    159137    $state = 'settings_saved';
    160    
     138
    161139    delete_option('ss_media_url');
    162    
     140
    163141    if ($config_type == 'ss_12') {
    164142      update_option('ss_config_dump', trim($_REQUEST['config_dump']));
    165      
    166      
     143
     144
    167145      $decoded = base64_decode(trim($_REQUEST['config_dump']));
    168146      $decoded = explode('^',$decoded);
    169      
     147
    170148      update_option('ss_clientid', trim($decoded[0]));
    171149      update_option('ss_secretkey', trim($decoded[1]));
     
    174152      update_option('ss_shopsite_url', trim($decoded[4]));
    175153    } else {
    176      
     154
    177155      $clientid = trim($_REQUEST['clientid']); update_option('ss_clientid', $clientid);
    178156      $secretkey = trim($_REQUEST['secretkey']); update_option('ss_secretkey', $secretkey);
     
    184162    }
    185163  }
    186    
    187     /*if (isset($_REQUEST['shopsite_url'])) { update_option('shopsite_url', trim($_REQUEST['shopsite_url'])); $state = 'settings_saved';}
    188   if (isset($_REQUEST['clientid'])) update_option('clientid', trim($_REQUEST['clientid']));
    189   if (isset($_REQUEST['secretkey'])) update_option('secretkey', trim($_REQUEST['secretkey']));
    190   if (isset($_REQUEST['code'])) update_option('code', trim($_REQUEST['code']));
    191   if (isset($_REQUEST['authorizationurl'])) update_option('authorizationurl', trim($_REQUEST['authorizationurl']));*/
    192  
     164
    193165  if (isset($_REQUEST['identifier'])) update_option('ss_identifier', trim($_REQUEST['identifier']));
    194166  if (isset($_REQUEST['test'])) {
     
    197169    $state = 'testing_completed';
    198170  }
    199  
     171
    200172  $config_type = get_option('ss_config_type');
    201173  if (strlen($config_type) == 0)
    202174    $config_type = 'ss_12';
    203     $config_dump = get_option('ss_config_dump');
    204     $shopsite_url = get_option('ss_shopsite_url');
     175  $config_dump = get_option('ss_config_dump');
     176  $shopsite_url = get_option('ss_shopsite_url');
    205177  $clientid = get_option('ss_clientid');
    206178  $secretkey = get_option('ss_secretkey');
    207179  $code = get_option('ss_code');
    208180  $authorizationurl = get_option('ss_authorizationurl');
    209  
    210  
     181
     182
    211183  $identifier = get_option('ss_identifier');
    212  
     184
    213185  $SKU_selected = $GUID_selected = "";
    214186  if ($identifier == 'SKU')
     
    216188  else
    217189    $GUID_selected = "checked";
    218    
     190
    219191  $ss_12_extra = "";
    220192  $ss_11_extra = "";
     
    224196  }
    225197
    226 //ss_action=plugins.php?page=shopsite_menu
     198  //ss_action=plugins.php?page=shopsite_menu
    227199  echo
    228200    "<script>
    229    
     201
    230202    \$('#ss_11').live('click', function() {\$('#config_type').val('ss_11'); \$('#ss_12_settings').css({'display':'none'}); \$('#ss_11_settings').css({'display':'table-row-group'}); });
    231     \$('#ss_12').live('click', function() {\$('#config_type').val('ss_12'); \$('#ss_11_settings').css({'display':'none'}); \$('#ss_12_settings').css({'display':'table-row-group'}); });
     203  \$('#ss_12').live('click', function() {\$('#config_type').val('ss_12'); \$('#ss_11_settings').css({'display':'none'}); \$('#ss_12_settings').css({'display':'table-row-group'}); });
    232204    </script>";
    233205
    234     echo   
     206  echo 
    235207    "<h1>ShopSite configuration</h1>
    236     Don't have a ShopSite store? <a id=get_shopsite target=_blank href='http://saas.shopsite.com/express/'>Get a free 10-product Express store</a>.
    237         <form method=post>
     208    Don't have a ShopSite store? <a id=get_shopsite target=_blank href='https://saas.shopsite.com/express/'>Get a free 10-product Express store</a>.
     209    <form method=post>
    238210    <input type=hidden id=config_type name=config_type value=$config_type>
    239211    <table>
    240212    <thead><tr><th colspan=2>Application settings</th></tr></thead>";
    241  
     213
    242214  echo
    243215    "<tbody id='ss_12_settings' $ss_12_extra><tr><td>Configuration data (paste from ShopSite)
    244216    <br><a id=ss_11>Click here if you have 5 fields to copy and paste in your ShopSite backoffice WordPress config</a></td>
    245217    <td><textarea name=config_dump id=config_dump style='height:100px;width:675px;'>$config_dump</textarea></td></tr></tbody>";
    246  
    247    
     218
     219
    248220  echo
    249221    "<tbody id='ss_11_settings' $ss_11_extra>
     
    254226    <tr><td>Authorization URL:</td><td><input type=text name=authorizationurl id=authorizationurl value='$authorizationurl' size=100></td></tr>
    255227    <tr><td>ShopSite callback URL:</td><td><input type=text name=shopsite_url value='$shopsite_url' size=100></td></tr>";
    256    
    257   /*echo
    258     "<tr id=store_url_instructions><td colspan=2>You can find Store URL under 'Preferences'->'Hosting Service'</td></tr>
    259     <tr><td>Store URL:</td><td><input type=text name=store_url value='$store_url' size=100></td></tr>";*/
    260    
     228
    261229  echo
    262230    "</tbody>";
    263     //http://eval.shopsite.com/cgi-bin/ilya/ss/preview.cgi?&storeid=*0c5125a8823496a6&image_preview=1&page=2
    264 
    265  
    266   /*echo "<tr><th colspan=2>Other settings</th></tr>*/
    267    
    268    
     231
    269232  echo "<tbody><tr><th colspan=2>Other settings</th></tr>
    270233    <tr><td>Unique product identifier:</td>
     
    274237    </tbody></table>
    275238    <br/><input type=submit name=test id=test_connection value='Test connection'>";
    276  
     239
    277240  if ($testing) {
    278241    echo "<div id=test_result>";
     
    283246    }
    284247    echo "</div>";
    285    
    286   }
    287  
     248
     249  }
     250
    288251  echo "<input type=hidden name=state id=state value=$state>";
    289252  echo "<br/><input type=submit id=save_settings value='Save settings'></form>";
    290253}
    291 
    292 /*onclick='window.open(\"".plugin_dir_url(__FILE__)."shopsite.php?ss_action=test&clientid=\"+document.forms[0].clientid.value+\"&secretkey=\"+document.forms[0].secretkey.value
    293       +\"&code=\"+document.forms[0].code.value+\"&authorizationurl=\"+document.forms[0].authorizationurl.value
    294       ,\"\",\"width=400,height=300\");' */
    295 //document.forms[0].clientid.value
    296 
    297 
    298 
    299 
    300254
    301255function shopsite_addbuttons() {
     
    303257   if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') )
    304258     return;
    305  
     259
    306260   // Add only in Rich Editor mode
    307261   if ( get_user_option('rich_editing') == 'true') {
     
    310264   }
    311265}
    312  
     266
    313267function register_shopsite_button($buttons) {
    314268   array_push($buttons, "separator", "shopsite");
    315269   return $buttons;
    316270}
    317  
     271
    318272// Load the TinyMCE plugin : editor_plugin.js (wp2.5)
    319 
    320 
    321273function add_shopsite_tinymce_plugin($plugin_array) {
    322274  $path = plugin_dir_url(__FILE__).'editor_plugin.js';
     
    324276  return $plugin_array;
    325277}
    326  
    327 
    328 
    329 
    330 
    331278
    332279function show_search_form() {
     
    334281  $tinymce_url = includes_url()."js/tinymce/tiny_mce_popup.js";
    335282  $jquery_url = includes_url()."js/jquery/jquery.js";
    336  
     283
    337284  $search_string = "*";
    338  
     285
    339286  $remember_search = "";
    340287  $selected_name = "";
     
    342289  if (get_option('ss_remember_search') == 'true') {
    343290      $remember_search = "checked";
    344      
     291
    345292    if (strlen(get_option('ss_remembered_search_string')))
    346293      $search_string = get_option('ss_remembered_search_string');
    347    
    348   }
    349  
     294  }
     295
    350296  echo "
    351297  <html>
     
    356302  <script type='text/javascript' src='".plugin_dir_url(__FILE__)."search_products.js?".time()."'></script>
    357303  <script type='text/javascript'> var ss_remembered_search_string = '".$search_string."';</script>
    358  
    359  
    360304  </head>
    361305  <body>";
    362  
    363  
    364  
    365306  echo "<div id=top_bar>";
    366307  echo "<div id=message>You can use CTRL and SHIFT keys to select multiple products.</div>"; 
     
    370311  echo "</div>";
    371312  echo "<div id=search_results></div>";
    372  
     313
    373314  $extra_space = "";
    374315  $message = "";
     
    379320    $message = "<div id=extra_message>Upgrade your store to ShopSite v12 sp1 or greater to see product images above.</div>";
    380321  }
    381  
     322
    382323  echo "<div id=\"bottom_fix\"$extra_space>$message<div id=\"insert_button\">Insert selected products into post</div></div>";
    383  
     324
    384325  echo "
    385326  </body>
     
    392333    $shopsite_url = get_option('ss_shopsite_url');
    393334    $url = $shopsite_url."&operation=get_setting&setting=output_url"; 
    394     $curl_res = curl_open($url);
    395     $outputurl = $curl_res[0];
     335    $resp = get_url($url);
     336    $outputurl = $resp[0];
    396337    if ($outputurl && strlen($outputurl) > 10) {
    397338      $media_url = $outputurl."/media/";
     
    408349  //echo ("in:|".$_REQUEST['search_string']."|<br>");
    409350  $search_string = stripslashes(trim($_REQUEST['search_string']));
    410  
    411  
     351
    412352  $remember_search = $_REQUEST['remember_search'];
    413  
     353
    414354  ///debug_print("Updating remember_search to $remember_search");
    415355  update_option('ss_remember_search', $remember_search);
    416  
     356
    417357  if ($remember_search == 'true') {
    418358    update_option('ss_remembered_search_string', $search_string);
    419359  }
    420360
    421  
     361
    422362  $shopsite_url = get_option('ss_shopsite_url');
    423363
    424  
     364
    425365  $media_url = get_media_url();
    426  
    427  
    428  
     366
    429367  $list_all = false;
    430368  if ($search_string == '*') {
     
    432370    $list_all = true;
    433371  }
    434    
    435  
     372
    436373  if (!$list_all) {
    437374    if ($media_url)
     
    440377      $search_array = array('search_on'=>"name", 'search_term'=>$search_string, 'search_filter'=>'contains');
    441378  }
    442    
     379
    443380  $products_xml = oauth(
    444381    get_option('ss_clientid'), get_option('ss_secretkey'), get_option('ss_code'), get_option('ss_authorizationurl'),
     
    446383    array_merge(array('clientApp'=>'1', 'dbname'=>'products', 'version'=>'11.2', 'fields'=>'|Product GUID|Name|SKU|Graphic|', 'limit'=>$limit), $search_array)
    447384  );
    448  
    449   debug_print(print_r(html_entity_decode($products_xml['data']),true));
    450  
     385
     386  //debug_print(print_r(html_entity_decode($products_xml['data']),true));
     387
    451388  if (!$products_xml['success']) {
    452389    echo "<div id=error_head>Unfortunately, something went wrong.</div>";
     
    466403    }
    467404  }
    468  
     405
    469406  //if media_url is absent, that means shopsite's XML API can't handle searches on multiple fields at once. We'll perform two separate searches then.
    470407  if (!$list_all && !$media_url) {
     
    483420    }
    484421  }
    485  
     422
    486423  if (count($products_ar) == 0) {
    487424    echo "<div id=no_products>No matching products.</div>";
    488425    return;
    489426  }
    490  
     427
    491428
    492429  ksort($products_ar);
    493430  $products_ar = array_slice($products_ar,0,$limit);
    494  
     431
    495432  echo "<div id=\"products\">";
    496433  if (count($products_ar) == $limit) {
     
    507444
    508445function print_product ($count, $name, $data, $media_url) {
    509  
     446
    510447  $id = $data[0];
    511448  $sku = rawurlencode($data[1]);
    512449  $image = $data[2];
    513  
     450
    514451  if (!strstr($image, "://")) {
    515452    if ($image && strstr($image,".") && $image != "no-image.png") {
     
    520457        $image = "ss_size2/".$image_ar[0];
    521458    } else
    522       $image = "ss_size2/no-image.png";
    523   }
    524  
     459      $image = false;//"ss_size2/no-image.png";
     460  }
     461
    525462  $outdated = "";
    526463  if (!$media_url)
    527464    $outdated = " outdated";
    528  
     465
    529466  echo "<div class=\"wp_product$outdated\" id=\"wp_product_$count\">";
    530   if ($media_url)
     467  if ($media_url && $image)
    531468    if (strstr($image, "://"))
    532       echo "<img class=\"product_image\" src=\"".$image."\">";
     469      echo "<img class=\"product_image\" alt=\"\" src=\"".$image."\">";
    533470    else
    534       echo "<img class=\"product_image\" src=\"".$media_url.$image."\">";
     471      echo "<img class=\"product_image\" alt=\"\" src=\"".$media_url.$image."\">";
    535472  if (strlen($sku) > 0)
    536473    echo "<div class='product_sku'>".strip_tags($sku)."</div>";
    537474  echo "<a class=\"product_name\" title=\"".str_replace("\"","&quot;",$name)."\">".strip_tags($name)."</a>";
    538  
     475
    539476  echo '<input type=hidden class="name_input" name="name_input" value="'.str_replace("\"","&quot;",$name).'">';
    540477  echo '<input type=hidden class="guid_input" name="guid_input" value="'.$id.'">';
     
    547484  global $product_list, $wp_id;
    548485  ///debug_print("product_handler entered");
    549     //return "<iframe src='$content' width='100%' height='300px' ><p>IFRAME FAIL!</p></iframe>";
    550  
     486  //return "<iframe src='$content' width='100%' height='300px' ><p>IFRAME FAIL!</p></iframe>";
     487
    551488  extract( shortcode_atts( array(
    552         'id' => '',
     489    'id' => '',
    553490    'sku'=>''
    554     ), $atts ) );
    555  
     491  ), $atts ) );
     492
    556493  if (get_option('ss_identifier') == 'SKU')
    557494    $identifier = $sku;
    558495  else
    559496    $identifier = $id;
    560    
    561  
     497
     498
    562499  if ($identifier == '')
    563500    return "";
    564    
    565    
     501
     502
    566503  /*$shopsite_url = get_option('shopsite_url');   
    567504  $handle = fopen($shopsite_url."&operation=get_product&id=$id",'r');
    568     $contents = stream_get_contents($handle);*/
    569  
     505  $contents = stream_get_contents($handle);*/
     506
    570507  $wp_id++;
    571  
    572    
     508
     509
    573510  if (!isset($product_list[$identifier])) {
    574511    ///debug_print("sticking stuff into product_list for $identifier");
     
    577514  ///debug_print("sticking stuff into product_list 2");
    578515  array_push($product_list[$identifier], $wp_id);
    579  
    580  
     516
     517
    581518  //$product_list["wp".$wp_id] = $id;
    582519  return "<div class=ss_product id=product_".$wp_id."></div>";
    583  
     520
    584521  //return $contents;
    585522}
     
    593530function init_product_list() {
    594531  global $product_list;
    595  
     532
    596533  echo "<script type='text/javascript' src=".plugin_dir_url(__FILE__)."populate_products.js></script>";
    597534  $product_list = array();
     
    607544  $identifier = get_option('ss_identifier');
    608545  $id_list = implode(",",array_unique(array_keys($product_list)));
    609  
     546
    610547  //debug_print("dispatched:|$id_list|");
    611  
    612  
     548
     549
    613550  echo
    614551    "<script> var ss_path='".plugin_dir_url(__FILE__)."';
    615     var ss_identifier = \"$identifier\";
    616     var ss_product_map = $product_map;
    617     var ss_id_list = \"$id_list\";
    618     var ss_shopsite_url=\"".get_option('ss_shopsite_url')."\";</script>";
     552  var ss_identifier = \"$identifier\";
     553  var ss_product_map = $product_map;
     554  var ss_id_list = '$id_list';
     555  var ss_shopsite_url='".get_option('ss_shopsite_url')."';</script>";
    619556  //echo "<script> var product_mapping = ".json_encode($product_list).";</script>";
    620557}
     
    634571  $hmachash = hash_hmac("sha1", $data, $secretkey, true);
    635572  $signature = rawurlencode(base64_encode($hmachash));
    636  
     573
    637574  $identifier = get_option('ss_identifier');
    638  
    639   //$shopsite_url = $_REQUEST['shopsite_url'];   
     575
    640576  $shopsite_url = get_option('ss_shopsite_url');
    641   //$id_list = "\"".str_replace(",","\",\"",$id_list)."\"";
    642577  $url = $shopsite_url."&operation=get_products&".$identifier."_list=".$id_list."&signature=".$signature;
    643578  //debug_print("DATA URL |$url|");
    644   /*$url_openable = ini_get('allow_url_fopen');
    645   ini_set('allow_url_fopen', true);
    646   $handle = fopen($url,'r');
    647   ini_set('allow_url_fopen', $url_openable);
    648     print(stream_get_contents($handle));*/
    649   $curl_res = curl_open($url);
    650   $product_data = $curl_res[0];
     579  $resp = get_url($url);
     580  $product_data = $resp[0];
    651581  if ($product_data)
    652582    print($product_data);
     
    656586
    657587function test_connection() {
    658 
    659   if  (!in_array  ('curl', get_loaded_extensions())) {
    660     return array("success"=>false, "error"=>"CURL PHP extension is not installed on your server. Contact your hosting provider.");
    661   }
    662 
    663588  $test_download_xml = oauth(
    664589    get_option('ss_clientid'), get_option('ss_secretkey'), get_option('ss_code'), get_option('ss_authorizationurl'),
     
    668593  if (!$test_download_xml["success"])
    669594    return $test_download_xml;
    670    
    671  
    672   $res = curl_open(get_option('ss_shopsite_url'));
     595
     596  $res = get_url(get_option('ss_shopsite_url'));
    673597  if ($res[0] == false)
    674598    return array("success"=>false, "error"=>$res[1]);
    675  
     599
    676600  return array("success"=>true);
    677601}
    678602
    679603function debug_print($text) {
    680   file_put_contents("log.txt", $text."\n", FILE_APPEND);
    681 }
    682 
    683 function curl_open($url) {
    684   $ch = curl_init();
    685   curl_setopt($ch, CURLOPT_URL, $url);
    686   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    687   curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    688   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    689   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    690   $html = curl_exec($ch);
    691   if (curl_errno($ch))
    692     $retval = array(false, curl_error($ch));
    693   else
    694     $retval = array($html);
    695 
    696   curl_close($ch);
    697      
     604  //file_put_contents("log.txt", $text."\n", FILE_APPEND);
     605}
     606
     607function get_url($url) {
     608  $response = wp_remote_get($url);
     609  if(wp_remote_retrieve_response_code($response) != 200) {
     610    $retval = array(false, wp_remote_retrieve_response_message($response));
     611  } else {
     612    $retval = array(wp_remote_retrieve_body($response));
     613  }
     614
    698615  return $retval;
    699616}
    700617
    701 ?>
Note: See TracChangeset for help on using the changeset viewer.