This repository has been archived by the owner on Aug 14, 2023. It is now read-only.
/
edd-recurring-pmcs.php
313 lines (258 loc) · 8.84 KB
/
edd-recurring-pmcs.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
<?php
/*
Plugin Name: Easy Digital Downloads - Recurring Payments - Prevent Multiple Cart Subscriptions
Plugin URI: https://easydigitaldownloads.com/extensions/recurring-payments/?ref=166
Description: Prevent multiple subscriptions from being added to the cart
Version: 1.0.1
Author: Andrew Munro, Sumobi
Author URI: http://sumobi.com/
License: GPL-2.0+
License URI: http://www.opensource.org/licenses/gpl-license.php
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! class_exists( 'EDD_Recurring_Payments_PMCS' ) ) {
final class EDD_Recurring_Payments_PMCS {
/**
* Holds the instance
*
* Ensures that only one instance of EDD_Recurring_Payments_PMCS exists in memory at any one
* time and it also prevents needing to define globals all over the place.
*
* TL;DR This is a static property property that holds the singleton instance.
*
* @var object
* @static
* @since 1.0
*/
private static $instance;
/**
* Plugin Version
*/
private $version = '1.0.1';
/**
* Plugin Title
*/
public $title = 'EDD Recurring Payments - Prevent Multiple Cart Subscriptions';
/**
* Main Instance
*
* Ensures that only one instance exists in memory at any one
* time. Also prevents needing to define globals all over the place.
*
* @since 1.0
*
*/
public static function get_instance() {
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof EDD_Recurring_Payments_PMCS ) ) {
self::$instance = new EDD_Recurring_Payments_PMCS;
self::$instance->setup_constants();
self::$instance->hooks();
}
return self::$instance;
}
/**
* Constructor Function
*
* @since 1.0
* @access private
*/
private function __construct() {
self::$instance = $this;
}
/**
* Reset the instance of the class
*
* @since 1.0
* @access public
* @static
*/
public static function reset() {
self::$instance = null;
}
/**
* Setup plugin constants
*
* @access private
* @since 1.0
* @return void
*/
private function setup_constants() {
// Plugin version
if ( ! defined( 'EDDRP_PMCS_VERSION' ) ) {
define( 'EDDRP_PMCS_VERSION', $this->version );
}
}
/**
* Setup the default hooks and actions
*
* @since 1.0
*
* @return void
*/
private function hooks() {
add_action( 'after_setup_theme', array( $this, 'load_textdomain' ) );
add_action( 'wp_footer', array( $this, 'js' ) );
add_action( 'edd_pre_add_to_cart', array( $this, 'pre_add_to_cart' ) );
add_filter( 'plugin_row_meta', array( $this, 'plugin_meta' ), 10, 2 );
add_filter( 'edd_purchase_link_defaults', array( $this, 'filter_purchase_link' ) );
do_action( 'eddrp_pmcs_setup_actions' );
}
/**
* Loads the plugin language files
*
* @access public
* @since 1.0
* @return void
*/
public function load_textdomain() {
// Set filter for plugin's languages directory
$lang_dir = dirname( plugin_basename( plugin_dir_path( __FILE__ ) ) ) . '/languages/';
$lang_dir = apply_filters( 'edd_purchase_rewards_languages_directory', $lang_dir );
// Traditional WordPress plugin locale filter
$locale = apply_filters( 'plugin_locale', get_locale(), 'eddrp_pmcs' );
$mofile = sprintf( '%1$s-%2$s.mo', 'eddrp_pmcs', $locale );
// Setup paths to current locale file
$mofile_local = $lang_dir . $mofile;
$mofile_global = WP_LANG_DIR . '/eddrp-pmcs/' . $mofile;
if ( file_exists( $mofile_global ) ) {
// Look in global /wp-content/languages/edd-purchase-rewards folder
load_textdomain( 'eddrp_pmcs', $mofile_global );
} elseif ( file_exists( $mofile_local ) ) {
// Look in local /wp-content/plugins/edd-purchase-rewards/languages/ folder
load_textdomain( 'eddrp_pmcs', $mofile_local );
} else {
// Load the default language files
load_plugin_textdomain( 'eddrp_pmcs', false, $lang_dir );
}
}
/**
* Modify plugin metalinks
*
* @access public
* @since 1.0.0
* @param array $links The current links array
* @param string $file A specific plugin table entry
* @return array $links The modified links array
*/
public function plugin_meta( $links, $file ) {
if ( $file == plugin_basename( __FILE__ ) ) {
$plugins_link = array(
'<a title="'. __( 'View more plugins for Easy Digital Downloads by Sumobi', 'eddrp_pmcs' ) .'" href="https://easydigitaldownloads.com/blog/author/andrewmunro/?ref=166" target="_blank">' . __( 'Author\'s EDD plugins', 'eddrp_pmcs' ) . '</a>'
);
$links = array_merge( $links, $plugins_link );
}
return $links;
}
/**
* Filter the purchase link to include our new classes
*
* @param $args download arguments
* @since 1.0
*/
public function filter_purchase_link( $args ) {
$download_id = $args['download_id'];
$cart_download_ids = $this->get_cart_ids();
// is a recurring product
if ( 'yes' === get_post_meta( $download_id, 'edd_recurring', true ) ) {
// add a recurring CSS class to all downloads that are recurring
$args['class'] = 'edd-submit recurring';
if ( $cart_download_ids ) {
// check cart to see if another recurring download exists
foreach ( $cart_download_ids as $id ) {
if ( 'yes' === get_post_meta( $id, 'edd_recurring', true ) && ! in_array( $download_id, $cart_download_ids ) ) {
// adds a "disabled" class to all other recurring product buttons, except the one thats in the cart
$args['class'] = 'edd-submit recurring disabled';
}
}
}
}
return $args;
}
/**
* Get the download IDs that exist in the cart
* @return array download IDs
* @since 1.0
*/
public function get_cart_ids() {
$cart_items = edd_get_cart_contents();
$cart_download_ids = $cart_items ? wp_list_pluck( $cart_items, 'id' ) : null;
return $cart_download_ids;
}
public function notice() {
return apply_filters( 'eddrp_pmcs_notice', __( 'Sorry, you can only purchase one subscription at a time. One already exists in the shopping cart.', 'eddrp_pmcs' ) );
}
/**
* Show customer a standard JS notice when they try to add another subscription
*
* @since 1.0
*/
public function js() { ?>
<script>
jQuery(document).ready(function($) {
// when recurring button is clicked, run some checks and prevent it from being added to cart
$('.edd-submit.recurring').click(function(e) {
// recurring product added to cart so add a disabled class to all other recurring buttons, and prevent them from being added to cart
if ( ! $(this).hasClass('disabled') ) {
$('.edd-submit.recurring').not(this).not('.edd_go_to_checkout').addClass('disabled').removeClass('edd-add-to-cart').find('.edd-loading').remove();
}
// when page is refreshed, this prevents them from being clicked and added to the cart.
if ( $(this).hasClass('disabled') ) {
e.preventDefault();
// show a message to the user
alert( "<?php echo $this->notice(); ?>" );
}
});
// on page load, find all disabled recurring buttons and prevent the add to cart functionality
$('.edd-submit.disabled.recurring').removeClass('edd-add-to-cart').find('.edd-loading').remove();
});
</script>
<?php }
/**
* Prevent recurring download from being added to cart with ?edd_action=add_to_cart&download_id=XXX
*
* @param int $download_id Download Post ID
*
* @since 1.0
*/
public function pre_add_to_cart( $download_id ) {
$cart_download_ids = $this->get_cart_ids();
if ( $cart_download_ids ) {
// check cart to see if another recurring download exists
foreach ( $cart_download_ids as $id ) {
if ( 'yes' == get_post_meta( $id, 'edd_recurring', true ) && in_array( $download_id, $cart_download_ids ) ) {
wp_die( $this->notice(), '', array( 'back_link' => true ) );
}
}
}
}
}
/**
* Loads a single instance
*
* This follows the PHP singleton design pattern.
*
* Use this function like you would a global variable, except without needing
* to declare the global.
*
* @example <?php $eddrp_pmcs = eddrp_pmcs(); ?>
*
* @since 1.0
*
* @see EDD_Recurring_Payments_PMCS::get_instance()
*
* @return object Returns an instance of the EDD_Recurring_Payments_PMCS class
*/
function eddrp_pmcs() {
if ( ! class_exists( 'Easy_Digital_Downloads' ) ) {
if ( ! class_exists( 'EDD_Extension_Activation' ) ) {
require_once 'includes/class-activation.php';
}
$activation = new EDD_Extension_Activation( plugin_dir_path( __FILE__ ), basename( __FILE__ ) );
$activation = $activation->run();
} else {
return EDD_Recurring_Payments_PMCS::get_instance();
}
}
add_action( 'plugins_loaded', 'eddrp_pmcs', apply_filters( 'eddrp_pmcs_action_priority', 10 ) );
}