-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame-woocommerce-gift-cards.php
409 lines (342 loc) · 13.1 KB
/
frame-woocommerce-gift-cards.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
<?php
/*
Plugin Name: Frame WooCommerce Gift Cards
Plugin URI: https://bitbucket.org/framecreative/woocommerce-gift-cards
Description: Allow your customers to send gift cards.
Version: 3.0.2
Author: Frame Creative
Author URI: http://framecreative.com.au
GitHub Plugin URI: https://bitbucket.org/framecreative/woocommerce-gift-cards
*/
if (! defined('ABSPATH')) {
exit;
}
class WC_Gift_Cards
{
public $plugin_version = '3.0.2';
public $plugin_prefix;
public $plugin_url;
public $plugin_path;
public $plugin_basefile;
public $plugin_basefile_path;
public $plugin_template_path;
public $theme_template_path;
public $post_type = 'wc_gift_card';
/**
* Instance of WCGC_Auto_Send
* @var class
*/
public $auto_send;
/**
* Constructor
*/
public function __construct()
{
// Define the constants
$this->plugin_prefix = 'wcgc_';
$this->plugin_basefile_path = __FILE__;
$this->plugin_basefile = plugin_basename($this->plugin_basefile_path);
$this->plugin_url = plugin_dir_url($this->plugin_basefile);
$this->plugin_path = trailingslashit(dirname($this->plugin_basefile_path));
$this->plugin_template_path = $this->plugin_path . 'templates/';
$this->theme_template_path = 'woocommerce/gift-cards/';
// Load after WooCommerce
add_action('woocommerce_init', [ $this, 'load' ]);
}
/**
*
*/
public function load()
{
if ($this->is_woocommerce_activated()) {
$this->includes();
$this->hooks();
$this->auto_send = new WCGC_Auto_Send();
new WCGC_Checkout_Hooks();
}
}
/**
* Include necessary files
*/
public function includes()
{
if (is_admin()) {
// Create all admin functions and pages
require_once $this->plugin_path . 'admin/columns.php';
require_once $this->plugin_path . 'admin/metabox.php';
}
// Required on checkout
require_once $this->plugin_path . 'admin/functions.php';
require_once $this->plugin_path . 'includes/class-auto-send.php';
require_once $this->plugin_path . 'includes/class-gift-card.php';
require_once $this->plugin_path . 'includes/class-checkout-hooks.php';
require_once $this->plugin_path . 'includes/giftcard-product.php';
require_once $this->plugin_path . 'includes/giftcard-forms.php';
require_once $this->plugin_path . 'includes/giftcard-paypal.php';
require_once $this->plugin_path . 'includes/giftcard-shortcodes.php';
require_once $this->plugin_path . 'includes/giftcard-functions.php';
require_once $this->plugin_path . 'includes/giftcard-meta.php';
}
/**
* Run action and filter hooks
*/
public function hooks()
{
global $wcgc_woo_giftcard_settings;
$wcgc_woo_giftcard_settings = get_option('wcgc_wg_options');
add_action('init', [ $this, 'create_post_type' ]);
add_filter('woocommerce_get_settings_pages', [ $this, 'add_settings_page' ], 10, 1);
add_action('wp_enqueue_scripts', [ $this, 'load_frontend_scripts' ]);
if (is_admin()) {
add_action('admin_enqueue_scripts', [ $this, 'load_admin_scripts' ], 99);
}
// Front end hooks
add_action('woocommerce_before_checkout_form', [ $this, 'render_checkout_form' ], 10);
add_action('woocommerce_before_add_to_cart_button', [ $this, 'render_add_to_cart_form' ]);
add_filter('woocommerce_get_item_data', [ $this, 'render_cart_item_fields' ], 10, 2);
}
/**
* Queue up the JavaScript file for the admin page, only on our admin page
*
* @param string $hook The current page in the admin
*
* @return void
* @access public
*/
public function load_admin_scripts($hook)
{
global $wp_scripts;
if ($this->post_type != $hook && 'post-new.php' != $hook && 'post.php' != $hook) {
return;
}
wp_enqueue_style('woocommerce_admin_styles', WC()->plugin_url() . '/assets/css/admin.css');
$jquery_version = isset($wp_scripts->registered['jquery-ui-core']->ver) ? $wp_scripts->registered['jquery-ui-core']->ver : '1.9.2';
wp_enqueue_script('woocommerce_writepanel');
wp_enqueue_script('jquery');
wp_enqueue_script('jquery-ui-core');
wp_enqueue_script('jquery-ui-datepicker');
}
/**
*
*/
public function load_frontend_scripts()
{
wp_enqueue_script(
'woocommerce-gift-cards',
$this->plugin_url . 'assets/js/woocommerce-gift-cards.js',
['jquery-blockui'],
$this->plugin_version,
true
);
}
/**
* Adds WC settings page
*
* @param $settings
* @return mixed|void
*/
public function add_settings_page($settings)
{
$settings[] = include($this->plugin_path . 'admin/settings-page.php');
return apply_filters($this->plugin_prefix . 'setting_classes', $settings);
}
/**
* Register custom post types
*/
public function create_post_type()
{
$show_in_menu = current_user_can('manage_woocommerce') ? 'woocommerce' : true;
register_post_type($this->post_type,
[
'labels' => [
'name' => __('Gift Cards', 'woocommerce-gift-cards'),
'singular_name' => __('Gift Card', 'woocommerce-gift-cards'),
'menu_name' => _x('Gift Cards', 'Admin menu name', 'woocommerce-gift-cards'),
'add_new' => __('Add Gift Card', 'woocommerce-gift-cards'),
'add_new_item' => __('Add New Gift Card', 'woocommerce-gift-cards'),
'edit' => __('Edit', 'woocommerce-gift-cards'),
'edit_item' => __('Edit Gift Card', 'woocommerce-gift-cards'),
'new_item' => __('New Gift Card', 'woocommerce-gift-cards'),
'view' => __('View Gift Cards', 'woocommerce-gift-cards'),
'view_item' => __('View Gift Card', 'woocommerce-gift-cards'),
'search_items' => __('Search Gift Cards', 'woocommerce-gift-cards'),
'not_found' => __('No Gift Cards found', 'woocommerce-gift-cards'),
'not_found_in_trash' => __('No Gift Cards found in trash', 'woocommerce-gift-cards'),
'parent' => __('Parent Gift Card', 'woocommerce-gift-cards')
],
'public' => true,
'has_archive' => true,
'publicly_queryable' => false,
'exclude_from_search' => false,
'show_in_menu' => $show_in_menu,
'hierarchical' => false,
'supports' => [ 'title', 'comments' ]
]
);
register_post_status('zerobalance', [
'label' => __('Zero Balance', 'woocommerce-gift-cards'),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop('Zero Balance <span class="count">(%s)</span>', 'Zero Balance <span class="count">(%s)</span>')
]);
}
/**
* Render the Gift card form for the checkout
*
* @return void
*/
public function render_checkout_form()
{
if (get_option('woocommerce_enable_giftcard_checkoutpage') == 'yes') {
wc_get_template('form-checkout.php', [], $this->theme_template_path, $this->plugin_template_path);
}
}
/**
* @param $gift_card WC_Gift_Card object
*/
public function send_gift_card_email($gift_card)
{
$send_email = get_bloginfo('admin_email');
$subject = apply_filters(
$this->plugin_prefix . 'gift_card_email_subject',
__('You have been sent a gift card', 'woocommerce-gift-cards')
);
$email_html = $this->generate_email_html($subject, $gift_card);
$headers = "From: " . $send_email . "\r\n" . " Reply-To: " . $send_email . "\r\n" . " Content-Type: text/html\r\n";
// Send the mail
add_filter('wp_mail_from', [ $this, 'from_email' ]);
add_filter('wp_mail_from_name', [ $this, 'from_name' ]);
wp_mail($gift_card->get_to_email(), $subject, $email_html, $headers);
remove_filter('wp_mail_from', [ $this, 'from_email' ]);
remove_filter('wp_mail_from_name', [ $this, 'from_name' ]);
$gift_card->set_email_sent();
}
/**
* @param $subject
* @param $gift_card WC_Gift_Card object
*
* @return string
*/
public function generate_email_html($subject, $gift_card)
{
$mailer = WC()->mailer();
// Get email body
ob_start();
wc_get_template('email-gift-recipient.php', [ 'gift_card' => $gift_card ], $this->theme_template_path, $this->plugin_template_path);
$email_body = ob_get_clean();
$email_body = apply_filters($this->plugin_prefix . 'gift_card_email_body', $email_body, $gift_card);
$email_html = $mailer->wrap_message($subject, $email_body);
return $email_html;
}
/**
* @return mixed|void
*/
public function from_email()
{
return apply_filters($this->plugin_prefix . 'from_email', WC()->mailer()->get_from_address());
}
/**
* @return mixed|void
*/
public function from_name()
{
return apply_filters($this->plugin_prefix . 'from_name', WC()->mailer()->get_from_name());
}
/**
*
*/
public function render_add_to_cart_form()
{
global $post;
$is_gift_card = get_post_meta($post->ID, '_giftcard', true);
if ($is_gift_card == 'yes') {
do_action(WCGC()->plugin_prefix . 'before_all_giftcard_fields', $post);
wc_get_template('form-add-to-cart.php', [], WCGC()->theme_template_path, WCGC()->plugin_template_path);
}
}
/**
* TODO make field names customisable and translatable
* @param $cart_item
*/
public function render_cart_item_fields($fields, $cart_item)
{
if ($this->is_gift_card($cart_item['product_id'])) {
$fields[] = [
'name' => 'Recipient name',
'value' => isset($cart_item['variation']['To']) ? $cart_item['variation']['To'] : ''
];
$fields[] = [
'name' => 'Recipient email',
'value' => isset($cart_item['variation']['To Email']) ? $cart_item['variation']['To Email'] : ''
];
$fields[] = [
'name' => 'Personal message',
'value' => isset($cart_item['variation']['Note']) ? $cart_item['variation']['Note'] : ''
];
}
return $fields;
}
/**
* @param $product_id
*
* @return bool
*/
public function is_gift_card($product_id)
{
$gift_card = get_post_meta($product_id, '_giftcard', true);
if ($gift_card != 'yes') {
return false;
}
return true;
}
/**
* @param $gift_card instance of WC_Gift_Card
*
* @return bool|WP_Error
*/
public function apply_gift_card_to_cart($gift_card)
{
// Validate gift card already added to cart
if (isset(WC()->session->giftcard_post)) {
return new WP_Error('wc-gift-cards', __('A Gift Card is already in the cart!', 'woocommerce-gift-cards'));
}
if (! $gift_card->exists()) {
return new WP_Error('wc-gift-cards', __('Gift Card does not exist.', 'woocommerce-gift-cards'));
}
if ($gift_card->is_expired()) {
return new WP_Error('wc-gift-cards', __('Gift Card has expired.', 'woocommerce-gift-cards'));
}
if ($gift_card->get_balance() <= 0) {
return new WP_Error('wc-gift-cards', __('Gift Card does not have a balance left.', 'woocommerce-gift-cards'));
}
return true;
}
/**
* Check if woocommerce is activated
*/
public function is_woocommerce_activated()
{
$blog_plugins = get_option('active_plugins', []);
$site_plugins = get_site_option('active_sitewide_plugins', []);
$woocommerce_basename = plugin_basename(WC_PLUGIN_FILE);
if ((in_array($woocommerce_basename, $blog_plugins) || isset($site_plugins[$woocommerce_basename])) && version_compare(WC_VERSION, '2.1', '>=')) {
return true;
}
return false;
}
protected static $_instance = null;
public static function instance()
{
if (is_null(self::$_instance)) {
self::$_instance = new self();
}
return self::$_instance;
}
}
function WCGC()
{
return WC_Gift_Cards::instance();
}
WCGC();