forked from strangerstudios/pmpro-cpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pmpro-cpt.php
361 lines (323 loc) · 12.3 KB
/
pmpro-cpt.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php
/**
* Plugin Name: Paid Memberships Pro - Custom Post Type Add On
* Plugin URI: https://www.paidmembershipspro.com/add-ons/custom-post-type-membership-access/
* Description: Add the PMPro meta box to CPTs and redirects non-members to a selected page.
* Version: 1.0.1
* Author: Paid Memberships Pro
* Author URI: https://www.paidmembershipspro.com/
* Text Domain: pmpro-cpt
* Domain Path: /languages
*/
define( 'PMPRO_CPT_BASENAME', plugin_basename( __FILE__ ) );
function pmprocpt_load_plugin_text_domain() {
load_plugin_textdomain( 'pmpro-cpt', false, basename( dirname( __FILE__ ) ) . '/languages' );
}
add_action( 'init', 'pmprocpt_load_plugin_text_domain');
/**
* pmprocpt_page_meta_wrapper Wrapper to add meta boxes
*
* This is legacy functionality from before the `pmpro_restrictable_post_types` filter was added and will be removed in a future version.
* This function should not be called if running a PMPro version supporting that filter. See `pmprocpt_init()` below for more info.
*
* @return [type] [description]
*/
function pmprocpt_page_meta_wrapper() {
$selected_cpts = pmprocpt_getCPTs();
foreach ( $selected_cpts as $selected_cpt ) {
add_meta_box( 'pmpro_page_meta', esc_html__('Require Membership', 'pmpro-cpt'), 'pmpro_page_meta', $selected_cpt, 'side' );
}
}
/**
* pmprocpt_template_redirect Redirect the restricted CPTs to the selected redirect page.
*
* @return [type] [description]
*/
function pmprocpt_template_redirect() {
if ( ! function_exists( 'pmpro_has_membership_access' ) ) {
return;
}
$selected_cpts = pmprocpt_getCPTs();
if ( empty( $selected_cpts ) ) {
return;
}
$options = get_option( 'pmprocpt_options' );
$redirect_to = isset( $options['redirect_to'][0] ) ? intval( $options['redirect_to'][0] ) : '';
if ( ! empty( $redirect_to ) ) {
$redirect_to = get_permalink( $redirect_to );
}
/**
* Filter the URL redirected to when accessing a restricted CPT
*
* @since .2
*/
$redirect_to = apply_filters( 'pmprocpt_redirect_to', $redirect_to, $selected_cpts, $options );
if ( ! pmpro_has_membership_access() && is_singular( $selected_cpts ) && ! empty( $redirect_to ) ) {
wp_redirect( $redirect_to );
exit;
}
}
add_action( 'template_redirect', 'pmprocpt_template_redirect' );
/**
* pmprocpt_getCPTs Get the array of selected CPTs from the settings page.
*
* @return array An arry of the selected Custom Post Type names to restrict and redirect from.
*/
function pmprocpt_getCPTs() {
$options = get_option( 'pmprocpt_options' );
if ( isset( $options['cpt_selections'] ) && is_array( $options['cpt_selections'] ) ) {
return $options['cpt_selections'];
} else {
return array();
}
}
/**
* Filter the post types that can be restricted.
*
* @param array $post_types An array of post type names.
* @return array An array of post type names.
*/
function pmprocpt_pmpro_restrictable_post_types( $post_types ) {
$selected_cpts = pmprocpt_getCPTs();
if ( ! empty( $selected_cpts ) ) {
$post_types = array_merge( $post_types, $selected_cpts );
}
return $post_types;
}
add_filter( 'pmpro_restrictable_post_types', 'pmprocpt_pmpro_restrictable_post_types' );
/**
* pmprocpt_init Add Settings Page to WordPress admin.
*
* This is legacy functionality from before the `pmpro_restrictable_post_types` filter was added and will be removed in a future version.
*
* @return [type] [description]
*/
function pmprocpt_init() {
// Check if the PMPro_REST_API_Routes::pmpro_rest_api_get_post_restrictions() method exists.
// If so, we are running a PMPro version that supports the `pmpro_restrictable_post_types` filter and
// we don't need to manually add meta boxes to CPTs.
if ( class_exists( 'PMPro_REST_API_Routes' ) && method_exists( 'PMPro_REST_API_Routes', 'pmpro_rest_api_get_post_restrictions' ) ) {
return;
}
if ( is_admin() ) {
add_action( 'admin_menu', 'pmprocpt_page_meta_wrapper' );
}
}
add_action( 'init', 'pmprocpt_init', 20 );
/**
* pmprocpt_admin_init Register settings page and fields for the plugin.
*
* @return [type] [description]
*/
function pmprocpt_admin_init() {
// setup settings
register_setting( 'pmprocpt_options', 'pmprocpt_options', 'pmprocpt_options_validate' );
add_settings_section( 'pmprocpt_section_general', 'Settings', 'pmprocpt_section_general', 'pmprocpt_options' );
add_settings_field( 'pmprocpt_option_cpt_selections', 'Select CPTs', 'pmprocpt_option_cpt_selections', 'pmprocpt_options', 'pmprocpt_section_general' );
add_settings_field( 'pmprocpt_option_redirect_to', 'Redirect to', 'pmprocpt_option_redirect_to', 'pmprocpt_options', 'pmprocpt_section_general' );
}
add_action( 'admin_init', 'pmprocpt_admin_init' );
/**
* pmprocpt_option_cpt_selections Display the multi-select settings field to select CPTs for restriction.
*
* @return [type] [description]
*/
function pmprocpt_option_cpt_selections() {
global $pmprocpt_cpts;
$options = get_option( 'pmprocpt_options' );
$selected_cpts = pmprocpt_getCPTs();
if ( ! empty( $pmprocpt_cpts ) ) {
echo "<select style='min-width: 30%; height: 200px;' multiple='yes' name=\"pmprocpt_options[cpt_selections][]\">";
foreach ( $pmprocpt_cpts as $cpt ) {
if ( in_array( $cpt, array( 'post', 'page', 'attachment', 'revision', 'nav_menu_item', 'forum', 'topic', 'reply', 'product_variation', 'shop_order', 'shop_order_refund', 'shop_coupon', 'shop_webhook', 'plugin_filter', 'plugin_group' ) ) ) {
continue;
}
echo "<option value='" . esc_attr( $cpt ) . "' ";
selected( in_array( $cpt, $selected_cpts ) );
echo '>' . esc_html( $cpt ) . '</option>';
}
echo '</select>';
} else {
echo '<p>';
esc_html_e('No CPTs found.', 'pmpro-cpt');
echo '</p>';
}
echo '<p class="description">';
esc_html_e( 'Setting membership access restrictions for a single CPT will not necessarily hide it from archives, search, or other custom template built into your theme.', 'pmpro-cpt' );
echo '</p>';
}
/**
* pmprocpt_option_redirect_to Display the dropdown settings field to select the redirection page for restricted CPTs.
*
* @return [type] [description]
*/
function pmprocpt_option_redirect_to() {
$options = get_option( 'pmprocpt_options' );
if ( isset( $options['redirect_to'] ) ) {
$redirect_to = $options['redirect_to'][0];
} else {
$redirect_to = '';
}
wp_dropdown_pages(
array(
'name' => 'pmprocpt_options[redirect_to]',
'echo' => 1,
'show_option_none' => '— ' . esc_html__( 'Do Not Redirect', 'pmpro-cpt' ) . ' —',
'option_none_value' => '0',
'selected' => $redirect_to,
)
);
echo '<p class="description">';
esc_html_e( 'This redirection will also apply to a search engine indexing your site.', 'pmpro-cpt' );
echo '</p>';
}
/**
* pmprocpt_section_general Display an information message at the top of the settings page.
*
* @return string Paragraph description
*/
function pmprocpt_section_general() {
echo '<p>';
esc_html_e( 'Select the CPTs (custom post types) from the box below to add the "Require Membership" meta box. Then, select the page to redirect to if a non-member attempts to access a protected CPT.', 'pmpro-cpt' );
echo '</p>';
}
/**
* pmprocpt_options_validate Validate our options
*
* @param [type] $input [description]
*
* @return [type] [description]
*/
function pmprocpt_options_validate( $input ) {
// selected CPTs
$newinput = array();
if ( ! empty( $input['cpt_selections'] ) && is_array( $input['cpt_selections'] ) ) {
$count = count( $input['cpt_selections'] );
for ( $i = 0; $i < $count; $i++ ) {
$newinput['cpt_selections'][] = trim( preg_replace( '[^a-zA-Z0-9\-]', '', $input['cpt_selections'][ $i ] ) );
};
}
if ( ! empty( $input['redirect_to'] ) ) {
//When submit form "redirect_to" comes as a string but if there's no option saved method is fired again and redirect_to comes as an array.
$redirect_to_id = is_array( $input['redirect_to'] ) ? $input['redirect_to'][0] : $input['redirect_to'];
$newinput['redirect_to'][] = trim( preg_replace( '[^a-zA-Z0-9\-]', '', $redirect_to_id ) );
}
return $newinput;
}
/**
* pmprocpt_admin_add_page Add the admin options page
*
* @return [type] [description]
*/
function pmprocpt_admin_add_page() {
add_submenu_page( 'pmpro-dashboard', esc_html__('PMPro Custom Post Type Membership Access', 'pmpro-cpt'), esc_html__('CPT Access', 'pmpro-cpt'), 'manage_options', 'pmprocpt_options', 'pmprocpt_options_page' );
}
add_action( 'admin_menu', 'pmprocpt_admin_add_page' );
/**
* pmprocpt_options_page HTML for options page
*
* @return [type] [description]
*/
function pmprocpt_options_page() {
global $pmprocpt_cpts;
// get options
$options = get_option( 'pmprocpt_options' );
$pmprocpt_cpts = get_post_types(
array(
'public' => true,
'_builtin' => false,
), 'names'
);
$cpt_selections = pmprocpt_getCPTs();
require_once( PMPRO_DIR . "/adminpages/admin_header.php" );
?>
<h1 class="wp-heading-inline">
<?php esc_html_e( 'Custom Post Type Membership Access', 'pmpro-cpt' ); ?>
</h1>
<form action="options.php" method="post">
<p><?php esc_html_e('This plugin will add the PMPro "Require Membership" meta box to all CPTs selected. If a non-member visits that single CPT (either a logged out visitor or a logged in user without membership access) they will be redirected to the selected page.', 'pmpro-cpt'); ?></p>
<hr />
<?php settings_fields( 'pmprocpt_options' ); ?>
<?php do_settings_sections( 'pmprocpt_options' ); ?>
<p class="submit">
<input type="hidden" name="pmprocpt_options[set]" value="1" />
<input type="submit" name="submit" class="button-primary" value="<?php esc_attr_e( 'Save Settings' ); ?>">
</p>
</form>
<?php
require_once( PMPRO_DIR . "/adminpages/admin_footer.php" );
}
/**
* Register activation hook.
*/
register_activation_hook( PMPRO_CPT_BASENAME, 'pmprocpt_admin_notice_activation_hook' );
/**
* pmprocpt_admin_notice_activation_hook Runs only when the plugin is activated.
*
* @since 0.1.0
*
* @return [type] [description]
*/
function pmprocpt_admin_notice_activation_hook() {
// Create transient data.
set_transient( 'pmprocpt-admin-notice', true, 5 );
}
/**
* pmprocpt_admin_notice Admin Notice on Activation.
*
* @since 0.1.0
*
* @return [type] [description]
*/
function pmprocpt_admin_notice() {
// Check transient, if available display notice.
if ( get_transient( 'pmprocpt-admin-notice' ) ) {
?>
<div class="updated notice is-dismissible">
<p><?php
/* translators: The placeholder is for a URL. */
esc_html_e( 'Thank you for activating.', 'pmpro-cpt' );
echo ' <a href="' . esc_url( get_admin_url( null, 'admin.php?page=pmprocpt_options' ) ) . '">';
esc_html_e( 'Visit the settings page to get started with the CPT Add On.', 'pmpro-cpt' );
echo '</a>';
?></p>
</div>
<?php
// Delete transient, only display this notice once.
delete_transient( 'pmprocpt-admin-notice' );
}
}
add_action( 'admin_notices', 'pmprocpt_admin_notice' );
/**
* pmprocpt_add_action_links Add links to the plugin action links
*
* @param array $links Array of existing plugin action links.
*
* @return array $links Array of links to be shown in plugin action links.
*/
function pmprocpt_add_action_links( $links ) {
$new_links = array(
'<a href="' . esc_url( get_admin_url( null, 'admin.php?page=pmprocpt_options' ) ) . '">' . esc_html__( 'Settings', 'pmpro-cpt' ) . '</a>',
);
return array_merge( $new_links, $links );
}
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'pmprocpt_add_action_links' );
/**
* pmprocpt_plugin_row_meta Function to add links to the plugin row meta
*
* @param array $links Array of existing links in plugin meta.
* @param string $file Filename of the plugin meta is being shown for.
*
* @return array $links Array of links to be shown in plugin meta.
*/
function pmprocpt_plugin_row_meta( $links, $file ) {
if ( strpos( $file, 'pmpro-cpt.php' ) !== false ) {
$new_links = array(
'<a href="' . esc_url( 'https://www.paidmembershipspro.com/add-ons/custom-post-type-membership-access/' ) . '" title="' . esc_attr__( 'View Documentation', 'pmpro-cpt' ) . '">' . esc_html__( 'Docs', 'pmpro-cpt' ) . '</a>',
'<a href="' . esc_url( 'https://www.paidmembershipspro.com/support/' ) . '" title="' . esc_attr__( 'Visit Customer Support Forum', 'pmpro-cpt' ) . '">' . esc_html__( 'Support', 'pmpro-cpt' ) . '</a>',
);
$links = array_merge( $links, $new_links );
}
return $links;
}
add_filter( 'plugin_row_meta', 'pmprocpt_plugin_row_meta', 10, 2 );