| @@ -6,9 +6,9 @@ | ||
| 6 | 6 | * @package LearnPress/Classes |
| 7 | 7 | * @version 3.0.1 |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | -use LP\Helpers\Config; | |
| 10 | +use LearnPress\Helpers\Config; | |
| 11 | 11 | |
| 12 | 12 | defined( 'ABSPATH' ) || exit(); |
| 13 | 13 | |
| 14 | 14 | if ( ! function_exists( 'LP_Install' ) ) { |
| @@ -17,16 +17,63 @@ | ||
| 17 | 17 | * Class LP_Install |
| 18 | 18 | */ |
| 19 | 19 | class LP_Install { |
| 20 | 20 | protected static $instance; |
| 21 | + protected $lp_db; | |
| 21 | 22 | /** |
| 22 | 23 | * Default pages used by LP |
| 23 | 24 | * |
| 24 | 25 | * @var array |
| 25 | 26 | */ |
| 26 | - private static $_pages = array( 'checkout', 'profile', 'courses', 'become_a_teacher', 'term_conditions' ); | |
| 27 | + private static $_pages = array( | |
| 28 | + 'checkout', | |
| 29 | + 'profile', | |
| 30 | + 'courses', | |
| 31 | + 'instructors', | |
| 32 | + 'single_instructor', | |
| 33 | + 'become_a_teacher', | |
| 34 | + 'term_conditions', | |
| 35 | + ); | |
| 27 | 36 | |
| 28 | 37 | protected function __construct() { |
| 38 | + $this->lp_db = LP_Database::getInstance(); | |
| 39 | + // Only run on backend. | |
| 40 | + if ( ! is_admin() ) { | |
| 41 | + return; | |
| 42 | + } | |
| 43 | + ini_set( 'max_execution_time', HOUR_IN_SECONDS ); | |
| 44 | + // From LP v4.2.2 temporary run create table thim_cache. | |
| 45 | + // After a long time, will remove this code. Only run create table when activate plugin LP. | |
| 46 | + if ( ! LP_Settings::is_created_tb_thim_cache() ) { | |
| 47 | + $this->create_table_thim_cache(); | |
| 48 | + } | |
| 49 | + | |
| 50 | + // From LP v4.2.6.9 temporary run create table learnpress_files. | |
| 51 | + // After a long time, will remove this code. Only run create table when activate plugin LP. | |
| 52 | + if ( ! LP_Settings::is_created_tb_courses() ) { | |
| 53 | + $this->create_table_courses(); | |
| 54 | + } | |
| 55 | + | |
| 56 | + // From LP v4.2.6.6 temporary run create table learnpress_files. | |
| 57 | + // After a long time, will remove this code. Only run create table when activate plugin LP. | |
| 58 | + if ( ! LP_Settings::is_created_tb_material_files() ) { | |
| 59 | + $this->create_table_learnpress_files(); | |
| 60 | + } | |
| 61 | + | |
| 62 | + // Runtime migration for MCP API keys table. | |
| 63 | + if ( ! LP_Settings::is_created_tb_mcp_api_keys() ) { | |
| 64 | + $this->create_table_mcp_api_keys(); | |
| 65 | + } | |
| 66 | + | |
| 67 | + // Runtime migration for Webhooks table. | |
| 68 | + if ( ! LP_Settings::is_created_tb_webhooks() ) { | |
| 69 | + $this->create_table_webhooks(); | |
| 70 | + } | |
| 71 | + | |
| 72 | + // Set roles and capabilities. | |
| 73 | + learn_press_add_user_roles(); | |
| 74 | + | |
| 75 | + ini_set( 'max_execution_time', LearnPress::$time_limit_default_of_sever ); | |
| 29 | 76 | } |
| 30 | 77 | |
| 31 | 78 | /** |
| 32 | 79 | * Get instance |
| @@ -68,11 +115,11 @@ | ||
| 68 | 115 | // flush_rewrite_rules(); |
| 69 | 116 | } |
| 70 | 117 | |
| 71 | 118 | // Force option users_can_register to ON. |
| 72 | - if ( ! get_option( 'users_can_register' ) ) { | |
| 119 | + /*if ( ! get_option( 'users_can_register' ) ) { | |
| 73 | 120 | update_option( 'users_can_register', 1 ); |
| 74 | - } | |
| 121 | + }*/ | |
| 75 | 122 | } |
| 76 | 123 | |
| 77 | 124 | /** |
| 78 | 125 | * Create tables required for LP |
| @@ -83,8 +130,28 @@ | ||
| 83 | 130 | foreach ( $tables as $table ) { |
| 84 | 131 | LP_Database::getInstance()->wpdb->query( $table ); |
| 85 | 132 | } |
| 86 | 133 | |
| 134 | + if ( ! LP_Settings::is_created_tb_thim_cache() ) { | |
| 135 | + $this->create_table_thim_cache(); | |
| 136 | + } | |
| 137 | + | |
| 138 | + if ( ! LP_Settings::is_created_tb_material_files() ) { | |
| 139 | + $this->create_table_learnpress_files(); | |
| 140 | + } | |
| 141 | + | |
| 142 | + // Ensure MCP API keys table exists for activation/upgrade flows. | |
| 143 | + // Keep this explicit guard in addition to tables-v4 config loading. | |
| 144 | + if ( ! LP_Settings::is_created_tb_mcp_api_keys() ) { | |
| 145 | + $this->create_table_mcp_api_keys(); | |
| 146 | + } | |
| 147 | + | |
| 148 | + // Ensure Webhooks table exists for activation/upgrade flows. | |
| 149 | + // Keep this explicit guard in addition to tables-v4 config loading. | |
| 150 | + if ( ! LP_Settings::is_created_tb_webhooks() ) { | |
| 151 | + $this->create_table_webhooks(); | |
| 152 | + } | |
| 153 | + | |
| 87 | 154 | update_option( 'learn_press_check_tables', 'yes' ); |
| 88 | 155 | } catch ( Throwable $e ) { |
| 89 | 156 | error_log( $e->getMessage() ); |
| 90 | 157 | } |
| @@ -90,8 +157,182 @@ | ||
| 90 | 157 | } |
| 91 | 158 | } |
| 92 | 159 | |
| 93 | 160 | /** |
| 161 | + * Create table thim_cache | |
| 162 | + * | |
| 163 | + * @since 4.2.2 | |
| 164 | + */ | |
| 165 | + private function create_table_thim_cache() { | |
| 166 | + global $wpdb; | |
| 167 | + | |
| 168 | + try { | |
| 169 | + $collation = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci'; | |
| 170 | + | |
| 171 | + $sql = "CREATE TABLE IF NOT EXISTS {$this->lp_db->tb_thim_cache} ( | |
| 172 | + key_cache VARCHAR (191) NOT NULL UNIQUE, | |
| 173 | + value LONGTEXT NOT NULL, | |
| 174 | + expiration VARCHAR (191), | |
| 175 | + PRIMARY KEY (key_cache) | |
| 176 | + ) $collation"; | |
| 177 | + | |
| 178 | + $rs = $wpdb->query( $sql ); | |
| 179 | + | |
| 180 | + if ( $rs ) { | |
| 181 | + update_option( 'thim_cache_tb_created', 'yes' ); | |
| 182 | + } | |
| 183 | + } catch ( Throwable $e ) { | |
| 184 | + error_log( $e->getMessage() ); | |
| 185 | + } | |
| 186 | + } | |
| 187 | + | |
| 188 | + /** | |
| 189 | + * Create table learnpress_courses | |
| 190 | + * | |
| 191 | + * @since 4.2.6.9 | |
| 192 | + */ | |
| 193 | + public function create_table_courses() { | |
| 194 | + global $wpdb; | |
| 195 | + | |
| 196 | + try { | |
| 197 | + $collation = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci'; | |
| 198 | + | |
| 199 | + $sql = "CREATE TABLE IF NOT EXISTS {$this->lp_db->tb_lp_courses} ( | |
| 200 | + ID bigint(20) unsigned NOT NULL, | |
| 201 | + json LONGTEXT NOT NULL, | |
| 202 | + price_to_sort FLOAT, | |
| 203 | + is_sale int(1) default 0, | |
| 204 | + post_author bigint unsigned, | |
| 205 | + post_date_gmt datetime, | |
| 206 | + post_content LONGTEXT, | |
| 207 | + post_title text not null, | |
| 208 | + post_status varchar(20) default 'publish' not null, | |
| 209 | + post_name varchar(200) default '', | |
| 210 | + menu_order int default 0, | |
| 211 | + lang varchar(20), | |
| 212 | + PRIMARY KEY (ID), | |
| 213 | + KEY post_title (post_title(191)), | |
| 214 | + KEY post_status (post_status), | |
| 215 | + KEY post_name (post_name(191)), | |
| 216 | + KEY id_status (ID, post_status) | |
| 217 | + ) $collation"; | |
| 218 | + | |
| 219 | + $rs = $wpdb->query( $sql ); | |
| 220 | + | |
| 221 | + if ( $rs ) { | |
| 222 | + update_option( 'tb_learnpress_courses', 'yes' ); | |
| 223 | + } | |
| 224 | + } catch ( Throwable $e ) { | |
| 225 | + error_log( $e->getMessage() ); | |
| 226 | + } | |
| 227 | + } | |
| 228 | + | |
| 229 | + /** | |
| 230 | + * Create table learnpress_material_files | |
| 231 | + * | |
| 232 | + * @since 4.2.6.6 | |
| 233 | + */ | |
| 234 | + private function create_table_learnpress_files() { | |
| 235 | + global $wpdb; | |
| 236 | + | |
| 237 | + try { | |
| 238 | + $collation = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci'; | |
| 239 | + | |
| 240 | + $sql = "CREATE TABLE IF NOT EXISTS {$this->lp_db->tb_lp_files} ( | |
| 241 | + file_id bigint(20) unsigned NOT NULL AUTO_INCREMENT, | |
| 242 | + file_name varchar(191) NOT NULL DEFAULT '', | |
| 243 | + file_type varchar(100) NOT NULL DEFAULT '', | |
| 244 | + item_id bigint(20) unsigned NOT NULL DEFAULT '0', | |
| 245 | + item_type varchar(100) NOT NULL DEFAULT '', | |
| 246 | + method varchar(10) NOT NULL DEFAULT 'upload', | |
| 247 | + file_path varchar(255) NOT NULL DEFAULT '', | |
| 248 | + orders int(4) NOT NULL DEFAULT '0', | |
| 249 | + created_at datetime NULL DEFAULT NULL, | |
| 250 | + PRIMARY KEY (file_id), | |
| 251 | + KEY file_name (file_name), | |
| 252 | + KEY item_id (item_id), | |
| 253 | + KEY item_type (item_type) | |
| 254 | + ) $collation"; | |
| 255 | + | |
| 256 | + $rs = $wpdb->query( $sql ); | |
| 257 | + | |
| 258 | + if ( $rs ) { | |
| 259 | + update_option( 'table_learnpress_files_created', 'yes' ); | |
| 260 | + } | |
| 261 | + } catch ( Throwable $e ) { | |
| 262 | + error_log( $e->getMessage() ); | |
| 263 | + } | |
| 264 | + } | |
| 265 | + | |
| 266 | + /** | |
| 267 | + * Create table learnpress_mcp_api_keys. | |
| 268 | + * | |
| 269 | + * @since 4.3.3 | |
| 270 | + * @return void | |
| 271 | + */ | |
| 272 | + public function create_table_mcp_api_keys() { | |
| 273 | + global $wpdb; | |
| 274 | + | |
| 275 | + try { | |
| 276 | + $collation = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci'; | |
| 277 | + | |
| 278 | + $sql = "CREATE TABLE IF NOT EXISTS {$this->lp_db->tb_lp_mcp_api_keys} ( | |
| 279 | + key_id bigint(20) unsigned NOT NULL AUTO_INCREMENT, | |
| 280 | + user_id bigint(20) unsigned NOT NULL, | |
| 281 | + description varchar(200) NULL DEFAULT NULL, | |
| 282 | + permissions varchar(10) NOT NULL DEFAULT 'read', | |
| 283 | + consumer_key char(64) NOT NULL, | |
| 284 | + consumer_secret char(64) NOT NULL, | |
| 285 | + truncated_key char(7) NOT NULL, | |
| 286 | + last_access datetime NULL DEFAULT NULL, | |
| 287 | + call_count bigint(20) unsigned NOT NULL DEFAULT 0, | |
| 288 | + created_at datetime NOT NULL, | |
| 289 | + updated_at datetime NULL DEFAULT NULL, | |
| 290 | + PRIMARY KEY (key_id), | |
| 291 | + UNIQUE KEY consumer_key (consumer_key), | |
| 292 | + KEY user_id (user_id) | |
| 293 | + ) $collation"; | |
| 294 | + | |
| 295 | + $wpdb->query( $sql ); | |
| 296 | + } catch ( Throwable $e ) { | |
| 297 | + error_log( $e->getMessage() ); | |
| 298 | + } | |
| 299 | + } | |
| 300 | + | |
| 301 | + /** | |
| 302 | + * Create table learnpress_webhooks. | |
| 303 | + * | |
| 304 | + * @since 4.3.3 | |
| 305 | + * @return void | |
| 306 | + */ | |
| 307 | + public function create_table_webhooks() { | |
| 308 | + global $wpdb; | |
| 309 | + | |
| 310 | + try { | |
| 311 | + $collation = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci'; | |
| 312 | + | |
| 313 | + $sql = "CREATE TABLE IF NOT EXISTS {$this->lp_db->tb_lp_webhooks} ( | |
| 314 | + webhook_id bigint(20) unsigned NOT NULL AUTO_INCREMENT, | |
| 315 | + user_id bigint(20) unsigned NOT NULL DEFAULT 0, | |
| 316 | + status varchar(20) NOT NULL DEFAULT 'active', | |
| 317 | + name varchar(200) NOT NULL DEFAULT '', | |
| 318 | + delivery_url longtext NOT NULL, | |
| 319 | + secret text NOT NULL, | |
| 320 | + events longtext NOT NULL, | |
| 321 | + created_at datetime NOT NULL, | |
| 322 | + updated_at datetime NULL DEFAULT NULL, | |
| 323 | + PRIMARY KEY (webhook_id), | |
| 324 | + KEY user_id (user_id), | |
| 325 | + KEY status (status) | |
| 326 | + ) $collation"; | |
| 327 | + | |
| 328 | + $wpdb->query( $sql ); | |
| 329 | + } catch ( Throwable $e ) { | |
| 330 | + error_log( $e->getMessage() ); | |
| 331 | + } | |
| 332 | + } | |
| 333 | + | |
| 334 | + /** | |
| 94 | 335 | * Create default pages for LP |
| 95 | 336 | */ |
| 96 | 337 | public static function create_pages() { |
| 97 | 338 | $pages = self::$_pages; |
| @@ -100,43 +341,37 @@ | ||
| 100 | 341 | foreach ( $pages as $page ) { |
| 101 | 342 | // Check if page has already existed |
| 102 | 343 | $page_id = get_option( "learn_press_{$page}_page_id", false ); |
| 103 | 344 | |
| 104 | - if ( $page_id && get_post_type( $page_id ) == 'page' && get_post_status( $page_id ) == 'publish' ) { | |
| 345 | + if ( $page_id && get_post_type( $page_id ) === 'page' && get_post_status( $page_id ) == 'publish' ) { | |
| 105 | 346 | continue; |
| 106 | 347 | } |
| 107 | 348 | |
| 108 | - //$page_id = self::_search_page( $page, $pages ); | |
| 109 | - | |
| 110 | 349 | if ( $page === 'courses' ) { |
| 111 | - $page_title = 'All Courses'; | |
| 350 | + $page_title = 'Courses'; | |
| 112 | 351 | $page_slug = $page; |
| 352 | + } elseif ( 'single_instructor' === $page ) { | |
| 353 | + $page_title = 'Instructor'; | |
| 354 | + $page_slug = 'instructor'; | |
| 355 | + } elseif ( 'instructors' === $page ) { | |
| 356 | + $page_title = 'Instructors'; | |
| 357 | + $page_slug = $page; | |
| 358 | + } elseif ( 'become_a_teacher' === $page ) { | |
| 359 | + $page_title = 'Become an Instructor'; | |
| 360 | + $page_slug = $page; | |
| 361 | + } elseif ( 'term_conditions' === $page ) { | |
| 362 | + $page_title = 'Terms and Conditions'; | |
| 363 | + $page_slug = $page; | |
| 113 | 364 | } else { |
| 114 | 365 | $page_title = ucwords( str_replace( '_', ' ', $page ) ); |
| 115 | 366 | $page_slug = 'lp-' . str_replace( '_', '-', $page ); |
| 116 | 367 | } |
| 117 | 368 | |
| 118 | - if ( $page === 'profile' ) { | |
| 119 | - $page_content = '<!-- wp:shortcode -->[' . apply_filters( 'learn-press/shortcode/profile/tag', 'learn_press_profile' ) . ']<!-- /wp:shortcode -->'; | |
| 120 | - } else { | |
| 121 | - $page_content = ''; | |
| 122 | - } | |
| 123 | - | |
| 124 | - $page_id = wp_insert_post( | |
| 125 | - array( | |
| 126 | - 'post_title' => $page_title, | |
| 127 | - 'post_name' => $page_slug, | |
| 128 | - 'post_status' => 'publish', | |
| 129 | - 'post_type' => 'page', | |
| 130 | - 'comment_status' => 'closed', | |
| 131 | - 'post_content' => $page_content ?? '', | |
| 132 | - 'post_author' => get_current_user_id(), | |
| 133 | - ) | |
| 369 | + $data_create_page = array( | |
| 370 | + 'post_title' => $page_title, | |
| 371 | + 'post_name' => $page_slug, | |
| 134 | 372 | ); |
| 135 | - | |
| 136 | - if ( ! $page_id instanceof WP_Error ) { | |
| 137 | - update_option( "learn_press_{$page}_page_id", $page_id ); | |
| 138 | - } | |
| 373 | + LP_Helper::create_page( $data_create_page, "learn_press_{$page}_page_id" ); | |
| 139 | 374 | } |
| 140 | 375 | |
| 141 | 376 | flush_rewrite_rules(); |
| 142 | 377 | } catch ( Exception $ex ) { |