is_instructor() ) { throw new Exception( __( 'You do not have permission to create or edit courses', 'learnpress' ) ); } $userCoursePostModel = new CoursePostModel(); if ( ! $userCoursePostModel->check_capabilities_create() ) { throw new Exception( __( 'You do not have permission to create or edit courses', 'learnpress' ) ); } $item_id = $data['item_id'] ?? ''; if ( empty( $item_id ) ) { throw new Exception( __( 'Invalid course ID', 'learnpress' ) ); } /*if ( ! CourseBuilderAccessPolicy::can_access_tab_post( 'courses', $item_id ) ) { throw new Exception( __( "Sorry, you don't have permission to access this content", 'learnpress' ) ); }*/ $is_create_new = $item_id === CourseBuilder::POST_NEW; $courseModel = false; if ( ! $is_create_new ) { $courseModel = CourseModel::find( (int) $item_id, true ); if ( ! $courseModel ) { throw new Exception( __( 'Course not found', 'learnpress' ) ); } // Check permission $coursePostModel = $courseModel->get_post_model(); if ( ! $coursePostModel->check_capabilities_update() ) { throw new Exception( __( 'You do not have permission to edit this course', 'learnpress' ) ); } if ( $courseModel->get_status() === PostModel::STATUS_TRASH ) { throw new Exception( __( 'You cannot edit this item because it is in the Trash. Please restore it and try again.', 'learnpress' ) ); } } $data['courseModel'] = $courseModel; $section = [ 'wrap' => sprintf( '
', esc_attr( $item_id ), ), 'header' => $this->html_header( $data ), 'tabs' => $this->html_tabs( $data ), 'wrap_end' => '
', ]; $html = Template::combine_components( $section ); } catch ( Throwable $e ) { $html = Template::print_message( $e->getMessage(), 'error', false ); } return $html; } /** * HTML header * * @param array $data * * @return string */ public function html_header( array $data = [] ): string { /** @var UserModel $userModel */ $userModel = $data['userModel'] ?? false; if ( ! $userModel instanceof UserModel ) { return ''; } /** @var CourseModel|false $courseModel */ $courseModel = $data['courseModel'] ?? false; $hide_instructor_access_admin_screen = LP_Settings::is_hide_instructor_access_admin_screen(); $more_actions_icon = LP_WP_Filesystem::get_icon_svg( 'ico-cb-more.svg' ); $title = $courseModel ? $courseModel->get_title() : __( 'Add New Course', 'learnpress' ); $status_badge = $courseModel ? $courseModel->get_status() : ''; $status = ''; if ( $courseModel ) { $status = $courseModel->get_status(); } $main_action_status = in_array( $status, array( 'publish', 'draft', 'pending', 'future', 'private', ), true ) ? $status : 'publish'; $wp_user = new WP_User( $userModel ); $hide_wp_edit_link = $hide_instructor_access_admin_screen && user_can( $wp_user, UserModel::ROLE_INSTRUCTOR ); $section = [ 'header_wrap' => '
', 'header_left' => '
', 'title' => sprintf( '

%s

', esc_html( $title ) ), 'status_badge' => $courseModel ? sprintf( '%s', $status_badge, esc_html( $courseModel->get_post_model()->get_status_i18n() ) ) : '', 'link_edit_on_wp' => $courseModel && ! $hide_wp_edit_link ? sprintf( ' %2$s ', esc_url( admin_url( "post.php?post={$courseModel->get_id()}&action=edit" ) ), esc_attr__( 'Edit with WordPress', 'learnpress' ), PostModel::STATUS_TRASH === $status ? ' style="display:none"' : '' ) : '', 'header_left_end' => '
', 'header_actions' => '
', 'preview_btn' => $courseModel && $courseModel->get_status() !== PostModel::STATUS_TRASH ? sprintf( '%2$s', esc_url( get_permalink( $courseModel->get_id() ) ), esc_html__( 'Preview', 'learnpress' ) ) : '', 'dropdown_wrap' => '
', 'update_btn' => sprintf( '
%6$s
', esc_attr( $main_action_status ), esc_attr__( 'Update', 'learnpress' ), esc_attr__( 'Publish', 'learnpress' ), esc_attr__( 'Save Draft', 'learnpress' ), esc_attr__( 'Submit for Review', 'learnpress' ), esc_html__( 'Update', 'learnpress' ) ), 'dropdown_wrap_end' => '
', 'expanded_actions' => $courseModel ? sprintf( '
%5$s
%6$s
', esc_attr__( 'More actions', 'learnpress' ), $more_actions_icon, esc_attr__( 'Are you sure?', 'learnpress' ), esc_attr__( 'Are you sure you want to duplicate this course?', 'learnpress' ), esc_html__( 'Duplicate', 'learnpress' ), esc_html__( 'Move to Trash', 'learnpress' ) ) : '', 'header_actions_end' => '
', 'header_end' => '
', ]; return Template::combine_components( $section ); } /** * Render tabs * * @param array $data * * @return string */ public function html_tabs( array $data = [] ): string { $section_tab = [ 'tabs_wrap' => '
', 'tabs' => '', 'tabs_end' => '
', ]; $section_content = [ 'wrap' => '
', 'content' => '', 'wrap-end' => '
', ]; $tabs = apply_filters( 'learn-press/course-builder/course/edit/tabs', [ 'overview' => [ 'title' => esc_html__( 'Overview', 'learnpress' ), 'html' => $this->html_tab_overview( $data ), ], 'curriculum' => [ 'title' => esc_html__( 'Curriculum', 'learnpress' ), 'html' => $this->html_tab_curriculum( $data ), ], 'settings' => [ 'title' => esc_html__( 'Settings', 'learnpress' ), 'html' => $this->html_tab_settings( $data ), ], ], $data ); $tab_active = array_key_first( $tabs ); if ( isset( $_GET['tab'] ) ) { $tab_active = $_GET['tab']; } foreach ( $tabs as $key => $tab ) { $is_active = $key === $tab_active; $section_tab['tabs'] .= sprintf( '%s', $is_active ? 'is-active' : '', esc_attr( $tab['slug'] ?? $key ), esc_html( $tab['title'] ?? '' ) ); /** * @uses html_tab_overview * @uses html_tab_curriculum * @uses html_tab_settings */ $section_content['content'] .= sprintf( '
%s
', $is_active ? '' : 'lp-hidden', esc_attr( $key ), $tab['html'] ); } $section = [ 'tabs' => Template::combine_components( $section_tab ), 'contents' => Template::combine_components( $section_content ), ]; return Template::combine_components( $section ); } public function html_tab_overview( array $data = [] ) { wp_enqueue_script( 'lp-course-builder' ); $courseModel = $data['courseModel'] ?? null; $course_id = 0; if ( $courseModel instanceof CourseModel ) { $course_id = $courseModel->get_id(); } $html_edit_title = $this->edit_title( $courseModel ); $html_edit_permalink = $this->edit_permalink( $courseModel ); $html_edit_features = $this->edit_featured_image( $courseModel ); $html_edit_publish = $this->edit_publish( $courseModel ); $html_edit_desc = $this->edit_desc( $courseModel ); $html_edit_cat = $this->edit_categories( $courseModel ); $html_edit_tags = $this->edit_tags( $courseModel ); $section = [ 'wrapper' => sprintf( '
', $course_id ), 'content_wrapper' => '
', // Left column 'left_column' => '
', 'edit_title' => $html_edit_title, 'edit_permalink' => $html_edit_permalink, 'edit_publish' => $html_edit_publish, 'edit_features' => $html_edit_features, 'left_column_end' => '
', // Right column 'right_column' => '
', 'edit_desc' => $html_edit_desc, 'edit_term_category' => '
', 'edit_cat' => $html_edit_cat, 'edit_term' => $html_edit_tags, 'edit_term_category_end' => '
', 'right_column_end' => '
', 'content_wrapper_end' => '
', 'ai_templates' => $this->html_overview_ai_templates(), 'wrapper_end' => '
', ]; return Template::combine_components( $section ); } /** * HTML Curriculum * * @param array $data * * @return string */ public function html_tab_curriculum( array $data = [] ): string { wp_enqueue_script( 'lp-cb-edit-curriculum' ); wp_enqueue_style( 'lp-cb-edit-curriculum' ); wp_enqueue_script( 'lp-cb-admin-learnpress' ); $course_id = CourseBuilder::get_item_id(); if ( $course_id === CourseBuilder::POST_NEW ) { return Template::print_message( __( 'Please save Course before add Section', 'learnpress' ), 'info', false ); } $courseModel = $data['courseModel'] ?? null; if ( ! $courseModel instanceof CourseModel ) { return Template::print_message( __( 'Course is invalid!', 'learnpress' ), 'error', false ); } return $this->html_curriculum( $courseModel ); } public function html_tab_settings( array $data = [] ) { wp_enqueue_script( 'lp-cb-edit-curriculum' ); wp_enqueue_script( 'lp-tom-select' ); wp_enqueue_style( 'lp-cb-edit-curriculum' ); wp_enqueue_script( 'lp-cb-learnpress' ); $course_id = CourseBuilder::get_item_id(); if ( $course_id === CourseBuilder::POST_NEW ) { return Template::print_message( __( 'Please save Course before setting course', 'learnpress' ), 'info', false ); } $courseModel = $data['courseModel'] ?? null; if ( ! $courseModel instanceof CourseModel ) { return Template::print_message( __( 'Course is invalid!', 'learnpress' ), 'error', false ); } if ( ! class_exists( 'LP_Meta_Box_Course' ) ) { require_once LP_PLUGIN_PATH . 'inc/admin/views/meta-boxes/course/settings.php'; } add_filter( 'learnpress/course/metabox/tabs', [ $this, 'filter_course_builder_settings_tabs' ], 999 ); add_filter( 'learn-press/course/meta-box/assessment/final-quiz/edit-link', [ $this, 'filter_course_builder_assessment_final_quiz_edit_link', ], 10, 2 ); $metabox = \LP_Meta_Box_Course::instance(); ob_start(); $metabox->output( $courseModel ); $settings = ob_get_clean(); remove_filter( 'learnpress/course/metabox/tabs', [ $this, 'filter_course_builder_settings_tabs' ], 999 ); remove_filter( 'learn-press/course/meta-box/assessment/final-quiz/edit-link', [ $this, 'filter_course_builder_assessment_final_quiz_edit_link', ], 10 ); $output = [ 'wrapper' => sprintf( '
', $course_id ), 'form_setting' => '
', 'settings' => $settings, 'form_setting_end' => '
', 'wrapper_end' => '
', ]; return Template::combine_components( $output ); } /** * Allow callback for AJAX. * @use self::render_edit_course_curriculum * @use self::render_html * * @param array $callbacks * * @return array */ public function allow_callback( array $callbacks ): array { $callbacks[] = AdminEditCurriculumTemplate::class . ':render_edit_course_curriculum'; return $callbacks; } /** * Render Course Builder course title field. * * Uses the raw post title for existing courses so the edit field does not receive * display filters. New courses start with an empty value and still render the * optional AI button when AI settings are enabled. * * @param CourseModel|false|null $courseModel Course model being edited, or empty when creating a new course. * * @return string HTML markup for the title input, character count, and optional AI button. * @since 4.3.0 * @version 1.0.0 */ public function edit_title( $courseModel ) { $title = ''; if ( ! empty( $courseModel ) ) { $post_id = absint( $courseModel->get_id() ); $title = $post_id ? (string) get_post_field( 'post_title', $post_id, 'raw' ) : ''; } $char_count = mb_strlen( wp_strip_all_tags( $title ) ); $ai_button = $this->html_overview_ai_button( '#lp-tmpl-edit-title-ai' ); $edit = [ 'wrapper' => '
', 'label_wrap' => '
', 'label' => sprintf( '', __( 'Course Title', 'learnpress' ) ), 'char_count' => sprintf( '%s', sprintf( __( '%d characters', 'learnpress' ), $char_count ) ), 'ai_button' => $ai_button, 'label_wrap_end' => '
', 'input' => sprintf( '', esc_attr( $title ), esc_attr__( 'example', 'learnpress' ) ), 'wrapper_end' => '
', ]; return Template::combine_components( $edit ); } /** * Render Course Builder permalink editor. * * Permalink editing is hidden for unsaved courses. Existing courses use * get_sample_permalink() when available to display a publish-style URL for slug * editing, while the preview link keeps the current permalink as its href. * * @param CourseModel|false|null $courseModel Course model being edited, or empty when creating a new course. * * @return string HTML markup for the permalink display/editor, or an empty string for new courses. * @since 4.3.0 * @version 1.0.0 */ public function edit_permalink( $courseModel ) { $post_id = ! empty( $courseModel ) ? $courseModel->get_id() : ''; $post_name = ''; // Hide permalink for new courses if ( empty( $post_id ) || $post_id === CourseBuilder::POST_NEW ) { return ''; } if ( $post_id ) { $post = get_post( $post_id ); $post_name = $post ? $post->post_name : ''; } // Get base URL for courses $courses_page_id = learn_press_get_page_id( 'courses' ); $base_url = ''; if ( $courses_page_id ) { $base_url = trailingslashit( get_permalink( $courses_page_id ) ); } else { $base_url = trailingslashit( home_url() ) . 'courses/'; } $full_url = urldecode( (string) get_permalink( $post_id ) ); $display_data = $this->get_course_display_permalink_data( (int) $post_id, (string) $post_name ); $display_url = (string) ( $display_data['url'] ?? '' ); $editor_slug = (string) ( $display_data['slug'] ?? '' ); if ( empty( $display_url ) ) { $display_url = $full_url; } if ( empty( $full_url ) ) { $full_url = $display_url; } if ( empty( $editor_slug ) ) { $editor_slug = (string) $post_name; } if ( empty( $editor_slug ) && ! empty( $display_url ) ) { $display_path = parse_url( $display_url, PHP_URL_PATH ); if ( is_string( $display_path ) && '' !== $display_path ) { $editor_slug = basename( untrailingslashit( $display_path ) ); } } // Use publish-style permalink as editable base, but keep href as current permalink. if ( ! empty( $editor_slug ) && false === strpos( $display_url, '?p=' ) && false === strpos( $display_url, '&p=' ) && false === strpos( $display_url, '?lp_course=' ) && false === strpos( $display_url, '&lp_course=' ) ) { $base_url = trailingslashit( preg_replace( '/' . preg_quote( $editor_slug, '/' ) . '\/?$/', '', $display_url ) ); } $state_a = sprintf( '%s ', __( 'Permalink', 'learnpress' ), esc_url( $full_url ), esc_html( $display_url ), __( 'Edit', 'learnpress' ) ); $state_b = sprintf( '', esc_html( $base_url ), esc_attr( $editor_slug ), esc_attr__( 'your-slug', 'learnpress' ), __( 'OK', 'learnpress' ), __( 'Cancel', 'learnpress' ) ); $hidden_base = sprintf( '', esc_attr( $base_url ) ); $edit = [ 'wrapper' => '', ]; return Template::combine_components( $edit ); } /** * Get permalink display URL in publish-style format for editing slug. * * @param int $post_id * @param string $post_name * * @return array */ private function get_course_display_permalink_data( int $post_id, string $post_name = '' ): array { $display_url = urldecode( (string) get_permalink( $post_id ) ); $sample_slug = $post_name; if ( ! function_exists( 'get_sample_permalink' ) ) { require_once ABSPATH . 'wp-admin/includes/post.php'; } if ( function_exists( 'get_sample_permalink' ) ) { $sample_permalink = get_sample_permalink( $post_id ); if ( is_array( $sample_permalink ) && ! empty( $sample_permalink[0] ) ) { $sample_slug = ! empty( $sample_permalink[1] ) ? (string) $sample_permalink[1] : $sample_slug; $display_url = str_replace( [ '%postname%', '%pagename%' ], $sample_slug, (string) $sample_permalink[0] ); } } $post = get_post( $post_id ); if ( $post instanceof \WP_Post ) { $display_url = \LP_Helper::handle_lp_permalink_structure( $display_url, $post ); } return [ 'url' => urldecode( $display_url ), 'slug' => urldecode( (string) $sample_slug ), ]; } /** * Render Course Builder description editor. * * Uses the current course post content when editing an existing CourseModel. * For new courses, the editor starts with an empty value. AI button rendering * is delegated to html_overview_ai_button(). * * @param CourseModel|false|null $courseModel Course model being edited, or empty when creating a new course. * * @return string HTML markup for the course description editor. */ public function edit_desc( $courseModel ) { $desc = $courseModel ? $courseModel->post_content : ''; $editor_id = 'course_description_editor'; $ai_button = $this->html_overview_ai_button( '#lp-tmpl-edit-description-ai' ); $editor_settings = array( 'textarea_name' => 'course_description', 'textarea_rows' => 10, 'teeny' => false, 'media_buttons' => true, 'tinymce' => array( 'content_style' => "body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif; font-size: 14px; line-height: 1.6; color: #1e1e1e; }", 'toolbar1' => 'formatselect,bold,italic,underline,bullist,numlist,blockquote,alignleft,aligncenter,alignright,link,unlink,spellchecker,wp_adv', 'toolbar2' => 'strikethrough,hr,forecolor,pastetext,removeformat,charmap,outdent,indent,undo,redo,wp_help', ), 'quicktags' => true, ); $edit = [ 'wrapper' => '
', 'label_wrap' => '
', 'label' => sprintf( '', __( 'Description', 'learnpress' ) ), 'ai_button' => $ai_button, 'label_wrap_end' => '
', 'edit' => AdminTemplate::editor_tinymce( $desc, $editor_id, $editor_settings ), 'wrapper_end' => '
', ]; return Template::combine_components( $edit ); } /** * Check if Overview AI button and templates should be shown. * * @return bool */ protected function can_show_overview_ai_button(): bool { return \LP_Settings::get_option( 'enable_open_ai', 'no' ) === 'yes' && ! empty( \LP_Settings::get_option( 'open_ai_secret_key', '' ) ); } /** * Render icon-only AI button in Course Builder overview. * * @param string $template_id * * @return string */ protected function html_overview_ai_button( string $template_id ): string { if ( ! $this->can_show_overview_ai_button() ) { return ''; } $button_label = esc_html__( 'Generate with AI', 'learnpress' ); return sprintf( '', esc_attr( $template_id ), esc_attr( $button_label ) ); } /** * Render AI popup templates for Course Builder overview edit page. * * @return string */ protected function html_overview_ai_templates(): string { if ( ! $this->can_show_overview_ai_button() ) { return ''; } try { return AdminEditWithAITemplate::instance()->render_for_frontend( [ 'title', 'description', 'image' ] ); } catch ( Throwable $e ) { error_log( __METHOD__ . ': ' . $e->getMessage() ); } return ''; } /** * Render AI popup template for curriculum edit in Course Builder. * * @return string */ protected function html_curriculum_ai_templates(): string { if ( ! $this->can_show_overview_ai_button() ) { return ''; } try { return AdminEditCourseCurriculumWithAITemplate::instance()->render_for_frontend(); } catch ( Throwable $e ) { error_log( __METHOD__ . ': ' . $e->getMessage() ); } return ''; } /** * Render Course Builder course category selector. * * Reuses the WordPress post_categories_meta_box() output for course_category, * then adds Course Builder search and "add new category" controls. During render, * checked_ontop is forced off so the checklist keeps taxonomy order. * * @param CourseModel|false|null $courseModel Course model being edited, or empty when creating a new course. * * @return string HTML markup for the course category checklist, search toolbar, and add-new form. * @since 4.3.0 * @version 1.0.0 */ public function edit_categories( $courseModel ) { if ( ! function_exists( 'post_categories_meta_box' ) ) { require_once ABSPATH . 'wp-admin/includes/meta-boxes.php'; } if ( ! function_exists( 'wp_popular_terms_checklist' ) ) { require_once ABSPATH . 'wp-admin/includes/template.php'; } $post_id = ! empty( $courseModel ) ? $courseModel->get_id() : get_the_ID(); $post = get_post( $post_id ); $force_checked_ontop_false = function ( $args ) { if ( isset( $args['taxonomy'] ) && 'course_category' === $args['taxonomy'] ) { $args['checked_ontop'] = false; } return $args; }; ob_start(); add_filter( 'wp_terms_checklist_args', $force_checked_ontop_false ); if ( function_exists( 'post_categories_meta_box' ) ) { \post_categories_meta_box( $post, array( 'id' => 'course_categorydiv', 'title' => __( 'Categories', 'learnpress' ), 'callback' => 'post_categories_meta_box', 'args' => array( 'taxonomy' => 'course_category', 'checked_ontop' => false, ), ) ); } remove_filter( 'wp_terms_checklist_args', $force_checked_ontop_false ); $html_meta_box = ob_get_clean(); // Build add new category form (between header and content) $parent_terms = get_terms( [ 'taxonomy' => 'course_category', 'hide_empty' => false, ] ); $parent_options = sprintf( '', __( 'Parent Category', 'learnpress' ) ); if ( ! empty( $parent_terms ) && ! is_wp_error( $parent_terms ) ) { foreach ( $parent_terms as $term ) { $parent_options .= sprintf( '', $term->term_id, esc_html( $term->name ) ); } } $form_add_category = sprintf( '', esc_attr__( 'Enter Category Name', 'learnpress' ), $parent_options, esc_html__( 'Cancel', 'learnpress' ), esc_html__( 'Add', 'learnpress' ), ); $edit = [ 'wrapper' => '
', 'header' => '
', 'label_wrap' => '
', 'label' => sprintf( '', __( 'Categories', 'learnpress' ) ), 'btn_search' => sprintf( '', esc_attr__( 'Search categories', 'learnpress' ) ), 'label_wrap_end' => '
', 'btn_add_new' => sprintf( '', __( 'Add New', 'learnpress' ) ), 'header_end' => '
', 'form_add_category' => $form_add_category, 'search' => sprintf( '
', esc_html__( 'Search categories', 'learnpress' ), esc_attr__( 'Search categories', 'learnpress' ) ), 'content' => $html_meta_box, 'wrapper_end' => '
', ]; return Template::combine_components( $edit ); } /** * Render Course Builder course tag selector. * * Builds tag chips from all course tags, placing selected tags before available * tags so the current course state is visible first. The returned markup includes * search, empty-state text, and add-new controls consumed by the builder runtime. * * @param CourseModel|false|null $courseModel Course model being edited, or empty when creating a new course. * * @return string HTML markup for the selected/available tag chips, search toolbar, and add-new form. * @since 4.3.0 * @version 1.0.0 */ public function edit_tags( $courseModel ) { $course_terms = ! empty( $courseModel ) ? $courseModel->get_tags() : []; $tags = get_terms( [ 'taxonomy' => LP_COURSE_TAXONOMY_TAG, 'hide_empty' => false, ] ); $selected_tag_ids = array_map( function ( $term ) { return (int) $term->term_id; }, $course_terms ); $html_selected_chips = ''; $html_available_chips = ''; if ( ! empty( $tags ) && ! is_wp_error( $tags ) ) { foreach ( $tags as $tag ) { $tag_id = $tag->term_id; $tag_name = $tag->name; $tag_count = $tag->count; $is_checked = in_array( (int) $tag_id, $selected_tag_ids, true ); $html_chip = $this->input_checkbox_tag_item( $tag_id, $tag_name, $is_checked, $tag_count ); if ( $is_checked ) { $html_selected_chips .= $html_chip; } else { $html_available_chips .= $html_chip; } } } $html_chips = $html_selected_chips . $html_available_chips; $count_all = substr_count( $html_chips, 'class="cb-tag-chip"' ); $empty_default = esc_html__( 'No tags found.', 'learnpress' ); $empty_search = esc_html__( 'No matching tags.', 'learnpress' ); $toolbar = sprintf( '
', esc_html__( 'Search tags', 'learnpress' ), esc_attr__( 'Search tags', 'learnpress' ) ); $edit = [ 'wrapper' => '
', 'header' => '
', 'label_wrap' => '
', 'label' => sprintf( '', __( 'Tags', 'learnpress' ) ), 'btn_search' => sprintf( '', esc_attr__( 'Search tags', 'learnpress' ) ), 'label_wrap_end' => '
', 'btn_add_new' => sprintf( '', __( 'Add New', 'learnpress' ) ), 'header_end' => '
', 'form_add_tag_wrapper' => '', 'toolbar' => $toolbar, 'wrapper_checkbox' => '
', 'checkbox' => $html_chips, 'wrapper_checkbox_end' => '
', 'empty' => sprintf( '

%2$s

', $count_all > 0 ? ' lp-hidden' : '', esc_attr( $empty_default ), esc_attr( $empty_search ) ), 'wrapper_end' => '
', ]; return Template::combine_components( $edit ); } public function input_checkbox_tag_item( $term_id, $term_name, $is_checked, $count = 0 ) { if ( 0 === $count ) { $tag_obj = get_term( $term_id, LP_COURSE_TAXONOMY_TAG ); if ( $tag_obj && ! is_wp_error( $tag_obj ) ) { $count = $tag_obj->count; } } $tag_name_search = wp_strip_all_tags( $term_name ); if ( function_exists( 'mb_strtolower' ) ) { $tag_name_search = mb_strtolower( $tag_name_search ); } else { $tag_name_search = strtolower( $tag_name_search ); } $html = sprintf( '
', esc_attr( $tag_name_search ), (int) $term_id ); $html .= sprintf( '', $term_id, $term_id, checked( $is_checked, true, false ) ); $html .= sprintf( '', $term_id, esc_html( $term_name ), $count ); $html .= '
'; return $html; } /** * Render Course Builder featured image uploader. * * Existing courses render the current thumbnail preview with replace/remove * controls. Courses without a thumbnail render the upload dropzone and hidden * course_thumbnail_id field used by the builder save flow. * * @param CourseModel|false|null $courseModel Course model being edited, or empty when creating a new course. * * @return string HTML markup for the featured image dropzone, thumbnail hidden field, and optional AI button. * @since 4.3.0 * @version 1.0.0 */ public function edit_featured_image( $courseModel ) { $post_id = ! empty( $courseModel ) ? $courseModel->get_id() : ''; $ai_button = $this->html_overview_ai_button( '#lp-tmpl-edit-image-ai' ); $thumbnail_id = ! empty( $post_id ) ? get_post_thumbnail_id( $post_id ) : ''; $thumbnail_url = ''; $thumbnail_alt = ''; if ( $thumbnail_id ) { $thumbnail_url = wp_get_attachment_image_url( $thumbnail_id, 'medium' ); $thumbnail_alt = get_post_meta( $thumbnail_id, '_wp_attachment_image_alt', true ); } $has_image = ! empty( $thumbnail_url ); $featured_image_html = ''; // End container $edit = [ 'wrapper' => '', ]; return Template::combine_components( $edit ); } /** * Render publish panel (status, visibility, publish date, danger zone) for Course Builder overview. * * @param CourseModel|false|string|null $courseModel * * @return string */ public function edit_publish( $courseModel ): string { $post_id = ! empty( $courseModel ) ? absint( $courseModel->get_id() ) : 0; $post = $post_id ? get_post( $post_id ) : null; $current_status = 'draft'; if ( $post && ! empty( $post->post_status ) ) { $current_status = sanitize_key( $post->post_status ); } $status_for_select = in_array( $current_status, [ 'publish', 'draft', 'pending', 'future' ], true ) ? $current_status : 'publish'; $current_password = $post ? (string) ( $post->post_password ?? '' ) : ''; $visibility = 'private' === $current_status ? 'private' : ( ! empty( $current_password ) ? 'password' : 'public' ); $published_on = ''; if ( $post && ! empty( $post->post_date ) && '0000-00-00 00:00:00' !== $post->post_date ) { $published_on = wp_date( 'Y-m-d\TH:i', strtotime( $post->post_date ), wp_timezone() ); } $has_future_publish_date = false; if ( $post && ! empty( $post->post_date ) && '0000-00-00 00:00:00' !== $post->post_date ) { $has_future_publish_date = strtotime( $post->post_date ) > current_time( 'timestamp' ); } $is_scheduled_status = 'future' === $status_for_select || ( in_array( $status_for_select, [ 'draft', 'pending' ], true ) && $has_future_publish_date ); $primary_status_value = $is_scheduled_status ? 'future' : 'publish'; $primary_status_label = $is_scheduled_status ? __( 'Scheduled', 'learnpress' ) : __( 'Published', 'learnpress' ); $selected_status_for_ui = in_array( $status_for_select, [ 'draft', 'pending' ], true ) ? $status_for_select : $primary_status_value; $status_options = [ $primary_status_value => $primary_status_label, 'draft' => __( 'Draft', 'learnpress' ), 'pending' => __( 'Pending Review', 'learnpress' ), ]; $publish_date_label = $has_future_publish_date ? __( 'Scheduled for', 'learnpress' ) : __( 'Published on', 'learnpress' ); $status_options_html = ''; foreach ( $status_options as $value => $label ) { $status_options_html .= sprintf( '', esc_attr( $value ), selected( $selected_status_for_ui, $value, false ), esc_html( $label ) ); } $visibility_options_html = sprintf( '', selected( $visibility, 'public', false ), esc_html__( 'Public', 'learnpress' ), selected( $visibility, 'private', false ), esc_html__( 'Private', 'learnpress' ), selected( $visibility, 'password', false ), esc_html__( 'Password protected', 'learnpress' ) ); $edit = [ 'wrapper' => '
', 'title' => sprintf( '

%s

', esc_html__( 'Publish', 'learnpress' ) ), 'status_row' => sprintf( '
', esc_html__( 'Status', 'learnpress' ), $status_options_html, esc_attr__( 'Published', 'learnpress' ), esc_attr__( 'Scheduled', 'learnpress' ), esc_attr( $primary_status_value ) ), 'visibility_row' => sprintf( '
', esc_html__( 'Visibility', 'learnpress' ), $visibility_options_html ), 'password_row' => sprintf( '
', 'password' === $visibility ? '' : 'lp-hidden', esc_html__( 'Password', 'learnpress' ), esc_attr( $current_password ) ), 'published_on_row' => sprintf( '
', esc_html( $publish_date_label ), esc_attr( $published_on ) ), 'wrapper_end' => '
', ]; return Template::combine_components( $edit ); } /** * Render Course Builder curriculum editor. * * Delegates the main curriculum layout to AdminEditCurriculumTemplate with the * is_course_builder context flag, then appends popup templates and optional AI * curriculum templates needed by the builder UI. * * @param CourseModel $courseModel Course model used to render curriculum sections and popup template IDs. * * @return string HTML markup for the curriculum editor plus Course Builder popup and AI templates. * @since 4.3.0 * @version 1.0.0 */ protected function html_curriculum( CourseModel $courseModel ): string { ob_start(); AdminEditCurriculumTemplate::instance()->edit_course_curriculum_layout( $courseModel, [ 'is_course_builder' => true ] ); $html_curriculum = ob_get_clean(); return $html_curriculum . $this->html_curriculum_popup_templates( $courseModel ) . $this->html_curriculum_ai_templates(); } protected function html_curriculum_popup_templates( CourseModel $courseModel ): string { $popup_templates = apply_filters( 'learn-press/course-builder/courses/curriculum/popup-templates', [ 'lesson' => sprintf( '', esc_attr( sprintf( 'lp-tmpl-builder-popup-curriculum-lesson-course-%d', $courseModel->get_id() ) ), TemplateAJAX::load_content_via_ajax( [ 'id_url' => 'builder-popup-lesson-curriculum', 'lesson_id' => 0, 'html_no_load_ajax_first' => sprintf( '
%s
', esc_html__( 'Loading...', 'learnpress' ) ), ], [ 'class' => BuilderPopupTemplate::class, 'method' => 'render_lesson_popup', ] ) ), 'quiz' => sprintf( '', esc_attr( sprintf( 'lp-tmpl-builder-popup-curriculum-quiz-course-%d', $courseModel->get_id() ) ), TemplateAJAX::load_content_via_ajax( [ 'id_url' => 'builder-popup-quiz-curriculum', 'quiz_id' => 0, 'html_no_load_ajax_first' => sprintf( '
%s
', esc_html__( 'Loading...', 'learnpress' ) ), ], [ 'class' => BuilderPopupTemplate::class, 'method' => 'render_quiz_popup', ] ) ), ], $courseModel ); return Template::combine_components( $popup_templates ); } /** * Add edit popup button for lesson and quiz items in Course Builder curriculum. * Replace the default edit button with popup button for lesson and quiz items. * * @param array $section_action Array of action buttons. * @param object|null $item Item data. * @param PostModel|null $itemModel Item model. * @param CourseModel $courseModel . * @param array $context_data Context data passed from AJAX. * * @return array * @since 4.3.0 * @version 1.0.2 * */ public function add_edit_popup_button( array $section_action, $item, $itemModel, $courseModel, $context_data = [] ): array { // Check if we are in Course Builder context via the flag passed in AJAX args $is_course_builder = ! empty( $context_data['is_course_builder'] ); if ( ! $is_course_builder ) { return $section_action; } $item_id = $item->item_id ?? 0; $item_type = $item->item_type ?? ''; $popup_data_attr = ''; $popup_type = ''; if ( $item_type === LP_LESSON_CPT ) { $popup_data_attr = sprintf( 'data-popup-lesson="%s"', $item_id ); $popup_type = 'lesson'; } elseif ( $item_type === LP_QUIZ_CPT ) { $popup_data_attr = sprintf( 'data-popup-quiz="%s"', $item_id ); $popup_type = 'quiz'; } $popup_type = sanitize_key( (string) apply_filters( 'learn-press/course-builder/courses/curriculum/popup-item-type', $popup_type, $item_type, $item, $itemModel, $courseModel, $context_data ) ); if ( empty( $popup_type ) ) { return $section_action; } $popup_template_id = sprintf( 'lp-tmpl-builder-popup-curriculum-%1$s-course-%2$d', $popup_type, $courseModel->get_id() ); // Replace edit button with popup button - use lp-icon-edit-square instead of lp-icon-expand $section_action['edit'] = sprintf( '
  • ', __( 'Edit in popup', 'learnpress' ), $courseModel->get_id(), esc_attr( $popup_template_id ), esc_attr( $popup_type ), $item_id, $popup_data_attr ); return $section_action; } /** * Keep supported course settings tabs in Course Builder. * * @param array $tabs * * @return array */ public function filter_course_builder_settings_tabs( array $tabs ): array { $allowed_tabs = [ 'general', 'offline', 'price', 'extra', 'assessment', 'author', 'material' ]; foreach ( array_keys( $tabs ) as $tab_key ) { if ( ! in_array( $tab_key, $allowed_tabs, true ) ) { unset( $tabs[ $tab_key ] ); } } return apply_filters( 'learn-press/course-builder/edit-course/settings/tabs', $tabs ); } /** * Convert final quiz edit link to Course Builder quiz settings URL. * * @param string $url * @param int $final_quiz_id * * @return string */ public function filter_course_builder_assessment_final_quiz_edit_link( string $url, int $final_quiz_id ): string { $final_quiz_id = absint( $final_quiz_id ); if ( ! $final_quiz_id ) { return $url; } return CourseBuilder::get_tab_link( 'quizzes', $final_quiz_id, 'settings' ) . '#_lp_passing_grade'; } }