add-user-to-course.php
1 year ago
add-user-to-space.php
1 year ago
create-lesson-in-course.php
10 months ago
create-new-post-feed.php
1 year ago
fetch-members.php
6 months ago
get-all-courses-list.php
1 year ago
get-all-spaces-list.php
1 year ago
like-post.php
6 months ago
list-course-sections.php
10 months ago
list-posts-in-space.php
6 months ago
remove-user-from-course.php
1 year ago
remove-user-from-space.php
1 year ago
reply-to-post.php
7 months ago
create-lesson-in-course.php
268 lines
| 1 | <?php |
| 2 | /** |
| 3 | * CreateLessonInCourse. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category CreateLessonInCourse |
| 7 | * @package SureTriggers |
| 8 | * @author BSF <username@example.com> |
| 9 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 | * @link https://www.brainstormforce.com/ |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | |
| 14 | namespace SureTriggers\Integrations\FluentCommunity\Actions; |
| 15 | |
| 16 | use Exception; |
| 17 | use SureTriggers\Integrations\AutomateAction; |
| 18 | use SureTriggers\Traits\SingletonLoader; |
| 19 | use FluentCommunity\Modules\Course\Model\CourseLesson; |
| 20 | use FluentCommunity\Modules\Course\Model\CourseTopic; |
| 21 | use FluentCommunity\Modules\Course\Services\CourseHelper; |
| 22 | use FluentCommunity\App\Services\RemoteUrlParser; |
| 23 | |
| 24 | /** |
| 25 | * CreateLessonInCourse |
| 26 | * |
| 27 | * @category CreateLessonInCourse |
| 28 | * @package SureTriggers |
| 29 | * @author BSF <username@example.com> |
| 30 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 31 | * @link https://www.brainstormforce.com/ |
| 32 | * @since 1.0.0 |
| 33 | */ |
| 34 | class CreateLessonInCourse extends AutomateAction { |
| 35 | |
| 36 | /** |
| 37 | * Integration type. |
| 38 | * |
| 39 | * @var string |
| 40 | */ |
| 41 | public $integration = 'FluentCommunity'; |
| 42 | |
| 43 | /** |
| 44 | * Action name. |
| 45 | * |
| 46 | * @var string |
| 47 | */ |
| 48 | public $action = 'fc_create_lesson_in_course'; |
| 49 | |
| 50 | use SingletonLoader; |
| 51 | |
| 52 | /** |
| 53 | * Register a action. |
| 54 | * |
| 55 | * @param array $actions actions. |
| 56 | * @return array |
| 57 | */ |
| 58 | public function register( $actions ) { |
| 59 | $actions[ $this->integration ][ $this->action ] = [ |
| 60 | 'label' => __( 'Create Lesson in Course', 'suretriggers' ), |
| 61 | 'action' => $this->action, |
| 62 | 'function' => [ $this, 'action_listener' ], |
| 63 | ]; |
| 64 | |
| 65 | return $actions; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Action listener. |
| 70 | * |
| 71 | * @param int $user_id User ID. |
| 72 | * @param int $automation_id Automation ID. |
| 73 | * @param array $fields Fields. |
| 74 | * @param array $selected_options Selected options. |
| 75 | * |
| 76 | * @return array|void |
| 77 | */ |
| 78 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 79 | $course_id = isset( $selected_options['course_id'] ) ? (int) sanitize_text_field( $selected_options['course_id'] ) : 0; |
| 80 | $section_id = isset( $selected_options['section_id'] ) ? (int) sanitize_text_field( $selected_options['section_id'] ) : 0; |
| 81 | $lesson_title = isset( $selected_options['lesson_title'] ) ? sanitize_text_field( $selected_options['lesson_title'] ) : ''; |
| 82 | $lesson_content = isset( $selected_options['lesson_content'] ) ? wp_kses_post( $selected_options['lesson_content'] ) : ''; |
| 83 | $lesson_status = isset( $selected_options['lesson_status'] ) ? sanitize_text_field( $selected_options['lesson_status'] ) : 'draft'; |
| 84 | $video_url = isset( $selected_options['video_url'] ) ? esc_url_raw( $selected_options['video_url'] ) : ''; |
| 85 | $content_type = isset( $selected_options['content_type'] ) ? sanitize_text_field( $selected_options['content_type'] ) : ( ! empty( $video_url ) ? 'video' : 'text' ); |
| 86 | $enable_comments = isset( $selected_options['enable_comments'] ) ? sanitize_text_field( $selected_options['enable_comments'] ) : 'yes'; |
| 87 | $enable_media = isset( $selected_options['enable_media'] ) ? sanitize_text_field( $selected_options['enable_media'] ) : 'yes'; |
| 88 | $free_preview = isset( $selected_options['free_preview'] ) ? sanitize_text_field( $selected_options['free_preview'] ) : 'no'; |
| 89 | |
| 90 | if ( empty( $lesson_title ) ) { |
| 91 | return [ |
| 92 | 'status' => 'error', |
| 93 | 'message' => 'Lesson title is required.', |
| 94 | ]; |
| 95 | } |
| 96 | |
| 97 | if ( empty( $course_id ) ) { |
| 98 | return [ |
| 99 | 'status' => 'error', |
| 100 | 'message' => 'Course ID is required.', |
| 101 | ]; |
| 102 | } |
| 103 | |
| 104 | if ( empty( $section_id ) ) { |
| 105 | return [ |
| 106 | 'status' => 'error', |
| 107 | 'message' => 'Section ID is required.', |
| 108 | ]; |
| 109 | } |
| 110 | |
| 111 | if ( ! class_exists( 'FluentCommunity\Modules\Course\Model\CourseLesson' ) ) { |
| 112 | return [ |
| 113 | 'status' => 'error', |
| 114 | 'message' => 'CourseLesson class not found.', |
| 115 | ]; |
| 116 | } |
| 117 | |
| 118 | if ( ! class_exists( 'FluentCommunity\Modules\Course\Model\CourseTopic' ) ) { |
| 119 | return [ |
| 120 | 'status' => 'error', |
| 121 | 'message' => 'CourseTopic class not found.', |
| 122 | ]; |
| 123 | } |
| 124 | |
| 125 | if ( ! class_exists( 'FluentCommunity\Modules\Course\Services\CourseHelper' ) ) { |
| 126 | return [ |
| 127 | 'status' => 'error', |
| 128 | 'message' => 'CourseHelper class not found.', |
| 129 | ]; |
| 130 | } |
| 131 | |
| 132 | if ( ! class_exists( 'FluentCommunity\App\Services\RemoteUrlParser' ) ) { |
| 133 | return [ |
| 134 | 'status' => 'error', |
| 135 | 'message' => 'RemoteUrlParser class not found.', |
| 136 | ]; |
| 137 | } |
| 138 | |
| 139 | $validation_result = $this->is_valid_section_in_course( $section_id, $course_id ); |
| 140 | if ( is_array( $validation_result ) ) { |
| 141 | return $validation_result; |
| 142 | } |
| 143 | if ( ! $validation_result ) { |
| 144 | return [ |
| 145 | 'status' => 'error', |
| 146 | 'message' => 'Invalid Section ID for the specified course.', |
| 147 | ]; |
| 148 | } |
| 149 | |
| 150 | try { |
| 151 | // First create the lesson with basic data. |
| 152 | $lesson_data = [ |
| 153 | 'title' => $lesson_title, |
| 154 | 'parent_id' => $section_id, |
| 155 | 'space_id' => $course_id, |
| 156 | 'status' => $lesson_status, |
| 157 | ]; |
| 158 | |
| 159 | // Create the lesson. |
| 160 | $lesson = CourseLesson::create( $lesson_data ); |
| 161 | |
| 162 | // Now prepare the meta data for the lesson. |
| 163 | $lesson_meta = [ |
| 164 | 'enable_comments' => $enable_comments, |
| 165 | 'enable_media' => $enable_media, |
| 166 | 'document_lists' => [], |
| 167 | ]; |
| 168 | |
| 169 | // Add video URL if provided. |
| 170 | if ( ! empty( $video_url ) ) { |
| 171 | // Parse the video URL to get oembed data. |
| 172 | $oembed_data = RemoteUrlParser::parse( $video_url ); |
| 173 | |
| 174 | if ( $oembed_data && ! is_wp_error( $oembed_data ) ) { |
| 175 | // Use the parsed oembed data which includes HTML embed code. |
| 176 | $lesson_meta['media'] = $oembed_data; |
| 177 | $lesson_meta['enable_media'] = 'yes'; |
| 178 | } else { |
| 179 | // If oembed parsing fails, try WordPress native oembed. |
| 180 | $wp_oembed = new \WP_oEmbed(); |
| 181 | $oembed_html = $wp_oembed->get_html( $video_url ); |
| 182 | |
| 183 | if ( $oembed_html ) { |
| 184 | $lesson_meta['media'] = [ |
| 185 | 'type' => 'oembed', |
| 186 | 'url' => $video_url, |
| 187 | 'content_type' => 'video', |
| 188 | 'provider' => '', |
| 189 | 'title' => '', |
| 190 | 'html' => $oembed_html, |
| 191 | ]; |
| 192 | $lesson_meta['enable_media'] = 'yes'; |
| 193 | } else { |
| 194 | // Final fallback - store URL for manual processing. |
| 195 | $lesson_meta['media'] = [ |
| 196 | 'type' => 'oembed', |
| 197 | 'url' => $video_url, |
| 198 | 'content_type' => 'video', |
| 199 | 'provider' => '', |
| 200 | 'title' => '', |
| 201 | 'html' => '', |
| 202 | ]; |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // Add free preview setting if specified. |
| 208 | if ( 'yes' === $free_preview ) { |
| 209 | $lesson_meta['free_preview_lesson'] = 'yes'; |
| 210 | } |
| 211 | |
| 212 | // Sanitize the lesson meta using CourseHelper. |
| 213 | $sanitized_meta = CourseHelper::sanitizeLessonMeta( $lesson_meta, $lesson ); |
| 214 | |
| 215 | // Update the lesson with sanitized content and meta. |
| 216 | $update_data = [ |
| 217 | 'message' => CourseHelper::santizeLessonBody( $lesson_content ), |
| 218 | 'content_type' => $content_type, |
| 219 | 'meta' => $sanitized_meta, |
| 220 | ]; |
| 221 | |
| 222 | $lesson->fill( $update_data ); |
| 223 | $lesson->save(); |
| 224 | |
| 225 | $lesson = CourseLesson::findOrFail( $lesson->id ); |
| 226 | |
| 227 | return [ |
| 228 | 'status' => 'success', |
| 229 | 'message' => 'Lesson created successfully in course', |
| 230 | 'lesson_id' => $lesson->id, |
| 231 | 'lesson' => $lesson, |
| 232 | 'course_id' => $course_id, |
| 233 | 'section_id' => $section_id, |
| 234 | ]; |
| 235 | |
| 236 | } catch ( Exception $e ) { |
| 237 | return [ |
| 238 | 'status' => 'error', |
| 239 | 'message' => 'An error occurred while creating the lesson: ' . $e->getMessage(), |
| 240 | ]; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Check if the section ID is valid for the specified course. |
| 246 | * |
| 247 | * @param int $section_id Section ID. |
| 248 | * @param int $course_id Course ID. |
| 249 | * @return bool|array |
| 250 | */ |
| 251 | private function is_valid_section_in_course( $section_id, $course_id ) { |
| 252 | if ( ! class_exists( 'FluentCommunity\Modules\Course\Model\CourseTopic' ) ) { |
| 253 | return [ |
| 254 | 'status' => 'error', |
| 255 | 'message' => 'CourseTopic class not found.', |
| 256 | ]; |
| 257 | } |
| 258 | |
| 259 | $topic = CourseTopic::where( 'id', $section_id ) |
| 260 | ->where( 'space_id', $course_id ) |
| 261 | ->first(); |
| 262 | |
| 263 | return (bool) $topic; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | CreateLessonInCourse::get_instance(); |
| 268 |