Advertisement
Guest User

class-gateways-by-location.php Replacement

a guest
May 22nd, 2015
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.79 KB | None | 0 0
  1. <?php
  2. /**
  3.  * WooCommerce Gateways By Location
  4.  *
  5.  * @author      Store Apps
  6.  * @category    Admin
  7.  * @package     WooCommerce Gateways By Location
  8.  * @version     1.2.1
  9.  */
  10.  
  11. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  12.  
  13. if ( ! class_exists( 'SA_WC_Gateways_By_Location' ) ) {
  14.  
  15. /**
  16.  * Class SA_WC_Gateways_By_Location
  17.  */
  18. class SA_WC_Gateways_By_Location {
  19.  
  20.     private $version = '1.2';
  21.     private $plugin_url;
  22.     private $text_domain = 'wc_gateways_by_location';
  23.    
  24.  
  25.     function __construct() {
  26.  
  27.         $this->plugin_url = plugins_url('', dirname(__FILE__));        
  28.         add_action( 'init', array( $this, 'localize' ) );
  29.         add_filter('woocommerce_available_payment_gateways',array( $this, 'display_gateway_by_location' ) );
  30.                    
  31.         if (is_admin() && current_user_can( 'manage_options' ) ) {             
  32.             add_action( 'admin_enqueue_scripts', array( &$this, 'load_js_css' ) );
  33.             add_action( 'woocommerce_admin_field_payment_gateways', array( $this, 'gateway_by_location_output' ), 20 );
  34.             add_action( 'woocommerce_update_options_checkout', array( $this, 'save' ), 1000 );
  35.         }
  36.  
  37.     }
  38.  
  39.     function localize() {
  40.         load_plugin_textdomain( $this->text_domain, false, dirname( plugin_basename( __FILE__ ) ).'/languages/' );
  41.     }
  42.  
  43.     function load_js_css() {
  44.         global $woocommerce;
  45.        
  46.         if ( version_compare($woocommerce->version, '2.3.0', '>=') ){
  47.             wp_register_script( 'sa_gateways_by_location_chosen', $this->plugin_url . '/assets/js/chosen.jquery.min.js', array( 'jquery' ), $this->version );  
  48.         }
  49.         wp_register_script( 'sa_gateways_by_location_js', $this->plugin_url . '/assets/js/admin.js', array( 'jquery' ), $this->version );
  50.         wp_enqueue_style( 'sa_gateways_by_location_css', $this->plugin_url . '/assets/css/admin.css', array(), $this->version );
  51.         wp_enqueue_style( 'dashicons' );
  52.     }  
  53.  
  54.     function gateway_by_location_output ( $settings ){
  55.         global $woocommerce;
  56.         $woocommerce = function_exists('WC') ? WC() : $GLOBALS['woocommerce']; // Before WC 2.1.x
  57.  
  58.         // Enqueue chosen when WC2.3 or above
  59.         if (version_compare($woocommerce->version, '2.3.0', '>=')){
  60.             if ( ! wp_script_is( 'sa_gateways_by_location_chosen' ) ) {
  61.                 wp_enqueue_script( 'sa_gateways_by_location_chosen' );
  62.             }
  63.         }
  64.  
  65.         if ( ! wp_script_is( 'sa_gateways_by_location_js' ) ) {
  66.             wp_enqueue_script( 'sa_gateways_by_location_js' );
  67.         }
  68.            
  69.         $available_gateways = (array) $woocommerce->payment_gateways->payment_gateways();
  70.         $gateway_locations = get_option( 'sa_wc_gateways_by_location', null );
  71.         $gateway_locations_search_in = get_option( 'sa_wc_gateways_by_location_search_in', null );
  72.         if( empty( $gateway_locations_search_in ) ) {
  73.             $gateway_locations_search_in['address'] = 'billing';
  74.             update_option( 'sa_wc_gateways_by_location_search_in', $gateway_locations_search_in );
  75.         }
  76.        
  77.         ?>
  78.        
  79.         <tr valign="top">
  80.         <th scope="row" class="titledesc"><?php _e( 'Gateway By Location', $this->text_domain ) ?></th>
  81.  
  82.         <td class="forminp gateway_by_location">
  83.             <table id="gateway_by_location" class="wc_gateways_by_location widefat" cellspacing="0">
  84.             <thead>
  85.             <tr>
  86.                 <td>
  87.                     <span class='search_in'><label><?php _e( 'Address to look in', $this->text_domain ) ?></label></span>
  88.                 </td>
  89.                 <td>
  90.                 <label for="billing">
  91.                         <input type="radio" name="sa_wc_gateways_by_location_search_in[address]" value="billing" <?php ( !empty( $gateway_locations_search_in['address'] ) ) ? checked( $gateway_locations_search_in['address'], 'billing' ) : ''; ?> />
  92.                             <?php _e( 'Billing', $this->text_domain ); ?>
  93.                 </label> &nbsp;
  94.                 <label for="shipping">
  95.                         <input type="radio" name="sa_wc_gateways_by_location_search_in[address]" value="shipping" <?php ( !empty( $gateway_locations_search_in['address'] ) ) ? checked( $gateway_locations_search_in['address'], 'shipping' ) : ''; ?> />
  96.                             <?php _e( 'Shipping', $this->text_domain ); ?>
  97.                 </label>
  98.                    
  99.                 </td>  
  100.             </tr>
  101.                 <tr>
  102.                     <?php
  103.                         $columns = array(
  104.                             'gateway_name'  => __( 'Gateway', $this->text_domain ),
  105.                             'available'     => __( 'Avaliable Only In', $this->text_domain ),
  106.                             'not_available' => __( 'Not Avaliable In', $this->text_domain ),
  107.                             'action'        => __('Action', $this->text_domain),
  108.                         );
  109.  
  110.                         foreach ( $columns as $key => $column ) {
  111.                             if( esc_attr( $key ) == 'action'){
  112.                                 echo '<th class="' . esc_attr( $key ) . '" colspan="2">' . esc_html( $column ) . '</th>';
  113.                             } else {
  114.                                 echo '<th class="' . esc_attr( $key ) . '">' . esc_html( $column ) . '</th>';
  115.                             }
  116.                         }
  117.                     ?>
  118.                 </tr>
  119.                 <tbody>
  120.                     <?php
  121.                         echo '<tr class="location">';
  122.                         echo '<td>';
  123.                         echo '<select id="gateway_list">';
  124.                         echo '<option value="select">'.__( 'Select Gateway', $this->text_domain ).'</option>';
  125.                             foreach ($available_gateways as $gateway) {
  126.                                 if ( $gateway->enabled == 'yes' ) {
  127.                                     echo '<option value="' . $gateway->id . '">' . esc_html( $gateway->title ) . '</option>';                                          
  128.                                 }
  129.                             }
  130.                         echo '</select>';                          
  131.                         echo '</td>';
  132.                         echo '<td>';
  133.                         echo '<select id="avail_in" name="sa_wc_gateways_by_location['.$gateway->id.'"][available_in]" data-placeholder="' . __( 'Select Countries', $this->text_domain ) .'" multiple="multiple" class="sa_gbl_location" >';
  134.                             foreach( $woocommerce->countries->countries as $code => $country ){
  135.                                 echo '<option value="' . esc_html( $code ) . '">' . $country . '</option>';
  136.                             }
  137.                         echo '</select></td>';
  138.                         echo '<td>';
  139.                         echo '<select id="not_avail_in" name="sa_wc_gateways_by_location['.$gateway->id.'"][not_available_in]" data-placeholder="' . __( 'Select Countries', $this->text_domain ) .'" multiple="multiple" class="sa_gbl_location" >';
  140.                             foreach( $woocommerce->countries->countries as $code => $country ){
  141.                                 echo '<option value="' . esc_html( $code ) . '">' . $country . '</option>';
  142.                             }                          
  143.                         echo '</select></td>';
  144.                         echo '<td colspan="2"><a class="button add_row">'.__( 'Add Rule', $this->text_domain ).'</a></td>';
  145.                         echo '</tr>';
  146.                            
  147.                         if( !empty( $gateway_locations ) && is_array( $gateway_locations )){
  148.                             foreach ($gateway_locations as $key => $value) {
  149.                                 $avail_list = $not_avail_list = array();
  150.                                 echo '<tr><td>'. esc_html( $value['title'] ) . '</td>';
  151.                                 $avail_in = array_filter(explode(',', $value['available_in'] ));
  152.                                 $not_avail_in = array_filter(explode(',', $value['not_available_in'] ));
  153.                                
  154.                                 if( !empty( $avail_in ) ){
  155.                                     for ($i=0; $i < count( $avail_in ); $i++) {
  156.                                         if (!empty($woocommerce->countries->countries[$avail_in[$i]])) {
  157.                                             $avail_list[] = $woocommerce->countries->countries[$avail_in[$i]];
  158.                                         }
  159.                                     }
  160.                                     echo '<td>' . implode(', ', $avail_list) . '</td>';
  161.                                 } else {
  162.                                     echo '<td>'.__( 'All', $this->text_domain ).'</td>';
  163.                                 }
  164.                                
  165.                                 if( !empty( $not_avail_in ) ) {
  166.                                     for ($j=0; $j < count( $not_avail_in ); $j++) {                
  167.                                         if (!empty($woocommerce->countries->countries[$not_avail_in[$j]])) {
  168.                                             $not_avail_list[] = $woocommerce->countries->countries[$not_avail_in[$j]];
  169.                                         }
  170.                                     }
  171.                                     echo '<td>' . implode(', ', $not_avail_list) . '</td>';
  172.                                 } else {
  173.                                     echo '<td>'.__('-', $this->text_domain) .'</td>';
  174.                                 }
  175.  
  176.                                 echo '<td class="edit_row" title="'.__('Edit Rule', $this->text_domain) .'"></td>' ;
  177.                                 echo '<td class="delete_row" title="'.__('Delete Rule', $this->text_domain) .'"></td>' ;
  178.                                 echo '<input type="hidden" class="gateway_id" name="sa_wc_gateways_by_location[' . $key . '][title]" value="' . esc_html( $value['title'] ) . '">';
  179.                                 echo '<input type="hidden" class="avail_in" name="sa_wc_gateways_by_location[' . $key . '][available_in]" value="' . esc_html( $value['available_in'] ) . '">';
  180.                                 echo '<input type="hidden" class="not_avail_in" name="sa_wc_gateways_by_location[' . $key . '][not_available_in]" value="' . esc_html( $value['not_available_in'] ) . '">';
  181.                                 echo '</tr>';
  182.                             }
  183.                         }
  184.                     ?>
  185.                 </tbody>
  186.             </table>
  187.                 <p><em><?php _e( 'Not adding rule for a gateway will show it for all countries.', $this->text_domain); ?></em></p>
  188.             </td>
  189.         </tr>      
  190.         <?php
  191.     }
  192.  
  193.     function display_gateway_by_location( $payment_gateways ){
  194.        
  195.         if( isset( $_GET['page'] ) && $_GET['page'] == 'wc-settings') {
  196.             return $payment_gateways;
  197.         }
  198.  
  199.         $gateway_locations = get_option( 'sa_wc_gateways_by_location', null );
  200.         $gateway_locations_search_in = get_option( 'sa_wc_gateways_by_location_search_in', null );
  201.  
  202.         $woocommerce = function_exists('WC') ? WC() : $GLOBALS['woocommerce']; //// Before WC 2.1.x
  203.  
  204.         // Collect Customer country
  205.         if( !empty( $gateway_locations_search_in ) && $gateway_locations_search_in['address'] == 'billing' ){
  206.             $country = ( ! empty( $woocommerce->customer->country ) ) ? $woocommerce->customer->country : $woocommerce->customer->get_country();
  207.         } else {
  208.             $country = ( ! empty( $woocommerce->customer->shipping_country ) ) ? $woocommerce->customer->shipping_country : $woocommerce->customer->get_shipping_country();
  209.         }
  210.        
  211.         if (! empty($country) && !empty($gateway_locations) && is_array($gateway_locations) ) {
  212.             foreach ( $gateway_locations as $code => $location_rules ) {
  213.                 if( !empty ( $location_rules ) ) {
  214.                         if(
  215.                             ( !empty( $location_rules['available_in'] ) && strpos( ",{$location_rules['available_in']},",  ",{$country}," ) === false ) ||
  216.                             ( !empty($location_rules['not_available_in']) && strpos(  ",{$location_rules['not_available_in']},",  ",{$country}," ) !== false )
  217.                         ){
  218.                             unset( $payment_gateways[ $code ] );
  219.                         }
  220.                 }
  221.             }
  222.         }
  223.         return $payment_gateways;
  224.     }
  225.  
  226.     function save(){
  227.         if( empty( $_GET['page'] ) || $_GET['page'] != 'wc-settings' || empty($_GET['tab']) || $_GET['tab'] != 'checkout' || !empty($_GET['section']) ) {
  228.             return;        
  229.         }
  230.  
  231.         if ( array_key_exists('sa_wc_gateways_by_location', $_POST) ) {
  232.  
  233.             $gateway_by_locations = $_POST['sa_wc_gateways_by_location'];          
  234.             if( !empty( $gateway_by_locations ) && is_array( $gateway_by_locations ) ){
  235.                 update_option( 'sa_wc_gateways_by_location', $gateway_by_locations );
  236.             }
  237.  
  238.         } else {
  239.             update_option( 'sa_wc_gateways_by_location', '' );
  240.         }
  241.  
  242.         if ( array_key_exists('sa_wc_gateways_by_location_search_in', $_POST) ) {
  243.             update_option( 'sa_wc_gateways_by_location_search_in', $_POST['sa_wc_gateways_by_location_search_in'] );
  244.         } else {
  245.             update_option( 'sa_wc_gateways_by_location_search_in', '' );
  246.         }
  247.     }
  248. }
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement