This repository has been archived by the owner on Aug 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathwoocommerce-sponsorship.php
240 lines (187 loc) · 7.65 KB
/
woocommerce-sponsorship.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/*
Plugin Name: WooCommerce Sponsorship Add-On
Plugin URI: http://woothemes.com/woocommerce
Description: Extends WooCommerce to provide projects that can be sponsored. Requires WooCommerce 1.6+
Version: 1.0
Author: Justin Kussow (jdkussow@gmail.com) and Chris Lema (cflema@gmail.com)
Author URI: http://www.chrislema.com
Copyright: © 2009-2012 WooThemes.
License: GNU General Public License v3.0
License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
/**
* Required functions
* */
if ( ! function_exists( 'is_woocommerce_active' ) )
require_once( 'woo-includes/woo-functions.php' );
if ( is_woocommerce_active() ) {
if ( class_exists( 'WC_Sponsorship' ) ) return;
class WC_Sponsorship {
var $plugin_url;
var $plugin_path;
var $messages = array( );
public static $name = 'sponsorship';
function __construct() {
// Include required functions and classes
$this->includes();
if ( is_admin() ) {
// add admin styles and scripts
add_action( 'woocommerce_admin_css', array( &$this, 'admin_styles' ) );
add_action( 'admin_enqueue_scripts', array( &$this, 'admin_scripts' ) );
add_action( 'admin_menu', array( &$this, 'menu' ) );
} else {
// add front end styles and scripts
add_action( 'wp_print_scripts', array( &$this, 'frontend_scripts' ) );
}
add_filter( 'add_to_cart_redirect', array( &$this, 'add_product_redirect' ) );
add_filter( 'woocommerce_add_to_cart_validation', array( &$this, 'add_product_validation' ), 10, 3 );
// some sidebar stuff
add_action( 'wp_head', create_function( "", 'ob_start();' ) );
add_action( 'get_sidebar', array( &$this, 'get_wc_sponsorship_sidebar' ) );
add_action( 'wp_footer', array( &$this, 'wc_sponsorship_sidebar_class_replace' ) );
register_sidebar( array( 'name' => 'Sponsorship Sidebar', 'id' => 'sponsorship_project_sidebar', 'description' => "Sidebar that overrides other sidebars when viewing a single product", ) );
// and a widget
add_action( 'widgets_init', array( &$this, 'init_widgets' ) );
}
/*
* Plugin maintenance - install and uninstall
*/
function activation() {
//wp_create_category('Sponsorship Project', 'product_cat');
}
function deactivation() {
}
/*
* Include required functions and classes
*/
function includes() {
include_once( 'classes/class-wc-sponsorship-product.php' );
include_once( 'classes/class-wc-sponsorship-order.php' );
include_once( 'classes/class-wc-sponsorship-checkout.php' );
}
/*
* Admin - include necessary admin files, scripts, styles and handle any
* admin related actions and filters
*/
function admin_styles() {
wp_enqueue_style( 'woocommerce_sponsorship_admin_styles', $this->plugin_url() . '/assets/css/admin.css' );
}
function admin_scripts() {
//wp_register_script( 'wc-sponsorship-product-js', $this->plugin_url() . '/assets/js/wc-sponsorship-admin-product.js' );
//wp_enqueue_script( 'wc-sponsorship-product-js' );
}
function menu() { }
/*
* Front end - include scripts, styles, and handle any front end related
* handlers for actions or filters
*/
function frontend_scripts() {
wp_enqueue_style( 'woocommerce_sponsorship_styles', $this->plugin_url() . '/assets/css/wc-sponsorship.css' );
}
/*
* Helper functions
*/
function plugin_url() {
if ( $this->plugin_url ) return $this->plugin_url;
return $this->plugin_url = plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) );
}
function plugin_path() {
if ( $this->plugin_path ) return $this->plugin_path;
return $this->plugin_path = untrailingslashit( plugin_dir_path( __FILE__ ) );
}
function is_sponsorship( $product ) {
if ( !is_object( $product ) ) {
$product = new WC_Product_Variable( $product );
}
if ( $product->is_type('variable') || $product->is_type('simple') ) {
$pm = get_post_meta( $product->id, '_sponsorship', true );
return is_array( $pm );
}
return ( $product->is_type( 'sponsorship-project' ) ) ? true : false;
}
function is_sponsorship_contribution_level( $product ) {
if ( !is_object( $product ) ) {
$product = new WC_Product_Variable( $product );
}
$prod_post = $product->post;
if ( !$prod_post ) {
$prod_post = get_post( $product->id );
}
if ( $prod_post->post_parent ) {
return WC_Sponsorship::is_sponsorship( $prod_post->post_parent );
}
return false;
}
function cart_contains_sponsorship_contribution() {
global $woocommerce;
$contains_contribution = false;
if ( !empty( $woocommerce->cart->cart_contents ) ) {
foreach ( $woocommerce->cart->cart_contents as $cart_item ) {
if ( WC_Sponsorship_Product::is_sponsorship_contribution_level( $cart_item[ 'product_id' ] ) ) {
$contains_contribution = true;
break;
}
}
}
return $contains_contribution;
}
function add_product_redirect( $url ) {
global $woocommerce;
if ( is_numeric( $_REQUEST[ 'add-to-cart' ] ) && WC_Sponsorship_Product::is_sponsorship_contribution_level( ( int ) $_REQUEST[ 'add-to-cart' ] ) ) {
$woocommerce->clear_messages();
$url = $woocommerce->cart->get_checkout_url();
}
return $url;
}
function add_product_validation( $valid, $product_id, $quantity ) {
global $woocommerce;
if ( WC_Sponsorship_Product::is_sponsorship_contribution_level( $product_id ) ) {
$woocommerce->cart->empty_cart();
} elseif ( WC_Sponsorship::cart_contains_sponsorship_contribution() ) {
WC_Sponsorship::remove_sponsorship_from_cart();
$woocommerce->add_error( 'A sponsorship contribution has been removed from your cart. Due to payment gateway restrictions, products can not be purchased at the same time.' );
$woocommerce->set_messages();
// Redirect to cart page to remove subscription & notify shopper
add_filter( 'add_to_cart_fragments', array( &$this, 'redirect_ajax_add_to_cart' ) );
}
return true;
}
function remove_sponsorship_from_cart() {
global $woocommerce;
foreach ( $woocommerce->cart->cart_contents as $cart_item_key => $cart_item ) if ( WC_Sponsorship_Product::is_sponsorship_contribution_level( $cart_item[ 'product_id' ] ) ) $woocommerce->cart->set_quantity( $cart_item_key, 0 );
}
function get_wc_sponsorship_sidebar( $name ) {
global $product;
if ( is_single() && WC_Sponsorship_Product::is_sponsorship( $product ) && 'Sponsorship Sidebar' != $name ) {
load_template( $this->plugin_path() . '/classes/class-wc-sponsorship-sidebar.php', true );
static $class = "wc-sponsorship-hidden";
$this->wc_sponsorship_sidebar_class_replace( $class );
}
}
function wc_sponsorship_sidebar_class_replace( $c = '' ) {
static $class = '';
if ( !empty( $c ) ) {
$class = $c;
} else {
echo str_replace( '<div id="sidebar">', '<div id="sidebar" class="' . $class . '"> ', ob_get_clean() );
ob_start();
}
}
function init_widgets() {
if ( !class_exists( !'WC_Sponsorship_Levels_Widget' ) )
require_once ( $this->plugin_path() . '/classes/widgets/class-wc-sponsorship-levels-widget.php');
register_widget( 'WC_Sponsorship_Levels_Widget' );
if ( !class_exists( !'WC_Sponsorship_Project_Status_Widget' ) )
require_once ( $this->plugin_path() . '/classes/widgets/class-wc-sponsorship-project-status-widget.php');
register_widget( 'WC_Sponsorship_Project_Status_Widget' );
}
}
// end WC_Sponsorship
// Init the main class
$GLOBALS[ 'wc_sponsorship' ] = new WC_Sponsorship();
// Hook into activation
register_activation_hook( __FILE__, array( $GLOBALS[ 'wc_sponsorship' ], 'activation' ) );
register_deactivation_hook( __FILE__, array( $GLOBALS[ 'wc_sponsorship' ], 'deactivation' ) );
} // end if ( is_woocommerce_active() )
?>