-
Notifications
You must be signed in to change notification settings - Fork 382
Expand file tree
/
Copy pathdivi.php
More file actions
245 lines (210 loc) · 7.79 KB
/
divi.php
File metadata and controls
245 lines (210 loc) · 7.79 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
<?php
class PMProDivi{
function __construct(){
if ( empty( $_GET['page'] ) || 'et_divi_role_editor' !== $_GET['page'] ) {
add_filter( 'et_builder_get_parent_modules', array( __CLASS__, 'toggle' ) );
add_filter( 'et_pb_module_content', array( __CLASS__, 'restrict_content' ), 10, 4 );
add_filter( 'et_pb_all_fields_unprocessed_et_pb_row', array( __CLASS__, 'row_settings' ) );
add_filter( 'et_pb_all_fields_unprocessed_et_pb_section', array( __CLASS__, 'row_settings' ) );
}
add_action( 'pmpro_element_class', array( __CLASS__, 'pmpro_element_class' ), 10, 2 );
add_action( 'et_pb_module_shortcode_attributes', array( __CLASS__, 'map_pmpro_divi_legacy_settings_on_page_load' ), 10, 3 );
}
/**
* Add "Paid Memberships Pro" toggle to Divi (row and section) settings modal.
*
* @since TBD
*
* @param array $modules The Divi modules.
* @return array $modules The modified Divi modules.
*/
public static function toggle( $modules ) {
if ( isset( $modules['et_pb_row'] ) && is_object( $modules['et_pb_row'] ) ) {
$modules['et_pb_row']->settings_modal_toggles['custom_css']['toggles']['paid-memberships-pro'] = __( 'Paid Memberships Pro', 'paid-memberships-pro' );
}
if ( isset( $modules['et_pb_section'] ) && is_object( $modules['et_pb_section'] ) ) {
$modules['et_pb_section']->settings_modal_toggles['custom_css']['toggles']['paid-memberships-pro'] = __( 'Paid Memberships Pro', 'paid-memberships-pro' );
}
return $modules;
}
/**
* Add settings to the Divi row and section settings modal for Paid Memberships Pro.
*
* @since TBD
*
* @param array $settings The Divi module settings.
* @return array $settings The modified Divi module settings.
*/
public static function row_settings( $settings ) {
$settings['pmpro_enable'] = array(
'tab_slug' => 'custom_css',
'label' => __( 'Enable Paid Memberships Pro content visibility?', 'paid-memberships-pro' ),
'type' => 'yes_no_button',
'options' => array(
'off' => __( 'No', 'paid-memberships-pro' ),
'on' => __( 'Yes', 'paid-memberships-pro' ),
),
'toggle_slug' => 'paid-memberships-pro',
);
$settings['pmpro_invert_restrictions'] = array(
'tab_slug' => 'custom_css',
'label' => __( 'Content Visibility', 'paid-memberships-pro' ),
'type' => 'select',
'options' => array(
'hide' => __( 'Hide content from...', 'paid-memberships-pro' ),
'show' => __( 'Show content to...', 'paid-memberships-pro' ),
),
'toggle_slug' => 'paid-memberships-pro',
'show_if' => array(
'pmpro_enable' => 'on',
),
);
$settings['pmpro_segment'] = array(
'tab_slug' => 'custom_css',
'type' => 'select',
'options' => array(
'all' => __( 'All Members', 'paid-memberships-pro' ),
'specific' => __( 'Specific Membership Levels', 'paid-memberships-pro' ),
'logged_in' => __( 'Logged-In Users', 'paid-memberships-pro' ),
),
'toggle_slug' => 'paid-memberships-pro',
'show_if' => array(
'pmpro_enable' => 'on'
),
);
$settings['pmpro_levels'] = array(
'tab_slug' => 'custom_css',
'label' => __( 'Membership Levels', 'paid-memberships-pro' ),
'description' => __( 'Enter comma-separated level IDs.', 'paid-memberships-pro' ),
'type' => 'text',
'default' => '',
'option_category' => 'configuration',
'toggle_slug' => 'paid-memberships-pro',
'show_if' => array(
'pmpro_enable' => 'on',
'pmpro_segment' => 'specific',
),
);
$settings['pmpro_show_noaccess'] = array(
'tab_slug' => 'custom_css',
'label' => __( 'Show no access message', 'paid-memberships-pro' ),
'description' => __( 'Displays a no access message to non-members.', 'paid-memberships-pro' ),
'type' => 'yes_no_button',
'options' => array(
'off' => __( 'No', 'paid-memberships-pro' ),
'on' => __( 'Yes', 'paid-memberships-pro' ),
),
'toggle_slug' => 'paid-memberships-pro',
'show_if' => array(
'pmpro_enable' => 'on',
'pmpro_invert_restrictions' => 'show'
),
);
return $settings;
}
/**
* Restrict Divi content based on PMPro membership levels access.
*
* @since TBD
*
* @param [type] $output
* @param [type] $props
* @param [type] $attrs
* @param [type] $slug
*
* @return string $output Returns the output of the content, in some cases it may be a blank string.
*/
public static function restrict_content( $output, $props, $attrs, $slug ) {
// Always allow content in Divi builder mode
if ( et_fb_is_enabled() ) {
return $output;
}
// Don't try to run this logic on anything besides a row or section since we only add restrictions to these modules.
if ( $slug !== 'et_pb_row' && $slug !== 'et_pb_section' ) {
return $output;
}
// If PMPro restriction isn’t turned on, let's migrate some options.
if ( ! isset( $props['pmpro_enable'] ) || $props['pmpro_enable'] !== 'on' ) {
return $output;
}
// Determine if we're in “show” or “hide” mode, defaulting to 'show'.
$show_mode = $props['pmpro_invert_restrictions'] ?? 'show';
// Pick the right segment key based on mode.
$segment = $props['pmpro_segment'] ?? 'all';
// If “specific” but no levels provided, just show content
if ( $segment === 'specific' ) {
$level_string = trim( $props['pmpro_levels'] ?? '' );
// If no levels are provided, restrict to all levels
if ( $level_string !== '0' && $level_string === '' ) {
$levels = array();
} else {
// Build an array of level IDs from a comma-separated string
$levels = strpos( $level_string, ',' ) !== false ? array_map( 'trim', explode( ',', $level_string ) ) : array( $level_string );
}
} else {
$levels = array();
}
//Check access based on segment chosen.
switch ( $segment ) {
case 'all':
$has_access = pmpro_hasMembershipLevel();
break;
case 'specific':
$has_access = pmpro_hasMembershipLevel( $levels );
break;
case 'logged_in':
$has_access = is_user_logged_in();
break;
default:
return $output;
}
// If in “show” mode & they have access, OR in “hide” mode & they do NOT have access, show the content.
if ( ( $show_mode == 'show' && $has_access ) || ( $show_mode == 'hide' && ! $has_access ) ) {
return $output;
}
// Otherwise, they don’t have access—show message or nothing
if ( isset( $props['pmpro_show_noaccess'] ) && $props['pmpro_show_noaccess'] === 'on' ) {
return pmpro_get_no_access_message( null, $levels );
}
// In case we ever make it here, just return nothing. This means they don't have access?
return '';
}
/**
* Filter the element classes added to the no_access messages for improved appearance in Divi.
* Hooked into pmpro_element_class.
* @since 2.8.2
*/
public static function pmpro_element_class( $class, $element ) {
if ( in_array( 'pmpro_content_message', $class ) ) {
$class[] = 'et_pb_row';
}
return $class;
}
/**
* Swap out any legacy settings that may happen on page load/frontend.
*
* @since TBD
*
* @param [type] $props
* @param [type] $atts
* @param [type] $slugs
* @return array $props The shortcode properties with the mapped props.
*/
static function map_pmpro_divi_legacy_settings_on_page_load( $props, $atts, $slugs ) {
if ( isset( $props['pmpro_enable'] ) || ! isset( $props['paid-memberships-pro'] ) ) {
return $props;
}
// Do nothing
if ( empty( $props['paid-memberships-pro'] ) ) {
return $props;
}
// Let's convert to the new props based on the old props.
$props['pmpro_enable'] = 'on';
$props['pmpro_segment'] = 'specific';
$props['pmpro_levels'] = $props['paid-memberships-pro'];
$props['pmpro_invert_restrictions'] = 'show';
$props['pmpro_show_noaccess'] = $props['pmpro_show_no_access_message'] ?? 'off';
return $props;
}
}
new PMProDivi();