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

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