PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 3.7.1
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v3.7.1
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / Core / Install.php

Install.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 3.7.1, at includes/Core/Install.php

247 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 if ( get_option( 'betterdocs_version' ) && str_replace( '.', '', $betterdocs_code_version ) >= 370 ) {
80 $this->migrate_customizer();
81 }
82
83 // Re-check if any migration is needed.
84 $this->container->get( Migration::class )->init( str_replace( '.', '', $betterdocs_code_version ) );
85
86 // Updated current version number of plugin.
87 $this->update_version();
88 }
89 }
90
91 /**
92 * Update BetterDocs version to current.
93 */
94 private function update_version() {
95 update_option( 'betterdocs_version', betterdocs()->version );
96 }
97
98 /**
99 * This method will run when activation hit
100 * @return void
101 */
102 public function activate() {
103 // Create DB Tables if not created.
104 $this->check_db_updates();
105
106 // Set admin roles
107 $this->container->get( Roles::class )->setup();
108
109 // Save default settings.
110 // $this->container->get( Settings::class )->save_default_settings();
111
112 // Set redirect transient
113 if ( current_user_can( 'delete_users' ) ) {
114 $this->database->set_transient( 'betterdocs_maybe_redirect', true );
115 }
116
117 $this->database->set_transient( 'betterdocs_flush_rewrite_rules', true );
118 }
119
120 /**
121 * This method will run when deactivation hit.
122 * @return void
123 */
124 public function deactivate() {
125 // Flush all the existing rewrite rules
126 flush_rewrite_rules();
127
128 // Reset admin roles
129 $this->container->get( Roles::class )->setup( true );
130 }
131
132 /**
133 * This method responsible for setup db tables for the plugin
134 * @return void
135 */
136 public function check_db_updates() {
137 $_db_version = get_option( 'betterdocs_db_version', '1.0' );
138 $_db_code_version = betterdocs()->db_version;
139 $requires_update = version_compare( $_db_version, $_db_code_version, '<' );
140
141 if ( ! $requires_update ) {
142 return;
143 }
144
145 global $wpdb;
146
147 $charset_collate = $wpdb->get_charset_collate();
148
149 $search_keyword_table = $wpdb->prefix . 'betterdocs_search_keyword';
150 $search_keyword = "CREATE TABLE $search_keyword_table (
151 id bigint NOT NULL AUTO_INCREMENT,
152 keyword text NOT NULL,
153 PRIMARY KEY (id)
154 ) {$charset_collate};";
155
156 $search_log_table = $wpdb->prefix . 'betterdocs_search_log';
157 $search_log = "CREATE TABLE $search_log_table (
158 id bigint NOT NULL AUTO_INCREMENT,
159 keyword_id bigint NOT NULL,
160 count mediumint(9) NULL,
161 not_found_count mediumint(9) NULL,
162 created_at date DEFAULT '0000-00-00' NOT NULL,
163 PRIMARY KEY (id),
164 KEY keyword_id (keyword_id),
165 KEY created_at (created_at)
166 ) {$charset_collate};";
167
168 $_analytics_table = $wpdb->prefix . 'betterdocs_analytics';
169 $analytics_table = "CREATE TABLE $_analytics_table (
170 id bigint NOT NULL AUTO_INCREMENT,
171 post_id bigint DEFAULT 0 NOT NULL,
172 impressions bigint DEFAULT 0 NOT NULL,
173 unique_visit bigint DEFAULT 0 NOT NULL,
174 happy bigint DEFAULT 0 NOT NULL,
175 sad bigint DEFAULT 0 NOT NULL,
176 normal bigint DEFAULT 0 NOT NULL,
177 created_at date DEFAULT '0000-00-00' NOT NULL,
178 PRIMARY KEY (id),
179 KEY post_id (post_id),
180 KEY impressions (impressions),
181 KEY unique_visit (unique_visit),
182 KEY happy (happy),
183 KEY sad (sad),
184 KEY normal (normal),
185 KEY created_at (created_at)
186 ) {$charset_collate};";
187
188 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
189 dbDelta( $search_keyword . $search_log . $analytics_table );
190
191 // Update DB Version
192 update_option( 'betterdocs_db_version', $_db_code_version );
193 }
194
195 public function migrate_customizer() {
196 $search_layout = get_theme_mod( 'betterdocs_search_layout_select' );
197 if ( empty( $search_layout ) ) {
198 set_theme_mod( 'betterdocs_search_layout_select', 'layout-1' );
199 }
200
201 $search_heading = get_theme_mod( 'betterdocs_live_search_heading_switch' );
202 if ( empty( $search_heading ) ) {
203 set_theme_mod( 'betterdocs_live_search_heading_switch', false );
204 }
205
206 $docs_layout = get_theme_mod( 'betterdocs_docs_layout_select' );
207 if ( empty( $docs_layout ) ) {
208 set_theme_mod( 'betterdocs_docs_layout_select', 'layout-1' );
209 }
210
211 $single_layout = get_theme_mod( 'betterdocs_single_layout_select' );
212 if ( empty( $single_layout ) ) {
213 set_theme_mod( 'betterdocs_single_layout_select', 'layout-1' );
214 }
215
216 $category_archive_layout = get_theme_mod( 'betterdocs_archive_layout_select' );
217 if ( empty( $category_archive_layout ) ) {
218 set_theme_mod( 'betterdocs_archive_layout_select', 'layout-1' );
219 }
220
221 $search_layout_select = get_theme_mod( 'betterdocs_search_layout_select' );
222 if ( empty( $search_layout_select ) ) {
223 set_theme_mod( 'betterdocs_search_layout_select', 'layout-1' );
224 }
225
226 $docs_faq = get_theme_mod( 'betterdocs_select_faq_template' );
227 if ( empty( $docs_faq ) ) {
228 set_theme_mod( 'betterdocs_select_faq_template', 'layout-1' );
229 }
230
231 if ( get_options('betterdocs_settings') && betterdocs()->settings->get( 'enable_estimated_reading_time' ) == false ) {
232 betterdocs()->settings->save( 'enable_estimated_reading_time', false );
233 }
234
235 if ( function_exists( 'betterdocs_pro' ) ) {
236 $mkb_layout = get_theme_mod( 'betterdocs_multikb_layout_select' );
237 if ( empty( $mkb_layout ) ) {
238 set_theme_mod( 'betterdocs_multikb_layout_select', 'layout-1' );
239 }
240 $mkb_faq = get_theme_mod( 'betterdocs_select_faq_template_mkb' );
241 if ( empty( $mkb_faq ) ) {
242 set_theme_mod( 'betterdocs_select_faq_template_mkb', 'layout-1' );
243 }
244 }
245 }
246 }
247