PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.3.8
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.3.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 4.2.0 4.2.1 All 138 releases
learnpress / inc / class-lp-install.php

class-lp-install.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.3.8, at inc/class-lp-install.php

353 lines 9.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Install and update functions.
4 *
5 * @author ThimPress
6 * @package LearnPress/Classes
7 * @version 3.0.1
8 */
9
10 use LearnPress\Helpers\Config;
11
12 defined( 'ABSPATH' ) || exit();
13
14 if ( ! function_exists( 'LP_Install' ) ) {
15
16 /**
17 * Class LP_Install
18 */
19 class LP_Install {
20 protected static $instance;
21 protected $lp_db;
22 /**
23 * Default pages used by LP
24 *
25 * @var array
26 */
27 private static $_pages = array(
28 'checkout',
29 'profile',
30 'courses',
31 'instructors',
32 'single_instructor',
33 'become_a_teacher',
34 'term_conditions',
35 );
36
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 // Set roles and capabilities.
68 learn_press_add_user_roles();
69
70 ini_set( 'max_execution_time', LearnPress::$time_limit_default_of_sever );
71 }
72
73 /**
74 * Get instance
75 *
76 * @return LP_Install
77 */
78 public static function instance(): self {
79 if ( ! self::$instance ) {
80 self::$instance = new self();
81 }
82
83 return self::$instance;
84 }
85
86 /**
87 * Do something after LP is activated.
88 *
89 * @since 4.0.0
90 */
91 public function on_activate() {
92 $this->create_tables();
93
94 if ( ! self::tables_install_done() ) {
95 return;
96 }
97
98 // Check if LP install before has db version
99 if ( ! get_option( LP_KEY_DB_VERSION, false ) ) {
100 // Save database version of LP
101 update_option( LP_KEY_DB_VERSION, LearnPress::instance()->db_version );
102 }
103
104 // Create pages default.
105 self::create_pages();
106
107 // Set permalink is "Post name".
108 if ( ! get_option( 'permalink_structure' ) ) {
109 update_option( 'permalink_structure', '/%postname%/' );
110 // flush_rewrite_rules();
111 }
112
113 // Force option users_can_register to ON.
114 /*if ( ! get_option( 'users_can_register' ) ) {
115 update_option( 'users_can_register', 1 );
116 }*/
117 }
118
119 /**
120 * Create tables required for LP
121 */
122 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 }
128
129 if ( ! LP_Settings::is_created_tb_thim_cache() ) {
130 $this->create_table_thim_cache();
131 }
132
133 if ( ! LP_Settings::is_created_tb_material_files() ) {
134 $this->create_table_learnpress_files();
135 }
136
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 }
142
143 update_option( 'learn_press_check_tables', 'yes' );
144 } catch ( Throwable $e ) {
145 error_log( $e->getMessage() );
146 }
147 }
148
149 /**
150 * Create table thim_cache
151 *
152 * @since 4.2.2
153 */
154 private function create_table_thim_cache() {
155 global $wpdb;
156
157 try {
158 $collation = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci';
159
160 $sql = "CREATE TABLE IF NOT EXISTS {$this->lp_db->tb_thim_cache} (
161 key_cache VARCHAR (191) NOT NULL UNIQUE,
162 value LONGTEXT NOT NULL,
163 expiration VARCHAR (191),
164 PRIMARY KEY (key_cache)
165 ) $collation";
166
167 $rs = $wpdb->query( $sql );
168
169 if ( $rs ) {
170 update_option( 'thim_cache_tb_created', 'yes' );
171 }
172 } catch ( Throwable $e ) {
173 error_log( $e->getMessage() );
174 }
175 }
176
177 /**
178 * Create table learnpress_courses
179 *
180 * @since 4.2.6.9
181 */
182 public function create_table_courses() {
183 global $wpdb;
184
185 try {
186 $collation = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci';
187
188 $sql = "CREATE TABLE IF NOT EXISTS {$this->lp_db->tb_lp_courses} (
189 ID bigint(20) unsigned NOT NULL,
190 json LONGTEXT NOT NULL,
191 price_to_sort FLOAT,
192 is_sale int(1) default 0,
193 post_author bigint unsigned,
194 post_date_gmt datetime,
195 post_content LONGTEXT,
196 post_title text not null,
197 post_status varchar(20) default 'publish' not null,
198 post_name varchar(200) default '',
199 menu_order int default 0,
200 lang varchar(20),
201 PRIMARY KEY (ID),
202 KEY post_title (post_title(191)),
203 KEY post_status (post_status),
204 KEY post_name (post_name(191)),
205 KEY id_status (ID, post_status)
206 ) $collation";
207
208 $rs = $wpdb->query( $sql );
209
210 if ( $rs ) {
211 update_option( 'tb_learnpress_courses', 'yes' );
212 }
213 } catch ( Throwable $e ) {
214 error_log( $e->getMessage() );
215 }
216 }
217
218 /**
219 * Create table learnpress_material_files
220 *
221 * @since 4.2.6.6
222 */
223 private function create_table_learnpress_files() {
224 global $wpdb;
225
226 try {
227 $collation = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci';
228
229 $sql = "CREATE TABLE IF NOT EXISTS {$this->lp_db->tb_lp_files} (
230 file_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
231 file_name varchar(191) NOT NULL DEFAULT '',
232 file_type varchar(100) NOT NULL DEFAULT '',
233 item_id bigint(20) unsigned NOT NULL DEFAULT '0',
234 item_type varchar(100) NOT NULL DEFAULT '',
235 method varchar(10) NOT NULL DEFAULT 'upload',
236 file_path varchar(255) NOT NULL DEFAULT '',
237 orders int(4) NOT NULL DEFAULT '0',
238 created_at datetime NULL DEFAULT NULL,
239 PRIMARY KEY (file_id),
240 KEY file_name (file_name),
241 KEY item_id (item_id),
242 KEY item_type (item_type)
243 ) $collation";
244
245 $rs = $wpdb->query( $sql );
246
247 if ( $rs ) {
248 update_option( 'table_learnpress_files_created', 'yes' );
249 }
250 } catch ( Throwable $e ) {
251 error_log( $e->getMessage() );
252 }
253 }
254
255 /**
256 * Create table learnpress_mcp_api_keys.
257 *
258 * @since 4.3.3
259 * @return void
260 */
261 public function create_table_mcp_api_keys() {
262 global $wpdb;
263
264 try {
265 $collation = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci';
266
267 $sql = "CREATE TABLE IF NOT EXISTS {$this->lp_db->tb_lp_mcp_api_keys} (
268 key_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
269 user_id bigint(20) unsigned NOT NULL,
270 description varchar(200) NULL DEFAULT NULL,
271 permissions varchar(10) NOT NULL DEFAULT 'read',
272 consumer_key char(64) NOT NULL,
273 consumer_secret char(64) NOT NULL,
274 truncated_key char(7) NOT NULL,
275 last_access datetime NULL DEFAULT NULL,
276 call_count bigint(20) unsigned NOT NULL DEFAULT 0,
277 created_at datetime NOT NULL,
278 updated_at datetime NULL DEFAULT NULL,
279 PRIMARY KEY (key_id),
280 UNIQUE KEY consumer_key (consumer_key),
281 KEY user_id (user_id)
282 ) $collation";
283
284 $wpdb->query( $sql );
285 } catch ( Throwable $e ) {
286 error_log( $e->getMessage() );
287 }
288 }
289
290 /**
291 * Create default pages for LP
292 */
293 public static function create_pages() {
294 $pages = self::$_pages;
295
296 try {
297 foreach ( $pages as $page ) {
298 // Check if page has already existed
299 $page_id = get_option( "learn_press_{$page}_page_id", false );
300
301 if ( $page_id && get_post_type( $page_id ) === 'page' && get_post_status( $page_id ) == 'publish' ) {
302 continue;
303 }
304
305 if ( $page === 'courses' ) {
306 $page_title = 'Courses';
307 $page_slug = $page;
308 } elseif ( 'single_instructor' === $page ) {
309 $page_title = 'Instructor';
310 $page_slug = 'instructor';
311 } elseif ( 'instructors' === $page ) {
312 $page_title = 'Instructors';
313 $page_slug = $page;
314 } elseif ( 'become_a_teacher' === $page ) {
315 $page_title = 'Become an Instructor';
316 $page_slug = $page;
317 } elseif ( 'term_conditions' === $page ) {
318 $page_title = 'Terms and Conditions';
319 $page_slug = $page;
320 } else {
321 $page_title = ucwords( str_replace( '_', ' ', $page ) );
322 $page_slug = 'lp-' . str_replace( '_', '-', $page );
323 }
324
325 $data_create_page = array(
326 'post_title' => $page_title,
327 'post_name' => $page_slug,
328 );
329 LP_Helper::create_page( $data_create_page, "learn_press_{$page}_page_id" );
330 }
331
332 flush_rewrite_rules();
333 } catch ( Exception $ex ) {
334 error_log( $ex->getMessage() );
335 }
336 }
337
338 /**
339 * Check installed all tables required for LearnPress.
340 *
341 * @return bool
342 */
343 public function tables_install_done(): bool {
344 $install_done = get_option( 'learn_press_check_tables', 'no' );
345 if ( is_multisite() && 'yes' !== $install_done ) {
346 $this->create_tables();
347 }
348
349 return $install_done;
350 }
351 }
352 }
353