| @@ -35,12 +35,8 @@ | ||
| 35 | 35 | ); |
| 36 | 36 | |
| 37 | 37 | protected function __construct() { |
| 38 | 38 | $this->lp_db = LP_Database::getInstance(); |
| 39 | - // Only run on backend. | |
| 40 | - if ( ! is_admin() ) { | |
| 41 | - return; | |
| 42 | - } | |
| 43 | 39 | ini_set( 'max_execution_time', HOUR_IN_SECONDS ); |
| 44 | 40 | // From LP v4.2.2 temporary run create table thim_cache. |
| 45 | 41 | // After a long time, will remove this code. Only run create table when activate plugin LP. |
| 46 | 42 | if ( ! LP_Settings::is_created_tb_thim_cache() ) { |
| @@ -88,16 +84,24 @@ | ||
| 88 | 84 | return self::$instance; |
| 89 | 85 | } |
| 90 | 86 | |
| 91 | 87 | /** |
| 92 | - * Do something after LP is activated. | |
| 88 | + * Run installation tasks when LearnPress is activated. | |
| 93 | 89 | * |
| 90 | + * 1. Create required database tables. | |
| 91 | + * 2. Stop here if table creation failed. | |
| 92 | + * 3. Save the current database version. | |
| 93 | + * 4. Register LearnPress custom post types and taxonomies. | |
| 94 | + * 5. Create default pages (courses, profile, checkout, become a teacher, etc.). | |
| 95 | + * 6. Set permalink structure to post name if not configured. | |
| 96 | + * 7. Soft flush rewrite rules so WordPress rebuilds them with LP CPTs and pages on the next request. | |
| 97 | + * | |
| 94 | 98 | * @since 4.0.0 |
| 95 | 99 | */ |
| 96 | 100 | public function on_activate() { |
| 97 | 101 | $this->create_tables(); |
| 98 | 102 | |
| 99 | - if ( ! self::tables_install_done() ) { | |
| 103 | + if ( ! $this->tables_install_done() ) { | |
| 100 | 104 | return; |
| 101 | 105 | } |
| 102 | 106 | |
| 103 | 107 | // Check if LP install before has db version |
| @@ -105,8 +109,16 @@ | ||
| 105 | 109 | // Save database version of LP |
| 106 | 110 | update_option( LP_KEY_DB_VERSION, LearnPress::instance()->db_version ); |
| 107 | 111 | } |
| 108 | 112 | |
| 113 | + // Register custom post type and taxonomies . | |
| 114 | + include_once LP_PLUGIN_PATH . 'inc/custom-post-types/abstract.php'; | |
| 115 | + include_once LP_PLUGIN_PATH . 'inc/custom-post-types/course.php'; | |
| 116 | + include_once LP_PLUGIN_PATH . 'inc/custom-post-types/lesson.php'; | |
| 117 | + include_once LP_PLUGIN_PATH . 'inc/custom-post-types/quiz.php'; | |
| 118 | + include_once LP_PLUGIN_PATH . 'inc/custom-post-types/question.php'; | |
| 119 | + include_once LP_PLUGIN_PATH . 'inc/custom-post-types/order.php'; | |
| 120 | + | |
| 109 | 121 | // Create pages default. |
| 110 | 122 | self::create_pages(); |
| 111 | 123 | |
| 112 | 124 | // Set permalink is "Post name". |
| @@ -114,8 +126,11 @@ | ||
| 114 | 126 | update_option( 'permalink_structure', '/%postname%/' ); |
| 115 | 127 | // flush_rewrite_rules(); |
| 116 | 128 | } |
| 117 | 129 | |
| 130 | + // Rebuilds them with LP CPTs and pages on next request. | |
| 131 | + flush_rewrite_rules(); | |
| 132 | + | |
| 118 | 133 | // Force option users_can_register to ON. |
| 119 | 134 | /*if ( ! get_option( 'users_can_register' ) ) { |
| 120 | 135 | update_option( 'users_can_register', 1 ); |
| 121 | 136 | }*/ |
| @@ -122,40 +137,40 @@ | ||
| 122 | 137 | } |
| 123 | 138 | |
| 124 | 139 | /** |
| 125 | 140 | * Create tables required for LP |
| 141 | + * @throws Exception | |
| 126 | 142 | */ |
| 127 | 143 | private function create_tables() { |
| 128 | - try { | |
| 129 | - $tables = Config::instance()->get( 'tables-v4', 'table' ); | |
| 130 | - foreach ( $tables as $table ) { | |
| 131 | - LP_Database::getInstance()->wpdb->query( $table ); | |
| 132 | - } | |
| 144 | + $db = LP_Database::getInstance(); | |
| 145 | + $tables = Config::instance()->get( 'tables-v4', 'table' ); | |
| 146 | + foreach ( $tables as $table ) { | |
| 147 | + $db->wpdb->query( $table ); | |
| 133 | 148 | |
| 134 | - if ( ! LP_Settings::is_created_tb_thim_cache() ) { | |
| 135 | - $this->create_table_thim_cache(); | |
| 136 | - } | |
| 149 | + $db->check_execute_has_error(); | |
| 150 | + } | |
| 137 | 151 | |
| 138 | - if ( ! LP_Settings::is_created_tb_material_files() ) { | |
| 139 | - $this->create_table_learnpress_files(); | |
| 140 | - } | |
| 152 | + if ( ! LP_Settings::is_created_tb_thim_cache() ) { | |
| 153 | + $this->create_table_thim_cache(); | |
| 154 | + } | |
| 141 | 155 | |
| 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 | - } | |
| 156 | + if ( ! LP_Settings::is_created_tb_material_files() ) { | |
| 157 | + $this->create_table_learnpress_files(); | |
| 158 | + } | |
| 147 | 159 | |
| 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 | - } | |
| 160 | + // Ensure MCP API keys table exists for activation/upgrade flows. | |
| 161 | + // Keep this explicit guard in addition to tables-v4 config loading. | |
| 162 | + if ( ! LP_Settings::is_created_tb_mcp_api_keys() ) { | |
| 163 | + $this->create_table_mcp_api_keys(); | |
| 164 | + } | |
| 153 | 165 | |
| 154 | - update_option( 'learn_press_check_tables', 'yes' ); | |
| 155 | - } catch ( Throwable $e ) { | |
| 156 | - error_log( $e->getMessage() ); | |
| 166 | + // Ensure Webhooks table exists for activation/upgrade flows. | |
| 167 | + // Keep this explicit guard in addition to tables-v4 config loading. | |
| 168 | + if ( ! LP_Settings::is_created_tb_webhooks() ) { | |
| 169 | + $this->create_table_webhooks(); | |
| 157 | 170 | } |
| 171 | + | |
| 172 | + update_option( 'learn_press_check_tables', 'yes' ); | |
| 158 | 173 | } |
| 159 | 174 | |
| 160 | 175 | /** |
| 161 | 176 | * Create table thim_cache |
| @@ -371,10 +386,8 @@ | ||
| 371 | 386 | 'post_name' => $page_slug, |
| 372 | 387 | ); |
| 373 | 388 | LP_Helper::create_page( $data_create_page, "learn_press_{$page}_page_id" ); |
| 374 | 389 | } |
| 375 | - | |
| 376 | - flush_rewrite_rules(); | |
| 377 | 390 | } catch ( Exception $ex ) { |
| 378 | 391 | error_log( $ex->getMessage() ); |
| 379 | 392 | } |
| 380 | 393 | } |
| @@ -384,11 +397,12 @@ | ||
| 384 | 397 | * |
| 385 | 398 | * @return bool |
| 386 | 399 | */ |
| 387 | 400 | public function tables_install_done(): bool { |
| 388 | - $install_done = get_option( 'learn_press_check_tables', 'no' ); | |
| 389 | - if ( is_multisite() && 'yes' !== $install_done ) { | |
| 401 | + $install_done = 'yes' === get_option( 'learn_press_check_tables', 'no' ); | |
| 402 | + if ( ! $install_done ) { | |
| 390 | 403 | $this->create_tables(); |
| 404 | + $install_done = 'yes' === get_option( 'learn_press_check_tables', 'no' ); | |
| 391 | 405 | } |
| 392 | 406 | |
| 393 | 407 | return $install_done; |
| 394 | 408 | } |