Skip to content

Commit 2ac96e8

Browse files
committed
Add comments explaining course saving process
1 parent 137a105 commit 2ac96e8

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

includes/admin/class-sensei-course-pre-publish-panel.php

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,33 @@ public function enqueue_pre_publish_panel_assets() {
6969
* @param string $old_status Old post status.
7070
*/
7171
public function maybe_publish_lessons( $course_id, $post, $old_status ) {
72+
/**
73+
* When Saving/Publishing a Course from the Course editor, usually three primary network calls are made from the Gutenberg editor serially -
74+
*
75+
* 1. The first call is initiated automatically by GB the moment we click the Publish/Update button, it saves the whole block `markup` of the course, but doesn't save the structure of the course, so no lessons or modules.
76+
* 2. After first call is successful, Sensei explicitly makes a second call to save the structure of the course, i.e. the lessons and modules.
77+
* 3. After the second call is successful, Sensei triggers the Post save call again, similar to the first call.
78+
*
79+
* When we click on the Publish button for a new Course containing new unsaved lessons, the first call (1) publishes the Draft course, new lessons are not saved yet.
80+
* So if we try to find lessons under this Course at this point with this `publish_course` hook, we'll only get existing lessons, not the new ones.
81+
*
82+
* The second call (2) saves the new lessons and modules of the course. This is not a Post save/publish call, so it doesn't trigger the `publish_course` hook.
83+
*
84+
* When the second call is successful, Sensei triggers the Post save call again, which invokes this `publish_course`. By this time, the new lessons are saved and we can find and publish them.
85+
* An important point is `publish_course` hook is invoked even when 'Update' is clicked on an already published course. So before publishing the lessons using this hook, we need to check if the course is being published or just being updated.
86+
*/
7287
if ( ! current_user_can( 'publish_post', $course_id ) ) {
7388
return;
7489
}
7590

91+
// In parallel to the 3 calls mentioned above, GB also initiates some calls to save metabox data, they also trigger this hook. But we don't want to process anything for them.
92+
$uri = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
93+
$is_metabox_save_call = strpos( $uri, 'meta-box-loader=1' ) > 0;
94+
95+
if ( $is_metabox_save_call ) {
96+
return;
97+
}
98+
7699
$publish_lessons = get_post_meta( $course_id, 'sensei_course_publish_lessons', true );
77100

78101
if ( ! $publish_lessons ) {
@@ -81,31 +104,24 @@ public function maybe_publish_lessons( $course_id, $post, $old_status ) {
81104

82105
$publishing_meta_key = '_sensei_course_publishing_started';
83106

84-
// Even if the course is already published, each subsequent updates also triggers this hook anyway
85-
// which caused the bug https://github.com/Automattic/sensei/issues/7555.
86-
// So we need to check if it's an actual publish call.
107+
// This is how we can determine if the current call is the main publish call or not. This should be true when call (1) is made to publish the course.
87108
$is_main_publish_call = 'publish' !== $old_status;
88109

89110
if ( $is_main_publish_call ) {
90-
// This is the first call made, it's not the structure saving call, so the added/updated lessons are not yet saved at this point.
91-
// So we set the flag to publish lessons on the next call, which is made after the structure is saved.
111+
// If it's the main publish call, we set this flag to use in the call (3) which will come later to determine if the call is made as part of the publishing sequence or just a normal update sequence.
92112
update_post_meta( $course_id, $publishing_meta_key, true );
113+
// We don't return early here, because we still need to publish the lessons, in case we are publishing a course with existing draft lessons.
93114
}
94115

95116
$is_publishing_started = get_post_meta( $course_id, $publishing_meta_key, true );
96117

97118
if ( ! $is_main_publish_call && ! $is_publishing_started ) {
98-
// If its not the "Publish" call and the flag is not set, then we don't need to publish lessons.
99-
// Because it that case it's just a normal "Update" call.
119+
// If it's not the main publish call and the flag is not set, then it's just an update call sequence, We don't publish anything in normal update sequence, so we return early.
100120
return;
101121
}
102122

103-
$uri = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
104-
$is_metabox_save_call = strpos( $uri, 'meta-box-loader=1' ) > 0;
105-
106-
if ( ! $is_main_publish_call && ! $is_metabox_save_call ) {
107-
// If it's not the main publish call, then it's the structure saving call that comes immediately after the main publish call.
108-
// So we can remove the flag now, because after this iteraction, the whole publishing cycle is complete.
123+
if ( ! $is_main_publish_call ) {
124+
// If it has reached here, this means it is call (3), end of the sequence has been reached by the Publishing cycle. So we just delete the flag.
109125
delete_post_meta( $course_id, $publishing_meta_key );
110126
}
111127

0 commit comments

Comments
 (0)