PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.8.0
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.8.0
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 4.8.0, at includes/Core/Install.php

256 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace WPDeveloper\BetterDocs\Core;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7
8
9 use WPDeveloper\BetterDocs\Utils\Base;
10 use WPDeveloper\BetterDocs\Utils\Database;
11 use WPDeveloper\BetterDocs\Dependencies\DI\Container;
12
13 class Install extends Base {
14 /**
15 * Container
16 * @var Container
17 */
18 public $container;
19 /**
20 * Database
21 * @var Database
22 */
23 public $database;
24
25 public function __construct( Container $container ) {
26 $this->container = $container;
27 $this->database = $this->container->get( Database::class );
28
29 register_activation_hook( BETTERDOCS_PLUGIN_FILE, [ $this, 'activate' ] );
30 register_deactivation_hook( BETTERDOCS_PLUGIN_FILE, [ $this, 'deactivate' ] );
31
32 //Update message for showing notice for new release
33 add_action( 'in_plugin_update_message-betterdocs/betterdocs.php', [ $this, 'plugin_update_message' ], 10, 2 );
34 add_action( 'init', [ $this, 'check_db_updates' ], 1 );
35 add_action( 'init', [ $this, 'check_version' ], 5 );
36 }
37
38 /**
39 * This is for upgrade message.
40 *
41 * @param mixed $plugin_data
42 * @param mixed $response
43 * @return void
44 */
45 public function plugin_update_message( $plugin_data, $response ) {
46 if ( isset( $response->upgrade_notice ) ) {
47 $new_version = $plugin_data['new_version'];
48 $current_version = betterdocs()->version;
49 $current_version_minor_part = explode( '.', $current_version )[1];
50 $new_version_minor_part = explode( '.', $new_version )[1];
51
52 betterdocs()->views->get(
53 'admin/notices/upgrade',
54 [
55 'response' => $response,
56 'plugin_data' => $plugin_data,
57 'major' => ! ( $current_version_minor_part === $new_version_minor_part )
58 ]
59 );
60 }
61 }
62
63 /**
64 * This method will run when version is updated.
65 * @return void
66 */
67 public function check_version() {
68 $betterdocs_version = get_option( 'betterdocs_version', '2.3.6' );
69 $betterdocs_code_version = betterdocs()->version;
70 $requires_update = version_compare( $betterdocs_version, $betterdocs_code_version, '<' );
71
72 if ( $requires_update ) {
73 /**
74 * This block of codes will execute
75 * if this plugin is activated via PRO PLUGIN.
76 */
77 if ( $this->database->get_transient( 'maybe_betterdocs_installed_by_pro' ) ) {
78 $this->activate();
79
80 $this->database->delete_transient( 'maybe_betterdocs_installed_by_pro' );
81 }
82
83 // Re-check if any setup needed.
84 $this->check_db_updates();
85
86 if ( get_option( 'betterdocs_version' ) && str_replace( '.', '', $betterdocs_code_version ) >= 370 ) {
87 $this->migrate_customizer();
88 }
89
90 // Re-check if any migration is needed.
91 $this->container->get( Migration::class )->init( str_replace( '.', '', $betterdocs_code_version ) );
92
93 // Updated current version number of plugin.
94 $this->update_version();
95 }
96 }
97
98 /**
99 * Update BetterDocs version to current.
100 */
101 private function update_version() {
102 update_option( 'betterdocs_version', betterdocs()->version );
103 }
104
105 /**
106 * This method will run when activation hit
107 * @return void
108 */
109 public function activate() {
110 // Create DB Tables if not created.
111 $this->check_db_updates();
112
113 // Set admin roles
114 $this->container->get( Roles::class )->setup();
115
116 // Save default settings.
117 // $this->container->get( Settings::class )->save_default_settings();
118
119 // Set redirect transient
120 if ( current_user_can( 'delete_users' ) ) {
121 $this->database->set_transient( 'betterdocs_maybe_redirect', true );
122 }
123
124 $this->database->set_transient( 'betterdocs_flush_rewrite_rules', true );
125 }
126
127 /**
128 * This method will run when deactivation hit.
129 * @return void
130 */
131 public function deactivate() {
132 // Flush all the existing rewrite rules
133 flush_rewrite_rules();
134
135 // Reset admin roles
136 $this->container->get( Roles::class )->setup( true );
137 }
138
139 /**
140 * This method responsible for setup db tables for the plugin
141 * @return void
142 */
143 public function check_db_updates() {
144 $_db_version = get_option( 'betterdocs_db_version', '1.0' );
145 $_db_code_version = betterdocs()->db_version;
146 $requires_update = version_compare( $_db_version, $_db_code_version, '<' );
147
148 if ( ! $requires_update ) {
149 return;
150 }
151
152 global $wpdb;
153
154 $charset_collate = $wpdb->get_charset_collate();
155
156 $search_keyword_table = $wpdb->prefix . 'betterdocs_search_keyword';
157 $search_keyword = "CREATE TABLE $search_keyword_table (
158 id bigint NOT NULL AUTO_INCREMENT,
159 keyword text NOT NULL,
160 PRIMARY KEY (id)
161 ) {$charset_collate};";
162
163 $search_log_table = $wpdb->prefix . 'betterdocs_search_log';
164 $search_log = "CREATE TABLE $search_log_table (
165 id bigint NOT NULL AUTO_INCREMENT,
166 keyword_id bigint NOT NULL,
167 count mediumint(9) NULL,
168 not_found_count mediumint(9) NULL,
169 created_at date DEFAULT '0000-00-00' NOT NULL,
170 PRIMARY KEY (id),
171 KEY keyword_id (keyword_id),
172 KEY created_at (created_at),
173 KEY keyword_created (keyword_id, created_at)
174 ) {$charset_collate};";
175
176 $_analytics_table = $wpdb->prefix . 'betterdocs_analytics';
177 $analytics_table = "CREATE TABLE $_analytics_table (
178 id bigint NOT NULL AUTO_INCREMENT,
179 post_id bigint DEFAULT 0 NOT NULL,
180 impressions bigint DEFAULT 0 NOT NULL,
181 unique_visit bigint DEFAULT 0 NOT NULL,
182 happy bigint DEFAULT 0 NOT NULL,
183 sad bigint DEFAULT 0 NOT NULL,
184 normal bigint DEFAULT 0 NOT NULL,
185 created_at date DEFAULT '0000-00-00' NOT NULL,
186 PRIMARY KEY (id),
187 KEY post_id (post_id),
188 KEY impressions (impressions),
189 KEY unique_visit (unique_visit),
190 KEY happy (happy),
191 KEY sad (sad),
192 KEY normal (normal),
193 KEY created_at (created_at),
194 UNIQUE KEY post_created (post_id, created_at)
195 ) {$charset_collate};";
196
197 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
198 dbDelta( $search_keyword . $search_log . $analytics_table );
199
200 // Update DB Version
201 update_option( 'betterdocs_db_version', $_db_code_version );
202 }
203
204 public function migrate_customizer() {
205 $search_layout = get_theme_mod( 'betterdocs_search_layout_select' );
206 if ( empty( $search_layout ) ) {
207 set_theme_mod( 'betterdocs_search_layout_select', 'layout-1' );
208 }
209
210 $search_heading = get_theme_mod( 'betterdocs_live_search_heading_switch' );
211 if ( empty( $search_heading ) ) {
212 set_theme_mod( 'betterdocs_live_search_heading_switch', false );
213 }
214
215 $docs_layout = get_theme_mod( 'betterdocs_docs_layout_select' );
216 if ( empty( $docs_layout ) ) {
217 set_theme_mod( 'betterdocs_docs_layout_select', 'layout-1' );
218 }
219
220 $single_layout = get_theme_mod( 'betterdocs_single_layout_select' );
221 if ( empty( $single_layout ) ) {
222 set_theme_mod( 'betterdocs_single_layout_select', 'layout-1' );
223 }
224
225 $category_archive_layout = get_theme_mod( 'betterdocs_archive_layout_select' );
226 if ( empty( $category_archive_layout ) ) {
227 set_theme_mod( 'betterdocs_archive_layout_select', 'layout-1' );
228 }
229
230 $search_layout_select = get_theme_mod( 'betterdocs_search_layout_select' );
231 if ( empty( $search_layout_select ) ) {
232 set_theme_mod( 'betterdocs_search_layout_select', 'layout-1' );
233 }
234
235 $docs_faq = get_theme_mod( 'betterdocs_select_faq_template' );
236 if ( empty( $docs_faq ) ) {
237 set_theme_mod( 'betterdocs_select_faq_template', 'layout-1' );
238 }
239
240 if ( get_option( 'betterdocs_settings' ) && betterdocs()->settings->get( 'enable_estimated_reading_time' ) == false ) {
241 betterdocs()->settings->save( 'enable_estimated_reading_time', false );
242 }
243
244 if ( function_exists( 'betterdocs_pro' ) ) {
245 $mkb_layout = get_theme_mod( 'betterdocs_multikb_layout_select' );
246 if ( empty( $mkb_layout ) ) {
247 set_theme_mod( 'betterdocs_multikb_layout_select', 'layout-1' );
248 }
249 $mkb_faq = get_theme_mod( 'betterdocs_select_faq_template_mkb' );
250 if ( empty( $mkb_faq ) ) {
251 set_theme_mod( 'betterdocs_select_faq_template_mkb', 'layout-1' );
252 }
253 }
254 }
255 }
256