-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamnesty-donations.php
498 lines (412 loc) · 12.8 KB
/
amnesty-donations.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
declare( strict_types = 1 );
namespace Amnesty\Donations;
/*
Plugin Name: Humanity Donations
Plugin URI: https://github.com/amnestywebsite/humanity-donations
Description: Add support for donations via WooCommerce
Version: 1.1.1
Author: Amnesty International
Author URI: https://www.amnesty.org
License: GPLv2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: aidonations
Domain Path: /languages
Network: true
Requires PHP: 8.2.0
Requires at least: 5.8.0
Tested up to: 6.6.2
*/
use WC_Cart;
use WC_Product;
require_once __DIR__ . '/includes/helpers.php';
require_once __DIR__ . '/includes/kses.php';
require_once __DIR__ . '/includes/render.php';
if ( ! function_exists( 'get_plugin_data' ) ) {
require_once ABSPATH . '/wp-admin/includes/plugin.php';
}
/**
* Plugin instantiation class
*/
class Init {
/**
* Reference to this plugin file
*
* @var string
*/
public static $file = __FILE__;
/**
* List of dependent plugins
*
* @var array
*/
protected static $dependencies = [];
/**
* Plugin data
*
* @var array
*/
protected $data = [];
/**
* Bind hooks
*/
public function __construct() {
$this->data = get_plugin_data( __FILE__ );
static::$dependencies = [
'woocommerce.php' => __( 'WooCommerce', 'woocommerce' ),
'woocommerce-checkout-manager.php' => __( 'Checkout Manager for WooCommerce', 'woocommerce-checkout-manager' ),
'woocommerce-name-your-price.php' => __( 'WooCommerce Name Your Price', 'wc_name_your_price' ),
'woocommerce-subscriptions.php' => __( 'WooCommerce Subscriptions', 'woocommerce-subscriptions' ),
];
add_action( 'all_admin_notices', [ $this, 'check_dependencies' ] );
add_filter( 'register_translatable_package', [ $this, 'register_translatable_package' ], 12 );
add_action( 'plugins_loaded', [ $this, 'textdomain' ] );
add_action( 'init', [ $this, 'register_block' ] );
add_action( 'init', [ $this, 'register_meta' ] );
if ( version_compare( $GLOBALS['wp_version'], '5.8', '<' ) ) {
add_filter( 'block_categories', [ $this, 'register_category' ], 100 );
} else {
add_filter( 'block_categories_all', [ $this, 'register_category' ], 100 );
}
add_action( 'enqueue_block_assets', [ $this, 'register_assets' ] );
add_action( 'enqueue_block_editor_assets', [ $this, 'register_block_assets' ] );
add_filter( 'woocommerce_register_post_type_product', [ $this, 'modify_product_post_type' ] );
add_filter( 'woocommerce_add_to_cart_product_id', [ $this, 'handle_cart_addition' ] );
add_filter( 'woocommerce_add_to_cart_product_id', [ $this, 'handle_cart_removal' ] );
add_filter( 'woocommerce_add_to_cart_redirect', [ $this, 'handle_cart_redirect' ], 20, 2 );
add_filter( 'woocommerce_checkout_get_value', [ $this, 'checkout_get_value' ], 10, 2 );
add_action( 'woocommerce_remove_cart_item', [ $this, 'maybe_update_session' ], 10, 2 );
add_action( 'woocommerce_after_product_object_save', [ $this, 'save_product_meta' ] );
add_filter( 'woocommerce_currency', [ $this, 'handle_currency_change' ] );
}
/**
* Register this plugin as a translatable package
*
* @param array<int,array<string,string>> $packages existing packages
*
* @return array<int,array<string,string>>
*/
public function register_translatable_package( array $packages = [] ): array {
$packages[] = [
'id' => 'humanity-donations',
'label' => __( 'Donations', 'aidonations' ),
'path' => realpath( __DIR__ ),
'pot' => realpath( __DIR__ ) . '/languages/aidonations.pot',
'domain' => 'aidonations',
];
return $packages;
}
/**
* Returns the currency, can be changed in the dropdown in donations
*
* @param string $currency the selected currency code
*
* @return mixed
*/
public function handle_currency_change( string $currency ): string {
if ( ! WC()->session ) {
return $currency;
}
if ( ! WC()->session->get( 'user_currency' ) ) {
return $currency;
}
return WC()->session->get( 'user_currency' );
}
/**
* Output warning & deactivate if dependent plugins aren't active
*
* @return void
*/
public function check_dependencies(): void {
$plugins = get_option( 'active_plugins' );
if ( is_multisite() ) {
$plugins = array_keys( get_site_option( 'active_sitewide_plugins' ) );
}
$plugins = array_unique( array_map( 'basename', $plugins ) );
$missing = array_diff( array_keys( static::$dependencies ), $plugins );
if ( empty( $missing ) ) {
return;
}
$missing_labels = [];
foreach ( $missing as $key ) {
$missing_labels[] = static::$dependencies[ $key ];
}
$missing = implode( ', ', $missing_labels );
printf(
'<div class="notice notice-error"><p>%s</p></div>',
// translators: [admin] %s: list of missing plugins
sprintf( esc_html__( 'The Amnesty International Donations plugin requires these plugins to be active: %s', 'aidonations' ), esc_html( $missing ) )
);
deactivate_plugins( plugin_basename( __FILE__ ), false, is_multisite() );
}
/**
* Register textdomain
*
* @return void
*/
public function textdomain(): void {
load_plugin_textdomain( 'aidonations', false, basename( __DIR__ ) . '/languages' );
}
/**
* Register the Gutenberg block
*
* @return void
*/
public function register_block(): void {
if ( ! current_theme_supports( 'woocommerce' ) ) {
return;
}
register_block_type(
'amnesty-wc/donation',
[
'render_callback' => 'amnesty_render_donation_block',
'attributes' => [
'showDonation' => [
'type' => 'boolean',
'default' => true,
],
'showSubscription' => [
'type' => 'boolean',
'default' => true,
],
],
]
);
}
/**
* Register meta for the API
*
* @return void
*/
public function register_meta(): void {
if ( ! post_type_exists( 'product' ) ) {
return;
}
register_meta(
'product',
'_campaigns',
[
'show_in_rest' => true,
'single' => true,
'type' => 'string',
]
);
}
/**
* Add a custom category to Gutenberg for easy selection of the custom blocks.
*
* @param array $categories - Current Gutenberg categories.
*
* @return array
*/
public function register_category( array $categories = [] ): array {
if ( current_theme_supports( 'woocommerce' ) ) {
array_splice(
$categories,
1,
0,
[
[
/* translators: [admin] */
'title' => __( 'Amnesty WooCommerce', 'aidonations' ),
'slug' => 'amnesty-wc',
],
]
);
}
return $categories;
}
/**
* Register assets for front-end
*
* @return void
*/
public function register_assets(): void {
if ( is_admin() || 'wp-login.php' === $GLOBALS['pagenow'] ) {
return;
}
wp_enqueue_style( 'aidonations-style', plugins_url( '/assets/styles/app.css', __FILE__ ), [], $this->data['Version'], 'all' );
wp_enqueue_script( 'aidonations-app', plugins_url( '/assets/scripts/app.js', __FILE__ ), [], $this->data['Version'], true );
}
/**
* Register assets for Gutenberg
*
* @return void
*/
public function register_block_assets(): void {
wp_enqueue_style( 'aidonations-style', plugins_url( '/assets/styles/app.css', __FILE__ ), [], $this->data['Version'], 'all' );
wp_enqueue_style( 'aidonations-editor', plugins_url( '/assets/styles/block.css', __FILE__ ), [ 'aidonations-style' ], $this->data['Version'], 'all' );
wp_enqueue_script( 'aidonations-editor', plugins_url( '/assets/scripts/block.js', __FILE__ ), [ 'lodash', 'wp-blocks', 'wc-settings' ], $this->data['Version'], true );
}
/**
* Modify post type registration args
*
* @param array $args the product registration arguments
*
* @return array
*/
public function modify_product_post_type( array $args = [] ): array {
$args['has_archive'] = 'products';
return $args;
}
/**
* If the product being added to the cart is a donation/subscription,
* clear out previously-added products before redirecting to the cart.
*
* @param int $id the ID of the product being added
*
* @return int $id
*/
public function handle_cart_addition( int $id = 0 ): int {
if ( 0 === $id ) {
return $id;
}
$is_donation = amnesty_product_is_donation( $id );
$is_subscription = amnesty_product_is_subscription( $id );
if ( ! $is_donation && ! $is_subscription ) {
return $id;
}
WC()->cart->empty_cart();
return $id;
}
/**
* If the product being added to the cart is not a donation/subscription,
* clear out previously-added donations/subscriptions.
*
* @param int $id the ID of the product being added
*
* @return int $id
*/
public function handle_cart_removal( int $id = 0 ): int {
if ( 0 === $id ) {
return $id;
}
$cart_items = WC()->cart->get_cart();
foreach ( $cart_items as $hash => $item ) {
$product_id = absint( $item['product_id'] );
if ( ! $product_id ) {
continue;
}
$is_donation = amnesty_product_is_donation( $product_id );
$is_subscription = amnesty_product_is_subscription( $product_id );
if ( ! $is_donation && ! $is_subscription ) {
continue;
}
WC()->cart->remove_cart_item( $hash );
}
return $id;
}
/**
* Redirect straight to cart if product added to basket is a donation/subscription
*
* @param string $url the redirect URL
* @param WC_Product $product the product being added to the cart
*
* @return string $url
*
* phpcs:disable WordPress.Security.NonceVerification.Missing
* ^ this callback is only triggered if WC has verified the request
*/
public function handle_cart_redirect( string $url, WC_Product $product = null ): string {
if ( ! $product ) {
return $url;
}
$is_donation = amnesty_product_is_donation( $product->get_id() );
$is_subscription = amnesty_product_is_subscription( $product->get_id() );
if ( ! $is_donation && ! $is_subscription ) {
return $url;
}
// store additional fields here - there's sadly no decent alternative hook
$data_fields = sanitize_text_field( $_POST['additional_field_names'] ?? '' );
if ( ! $data_fields ) {
return wc_get_checkout_url();
}
$data_fields = explode( ',', $data_fields );
$save_fields = [];
foreach ( $data_fields as $field ) {
$value = sanitize_text_field( $_POST[ $field ] ?? '' );
if ( ! $value ) {
continue;
}
WC()->session->set( $field, $value );
$save_fields[] = $field;
}
WC()->session->set( 'custom_session_fields', implode( ',', $save_fields ) );
return wc_get_checkout_url();
}
// phpcs:enable WordPress.Security.NonceVerification.Missing
/**
* Set data from the session, if there is a value in the store.
* Allows us to pre-populate additional fields from WOOCCM.
*
* @param null $retval the override return value
* @param string $key the key of the value to be retrieved
*
* @return mixed
*/
public function checkout_get_value( $retval = null, string $key = '' ) {
if ( ! WC()->session ) {
return $retval;
}
$fields = explode( ',', WC()->session->get( 'custom_session_fields' ) ?: '' );
if ( ! $fields || ! in_array( $key, $fields, true ) ) {
return $retval;
}
if ( WC()->session->get( $key ) ) {
return WC()->session->get( $key );
}
return $retval;
}
/**
* Remove data from the session upon removal of a product from the cart.
*
* @param string $key the cart item key
* @param WC_Cart $cart the WooCommerce cart object
*/
public function maybe_update_session( string $key, WC_Cart $cart ): void {
$product = $cart->get_cart_item( $key )['product_id'];
$is_donation = amnesty_product_is_donation( $product );
$is_subscription = amnesty_product_is_subscription( $product );
if ( ! $is_donation && ! $is_subscription ) {
return;
}
$fields = WC()->session->get( 'custom_session_fields' );
if ( null === $fields ) {
return;
}
$fields = explode( ',', $fields );
if ( ! $fields || ! in_array( $key, $fields, true ) ) {
return;
}
foreach ( $fields as $field ) {
WC()->session->set( $field, '' );
}
}
/**
* Save donation product meta
*
* @param WC_Product $product the product being saved
*
* @return bool
*/
public function save_product_meta( WC_Product $product ): bool {
// v this callback is only triggered if WC has verified the request
// phpcs:ignore WordPress.Security.NonceVerification.Missing
$is_donation = sanitize_text_field( $_POST['amnesty_wc_product_type'] ?? '' );
if ( ! $is_donation || 'donation' !== $is_donation ) {
return delete_post_meta( $product->get_id(), 'amnesty_wc_product_type' );
}
return ! ! update_post_meta( $product->get_id(), 'amnesty_wc_product_type', 'donation' );
}
/**
* Support currency changing
*
* @param string $currency the currency being changed
*
* @return string
*/
public function currency_change( string $currency ): string {
return WC()->session->get( 'user_currency' ) ?: $currency;
}
}
new Init();