0 ) {
if ( ! CourseBuilderAccessPolicy::can_edit_item( $item_type, $item_id ) ) {
throw new Exception( __( "Sorry, you don't have permission to access this content", 'learnpress' ) );
}
return;
}
if ( ! CourseBuilderAccessPolicy::can_create_item_type( $item_type ) ) {
throw new Exception( __( "Sorry, you don't have permission to create this item", 'learnpress' ) );
}
}*/
/**
* Get popup wrapper HTML structure.
*/
public function get_popup_wrapper( string $type, int $post_id, string $title, string $status = '' ): array {
$status_html = '';
if ( ! empty( $status ) ) {
$status_html = sprintf( '%s', $type, esc_attr( $status ), esc_html( $status ) );
}
return [
'overlay' => '
',
'wrapper' => sprintf( '',
];
}
/**
* Render Lesson Popup
* @throws Exception
*/
public static function render_lesson_popup( array $args = [] ): stdClass {
$response = new stdClass();
$lesson_id = absint( $args['lesson_id'] ?? 0 );
//self::ensure_popup_access( LP_LESSON_CPT, $lesson_id );
$lesson_model = $lesson_id ? LessonPostModel::find( $lesson_id, true ) : null;
if ( $lesson_model instanceof LessonPostModel ) {
$lesson_model->check_capabilities_update_item_course();
} else {
$lessonPostModelNew = new LessonPostModel();
$lessonPostModelNew->check_capabilities_create_item_course();
}
$title = $lesson_model ? __( 'Edit Lesson', 'learnpress' ) : __( 'New Lesson', 'learnpress' );
$status = $lesson_model ? $lesson_model->post_status : '';
$instance = self::instance();
$content = $instance->build_lesson_content( $lesson_id, $lesson_model );
$html = array_merge(
$instance->get_popup_wrapper( 'lesson', $lesson_id, $title, $status ),
[ 'content' => $content ],
$instance->get_popup_footer( 'lesson', $lesson_id, $status )
);
$response->content = Template::combine_components( $html );
return $response;
}
/**
* Build lesson content
* @throws Exception
*/
private function build_lesson_content( int $lesson_id, $lesson_model ): string {
if ( $lesson_model && $lesson_model->post_status === PostModel::STATUS_TRASH ) {
$message = __(
'You cannot edit this item because it is in the Trash. Please restore it and try again.',
'learnpress'
);
return Template::print_message( $message, 'error', false );
}
$template = BuilderEditLessonTemplate::instance();
$lesson_context_id = $lesson_id > 0 ? $lesson_id : CourseBuilder::POST_NEW;
$lesson_data = [
'item_id' => $lesson_context_id,
'lessonModel' => $lesson_model ?? false,
];
$overview_html = $template->html_tab_overview( $lesson_data );
$settings_html = $template->html_tab_settings( $lesson_data );
$sections = [
'wrapper' => sprintf( '', $lesson_id ),
'tabs' => $this->build_tabs( 'lesson', [ 'overview', 'settings' ] ),
'tab_content_start' => '',
'wrapper_end' => '
',
];
return Template::combine_components( $sections );
}
/**
* Render Quiz Popup
* @throws Exception
*/
public static function render_quiz_popup( array $args = [] ): stdClass {
$response = new stdClass();
$quiz_id = absint( $args['quiz_id'] ?? 0 );
//self::ensure_popup_access( LP_QUIZ_CPT, $quiz_id );
$quiz_model = $quiz_id ? QuizPostModel::find( $quiz_id, true ) : null;
if ( $quiz_model instanceof QuizPostModel ) {
$quiz_model->check_capabilities_update_item_course();
} else {
$quizPostModelNew = new QuizPostModel();
$quizPostModelNew->check_capabilities_create_item_course();
}
$title = $quiz_model ? __( 'Edit Quiz', 'learnpress' ) : __( 'New Quiz', 'learnpress' );
$status = $quiz_model ? $quiz_model->post_status : '';
$instance = self::instance();
$content = $instance->build_quiz_content( $quiz_id, $quiz_model );
$html = array_merge(
$instance->get_popup_wrapper( 'quiz', $quiz_id, $title, $status ),
[ 'content' => $content ],
$instance->get_popup_footer( 'quiz', $quiz_id, $status )
);
$response->content = Template::combine_components( $html );
return $response;
}
/**
* Build quiz content
*/
private function build_quiz_content( int $quiz_id, $quiz_model = false ): string {
$template = BuilderEditQuizTemplate::instance();
$quiz_context_id = $quiz_id > 0 ? $quiz_id : CourseBuilder::POST_NEW;
$quiz_data = [
'item_id' => $quiz_context_id,
'quizModel' => $quiz_model,
];
$overview_html = $template->html_tab_overview( $quiz_data );
$question_html = $template->html_tab_question( $quiz_data );
$settings_html = $template->html_tab_settings( $quiz_data );
$sections = [
'wrapper' => sprintf( '', $quiz_id ),
'tabs' => $this->build_tabs( 'quiz', [ 'overview', 'questions', 'settings' ] ),
'tab_content_start' => '',
'wrapper_end' => '
',
];
return Template::combine_components( $sections );
}
/**
* Render Question Popup
* @throws Exception
*/
public static function render_question_popup( array $args = [] ): stdClass {
$response = new stdClass();
$question_id = absint( $args['question_id'] ?? 0 );
//self::ensure_popup_access( LP_QUESTION_CPT, $question_id );
$question_model = $question_id ? QuestionPostModel::find( $question_id, true ) : null;
if ( $question_model instanceof QuestionPostModel ) {
$question_model->check_capabilities_update_item_course();
} else {
$questionPostModelNew = new QuestionPostModel();
$questionPostModelNew->check_capabilities_create_item_course();
}
$title = $question_model ? __( 'Edit Question', 'learnpress' ) : __( 'New Question', 'learnpress' );
$status = $question_model ? $question_model->post_status : '';
$instance = self::instance();
$content = $instance->build_question_content( $question_id, $question_model );
$html = array_merge(
$instance->get_popup_wrapper( 'question', $question_id, $title, $status ),
[ 'content' => $content ],
$instance->get_popup_footer( 'question', $question_id, $status )
);
$response->content = Template::combine_components( $html );
return $response;
}
/**
* Build question content
*/
private function build_question_content( int $question_id, $question_model = false ): string {
$template = BuilderEditQuestionTemplate::instance();
$question_context_id = $question_id > 0 ? $question_id : CourseBuilder::POST_NEW;
$question_data = [
'item_id' => $question_context_id,
'questionModel' => $question_model,
];
$overview_html = $template->html_tab_overview( $question_data );
$settings_html = $template->html_tab_settings( $question_data );
$sections = [
'wrapper' => sprintf( '', $question_id ),
'tabs' => $this->build_tabs( 'question', [ 'overview', 'settings' ] ),
'tab_content_start' => '',
'wrapper_end' => '
',
];
return Template::combine_components( $sections );
}
/**
* Build tabs navigation.
*/
public function build_tabs( string $type, array $tabs ): string {
$tab_labels = apply_filters(
"learn-press/course-builder/popup/{$type}/tab-labels",
[
'overview' => __( 'Overview', 'learnpress' ),
'settings' => __( 'Settings', 'learnpress' ),
'questions' => __( 'Questions', 'learnpress' ),
'answers' => __( 'Answers', 'learnpress' ),
],
$type,
$tabs
);
$tabs_html = '';
return $tabs_html;
}
}