woocommerce/includes/class-wc-payment-gateways.php

170 lines
3.5 KiB
PHP
Raw Normal View History

2011-08-09 15:16:18 +00:00
<?php
/**
2011-08-10 17:11:11 +00:00
* WooCommerce Payment Gateways class
2012-08-15 18:15:06 +00:00
*
2011-08-10 17:11:11 +00:00
* Loads payment gateways via hooks for use in the store.
*
2012-01-27 16:38:39 +00:00
* @class WC_Payment_Gateways
2012-08-15 18:15:06 +00:00
* @version 1.6.4
* @package WooCommerce/Classes/Payment
2013-02-20 17:14:46 +00:00
* @category Class
2012-08-15 18:15:06 +00:00
* @author WooThemes
2011-08-10 17:11:11 +00:00
*/
2012-01-27 16:38:39 +00:00
class WC_Payment_Gateways {
2012-08-15 18:15:06 +00:00
/** @var array Array of payment gateway classes. */
var $payment_gateways;
2012-08-15 18:15:06 +00:00
/**
* @var WooCommerce The single instance of the class
* @since 2.1
*/
protected static $_instance = null;
/**
* Main WooCommerce Instance
*
* Ensures only one instance of WooCommerce is loaded or can be loaded.
*
* @since 2.1
* @static
* @see WC()
* @return Main WooCommerce instance
*/
public static function instance() {
if ( is_null( self::$_instance ) )
self::$_instance = new self();
return self::$_instance;
}
/**
* Cloning is forbidden.
*
* @since 2.1
*/
public function __clone() {
_doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?' ), '2.1' );
}
/**
* Unserializing instances of this class is forbidden.
*
* @since 2.1
*/
public function __wakeup() {
_doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?' ), '2.1' );
}
/**
* __construct function.
*
* @access public
* @return void
*/
public function __construct() {
$this->init();
}
2012-08-15 18:15:06 +00:00
/**
* Load gateways and hook in functions.
*
* @access public
* @return void
*/
function init() {
2012-08-15 18:15:06 +00:00
$load_gateways = apply_filters( 'woocommerce_payment_gateways', array(
'WC_Gateway_BACS',
'WC_Gateway_Cheque',
'WC_Gateway_COD',
'WC_Gateway_Mijireh',
'WC_Gateway_Paypal'
) );
2012-08-15 18:15:06 +00:00
2011-12-06 16:45:08 +00:00
// Get order option
$ordering = (array) get_option('woocommerce_gateway_order');
$order_end = 999;
2012-08-15 18:15:06 +00:00
2011-12-06 16:45:08 +00:00
// Load gateways in order
foreach ($load_gateways as $gateway) :
2012-08-15 18:15:06 +00:00
2012-01-10 15:11:06 +00:00
$load_gateway = new $gateway();
2012-08-15 18:15:06 +00:00
2011-12-06 16:45:08 +00:00
if (isset($ordering[$load_gateway->id]) && is_numeric($ordering[$load_gateway->id])) :
// Add in position
$this->payment_gateways[$ordering[$load_gateway->id]] = $load_gateway;
else :
// Add to end of the array
$this->payment_gateways[$order_end] = $load_gateway;
$order_end++;
endif;
2012-08-15 18:15:06 +00:00
2011-08-09 15:16:18 +00:00
endforeach;
2012-08-15 18:15:06 +00:00
ksort( $this->payment_gateways );
2011-08-09 15:16:18 +00:00
}
2012-08-15 18:15:06 +00:00
/**
* Get gateways.
*
* @access public
* @return array
*/
2011-08-09 15:16:18 +00:00
function payment_gateways() {
2012-08-15 18:15:06 +00:00
2011-08-09 15:16:18 +00:00
$_available_gateways = array();
2012-08-15 18:15:06 +00:00
if ( sizeof( $this->payment_gateways ) > 0 )
foreach ( $this->payment_gateways as $gateway )
$_available_gateways[ $gateway->id ] = $gateway;
2011-08-09 15:16:18 +00:00
return $_available_gateways;
}
2012-08-15 18:15:06 +00:00
/**
* Get available gateways.
*
* @access public
* @return array
*/
2011-08-09 15:16:18 +00:00
function get_available_payment_gateways() {
2012-08-15 18:15:06 +00:00
2011-08-09 15:16:18 +00:00
$_available_gateways = array();
2012-08-15 18:15:06 +00:00
foreach ( $this->payment_gateways as $gateway ) :
2012-08-15 18:15:06 +00:00
2011-08-09 15:16:18 +00:00
if ($gateway->is_available()) $_available_gateways[$gateway->id] = $gateway;
2012-08-15 18:15:06 +00:00
2011-08-09 15:16:18 +00:00
endforeach;
return apply_filters( 'woocommerce_available_payment_gateways', $_available_gateways );
2011-08-09 15:16:18 +00:00
}
2012-08-15 18:15:06 +00:00
/**
* Save options in admin.
*
* @access public
* @return void
*/
2011-12-06 16:45:08 +00:00
function process_admin_options() {
2012-08-15 18:15:06 +00:00
2012-10-17 12:46:35 +00:00
$default_gateway = ( isset( $_POST['default_gateway'] ) ) ? esc_attr( $_POST['default_gateway'] ) : '';
$gateway_order = ( isset( $_POST['gateway_order'] ) ) ? $_POST['gateway_order'] : '';
2012-08-15 18:15:06 +00:00
2011-12-06 16:45:08 +00:00
$order = array();
2012-08-15 18:15:06 +00:00
2012-10-17 12:46:35 +00:00
if ( is_array( $gateway_order ) && sizeof( $gateway_order ) > 0 ) {
2011-12-06 16:45:08 +00:00
$loop = 0;
2012-10-17 12:46:35 +00:00
foreach ( $gateway_order as $gateway_id ) {
$order[ esc_attr( $gateway_id ) ] = $loop;
2011-12-06 16:45:08 +00:00
$loop++;
2012-10-17 12:46:35 +00:00
}
}
2012-08-15 18:15:06 +00:00
2011-12-06 16:45:08 +00:00
update_option( 'woocommerce_default_gateway', $default_gateway );
update_option( 'woocommerce_gateway_order', $order );
}
2011-08-09 15:16:18 +00:00
}