Advertisement
sparkweb

FoxyShop: Connection to Self-Hosted Script for Amazon S3

Nov 7th, 2012
554
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.30 KB | None | 0 0
  1. //You'll also need to include this library: http://undesigned.org.za/2007/10/22/amazon-s3-php-class
  2.  
  3. //Show New Field in Admin
  4. add_action('foxyshop_admin_product_details','my_custom_file_download_link');
  5. function my_custom_file_download_link($post_id) {
  6.  
  7.     include "S3.php";
  8.     define('S3_ACCESS_KEY_ID','xxxxxxxxxxxxx');
  9.     define('S3_SECRET_ACCESS_KEY','xxxxxxxxxxxxxx');
  10.     define('S3_URL','s3.amazonaws.com');
  11.     define('S3_BUCKET','bucket_name');
  12.  
  13.     $_download_link = get_post_meta($post_id,'_download_link',1);
  14.     $s3 = new S3(S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY);
  15.     ?>
  16.  
  17.     <div style="clear:both;"></div>
  18.     <div class="foxyshop_field_control">
  19.         <?php
  20.         //Get List of Assets From S3 Bucket
  21.         $file_list = array();
  22.         $s3 = new S3(S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY);
  23.  
  24.         if (($contents = $s3->getBucket(S3_BUCKET)) !== false) {
  25.             foreach ($contents as $fileinfo) {
  26.                 $file_list[] = $fileinfo['name'];
  27.             }
  28.         }
  29.  
  30.         ?>
  31.         <select name="_download_link" id="_download_link" style="max-width: 256px; margin-top: 6px;">
  32.         <option value="">- - Selected File - -</option>
  33.         <?php
  34.  
  35.         asort($file_list);
  36.         foreach($file_list as $filename) {
  37.             echo '<option value="'.$filename.'"' . ($_download_link == $filename ? ' selected="selected"' : "") . '>'.$filename.'</option>' . "\n";
  38.         }
  39.             ?>
  40.         </select>
  41.  
  42.     </div>
  43.     <div style="clear:both"></div>
  44.     <?php
  45.  
  46. }
  47.  
  48. //Save New Field
  49. add_action('foxyshop_save_product','my_custom_file_download_link_save');
  50. function my_custom_file_download_link_save($post_id) {
  51.     global $post_id, $wpdb;
  52.     foxyshop_save_meta_data('_download_link', $_POST['_download_link']);
  53.  
  54.     //Sync with Self Hosted Script
  55.     $table_prefix = "Foxy"; //UPDATE HERE IF YOU USED SOMETHING OTHER THAN "Foxy"
  56.  
  57.     //Get Values
  58.     $product_code = $post_id;
  59.     $product_name = get_the_title($post_id);
  60.     $price = get_post_meta($post_id,'_price',1);
  61.     $file_name = $_POST['_download_link'];
  62.     $file_location = $_POST['_download_link'];
  63.  
  64.     // Check if the product already exists.
  65.     $product_result = $wpdb->get_row("SELECT `product_id`, `product_code`, `file_location` FROM `" . $table_prefix . "Product` WHERE `product_code` = '$product_code';");
  66.  
  67.     // Edit existing.
  68.     if ($product_result) {
  69.         $query = "
  70.             UPDATE `" . $table_prefix . "Product` SET
  71.                 product_name    = '".mysql_real_escape_string($product_name)."',
  72.                 product_code    = '".mysql_real_escape_string($product_code)."',
  73.                 price       = '".mysql_real_escape_string($price)."',
  74.                 file_name   = '".mysql_real_escape_string($file_name)."',
  75.                 file_location   = '".mysql_real_escape_string($file_location)."'
  76.             WHERE `product_code` = '$product_code';
  77.         ";
  78.         $wpdb->query($query);
  79.         $output = "<div class=\"productRow\">&raquo; Updated <em>$product_name</em> ($product_code)</div>\n";
  80.  
  81.     // Add
  82.     } else {
  83.         // Add as new.
  84.         $query = "
  85.             INSERT INTO `" . $table_prefix . "Product` SET
  86.                 product_code    = '".mysql_real_escape_string($product_code)."',
  87.                 product_name    = '".mysql_real_escape_string($product_name)."',
  88.                 price       = '".mysql_real_escape_string($price)."',
  89.                 file_name   = '".mysql_real_escape_string($file_name)."',
  90.                 file_location   = '".mysql_real_escape_string($file_location)."'
  91.         ";
  92.         $wpdb->query($query);
  93.         $output = "<div class=\"productRow\">&raquo; Added <em>$product_name</em> ($product_code)</div>\n";
  94.     }
  95.  
  96.     return $post_id;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement