PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.9
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.9
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 4.1.7.3.2 All 140 releases
← All changes | inc/class-lp-install.php +270 -75 4.1.7.1 → 4.4.9 View file →
@@ -3,12 +3,12 @@
3 3 * Install and update functions.
4 4 *
5 5 * @author ThimPress
6 6 * @package LearnPress/Classes
7 - * @version 3.0.0
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,18 +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 - // update_option( 'learn_press_status', 'activated' );
51 -
52 101 $this->create_tables();
53 102
54 - if ( ! self::tables_install_done() ) {
103 + if ( ! $this->tables_install_done() ) {
55 104 return;
56 105 }
57 106
58 107 // Check if LP install before has db version
@@ -60,8 +109,16 @@
60 109 // Save database version of LP
61 110 update_option( LP_KEY_DB_VERSION, LearnPress::instance()->db_version );
62 111 }
63 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 +
64 121 // Create pages default.
65 122 self::create_pages();
66 123
67 124 // Set permalink is "Post name".
@@ -69,77 +126,222 @@
69 126 update_option( 'permalink_structure', '/%postname%/' );
70 127 // flush_rewrite_rules();
71 128 }
72 129
130 + // Rebuilds them with LP CPTs and pages on next request.
131 + flush_rewrite_rules();
132 +
73 133 // Force option users_can_register to ON.
74 - if ( ! get_option( 'users_can_register' ) ) {
134 + /*if ( ! get_option( 'users_can_register' ) ) {
75 135 update_option( 'users_can_register', 1 );
136 + }*/
137 + }
138 +
139 + /**
140 + * Create tables required for LP
141 + * @throws Exception
142 + */
143 + private function create_tables() {
144 + $db = LP_Database::getInstance();
145 + $tables = Config::instance()->get( 'tables-v4', 'table' );
146 + foreach ( $tables as $table ) {
147 + $db->wpdb->query( $table );
148 +
149 + $db->check_execute_has_error();
76 150 }
151 +
152 + if ( ! LP_Settings::is_created_tb_thim_cache() ) {
153 + $this->create_table_thim_cache();
154 + }
155 +
156 + if ( ! LP_Settings::is_created_tb_material_files() ) {
157 + $this->create_table_learnpress_files();
158 + }
159 +
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 + }
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' );
77 173 }
78 174
79 175 /**
80 - * Check to run installer in the first-time LP installed.
176 + * Create table thim_cache
81 177 *
82 - * @since 3.x.x
178 + * @since 4.2.2
83 179 */
84 - public static function do_install() {
85 - if ( ! current_user_can( 'manage_options' ) ) {
86 - return;
180 + private function create_table_thim_cache() {
181 + global $wpdb;
182 +
183 + try {
184 + $collation = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci';
185 +
186 + $sql = "CREATE TABLE IF NOT EXISTS {$this->lp_db->tb_thim_cache} (
187 + key_cache VARCHAR (191) NOT NULL UNIQUE,
188 + value LONGTEXT NOT NULL,
189 + expiration VARCHAR (191),
190 + PRIMARY KEY (key_cache)
191 + ) $collation";
192 +
193 + $rs = $wpdb->query( $sql );
194 +
195 + if ( $rs ) {
196 + update_option( 'thim_cache_tb_created', 'yes' );
197 + }
198 + } catch ( Throwable $e ) {
199 + error_log( $e->getMessage() );
87 200 }
201 + }
88 202
89 - $status = get_option( 'learn_press_status' );
203 + /**
204 + * Create table learnpress_courses
205 + *
206 + * @since 4.2.6.9
207 + */
208 + public function create_table_courses() {
209 + global $wpdb;
90 210
91 - switch ( $status ) {
92 - case 'activated':
93 - self::install();
94 - update_option( 'learn_press_status', 'installed' );
211 + try {
212 + $collation = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci';
213 +
214 + $sql = "CREATE TABLE IF NOT EXISTS {$this->lp_db->tb_lp_courses} (
215 + ID bigint(20) unsigned NOT NULL,
216 + json LONGTEXT NOT NULL,
217 + price_to_sort FLOAT,
218 + is_sale int(1) default 0,
219 + post_author bigint unsigned,
220 + post_date_gmt datetime,
221 + post_content LONGTEXT,
222 + post_title text not null,
223 + post_status varchar(20) default 'publish' not null,
224 + post_name varchar(200) default '',
225 + menu_order int default 0,
226 + lang varchar(20),
227 + PRIMARY KEY (ID),
228 + KEY post_title (post_title(191)),
229 + KEY post_status (post_status),
230 + KEY post_name (post_name(191)),
231 + KEY id_status (ID, post_status)
232 + ) $collation";
233 +
234 + $rs = $wpdb->query( $sql );
235 +
236 + if ( $rs ) {
237 + update_option( 'tb_learnpress_courses', 'yes' );
238 + }
239 + } catch ( Throwable $e ) {
240 + error_log( $e->getMessage() );
95 241 }
96 242 }
97 243
98 244 /**
99 - * Run installation after LearnPress is activated.
245 + * Create table learnpress_material_files
100 246 *
101 - * @depecated 4.1.6.4
247 + * @since 4.2.6.6
102 248 */
103 - public static function install() {
104 - _deprecated_function( __FUNCTION__, '4.1.6.4' );
105 - /*self::create_options();
106 - self::_create_pages();
107 - self::_create_cron_jobs();
108 - self::_delete_transients();
109 - //self::_create_log_path();
110 - self::_clear_backgrounds();
249 + private function create_table_learnpress_files() {
250 + global $wpdb;
111 251
112 - $current_version = get_option( 'learnpress_version', null );
113 - $current_db_version = get_option( 'learnpress_db_version', null );
252 + try {
253 + $collation = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci';
114 254
115 - // Fresh installation .
116 - if ( is_null( $current_db_version ) ) {
117 - update_option( 'learn_press_install', 'yes' );
118 - set_transient( 'lp_activation_redirect', 'yes', 60 );
255 + $sql = "CREATE TABLE IF NOT EXISTS {$this->lp_db->tb_lp_files} (
256 + file_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
257 + file_name varchar(191) NOT NULL DEFAULT '',
258 + file_type varchar(100) NOT NULL DEFAULT '',
259 + item_id bigint(20) unsigned NOT NULL DEFAULT '0',
260 + item_type varchar(100) NOT NULL DEFAULT '',
261 + method varchar(10) NOT NULL DEFAULT 'upload',
262 + file_path varchar(255) NOT NULL DEFAULT '',
263 + orders int(4) NOT NULL DEFAULT '0',
264 + created_at datetime NULL DEFAULT NULL,
265 + PRIMARY KEY (file_id),
266 + KEY file_name (file_name),
267 + KEY item_id (item_id),
268 + KEY item_type (item_type)
269 + ) $collation";
270 +
271 + $rs = $wpdb->query( $sql );
272 +
273 + if ( $rs ) {
274 + update_option( 'table_learnpress_files_created', 'yes' );
275 + }
276 + } catch ( Throwable $e ) {
277 + error_log( $e->getMessage() );
119 278 }
279 + }
120 280
121 - // Force to show notice outdated template .
122 - learn_press_delete_user_option( 'hide-notice-template-files' );
281 + /**
282 + * Create table learnpress_mcp_api_keys.
283 + *
284 + * @since 4.3.3
285 + * @return void
286 + */
287 + public function create_table_mcp_api_keys() {
288 + global $wpdb;
123 289
124 - LP_Admin_Notice::instance()->remove_dismissed_notice( array( 'outdated-template' ) );
290 + try {
291 + $collation = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci';
125 292
126 - //if ( ! get_option( 'learnpress_db_version' ) ) {
127 - update_option( 'learnpress_db_version', (int) LEARNPRESS_VERSION );
128 - //}*/
293 + $sql = "CREATE TABLE IF NOT EXISTS {$this->lp_db->tb_lp_mcp_api_keys} (
294 + key_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
295 + user_id bigint(20) unsigned NOT NULL,
296 + description varchar(200) NULL DEFAULT NULL,
297 + permissions varchar(10) NOT NULL DEFAULT 'read',
298 + consumer_key char(64) NOT NULL,
299 + consumer_secret char(64) NOT NULL,
300 + truncated_key char(7) NOT NULL,
301 + last_access datetime NULL DEFAULT NULL,
302 + call_count bigint(20) unsigned NOT NULL DEFAULT 0,
303 + created_at datetime NOT NULL,
304 + updated_at datetime NULL DEFAULT NULL,
305 + PRIMARY KEY (key_id),
306 + UNIQUE KEY consumer_key (consumer_key),
307 + KEY user_id (user_id)
308 + ) $collation";
309 +
310 + $wpdb->query( $sql );
311 + } catch ( Throwable $e ) {
312 + error_log( $e->getMessage() );
313 + }
129 314 }
130 315
131 316 /**
132 - * Create tables required for LP
317 + * Create table learnpress_webhooks.
318 + *
319 + * @since 4.3.3
320 + * @return void
133 321 */
134 - private function create_tables() {
322 + public function create_table_webhooks() {
323 + global $wpdb;
324 +
135 325 try {
136 - $tables = Config::instance()->get( 'tables-v4', 'table' );
137 - foreach ( $tables as $table ) {
138 - LP_Database::getInstance()->wpdb->query( $table );
139 - }
326 + $collation = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci';
140 327
141 - update_option( 'learn_press_check_tables', 'yes' );
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 );
142 344 } catch ( Throwable $e ) {
143 345 error_log( $e->getMessage() );
144 346 }
145 347 }
@@ -154,46 +356,38 @@
154 356 foreach ( $pages as $page ) {
155 357 // Check if page has already existed
156 358 $page_id = get_option( "learn_press_{$page}_page_id", false );
157 359
158 - if ( $page_id && get_post_type( $page_id ) == 'page' && get_post_status( $page_id ) == 'publish' ) {
360 + if ( $page_id && get_post_type( $page_id ) === 'page' && get_post_status( $page_id ) == 'publish' ) {
159 361 continue;
160 362 }
161 363
162 - //$page_id = self::_search_page( $page, $pages );
163 -
164 364 if ( $page === 'courses' ) {
165 - $page_title = 'All Courses';
365 + $page_title = 'Courses';
166 366 $page_slug = $page;
367 + } elseif ( 'single_instructor' === $page ) {
368 + $page_title = 'Instructor';
369 + $page_slug = 'instructor';
370 + } elseif ( 'instructors' === $page ) {
371 + $page_title = 'Instructors';
372 + $page_slug = $page;
373 + } elseif ( 'become_a_teacher' === $page ) {
374 + $page_title = 'Become an Instructor';
375 + $page_slug = $page;
376 + } elseif ( 'term_conditions' === $page ) {
377 + $page_title = 'Terms and Conditions';
378 + $page_slug = $page;
167 379 } else {
168 380 $page_title = ucwords( str_replace( '_', ' ', $page ) );
169 381 $page_slug = 'lp-' . str_replace( '_', '-', $page );
170 382 }
171 383
172 - if ( $page === 'profile' ) {
173 - $page_content = '<!-- wp:shortcode -->[' . apply_filters( 'learn-press/shortcode/profile/tag', 'learn_press_profile' ) . ']<!-- /wp:shortcode -->';
174 - } else {
175 - $page_content = '';
176 - }
177 -
178 - $page_id = wp_insert_post(
179 - array(
180 - 'post_title' => $page_title,
181 - 'post_name' => $page_slug,
182 - 'post_status' => 'publish',
183 - 'post_type' => 'page',
184 - 'comment_status' => 'closed',
185 - 'post_content' => $page_content ?? '',
186 - 'post_author' => get_current_user_id(),
187 - )
384 + $data_create_page = array(
385 + 'post_title' => $page_title,
386 + 'post_name' => $page_slug,
188 387 );
189 -
190 - if ( ! $page_id instanceof WP_Error ) {
191 - update_option( "learn_press_{$page}_page_id", $page_id );
192 - }
388 + LP_Helper::create_page( $data_create_page, "learn_press_{$page}_page_id" );
193 389 }
194 -
195 - flush_rewrite_rules();
196 390 } catch ( Exception $ex ) {
197 391 error_log( $ex->getMessage() );
198 392 }
199 393 }
@@ -203,11 +397,12 @@
203 397 *
204 398 * @return bool
205 399 */
206 400 public function tables_install_done(): bool {
207 - $install_done = get_option( 'learn_press_check_tables', 'no' );
208 - if ( is_multisite() && 'yes' !== $install_done ) {
401 + $install_done = 'yes' === get_option( 'learn_press_check_tables', 'no' );
402 + if ( ! $install_done ) {
209 403 $this->create_tables();
404 + $install_done = 'yes' === get_option( 'learn_press_check_tables', 'no' );
210 405 }
211 406
212 407 return $install_done;
213 408 }