index.php
7.77 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
<?php
/**
* Handles all server side logic for the ld-course-content Gutenberg Block. This block is functionally the same
* as the course_content shortcode used within LearnDash.
*
* @package LearnDash
* @since 2.5.9
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ( class_exists( 'LearnDash_Gutenberg_Block' ) ) && ( ! class_exists( 'LearnDash_Gutenberg_Block_Course_Content' ) ) ) {
/**
* Class for handling LearnDash Course Content Block
*/
class LearnDash_Gutenberg_Block_Course_Content extends LearnDash_Gutenberg_Block {
/**
* Object constructor
*/
public function __construct() {
$this->shortcode_slug = 'course_content';
$this->block_slug = 'ld-course-content';
$this->block_attributes = array(
'display_type' => array(
'type' => 'string',
),
'course_id' => array(
'type' => 'string',
),
'post_id' => array(
'type' => 'string',
),
'group_id' => array(
'type' => 'string',
),
'per_page' => array(
'type' => 'string',
),
'preview_show' => array(
'type' => 'boolean',
),
'preview_user_id' => array(
'type' => 'string',
),
'example_show' => array(
'type' => 'boolean',
),
'editing_post_meta' => array(
'type' => 'object',
),
);
$this->self_closing = true;
$this->init();
}
/**
* Render Block
*
* This function is called per the register_block_type() function above. This function will output
* the block rendered content. In the case of this function the rendered output will be for the
* [ld_profile] shortcode.
*
* @since 2.5.9
*
* @param array $block_attributes The block attributes.
* @param string $block_content The block content.
* @param WP_block $block The block object.
*
* @return none The output is echoed.
*/
public function render_block( $block_attributes = array(), $block_content = '', WP_block $block = null ) {
$block_attributes = $this->preprocess_block_attributes( $block_attributes );
if ( ( isset( $block_attributes['example_show'] ) ) && ( ! empty( $block_attributes['example_show'] ) ) ) {
$block_attributes['course_id'] = $this->get_example_post_id( learndash_get_post_type_slug( 'course' ) );
$block_attributes['preview_show'] = true;
unset( $block_attributes['example_show'] );
}
// Only the 'editing_post_meta' element will be sent from within the post edit screen.
if ( $this->block_attributes_is_editing_post( $block_attributes ) ) {
/**
* We are only attempting to validate the block settings to display any errors to the user editing the post.
*/
$block_attributes['course_id'] = $this->block_attributes_get_post_id( $block_attributes, 'course' );
$block_attributes['post_id'] = $this->block_attributes_get_post_id( $block_attributes, 'post' );
$block_attributes['group_id'] = $this->block_attributes_get_post_id( $block_attributes, 'group' );
if ( ( empty( $block_attributes['course_id'] ) ) && ( empty( $block_attributes['post_id'] ) ) && ( empty( $block_attributes['group_id'] ) ) ) {
$edit_post_type = $this->block_attributes_get_editing_post_type( $block_attributes );
$edit_post_id = $this->block_attributes_get_editing_post_id( $block_attributes );
if ( learndash_get_post_type_slug( 'group' ) === $edit_post_type ) {
if ( ! empty( $edit_post_id ) ) {
$block_attributes['group_id'] = $edit_post_id;
}
}
if ( in_array( $edit_post_type, learndash_get_post_types( 'course' ), true ) ) {
if ( ! empty( $edit_post_id ) ) {
$block_attributes['post_id'] = $edit_post_id;
}
$course_id = $this->block_attributes_get_editing_course_id( $block_attributes );
if ( ! empty( $course_id ) ) {
$block_attributes['course_id'] = $course_id;
} elseif ( ! empty( $edit_post_id ) ) {
$course_id = learndash_get_course_id( $edit_post_id );
if ( ! empty( $course_id ) ) {
$block_attributes['course_id'] = $course_id;
}
}
}
}
if ( ( empty( $block_attributes['course_id'] ) ) && ( empty( $block_attributes['group_id'] ) ) ) {
return $this->render_block_wrap(
'<span class="learndash-block-error-message">' . sprintf(
// translators: placeholder: Course, Group.
_x( '%1$s ID or %2$s ID is required.', 'placeholder: Course, Group', 'learndash' ),
LearnDash_Custom_Label::get_label( 'course' ),
LearnDash_Custom_Label::get_label( 'group' )
) . '</span>'
);
}
if ( ! empty( $block_attributes['course_id'] ) ) {
$course_post = get_post( $block_attributes['course_id'] );
if ( ( ! is_a( $course_post, 'WP_Post' ) ) || ( learndash_get_post_type_slug( 'course' ) !== $course_post->post_type ) ) {
return $this->render_block_wrap(
'<span class="learndash-block-error-message">' . sprintf(
// translators: placeholder: Course.
_x( 'Invalid %1$s ID.', 'placeholder: Course', 'learndash' ),
LearnDash_Custom_Label::get_label( 'course' )
) . '</span>'
);
}
}
if ( ! empty( $block_attributes['group_id'] ) ) {
$group_post = get_post( $block_attributes['group_id'] );
if ( ( ! is_a( $group_post, 'WP_Post' ) ) || ( learndash_get_post_type_slug( 'group' ) !== $group_post->post_type ) ) {
return $this->render_block_wrap(
'<span class="learndash-block-error-message">' . sprintf(
// translators: placeholder: Group.
_x( 'Invalid %1$s ID.', 'placeholder: Group', 'learndash' ),
LearnDash_Custom_Label::get_label( 'group' )
) . '</span>'
);
}
}
if ( ! empty( $block_attributes['post_id'] ) ) {
$post_post = get_post( $block_attributes['post_id'] );
if ( ( ! is_a( $post_post, 'WP_Post' ) ) || ( ! in_array( $post_post->post_type, learndash_get_post_types( 'course' ), true ) ) ) {
return $this->render_block_wrap(
'<span class="learndash-block-error-message">' . esc_html__( 'Invalid Step ID.', 'learndash' ) . '</span>'
);
}
}
}
/** This filter is documented in includes/gutenberg/blocks/ld-course-list/index.php */
$block_attributes = apply_filters( 'learndash_block_markers_shortcode_atts', $block_attributes, $this->shortcode_slug, $this->block_slug, '' );
$shortcode_out = '';
$shortcode_str = $this->build_block_shortcode( $block_attributes, $block_content );
if ( ! empty( $shortcode_str ) ) {
$shortcode_out = do_shortcode( $shortcode_str );
}
if ( ! empty( $shortcode_out ) ) {
if ( $this->block_attributes_is_editing_post( $block_attributes ) ) {
$shortcode_out = $this->render_block_wrap( $shortcode_out );
}
}
return $shortcode_out;
}
/**
* Called from the LD function learndash_convert_block_markers_shortcode() when parsing the block content.
*
* @since 2.5.9
*
* @param array $block_attributes The array of attributes parse from the block content.
* @param string $shortcode_slug This will match the related LD shortcode ld_profile, ld_course_list, etc.
* @param string $block_slug This is the block token being processed. Normally same as the shortcode but underscore replaced with dash.
* @param string $content This is the original full content being parsed.
*
* @return array $block_attributes.
*/
public function learndash_block_markers_shortcode_atts_filter( $block_attributes = array(), $shortcode_slug = '', $block_slug = '', $content = '' ) {
if ( $shortcode_slug === $this->shortcode_slug ) {
if ( isset( $block_attributes['per_page'] ) ) {
if ( ! isset( $block_attributes['num'] ) ) {
$block_attributes['num'] = $block_attributes['per_page'];
}
}
}
return $block_attributes;
}
// End of functions.
}
}
new LearnDash_Gutenberg_Block_Course_Content();