Advertisement
Guest User

WPEC Personalize Update

a guest
Jan 31st, 2013
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 15.98 KB | None | 0 0
  1. <?php
  2. /* Test New Permissions */
  3. /*
  4. Plugin Name: WPEC Personalize
  5. Plugin URI: http://www.derekweathersbee.com/products/wpec-personalize
  6. Description: Plugin for Extended Personalization of WP E-Commerce
  7. Version: 1.0
  8. Author: Derek Weathersbee (derek@derekweathersbee.com), Chris Vanzo of Vanzo Ink (http://vanzoink.com), and Sluchak Anton (anketko@gmail.com).
  9. Author URI: http://derekweathersbee.com
  10. */
  11.  
  12. /*
  13.  
  14. IMPORTANT NOTE added by vanzoink.com
  15. -----------------------------------------------------------------------------
  16. The file "wpsc-admin/includes/display-items-functions.php" has a bug that will be fixed in the next revision 3.8.5 of wpec details below
  17.  
  18. Revision details at: http://code.google.com/p/wp-e-commerce/source/detail?r=491
  19.  
  20. Instructions for manual fix:  
  21.     1. You must open up the file wpsc-admin/includes/display-items-functions.php
  22.     2. Find the following code on the following lines:
  23.         754     -       ob_start();
  24.         755 754         do_action( 'wpsc_add_advanced_options', $post->ID );
  25.         756     -       ob_get_contents();
  26.         757     -       ob_end_clean();
  27.     3. Comment or delete out lines 754, 756 and 757 to get
  28.         754     -       //ob_start();
  29.         755 754         do_action( 'wpsc_add_advanced_options', $post->ID );
  30.         756     -       //ob_get_contents();
  31.         757     -       //ob_end_clean();
  32. ---------------------------------------------------------------------------
  33. */
  34.  
  35.  
  36.  
  37. /*
  38. if (is_admin()) {
  39. function wpsc_add_modules_admin_pages($page_hooks, $base_page) {
  40. @$page_hooks[] = @add_submenu_page($base_page, __('-New Page','wpsc'), __('-New Page','wpsc'), 7, 'customization_ec.php', 'wpsc_display_admin_pages');
  41. return $page_hooks;
  42. }
  43. @add_filter('wpsc_additional_pages', 'wpsc_add_modules_admin_pages',10, 2);
  44. }
  45. */
  46.  
  47.  
  48. //PLUGIN START!!!
  49. register_activation_hook( __FILE__, 'customisation_ec_activate' );
  50. function customisation_ec_activate(){
  51.     global $wpdb;
  52.     $table_name = $wpdb->prefix . "wpsc_pers_products";
  53.     $table_name_global = $wpdb->prefix . "wpsc_pers_global";
  54.     $sql = "CREATE TABLE IF NOT EXISTS ".$table_name_global." (pers_id int(11) NOT NULL, pers_name text, pers_type text, PRIMARY KEY (pers_id)) ENGINE=MyISAM  DEFAULT CHARSET=utf8;";
  55.     $sql2 = "CREATE TABLE IF NOT EXISTS ".$table_name."( product_id int(11) NOT NULL, pers_id int(11) NOT NULL, enabled tinyint(1) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
  56.         $wpdb->query($sql);
  57.         $wpdb->query($sql2);
  58.  
  59. }
  60. $page_single = false;
  61. $product_counter = 0;
  62. $js = '';
  63.  
  64. add_action('wpsc_product_before_description', 'checker_single');
  65. function checker_single(){
  66.     global $page_single;
  67.     $page_single = wpsc_is_single_product();
  68. }
  69.  
  70. function wpsc_display_products_page_ext($query) {
  71.     global $wpdb, $wpsc_query, $wpsc_theme_path;
  72.     /// added by xiligroup.dev to be compatible with touchshop
  73.     $cur_wpsc_theme_folder = apply_filters('wpsc_theme_folder',$wpsc_theme_path.WPSC_THEME_DIR);
  74.     /// end of added by xiligroup.dev to be compatible with touchshop
  75.     $temp_wpsc_query = new WPSC_query($query);
  76.     list($wpsc_query, $temp_wpsc_query) = array($temp_wpsc_query, $wpsc_query); // swap the wpsc_query objects
  77.     $GLOBALS['nzshpcrt_activateshpcrt'] = true;
  78.     ob_start();
  79.         include($cur_wpsc_theme_folder."/single_product.php");
  80.     $output = ob_get_contents();
  81.     ob_end_clean();
  82.     //$output = str_replace('$','\$', $output);
  83.     list($temp_wpsc_query, $wpsc_query) = array($wpsc_query, $temp_wpsc_query); // swap the wpsc_query objects back
  84.     return $output;
  85. }
  86.  
  87. function wpsc_products_shorttag_ext($atts) {
  88.     $number_per_page = get_option('use_pagination') ? get_option('wpsc_products_per_page') : 0;
  89.     $query = shortcode_atts(array(
  90.         'product_id' => 0,
  91.         'product_url_name' => null,
  92.         'product_name' => null,
  93.         'category_id' => 0,
  94.         'category_url_name' => null,
  95.         'tag' => null,
  96.         'price' => 0,
  97.         'limit_of_items' => 0,
  98.         'sort_order' => null,
  99.         'number_per_page' => $number_per_page,
  100.         'page' => 0,
  101.     ), $atts);
  102.     return wpsc_display_products_page_ext($query);
  103. }
  104. add_shortcode('wpsc_products_ext', 'wpsc_products_shorttag_ext');
  105.  
  106.  
  107. $prod_id = 0;
  108.  
  109.  
  110. add_action('testaction', 'shopping_cart_echo');
  111. function shopping_cart_echo($product_id){
  112.     echo "<<<<:".$product_id;
  113. }
  114.  
  115.  
  116.  
  117. add_action('wpsc_product_addon_after_descr', 'client_echo');        // show data to the user
  118. function client_echo($product_id){
  119.     global $prod_id;
  120.     $prod_id = $product_id;
  121.     ?>
  122.     <style type="text/css">
  123.     .custom_text {
  124.         display:none;
  125.     }
  126.     </style>
  127.     <?php
  128.     global $wpdb;
  129.     global $page_single;
  130.     global $product_counter;
  131.  
  132.     $table_name = $wpdb->prefix . "wpsc_pers_products";
  133.     $table_name_global = $wpdb->prefix . "wpsc_pers_global";
  134.     if (isset($product_id) /*&& ($page_single)*/){
  135.         global $js;
  136.        // $product_id = $_GET['product_id'];      //custom_text
  137.        $js .= "<script language=\"JavaScript\" type=\"text/javascript\">
  138.                 document.getElementById('product_".$product_id."_submit_button').onclick = function(){
  139.                     var j = document.getElementById('ii_".$product_id."').value;
  140.                     text_general = '';
  141.                     caption = '';
  142.                     text = '';
  143.                     for  (var i=0; i<j; i++){
  144.                         capt_name = 'pers_capt_".$product_id."_'+i;
  145.                         pers_capt_name = 'personal_".$product_id."_'+i;
  146.                         if(document.getElementById(pers_capt_name).value != ''){
  147.                             caption = document.getElementById(capt_name).value;
  148.                             text = document.getElementById(pers_capt_name).value;
  149.                             text_general += '<p><strong>'+caption+'</strong> <br><em>'+text+'</em><br></p>';
  150.                         }
  151.                     }
  152.                     document.getElementsByName('custom_text')[".$product_counter."].value = text_general;
  153.                 }
  154.             </script>";
  155.             $product_counter++;
  156.             $i = 0;
  157.             echo '<div class="customisation_extended">';
  158.  
  159.             $myresult = $wpdb->get_results("SELECT pers_id FROM ". $table_name." WHERE product_id = ".$product_id." AND enabled = 1 ORDER BY pers_id ASC");
  160.  
  161.             foreach ($myresult as $personalization){
  162.  
  163.                 $echo_vars = $wpdb->get_results("SELECT * FROM ". $table_name_global ." WHERE pers_id  = ". $personalization->pers_id);
  164.  
  165.                 foreach ($echo_vars as $echo_var){
  166.  
  167.                     echo '<input id="pers_capt_'.$product_id.'_'.$i.'" name="pers_capt_'.$product_id.'_'.$i.'" type="hidden" value="'.$echo_var->pers_name.'">';
  168.  
  169.                     echo '<input id="pers_typeid_'.$product_id.'_'.$i.'" name="pers_type_'.$product_id.'_'.$i.'" type="hidden" value="'.$echo_var->pers_type.'">';
  170.  
  171.                     echo '<div class="personalization_caption">'.$echo_var->pers_name.'</div>';
  172.  
  173.                     echo '<div class="personalisation_input">';
  174.  
  175.                     /* edited by dw to add a title "slug" to each field */
  176.                    
  177.                     $fieldLabel = $echo_var->pers_name;
  178.                     $fieldLabel = preg_replace("/[^a-z0-9- ]/i", "", $fieldLabel);
  179.                     $fieldLabel = str_replace(" ", "-", $fieldLabel);
  180.                     $fieldLabel = strtolower($fieldLabel);
  181.  
  182.                     if ($echo_var->pers_type == 'textarea'){
  183.                         echo '<textarea title="'.$fieldLabel.'" class="personalisation_textarea" id="personal_'.$product_id.'_'.$i.'" name="personal_'.$product_id.'_'.$personalization->pers_id.'" rows=5 cols=20 wrap="off"></textarea>';
  184.                     }else{
  185.                         echo '<input title="'.$fieldLabel.'" class="personalisation_textfield" id="personal_'.$product_id.'_'.$i.'" name="personal_'.$product_id.'_'.$personalization->pers_id.'" type="text" value="">';
  186.                     }
  187.                     echo '</div>';
  188.                 }
  189.                 $i++;
  190.             }
  191.             echo '<input name="iii" id="ii_'.$product_id.'" type="hidden" value="'.$i.'">';
  192.             echo '</div>';
  193.     }
  194. }
  195.  
  196.  
  197.  
  198.  
  199.  
  200. add_action('wp_footer', 'footer_script');
  201.  
  202. function footer_script(){
  203.  
  204. global $js;
  205.  
  206. echo $js;
  207.  
  208. }
  209.  
  210.  
  211. add_action('wpsc_add_advanced_options', 'enable_settings'); // add to add-edit products pages
  212. function enable_settings($product_id){
  213.     global $wpdb;
  214.  
  215.     $table_name = $wpdb->prefix . "wpsc_pers_products";
  216.  
  217.     $table_name_global = $wpdb->prefix . "wpsc_pers_global";
  218.  
  219.     if(isset($product_id)){
  220.         //add fields
  221.  
  222.         $myresult = $wpdb->get_results("SELECT pers_id FROM ". $table_name_global);
  223.  
  224.  
  225.  
  226.         foreach ($myresult as $myresults){
  227.  
  228.             $add = $wpdb->get_var( $wpdb->prepare(
  229.     "SELECT enabled FROM %s WHERE pers_id = %d AND product_id = %d",
  230.     $table_name,
  231.     $myresults->pers_id,
  232.     $product_id
  233. ));
  234.  
  235.  
  236.             if ($add ==''){
  237.  
  238.                 $aff_rows = $wpdb->insert( $table_name, array( 'product_id' => $product_id, 'pers_id' => $myresults->pers_id, 'enabled' => '1' ) );
  239.  
  240.             }
  241.         }
  242.  
  243.     ?>
  244. <script language="JavaScript" type="text/javascript">
  245.  
  246.         document.getElementById('add_engrave_text').checked = true;
  247.  
  248.         document.getElementById('add_engrave_text').onclick = function(){
  249.             document.getElementById('add_engrave_text').checked = true;
  250.         }
  251.  
  252. </script>
  253. <style>
  254. .personalizations-admin {
  255.     margin: 14px 0 0;
  256. }
  257. .personalizations-admin td {
  258.     padding: 3px 8px;
  259.     text-align: left;
  260.     text-transform: capitalize;
  261. }
  262. .personalizations-admin th {
  263.     padding: 3px 8px 8px;
  264.     font-weight: bold;
  265.     text-transform: capitalize;
  266.     text-align: left;
  267.     color: #21759B;
  268.     font-size: 120%;
  269. }
  270. </style>
  271.  
  272. <table class='personalizations-admin'>
  273.    
  274.         <th colspan = 3> <strong>Personalization options extended</strong> </th>
  275.     <tr>
  276.         <td><b>enabled</b></td>
  277.         <td><b>personalization name</b></td>
  278.         <td><b>field type</b></td>
  279.     </tr>
  280.     <?php
  281.  
  282.         $myresult = $wpdb->get_results("SELECT pers_id, enabled FROM ". $table_name ." WHERE product_id  = ". $product_id." ORDER BY pers_id ASC");
  283.  
  284.         foreach ($myresult as $myresults){
  285.  
  286.             $row = $wpdb->get_row("SELECT * FROM ". $table_name_global ." WHERE pers_id  = ". $myresults->pers_id);
  287.  
  288.             if  ($myresults->enabled == 1){
  289.                 $checked = 'checked';
  290.             }else{
  291.                 $checked = '';
  292.             }
  293.  
  294.  
  295.  
  296.             echo '<tr>  <td><input name="enabled_'.$row->pers_id.'" type="checkbox" value="ON" '.$checked.'></td>   <td>'.$row->pers_name.'</td>    <td>'.$row->pers_type.'</td></tr>';
  297.  
  298.         }
  299.  
  300. ?>
  301. </table>
  302. <?php
  303.  
  304.     }
  305.  
  306. }
  307.  
  308. add_action('wpsc_edit_product', 'add_customise'); // add to add-edit products pages
  309. function add_customise($product_id){
  310.     global $wpdb;
  311.  
  312.     $table_name_global = $wpdb->prefix . "wpsc_pers_global";
  313.  
  314.     $table_name = $wpdb->prefix . "wpsc_pers_products";
  315.  
  316.  
  317.  
  318.     if (isset($product_id)){
  319.  
  320.         //add fields
  321.  
  322.         $myresult = $wpdb->get_results("SELECT pers_id FROM ". $table_name_global);
  323.  
  324.         foreach ($myresult as $myresults){
  325.  
  326.             $add = $wpdb->get_var( $wpdb->prepare(
  327.     "SELECT enabled FROM %s WHERE pers_id = %d AND product_id = %d",
  328.     $table_name,
  329.     $myresults->pers_id,
  330.     $product_id
  331. ));
  332.            
  333.    
  334.  
  335.             if ($add ==''){
  336.  
  337.                 $aff_rows = $wpdb->insert( $table_name, array( 'product_id' => $product_id, 'pers_id' => $myresults->pers_id, 'enabled' => '1' ) );
  338.  
  339.             }
  340.         }
  341.  
  342.         //enable\disable personalization
  343.  
  344.         $myresult = $wpdb->get_results("SELECT pers_id FROM ". $table_name ." WHERE product_id = ".$product_id);
  345.  
  346.         foreach ($myresult as $myresults){
  347.  
  348.             $post_var_name = 'enabled_'.$myresults->pers_id;
  349.  
  350.             if ($_POST[$post_var_name] == 'ON'){
  351.  
  352.  
  353.                     $query = "UPDATE ".$table_name." SET `enabled` = '1' WHERE `product_id` = ".$product_id." AND `pers_id` = ".$myresults->pers_id." LIMIT 1 ;";
  354.  
  355.                     $wpdb->query($query);
  356.  
  357.  
  358.             }else{
  359.  
  360.  
  361.  
  362.                     $query = "UPDATE ".$table_name." SET `enabled` = '0' WHERE `product_id` = ".$product_id." AND `pers_id` = ".$myresults->pers_id." LIMIT 1 ;";
  363.  
  364.                     $wpdb->query($query);
  365.  
  366.             }
  367.  
  368.         }
  369.  
  370.     }
  371.  
  372. }
  373.  
  374. add_action('admin_menu', 'template_plugin');
  375. function template_plugin() {
  376.  
  377.     //create new top-level menu
  378.  
  379. //  add_menu_page('Header Options Plugin Settings', 'Header Options', 'administrator', __FILE__, 'display_menu_settings_page',plugins_url('/images/icon.png', __FILE__));
  380.  
  381.     add_menu_page('WPEC Personalize', 'WPEC Personalize', 'administrator', __FILE__, 'template_plugin_settings');
  382.  
  383. }
  384.  
  385. /*
  386.     ADMIN
  387. */
  388. function template_plugin_settings() {
  389.     global $wpdb;
  390.     // added by vanzoink.com to clear up some php warnings
  391.     $update = $del_option = $add_option = false;
  392.     // end of add
  393.     $table_name = $wpdb->prefix . "wpsc_pers_global";
  394.  
  395.     $table_name_all = $wpdb->prefix . 'wpsc_pers_products';
  396.  
  397. if (isset ($_POST['option_change'])){
  398.  
  399.     $myresult = $wpdb->get_results("SELECT pers_id FROM ". $table_name );
  400.  
  401.     foreach ($myresult as $myresults){
  402.  
  403.  
  404.         $pers_name = 'custom_text_'.$myresults->pers_id;
  405.  
  406.         $pers_type = 'pers_type_'.$myresults->pers_id;
  407.  
  408.         $wpdb->update( $table_name, array( 'pers_name' => $_POST[$pers_name], 'pers_type' => $_POST[$pers_type] ), array( 'pers_id' => $myresults->pers_id));
  409.     }
  410.  
  411.  
  412.  
  413.     $update = true;
  414.  
  415. }
  416.  
  417.  
  418.  
  419. if ($_GET['action'] == 'add' && !isset ($_POST['option_change'])){
  420.  
  421.  
  422.     $id_pers = $wpdb->get_var($wpdb->prepare("SELECT pers_id FROM ".$table_name." ORDER BY pers_id DESC LIMIT 0 , 1"));
  423.  
  424.     $id_pers++;
  425.  
  426.  
  427.     $aff_rows = $wpdb->insert( $table_name, array( 'pers_id' => $id_pers, 'pers_name' => 'personalization text', 'pers_type' => 'textfield' ) );
  428.  
  429.     /* changed by vanzoink.com for 1.8 compatibility */
  430.     //    $myresult = $wpdb->get_results("SELECT id FROM ". $wpdb->prefix ."wpsc_product_list" );
  431.     $myresult = $wpdb->get_results("SELECT id FROM ". $wpdb->prefix ."posts WHERE  'post_type' = 'wpsc-product' " );
  432.     /* end of change */
  433.  
  434.  
  435.     foreach ($myresult as $myresults){
  436.  
  437.         $aff_rows = $wpdb->insert( $table_name_all, array( 'product_id' => $myresults->id, 'pers_id' => $id_pers, 'enabled' => 1 ) );
  438.        
  439.  
  440.     }
  441.  
  442.     $add_option = true;
  443. }
  444.  
  445. if ($_GET['action'] == 'del' && !isset ($_POST['option_change'])){
  446.  
  447.  
  448.     $wpdb->query('DELETE FROM `'.$table_name.'` WHERE `pers_id` = '.$_GET['id'].' LIMIT 1');
  449.  
  450.     $wpdb->query('DELETE FROM `'.$table_name_all.'` WHERE `pers_id` = '.$_GET['id']);
  451.  
  452.  
  453.  
  454.     $del_option = true;
  455. }
  456.  
  457.  
  458.  
  459. ?>
  460. <div class="wrap">
  461.     <h2>WPEC Personalize</h2>
  462.     <?php if($update){?>
  463.     <div class="updated">
  464.         <p><strong>Options saved.</strong></p>
  465.     </div>
  466.     <?php }?>
  467.     <?php if($add_option){?>
  468.     <div class="updated">
  469.         <p><strong>New field was successfully added</strong></p>
  470.     </div>
  471.     <?php }?>
  472.     <?php if($del_option){?>
  473.     <div class="updated">
  474.         <p><strong>The field was successfully deleted</strong></p>
  475.     </div>
  476.     <?php }?>
  477.     <form method="post"  >
  478.         <input name="option_change" type="hidden" value="">
  479.         <?
  480.  
  481.     $plugin_url = explode ('&', $_SERVER['REQUEST_URI']);
  482.  
  483.     $myresult = $wpdb->get_results("SELECT * FROM ". $table_name." ORDER BY pers_id ASC" );
  484.  
  485.     foreach ($myresult as $personalization){
  486.         echo '<input name="custom_text_'.$personalization->pers_id.'" type="text" size="60" value="'.$personalization->pers_name.'">';
  487.         if($personalization->pers_type == 'textfield'){
  488.             echo '<select size="1" name="pers_type_'.$personalization->pers_id.'">  <option value="textfield" SELECTED >textfield</option>  <option value="textarea">textarea</option> </select>';
  489.         }else{
  490.             echo '<select size="1" name="pers_type_'.$personalization->pers_id.'">  <option value="textfield">textfield</option>  <option value="textarea" SELECTED>textarea</option> </select>';
  491.         }
  492.         echo '<a class="submitdelete deletion" href="'.$plugin_url[0].'&action=del&id='.$personalization->pers_id.'">delete</a> <br>';
  493.     }
  494. ?>
  495.         <p class="submit">
  496.             <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
  497.             <a href="<?php echo $plugin_url[0]; ?>&action=add">Add new field</a> </p>
  498.     </form>
  499. </div>
  500. <? // donation section ?>
  501. <style>
  502. .donation-section  {
  503. padding: 20px;
  504. text-align: left;
  505. width: 600px;
  506. }
  507. .donation-section h3 {
  508.     background: transparent!important;
  509.     font-size: 24px;
  510.     font-weight: bold;
  511.     color: #555;
  512.     padding: 0;
  513.     margin: 0 0 18px;
  514. }
  515. .donation-section .donate-link {
  516. display: inline-block;
  517. padding: 0 10px 3px;
  518. background: #069;
  519. text-shadow: #000 1px 1px 1px;
  520. color: #fff !important;
  521. line-height: 22px;
  522. text-decoration: none;
  523. -moz-border-radius: 4px;
  524. -webkit-border-radius: 4px;
  525. border-radius: 4px;
  526. }
  527. </style>
  528. <div class="postbox donation-section">
  529.     <h3>Donate to the Author</h3>
  530.     <p>Has this plugin helped you? If so, please consider making a donation to compensate for the time, energy, and financial investment of the developers.</p>
  531.     <p><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=U98WSHGPXJ2V8" target='_blank' class='donate-link'>Donate Now</a></p>
  532. </div>
  533. <? // donation section ?>
  534. <?php
  535. }
  536. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement