Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/paid-memberships-pro/includes/blocks.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
<?php
2
+
/**
3
+
* Add new block category for Paid Memberships Pro blocks.
4
+
*
5
+
* @since 1.0
6
+
*
7
+
* @param array $categories Array of block categories.
8
+
* @return array Array of block categories.
9
+
*/
10
+
function pmpro_block_categories( $categories ) {
11
+
return array_merge(
12
+
$categories,
13
+
array(
14
+
array(
15
+
'slug' => 'pmpro',
16
+
'title' => esc_html__( 'Paid Memberships Pro', 'paid-memberships-pro' ),
17
+
),
18
+
array(
19
+
'slug' => 'pmpro-pages',
20
+
'title' => esc_html__( 'Paid Memberships Pro Pages', 'paid-memberships-pro' ),
21
+
),
22
+
)
23
+
);
24
+
}
25
+
add_filter( 'block_categories_all', 'pmpro_block_categories' );
26
+
27
+
/**
28
+
* Register block types for the block editor.
29
+
*/
30
+
function pmpro_register_block_types() {
31
+
if ( function_exists( 'register_block_type' ) ) {
32
+
register_block_type( PMPRO_DIR . '/blocks/build/account-invoices-section' );
33
+
register_block_type( PMPRO_DIR . '/blocks/build/account-profile-section' );
34
+
register_block_type( PMPRO_DIR . '/blocks/build/account-links-section' );
35
+
register_block_type( PMPRO_DIR . '/blocks/build/account-membership-section' );
36
+
register_block_type( PMPRO_DIR . '/blocks/build/account-page' );
37
+
register_block_type( PMPRO_DIR . '/blocks/build/billing-page' );
38
+
register_block_type( PMPRO_DIR . '/blocks/build/cancel-page' );
39
+
register_block_type( PMPRO_DIR . '/blocks/build/checkout-button' );
40
+
register_block_type( PMPRO_DIR . '/blocks/build/checkout-page' );
41
+
register_block_type( PMPRO_DIR . '/blocks/build/confirmation-page' );
42
+
register_block_type( PMPRO_DIR . '/blocks/build/invoice-page' );
43
+
register_block_type( PMPRO_DIR . '/blocks/build/levels-page' );
44
+
register_block_type( PMPRO_DIR . '/blocks/build/login' );
45
+
register_block_type( PMPRO_DIR . '/blocks/build/member-profile-edit' );
46
+
register_block_type( PMPRO_DIR . '/blocks/build/membership' );
47
+
register_block_type( PMPRO_DIR . '/blocks/build/single-level' );
48
+
register_block_type( PMPRO_DIR . '/blocks/build/single-level-name' );
49
+
register_block_type( PMPRO_DIR . '/blocks/build/single-level-expiration' );
50
+
register_block_type( PMPRO_DIR . '/blocks/build/single-level-description' );
51
+
register_block_type( PMPRO_DIR . '/blocks/build/single-level-price' );
52
+
}
53
+
}
54
+
add_action( 'init', 'pmpro_register_block_types' );
55
+
/**
56
+
* Enqueue block editor only CSS.
57
+
*/
58
+
function pmpro_block_editor_assets() {
59
+
// Enqueue the CSS file css/blocks.editor.css.
60
+
wp_enqueue_style(
61
+
'pmpro-block-editor-css',
62
+
PMPRO_URL . '/css/blocks.editor.css',
63
+
array( 'wp-edit-blocks' )
64
+
);
65
+
66
+
// If we're editing a post that can be restricted, enqueue the sidebar block editor script.
67
+
if ( in_array( get_post_type(), apply_filters( 'pmpro_restrictable_post_types', array( 'page', 'post' ) ) ) ) {
68
+
wp_register_script(
69
+
'pmpro-sidebar-editor-script',
70
+
PMPRO_URL . '/blocks/build/sidebar/index.js',
71
+
array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-i18n', 'wp-editor', 'wp-api-request', 'wp-plugins', 'wp-edit-post' )
72
+
);
73
+
wp_localize_script(
74
+
'pmpro-sidebar-editor-script',
75
+
'pmpro_block_editor_sidebar',
76
+
array(
77
+
'post_id' => get_the_ID(),
78
+
)
79
+
);
80
+
wp_enqueue_script( 'pmpro-sidebar-editor-script' );
81
+
}
82
+
83
+
wp_register_script(
84
+
'pmpro-component-content-visibility-script',
85
+
PMPRO_URL . '/blocks/build/component-content-visibility/index.js',
86
+
array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-i18n', 'wp-editor', 'wp-api-request', 'wp-plugins', 'wp-edit-post' )
87
+
);
88
+
89
+
wp_localize_script(
90
+
'pmpro-component-content-visibility-script',
91
+
'pmpro_content_visibility_component',
92
+
array(
93
+
'post_id' => get_the_ID(),
94
+
)
95
+
);
96
+
wp_enqueue_script( 'pmpro-component-content-visibility-script' );
97
+
98
+
}
99
+
add_action( 'enqueue_block_editor_assets', 'pmpro_block_editor_assets' );
100
+
101
+
/**
102
+
* Register post meta needed for our blocks.
103
+
*
104
+
* @since 3.0
105
+
*/
106
+
function pmpro_register_post_meta() {
107
+
// Register pmpro_default_level for the checkout block.
108
+
register_post_meta(
109
+
'',
110
+
'pmpro_default_level',
111
+
array(
112
+
'show_in_rest' => true,
113
+
'single' => true,
114
+
'type' => 'string',
115
+
)
116
+
);
117
+
}
118
+
add_action( 'init', 'pmpro_register_post_meta' );
119
+
120
+
/**
121
+
* Render the block content on the frontend based on content visibility attributes.
122
+
*
123
+
* @param array $attributes The block attributes.
124
+
* @param array $content The block content.
125
+
* @return string the filtered output
126
+
* @since 3.0
127
+
*/
128
+
function pmpro_apply_block_visibility( $attributes, $content ) {
129
+
$output = '';
130
+
131
+
if ( 'all' === $attributes['segment'] && ! empty( $attributes['levels'] ) ) {
132
+
// Legacy setup for PMPro < 3.0.
133
+
if ( ! array_key_exists( 'levels', $attributes ) || empty( $attributes['levels'] ) ) {
134
+
// Assume require any membership level, and do not show to non-members.
135
+
if ( pmpro_hasMembershipLevel() ) {
136
+
$output = do_blocks( $content );
137
+
}
138
+
} else {
139
+
if ( pmpro_hasMembershipLevel( $attributes['levels'] ) ) {
140
+
$output = do_blocks( $content );
141
+
} elseif ( ! empty( $attributes['show_noaccess'] ) ) {
142
+
$output = pmpro_get_no_access_message( NULL, $attributes['levels'] );
143
+
}
144
+
}
145
+
} else {
146
+
// Setup for PMPro >= 3.0.
147
+
switch ( $attributes['segment'] ) {
148
+
case 'all':
149
+
$levels_to_check = null; // All levels.
150
+
break;
151
+
case 'specific':
152
+
$levels_to_check = $attributes['levels']; // Specific levels.
153
+
break;
154
+
case 'logged_in':
155
+
$levels_to_check = 'L'; // Logged in users.
156
+
break;
157
+
}
158
+
159
+
$should_show = empty( $attributes['invert_restrictions'] ) ? pmpro_hasMembershipLevel( $levels_to_check ) : ! pmpro_hasMembershipLevel( $levels_to_check );
160
+
if ( $should_show ) {
161
+
$output = do_blocks( $content );
162
+
} elseif ( ! empty( $attributes['show_noaccess'] ) && empty( $attributes['invert_restrictions'] ) ) {
163
+
$output = pmpro_get_no_access_message( NULL, $attributes['levels'] );
164
+
}
165
+
}
166
+
return $output;
167
+
}
168
+
169
+
/**
170
+
* Hook into render_block to filter core blocks and apply content visibility rules.
171
+
*
172
+
* @param string $block_content The block content.
173
+
* @param array $block The block.
174
+
* @return string The filtered block content.
175
+
* @since 3.0
176
+
*/
177
+
function pmpro_filter_core_blocks( $block_content, $block ) {
178
+
// TODO Replace with https://www.php.net/manual/en/function.str-starts-with when we drop support for PHP 7.x.
179
+
180
+
// Return block if there are no attributes or content visibility is not enabled.
181
+
if ( empty( $block['attrs'] ) || empty( $block['attrs']['visibilityBlockEnabled'] ) ) {
182
+
return $block_content;
183
+
}
184
+
185
+
// Return block if this is not a core block.
186
+
if ( strpos( $block['blockName'], 'core/' ) != 0 ) {
187
+
return $block_content;
188
+
}
189
+
190
+
// We need defaults because WP doesn't store defaults in the DB.
191
+
$attributes = wp_parse_args( $block['attrs'], array(
192
+
'segment' => 'all',
193
+
'levels' => array(),
194
+
'show_noaccess' => '0',
195
+
'invert_restrictions' => '0',
196
+
) );
197
+
return pmpro_apply_block_visibility( $attributes, $block_content );
198
+
}
199
+
add_filter( 'render_block', 'pmpro_filter_core_blocks', 10, 2 );
200
+
201
+
/**
202
+
* Add visibility content attributes server side as well.
203
+
*
204
+
* @param array $metadata The block metadata.
205
+
* @return array The filtered block metadata.
206
+
* @since 3.1
207
+
*
208
+
*/
209
+
function pmpro_block_type_metadata( $metadata ) {
210
+
//bail if it's not a core block
211
+
if ( empty( $metadata['name'] ) || ! str_starts_with( $metadata['name'], 'core/' ) ) {
212
+
return $metadata;
213
+
}
214
+
215
+
$metadata['attributes']['visibilityBlockEnabled'] = array(
216
+
'type' => 'boolean',
217
+
'default' => false,
218
+
);
219
+
$metadata['attributes']['invert_restrictions'] = array(
220
+
'type' => 'boolean',
221
+
'default' => false,
222
+
);
223
+
$metadata['attributes']['segment'] = array(
224
+
'type' => 'string',
225
+
'default' => 'all',
226
+
);
227
+
$metadata['attributes']['levels'] = array(
228
+
'type' => 'array',
229
+
'default' => array(),
230
+
);
231
+
$metadata['attributes']['show_noaccess'] = array(
232
+
'type' => 'boolean',
233
+
'default' => false,
234
+
);
235
+
return $metadata;
236
+
}
237
+
238
+
add_filter( 'block_type_metadata', 'pmpro_block_type_metadata', 10, 1 );