PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.9.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.9.1
4.4.9.1 4.4.9 4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 All 141 releases
← All changes | inc/class-lp-install.php +290 -38 4.1.7.3 → 4.4.9.1 View file →
@@ -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,59 @@
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 + ini_set( 'max_execution_time', HOUR_IN_SECONDS );
40 + // From LP v4.2.2 temporary run create table thim_cache.
41 + // After a long time, will remove this code. Only run create table when activate plugin LP.
42 + if ( ! LP_Settings::is_created_tb_thim_cache() ) {
43 + $this->create_table_thim_cache();
44 + }
45 +
46 + // From LP v4.2.6.9 temporary run create table learnpress_files.
47 + // After a long time, will remove this code. Only run create table when activate plugin LP.
48 + if ( ! LP_Settings::is_created_tb_courses() ) {
49 + $this->create_table_courses();
50 + }
51 +
52 + // From LP v4.2.6.6 temporary run create table learnpress_files.
53 + // After a long time, will remove this code. Only run create table when activate plugin LP.
54 + if ( ! LP_Settings::is_created_tb_material_files() ) {
55 + $this->create_table_learnpress_files();
56 + }
57 +
58 + // Runtime migration for MCP API keys table.
59 + if ( ! LP_Settings::is_created_tb_mcp_api_keys() ) {
60 + $this->create_table_mcp_api_keys();
61 + }
62 +
63 + // Runtime migration for Webhooks table.
64 + if ( ! LP_Settings::is_created_tb_webhooks() ) {
65 + $this->create_table_webhooks();
66 + }
67 +
68 + // Set roles and capabilities.
69 + learn_press_add_user_roles();
70 +
71 + ini_set( 'max_execution_time', LearnPress::$time_limit_default_of_sever );
29 72 }
30 73
31 74 /**
32 75 * Get instance
@@ -41,16 +84,24 @@
41 84 return self::$instance;
42 85 }
43 86
44 87 /**
45 - * Do something after LP is activated.
88 + * Run installation tasks when LearnPress is activated.
46 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 + *
47 98 * @since 4.0.0
48 99 */
49 100 public function on_activate() {
50 101 $this->create_tables();
51 102
52 - if ( ! self::tables_install_done() ) {
103 + if ( ! $this->tables_install_done() ) {
53 104 return;
54 105 }
55 106
56 107 // Check if LP install before has db version
@@ -58,8 +109,16 @@
58 109 // Save database version of LP
59 110 update_option( LP_KEY_DB_VERSION, LearnPress::instance()->db_version );
60 111 }
61 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 +
62 121 // Create pages default.
63 122 self::create_pages();
64 123
65 124 // Set permalink is "Post name".
@@ -67,25 +126,120 @@
67 126 update_option( 'permalink_structure', '/%postname%/' );
68 127 // flush_rewrite_rules();
69 128 }
70 129
130 + // Set roles and capabilities.
131 + learn_press_add_user_roles();
132 +
133 + // Rebuilds them with LP CPTs and pages on next request.
134 + flush_rewrite_rules();
135 +
71 136 // Force option users_can_register to ON.
72 - if ( ! get_option( 'users_can_register' ) ) {
137 + /*if ( ! get_option( 'users_can_register' ) ) {
73 138 update_option( 'users_can_register', 1 );
74 - }
139 + }*/
75 140 }
76 141
77 142 /**
78 143 * Create tables required for LP
144 + * @throws Exception
79 145 */
80 146 private function create_tables() {
147 + $db = LP_Database::getInstance();
148 + $tables = Config::instance()->get( 'tables-v4', 'table' );
149 + foreach ( $tables as $table ) {
150 + $db->wpdb->query( $table );
151 +
152 + $db->check_execute_has_error();
153 + }
154 +
155 + if ( ! LP_Settings::is_created_tb_thim_cache() ) {
156 + $this->create_table_thim_cache();
157 + }
158 +
159 + if ( ! LP_Settings::is_created_tb_material_files() ) {
160 + $this->create_table_learnpress_files();
161 + }
162 +
163 + // Ensure MCP API keys table exists for activation/upgrade flows.
164 + // Keep this explicit guard in addition to tables-v4 config loading.
165 + if ( ! LP_Settings::is_created_tb_mcp_api_keys() ) {
166 + $this->create_table_mcp_api_keys();
167 + }
168 +
169 + // Ensure Webhooks table exists for activation/upgrade flows.
170 + // Keep this explicit guard in addition to tables-v4 config loading.
171 + if ( ! LP_Settings::is_created_tb_webhooks() ) {
172 + $this->create_table_webhooks();
173 + }
174 +
175 + update_option( 'learn_press_check_tables', 'yes' );
176 + }
177 +
178 + /**
179 + * Create table thim_cache
180 + *
181 + * @since 4.2.2
182 + */
183 + private function create_table_thim_cache() {
184 + global $wpdb;
185 +
81 186 try {
82 - $tables = Config::instance()->get( 'tables-v4', 'table' );
83 - foreach ( $tables as $table ) {
84 - LP_Database::getInstance()->wpdb->query( $table );
187 + $collation = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci';
188 +
189 + $sql = "CREATE TABLE IF NOT EXISTS {$this->lp_db->tb_thim_cache} (
190 + key_cache VARCHAR (191) NOT NULL UNIQUE,
191 + value LONGTEXT NOT NULL,
192 + expiration VARCHAR (191),
193 + PRIMARY KEY (key_cache)
194 + ) $collation";
195 +
196 + $rs = $wpdb->query( $sql );
197 +
198 + if ( $rs ) {
199 + update_option( 'thim_cache_tb_created', 'yes' );
85 200 }
201 + } catch ( Throwable $e ) {
202 + error_log( $e->getMessage() );
203 + }
204 + }
86 205
87 - update_option( 'learn_press_check_tables', 'yes' );
206 + /**
207 + * Create table learnpress_courses
208 + *
209 + * @since 4.2.6.9
210 + */
211 + public function create_table_courses() {
212 + global $wpdb;
213 +
214 + try {
215 + $collation = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci';
216 +
217 + $sql = "CREATE TABLE IF NOT EXISTS {$this->lp_db->tb_lp_courses} (
218 + ID bigint(20) unsigned NOT NULL,
219 + json LONGTEXT NOT NULL,
220 + price_to_sort FLOAT,
221 + is_sale int(1) default 0,
222 + post_author bigint unsigned,
223 + post_date_gmt datetime,
224 + post_content LONGTEXT,
225 + post_title text not null,
226 + post_status varchar(20) default 'publish' not null,
227 + post_name varchar(200) default '',
228 + menu_order int default 0,
229 + lang varchar(20),
230 + PRIMARY KEY (ID),
231 + KEY post_title (post_title(191)),
232 + KEY post_status (post_status),
233 + KEY post_name (post_name(191)),
234 + KEY id_status (ID, post_status)
235 + ) $collation";
236 +
237 + $rs = $wpdb->query( $sql );
238 +
239 + if ( $rs ) {
240 + update_option( 'tb_learnpress_courses', 'yes' );
241 + }
88 242 } catch ( Throwable $e ) {
89 243 error_log( $e->getMessage() );
90 244 }
91 245 }
@@ -90,8 +244,113 @@
90 244 }
91 245 }
92 246
93 247 /**
248 + * Create table learnpress_material_files
249 + *
250 + * @since 4.2.6.6
251 + */
252 + private function create_table_learnpress_files() {
253 + global $wpdb;
254 +
255 + try {
256 + $collation = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci';
257 +
258 + $sql = "CREATE TABLE IF NOT EXISTS {$this->lp_db->tb_lp_files} (
259 + file_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
260 + file_name varchar(191) NOT NULL DEFAULT '',
261 + file_type varchar(100) NOT NULL DEFAULT '',
262 + item_id bigint(20) unsigned NOT NULL DEFAULT '0',
263 + item_type varchar(100) NOT NULL DEFAULT '',
264 + method varchar(10) NOT NULL DEFAULT 'upload',
265 + file_path varchar(255) NOT NULL DEFAULT '',
266 + orders int(4) NOT NULL DEFAULT '0',
267 + created_at datetime NULL DEFAULT NULL,
268 + PRIMARY KEY (file_id),
269 + KEY file_name (file_name),
270 + KEY item_id (item_id),
271 + KEY item_type (item_type)
272 + ) $collation";
273 +
274 + $rs = $wpdb->query( $sql );
275 +
276 + if ( $rs ) {
277 + update_option( 'table_learnpress_files_created', 'yes' );
278 + }
279 + } catch ( Throwable $e ) {
280 + error_log( $e->getMessage() );
281 + }
282 + }
283 +
284 + /**
285 + * Create table learnpress_mcp_api_keys.
286 + *
287 + * @since 4.3.3
288 + * @return void
289 + */
290 + public function create_table_mcp_api_keys() {
291 + global $wpdb;
292 +
293 + try {
294 + $collation = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci';
295 +
296 + $sql = "CREATE TABLE IF NOT EXISTS {$this->lp_db->tb_lp_mcp_api_keys} (
297 + key_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
298 + user_id bigint(20) unsigned NOT NULL,
299 + description varchar(200) NULL DEFAULT NULL,
300 + permissions varchar(10) NOT NULL DEFAULT 'read',
301 + consumer_key char(64) NOT NULL,
302 + consumer_secret char(64) NOT NULL,
303 + truncated_key char(7) NOT NULL,
304 + last_access datetime NULL DEFAULT NULL,
305 + call_count bigint(20) unsigned NOT NULL DEFAULT 0,
306 + created_at datetime NOT NULL,
307 + updated_at datetime NULL DEFAULT NULL,
308 + PRIMARY KEY (key_id),
309 + UNIQUE KEY consumer_key (consumer_key),
310 + KEY user_id (user_id)
311 + ) $collation";
312 +
313 + $wpdb->query( $sql );
314 + } catch ( Throwable $e ) {
315 + error_log( $e->getMessage() );
316 + }
317 + }
318 +
319 + /**
320 + * Create table learnpress_webhooks.
321 + *
322 + * @since 4.3.3
323 + * @return void
324 + */
325 + public function create_table_webhooks() {
326 + global $wpdb;
327 +
328 + try {
329 + $collation = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci';
330 +
331 + $sql = "CREATE TABLE IF NOT EXISTS {$this->lp_db->tb_lp_webhooks} (
332 + webhook_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
333 + user_id bigint(20) unsigned NOT NULL DEFAULT 0,
334 + status varchar(20) NOT NULL DEFAULT 'active',
335 + name varchar(200) NOT NULL DEFAULT '',
336 + delivery_url longtext NOT NULL,
337 + secret text NOT NULL,
338 + events longtext NOT NULL,
339 + created_at datetime NOT NULL,
340 + updated_at datetime NULL DEFAULT NULL,
341 + PRIMARY KEY (webhook_id),
342 + KEY user_id (user_id),
343 + KEY status (status)
344 + ) $collation";
345 +
346 + $wpdb->query( $sql );
347 + } catch ( Throwable $e ) {
348 + error_log( $e->getMessage() );
349 + }
350 + }
351 +
352 + /**
94 353 * Create default pages for LP
95 354 */
96 355 public static function create_pages() {
97 356 $pages = self::$_pages;
@@ -100,46 +359,38 @@
100 359 foreach ( $pages as $page ) {
101 360 // Check if page has already existed
102 361 $page_id = get_option( "learn_press_{$page}_page_id", false );
103 362
104 - if ( $page_id && get_post_type( $page_id ) == 'page' && get_post_status( $page_id ) == 'publish' ) {
363 + if ( $page_id && get_post_type( $page_id ) === 'page' && get_post_status( $page_id ) == 'publish' ) {
105 364 continue;
106 365 }
107 366
108 - //$page_id = self::_search_page( $page, $pages );
109 -
110 367 if ( $page === 'courses' ) {
111 - $page_title = 'All Courses';
368 + $page_title = 'Courses';
112 369 $page_slug = $page;
370 + } elseif ( 'single_instructor' === $page ) {
371 + $page_title = 'Instructor';
372 + $page_slug = 'instructor';
373 + } elseif ( 'instructors' === $page ) {
374 + $page_title = 'Instructors';
375 + $page_slug = $page;
376 + } elseif ( 'become_a_teacher' === $page ) {
377 + $page_title = 'Become an Instructor';
378 + $page_slug = $page;
379 + } elseif ( 'term_conditions' === $page ) {
380 + $page_title = 'Terms and Conditions';
381 + $page_slug = $page;
113 382 } else {
114 383 $page_title = ucwords( str_replace( '_', ' ', $page ) );
115 384 $page_slug = 'lp-' . str_replace( '_', '-', $page );
116 385 }
117 386
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 - )
387 + $data_create_page = array(
388 + 'post_title' => $page_title,
389 + 'post_name' => $page_slug,
134 390 );
135 -
136 - if ( ! $page_id instanceof WP_Error ) {
137 - update_option( "learn_press_{$page}_page_id", $page_id );
138 - }
391 + LP_Helper::create_page( $data_create_page, "learn_press_{$page}_page_id" );
139 392 }
140 -
141 - flush_rewrite_rules();
142 393 } catch ( Exception $ex ) {
143 394 error_log( $ex->getMessage() );
144 395 }
145 396 }
@@ -149,11 +400,12 @@
149 400 *
150 401 * @return bool
151 402 */
152 403 public function tables_install_done(): bool {
153 - $install_done = get_option( 'learn_press_check_tables', 'no' );
154 - if ( is_multisite() && 'yes' !== $install_done ) {
404 + $install_done = 'yes' === get_option( 'learn_press_check_tables', 'no' );
405 + if ( ! $install_done ) {
155 406 $this->create_tables();
407 + $install_done = 'yes' === get_option( 'learn_press_check_tables', 'no' );
156 408 }
157 409
158 410 return $install_done;
159 411 }