| @@ -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() ) { |
| @@ -63,8 +59,13 @@ | ||
| 63 | 59 | if ( ! LP_Settings::is_created_tb_mcp_api_keys() ) { |
| 64 | 60 | $this->create_table_mcp_api_keys(); |
| 65 | 61 | } |
| 66 | 62 | |
| 63 | + // Runtime migration for Webhooks table. | |
| 64 | + if ( ! LP_Settings::is_created_tb_webhooks() ) { | |
| 65 | + $this->create_table_webhooks(); | |
| 66 | + } | |
| 67 | + | |
| 67 | 68 | // Set roles and capabilities. |
| 68 | 69 | learn_press_add_user_roles(); |
| 69 | 70 | |
| 70 | 71 | ini_set( 'max_execution_time', LearnPress::$time_limit_default_of_sever ); |
| @@ -83,16 +84,24 @@ | ||
| 83 | 84 | return self::$instance; |
| 84 | 85 | } |
| 85 | 86 | |
| 86 | 87 | /** |
| 87 | - * Do something after LP is activated. | |
| 88 | + * Run installation tasks when LearnPress is activated. | |
| 88 | 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 | + * | |
| 89 | 98 | * @since 4.0.0 |
| 90 | 99 | */ |
| 91 | 100 | public function on_activate() { |
| 92 | 101 | $this->create_tables(); |
| 93 | 102 | |
| 94 | - if ( ! self::tables_install_done() ) { | |
| 103 | + if ( ! $this->tables_install_done() ) { | |
| 95 | 104 | return; |
| 96 | 105 | } |
| 97 | 106 | |
| 98 | 107 | // Check if LP install before has db version |
| @@ -100,8 +109,16 @@ | ||
| 100 | 109 | // Save database version of LP |
| 101 | 110 | update_option( LP_KEY_DB_VERSION, LearnPress::instance()->db_version ); |
| 102 | 111 | } |
| 103 | 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 | + | |
| 104 | 121 | // Create pages default. |
| 105 | 122 | self::create_pages(); |
| 106 | 123 | |
| 107 | 124 | // Set permalink is "Post name". |
| @@ -109,8 +126,11 @@ | ||
| 109 | 126 | update_option( 'permalink_structure', '/%postname%/' ); |
| 110 | 127 | // flush_rewrite_rules(); |
| 111 | 128 | } |
| 112 | 129 | |
| 130 | + // Rebuilds them with LP CPTs and pages on next request. | |
| 131 | + flush_rewrite_rules(); | |
| 132 | + | |
| 113 | 133 | // Force option users_can_register to ON. |
| 114 | 134 | /*if ( ! get_option( 'users_can_register' ) ) { |
| 115 | 135 | update_option( 'users_can_register', 1 ); |
| 116 | 136 | }*/ |
| @@ -117,34 +137,40 @@ | ||
| 117 | 137 | } |
| 118 | 138 | |
| 119 | 139 | /** |
| 120 | 140 | * Create tables required for LP |
| 141 | + * @throws Exception | |
| 121 | 142 | */ |
| 122 | 143 | private function create_tables() { |
| 123 | - try { | |
| 124 | - $tables = Config::instance()->get( 'tables-v4', 'table' ); | |
| 125 | - foreach ( $tables as $table ) { | |
| 126 | - LP_Database::getInstance()->wpdb->query( $table ); | |
| 127 | - } | |
| 144 | + $db = LP_Database::getInstance(); | |
| 145 | + $tables = Config::instance()->get( 'tables-v4', 'table' ); | |
| 146 | + foreach ( $tables as $table ) { | |
| 147 | + $db->wpdb->query( $table ); | |
| 128 | 148 | |
| 129 | - if ( ! LP_Settings::is_created_tb_thim_cache() ) { | |
| 130 | - $this->create_table_thim_cache(); | |
| 131 | - } | |
| 149 | + $db->check_execute_has_error(); | |
| 150 | + } | |
| 132 | 151 | |
| 133 | - if ( ! LP_Settings::is_created_tb_material_files() ) { | |
| 134 | - $this->create_table_learnpress_files(); | |
| 135 | - } | |
| 152 | + if ( ! LP_Settings::is_created_tb_thim_cache() ) { | |
| 153 | + $this->create_table_thim_cache(); | |
| 154 | + } | |
| 136 | 155 | |
| 137 | - // Ensure MCP API keys table exists for activation/upgrade flows. | |
| 138 | - // Keep this explicit guard in addition to tables-v4 config loading. | |
| 139 | - if ( ! LP_Settings::is_created_tb_mcp_api_keys() ) { | |
| 140 | - $this->create_table_mcp_api_keys(); | |
| 141 | - } | |
| 156 | + if ( ! LP_Settings::is_created_tb_material_files() ) { | |
| 157 | + $this->create_table_learnpress_files(); | |
| 158 | + } | |
| 142 | 159 | |
| 143 | - update_option( 'learn_press_check_tables', 'yes' ); | |
| 144 | - } catch ( Throwable $e ) { | |
| 145 | - error_log( $e->getMessage() ); | |
| 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(); | |
| 146 | 164 | } |
| 165 | + | |
| 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(); | |
| 170 | + } | |
| 171 | + | |
| 172 | + update_option( 'learn_press_check_tables', 'yes' ); | |
| 147 | 173 | } |
| 148 | 174 | |
| 149 | 175 | /** |
| 150 | 176 | * Create table thim_cache |
| @@ -287,8 +313,41 @@ | ||
| 287 | 313 | } |
| 288 | 314 | } |
| 289 | 315 | |
| 290 | 316 | /** |
| 317 | + * Create table learnpress_webhooks. | |
| 318 | + * | |
| 319 | + * @since 4.3.3 | |
| 320 | + * @return void | |
| 321 | + */ | |
| 322 | + public function create_table_webhooks() { | |
| 323 | + global $wpdb; | |
| 324 | + | |
| 325 | + try { | |
| 326 | + $collation = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci'; | |
| 327 | + | |
| 328 | + $sql = "CREATE TABLE IF NOT EXISTS {$this->lp_db->tb_lp_webhooks} ( | |
| 329 | + webhook_id bigint(20) unsigned NOT NULL AUTO_INCREMENT, | |
| 330 | + user_id bigint(20) unsigned NOT NULL DEFAULT 0, | |
| 331 | + status varchar(20) NOT NULL DEFAULT 'active', | |
| 332 | + name varchar(200) NOT NULL DEFAULT '', | |
| 333 | + delivery_url longtext NOT NULL, | |
| 334 | + secret text NOT NULL, | |
| 335 | + events longtext NOT NULL, | |
| 336 | + created_at datetime NOT NULL, | |
| 337 | + updated_at datetime NULL DEFAULT NULL, | |
| 338 | + PRIMARY KEY (webhook_id), | |
| 339 | + KEY user_id (user_id), | |
| 340 | + KEY status (status) | |
| 341 | + ) $collation"; | |
| 342 | + | |
| 343 | + $wpdb->query( $sql ); | |
| 344 | + } catch ( Throwable $e ) { | |
| 345 | + error_log( $e->getMessage() ); | |
| 346 | + } | |
| 347 | + } | |
| 348 | + | |
| 349 | + /** | |
| 291 | 350 | * Create default pages for LP |
| 292 | 351 | */ |
| 293 | 352 | public static function create_pages() { |
| 294 | 353 | $pages = self::$_pages; |
| @@ -327,10 +386,8 @@ | ||
| 327 | 386 | 'post_name' => $page_slug, |
| 328 | 387 | ); |
| 329 | 388 | LP_Helper::create_page( $data_create_page, "learn_press_{$page}_page_id" ); |
| 330 | 389 | } |
| 331 | - | |
| 332 | - flush_rewrite_rules(); | |
| 333 | 390 | } catch ( Exception $ex ) { |
| 334 | 391 | error_log( $ex->getMessage() ); |
| 335 | 392 | } |
| 336 | 393 | } |
| @@ -340,11 +397,12 @@ | ||
| 340 | 397 | * |
| 341 | 398 | * @return bool |
| 342 | 399 | */ |
| 343 | 400 | public function tables_install_done(): bool { |
| 344 | - $install_done = get_option( 'learn_press_check_tables', 'no' ); | |
| 345 | - if ( is_multisite() && 'yes' !== $install_done ) { | |
| 401 | + $install_done = 'yes' === get_option( 'learn_press_check_tables', 'no' ); | |
| 402 | + if ( ! $install_done ) { | |
| 346 | 403 | $this->create_tables(); |
| 404 | + $install_done = 'yes' === get_option( 'learn_press_check_tables', 'no' ); | |
| 347 | 405 | } |
| 348 | 406 | |
| 349 | 407 | return $install_done; |
| 350 | 408 | } |