forked from wpswings/woo-gift-cards-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwoocommerce_gift_cards_lite.php
284 lines (267 loc) · 8.89 KB
/
woocommerce_gift_cards_lite.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
/**
* The plugin bootstrap file
*
* This file is read by WordPress to generate the plugin information in the plugin
* admin area. This file also includes all of the dependencies used by the plugin,
* registers the activation and deactivation functions, and defines a function
* that starts the plugin.
*
* @link http://makewebbetter.com/
* @since 1.0.0
* @package Woocommerce_gift_cards_lite
*
* @wordpress-plugin
* Plugin Name: Woocommerce Gift Cards Lite
* Plugin URI: http://makewebbetter.com/woocommerce-gift-cards-lite
* Description: Woocommerce Gift Cards Lite allows merchants to create and sell multiple Gift Card Product having multiple price variation
* Version: 1.0.0
* Author: makewebbetter
* Author URI: http://makewebbetter.com/
* License: GPL-3.0+
* License URI: http://www.gnu.org/licenses/gpl-3.0.txt
* Text Domain: woocommerce_gift_cards_lite
* Tested up to: 4.9
* WC tested up to: 3.3.3
* Domain Path: /languages
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
$activated = true;
$mwb_wgc_pro_plugin_active = false;
if ( function_exists( 'is_multisite' ) && is_multisite() ){
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if ( !is_plugin_active( 'woocommerce/woocommerce.php' ) ){
$activated = false;
}
if ( !is_plugin_active( 'woocommerce-ultimate-gift-card/woocommerce-ultimate-gift-card.php' ) ){
$activated = false;
$mwb_wgc_pro_plugin_active = true;
}
}
else{
if ( !in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) )) ){
$activated = false;
}
if ( in_array( 'woocommerce-ultimate-gift-card/woocommerce-ultimate-gift-card.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) )) ){
$activated = false;
$mwb_wgc_pro_plugin_active = true;
}
}
/**
* Checking if WooCommerce is active
**/
if ($activated && !$mwb_wgc_pro_plugin_active){
define( 'MWB_WGC_DIRPATH', plugin_dir_path( __FILE__ ) );
define( 'MWB_WGC_URL', plugin_dir_url( __FILE__ ) );
define( 'MWB_WGC_ADMIN_URL', admin_url() );
/**
* The core plugin class that is used to define internationalization,
* admin-specific hooks, and public-facing site hooks.
*/
require plugin_dir_path( __FILE__ ) . 'includes/class-woocommerce_gift_cards_lite.php';
/**
add link for settings
*/
add_filter ( 'plugin_action_links','mwb_wgc_admin_settings', 10, 5 );
/**
* Adds the Setting Links
*
* @name mwb_wgc_admin_settings
* @author makewebbetter<[email protected]>
* @link http://www.makewebbetter.com/
*/
function mwb_wgc_admin_settings($actions, $plugin_file){
static $plugin;
if (! isset ( $plugin )) {
$plugin = plugin_basename ( __FILE__ );
}
if ($plugin == $plugin_file) {
$settings = array (
'settings' => '<a href="' . admin_url ( 'admin.php?page=mwb-wgc-setting-lite' ) . '">' . __ ( 'Settings', 'woocommerce_gift_cards_lite' ) . '</a>',
'get_paid_version' => '<a href="https://codecanyon.net/item/woocommerce-ultimate-gift-card/19191057?s_rank=24" target="_blank">' . __ ( 'Get Premium Version', 'woocommerce_gift_cards_lite' ) . '</a>'
);
$actions = array_merge ( $settings, $actions );
}
return $actions;
}
/**
* Checks the Plugin is enable or not
*
* @name mwb_wgc_giftcard_enable
* @author makewebbetter<[email protected]>
* @link http://www.makewebbetter.com/
*/
function mwb_wgc_giftcard_enable()
{
$giftcard_enable = get_option("mwb_wgm_general_setting_enable", false);
if($giftcard_enable == "on"){
return true;
}
else{
return false;
}
}
register_activation_hook(__FILE__, 'mwb_wgc_create_gift_card_taxonomy');
/**
* Create the Taxonomy for Gift Card Product at activation
*
* @name mwb_wgc_create_gift_card_taxonomy
* @author makewebbetter<[email protected]>
* @link http://www.makewebbetter.com/
*/
function mwb_wgc_create_gift_card_taxonomy(){
$page_taxonomy_created = get_option("mwb_wgc_create_gift_card_taxonomy", false);
if($page_taxonomy_created == false){
update_option("mwb_wgc_create_gift_card_taxonomy", true);
$term = __('Gift Card', 'woocommerce_gift_cards_lite' );
$taxonomy = 'product_cat';
$term_exist = term_exists( $term, $taxonomy);
if ($term_exist == 0 || $term_exist == null){
$args['slug'] = "mwb_wgm_giftcard";
$term_exist = wp_insert_term( $term, $taxonomy, $args );
}
$terms = get_term( $term_exist['term_id'], $taxonomy, ARRAY_A);
$giftcard_category = $terms['slug'];
$giftcard_content = "[product_category category='$giftcard_category']";
$customer_reports = array(
'post_author' => get_current_user_id(),
'post_name' => __('Gift Card', 'woocommerce_gift_cards_lite'),
'post_title' => __('Gift Card', 'woocommerce_gift_cards_lite'),
'post_type' => 'page',
'post_status' => 'publish',
'post_content' => $giftcard_content
);
$page_id = wp_insert_post($customer_reports);
}
}
//on plugin load
add_action( 'plugins_loaded', 'mwb_wgc_register_gift_card_product_type' );
/**
* Saving the Product Type by creating the Instance of this
*
* @name mwb_wgc_register_gift_card_product_type
* @author makewebbetter<[email protected]>
* @link http://www.makewebbetter.com/
*/
function mwb_wgc_register_gift_card_product_type()
{
class WC_Product_Wgm_gift_card extends WC_Product {
/**
* Initialize simple product.
*
* @param mixed $product
*/
public function __construct( $product ) {
$this->product_type = 'wgm_gift_card';
parent::__construct( $product );
}
}
}
/**
* Generate the Dynamic code for Gift Cards
*
* @name mwb_wgc_coupon_generator
* @author makewebbetter<[email protected]>
* @link http://www.makewebbetter.com/
*/
function mwb_wgc_coupon_generator($length = 5){
if( $length == "" ){
$length = 5;
}
$password = '';
$alphabets = range('A','Z');
$numbers = range('0','9');
$final_array = array_merge($alphabets,$numbers);
while($length--)
{
$key = array_rand($final_array);
$password .= $final_array[$key];
}
$giftcard_prefix = get_option('mwb_wgm_general_setting_giftcard_prefix', '');
$password = $giftcard_prefix.$password;
$password = apply_filters('mwb_wgm_custom_coupon', $password);
return $password;
}
/**
* Begins execution of the plugin.
*
* Since everything within the plugin is registered via hooks,
* then kicking off the plugin from this point in the file does
* not affect the page life cycle.
*
* @since 1.0.0
*/
function run_woocommerce_gift_cards_lite(){
$plugin = new Woocommerce_gift_cards_lite();
$plugin->run();
}
run_woocommerce_gift_cards_lite();
}
else{
if($mwb_wgc_pro_plugin_active){
//deactivate if Woocommerce Ultimate Gift Card is already active
add_action( 'admin_init', 'mwb_wgc_deactivate_pro_is_activated' );
}else{
//deactivate if Woocommerce is not activated
add_action( 'admin_init', 'mwb_wgc_plugin_deactivate' );
}
/**
* Show warning message if woocommerce is not install
* @since 1.0.0
* @name mwb_wgc_plugin_error_notice()
* @author makewebbetter<[email protected]>
* @link http://www.makewebbetter.com/
*/
function mwb_wgc_plugin_error_notice(){
?>
<div class="error notice is-dismissible">
<p><?php _e( 'Woocommerce is not activated, Please activate Woocommerce first to install WooCommerce Gift Cards Lite.', 'woocommerce_gift_cards_lite' ); ?></p>
</div>
<style>
#message{display:none;}
</style>
<?php
}
/**
* Show warning message if Woocommerce Ultimate Gift Card is already active
* @since 1.0.0
* @name mwb_wgc_plugin_error_notice_for_pro()
* @author makewebbetter<[email protected]>
* @link http://www.makewebbetter.com/
*/
function mwb_wgc_plugin_error_notice_for_pro(){
?>
<div class="error notice is-dismissible">
<p><?php _e( 'Woocommerce Gift Cards Lite deactivated successfully, As you have activated its Pro Version(Woocommerce Ultimate Gift Card)', 'woocommerce_gift_cards_lite' ); ?></p>
</div>
<style>
#message{display:none;}
</style>
<?php
}
/**
* Call Admin notices for Woocommerce
*
* @name mwb_wgc_plugin_deactivate()
* @author makewebbetter<[email protected]>
* @link http://www.makewebbetter.com/
*/
function mwb_wgc_plugin_deactivate(){
deactivate_plugins( plugin_basename( __FILE__ ) );
add_action( 'admin_notices', 'mwb_wgc_plugin_error_notice' );
}
/**
* Call Admin notices for Pro version
*
* @name mwb_wgc_deactivate_pro_is_activated()
* @author makewebbetter<[email protected]>
* @link http://www.makewebbetter.com/
*/
function mwb_wgc_deactivate_pro_is_activated(){
deactivate_plugins( plugin_basename( __FILE__ ) );
add_action( 'admin_notices', 'mwb_wgc_plugin_error_notice_for_pro' );
}
}