forked from wp-plugins/woocommerce-role-pricing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwoo-role-pricing-light.php
executable file
·264 lines (209 loc) · 7.65 KB
/
woo-role-pricing-light.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
/**
* woo-role-pricing-light.php
*
* Copyright (c) 2011,2012 Antonio Blanco http://www.blancoleon.com
*
* This code is released under the GNU General Public License.
* See COPYRIGHT.txt and LICENSE.txt.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This header and all notices must be kept intact.
*
* @author Antonio Blanco
* @package woorolepricinglight
* @since woorolepricinglight 1.0.0
*
* Plugin Name: Woocommerce Role Pricing Light
* Plugin URI: http://www.eggemplo.com/plugins/woocommerce-role-pricing
* Description: Shows different prices according to the user's role
* Version: 1.0
* Author: eggemplo
* Author URI: http://www.eggemplo.com
* License: GPLv3
*/
define( 'WOO_ROLE_PRICING_LIGHT_DOMAIN', 'woorolepricing' );
define( 'WOO_ROLE_PRICING_LIGHT_PLUGIN_NAME', 'woo-role-pricing-light' );
define( 'WOO_ROLE_PRICING_LIGHT_FILE', __FILE__ );
if ( !defined( 'WOO_ROLE_PRICING_LIGHT_CORE_DIR' ) ) {
define( 'WOO_ROLE_PRICING_LIGHT_CORE_DIR', WP_PLUGIN_DIR . '/woocommerce-role-pricing/core' );
}
define ( 'WOO_ROLE_PRICING_LIGHT_DECIMALS', apply_filters( 'woo_role_pricing_num_decimals', 2 ) );
class WooRolePricingLight_Plugin {
private static $notices = array();
public static function init() {
load_plugin_textdomain( WOO_ROLE_PRICING_LIGHT_DOMAIN, null, WOO_ROLE_PRICING_LIGHT_PLUGIN_NAME . '/languages' );
register_activation_hook( WOO_ROLE_PRICING_LIGHT_FILE, array( __CLASS__, 'activate' ) );
register_deactivation_hook( WOO_ROLE_PRICING_LIGHT_FILE, array( __CLASS__, 'deactivate' ) );
register_uninstall_hook( WOO_ROLE_PRICING_LIGHT_FILE, array( __CLASS__, 'uninstall' ) );
add_action( 'init', array( __CLASS__, 'wp_init' ) );
add_action( 'admin_notices', array( __CLASS__, 'admin_notices' ) );
}
public static function wp_init() {
if ( is_multisite() ) {
$active_plugins_sitewide = get_site_option( 'active_sitewide_plugins', array() );
$active_plugins = array_merge(array_keys($active_plugins_sitewide), get_option( 'active_plugins', array() ));
} else {
$active_plugins = get_option( 'active_plugins', array() );
}
$woo_is_active = in_array( 'woocommerce/woocommerce.php', $active_plugins );
if ( !$woo_is_active ) {
self::$notices[] = "<div class='error'>" . __( 'The <strong>Woocommerce Role Pricing Light</strong> plugin requires the <a href="http://wordpress.org/extend/plugins/woocommerce" target="_blank">Woocommerce</a> plugin to be activated.', WOO_ROLE_PRICING_LIGHT_DOMAIN ) . "</div>";
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
deactivate_plugins( array( __FILE__ ) );
} else {
add_action( 'admin_menu', array( __CLASS__, 'admin_menu' ), 40 );
//call register settings function
add_action( 'admin_init', array( __CLASS__, 'register_woorolepricinglight_settings' ) );
if ( !class_exists( "WooRolePricingLight" ) ) {
include_once 'core/class-woorolepricinglight.php';
}
}
}
public static function register_woorolepricinglight_settings() {
register_setting( 'woorolepricinglight', 'wrp-method' );
add_option( 'wrp-method','rate' ); // by default rate
register_setting( 'woorolepricinglight', 'wrp-baseprice' );
add_option( 'wrp-baseprice','regular' ); // by default regular
}
public static function admin_notices() {
if ( !empty( self::$notices ) ) {
foreach ( self::$notices as $notice ) {
echo $notice;
}
}
}
/**
* Adds the admin section.
*/
public static function admin_menu() {
$admin_page = add_submenu_page(
'woocommerce',
__( 'Role Pricing Light' ),
__( 'Role Pricing Light' ),
'manage_options',
'woorolepricinglight',
array( __CLASS__, 'woorolepricinglight_settings' )
);
}
public static function woorolepricinglight_settings () {
?>
<div class="wrap">
<h2><?php echo __( 'Woocommerce Role Pricing Light', WOO_ROLE_PRICING_LIGHT_DOMAIN ); ?></h2>
<?php
$alert = "";
global $wp_roles;
if ( class_exists( 'WP_Roles' ) ) {
if ( ! isset( $wp_roles ) ) {
$wp_roles = new WP_Roles();
}
}
if ( isset( $_POST['submit'] ) ) {
$alert = __("Saved", WOO_ROLE_PRICING_LIGHT_DOMAIN);
add_option( "wrp-method",$_POST[ "wrp-method" ] );
update_option( "wrp-method", $_POST[ "wrp-method" ] );
add_option( "wrp-baseprice",$_POST[ "wrp-baseprice" ] );
update_option( "wrp-baseprice", $_POST[ "wrp-baseprice" ] );
foreach ( $wp_roles->role_objects as $role ) {
if ( isset( $_POST[ "wrp-" . $role->name ] ) && ( $_POST[ "wrp-" . $role->name ] !== "" ) ) {
add_option( "wrp-" . $role->name,$_POST[ "wrp-" . $role->name ] );
update_option( "wrp-" . $role->name, $_POST[ "wrp-" . $role->name ] );
} else {
delete_option( "wrp-" . $role->name );
}
}
}
if ($alert != "")
echo '<div style="background-color: #ffffe0;border: 1px solid #993;padding: 1em;margin-right: 1em;">' . $alert . '</div>';
?>
<div class="wrap" style="border: 1px solid #ccc; padding:10px;">
<form method="post" action="">
<table class="form-table">
<tr valign="top">
<th scope="row"><strong><?php echo __( 'Products discount method:', WOO_ROLE_PRICING_LIGHT_DOMAIN ); ?></strong></th>
<td>
<select name="wrp-method">
<?php
if ( get_option("wrp-method") == "amount" ) {
?>
<option value="rate">Rate</option>
<option value="amount" selected="selected">Amount</option>
<?php
} else {
?>
<option value="rate" selected="selected">Rate</option>
<option value="amount">Amount</option>
<?php
}
?>
</select>
</tr>
<tr valign="top">
<th scope="row"><strong><?php echo __( 'Apply to:', WOO_ROLE_PRICING_LIGHT_DOMAIN ); ?></strong></th>
<td>
<select name="wrp-baseprice">
<?php
if ( get_option("wrp-baseprice") == "sale" ) {
?>
<option value="regular">Regular price</option>
<option value="sale" selected="selected">Sale price</option>
<?php
} else {
?>
<option value="regular" selected="selected">Regular price</option>
<option value="sale">Sale price</option>
<?php
}
?>
</select>
</tr>
</table>
<h3><?php echo __( 'Roles:', WOO_ROLE_PRICING_LIGHT_DOMAIN ); ?></h3>
<div class="description">Leave empty if no role discount should be applied (default setting).<br>
Example with rate method: Indicate 0.1 for 10% discounts on every product.
</div>
<table class="form-table">
<?php
foreach ( $wp_roles->role_objects as $role ) {
?>
<tr valign="top">
<th scope="row"><?php echo ucwords($role->name) . ':'; ?></th>
<td>
<input type="text" name="wrp-<?php echo $role->name;?>" value="<?php echo get_option( "wrp-" . $role->name ); ?>" />
</td>
</tr>
<?php
}
?>
</table>
<?php submit_button( __( "Save", WOO_ROLE_PRICING_LIGHT_DOMAIN ) ); ?>
<?php settings_fields( 'woorolepricinglight' ); ?>
</form>
</div>
</div>
<?php
}
/**
* Plugin activation work.
*
*/
public static function activate() {
}
/**
* Plugin deactivation.
*
*/
public static function deactivate() {
}
/**
* Plugin uninstall. Delete database table.
*
*/
public static function uninstall() {
}
}
WooRolePricingLight_Plugin::init();