Advertisement
emcniece

WooCommerce Price By Country add_inline_js() fix

Feb 20th, 2015
805
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.83 KB | None | 0 0
  1. <?php
  2. /*
  3. FILE NAME: class-woocommerce-price-by-country-settings.php
  4. Version: 0.34
  5. Author: Sweet Homes
  6. Author URI: http://www.sweethomes.es
  7. Email: info@sweethomes.es
  8. */
  9. /*********************************
  10. Copyright (c) 2013 - sweethomes.es
  11. **********************************/
  12.  
  13. if ( !defined( 'ABSPATH' ) ) die;
  14.  
  15. if ( !class_exists( 'WC_Integration' ) )
  16.     return;
  17.  
  18. class Woocomerce_Price_by_Country_Settings extends WC_Integration {
  19.  
  20.  
  21.     function __construct() {
  22.  
  23.         $this->id = 'price_by_country';
  24.         $this->method_title = __( 'Price by Country', 'woocomerce-price-by-country' );
  25.         //$this->method_description = __( 'Add price groups by country', 'woocomerce-price-by-country');
  26.  
  27.         $this->init_form_fields();
  28.         $this->init_settings();
  29.  
  30.         add_action( 'woocommerce_update_options_integration_' . $this->id , array( &$this, 'process_admin_options') );
  31.         add_action( 'wp_ajax_wpbc_get_uniqid', array( &$this, 'get_id' ) );
  32.     }
  33.  
  34.  
  35.     function init_form_fields() {
  36.  
  37.  
  38.     }
  39.  
  40.  
  41.     function get_id() {
  42.  
  43.         die( json_encode( array( 'id' => uniqid() ) ) );
  44.  
  45.     }
  46.  
  47.  
  48.     function admin_options() {
  49.         ?>
  50.  
  51.         <h3><?php echo isset( $this->method_title ) ? $this->method_title : __( 'Settings', 'woocomerce-price-by-country' ) ; ?></h3>
  52.  
  53.         <?php echo isset( $this->method_description ) ? wpautop( $this->method_description ) : ''; ?>
  54.  
  55.         <?php
  56.         ///////////////////////////////////////////
  57.  
  58.         $settingsForEdit = get_option( 'woocommerce_'. $this->id . '_settings' );
  59.         $oldSettingsForEdit = unserialize($settingsForEdit);
  60.  
  61.         if(is_array($oldSettingsForEdit)):
  62.  
  63.             $ArrayForJson = array();
  64.             foreach($oldSettingsForEdit as $key => $element):
  65.                 $ArrayForJson[$key] = $element['countries'];
  66.             endforeach;
  67.  
  68.  
  69.         endif;
  70.  
  71.         $JsonSetings = json_encode($ArrayForJson);
  72.  
  73.         //print_r($JsonSetings);
  74.         ?>
  75.         <table id="wpbc_settings_table" class="form-table" data-json-settings='<?php echo $JsonSetings; ?>'>
  76.             <?php $this->min_settings() ?>
  77.             <?php $this->generate_settings_html(); ?>
  78.         </table>
  79.  
  80.         <div><input type="hidden" name="section" value="<?php echo $this->id; ?>" /></div>
  81.  
  82.         <?php
  83.     }
  84.  
  85.  
  86.  
  87.     function min_settings() {
  88.         global $woocommerce;
  89.  
  90.         $settingsGlobal = get_option( 'woocommerce_global_'. $this->id . '_settings' );
  91.  
  92.         $defaults = array ( 'wpbc_price_checkoutselector' => 'billing');
  93.  
  94.         if(empty($settingsGlobal)){
  95.             $settingsGlobal = wp_parse_args( $settings, $defaults );
  96.         }
  97.  
  98.         $c = new WC_Countries();
  99.  
  100.         if ( !isset( $s ) )
  101.             $s = array();
  102.  
  103.  
  104.         ?>
  105.         <script>
  106.         jQuery( document ).ready( function( $ ) {
  107.             $( "#wpbc_add_new_group" ).click( function() {
  108.  
  109.                 var uid = null;
  110.  
  111.                 $.post( ajaxurl, { action:'wpbc_get_uniqid' }, function( data ) {
  112.  
  113.                     try {
  114.                         var j = $.parseJSON( data );
  115.  
  116.                         uid = j.id;
  117.  
  118.                     } catch( err ) {
  119.  
  120.                         alert( '<?php _e( 'An error occurred. Try again.', 'woocomerce-price-by-country' )?>');
  121.                         return false;
  122.  
  123.                     }
  124.  
  125.                     var html = '\
  126.                         <tr>\
  127.                         <td width="10%">\
  128.                             <label class="">\
  129.                                 <input type="text" name="group_level_name[group_level_' + uid + ']" placeholder="<?php _e( 'Enter a group name','woocomerce-price-by-country' )?>" value="">\
  130.                             </label>\
  131.                         </td>\
  132.                         <td>\
  133.                             <div style="" class="group_states_box">\
  134.                                 <select name="group_level_country[group_level_' + uid + '][]" multiple="multiple" class="chosen_select">\
  135.                                 <?php
  136.                                 foreach( $c->countries as $k => $v ) {
  137.                                     if ( in_array( $k, (array)$s ) )
  138.                                         $selected = ' selected';
  139.                                     else
  140.                                         $selected = '';
  141.                                     echo '<option value="' . $k . '"' . $selected . '>' . $v . '</option>\ ';
  142.                                 }
  143.                                 ?>
  144.                                 </select>\
  145.                             </div>\
  146.                             <button type="button" class="button all_button" style="display:inline-block;"><?php _e('All'); ?></button>\
  147.                             <button type="button" class="button none_button" style="display:inline-block;"><?php _e('None'); ?></button>\
  148.                             <button type="button" class="button es_button" style="display:inline-block;"><?php _e('ES'); ?></button>\
  149.                             <button type="button" class="button usa_button" style="display:inline-block;"><?php _e('USA'); ?></button>\
  150.                             <button type="button" class="button eu_button" style="display:inline-block;"><?php _e('EU'); ?></button>\
  151.                         </td>\
  152.                         <td width="10%">\
  153.                         </td>\
  154.                     </tr>\
  155.                     ';
  156.  
  157.                     $( '.group_table' ).append( html );
  158.                     $("select.chosen_select").chosen();
  159.                     return false;
  160.  
  161.                 });
  162.  
  163.  
  164.             })
  165.  
  166.             $( ".edit_group" ).click( function() {
  167.  
  168.                 var uid = $(this).attr('id'),
  169.                     prev = $(this).prev('.group-name'),
  170.                     prevName = prev.val(),
  171.                     parentTrLinea = $(this).parents('tr').eq(0);
  172.  
  173.  
  174.  
  175.                 $.post( ajaxurl, { action:'wpbc_get_uniqid' }, function( data ) {
  176.  
  177.                     var settings = $('#wpbc_settings_table').attr('data-json-settings'),
  178.                         jsonSettings = JSON.parse(settings),
  179.                         currentEdit = jsonSettings[uid];
  180.  
  181.                         console.log(currentEdit);
  182.  
  183.                     var html = '\
  184.                         <td width="10%">\
  185.                             <label class="group-name">\
  186.                                 <input type="text"  name="group_level_name[' + uid + ']" placeholder="<?php _e( 'Enter a group name','woocomerce-price-by-country' )?>" value="'+prevName+'">\
  187.                             </label>\
  188.                         </td>\
  189.                         <td>\
  190.                             <div style="width:300px;" class="group_states_box">\
  191.                                 <select name="group_level_country['+ uid + '][]" multiple="multiple" class="chosen_select">\ ';
  192.  
  193.                         var html1 = '';
  194.  
  195.  
  196.                         <?php foreach( $c->countries as $k => $v ) { ?>
  197.  
  198.                             var founded = $.inArray( "<?php echo $k ?>", currentEdit );
  199.                             var selected = '';
  200.  
  201.                             if(founded != -1){
  202.                                 selected = 'selected';
  203.                             } else {
  204.                                 selected = '';
  205.                             }
  206.                             html1 +=  '<option value="<?php echo $k ;?>" '+selected+'><?php echo  $v ?></option>\ ';
  207.  
  208.                         <?php } ?>
  209.  
  210.                         var html2 = '</select>\
  211.                             </div>\
  212.                             <button type="button" class="button all_button" style="display:inline-block;"><?php _e('All'); ?></button>\
  213.                             <button type="button" class="button none_button" style="display:inline-block;"><?php _e('None'); ?></button>\
  214.                             <button type="button" class="button es_button" style="display:inline-block;"><?php _e('ES'); ?></button>\
  215.                             <button type="button" class="button usa_button" style="display:inline-block;"><?php _e('USA'); ?></button>\
  216.                             <button type="button" class="button eu_button" style="display:inline-block;"><?php _e('EU'); ?></button>\
  217.                         </td>\
  218.                         <td width="10%">\
  219.                         </td>\
  220.                     ';
  221.  
  222.                     var res = html.concat(html1,html2);
  223.  
  224.  
  225.                     parentTrLinea.html( res );
  226.                     $("select.chosen_select").chosen({width: "95%"});
  227.                     return false;
  228.  
  229.                 });
  230.  
  231.  
  232.             })
  233.  
  234.  
  235.  
  236.         })
  237.         </script>
  238.  
  239.         <tr valign="top">
  240.             <th class="titledesc" scope="row" colspan="2">
  241.                 <h5 style="margin:0"><?php _e( 'Selects which is the primary modifier price', 'woocomerce-price-by-country' )?>:</h5>
  242.             </th>
  243.         </tr>
  244.  
  245.         <tr valign="top">
  246.             <th class="titledesc" scope="row">
  247.                 <label for="groups">
  248.                 <?php _e( 'Billing address', 'woocomerce-price-by-country' )?>
  249.                 </label>
  250.             </th>
  251.             <td class="forminp wpbc_groups">
  252.  
  253.                 <input type="radio" name="wpbc_price_checkoutselector" value="billing" <?php checked( $settingsGlobal['wpbc_price_checkoutselector'], 'billing', true ) ?>>
  254.                 <img class="help_tip tiered" src="<?php echo $woocommerce->plugin_url() ?>/assets/images/help.png" data-tip="<?php _e( 'Billing defines Final Price.', 'woocomerce-price-by-country' )?>">
  255.             </td>
  256.  
  257.         </tr>
  258.         <tr valign="top">
  259.             <th class="titledesc" scope="row">
  260.                 <label for="groups">
  261.                 <?php _e( 'Shipping address', 'woocomerce-price-by-country' )?>
  262.                 </label>
  263.             </th>
  264.             <td class="forminp wpbc_groups">
  265.  
  266.                 <input type="radio" name="wpbc_price_checkoutselector" value="shipping" <?php checked( $settingsGlobal['wpbc_price_checkoutselector'], 'shipping', true ) ?>>
  267.                 <img class="help_tip tiered" src="<?php echo $woocommerce->plugin_url() ?>/assets/images/help.png" data-tip="<?php _e( 'Shipping defines Final Price.', 'woocomerce-price-by-country' )?>">
  268.             </td>
  269.         </tr>
  270.  
  271.         <tr valign="top">
  272.             <th class="titledesc" scope="row">
  273.                 <label for="groups">
  274.                 <?php _e( 'Country selector class', 'woocomerce-price-by-country' )?>
  275.                 </label>
  276.             </th>
  277.             <td class="forminp wpbc_groups">
  278.  
  279.                 <input type="text" name="wpbc_price_countrySelectorClass" value="<?php echo $settingsGlobal['wpbc_price_countrySelectorClass'] ?>">
  280.                 <img class="help_tip tiered" src="<?php echo $woocommerce->plugin_url() ?>/assets/images/help.png" data-tip="<?php _e( 'Defines extra classe for country selector.', 'woocomerce-price-by-country' )?>">
  281.             </td>
  282.         </tr>
  283.  
  284.         <tr valign="top">
  285.             <th class="titledesc" scope="row">
  286.                 <label for="groups">
  287.                 <?php _e( 'Message for country outside', 'woocomerce-price-by-country' )?>
  288.                 </label>
  289.             </th>
  290.             <td class="forminp wpbc_groups">
  291.  
  292.                 <input type="text" name="wpbc_outsideMesage" value="<?php echo $settingsGlobal['wpbc_outsideMesage'] ?>">
  293.                 <img class="help_tip tiered" src="<?php echo $woocommerce->plugin_url() ?>/assets/images/help.png" data-tip="<?php _e( 'Message for replacing price in case you customer is not your country groups list.', 'woocomerce-price-by-country' )?>">
  294.             </td>
  295.         </tr>
  296.  
  297.         <tr valign="top">
  298.             <th class="titledesc" scope="row" colspan="2">
  299.                 <h5 style="margin:0">
  300.                 <?php _e( 'Add price groups by country', 'woocomerce-price-by-country' )?>:
  301.                 </h5>
  302.             </th>
  303.         </tr>
  304.         <style>
  305.             .help_tip.tiered { width: 16px; float: none !important; }
  306.             .group_states_box .chzn-container { width: 100% !important; }
  307.         </style>
  308.  
  309.         <tr valign="top">
  310.             <td colspan="2" class="forminp wpbc_groups">
  311.                 <table width="100%" class="group_table">
  312.                     <tr>
  313.                         <th>
  314.                             <strong><?php _e( 'Group Name', 'woocomerce-price-by-country' ) ?></strong>
  315.                         </th>
  316.                         <th>
  317.                             <strong><?php _e( 'Countries', 'woocomerce-price-by-country' ) ?></strong>
  318.                         </th>
  319.                         <th>
  320.                             <strong><?php _e( 'Delete', 'ignitewoo_tiered_pricing' ) ?></strong>
  321.                             <img class="help_tip tiered" src="<?php echo $woocommerce->plugin_url() ?>/assets/images/help.png" data-tip="<?php _e( 'Delete a group of price', 'woocomerce-price-by-country' )?>">
  322.                         </th>
  323.                         <th>
  324.                             <strong><?php _e( 'Edit', 'ignitewoo_tiered_pricing' ) ?></strong>
  325.                             <img class="help_tip tiered" src="<?php echo $woocommerce->plugin_url() ?>/assets/images/help.png" data-tip="<?php _e( 'Edit group', 'woocomerce-price-by-country' )?>">
  326.                         </th>
  327.                     </tr>
  328.                     <?php
  329.  
  330.                     $settingsview = get_option( 'woocommerce_'. $this->id . '_settings' );
  331.                     $settingsArray = unserialize($settingsview);
  332.  
  333.                     if (!empty($settingsArray)):
  334.                         foreach($settingsArray as $key => $data):
  335.                     ?>
  336.                         <tr>
  337.                             <td width="10%">
  338.                                 <label class="group-name">
  339.                                     <span><?php echo stripslashes( $data['name'] ) ?></span>
  340.                                 </label>
  341.                             </td>
  342.                             <td>
  343.                                 <?php echo implode(', ', $data['countries']); ?>
  344.                             </td>
  345.                             <td width="10%">
  346.                                 <input type="checkbox" value="<?php echo $key ?>" style="" id="<?php echo $key ?>" name="group_level_delete[<?php echo $key ?>]" class="input-text wide-input ">
  347.                             </td>
  348.                             <td width="20%">
  349.                                 <input type="hidden" class="group-name" name="group-name" value="<?php echo stripslashes( $data['name'] ) ?>">
  350.                                 <button type="button" class="button edit_group" id="<?php echo $key ?>" value="group_level_edit[<?php echo $key ?>]"><?php _e( 'Edit ', 'woocomerce-price-by-country' )?></button>
  351.                             </td>
  352.                         </tr>
  353.                     <?php
  354.                         endforeach;
  355.  
  356.                     endif;
  357.                     ?>
  358.                 </table>
  359.             </td>
  360.         </tr>
  361.  
  362.         <tr>
  363.             <th></th>
  364.             <td><button type="button" class="button" id="wpbc_add_new_group"><?php _e( 'Add New Group', 'woocomerce-price-by-country' )?></button></td>
  365.  
  366.             <script type="text/javascript">
  367.                 jQuery('.all_button').live('click', function(){
  368.                     var self = jQuery(this),
  369.                         parentTD = self.parent();
  370.                     parentTD.find('select option').attr("selected","selected");
  371.                     parentTD.find('select').trigger('chosen:updated');
  372.                     return false;
  373.                 });
  374.  
  375.                 jQuery('.none_button').live('click', function(){
  376.                     var self = jQuery(this),
  377.                         parentTD = self.parent();
  378.                     parentTD.find('select option').removeAttr("selected");
  379.                     parentTD.find('select').trigger('chosen:updated');
  380.                     return false;
  381.                 });
  382.  
  383.                 jQuery('.es_button').live('click', function(){
  384.                     var self = jQuery(this),
  385.                         parentTD = self.parent();
  386.                     parentTD.find('option[value="ES"]').attr("selected","selected");
  387.                     parentTD.find('select').trigger('chosen:updated');
  388.                     return false;
  389.                 });
  390.  
  391.                 jQuery('.usa_button').live('click', function(){
  392.                     var self = jQuery(this),
  393.                         parentTD = self.parent();
  394.                     parentTD.find('option[value="US"]').attr("selected","selected");
  395.                     parentTD.find('select').trigger('chosen:updated');
  396.                     return false;
  397.                 });
  398.  
  399.                 jQuery('.eu_button').live('click', function(){
  400.                     var self = jQuery(this),
  401.                         parentTD = self.parent();
  402.                     parentTD.find('option[value="AL"], option[value="AD"], option[value="AM"], option[value="AT"], option[value="BY"], option[value="BE"], option[value="BA"], option[value="BG"], option[value="CH"], option[value="CY"], option[value="CZ"], option[value="DE"], option[value="DK"], option[value="EE"], option[value="ES"], option[value="FO"], option[value="FI"], option[value="FR"], option[value="GB"], option[value="GE"], option[value="GI"], option[value="GR"], option[value="HU"], option[value="HR"], option[value="IE"], option[value="IS"], option[value="IT"], option[value="LT"], option[value="LU"], option[value="LV"], option[value="MC"], option[value="MK"], option[value="MT"], option[value="NO"], option[value="NL"], option[value="PO"], option[value="PT"], option[value="RO"], option[value="RU"], option[value="SE"], option[value="SI"], option[value="SK"], option[value="SM"], option[value="TR"], option[value="UA"], option[value="VA"]').attr("selected","selected");
  403.                     parentTD.find('select').trigger('chosen:updated');
  404.                     return false;
  405.                 });
  406.             </script>
  407.         <?php
  408.  
  409.         // preset buttons
  410.         /* DEPRECATED!
  411.         $woocommerce->add_inline_js("");*/
  412.  
  413.         ?>
  414.         </tr>
  415.         <tr>
  416.             <td colspan="2" align="left"><a href="http://www.sweethomes.es" style="text-decoration:none;"><?php echo '<img src="'. plugins_url('sweethomes.png', __FILE__).'" alt="Sweet Homes">'; ?> <span style="color: rgb(0, 0, 0); font: bold 14px/20px Arial,sans-serif; vertical-align: top;">Sweet Homes</span></a> <span style="diplay:block; vertical-align: super;">is a study of online communication based in Barcelona. We have extensive experience developing projects in wordpress and other CMS's </span></td>
  417.         </tr>
  418.         <?php
  419.  
  420.     }
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.     function process_admin_options() {
  428.  
  429.         //parent::process_admin_options();
  430.  
  431.  
  432.         $OldsettingsGlobal = get_option( 'woocommerce_global_'. $this->id . '_settings' );
  433.  
  434.         if ( empty( $OldsettingsGlobal ) ) {
  435.  
  436.             $OldsettingsGlobal = '';
  437.             add_option( 'woocommerce_global_'. $this->id . '_settings', $OldsettingsGlobal, '', 'yes' );
  438.         }
  439.  
  440.         $OldsettingsGlobal['wpbc_price_checkoutselector'] =  isset( $_POST['wpbc_price_checkoutselector'] ) ? $_POST['wpbc_price_checkoutselector'] : '';
  441.         $OldsettingsGlobal['wpbc_price_countrySelectorClass'] =  isset( $_POST['wpbc_price_countrySelectorClass'] ) ? $_POST['wpbc_price_countrySelectorClass'] : '';
  442.         $OldsettingsGlobal['wpbc_outsideMesage'] =  isset( $_POST['wpbc_outsideMesage'] ) ? $_POST['wpbc_outsideMesage'] : '';
  443.  
  444.  
  445.         /*var_dump($OldsettingsGlobal);
  446.         die();*/
  447.  
  448.         update_option( 'woocommerce_global_'.  $this->id . '_settings', $OldsettingsGlobal );
  449.  
  450.  
  451.         ////////////////////////////////////////////////////////////
  452.         $settings = get_option( 'woocommerce_'. $this->id . '_settings' );
  453.         $oldSettings = unserialize($settings);
  454.  
  455.         if ( empty( $oldSettings ) ) {
  456.             $settings = '';
  457.             add_option( 'woocommerce_'. $this->id . '_settings', $settings, '', 'yes' );
  458.         }
  459.  
  460.         if ((!empty($_POST['group_level_name'])) && (!empty($_POST['group_level_country']))):
  461.  
  462.             $names = $_POST['group_level_name'];
  463.             $countries = $_POST['group_level_country'];
  464.  
  465.             if ($names):
  466.                 $rawSettings = array();
  467.                 foreach($names as $key => $name):
  468.                     $rawSettings[$key]['name'] = $name;
  469.                     $rawSettings[$key]['countries'] = $countries[$key];
  470.                 endforeach;
  471.             endif;
  472.  
  473.             if ($oldSettings):
  474.  
  475.                 $result = array_merge($oldSettings,$rawSettings );
  476.                 $newsettings = serialize($result);
  477.                 update_option( 'woocommerce_'.  $this->id . '_settings', $newsettings );
  478.             else:
  479.                 $settings = serialize($rawSettings);
  480.                 update_option( 'woocommerce_'.  $this->id . '_settings', $settings );
  481.             endif;
  482.         endif;
  483.  
  484.  
  485.         if ( !empty( $_POST[ 'group_level_delete' ] ) ) {
  486.             foreach( $_POST[ 'group_level_delete' ] as $key => $delgroup ) {
  487.                 unset($oldSettings[$delgroup]);
  488.             }
  489.             $newsettings = serialize($oldSettings);
  490.             update_option( 'woocommerce_'.  $this->id . '_settings', $newsettings );
  491.         }
  492.  
  493.     }
  494.  
  495.  
  496. }
  497.  
  498.  
  499. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement