1 <?php
  2 /**
  3  * WooCommerce Payment Gateways class
  4  *
  5  * Loads payment gateways via hooks for use in the store.
  6  *
  7  * @class       WC_Payment_Gateways
  8  * @version     1.6.4
  9  * @package     WooCommerce/Classes/Payment
 10  * @category    Class
 11  * @author      WooThemes
 12  */
 13 class WC_Payment_Gateways {
 14 
 15     /** @var array Array of payment gateway classes. */
 16     var $payment_gateways;
 17 
 18     /**
 19      * @var WooCommerce The single instance of the class
 20      * @since 2.1
 21      */
 22     protected static $_instance = null;
 23 
 24     /**
 25      * Main WooCommerce Instance
 26      *
 27      * Ensures only one instance of WooCommerce is loaded or can be loaded.
 28      *
 29      * @since 2.1
 30      * @static
 31      * @see WC()
 32      * @return Main WooCommerce instance
 33      */
 34     public static function instance() {
 35         if ( is_null( self::$_instance ) )
 36             self::$_instance = new self();
 37         return self::$_instance;
 38     }
 39 
 40     /**
 41      * Cloning is forbidden.
 42      *
 43      * @since 2.1
 44      */
 45     public function __clone() {
 46         _doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'woocommerce' ), '2.1' );
 47     }
 48 
 49     /**
 50      * Unserializing instances of this class is forbidden.
 51      *
 52      * @since 2.1
 53      */
 54     public function __wakeup() {
 55         _doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'woocommerce' ), '2.1' );
 56     }
 57 
 58     /**
 59      * __construct function.
 60      *
 61      * @access public
 62      * @return void
 63      */
 64     public function __construct() {
 65         $this->init();
 66     }
 67 
 68     /**
 69      * Load gateways and hook in functions.
 70      *
 71      * @access public
 72      * @return void
 73      */
 74     function init() {
 75 
 76         $load_gateways = apply_filters( 'woocommerce_payment_gateways', array(
 77             'WC_Gateway_BACS',
 78             'WC_Gateway_Cheque',
 79             'WC_Gateway_COD',
 80             'WC_Gateway_Mijireh',
 81             'WC_Gateway_Paypal'
 82         ) );
 83 
 84         // Get order option
 85         $ordering   = (array) get_option('woocommerce_gateway_order');
 86         $order_end  = 999;
 87 
 88         // Load gateways in order
 89         foreach ($load_gateways as $gateway) :
 90 
 91             $load_gateway = new $gateway();
 92 
 93             if (isset($ordering[$load_gateway->id]) && is_numeric($ordering[$load_gateway->id])) :
 94                 // Add in position
 95                 $this->payment_gateways[$ordering[$load_gateway->id]] = $load_gateway;
 96             else :
 97                 // Add to end of the array
 98                 $this->payment_gateways[$order_end] = $load_gateway;
 99                 $order_end++;
100             endif;
101 
102         endforeach;
103 
104         ksort( $this->payment_gateways );
105     }
106 
107 
108     /**
109      * Get gateways.
110      *
111      * @access public
112      * @return array
113      */
114     function payment_gateways() {
115 
116         $_available_gateways = array();
117 
118         if ( sizeof( $this->payment_gateways ) > 0 )
119             foreach ( $this->payment_gateways as $gateway )
120                 $_available_gateways[ $gateway->id ] = $gateway;
121 
122         return $_available_gateways;
123     }
124 
125 
126     /**
127      * Get available gateways.
128      *
129      * @access public
130      * @return array
131      */
132     function get_available_payment_gateways() {
133 
134         $_available_gateways = array();
135 
136         foreach ( $this->payment_gateways as $gateway ) :
137 
138             if ( $gateway->is_available() ) {
139                 if ( ! is_add_payment_method_page() )
140                     $_available_gateways[$gateway->id] = $gateway;
141                 elseif( $gateway->supports( 'add_payment_method' ) )
142                     $_available_gateways[$gateway->id] = $gateway;
143             }
144 
145         endforeach;
146 
147         return apply_filters( 'woocommerce_available_payment_gateways', $_available_gateways );
148     }
149 
150 
151     /**
152      * Save options in admin.
153      *
154      * @access public
155      * @return void
156      */
157     function process_admin_options() {
158 
159         $default_gateway = ( isset( $_POST['default_gateway'] ) ) ? esc_attr( $_POST['default_gateway'] ) : '';
160         $gateway_order = ( isset( $_POST['gateway_order'] ) ) ? $_POST['gateway_order'] : '';
161 
162         $order = array();
163 
164         if ( is_array( $gateway_order ) && sizeof( $gateway_order ) > 0 ) {
165             $loop = 0;
166             foreach ( $gateway_order as $gateway_id ) {
167                 $order[ esc_attr( $gateway_id ) ] = $loop;
168                 $loop++;
169             }
170         }
171 
172         update_option( 'woocommerce_default_gateway', $default_gateway );
173         update_option( 'woocommerce_gateway_order', $order );
174     }
175 }
WooCommerce API documentation generated by ApiGen 2.8.0