|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Fundraising Add-on. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Pronamic <[email protected]> |
|
6
|
|
|
* @copyright 2005-2021 Pronamic |
|
7
|
|
|
* @license GPL-3.0-or-later |
|
8
|
|
|
* @package Pronamic\WordPress\Pay |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Pronamic\WordPress\Pay\Fundraising; |
|
12
|
|
|
|
|
13
|
|
|
use Pronamic\WordPress\Pay\Payments\Payment; |
|
14
|
|
|
use Pronamic\WordPress\Pay\Payments\PaymentStatus; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Addon. |
|
18
|
|
|
* |
|
19
|
|
|
* @author Reüel van der Steege |
|
20
|
|
|
* @since 1.0.0 |
|
21
|
|
|
* @version 1.0.0 |
|
22
|
|
|
*/ |
|
23
|
|
|
class Addon { |
|
24
|
|
|
/** |
|
25
|
|
|
* Instance. |
|
26
|
|
|
* |
|
27
|
|
|
* @var Addon|null |
|
28
|
|
|
*/ |
|
29
|
|
|
protected static $instance; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Instance. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string|array|object $args The add-on arguments. |
|
35
|
|
|
* |
|
36
|
|
|
* @return Addon |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function instance( $args = array() ) { |
|
39
|
|
|
if ( is_null( self::$instance ) ) { |
|
40
|
|
|
self::$instance = new self( $args ); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return self::$instance; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* The root file of this WordPress plugin. |
|
48
|
|
|
* |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
public $file; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Version. |
|
55
|
|
|
* |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
public $version = ''; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Blocks. |
|
62
|
|
|
* |
|
63
|
|
|
* @var Blocks |
|
64
|
|
|
*/ |
|
65
|
|
|
private $blocks; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Add-on constructor. |
|
69
|
|
|
* |
|
70
|
|
|
* @param array $args Arguments. |
|
71
|
|
|
*/ |
|
72
|
|
|
public function __construct( $args = array() ) { |
|
73
|
|
|
$args = wp_parse_args( |
|
74
|
|
|
$args, |
|
75
|
|
|
array( |
|
76
|
|
|
'file' => null, |
|
77
|
|
|
'options' => array(), |
|
78
|
|
|
) |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
|
|
$this->file = $args['file']; |
|
82
|
|
|
|
|
83
|
|
|
// Version from plugin file header. |
|
84
|
|
|
if ( null !== $args['file'] ) { |
|
85
|
|
|
$file_data = get_file_data( $this->file, array( 'Version' => 'Version' ) ); |
|
86
|
|
|
|
|
87
|
|
|
if ( \array_key_exists( 'Version', $file_data ) ) { |
|
88
|
|
|
$this->version = $file_data['Version']; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// Actions. |
|
93
|
|
|
$plugins_loaded_function = array( $this, 'plugins_loaded' ); |
|
94
|
|
|
|
|
95
|
|
|
if ( ! \has_action( 'plugins_loaded', $plugins_loaded_function ) ) { |
|
96
|
|
|
\add_action( 'plugins_loaded', $plugins_loaded_function ); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Plugins loaded. |
|
102
|
|
|
* |
|
103
|
|
|
* @link https://developer.wordpress.org/reference/hooks/plugins_loaded/ |
|
104
|
|
|
* @link https://developer.wordpress.org/reference/functions/load_plugin_textdomain/ |
|
105
|
|
|
* @return void |
|
106
|
|
|
*/ |
|
107
|
|
|
public function plugins_loaded() { |
|
108
|
|
|
if ( ! \function_exists( '\pronamic_pay_plugin' ) ) { |
|
109
|
|
|
// @todo Add admin notice if Pronamic Pay is not active. |
|
110
|
|
|
return; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if ( ! \function_exists( '\register_block_type' ) ) { |
|
114
|
|
|
// @todo Add admin notice if blocks can not be registered. |
|
115
|
|
|
return; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
// Blocks. |
|
119
|
|
|
$this->blocks = new Blocks( $this ); |
|
120
|
|
|
$this->blocks->setup(); |
|
121
|
|
|
|
|
122
|
|
|
// Update blocks on payment status update. |
|
123
|
|
|
\add_action( 'pronamic_payment_status_update', array( $this, 'payment_status_block_update' ), 10, 1 ); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Update blocks on payment status update. |
|
128
|
|
|
* |
|
129
|
|
|
* @param Payment $payment Payment. |
|
130
|
|
|
* @return void |
|
131
|
|
|
*/ |
|
132
|
|
|
public function payment_status_block_update( Payment $payment ) { |
|
133
|
|
|
if ( PaymentStatus::SUCCESS !== $payment->get_status() ) { |
|
134
|
|
|
return; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
$post_id = $payment->get_origin_id(); |
|
138
|
|
|
|
|
139
|
|
|
if ( null === $post_id ) { |
|
140
|
|
|
return; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$origin_post = \get_post( $post_id ); |
|
144
|
|
|
|
|
145
|
|
|
if ( null === $origin_post ) { |
|
146
|
|
|
return; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
if ( ! \has_blocks( $origin_post->post_content ) ) { |
|
150
|
|
|
return; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
// Use block updater to update blocks in origin post. |
|
154
|
|
|
$updater = new BlockUpdater(); |
|
155
|
|
|
|
|
156
|
|
|
$updater->add_raised_money( $payment->get_total_amount() ); |
|
157
|
|
|
|
|
158
|
|
|
$updater->update_post( $origin_post ); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|