| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDeveloper\BetterDocs\Core; |
| 4 |
|
| 5 |
use WPDeveloper\BetterDocs\Utils\Base; |
| 6 |
use WPDeveloper\BetterDocs\Utils\Database; |
| 7 |
use WPDeveloper\BetterDocs\Dependencies\DI\Container; |
| 8 |
|
| 9 |
class Install extends Base { |
| 10 |
/** |
| 11 |
* Container |
| 12 |
* @var Container |
| 13 |
*/ |
| 14 |
public $container; |
| 15 |
/** |
| 16 |
* Database |
| 17 |
* @var Database |
| 18 |
*/ |
| 19 |
public $database; |
| 20 |
|
| 21 |
public function __construct( Container $container ) { |
| 22 |
$this->container = $container; |
| 23 |
$this->database = $this->container->get( Database::class ); |
| 24 |
|
| 25 |
register_activation_hook( BETTERDOCS_PLUGIN_FILE, [$this, 'activate'] ); |
| 26 |
register_deactivation_hook( BETTERDOCS_PLUGIN_FILE, [$this, 'deactivate'] ); |
| 27 |
|
| 28 |
//Update message for showing notice for new release |
| 29 |
add_action( 'in_plugin_update_message-betterdocs/betterdocs.php', [$this, 'plugin_update_message'], 10, 2 ); |
| 30 |
add_action( 'init', [$this, 'check_db_updates'], 1 ); |
| 31 |
add_action( 'init', [$this, 'check_version'], 5 ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* This is for upgrade message. |
| 36 |
* |
| 37 |
* @param mixed $plugin_data |
| 38 |
* @param mixed $response |
| 39 |
* @return void |
| 40 |
*/ |
| 41 |
public function plugin_update_message( $plugin_data, $response ) { |
| 42 |
if ( isset( $response->upgrade_notice ) ) { |
| 43 |
$new_version = $plugin_data['new_version']; |
| 44 |
$current_version = betterdocs()->version; |
| 45 |
$current_version_minor_part = explode( '.', $current_version )[1]; |
| 46 |
$new_version_minor_part = explode( '.', $new_version )[1]; |
| 47 |
|
| 48 |
betterdocs()->views->get( 'admin/notices/upgrade', [ |
| 49 |
'response' => $response, |
| 50 |
'plugin_data' => $plugin_data, |
| 51 |
'major' => ! ( $current_version_minor_part === $new_version_minor_part ) |
| 52 |
] ); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* This method will run when version is updated. |
| 58 |
* @return void |
| 59 |
*/ |
| 60 |
public function check_version() { |
| 61 |
$betterdocs_version = get_option( 'betterdocs_version', '2.3.6' ); |
| 62 |
$betterdocs_code_version = betterdocs()->version; |
| 63 |
$requires_update = version_compare( $betterdocs_version, $betterdocs_code_version, '<' ); |
| 64 |
|
| 65 |
if ( $requires_update ) { |
| 66 |
/** |
| 67 |
* This block of codes will execute |
| 68 |
* if this plugin is activated via PRO PLUGIN. |
| 69 |
*/ |
| 70 |
if ( $this->database->get_transient( 'maybe_betterdocs_installed_by_pro' ) ) { |
| 71 |
$this->activate(); |
| 72 |
|
| 73 |
$this->database->delete_transient( 'maybe_betterdocs_installed_by_pro' ); |
| 74 |
} |
| 75 |
|
| 76 |
// Re-check if any setup needed. |
| 77 |
$this->check_db_updates(); |
| 78 |
|
| 79 |
// Re-check if any migration is needed. |
| 80 |
$this->container->get( Migration::class )->init( str_replace( '.', '', $betterdocs_code_version ) ); |
| 81 |
|
| 82 |
// Updated current version number of plugin. |
| 83 |
$this->update_version(); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Update BetterDocs version to current. |
| 89 |
*/ |
| 90 |
private function update_version() { |
| 91 |
update_option( 'betterdocs_version', betterdocs()->version ); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* This method will run when activation hit |
| 96 |
* @return void |
| 97 |
*/ |
| 98 |
public function activate() { |
| 99 |
// Create DB Tables if not created. |
| 100 |
$this->check_db_updates(); |
| 101 |
|
| 102 |
// Set admin roles |
| 103 |
$this->container->get( Roles::class )->setup(); |
| 104 |
|
| 105 |
// Save default settings. |
| 106 |
// $this->container->get( Settings::class )->save_default_settings(); |
| 107 |
|
| 108 |
// Set redirect transient |
| 109 |
if ( current_user_can( 'delete_users' ) ) { |
| 110 |
$this->database->set_transient( 'betterdocs_maybe_redirect', true ); |
| 111 |
} |
| 112 |
|
| 113 |
$this->database->set_transient( 'betterdocs_flush_rewrite_rules', true ); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* This method will run when deactivation hit. |
| 118 |
* @return void |
| 119 |
*/ |
| 120 |
public function deactivate() { |
| 121 |
// Flush all the existing rewrite rules |
| 122 |
flush_rewrite_rules(); |
| 123 |
|
| 124 |
// Reset admin roles |
| 125 |
$this->container->get( Roles::class )->setup( true ); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* This method responsible for setup db tables for the plugin |
| 130 |
* @return void |
| 131 |
*/ |
| 132 |
public function check_db_updates() { |
| 133 |
$_db_version = get_option( 'betterdocs_db_version', '1.0' ); |
| 134 |
$_db_code_version = betterdocs()->db_version; |
| 135 |
$requires_update = version_compare( $_db_version, $_db_code_version, '<' ); |
| 136 |
|
| 137 |
if ( ! $requires_update ) { |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
global $wpdb; |
| 142 |
|
| 143 |
$charset_collate = $wpdb->get_charset_collate(); |
| 144 |
|
| 145 |
$search_keyword_table = $wpdb->prefix . 'betterdocs_search_keyword'; |
| 146 |
$search_keyword = "CREATE TABLE $search_keyword_table ( |
| 147 |
id bigint NOT NULL AUTO_INCREMENT, |
| 148 |
keyword text NOT NULL, |
| 149 |
PRIMARY KEY (id) |
| 150 |
) {$charset_collate};"; |
| 151 |
|
| 152 |
$search_log_table = $wpdb->prefix . 'betterdocs_search_log'; |
| 153 |
$search_log = "CREATE TABLE $search_log_table ( |
| 154 |
id bigint NOT NULL AUTO_INCREMENT, |
| 155 |
keyword_id bigint NOT NULL, |
| 156 |
count mediumint(9) NULL, |
| 157 |
not_found_count mediumint(9) NULL, |
| 158 |
created_at date DEFAULT '0000-00-00' NOT NULL, |
| 159 |
PRIMARY KEY (id), |
| 160 |
KEY keyword_id (keyword_id), |
| 161 |
KEY created_at (created_at) |
| 162 |
) {$charset_collate};"; |
| 163 |
|
| 164 |
$_analytics_table = $wpdb->prefix . 'betterdocs_analytics'; |
| 165 |
$analytics_table = "CREATE TABLE $_analytics_table ( |
| 166 |
id bigint NOT NULL AUTO_INCREMENT, |
| 167 |
post_id bigint DEFAULT 0 NOT NULL, |
| 168 |
impressions bigint DEFAULT 0 NOT NULL, |
| 169 |
unique_visit bigint DEFAULT 0 NOT NULL, |
| 170 |
happy bigint DEFAULT 0 NOT NULL, |
| 171 |
sad bigint DEFAULT 0 NOT NULL, |
| 172 |
normal bigint DEFAULT 0 NOT NULL, |
| 173 |
created_at date DEFAULT '0000-00-00' NOT NULL, |
| 174 |
PRIMARY KEY (id), |
| 175 |
KEY post_id (post_id), |
| 176 |
KEY impressions (impressions), |
| 177 |
KEY unique_visit (unique_visit), |
| 178 |
KEY happy (happy), |
| 179 |
KEY sad (sad), |
| 180 |
KEY normal (normal), |
| 181 |
KEY created_at (created_at) |
| 182 |
) {$charset_collate};"; |
| 183 |
|
| 184 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 185 |
dbDelta( $search_keyword . $search_log . $analytics_table ); |
| 186 |
|
| 187 |
// Update DB Version |
| 188 |
update_option( 'betterdocs_db_version', $_db_code_version ); |
| 189 |
} |
| 190 |
} |
| 191 |
|