0, 'id' => 0, ], $attributes ); $course_id = absint( $atts['course_id'] ? $atts['course_id'] : $atts['id'] ); if ( ! $course_id ) { $course_id = (int) get_the_ID(); } // get_the_current_course_id() ignores an empty course name and would // return an arbitrary course, so only ask when a course is being viewed. if ( 'academy_courses' !== get_post_type( $course_id ) && get_query_var( 'course_name' ) ) { $course_id = (int) \Academy\Helper::get_the_current_course_id(); } if ( ! $course_id || 'academy_courses' !== get_post_type( $course_id ) ) { return 0; } // A course_id attribute can name any course, so don't expose drafts, // private or password-protected courses to people who can't read them. if ( 'publish' !== get_post_status( $course_id ) && ! current_user_can( 'read_post', $course_id ) ) { return 0; } if ( post_password_required( $course_id ) ) { return 0; } // The page may not mention the shortcode in its content (page builder // widget, template, do_shortcode() call), so load the course page's // styles and scripts here; without them the tabs and lists are unstyled. if ( class_exists( '\Academy\Blocks' ) ) { \Academy\Blocks::use_course_assets(); } return $course_id; } /** * Shown only to people who can edit pages, so an empty shortcode on a * normal page explains itself instead of silently printing nothing. * * @return string */ public static function missing_course_notice() { if ( ! current_user_can( 'edit_pages' ) ) { return ''; } return '
' . esc_html__( 'Academy shortcode: add a course_id attribute, for example [shortcode course_id="123"], when using it outside a course page. (Only editors see this message.)', 'academy' ) . '
'; } /** * Run a renderer with the course as the current post, for templates that * read get_the_ID() / the global post. * * @param int $course_id Course id. * @param callable $render Renderer. * @return string */ public static function render_for_course( $course_id, $render ) { global $post; $previous = $post; $switch_to = ! $previous || (int) $previous->ID !== (int) $course_id; if ( $switch_to ) { $post = get_post( $course_id ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- restored below. setup_postdata( $post ); } $output = (string) call_user_func( $render ); if ( $switch_to ) { $post = $previous; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- restoring. if ( $previous ) { setup_postdata( $previous ); } } return $output; } public function single_course_additional_info( $attributes, $content = '' ) { $course_id = $this->resolve_course_id( $attributes ); if ( ! $course_id ) { return self::missing_course_notice(); } ob_start(); $benefits = \Academy\Helper::string_to_array( get_post_meta( $course_id, 'academy_course_benefits', true ) ); $audience = \Academy\Helper::string_to_array( get_post_meta( $course_id, 'academy_course_audience', true ) ); $requirements = \Academy\Helper::string_to_array( get_post_meta( $course_id, 'academy_course_requirements', true ) ); $materials = \Academy\Helper::string_to_array( get_post_meta( $course_id, 'academy_course_materials_included', true ) ); $tabs_nav = []; $tabs_content = []; if ( is_array( $benefits ) && count( $benefits ) > 0 ) { \Academy\Helper::get_template( 'single-course/benefits.php', apply_filters( 'academy/single_course_content_benefits_args', [ 'benefits' => $benefits, ] ) ); } if ( is_array( $audience ) && count( $audience ) > 0 ) { $tabs_nav['audience'] = esc_html__( 'Targeted Audience', 'academy' ); $tabs_content['audience'] = $audience; } if ( is_array( $requirements ) && count( $requirements ) > 0 ) { $tabs_nav['requirements'] = esc_html__( 'Requirements', 'academy' ); $tabs_content['requirements'] = $requirements; } if ( is_array( $materials ) && count( $materials ) > 0 ) { $tabs_nav['materials'] = esc_html__( 'Materials Included', 'academy' ); $tabs_content['materials'] = $materials; } \Academy\Helper::get_template( 'single-course/additional-info.php', apply_filters( 'academy/single_course_content_additional_info_args', [ 'tabs_nav' => $tabs_nav, 'tabs_content' => $tabs_content, ] ) ); $output = ob_get_clean(); return apply_filters( 'academy/templates/shortcode/single_course_additional_info', $output ); } public function single_course_description( $attributes, $content = '' ) { static $rendering = false; // The template runs post_content through the_content, which re-runs // do_shortcode(). If this shortcode sits on the very page whose // content it is rendering, that recurses into itself until the // memory limit is hit, so bail out on re-entry. if ( $rendering ) { return ''; } $course_id = $this->resolve_course_id( $attributes ); if ( ! $course_id ) { return self::missing_course_notice(); } $rendering = true; $output = $this->render_for_course( $course_id, function () { ob_start(); \Academy\Helper::get_template( 'single-course/description.php' ); return ob_get_clean(); } ); $rendering = false; return apply_filters( 'academy/templates/shortcode/single_course_description', $output ); } public function single_course_curriculums( $attributes, $content = '' ) { $course_id = $this->resolve_course_id( $attributes ); if ( ! $course_id ) { return self::missing_course_notice(); } ob_start(); $curriculums = \Academy\Helper::get_course_curriculum( $course_id, false ); $topics_first_item_open_status = (bool) \Academy\Helper::get_settings( 'is_opened_course_single_first_topic', true ); \Academy\Helper::get_template( 'single-course/curriculums.php', array( 'course_id' => $course_id, 'curriculums' => $curriculums, 'topics_first_item_open_status' => $topics_first_item_open_status, ) ); $output = ob_get_clean(); return apply_filters( 'academy/templates/shortcode/single_course_curriculums', $output ); } public function single_course_review_rating( $attributes, $content = '' ) { $course_id = $this->resolve_course_id( $attributes ); if ( ! $course_id || ! (bool) \Academy\Helper::get_settings( 'is_enabled_course_review', true ) || get_post_meta( $course_id, 'academy_is_disabled_course_review', true ) ) { return ''; } ob_start(); $rating = \Academy\Helper::get_course_rating( $course_id ); \Academy\Helper::get_template( 'single-course/feedback.php', array( 'rating' => $rating ) ); $output = ob_get_clean(); return apply_filters( 'academy/templates/shortcode/single_course_review_rating', $output ); } public function single_course_review_form( $attributes, $content = '' ) { global $current_user; $course_id = $this->resolve_course_id( $attributes ); if ( ! $course_id || ! (bool) \Academy\Helper::get_settings( 'is_enabled_course_review', true ) || get_post_meta( $course_id, 'academy_is_disabled_course_review', true ) ) { return ''; } ob_start(); $usercomment = get_comments(array( 'user_id' => $current_user->ID, 'post_id' => $course_id, )); $completed_percent = (float) \Academy\Helper::get_percentage_of_completed_topics_by_student_and_course_id( $current_user->ID, $course_id ); $required_percent = (float) \Academy\Helper::get_settings( 'minimum_course_completion_on_review', 0 ); if ( ! $usercomment && \Academy\Helper::is_enrolled( $course_id, $current_user->ID ) && ( $completed_percent >= $required_percent ) ) { \Academy\Helper::get_template( 'single-course/review-form.php' ); } $output = ob_get_clean(); return apply_filters( 'academy/templates/shortcode/single_course_review_form', $output ); } public function single_course_user_display_reviews( $attributes, $content = '' ) { $course_id = $this->resolve_course_id( $attributes ); if ( ! $course_id ) { return self::missing_course_notice(); } $completed_percent = (float) \Academy\Helper::get_percentage_of_completed_topics_by_student_and_course_id( get_current_user_id(), $course_id ); $required_percent = (float) \Academy\Helper::get_settings( 'minimum_course_completion_on_review', 0 ); if ( ! (bool) \Academy\Helper::get_settings( 'is_enabled_course_review', true ) || get_post_meta( $course_id, 'academy_is_disabled_course_review', true ) || ( $completed_percent < $required_percent && $completed_percent !== $required_percent ) ) { return ''; } ob_start(); ?>
comment_ID; ?>-
id="academy-review-">
comment_author_email,
apply_filters( 'academy/review_gravatar_size', '80' )
);
?>
comment_approved ) {
?>
user_id === $current_user_id ) {
?>
user_id === $current_user_id
) :
?>
' . esc_html__( 'No reviews yet. Be the first to leave one!', 'academy' ) . ''; }//end if $total_comments = get_comments( array( 'post_id' => $course_id, 'status' => 'approve', 'count' => true, ) ); $max_pages = ceil( (int) $total_comments / $comments_per_page ); the_comments_pagination( array( 'total' => $max_pages, 'current' => $paged, 'mid_size' => 1, 'prev_text' => sprintf( '%s', esc_html__( 'Prev', 'academy' ) ), 'next_text' => sprintf( '%s', esc_html__( 'Next', 'academy' ) ), ) ); if ( ! comments_open( $course_id ) ) { echo 'comment_author ); ?>
' . esc_html__( 'Reviews are closed.', 'academy' ) . '
'; } ?>