| 1 |
<?php |
| 2 |
/** |
| 3 |
* Install and update functions. |
| 4 |
* |
| 5 |
* @author ThimPress |
| 6 |
* @package LearnPress/Classes |
| 7 |
* @version 3.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
use LP\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 |
/** |
| 22 |
* Default pages used by LP |
| 23 |
* |
| 24 |
* @var array |
| 25 |
*/ |
| 26 |
private static $_pages = array( 'checkout', 'profile', 'courses', 'become_a_teacher', 'term_conditions' ); |
| 27 |
|
| 28 |
protected function __construct() { |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Get instance |
| 33 |
* |
| 34 |
* @return LP_Install |
| 35 |
*/ |
| 36 |
public static function instance(): self { |
| 37 |
if ( ! self::$instance ) { |
| 38 |
self::$instance = new self(); |
| 39 |
} |
| 40 |
|
| 41 |
return self::$instance; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Do something after LP is activated. |
| 46 |
* |
| 47 |
* @since 4.0.0 |
| 48 |
*/ |
| 49 |
public function on_activate() { |
| 50 |
// update_option( 'learn_press_status', 'activated' ); |
| 51 |
|
| 52 |
$this->create_tables(); |
| 53 |
|
| 54 |
if ( ! self::tables_install_done() ) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
// Check if LP install before has db version |
| 59 |
if ( ! get_option( LP_KEY_DB_VERSION, false ) ) { |
| 60 |
// Save database version of LP |
| 61 |
update_option( LP_KEY_DB_VERSION, LearnPress::instance()->db_version ); |
| 62 |
} |
| 63 |
|
| 64 |
// Create pages default. |
| 65 |
self::create_pages(); |
| 66 |
|
| 67 |
// Set permalink is "Post name". |
| 68 |
if ( ! get_option( 'permalink_structure' ) ) { |
| 69 |
update_option( 'permalink_structure', '/%postname%/' ); |
| 70 |
// flush_rewrite_rules(); |
| 71 |
} |
| 72 |
|
| 73 |
// Force option users_can_register to ON. |
| 74 |
if ( ! get_option( 'users_can_register' ) ) { |
| 75 |
update_option( 'users_can_register', 1 ); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Check to run installer in the first-time LP installed. |
| 81 |
* |
| 82 |
* @since 3.x.x |
| 83 |
*/ |
| 84 |
public static function do_install() { |
| 85 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
$status = get_option( 'learn_press_status' ); |
| 90 |
|
| 91 |
switch ( $status ) { |
| 92 |
case 'activated': |
| 93 |
self::install(); |
| 94 |
update_option( 'learn_press_status', 'installed' ); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Run installation after LearnPress is activated. |
| 100 |
* |
| 101 |
* @depecated 4.1.6.4 |
| 102 |
*/ |
| 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(); |
| 111 |
|
| 112 |
$current_version = get_option( 'learnpress_version', null ); |
| 113 |
$current_db_version = get_option( 'learnpress_db_version', null ); |
| 114 |
|
| 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 ); |
| 119 |
} |
| 120 |
|
| 121 |
// Force to show notice outdated template . |
| 122 |
learn_press_delete_user_option( 'hide-notice-template-files' ); |
| 123 |
|
| 124 |
LP_Admin_Notice::instance()->remove_dismissed_notice( array( 'outdated-template' ) ); |
| 125 |
|
| 126 |
//if ( ! get_option( 'learnpress_db_version' ) ) { |
| 127 |
update_option( 'learnpress_db_version', (int) LEARNPRESS_VERSION ); |
| 128 |
//}*/ |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Create tables required for LP |
| 133 |
*/ |
| 134 |
private function create_tables() { |
| 135 |
try { |
| 136 |
$tables = Config::instance()->get( 'tables-v4', 'table' ); |
| 137 |
foreach ( $tables as $table ) { |
| 138 |
LP_Database::getInstance()->wpdb->query( $table ); |
| 139 |
} |
| 140 |
|
| 141 |
update_option( 'learn_press_check_tables', 'yes' ); |
| 142 |
} catch ( Throwable $e ) { |
| 143 |
error_log( $e->getMessage() ); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Create default pages for LP |
| 149 |
*/ |
| 150 |
public static function create_pages() { |
| 151 |
$pages = self::$_pages; |
| 152 |
|
| 153 |
try { |
| 154 |
foreach ( $pages as $page ) { |
| 155 |
// Check if page has already existed |
| 156 |
$page_id = get_option( "learn_press_{$page}_page_id", false ); |
| 157 |
|
| 158 |
if ( $page_id && get_post_type( $page_id ) == 'page' && get_post_status( $page_id ) == 'publish' ) { |
| 159 |
continue; |
| 160 |
} |
| 161 |
|
| 162 |
//$page_id = self::_search_page( $page, $pages ); |
| 163 |
|
| 164 |
if ( $page === 'courses' ) { |
| 165 |
$page_title = 'All Courses'; |
| 166 |
$page_slug = $page; |
| 167 |
} else { |
| 168 |
$page_title = ucwords( str_replace( '_', ' ', $page ) ); |
| 169 |
$page_slug = 'lp-' . str_replace( '_', '-', $page ); |
| 170 |
} |
| 171 |
|
| 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 |
) |
| 188 |
); |
| 189 |
|
| 190 |
if ( ! $page_id instanceof WP_Error ) { |
| 191 |
update_option( "learn_press_{$page}_page_id", $page_id ); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
flush_rewrite_rules(); |
| 196 |
} catch ( Exception $ex ) { |
| 197 |
error_log( $ex->getMessage() ); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Check installed all tables required for LearnPress. |
| 203 |
* |
| 204 |
* @return bool |
| 205 |
*/ |
| 206 |
public function tables_install_done(): bool { |
| 207 |
$install_done = get_option( 'learn_press_check_tables', 'no' ); |
| 208 |
if ( is_multisite() && 'yes' !== $install_done ) { |
| 209 |
$this->create_tables(); |
| 210 |
} |
| 211 |
|
| 212 |
return $install_done; |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
|