| @@ -1,230 +1,230 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace LearnPress\CourseBuilder; | |
| 4 | - | |
| 5 | -use LearnPress\Helpers\Config; | |
| 6 | -use LearnPress\Models\UserModel; | |
| 7 | -use LP_Settings; | |
| 8 | -use WP_Query; | |
| 9 | - | |
| 10 | -/** | |
| 11 | - * Course Builder class. | |
| 12 | - * | |
| 13 | - * @since 4.3.0 | |
| 14 | - * @version 1.0.0 | |
| 15 | - */ | |
| 16 | -class CourseBuilder { | |
| 17 | - /** | |
| 18 | - * Constant for new post identifier | |
| 19 | - */ | |
| 20 | - const POST_NEW = 'new'; | |
| 21 | - const QUERY_VAR_IS_COURSE_BUILDER = 'is_course_builder'; | |
| 22 | - const QUERY_VAR_ITEM_ID = 'lp_cb_item_id'; | |
| 23 | - const QUERY_VAR_MENU_SLUG = 'lp_cb_menu_slug'; | |
| 24 | - | |
| 25 | - /** | |
| 26 | - * Constructor | |
| 27 | - * | |
| 28 | - */ | |
| 29 | - protected function __construct() { | |
| 30 | - } | |
| 31 | - | |
| 32 | - /** | |
| 33 | - * Get menus structure default in course builder. | |
| 34 | - * | |
| 35 | - * @return array | |
| 36 | - * @since 4.3.6 | |
| 37 | - * @version 1.0.0 | |
| 38 | - */ | |
| 39 | - public static function get_menus_arr(): array { | |
| 40 | - return Config::instance()->get( 'menus', 'course-builder' ); | |
| 41 | - } | |
| 42 | - | |
| 43 | - /** | |
| 44 | - * Get the current menu slug for the Course Builder page. | |
| 45 | - * | |
| 46 | - * Retrieves the active menu slug from the WP_Query object based on the 'lp_cb_menu_slug' query variable. | |
| 47 | - * | |
| 48 | - * @return string The current menu slug. | |
| 49 | - * | |
| 50 | - * @since 4.3.6 | |
| 51 | - * @version 1.0.0 | |
| 52 | - */ | |
| 53 | - public static function get_menu_current(): string { | |
| 54 | - /** @var WP_Query $wp_query */ | |
| 55 | - global $wp_query; | |
| 56 | - return (string) $wp_query->get( 'lp_cb_menu_slug', 'dashboard' ); | |
| 57 | - } | |
| 58 | - | |
| 59 | - /** | |
| 60 | - * Get the current section being viewed in a course builder tab. | |
| 61 | - * @param string $current | |
| 62 | - * @param string $tab | |
| 63 | - * @return string | |
| 64 | - * | |
| 65 | - * @since 4.3.0 | |
| 66 | - * @version 1.0.0 | |
| 67 | - */ | |
| 68 | - public static function get_current_section( $current = '', $tab = '' ) { | |
| 69 | - global $wp; | |
| 70 | - | |
| 71 | - if ( empty( $_REQUEST[ self::QUERY_VAR_ITEM_ID ] ) && empty( $wp->query_vars[ self::QUERY_VAR_ITEM_ID ] ) ) { | |
| 72 | - return $current; | |
| 73 | - } | |
| 74 | - | |
| 75 | - if ( ! empty( $_REQUEST['section'] ) ) { | |
| 76 | - $current = sanitize_text_field( $_REQUEST['section'] ); | |
| 77 | - } elseif ( ! empty( $wp->query_vars['section'] ) ) { | |
| 78 | - $current = $wp->query_vars['section']; | |
| 79 | - } else { | |
| 80 | - if ( ! $tab ) { | |
| 81 | - $current_tab = self::get_menu_current(); | |
| 82 | - } else { | |
| 83 | - $current_tab = $tab; | |
| 84 | - } | |
| 85 | - $tab_data = self::get_data( $current_tab ); | |
| 86 | - | |
| 87 | - if ( ! empty( $tab_data['sections'] ) ) { | |
| 88 | - $sections = $tab_data['sections']; | |
| 89 | - $section = reset( $sections ); | |
| 90 | - if ( ! empty( $section['slug'] ) ) { | |
| 91 | - $current = $section['slug']; | |
| 92 | - } else { | |
| 93 | - $current = array_keys( $tab_data['sections'] ); | |
| 94 | - } | |
| 95 | - } | |
| 96 | - } | |
| 97 | - | |
| 98 | - return $current; | |
| 99 | - } | |
| 100 | - | |
| 101 | - /** | |
| 102 | - * Retrieves tabs data or a specific tab by key. | |
| 103 | - * | |
| 104 | - * @param string|bool | |
| 105 | - * @return array | |
| 106 | - * | |
| 107 | - * @since 4.3.0 | |
| 108 | - * @version 1.0.0 | |
| 109 | - */ | |
| 110 | - public static function get_data( $key = false ) { | |
| 111 | - $tabs = self::get_menus_arr(); | |
| 112 | - return false !== $key ? ( array_key_exists( $key, $tabs ) ? $tabs[ $key ] : [] ) : $tabs; | |
| 113 | - } | |
| 114 | - | |
| 115 | - /** | |
| 116 | - * Get link for course builder | |
| 117 | - * | |
| 118 | - * @param string $sub | |
| 119 | - * | |
| 120 | - * @return string | |
| 121 | - */ | |
| 122 | - public static function get_link_course_builder( string $sub = '' ): string { | |
| 123 | - $page = LP_Settings::get_option( 'course_builder', 'course-builder' ); | |
| 124 | - return home_url( "{$page}/{$sub}" ); | |
| 125 | - } | |
| 126 | - | |
| 127 | - /** | |
| 128 | - * Get link for add new an item. | |
| 129 | - * | |
| 130 | - * @param $type | |
| 131 | - * | |
| 132 | - * @return string | |
| 133 | - */ | |
| 134 | - public static function get_link_add_new( $type ): string { | |
| 135 | - return self::get_link_course_builder( "{$type}/create" ); | |
| 136 | - } | |
| 137 | - | |
| 138 | - /** | |
| 139 | - * Get tab link | |
| 140 | - * | |
| 141 | - * @param string|false $tab | |
| 142 | - * @param int|string|null $post_id | |
| 143 | - * @param string|false $section | |
| 144 | - * | |
| 145 | - * @return string | |
| 146 | - * | |
| 147 | - * @since 4.3.0 | |
| 148 | - * @version 1.0.0 | |
| 149 | - */ | |
| 150 | - public static function get_tab_link( $tab = false, $post_id = null, $section = false ): string { | |
| 151 | - $link = ''; | |
| 152 | - if ( ! $tab ) { | |
| 153 | - return $link; | |
| 154 | - } | |
| 155 | - | |
| 156 | - $link = self::get_link_course_builder(); | |
| 157 | - | |
| 158 | - if ( ! empty( $tab ) ) { | |
| 159 | - $link .= $tab . '/'; | |
| 160 | - } | |
| 161 | - | |
| 162 | - if ( ! empty( $post_id ) ) { | |
| 163 | - $link .= $post_id . '/'; | |
| 164 | - } | |
| 165 | - | |
| 166 | - if ( ! empty( $section ) ) { | |
| 167 | - $link .= $section . '/'; | |
| 168 | - } | |
| 169 | - | |
| 170 | - return $link; | |
| 171 | - } | |
| 172 | - | |
| 173 | - /** | |
| 174 | - * Get item id | |
| 175 | - * | |
| 176 | - * @return int|string | |
| 177 | - * | |
| 178 | - * @since 4.3.6 | |
| 179 | - * @version 1.0.0 | |
| 180 | - */ | |
| 181 | - public static function get_item_id() { | |
| 182 | - /** @var WP_Query $wp_query */ | |
| 183 | - global $wp_query; | |
| 184 | - | |
| 185 | - return $wp_query->get( self::QUERY_VAR_ITEM_ID ); | |
| 186 | - } | |
| 187 | - | |
| 188 | - /** | |
| 189 | - * Get permission to view course builder | |
| 190 | - * | |
| 191 | - * @return bool | |
| 192 | - */ | |
| 193 | - public static function can_view_course_builder(): bool { | |
| 194 | - $userModel = UserModel::find( get_current_user_id(), true ); | |
| 195 | - return $userModel && $userModel->is_instructor(); | |
| 196 | - } | |
| 197 | - | |
| 198 | - /** | |
| 199 | - * Get title page of course builder | |
| 200 | - * | |
| 201 | - * @return string | |
| 202 | - * @since 4.3.6 | |
| 203 | - * @version 1.0.0 | |
| 204 | - */ | |
| 205 | - public static function get_title_page(): string { | |
| 206 | - /** @var WP_Query $wp_query */ | |
| 207 | - global $wp_query; | |
| 208 | - $title = ''; | |
| 209 | - | |
| 210 | - $menu_current = self::get_menu_current(); | |
| 211 | - $menus = self::get_menus_arr(); | |
| 212 | - if ( isset( $menus[ $menu_current ] ) ) { | |
| 213 | - $title = $menus[ $menu_current ]['title'] ?? ''; | |
| 214 | - | |
| 215 | - $post_id = CourseBuilder::get_item_id(); | |
| 216 | - if ( ! empty( $post_id ) ) { | |
| 217 | - if ( $post_id === self::POST_NEW ) { | |
| 218 | - $title .= ' - ' . __( 'New', 'learnpress' ); | |
| 219 | - } else { | |
| 220 | - $post = get_post( $post_id ); | |
| 221 | - if ( $post ) { | |
| 222 | - $title = sprintf( __( 'Edit "%1$s" - %2$s', 'learnpress' ), $post->post_title, $title ); | |
| 223 | - } | |
| 224 | - } | |
| 225 | - } | |
| 226 | - } | |
| 227 | - | |
| 228 | - return $title; | |
| 229 | - } | |
| 230 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace LearnPress\CourseBuilder; | |
| 4 | + | |
| 5 | +use LearnPress\Helpers\Config; | |
| 6 | +use LearnPress\Models\UserModel; | |
| 7 | +use LP_Settings; | |
| 8 | +use WP_Query; | |
| 9 | + | |
| 10 | +/** | |
| 11 | + * Course Builder class. | |
| 12 | + * | |
| 13 | + * @since 4.3.0 | |
| 14 | + * @version 1.0.0 | |
| 15 | + */ | |
| 16 | +class CourseBuilder { | |
| 17 | + /** | |
| 18 | + * Constant for new post identifier | |
| 19 | + */ | |
| 20 | + const POST_NEW = 'new'; | |
| 21 | + const QUERY_VAR_IS_COURSE_BUILDER = 'is_course_builder'; | |
| 22 | + const QUERY_VAR_ITEM_ID = 'lp_cb_item_id'; | |
| 23 | + const QUERY_VAR_MENU_SLUG = 'lp_cb_menu_slug'; | |
| 24 | + | |
| 25 | + /** | |
| 26 | + * Constructor | |
| 27 | + * | |
| 28 | + */ | |
| 29 | + protected function __construct() { | |
| 30 | + } | |
| 31 | + | |
| 32 | + /** | |
| 33 | + * Get menus structure default in course builder. | |
| 34 | + * | |
| 35 | + * @return array | |
| 36 | + * @since 4.3.6 | |
| 37 | + * @version 1.0.0 | |
| 38 | + */ | |
| 39 | + public static function get_menus_arr(): array { | |
| 40 | + return Config::instance()->get( 'menus', 'course-builder' ); | |
| 41 | + } | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * Get the current menu slug for the Course Builder page. | |
| 45 | + * | |
| 46 | + * Retrieves the active menu slug from the WP_Query object based on the 'lp_cb_menu_slug' query variable. | |
| 47 | + * | |
| 48 | + * @return string The current menu slug. | |
| 49 | + * | |
| 50 | + * @since 4.3.6 | |
| 51 | + * @version 1.0.0 | |
| 52 | + */ | |
| 53 | + public static function get_menu_current(): string { | |
| 54 | + /** @var WP_Query $wp_query */ | |
| 55 | + global $wp_query; | |
| 56 | + return (string) $wp_query->get( 'lp_cb_menu_slug', 'dashboard' ); | |
| 57 | + } | |
| 58 | + | |
| 59 | + /** | |
| 60 | + * Get the current section being viewed in a course builder tab. | |
| 61 | + * @param string $current | |
| 62 | + * @param string $tab | |
| 63 | + * @return string | |
| 64 | + * | |
| 65 | + * @since 4.3.0 | |
| 66 | + * @version 1.0.0 | |
| 67 | + */ | |
| 68 | + public static function get_current_section( $current = '', $tab = '' ) { | |
| 69 | + global $wp; | |
| 70 | + | |
| 71 | + if ( empty( $_REQUEST[ self::QUERY_VAR_ITEM_ID ] ) && empty( $wp->query_vars[ self::QUERY_VAR_ITEM_ID ] ) ) { | |
| 72 | + return $current; | |
| 73 | + } | |
| 74 | + | |
| 75 | + if ( ! empty( $_REQUEST['section'] ) ) { | |
| 76 | + $current = sanitize_text_field( $_REQUEST['section'] ); | |
| 77 | + } elseif ( ! empty( $wp->query_vars['section'] ) ) { | |
| 78 | + $current = $wp->query_vars['section']; | |
| 79 | + } else { | |
| 80 | + if ( ! $tab ) { | |
| 81 | + $current_tab = self::get_menu_current(); | |
| 82 | + } else { | |
| 83 | + $current_tab = $tab; | |
| 84 | + } | |
| 85 | + $tab_data = self::get_data( $current_tab ); | |
| 86 | + | |
| 87 | + if ( ! empty( $tab_data['sections'] ) ) { | |
| 88 | + $sections = $tab_data['sections']; | |
| 89 | + $section = reset( $sections ); | |
| 90 | + if ( ! empty( $section['slug'] ) ) { | |
| 91 | + $current = $section['slug']; | |
| 92 | + } else { | |
| 93 | + $current = array_keys( $tab_data['sections'] ); | |
| 94 | + } | |
| 95 | + } | |
| 96 | + } | |
| 97 | + | |
| 98 | + return $current; | |
| 99 | + } | |
| 100 | + | |
| 101 | + /** | |
| 102 | + * Retrieves tabs data or a specific tab by key. | |
| 103 | + * | |
| 104 | + * @param string|bool | |
| 105 | + * @return array | |
| 106 | + * | |
| 107 | + * @since 4.3.0 | |
| 108 | + * @version 1.0.0 | |
| 109 | + */ | |
| 110 | + public static function get_data( $key = false ) { | |
| 111 | + $tabs = self::get_menus_arr(); | |
| 112 | + return false !== $key ? ( array_key_exists( $key, $tabs ) ? $tabs[ $key ] : [] ) : $tabs; | |
| 113 | + } | |
| 114 | + | |
| 115 | + /** | |
| 116 | + * Get link for course builder | |
| 117 | + * | |
| 118 | + * @param string $sub | |
| 119 | + * | |
| 120 | + * @return string | |
| 121 | + */ | |
| 122 | + public static function get_link_course_builder( string $sub = '' ): string { | |
| 123 | + $page = LP_Settings::get_option( 'course_builder', 'course-builder' ); | |
| 124 | + return home_url( "{$page}/{$sub}" ); | |
| 125 | + } | |
| 126 | + | |
| 127 | + /** | |
| 128 | + * Get link for add new an item. | |
| 129 | + * | |
| 130 | + * @param $type | |
| 131 | + * | |
| 132 | + * @return string | |
| 133 | + */ | |
| 134 | + public static function get_link_add_new( $type ): string { | |
| 135 | + return self::get_link_course_builder( "{$type}/create" ); | |
| 136 | + } | |
| 137 | + | |
| 138 | + /** | |
| 139 | + * Get tab link | |
| 140 | + * | |
| 141 | + * @param string|false $tab | |
| 142 | + * @param int|string|null $post_id | |
| 143 | + * @param string|false $section | |
| 144 | + * | |
| 145 | + * @return string | |
| 146 | + * | |
| 147 | + * @since 4.3.0 | |
| 148 | + * @version 1.0.0 | |
| 149 | + */ | |
| 150 | + public static function get_tab_link( $tab = false, $post_id = null, $section = false ): string { | |
| 151 | + $link = ''; | |
| 152 | + if ( ! $tab ) { | |
| 153 | + return $link; | |
| 154 | + } | |
| 155 | + | |
| 156 | + $link = self::get_link_course_builder(); | |
| 157 | + | |
| 158 | + if ( ! empty( $tab ) ) { | |
| 159 | + $link .= $tab . '/'; | |
| 160 | + } | |
| 161 | + | |
| 162 | + if ( ! empty( $post_id ) ) { | |
| 163 | + $link .= $post_id . '/'; | |
| 164 | + } | |
| 165 | + | |
| 166 | + if ( ! empty( $section ) ) { | |
| 167 | + $link .= $section . '/'; | |
| 168 | + } | |
| 169 | + | |
| 170 | + return $link; | |
| 171 | + } | |
| 172 | + | |
| 173 | + /** | |
| 174 | + * Get item id | |
| 175 | + * | |
| 176 | + * @return int|string | |
| 177 | + * | |
| 178 | + * @since 4.3.6 | |
| 179 | + * @version 1.0.0 | |
| 180 | + */ | |
| 181 | + public static function get_item_id() { | |
| 182 | + /** @var WP_Query $wp_query */ | |
| 183 | + global $wp_query; | |
| 184 | + | |
| 185 | + return $wp_query->get( self::QUERY_VAR_ITEM_ID ); | |
| 186 | + } | |
| 187 | + | |
| 188 | + /** | |
| 189 | + * Get permission to view course builder | |
| 190 | + * | |
| 191 | + * @return bool | |
| 192 | + */ | |
| 193 | + public static function can_view_course_builder(): bool { | |
| 194 | + $userModel = UserModel::find( get_current_user_id(), true ); | |
| 195 | + return $userModel && $userModel->is_instructor(); | |
| 196 | + } | |
| 197 | + | |
| 198 | + /** | |
| 199 | + * Get title page of course builder | |
| 200 | + * | |
| 201 | + * @return string | |
| 202 | + * @since 4.3.6 | |
| 203 | + * @version 1.0.0 | |
| 204 | + */ | |
| 205 | + public static function get_title_page(): string { | |
| 206 | + /** @var WP_Query $wp_query */ | |
| 207 | + global $wp_query; | |
| 208 | + $title = ''; | |
| 209 | + | |
| 210 | + $menu_current = self::get_menu_current(); | |
| 211 | + $menus = self::get_menus_arr(); | |
| 212 | + if ( isset( $menus[ $menu_current ] ) ) { | |
| 213 | + $title = $menus[ $menu_current ]['title'] ?? ''; | |
| 214 | + | |
| 215 | + $post_id = CourseBuilder::get_item_id(); | |
| 216 | + if ( ! empty( $post_id ) ) { | |
| 217 | + if ( $post_id === self::POST_NEW ) { | |
| 218 | + $title .= ' - ' . __( 'New', 'learnpress' ); | |
| 219 | + } else { | |
| 220 | + $post = get_post( $post_id ); | |
| 221 | + if ( $post ) { | |
| 222 | + $title = sprintf( __( 'Edit "%1$s" - %2$s', 'learnpress' ), $post->post_title, $title ); | |
| 223 | + } | |
| 224 | + } | |
| 225 | + } | |
| 226 | + } | |
| 227 | + | |
| 228 | + return $title; | |
| 229 | + } | |
| 230 | +} | |