| @@ -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,63 @@ | ||
| 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 | + // 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 | + // Runtime migration for Webhooks table. | |
| 68 | + if ( ! LP_Settings::is_created_tb_webhooks() ) { | |
| 69 | + $this->create_table_webhooks(); | |
| 70 | + } | |
| 71 | + | |
| 72 | + // Set roles and capabilities. | |
| 73 | + learn_press_add_user_roles(); | |
| 74 | + | |
| 75 | + ini_set( 'max_execution_time', LearnPress::$time_limit_default_of_sever ); | |
| 29 | 76 | } |
| 30 | 77 | |
| 31 | 78 | /** |
| 32 | 79 | * Get instance |
| @@ -41,24 +88,13 @@ | ||
| 41 | 88 | return self::$instance; |
| 42 | 89 | } |
| 43 | 90 | |
| 44 | 91 | /** |
| 45 | - * Init action. | |
| 46 | - */ | |
| 47 | - public static function init() { | |
| 48 | - // add_action( 'learn-press/activate', array( __CLASS__, 'on_activate' ) ); | |
| 49 | - // add_action( 'admin_init', array( __CLASS__, 'do_install' ) ); | |
| 50 | - // add_action( 'admin_init', array( __CLASS__, 'subscription_button' ) ); | |
| 51 | - } | |
| 52 | - | |
| 53 | - /** | |
| 54 | 92 | * Do something after LP is activated. |
| 55 | 93 | * |
| 56 | 94 | * @since 4.0.0 |
| 57 | 95 | */ |
| 58 | 96 | public function on_activate() { |
| 59 | - // update_option( 'learn_press_status', 'activated' ); | |
| 60 | - | |
| 61 | 97 | $this->create_tables(); |
| 62 | 98 | |
| 63 | 99 | if ( ! self::tables_install_done() ) { |
| 64 | 100 | return; |
| @@ -79,193 +115,150 @@ | ||
| 79 | 115 | // flush_rewrite_rules(); |
| 80 | 116 | } |
| 81 | 117 | |
| 82 | 118 | // Force option users_can_register to ON. |
| 83 | - if ( ! get_option( 'users_can_register' ) ) { | |
| 119 | + /*if ( ! get_option( 'users_can_register' ) ) { | |
| 84 | 120 | update_option( 'users_can_register', 1 ); |
| 85 | - } | |
| 121 | + }*/ | |
| 86 | 122 | } |
| 87 | 123 | |
| 88 | 124 | /** |
| 89 | - * Check to run installer in the first-time LP installed. | |
| 90 | - * | |
| 91 | - * @since 3.x.x | |
| 125 | + * Create tables required for LP | |
| 92 | 126 | */ |
| 93 | - public static function do_install() { | |
| 94 | - if ( ! current_user_can( 'manage_options' ) ) { | |
| 95 | - return; | |
| 96 | - } | |
| 127 | + 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 | + } | |
| 97 | 133 | |
| 98 | - $status = get_option( 'learn_press_status' ); | |
| 134 | + if ( ! LP_Settings::is_created_tb_thim_cache() ) { | |
| 135 | + $this->create_table_thim_cache(); | |
| 136 | + } | |
| 99 | 137 | |
| 100 | - switch ( $status ) { | |
| 101 | - case 'activated': | |
| 102 | - self::install(); | |
| 103 | - update_option( 'learn_press_status', 'installed' ); | |
| 104 | - } | |
| 105 | - } | |
| 138 | + if ( ! LP_Settings::is_created_tb_material_files() ) { | |
| 139 | + $this->create_table_learnpress_files(); | |
| 140 | + } | |
| 106 | 141 | |
| 107 | - public static function subscription_button() { | |
| 108 | - // Only administrator of the site can do this | |
| 109 | - if ( ! current_user_can( 'administrator' ) ) { | |
| 110 | - return; | |
| 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 | + } | |
| 147 | + | |
| 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 | + } | |
| 153 | + | |
| 154 | + update_option( 'learn_press_check_tables', 'yes' ); | |
| 155 | + } catch ( Throwable $e ) { | |
| 156 | + error_log( $e->getMessage() ); | |
| 111 | 157 | } |
| 112 | - | |
| 113 | - LP_Admin_Notice::instance()->add( 'tools/subscription-button.php', '', true, 'newsletter-button' ); | |
| 114 | 158 | } |
| 115 | 159 | |
| 116 | 160 | /** |
| 117 | - * Run installation after LearnPress is activated. | |
| 161 | + * Create table thim_cache | |
| 118 | 162 | * |
| 119 | - * @depecated 4.1.6.4 | |
| 163 | + * @since 4.2.2 | |
| 120 | 164 | */ |
| 121 | - public static function install() { | |
| 122 | - _deprecated_function( __FUNCTION__, '4.1.6.4' ); | |
| 123 | - /*self::create_options(); | |
| 124 | - self::_create_pages(); | |
| 125 | - self::_create_cron_jobs(); | |
| 126 | - self::_delete_transients(); | |
| 127 | - //self::_create_log_path(); | |
| 128 | - self::_clear_backgrounds(); | |
| 165 | + private function create_table_thim_cache() { | |
| 166 | + global $wpdb; | |
| 129 | 167 | |
| 130 | - $current_version = get_option( 'learnpress_version', null ); | |
| 131 | - $current_db_version = get_option( 'learnpress_db_version', null ); | |
| 168 | + try { | |
| 169 | + $collation = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci'; | |
| 132 | 170 | |
| 133 | - // Fresh installation . | |
| 134 | - if ( is_null( $current_db_version ) ) { | |
| 135 | - update_option( 'learn_press_install', 'yes' ); | |
| 136 | - set_transient( 'lp_activation_redirect', 'yes', 60 ); | |
| 137 | - } | |
| 171 | + $sql = "CREATE TABLE IF NOT EXISTS {$this->lp_db->tb_thim_cache} ( | |
| 172 | + key_cache VARCHAR (191) NOT NULL UNIQUE, | |
| 173 | + value LONGTEXT NOT NULL, | |
| 174 | + expiration VARCHAR (191), | |
| 175 | + PRIMARY KEY (key_cache) | |
| 176 | + ) $collation"; | |
| 138 | 177 | |
| 139 | - // Force to show notice outdated template . | |
| 140 | - learn_press_delete_user_option( 'hide-notice-template-files' ); | |
| 178 | + $rs = $wpdb->query( $sql ); | |
| 141 | 179 | |
| 142 | - LP_Admin_Notice::instance()->remove_dismissed_notice( array( 'outdated-template' ) ); | |
| 143 | - | |
| 144 | - //if ( ! get_option( 'learnpress_db_version' ) ) { | |
| 145 | - update_option( 'learnpress_db_version', (int) LEARNPRESS_VERSION ); | |
| 146 | - //}*/ | |
| 180 | + if ( $rs ) { | |
| 181 | + update_option( 'thim_cache_tb_created', 'yes' ); | |
| 182 | + } | |
| 183 | + } catch ( Throwable $e ) { | |
| 184 | + error_log( $e->getMessage() ); | |
| 185 | + } | |
| 147 | 186 | } |
| 148 | 187 | |
| 149 | - protected static function _clear_backgrounds() { | |
| 150 | - global $wpdb; | |
| 151 | - | |
| 152 | - $query = $wpdb->prepare( | |
| 153 | - " | |
| 154 | - DELETE FROM {$wpdb->options} WHERE option_name LIKE %s | |
| 155 | - ", | |
| 156 | - $wpdb->esc_like( 'lp_schedule_items_batch_' ) . '%' | |
| 157 | - ); | |
| 158 | - $wpdb->query( $query ); | |
| 159 | - | |
| 160 | - $query = $wpdb->prepare( | |
| 161 | - " | |
| 162 | - DELETE FROM {$wpdb->options} WHERE option_name LIKE %s | |
| 163 | - ", | |
| 164 | - $wpdb->esc_like( 'lp_installer_batch_' ) . '%' | |
| 165 | - ); | |
| 166 | - $wpdb->query( $query ); | |
| 167 | - } | |
| 168 | - | |
| 169 | 188 | /** |
| 170 | - * Update default options for LP | |
| 189 | + * Create table learnpress_courses | |
| 171 | 190 | * |
| 172 | - * @editor tungnx - comment - not use on v4.1.6.6 | |
| 191 | + * @since 4.2.6.9 | |
| 173 | 192 | */ |
| 174 | - /*public static function create_options() { | |
| 175 | - $settings_classes = array( | |
| 176 | - 'LP_Settings_General' => include_once LP_PLUGIN_PATH . '/inc/admin/settings/class-lp-settings-general.php', | |
| 177 | - 'LP_Settings_Courses' => include_once LP_PLUGIN_PATH . '/inc/admin/settings/class-lp-settings-courses.php', | |
| 178 | - 'LP_Settings_Profile' => include_once LP_PLUGIN_PATH . '/inc/admin/settings/class-lp-settings-profile.php', | |
| 179 | - 'LP_Settings_Payments' => include_once LP_PLUGIN_PATH . '/inc/admin/settings/class-lp-settings-payments.php', | |
| 180 | - ); | |
| 193 | + public function create_table_courses() { | |
| 194 | + global $wpdb; | |
| 181 | 195 | |
| 182 | - ob_start(); | |
| 183 | - $str = array(); | |
| 196 | + try { | |
| 197 | + $collation = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci'; | |
| 184 | 198 | |
| 185 | - foreach ( $settings_classes as $c => $class ) { | |
| 186 | - if ( ! is_object( $class ) ) { | |
| 187 | - $class = @new $c(); | |
| 188 | - } | |
| 199 | + $sql = "CREATE TABLE IF NOT EXISTS {$this->lp_db->tb_lp_courses} ( | |
| 200 | + ID bigint(20) unsigned NOT NULL, | |
| 201 | + json LONGTEXT NOT NULL, | |
| 202 | + price_to_sort FLOAT, | |
| 203 | + is_sale int(1) default 0, | |
| 204 | + post_author bigint unsigned, | |
| 205 | + post_date_gmt datetime, | |
| 206 | + post_content LONGTEXT, | |
| 207 | + post_title text not null, | |
| 208 | + post_status varchar(20) default 'publish' not null, | |
| 209 | + post_name varchar(200) default '', | |
| 210 | + menu_order int default 0, | |
| 211 | + lang varchar(20), | |
| 212 | + PRIMARY KEY (ID), | |
| 213 | + KEY post_title (post_title(191)), | |
| 214 | + KEY post_status (post_status), | |
| 215 | + KEY post_name (post_name(191)), | |
| 216 | + KEY id_status (ID, post_status) | |
| 217 | + ) $collation"; | |
| 189 | 218 | |
| 190 | - $options = array(); | |
| 219 | + $rs = $wpdb->query( $sql ); | |
| 191 | 220 | |
| 192 | - if ( is_callable( array( $class, 'get_settings' ) ) ) { | |
| 193 | - $options = $class->get_settings( '', '' ); | |
| 221 | + if ( $rs ) { | |
| 222 | + update_option( 'tb_learnpress_courses', 'yes' ); | |
| 194 | 223 | } |
| 224 | + } catch ( Throwable $e ) { | |
| 225 | + error_log( $e->getMessage() ); | |
| 226 | + } | |
| 227 | + } | |
| 195 | 228 | |
| 196 | - if ( ! $options ) { | |
| 197 | - continue; | |
| 198 | - } | |
| 229 | + /** | |
| 230 | + * Create table learnpress_material_files | |
| 231 | + * | |
| 232 | + * @since 4.2.6.6 | |
| 233 | + */ | |
| 234 | + private function create_table_learnpress_files() { | |
| 235 | + global $wpdb; | |
| 199 | 236 | |
| 200 | - foreach ( $options as $option ) { | |
| 201 | - if ( ! isset( $option['id'] ) ) { | |
| 202 | - continue; | |
| 203 | - } | |
| 237 | + try { | |
| 238 | + $collation = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci'; | |
| 204 | 239 | |
| 205 | - $default_value = ''; | |
| 240 | + $sql = "CREATE TABLE IF NOT EXISTS {$this->lp_db->tb_lp_files} ( | |
| 241 | + file_id bigint(20) unsigned NOT NULL AUTO_INCREMENT, | |
| 242 | + file_name varchar(191) NOT NULL DEFAULT '', | |
| 243 | + file_type varchar(100) NOT NULL DEFAULT '', | |
| 244 | + item_id bigint(20) unsigned NOT NULL DEFAULT '0', | |
| 245 | + item_type varchar(100) NOT NULL DEFAULT '', | |
| 246 | + method varchar(10) NOT NULL DEFAULT 'upload', | |
| 247 | + file_path varchar(255) NOT NULL DEFAULT '', | |
| 248 | + orders int(4) NOT NULL DEFAULT '0', | |
| 249 | + created_at datetime NULL DEFAULT NULL, | |
| 250 | + PRIMARY KEY (file_id), | |
| 251 | + KEY file_name (file_name), | |
| 252 | + KEY item_id (item_id), | |
| 253 | + KEY item_type (item_type) | |
| 254 | + ) $collation"; | |
| 206 | 255 | |
| 207 | - if ( array_key_exists( 'default', $option ) ) { | |
| 208 | - $default_value = $option['default']; | |
| 209 | - } elseif ( array_key_exists( 'std', $option ) ) { | |
| 210 | - $default_value = $option['std']; | |
| 211 | - } | |
| 256 | + $rs = $wpdb->query( $sql ); | |
| 212 | 257 | |
| 213 | - if ( $default_value === '' || $default_value === null ) { | |
| 214 | - continue; | |
| 215 | - } | |
| 216 | - | |
| 217 | - if ( ! preg_match( '~^learn_press_~', $option['id'] ) ) { | |
| 218 | - $option_name = 'learn_press_' . $option['id']; | |
| 219 | - } else { | |
| 220 | - $option_name = $option['id']; | |
| 221 | - } | |
| 222 | - | |
| 223 | - // Don't update existing option | |
| 224 | - if ( false !== get_option( $option_name ) ) { | |
| 225 | - continue; | |
| 226 | - } | |
| 227 | - | |
| 228 | - // If option name doesn't like an array then update directly. | |
| 229 | - if ( ! preg_match( '/\[|\]/', $option_name ) ) { | |
| 230 | - update_option( $option_name, $default_value, 'yes' ); | |
| 231 | - continue; | |
| 232 | - } | |
| 233 | - | |
| 234 | - // Concat option as query string to parse it later | |
| 235 | - $value = maybe_serialize( $default_value ); | |
| 236 | - $str[] = "{$option_name}=$value"; | |
| 258 | + if ( $rs ) { | |
| 259 | + update_option( 'table_learnpress_files_created', 'yes' ); | |
| 237 | 260 | } |
| 238 | - } | |
| 239 | - | |
| 240 | - // Parse query string to get options | |
| 241 | - if ( $str ) { | |
| 242 | - $str = join( '&', $str ); | |
| 243 | - $options = array(); | |
| 244 | - parse_str( $str, $options ); | |
| 245 | - | |
| 246 | - if ( $options ) { | |
| 247 | - foreach ( $options as $name => $value ) { | |
| 248 | - $value = LP_Helper::maybe_unserialize( $value ); | |
| 249 | - update_option( $name, $value, 'yes' ); | |
| 250 | - } | |
| 251 | - } | |
| 252 | - } | |
| 253 | - | |
| 254 | - ob_get_clean(); | |
| 255 | - }*/ | |
| 256 | - | |
| 257 | - /** | |
| 258 | - * Create tables required for LP | |
| 259 | - */ | |
| 260 | - private function create_tables() { | |
| 261 | - try { | |
| 262 | - $tables = Config::instance()->get( 'tables-v4', 'table' ); | |
| 263 | - foreach ( $tables as $table ) { | |
| 264 | - LP_Database::getInstance()->wpdb->query( $table ); | |
| 265 | - } | |
| 266 | - | |
| 267 | - update_option( 'learn_press_check_tables', 'yes' ); | |
| 268 | 261 | } catch ( Throwable $e ) { |
| 269 | 262 | error_log( $e->getMessage() ); |
| 270 | 263 | } |
| 271 | 264 | } |
| @@ -270,167 +263,75 @@ | ||
| 270 | 263 | } |
| 271 | 264 | } |
| 272 | 265 | |
| 273 | 266 | /** |
| 274 | - * Delete transients | |
| 275 | - */ | |
| 276 | - private static function _delete_transients() { | |
| 277 | - global $wpdb; | |
| 278 | - $sql = " | |
| 279 | - DELETE a, b FROM $wpdb->options a, $wpdb->options b | |
| 280 | - WHERE a.option_name LIKE %s | |
| 281 | - AND a.option_name NOT LIKE %s | |
| 282 | - AND b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) ) | |
| 283 | - AND b.option_value < %d | |
| 284 | - "; | |
| 285 | - $wpdb->query( | |
| 286 | - $wpdb->prepare( | |
| 287 | - $sql, | |
| 288 | - $wpdb->esc_like( '_transient_' ) . '%', | |
| 289 | - $wpdb->esc_like( '_transient_timeout_' ) . '%', | |
| 290 | - time() | |
| 291 | - ) | |
| 292 | - ); | |
| 293 | - } | |
| 294 | - | |
| 295 | - /** | |
| 296 | - * Remove learnpress page if total of learn page > 10 | |
| 267 | + * Create table learnpress_mcp_api_keys. | |
| 297 | 268 | * |
| 298 | - * @return mixed | |
| 299 | - * @depecated 4.1.6.4 | |
| 269 | + * @since 4.3.3 | |
| 270 | + * @return void | |
| 300 | 271 | */ |
| 301 | - /*public static function _remove_pages() { | |
| 272 | + public function create_table_mcp_api_keys() { | |
| 302 | 273 | global $wpdb; |
| 303 | 274 | |
| 304 | - // Get all pages | |
| 305 | - $sql = $wpdb->prepare( | |
| 306 | - " | |
| 307 | - SELECT * | |
| 308 | - FROM {$wpdb->posts} p | |
| 309 | - INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id AND pm.meta_key= %s AND p.post_type = %s | |
| 310 | - ", | |
| 311 | - '_learn_press_page', | |
| 312 | - 'page' | |
| 313 | - ); | |
| 275 | + try { | |
| 276 | + $collation = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci'; | |
| 314 | 277 | |
| 315 | - $page_ids = $wpdb->get_col( $sql ); | |
| 278 | + $sql = "CREATE TABLE IF NOT EXISTS {$this->lp_db->tb_lp_mcp_api_keys} ( | |
| 279 | + key_id bigint(20) unsigned NOT NULL AUTO_INCREMENT, | |
| 280 | + user_id bigint(20) unsigned NOT NULL, | |
| 281 | + description varchar(200) NULL DEFAULT NULL, | |
| 282 | + permissions varchar(10) NOT NULL DEFAULT 'read', | |
| 283 | + consumer_key char(64) NOT NULL, | |
| 284 | + consumer_secret char(64) NOT NULL, | |
| 285 | + truncated_key char(7) NOT NULL, | |
| 286 | + last_access datetime NULL DEFAULT NULL, | |
| 287 | + call_count bigint(20) unsigned NOT NULL DEFAULT 0, | |
| 288 | + created_at datetime NOT NULL, | |
| 289 | + updated_at datetime NULL DEFAULT NULL, | |
| 290 | + PRIMARY KEY (key_id), | |
| 291 | + UNIQUE KEY consumer_key (consumer_key), | |
| 292 | + KEY user_id (user_id) | |
| 293 | + ) $collation"; | |
| 316 | 294 | |
| 317 | - if ( sizeof( $page_ids ) < 10 ) { | |
| 318 | - return $page_ids; | |
| 295 | + $wpdb->query( $sql ); | |
| 296 | + } catch ( Throwable $e ) { | |
| 297 | + error_log( $e->getMessage() ); | |
| 319 | 298 | } |
| 299 | + } | |
| 320 | 300 | |
| 321 | - // Delete pages | |
| 322 | - $query = $wpdb->prepare( | |
| 323 | - " | |
| 324 | - DELETE FROM p, pm | |
| 325 | - USING {$wpdb->posts} AS p LEFT JOIN {$wpdb->postmeta} AS pm ON p.ID = pm.post_id AND p.post_type = %s | |
| 326 | - WHERE p.post_status = %s | |
| 327 | - AND p.ID IN(" . implode( ',', $page_ids ) . ') | |
| 328 | - ', | |
| 329 | - 'page', | |
| 330 | - 'publish' | |
| 331 | - ); | |
| 332 | - | |
| 333 | - $wpdb->query( $query ); | |
| 334 | - | |
| 335 | - $pages = self::$_pages; | |
| 336 | - foreach ( $pages as $page ) { | |
| 337 | - delete_option( "learn_press_{$page}_page_id" ); | |
| 338 | - } | |
| 339 | - | |
| 340 | - return array(); | |
| 341 | - }*/ | |
| 342 | - | |
| 343 | 301 | /** |
| 344 | - * Create default pages for LP | |
| 302 | + * Create table learnpress_webhooks. | |
| 303 | + * | |
| 304 | + * @since 4.3.3 | |
| 305 | + * @return void | |
| 345 | 306 | */ |
| 346 | - /*public static function _create_pages() { | |
| 307 | + public function create_table_webhooks() { | |
| 347 | 308 | global $wpdb; |
| 348 | 309 | |
| 349 | - // Just delete duplicated pages | |
| 350 | - $created_page = self::_remove_pages(); | |
| 351 | - $pages = self::$_pages; | |
| 310 | + try { | |
| 311 | + $collation = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci'; | |
| 352 | 312 | |
| 353 | - foreach ( $pages as $page ) { | |
| 354 | - // If page already existed | |
| 355 | - $page_id = get_option( "learn_press_{$page}_page_id" ); | |
| 313 | + $sql = "CREATE TABLE IF NOT EXISTS {$this->lp_db->tb_lp_webhooks} ( | |
| 314 | + webhook_id bigint(20) unsigned NOT NULL AUTO_INCREMENT, | |
| 315 | + user_id bigint(20) unsigned NOT NULL DEFAULT 0, | |
| 316 | + status varchar(20) NOT NULL DEFAULT 'active', | |
| 317 | + name varchar(200) NOT NULL DEFAULT '', | |
| 318 | + delivery_url longtext NOT NULL, | |
| 319 | + secret text NOT NULL, | |
| 320 | + events longtext NOT NULL, | |
| 321 | + created_at datetime NOT NULL, | |
| 322 | + updated_at datetime NULL DEFAULT NULL, | |
| 323 | + PRIMARY KEY (webhook_id), | |
| 324 | + KEY user_id (user_id), | |
| 325 | + KEY status (status) | |
| 326 | + ) $collation"; | |
| 356 | 327 | |
| 357 | - if ( $page_id && get_post_type( $page_id ) == 'page' && get_post_status( $page_id ) == 'publish' ) { | |
| 358 | - continue; | |
| 359 | - } | |
| 360 | - | |
| 361 | - $page_id = self::_search_page( $page, $pages ); | |
| 362 | - | |
| 363 | - if ( ! $page_id ) { | |
| 364 | - // Check if page has already existed | |
| 365 | - switch ( $page ) { | |
| 366 | - case 'courses': | |
| 367 | - $_lpr_settings_pages = (array) get_option( '_lpr_settings_pages' ); | |
| 368 | - | |
| 369 | - if ( ! empty( $_lpr_settings_pages['general'] ) ) { | |
| 370 | - if ( ! empty( $_lpr_settings_pages['general']['courses_page_id'] ) ) { | |
| 371 | - $page_id = $_lpr_settings_pages['general']['courses_page_id']; | |
| 372 | - } | |
| 373 | - } | |
| 374 | - break; | |
| 375 | - case 'profile': | |
| 376 | - $_lpr_settings_general = (array) get_option( '_lpr_settings_general' ); | |
| 377 | - if ( ! empty( $_lpr_settings_general['set_page'] ) && $_lpr_settings_general['set_page'] == 'lpr_profile' ) { | |
| 378 | - $page_id = $wpdb->get_var( | |
| 379 | - $wpdb->prepare( | |
| 380 | - " | |
| 381 | - SELECT ID | |
| 382 | - FROM $wpdb->posts p | |
| 383 | - INNER JOIN $wpdb->postmeta pm ON p.ID = pm.post_id AND pm.meta_key = %s AND pm.meta_value = %d | |
| 384 | - ", | |
| 385 | - '_lpr_is_profile_page', | |
| 386 | - 1 | |
| 387 | - ) | |
| 388 | - ); | |
| 389 | - } | |
| 390 | - break; | |
| 391 | - } | |
| 392 | - | |
| 393 | - if ( ! $page_id ) { | |
| 394 | - if ( $page === 'courses' ) { | |
| 395 | - $page_title = 'All Courses'; | |
| 396 | - } else { | |
| 397 | - $page_title = ucwords( str_replace( '_', ' ', $page ) ); | |
| 398 | - } | |
| 399 | - | |
| 400 | - if ( $page === 'profile' ) { | |
| 401 | - $page_content = '<!-- wp:shortcode -->[' . apply_filters( 'learn-press/shortcode/profile/tag', 'learn_press_profile' ) . ']<!-- /wp:shortcode -->'; | |
| 402 | - } else { | |
| 403 | - $page_content = ''; | |
| 404 | - } | |
| 405 | - | |
| 406 | - $page_slug = 'lp-' . str_replace( '_', '-', $page ); | |
| 407 | - | |
| 408 | - $inserted = wp_insert_post( | |
| 409 | - array( | |
| 410 | - 'post_title' => $page_title, | |
| 411 | - 'post_name' => $page_slug, | |
| 412 | - 'post_status' => 'publish', | |
| 413 | - 'post_type' => 'page', | |
| 414 | - 'comment_status' => 'closed', | |
| 415 | - 'post_content' => isset( $page_content ) ? $page_content : '', | |
| 416 | - 'post_author' => get_current_user_id(), | |
| 417 | - ) | |
| 418 | - ); | |
| 419 | - if ( $inserted ) { | |
| 420 | - $page_id = $inserted; | |
| 421 | - } | |
| 422 | - } | |
| 423 | - } | |
| 424 | - if ( $page_id ) { | |
| 425 | - update_option( "learn_press_{$page}_page_id", $page_id ); | |
| 426 | - update_post_meta( $page_id, '_learn_press_page', $page ); | |
| 427 | - } | |
| 328 | + $wpdb->query( $sql ); | |
| 329 | + } catch ( Throwable $e ) { | |
| 330 | + error_log( $e->getMessage() ); | |
| 428 | 331 | } |
| 332 | + } | |
| 429 | 333 | |
| 430 | - flush_rewrite_rules(); | |
| 431 | - }*/ | |
| 432 | - | |
| 433 | 334 | /** |
| 434 | 335 | * Create default pages for LP |
| 435 | 336 | */ |
| 436 | 337 | public static function create_pages() { |
| @@ -440,43 +341,37 @@ | ||
| 440 | 341 | foreach ( $pages as $page ) { |
| 441 | 342 | // Check if page has already existed |
| 442 | 343 | $page_id = get_option( "learn_press_{$page}_page_id", false ); |
| 443 | 344 | |
| 444 | - if ( $page_id && get_post_type( $page_id ) == 'page' && get_post_status( $page_id ) == 'publish' ) { | |
| 345 | + if ( $page_id && get_post_type( $page_id ) === 'page' && get_post_status( $page_id ) == 'publish' ) { | |
| 445 | 346 | continue; |
| 446 | 347 | } |
| 447 | 348 | |
| 448 | - //$page_id = self::_search_page( $page, $pages ); | |
| 449 | - | |
| 450 | 349 | if ( $page === 'courses' ) { |
| 451 | - $page_title = 'All Courses'; | |
| 350 | + $page_title = 'Courses'; | |
| 452 | 351 | $page_slug = $page; |
| 352 | + } elseif ( 'single_instructor' === $page ) { | |
| 353 | + $page_title = 'Instructor'; | |
| 354 | + $page_slug = 'instructor'; | |
| 355 | + } elseif ( 'instructors' === $page ) { | |
| 356 | + $page_title = 'Instructors'; | |
| 357 | + $page_slug = $page; | |
| 358 | + } elseif ( 'become_a_teacher' === $page ) { | |
| 359 | + $page_title = 'Become an Instructor'; | |
| 360 | + $page_slug = $page; | |
| 361 | + } elseif ( 'term_conditions' === $page ) { | |
| 362 | + $page_title = 'Terms and Conditions'; | |
| 363 | + $page_slug = $page; | |
| 453 | 364 | } else { |
| 454 | 365 | $page_title = ucwords( str_replace( '_', ' ', $page ) ); |
| 455 | 366 | $page_slug = 'lp-' . str_replace( '_', '-', $page ); |
| 456 | 367 | } |
| 457 | 368 | |
| 458 | - if ( $page === 'profile' ) { | |
| 459 | - $page_content = '<!-- wp:shortcode -->[' . apply_filters( 'learn-press/shortcode/profile/tag', 'learn_press_profile' ) . ']<!-- /wp:shortcode -->'; | |
| 460 | - } else { | |
| 461 | - $page_content = ''; | |
| 462 | - } | |
| 463 | - | |
| 464 | - $page_id = wp_insert_post( | |
| 465 | - array( | |
| 466 | - 'post_title' => $page_title, | |
| 467 | - 'post_name' => $page_slug, | |
| 468 | - 'post_status' => 'publish', | |
| 469 | - 'post_type' => 'page', | |
| 470 | - 'comment_status' => 'closed', | |
| 471 | - 'post_content' => $page_content ?? '', | |
| 472 | - 'post_author' => get_current_user_id(), | |
| 473 | - ) | |
| 369 | + $data_create_page = array( | |
| 370 | + 'post_title' => $page_title, | |
| 371 | + 'post_name' => $page_slug, | |
| 474 | 372 | ); |
| 475 | - | |
| 476 | - if ( ! $page_id instanceof WP_Error ) { | |
| 477 | - update_option( "learn_press_{$page}_page_id", $page_id ); | |
| 478 | - } | |
| 373 | + LP_Helper::create_page( $data_create_page, "learn_press_{$page}_page_id" ); | |
| 479 | 374 | } |
| 480 | 375 | |
| 481 | 376 | flush_rewrite_rules(); |
| 482 | 377 | } catch ( Exception $ex ) { |
| @@ -481,62 +376,8 @@ | ||
| 481 | 376 | flush_rewrite_rules(); |
| 482 | 377 | } catch ( Exception $ex ) { |
| 483 | 378 | error_log( $ex->getMessage() ); |
| 484 | 379 | } |
| 485 | - } | |
| 486 | - | |
| 487 | - /** | |
| 488 | - * Search LP page to see if they are already created. | |
| 489 | - * | |
| 490 | - * @param $type | |
| 491 | - * @param $types | |
| 492 | - * | |
| 493 | - * @return int|mixed | |
| 494 | - * @depecated 4.1.6.4 | |
| 495 | - */ | |
| 496 | - /*protected static function _search_page( $type, $types ) { | |
| 497 | - static $pages = array(); | |
| 498 | - | |
| 499 | - if ( empty( $pages[ $type ] ) ) { | |
| 500 | - global $wpdb; | |
| 501 | - $in_types = array_fill( 0, sizeof( $types ), '%s' ); | |
| 502 | - $args = array( '_learn_press_page' ); | |
| 503 | - $args = array_merge( $args, $types ); | |
| 504 | - $args[] = 'publish'; | |
| 505 | - $query = $wpdb->prepare( | |
| 506 | - " | |
| 507 | - SELECT ID, pm.meta_value as type | |
| 508 | - FROM {$wpdb->posts} p | |
| 509 | - INNER JOIN {$wpdb->postmeta} pm ON pm.post_id = p.ID AND pm.meta_key = %s AND pm.meta_value IN(" . join( ',', | |
| 510 | - $in_types ) . ') | |
| 511 | - WHERE p.post_status = %s | |
| 512 | - ', | |
| 513 | - $args | |
| 514 | - ); | |
| 515 | - | |
| 516 | - if ( $rows = $wpdb->get_results( $query ) ) { | |
| 517 | - foreach ( $rows as $row ) { | |
| 518 | - $pages[ $row->type ] = $row->ID; | |
| 519 | - } | |
| 520 | - } | |
| 521 | - } | |
| 522 | - | |
| 523 | - $page_id = ! empty( $pages[ $type ] ) ? $pages[ $type ] : 0; | |
| 524 | - | |
| 525 | - return $page_id; | |
| 526 | - }*/ | |
| 527 | - | |
| 528 | - /** | |
| 529 | - * @depecated 4.1.6.4 | |
| 530 | - */ | |
| 531 | - private static function _create_cron_jobs() { | |
| 532 | - _deprecated_function( __FUNCTION__, '4.1.6.4' ); | |
| 533 | - wp_clear_scheduled_hook( 'learn_press_cleanup_sessions' ); | |
| 534 | - wp_schedule_event( | |
| 535 | - time(), | |
| 536 | - apply_filters( 'learn_press_cleanup_session_recurrence', 'twicedaily' ), | |
| 537 | - 'learn_press_cleanup_sessions' | |
| 538 | - ); | |
| 539 | 380 | } |
| 540 | 381 | |
| 541 | 382 | /** |
| 542 | 383 | * Check installed all tables required for LearnPress. |