forked from converslabs/subscription
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscription.php
More file actions
328 lines (287 loc) · 7.8 KB
/
subscription.php
File metadata and controls
328 lines (287 loc) · 7.8 KB
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
<?php
/**
* Plugin Name: Subscription & Recurring Payment Plugin for WooCommerce
* Plugin URI: https://wpsubscription.co/
* Description: WPSubscription allow WooCommerce to enables recurring payments, subscriptions, and auto-renewals for digital and physical products. Supports Stripe, PayPal, Paddle, and more.
*
* Version: #WPSUBS_VERSION
*
* Author: ConversWP
* Author URI: https://wpsubscription.co/
*
* Text Domain: subscription
* Domain Path: /languages
*
* Requires PHP: 7.4
*
* Requires at least: 6.0
* Tested up to: 6.9
*
* WC requires at least: 6.0
* WC tested up to: 10.3
*
* Requires Plugins: woocommerce
*
* @package Subscription
*/
// don't call the file directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry;
use SpringDevs\Subscription\Illuminate\Gateways\Paypal\Paypal_Blocks_Integration;
require_once __DIR__ . '/vendor/autoload.php';
/**
* Sdevs_Subscription class
*
* @class Sdevs_Subscription The class that holds the entire plugin
*/
final class Sdevs_Subscription {
/**
* Plugin version
*
* @var string
*/
const VERSION = '#WPSUBS_VERSION';
/**
* Holds various class instances
*
* @var array
*/
private $container = array();
/**
* Constructor for the Sdevs_Wc_Subscription class
*
* Sets up all the appropriate hooks and actions
* within our plugin.
*/
private function __construct() {
$this->define_constants();
register_activation_hook( __FILE__, array( $this, 'activate' ) );
register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );
add_action( 'plugins_loaded', array( $this, 'init_plugin' ) );
}
/**
* Initializes the Sdevs_Wc_Subscription() class
*
* Checks for an existing Sdevs_Wc_Subscription() instance
* and if it doesn't find one, creates it.
*
* @return Sdevs_Subscription|bool
*/
public static function init() {
static $instance = false;
if ( ! $instance ) {
$instance = new Sdevs_Subscription();
}
return $instance;
}
/**
* Magic getter to bypass referencing plugin.
*
* @param mixed $prop Prop.
*
* @return mixed
*/
public function __get( $prop ) {
if ( array_key_exists( $prop, $this->container ) ) {
return $this->container[ $prop ];
}
return $this->{$prop};
}
/**
* Magic isset to bypass referencing plugin.
*
* @param mixed $prop Prop.
*
* @return bool
*/
public function __isset( $prop ) {
return isset( $this->{$prop} ) || isset( $this->container[ $prop ] );
}
/**
* Define the constants
*
* @return void
*/
public function define_constants() {
define( 'WP_SUBSCRIPTION_VERSION', self::VERSION );
define( 'WP_SUBSCRIPTION_FILE', __FILE__ );
define( 'WP_SUBSCRIPTION_PATH', dirname( WP_SUBSCRIPTION_FILE ) );
define( 'WP_SUBSCRIPTION_INCLUDES', WP_SUBSCRIPTION_PATH . '/includes' );
define( 'WP_SUBSCRIPTION_TEMPLATES', WP_SUBSCRIPTION_PATH . '/templates/' );
define( 'WP_SUBSCRIPTION_URL', plugins_url( '', WP_SUBSCRIPTION_FILE ) );
define( 'WP_SUBSCRIPTION_ASSETS', WP_SUBSCRIPTION_URL . '/assets' );
}
/**
* Load the plugin after all plugins are loaded
*
* @return void
*/
public function init_plugin() {
$this->includes();
$this->init_hooks();
}
/**
* Placeholder for activation function
*/
public function activate() {
$installer = new SpringDevs\Subscription\Installer();
$installer->run();
}
/**
* Placeholder for deactivation function
*
* Nothing being called here yet.
*/
public function deactivate() {
wp_clear_scheduled_hook( 'subscrpt_daily_cron' );
}
/**
* Include the required files
*
* @return void
*/
public function includes() {
// Include functions file first to ensure global functions are available
require_once WP_SUBSCRIPTION_INCLUDES . '/functions.php';
if ( $this->is_request( 'admin' ) ) {
$this->container['admin'] = new SpringDevs\Subscription\Admin();
}
if ( $this->is_request( 'frontend' ) ) {
$this->container['frontend'] = new SpringDevs\Subscription\Frontend();
}
$this->container['illuminate'] = new SpringDevs\Subscription\Illuminate();
}
/**
* Initialize the hooks
*
* @return void
*/
public function init_hooks() {
add_action( 'init', array( $this, 'init_classes' ) );
add_action( 'init', array( $this, 'localization_setup' ) );
add_action( 'init', array( $this, 'run_update' ) );
// HPOS Compatibility: Declare support for custom order tables
add_action(
'before_woocommerce_init',
function () {
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
}
}
);
// HPOS Compatibility: Register subscription post type with HPOS support
add_action(
'init',
function () {
register_post_type(
'subscrpt_order',
array(
'hpos' => true,
'public' => false,
'show_ui' => true,
'show_in_menu' => false,
'supports' => array( 'title' ),
'capability_type' => 'post',
'capabilities' => array(
'create_posts' => false,
),
'map_meta_cap' => true,
)
);
},
0
);
}
/**
* Need to do some actions after update plugin
*
* @return void
*/
public function run_update() {
$upgrade = new \SpringDevs\Subscription\Upgrade();
$upgrade->run();
}
/**
* Instantiate the required classes
*
* @return void
*/
public function init_classes() {
if ( $this->is_request( 'ajax' ) ) {
$this->container['ajax'] = new SpringDevs\Subscription\Ajax();
}
$this->container['api'] = new SpringDevs\Subscription\API();
$this->container['assets'] = new SpringDevs\Subscription\Assets();
}
/**
* Initialize plugin for localization
*
* Note: WordPress automatically loads translations for plugins hosted on WordPress.org
* since version 4.6, so manual loading is not required.
*/
public function localization_setup() {
// WordPress auto-loads translations for plugins hosted on WordPress.org since v4.6.
}
/**
* What type of request is this?
*
* @param string $type admin, ajax, cron or frontend.
*
* @return bool
*/
private function is_request( $type ) {
switch ( $type ) {
case 'admin':
return is_admin();
case 'ajax':
return defined( 'DOING_AJAX' );
case 'rest':
return defined( 'REST_REQUEST' );
case 'cron':
return defined( 'DOING_CRON' );
case 'frontend':
return ( ! is_admin() || defined( 'DOING_AJAX' ) ) && ! defined( 'DOING_CRON' );
}
}
} // Sdevs_Wc_Subscription
// Add Paypal Gateway Blocks.
if ( ! function_exists( 'wp_subscription_register_paypal_block' ) ) {
/**
* Register the PayPal block for WooCommerce Blocks.
*/
function wp_subscription_register_paypal_block() {
// Check if the required class exists.
if ( ! class_exists( 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) {
return;
}
// Hook the registration function to the 'woocommerce_blocks_payment_method_type_registration' action.
add_action(
'woocommerce_blocks_payment_method_type_registration',
function ( PaymentMethodRegistry $payment_method_registry ) {
$payment_method_registry->register( new Paypal_Blocks_Integration() );
}
);
}
// Register PayPal integration only if WordPress functions are available
if ( function_exists( 'get_option' ) ) {
// Is PayPal integration enabled?
$is_paypal_integration_enabled = 'on' === get_option( 'wp_subs_paypal_integration_enabled', 'off' );
if ( $is_paypal_integration_enabled ) {
add_action( 'woocommerce_blocks_loaded', 'wp_subscription_register_paypal_block' );
}
}
}
/**
* Initialize the main plugin
*
* @return Sdevs_Subscription|bool
*/
function sdevs_subscription() {
return Sdevs_Subscription::init();
}
/**
* Kick-off the plugin
*/
sdevs_subscription();