forked from wpswings/subscriptions-for-woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscriptions-for-woocommerce.php
336 lines (293 loc) · 13.2 KB
/
subscriptions-for-woocommerce.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
<?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 https://makewebbetter.com/
* @since 1.0.0
* @package Subscriptions_For_Woocommerce
*
* @wordpress-plugin
* Plugin Name: Subscriptions For WooCommerce
* Plugin URI: https://wordpress.org/plugins/subscriptions-for-woocommerce/
* Description: With Subscriptions for WooCommerce, allow the WooCommerce merchants to sell their subscriptions and avail recurring revenue.
* Version: 1.1.0
* Author: MakeWebBetter
* Author URI: https://makewebbetter.com/?utm_source=MWB-subscriptions-backend&utm_medium=MWB-ORG-backend&utm_campaign=MWB-backend
* Text Domain: subscriptions-for-woocommerce
* Domain Path: /languages
*
* Requires at least: 4.6
* Tested up to: 5.8
* WC requires at least: 4.0
* WC tested up to: 5.7
*
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
die;
}
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
/**
* Define plugin constants.
*
* @since 1.0.0
*/
function define_subscriptions_for_woocommerce_constants() {
subscriptions_for_woocommerce_constants( 'SUBSCRIPTIONS_FOR_WOOCOMMERCE_VERSION', '1.1.0' );
subscriptions_for_woocommerce_constants( 'SUBSCRIPTIONS_FOR_WOOCOMMERCE_DIR_PATH', plugin_dir_path( __FILE__ ) );
subscriptions_for_woocommerce_constants( 'SUBSCRIPTIONS_FOR_WOOCOMMERCE_DIR_URL', plugin_dir_url( __FILE__ ) );
subscriptions_for_woocommerce_constants( 'SUBSCRIPTIONS_FOR_WOOCOMMERCE_SERVER_URL', 'https://makewebbetter.com' );
subscriptions_for_woocommerce_constants( 'SUBSCRIPTIONS_FOR_WOOCOMMERCE_ITEM_REFERENCE', 'Subscriptions For Woocommerce' );
}
/**
* Callable function for defining plugin constants.
*
* @param String $key Key for contant.
* @param String $value value for contant.
* @since 1.0.0
*/
function subscriptions_for_woocommerce_constants( $key, $value ) {
if ( ! defined( $key ) ) {
define( $key, $value );
}
}
/**
* The code that runs during plugin activation.
* This action is documented in includes/class-subscriptions-for-woocommerce-activator.php
*/
function activate_subscriptions_for_woocommerce() {
require_once plugin_dir_path( __FILE__ ) . 'includes/class-subscriptions-for-woocommerce-activator.php';
Subscriptions_For_Woocommerce_Activator::subscriptions_for_woocommerce_activate();
$mwb_sfw_active_plugin = get_option( 'mwb_all_plugins_active', false );
if ( is_array( $mwb_sfw_active_plugin ) && ! empty( $mwb_sfw_active_plugin ) ) {
$mwb_sfw_active_plugin['subscriptions-for-woocommerce'] = array(
'plugin_name' => __( 'Subscriptions For Woocommerce', 'subscriptions-for-woocommerce' ),
'active' => '1',
);
} else {
$mwb_sfw_active_plugin = array();
$mwb_sfw_active_plugin['subscriptions-for-woocommerce'] = array(
'plugin_name' => __( 'Subscriptions For Woocommerce', 'subscriptions-for-woocommerce' ),
'active' => '1',
);
}
update_option( 'mwb_all_plugins_active', $mwb_sfw_active_plugin );
}
/**
* The code that runs during plugin deactivation.
* This action is documented in includes/class-subscriptions-for-woocommerce-deactivator.php
*/
function deactivate_subscriptions_for_woocommerce() {
require_once plugin_dir_path( __FILE__ ) . 'includes/class-subscriptions-for-woocommerce-deactivator.php';
Subscriptions_For_Woocommerce_Deactivator::subscriptions_for_woocommerce_deactivate();
$mwb_sfw_deactive_plugin = get_option( 'mwb_all_plugins_active', false );
if ( is_array( $mwb_sfw_deactive_plugin ) && ! empty( $mwb_sfw_deactive_plugin ) ) {
foreach ( $mwb_sfw_deactive_plugin as $mwb_sfw_deactive_key => $mwb_sfw_deactive ) {
if ( 'subscriptions-for-woocommerce' === $mwb_sfw_deactive_key ) {
$mwb_sfw_deactive_plugin[ $mwb_sfw_deactive_key ]['active'] = '0';
}
}
}
update_option( 'mwb_all_plugins_active', $mwb_sfw_deactive_plugin );
}
register_activation_hook( __FILE__, 'activate_subscriptions_for_woocommerce' );
register_deactivation_hook( __FILE__, 'deactivate_subscriptions_for_woocommerce' );
/**
* 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-subscriptions-for-woocommerce.php';
if ( ! function_exists( 'mwb_sfw_check_multistep' ) ) {
/**
* This function is used to check susbcripton product in cart.
*
* @name mwb_sfw_check_multistep
* @since 1.0.2
*/
function mwb_sfw_check_multistep() {
$bool = false;
$mwb_sfw_check = get_option( 'mwb_sfw_multistep_done', false );
if ( ! empty( $mwb_sfw_check ) ) {
$bool = true;
}
$bool = apply_filters( 'mwb_sfw_multistep_done', $bool );
return $bool;
}
}
/**
* 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_subscriptions_for_woocommerce() {
define_subscriptions_for_woocommerce_constants();
$sfw_plugin_standard = new Subscriptions_For_Woocommerce();
$sfw_plugin_standard->sfw_run();
$GLOBALS['sfw_mwb_sfw_obj'] = $sfw_plugin_standard;
$GLOBALS['mwb_sfw_notices'] = false;
}
run_subscriptions_for_woocommerce();
// Add settings link on plugin page.
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'subscriptions_for_woocommerce_settings_link' );
/**
* Settings link.
*
* @since 1.0.0
* @param Array $links Settings link array.
*/
function subscriptions_for_woocommerce_settings_link( $links ) {
$my_link = array(
'<a href="' . admin_url( 'admin.php?page=subscriptions_for_woocommerce_menu' ) . '">' . __( 'Settings', 'subscriptions-for-woocommerce' ) . '</a>',
);
if ( ! is_plugin_active( 'woocommerce-subscriptions-pro/woocommerce-subscriptions-pro.php' ) ) {
$my_link['goPro'] = '<a class="mwb-sfw-go-pro" target="_blank" href="https://makewebbetter.com/product/woocommerce-subscriptions-pro/?utm_source=MWB-subscriptions-backend&utm_medium=MWB-ORG-backend&utm_campaign=MWB-gopro">' . esc_html__( 'GO PRO', 'subscriptions-for-woocommerce' ) . '</a>';
}
return array_merge( $my_link, $links );
}
add_filter( 'plugin_row_meta', 'mwb_sfw_doc_and_premium_link', 10, 2 );
/**
* Callable function for adding plugin row meta.
*
* @name mwb_sfw_doc_and_premium_link.
* @param string $links link of the constant.
* @param array $file name of the plugin.
*/
function mwb_sfw_doc_and_premium_link( $links, $file ) {
if ( strpos( $file, 'subscriptions-for-woocommerce.php' ) !== false ) {
$row_meta = array(
'demo' => '<a target="_blank" href="https://demo.makewebbetter.com/subscriptions-for-woocommerce/?utm_source=MWB-subscriptions-backend&utm_medium=MWB-demoORG-backend&utm_campaign=MWB-backend"><img src="' . esc_url( SUBSCRIPTIONS_FOR_WOOCOMMERCE_DIR_URL ) . 'admin/images/Demo.svg" class="mwb-info-img" alt="Demo image">' . esc_html__( 'Free Demo', 'subscriptions-for-woocommerce' ) . '</a>',
'docs' => '<a target="_blank" href="https://docs.makewebbetter.com/subscriptions-for-woocommerce/?utm_source=MWB-subscriptions-backend&utm_medium=MWB-docORG-backend&utm_campaign=MWB-backend"><img src="' . esc_url( SUBSCRIPTIONS_FOR_WOOCOMMERCE_DIR_URL ) . 'admin/images/Documentation.svg" class="mwb-info-img" alt="documentation image">' . esc_html__( 'Documentation', 'subscriptions-for-woocommerce' ) . '</a>',
'support' => '<a target="_blank" href="https://makewebbetter.com/submit-query/?utm_source=MWB-subscriptions-backend&utm_medium=MWB-ORG-backend&utm_campaign=MWB-support"><img src="' . esc_url( SUBSCRIPTIONS_FOR_WOOCOMMERCE_DIR_URL ) . 'admin/images/Support.svg" class="mwb-info-img" alt="support image">' . esc_html__( 'Support', 'subscriptions-for-woocommerce' ) . '</a>',
);
return array_merge( $links, $row_meta );
}
return (array) $links;
}
register_activation_hook( __FILE__, 'mwb_sfw_flush_rewrite_rules' );
register_deactivation_hook( __FILE__, 'mwb_sfw_flush_rewrite_rules' );
/**
* This function is used to create tabs
*
* @name mwb_sfw_flush_rewrite_rules
* @since 1.0.0.
* @author makewebbetter<[email protected]>
* @link https://www.makewebbetter.com/
*/
function mwb_sfw_flush_rewrite_rules() {
add_rewrite_endpoint( 'mwb_subscriptions', EP_PAGES );
add_rewrite_endpoint( 'show-subscription', EP_PAGES );
add_rewrite_endpoint( 'mwb-add-payment-method', EP_PAGES );
flush_rewrite_rules();
}
add_action( 'init', 'mwb_sfw_register_custom_order_types' );
/**
* This function is used to create custom post type for subscription.
*
* @name mwb_sfw_register_custom_order_types
* @since 1.0.0
*/
function mwb_sfw_register_custom_order_types() {
wc_register_order_type(
'mwb_subscriptions',
apply_filters(
'mwb_sfw_register_custom_order_types',
array(
'labels' => array(
'name' => __( 'Subscriptions', 'subscriptions-for-woocommerce' ),
'singular_name' => __( 'Subscription', 'subscriptions-for-woocommerce' ),
'add_new' => __( 'Add Subscription', 'subscriptions-for-woocommerce' ),
'add_new_item' => __( 'Add New Subscription', 'subscriptions-for-woocommerce' ),
'edit' => __( 'Edit', 'subscriptions-for-woocommerce' ),
'edit_item' => __( 'Edit Subscription', 'subscriptions-for-woocommerce' ),
'new_item' => __( 'New Subscription', 'subscriptions-for-woocommerce' ),
'view' => __( 'View Subscription', 'subscriptions-for-woocommerce' ),
'view_item' => __( 'View Subscription', 'subscriptions-for-woocommerce' ),
'search_items' => __( 'Search Subscriptions', 'subscriptions-for-woocommerce' ),
'not_found' => __( 'Not Found', 'subscriptions-for-woocommerce' ),
'not_found_in_trash' => __( 'No Subscriptions found in the trash', 'subscriptions-for-woocommerce' ),
'parent' => __( 'Parent Subscriptions', 'subscriptions-for-woocommerce' ),
'menu_name' => __( 'Subscriptions', 'subscriptions-for-woocommerce' ),
),
'description' => __( 'These subscriptions are stored.', 'subscriptions-for-woocommerce' ),
'public' => false,
'show_ui' => true,
'capability_type' => 'shop_order',
'map_meta_cap' => true,
'publicly_queryable' => false,
'exclude_from_search' => true,
'show_in_menu' => false,
'hierarchical' => false,
'show_in_nav_menus' => false,
'rewrite' => false,
'query_var' => false,
'supports' => array( 'title', 'comments', 'custom-fields' ),
'has_archive' => false,
'exclude_from_orders_screen' => true,
'add_order_meta_boxes' => true,
'exclude_from_order_count' => true,
'exclude_from_order_views' => true,
'exclude_from_order_webhooks' => true,
'exclude_from_order_reports' => true,
'exclude_from_order_sales_reports' => true,
)
)
);
}
add_action( 'activated_plugin', 'mwb_sfe_redirect_on_settings' );
if ( ! function_exists( 'mwb_sfe_redirect_on_settings' ) ) {
/**
* This function is used to check plugin.
*
* @name mwb_sfe_redirect_on_settings
* @param string $plugin plugin.
* @since 1.0.3
*/
function mwb_sfe_redirect_on_settings( $plugin ) {
if ( plugin_basename( __FILE__ ) === $plugin ) {
$general_settings_url = admin_url( 'admin.php?page=subscriptions_for_woocommerce_menu' );
wp_redirect( esc_url( $general_settings_url ) );
exit();
}
}
}
} else {
// WooCommerce is not active so deactivate this plugin.
add_action( 'admin_init', 'mwb_sfw_activation_failure' );
/**
* Deactivate this plugin.
*
* @name mwb_sfw_activation_failure
* @since 1.0.0
*/
function mwb_sfw_activation_failure() {
deactivate_plugins( plugin_basename( __FILE__ ) );
}
// Add admin error notice.
add_action( 'admin_notices', 'mwb_sfw_activation_failure_admin_notice' );
/**
* This function is used to display admin error notice when WooCommerce is not active.
*
* @name mwb_sfw_activation_failure_admin_notice
* @since 1.0.0
*/
function mwb_sfw_activation_failure_admin_notice() {
// to hide Plugin activated notice.
unset( $_GET['activate'] );
?>
<div class="notice notice-error is-dismissible">
<p><?php esc_html_e( 'WooCommerce is not activated, Please activate WooCommerce first to activate Subscriptions for Woocommerce.', 'subscriptions-for-woocommerce' ); ?></p>
</div>
<?php
}
}