-
Notifications
You must be signed in to change notification settings - Fork 11
/
pmpro-toolkit.php
227 lines (172 loc) · 8.07 KB
/
pmpro-toolkit.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
<?php
/**
* Plugin Name: PMPro Developer's Toolkit
* Author: Stranger Studios
* Description: Various tools to test and debug Paid Memberships Pro enabled websites.
* Version: .1.2
*/
/*
* Globals
*/
global $pmprodev_options, $gateway;
$pmprodev_options = get_option('pmprodev_options');
if(empty($pmprodev_options)) {
$pmprodev_options = array(
'ipn_debug' => '',
'authnet_silent_post_debug' => '',
'stripe_webhook_debug' => '',
'ins_debug' => '',
'redirect_email' => '',
'checkout_debug_email' => '',
'view_as_enabled' => false
);
}
/*
* Gateway Debug Constants
*/
function pmprodev_gateway_debug_setup() {
global $pmprodev_options;
if(!empty($pmprodev_options['ipn_debug']) && !defined('PMPRO_IPN_DEBUG'))
define('PMPRO_IPN_DEBUG', $pmprodev_options['ipn_debug']);
if(!empty($pmprodev_options['authnet_silent_post_debug']) && !defined('PMPRO_AUTHNET_SILENT_POST_DEBUG'))
define('PMPRO_AUTHNET_SILENT_POST_DEBUG', $pmprodev_options['authnet_silent_post_debug']);
if(!empty($pmprodev_options['stripe_webhook_debug']) && !defined('PMPRO_STRIPE_WEBHOOK_DEBUG'))
define('PMPRO_STRIPE_WEBHOOK_DEBUG', $pmprodev_options['stripe_webhook_debug']);
if(!empty($pmprodev_options['ins_debug']) && !defined('PMPRO_INS_DEBUG'))
define('PMPRO_INS_DEBUG', $pmprodev_options['ins_debug']);
}
add_action('init', 'pmprodev_gateway_debug_setup');
/*
* Redirect PMPro Emails
*/
function pmprodev_redirect_emails($recipient, $email) {
global $pmprodev_options;
if(!empty($pmprodev_options['redirect_email']))
$recipient = $pmprodev_options['redirect_email'];
return $recipient;
}
add_filter('pmpro_email_recipient', 'pmprodev_redirect_emails', 10, 2);
/*
* Send debug email every time checkout page is hit.
*/
function pmprodev_checkout_debug_email($level) {
global $pmprodev_options, $current_user, $wpdb;
if(empty($pmprodev_options['checkout_debug_email']))
return $level;
$email = new PMProEmail();
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')
$http = 'https://';
else
$http = 'http://';
$email->subject = sprintf('%s Checkout Page Debug Log', get_bloginfo('name'));
$email->recipient = $pmprodev_options['checkout_debug_email'];
$email->template = 'checkout_debug';
$email->body = file_get_contents(plugin_dir_path(__FILE__) . '/email/checkout_debug.html');
$email->data = array(
'sitename' => get_bloginfo('sitename'),
'checkout_url' => $http . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'],
'submit' => print_r($_REQUEST['submit-checkout'], true),
'level' => print_r($level, true),
'user' => print_r($current_user->data, true),
'request' => print_r($_REQUEST, true)
);
$order = new MemberOrder();
$order->getLastMemberOrder($current_user->user_id);
if(!empty($order))
$email->data['order'] = print_r($order, true);
$email->sendEmail();
return $level;
}
add_filter('pmpro_checkout_level', 'pmprodev_checkout_debug_email');
/*
* View as specific Membership Level
*/
//create cookie based on query string parameters
function pmprodev_view_as_init() {
global $current_user, $pmprodev_options;
if(!empty($_REQUEST['pmprodev_view_as']))
$view_as_level_ids = $_REQUEST['pmprodev_view_as'];
$membership_level_capability = apply_filters('pmpro_edit_member_capability', 'manage_options');
if(!empty($view_as_level_ids) && !empty($pmprodev_options['view_as_enabled']) && current_user_can($membership_level_capability)) {
//are we resetting the filter?
if($view_as_level_ids == 'r')
setcookie('pmprodev_view_as', '', 0);
else
setcookie('pmprodev_view_as', $view_as_level_ids, null);
}
}
add_action('init', 'pmprodev_view_as_init');
//override pmpro_has_membership_access
function pmprodev_view_as_access_filter($hasaccess, $post, $user, $levels) {
global $pmprodev_options;
$view_as_level_ids = $_COOKIE['pmprodev_view_as'];
$membership_level_capability = apply_filters('pmpro_edit_member_capability', 'manage_options');
if(isset($view_as_level_ids) && current_user_can($membership_level_capability)) {
//default to false to override any real membership levels
$hasaccess = false;
//get level ids for this post
$post_level_ids = array();
foreach($levels as $key=>$level)
$post_level_ids[] = $level->id;
//get view as level ids from cookie
$view_as_level_ids = explode('-', $view_as_level_ids);
foreach($view_as_level_ids as $id) {
//return true when we find a match
if(in_array($id, $post_level_ids))
$hasaccess = true;
}
}
return $hasaccess;
}
add_filter('pmpro_has_membership_access_filter', 'pmprodev_view_as_access_filter', 10, 4);
//override pmpro_hasMembershipLevel() function
//TODO: figure out why this is running before cookie is set...
function pmprodev_view_as_has_membership_level($return, $user_id, $levels) {
global $pmprodev_options;
$view_as_level_ids = $_COOKIE['pmprodev_view_as'];
$membership_level_capability = apply_filters('pmpro_edit_member_capability', 'manage_options');
if(isset($view_as_level_ids) && current_user_can($membership_level_capability)) {
//if we're checking for "0"
if($levels == '0' && $view_as_level_ids == 'n')
return true;
//make levels array if it's not already
if(!is_array($levels))
$levels = array($levels);
//get view as level ids from cookie
$view_as_level_ids = explode('-', $view_as_level_ids);
foreach($view_as_level_ids as $id) {
if(in_array($id, $levels))
return true;
}
//default to false to overrdide real levels
return false;
}
}
add_filter('pmpro_has_membership_level', 'pmprodev_view_as_has_membership_level', 10, 3);
/*
* Add settings page
*/
function pmprodev_admin_menu() {
add_options_page('PMPro Toolkit Settings', 'PMPro Toolkit', apply_filters('pmpro_edit_member_capability', 'manage_options'), 'pmprodev', 'pmprodev_settings_page');
}
add_action('admin_menu', 'pmprodev_admin_menu');
function pmprodev_admin_init() {
//register setting
register_setting('pmprodev_options', 'pmprodev_options');
//add settings sections
add_settings_section('pmprodev-gateway', 'Gateway/Checkout Debugging', 'pmprodev_gateway_settings', 'pmprodev');
add_settings_section('pmprodev-email', 'Email Debugging', 'pmprodev_email_settings', 'pmprodev');
add_settings_section('pmprodev-view-as', '"View as..."', 'pmprodev_view_as_settings', 'pmprodev');
//add settings fields
add_settings_field('ipn-debug', 'PayPal IPN Debug Eamil', 'pmprodev_settings_ipn_debug', 'pmprodev', 'pmprodev-gateway');
add_settings_field('authnet_silent_post_debug', 'Authorize.net Silent Post Debug Eamil', 'pmprodev_settings_authnet_silent_post_debug', 'pmprodev', 'pmprodev-gateway');
add_settings_field('stripe_webhook_debug', 'Stripe Webhook Debug Eamil', 'pmprodev_settings_stripe_webhook_debug', 'pmprodev', 'pmprodev-gateway');
add_settings_field('ins_debug', '2Checkout INS Debug Eamil', 'pmprodev_settings_ins_debug', 'pmprodev', 'pmprodev-gateway');
add_settings_field('redirect_email', 'Redirect PMPro Emails', 'pmprodev_settings_redirect_email', 'pmprodev', 'pmprodev-email');
add_settings_field('checkout_debug_email', 'Send Checkout Debug Email', 'pmprodev_settings_checkout_debug_email', 'pmprodev', 'pmprodev-gateway');
add_settings_field('view_as_enabled', 'Enable "View As" feature', 'pmprodev_settings_view_as_enabled', 'pmprodev', 'pmprodev-view-as');
}
add_action('admin_init', 'pmprodev_admin_init');
function pmprodev_settings_page() {
require_once(plugin_dir_path(__FILE__) . '/adminpages/settings.php');
}