emcniece

WooCommerce Price By Country add_inline_js() fix

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