Advertisement
Guest User

Untitled

a guest
Aug 1st, 2015
605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.65 KB | None | 0 0
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3.     exit;
  4. }
  5.  
  6. /**
  7.  * Required functions
  8.  */
  9. if ( ! function_exists( 'woothemes_queue_update' ) ) {
  10.     require_once( 'woo-includes/woo-functions.php' );
  11. }
  12.  
  13. /**
  14.  * Plugin updates
  15.  */
  16. woothemes_queue_update( plugin_basename( __FILE__ ), 'a6fcf35d3297c328078dfe822e00bd06', '18728' );
  17.  
  18. if ( is_woocommerce_active() ) {
  19.  
  20.     /**
  21.      * WC_Account_Funds
  22.      */
  23.     class WC_Account_Funds {
  24.  
  25.         /**
  26.          * Constructor
  27.          */
  28.         public function __construct() {
  29.             add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
  30.             add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'plugin_action_links' ) );
  31.             add_action( 'init', array( $this, 'init' ) );
  32.             add_action( 'plugins_loaded', array( $this, 'gateway_init' ), 0 );
  33.             add_action( 'widgets_init', array( $this, 'widgets_init' ) );
  34.             add_action( 'init', array( $this, 'admin_init' ) );
  35.             add_filter( 'woocommerce_payment_gateways', array( $this, 'register_gateway' ) );
  36.             add_action( 'wp_enqueue_scripts', array( $this, 'checkout_scripts' ) );
  37.  
  38.             define( 'WC_ACCOUNT_FUNDS_FILE', __FILE__ );
  39.             register_activation_hook( __FILE__, array( $this, 'activate' ) );
  40.         }
  41.  
  42.         /**
  43.          * Load plugin textdomain
  44.          */
  45.         public function load_plugin_textdomain() {
  46.             load_plugin_textdomain( 'woocommerce-account-funds', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
  47.         }
  48.  
  49.         /**
  50.          * Load classes
  51.          */
  52.         public function init() {
  53.             include_once( 'includes/class-wc-product-deposit.php' );
  54.             include_once( 'includes/class-wc-product-topup.php' );
  55.             include_once( 'includes/class-wc-account-funds-cart-manager.php' );
  56.             include_once( 'includes/class-wc-account-funds-deposits-manager.php' );
  57.             include_once( 'includes/class-wc-account-funds-order-manager.php' );
  58.             include_once( 'includes/class-wc-account-funds-integration.php' );
  59.             include_once( 'includes/class-wc-account-funds-shortcodes.php' );
  60.             include_once( 'includes/class-wc-account-funds-my-account.php' );
  61.         }
  62.  
  63.         /**
  64.          * Init Gateway
  65.          */
  66.         public function gateway_init() {
  67.             if ( ! class_exists( 'WC_Payment_Gateway' ) ) {
  68.                 return;
  69.             }
  70.             include_once( 'includes/class-wc-gateway-account-funds.php' );
  71.         }
  72.  
  73.         /**
  74.          * Load Widget
  75.          */
  76.         public function widgets_init() {
  77.             include_once( 'includes/class-wc-account-funds-widget.php' );
  78.         }
  79.  
  80.         /**
  81.          * Load admin
  82.          */
  83.         public function admin_init() {
  84.             if ( is_admin() ) {
  85.                 include_once( 'includes/class-wc-account-funds-admin.php' );
  86.                 include_once( 'includes/class-wc-account-funds-admin-product.php' );
  87.                 include_once( 'includes/class-wc-account-funds-reports.php' );
  88.             }
  89.         }
  90.  
  91.         /**
  92.          * Activation
  93.          */
  94.         public function activate() {
  95.             $old_settings = get_option( 'wcaf_settings', array(
  96.                 'give_discount'   => 0,
  97.                 'discount_type'   => 'fixed',
  98.                 'discount_amount' => 0
  99.             ) );
  100.             add_option( 'account_funds_give_discount', $old_settings['give_discount'] );
  101.             add_option( 'account_funds_discount_type', $old_settings['discount_type'] );
  102.             add_option( 'account_funds_discount_amount', $old_settings['discount_amount'] );
  103.             add_option( 'account_funds_enable_topup', 'no' );
  104.             add_option( 'account_funds_min_topup', '' );
  105.             add_option( 'account_funds_max_topup', '' );
  106.             add_option( 'account_funds_partial_payment', 'no' );
  107.         }
  108.  
  109.         /**
  110.          * Add custom action links on the plugin screen.
  111.          *
  112.          * @param   mixed $actions Plugin Actions Links
  113.          * @return  array
  114.          */
  115.         public function plugin_action_links( $actions ) {
  116.             return array_merge( array(
  117.                 'docs'      => sprintf( '<a href="%s">%s</a>', 'http://docs.woothemes.com/document/account-funds/', __( 'Docs', 'woocommerce-account-funds' ) ),
  118.                 'support'   => sprintf( '<a href="%s">%s</a>', 'http://support.woothemes.com/', __( 'Support', 'woocommerce-account-funds' ) ),
  119.                 'changelog' => sprintf( '<a href="%s" target="_blank">%s</a>', 'https://www.woothemes.com/changelogs/extensions/woocommerce-account-funds/changelog.txt', __( 'Changelog', 'woocommerce-account-funds' ) )
  120.             ), $actions );
  121.         }
  122.  
  123.         /**
  124.          * Get a users funds amount
  125.          * @param  int  $user_id
  126.          * @param  boolean $formatted
  127.          * @return string
  128.          */
  129.         public static function get_account_funds( $user_id = null, $formatted = true, $exclude_order_id = 0 ) {
  130.             $user_id = $user_id ? $user_id : get_current_user_id();
  131.  
  132.             if ( $user_id ) {
  133.                 $funds = max( 0, get_user_meta( $user_id, 'account_funds', true ) );
  134.  
  135.                 // Account for pending orders
  136.                 $orders_with_pending_funds = get_posts( array(
  137.                     'numberposts' => -1,
  138.                     'post_type'   => 'shop_order',
  139.                     'post_status' => array_keys( wc_get_order_statuses() ),
  140.                     'fields'      => 'ids',
  141.                     'meta_query'  => array(
  142.                         array(
  143.                             'key'   => '_customer_user',
  144.                             'value' => $user_id
  145.                         ),
  146.                         array(
  147.                             'key'   => '_funds_removed',
  148.                             'value' => '0',
  149.                         ),
  150.                         array(
  151.                             'key'     => '_funds_used',
  152.                             'value'   => '0',
  153.                             'compare' => '>'
  154.                         )
  155.                     )
  156.                 ) );
  157.  
  158.                 foreach ( $orders_with_pending_funds as $order_id ) {
  159.                     if ( null !== WC()->session && ! empty( WC()->session->order_awaiting_payment ) && $order_id == WC()->session->order_awaiting_payment ) {
  160.                         continue;
  161.                     }
  162.                     if ( $exclude_order_id === $order_id ) {
  163.                         continue;
  164.                     }
  165.                     $funds = $funds - floatval( get_post_meta( $order_id, '_funds_used', true ) );
  166.                 }
  167.             } else {
  168.                 $funds = 0;
  169.             }
  170.  
  171.             return $formatted ? wc_price( $funds ) : $funds;
  172.         }
  173.  
  174.         /**
  175.          * Add funds to user account
  176.          * @param int $customer_id
  177.          * @param float $amount
  178.          */
  179.         public static function add_funds( $customer_id, $amount ) {
  180.             $funds = get_user_meta( $customer_id, 'account_funds', true );
  181.             $funds = $funds ? $funds : 0;
  182.             $funds += floatval( $amount );
  183.             update_user_meta( $customer_id, 'account_funds', $funds );
  184.         }
  185.  
  186.         /**
  187.          * Remove funds from user account
  188.          * @param int $customer_id
  189.          * @param float $amount
  190.          */
  191.         public static function remove_funds( $customer_id, $amount ) {
  192.             $funds = get_user_meta( $customer_id, 'account_funds', true );
  193.             $funds = $funds ? $funds : 0;
  194.             $funds = $funds - floatval( $amount );
  195.             update_user_meta( $customer_id, 'account_funds', max( 0, $funds ) );
  196.         }
  197.  
  198.         /**
  199.          * Register the gateway for use
  200.          */
  201.         public function register_gateway( $methods ) {
  202.             $methods[] = 'WC_Gateway_Account_Funds';
  203.             return $methods;
  204.         }
  205.  
  206.         /**
  207.          * Add scripts to checkout process
  208.          */
  209.         public function checkout_scripts() {
  210.             wp_enqueue_script( 'account_funds', plugins_url( 'assets/js/account-funds.js', WC_ACCOUNT_FUNDS_FILE ), array( 'jquery' ), true );
  211.         }
  212.     }
  213.  
  214.     new WC_Account_Funds();
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement