| 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 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 |
$this->create_tables(); |
| 51 |
|
| 52 |
if ( ! self::tables_install_done() ) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
// Check if LP install before has db version |
| 57 |
if ( ! get_option( LP_KEY_DB_VERSION, false ) ) { |
| 58 |
// Save database version of LP |
| 59 |
update_option( LP_KEY_DB_VERSION, LearnPress::instance()->db_version ); |
| 60 |
} |
| 61 |
|
| 62 |
// Create pages default. |
| 63 |
self::create_pages(); |
| 64 |
|
| 65 |
// Set permalink is "Post name". |
| 66 |
if ( ! get_option( 'permalink_structure' ) ) { |
| 67 |
update_option( 'permalink_structure', '/%postname%/' ); |
| 68 |
// flush_rewrite_rules(); |
| 69 |
} |
| 70 |
|
| 71 |
// Force option users_can_register to ON. |
| 72 |
if ( ! get_option( 'users_can_register' ) ) { |
| 73 |
update_option( 'users_can_register', 1 ); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Create tables required for LP |
| 79 |
*/ |
| 80 |
private function create_tables() { |
| 81 |
try { |
| 82 |
$tables = Config::instance()->get( 'tables-v4', 'table' ); |
| 83 |
foreach ( $tables as $table ) { |
| 84 |
LP_Database::getInstance()->wpdb->query( $table ); |
| 85 |
} |
| 86 |
|
| 87 |
update_option( 'learn_press_check_tables', 'yes' ); |
| 88 |
} catch ( Throwable $e ) { |
| 89 |
error_log( $e->getMessage() ); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Create default pages for LP |
| 95 |
*/ |
| 96 |
public static function create_pages() { |
| 97 |
$pages = self::$_pages; |
| 98 |
|
| 99 |
try { |
| 100 |
foreach ( $pages as $page ) { |
| 101 |
// Check if page has already existed |
| 102 |
$page_id = get_option( "learn_press_{$page}_page_id", false ); |
| 103 |
|
| 104 |
if ( $page_id && get_post_type( $page_id ) == 'page' && get_post_status( $page_id ) == 'publish' ) { |
| 105 |
continue; |
| 106 |
} |
| 107 |
|
| 108 |
//$page_id = self::_search_page( $page, $pages ); |
| 109 |
|
| 110 |
if ( $page === 'courses' ) { |
| 111 |
$page_title = 'All Courses'; |
| 112 |
$page_slug = $page; |
| 113 |
} else { |
| 114 |
$page_title = ucwords( str_replace( '_', ' ', $page ) ); |
| 115 |
$page_slug = 'lp-' . str_replace( '_', '-', $page ); |
| 116 |
} |
| 117 |
|
| 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 |
) |
| 134 |
); |
| 135 |
|
| 136 |
if ( ! $page_id instanceof WP_Error ) { |
| 137 |
update_option( "learn_press_{$page}_page_id", $page_id ); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
flush_rewrite_rules(); |
| 142 |
} catch ( Exception $ex ) { |
| 143 |
error_log( $ex->getMessage() ); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Check installed all tables required for LearnPress. |
| 149 |
* |
| 150 |
* @return bool |
| 151 |
*/ |
| 152 |
public function tables_install_done(): bool { |
| 153 |
$install_done = get_option( 'learn_press_check_tables', 'no' ); |
| 154 |
if ( is_multisite() && 'yes' !== $install_done ) { |
| 155 |
$this->create_tables(); |
| 156 |
} |
| 157 |
|
| 158 |
return $install_done; |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|