PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 3.5.3
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v3.5.3
4.9.2 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 All 200 releases
← All changes | includes/Core/KBMigration.php +462 -511 4.9.23.5.3 View file →
@@ -1,559 +1,510 @@
1 1 <?php
2 +
2 3 namespace WPDeveloper\BetterDocs\Core;
3 4
4 -if ( ! defined( 'ABSPATH' ) ) {
5 - exit;
6 -}
7 -
8 -
9 -use WP_Query;
10 5 use WPDeveloper\BetterDocs\Utils\Base;
11 6 use WPDeveloper\BetterDocs\Utils\Helper;
12 7 use WPDeveloper\BetterDocs\Admin\Builder\Rules;
13 8
14 9 class KBMigration extends Base {
15 - public $existing_plugins;
16 - public $migrated_plugins;
17 - public $active = 'helpscout';
10 + public $existing_plugins;
11 + public $migrated_plugins;
12 + public $active = 'helpscout';
18 13
19 - public function __construct() {
20 - $this->existing_plugins = $this->knowledge_base_plugins();
21 - $this->migrated_plugins = $this->get_migrated_plugins();
14 + public function __construct() {
15 + $this->existing_plugins = $this->knowledge_base_plugins();
16 + $this->migrated_plugins = $this->get_migrated_plugins();
22 17
23 - add_filter( 'betterdocs_settings_tabs', [ $this, 'migration_settings' ], 10, 1 );
24 - if ( ! $this->existing_plugins || ( $this->existing_plugins && in_array( $this->existing_plugins[0][0], $this->migrated_plugins ) ) ) {
25 - return;
26 - }
27 - $this->active = 'kb-migration';
28 - //add_filter( 'admin_notices', [ $this, 'migration_notice' ], 10, 1 );
29 - add_filter( 'betterdocs_migration_tab_sections', [ $this, 'kb_migration_settings' ], 10, 1 );
30 - }
18 + add_filter( 'betterdocs_settings_tabs', [ $this, 'migration_settings' ], 10, 1 );
19 + if ( ! $this->existing_plugins || ( $this->existing_plugins && in_array( $this->existing_plugins[0][0], $this->migrated_plugins )) ) {
20 + return;
21 + }
22 + $this->active = 'kb-migration';
23 + //add_filter( 'admin_notices', [ $this, 'migration_notice' ], 10, 1 );
24 + add_filter( 'betterdocs_migration_tab_sections', [ $this, 'kb_migration_settings' ], 10, 1 );
25 + }
31 26
32 - public function migration_notice() {
33 - $screen = get_current_screen()->id;
34 - if ( $screen === 'betterdocs_page_betterdocs-setup' ) {
35 - return;
36 - }
37 - ?>
38 - <div class="notice notice-success is-dismissible">
39 - <?php
40 - printf(
41 - '<p>%s<a class="button button-primary betterdocs-migration-notice" href="%s">%s</a><a class="button" href="#">%s</a><a class="button" href="#">%s</a></p>',
42 - esc_html(
43 - sprintf(
44 - /* translators: %1$s and %2$s are the name of the existing plugin. */
45 - __(
46 - 'Whoops! You are already using %1$s on your website. To migrate your %2$s data to BetterDocs, click here ',
47 - 'betterdocs'
48 - ),
49 - '<strong>' . esc_html( $this->existing_plugins[0][1] ) . '</strong>',
50 - '<strong>' . esc_html( $this->existing_plugins[0][1] ) . '</strong>'
51 - )
52 - ),
53 - esc_url( admin_url( 'admin.php?page=betterdocs-settings#migration' ) ),
54 - esc_html__( 'Start Migration', 'betterdocs' ),
55 - esc_html__( 'Maybe Later', 'betterdocs' ),
56 - esc_html__( 'Never Show Again', 'betterdocs' )
57 - );
58 - ?>
59 - </div>
60 - <?php
61 - }
27 + public function migration_notice() {
28 + $screen = get_current_screen()->id;
29 + if ( $screen === 'betterdocs_page_betterdocs-setup' ) {
30 + return;
31 + }
32 + ?>
33 + <div class="notice notice-success is-dismissible">
34 + <?php
35 + printf(
36 + '<p>%s<a class="button button-primary betterdocs-migration-notice" href="%s">%s</a><a class="button" href="">Maybe Later</a><a class="button" href="">Never Show Again</a></p>',
37 + __(
38 + sprintf(
39 + 'Whoops! You are already using %s on your website. To migrate your %s data to BetterDocs, click here ',
40 + '<strong>'. esc_html($this->existing_plugins[0][1]) .'</strong>',
41 + '<strong>'. esc_html($this->existing_plugins[0][1]) .'</strong>'
42 + ),
43 + 'betterdocs'
44 + ),
45 + esc_url(admin_url('admin.php?page=betterdocs-settings&tab=tab-migration')),
46 + esc_html__('Start Migration', 'betterdocs')
47 + );
48 + ?>
49 + </div>
50 + <?php
51 + }
62 52
53 + public function migration_settings( $args ) {
54 + /**
55 + * License Tab
56 + */
57 + $args['tab-migration'] = apply_filters( 'betterdocs_settings_tab_migration', [
58 + 'id' => 'tab-migration',
59 + 'name' => 'tab-migration',
60 + 'classes' => 'tab-migration',
61 + 'label' => __( 'Migration', 'betterdocs' ),
62 + 'priority' => 80,
63 + 'fields' => [
64 + 'sections-migration' => [
65 + 'name' => 'sections-migration',
66 + 'type' => 'section',
67 + 'label' => __( 'Migration', 'betterdocs' ),
68 + 'priority' => 30,
69 + 'fields' => [
70 + 'all-migration' => [
71 + 'id' => 'all-tab-migration',
72 + 'name' => 'all-tab-migration',
73 + 'label' => __( 'Migration Settings', 'betterdocs' ),
74 + 'classes' => 'tab-layout',
75 + 'type' => "tab",
76 + 'active' => $this->active,
77 + 'completionTrack' => true,
78 + 'sidebar' => false,
79 + 'save' => false,
80 + 'title' => false,
81 + 'config' => [
82 + 'active' => $this->active,
83 + 'sidebar' => false,
84 + 'title' => false
85 + ],
86 + 'submit' => [
87 + 'show' => false
88 + ],
89 + 'step' => [
90 + 'show' => false
91 + ],
92 + 'priority' => 20,
93 + 'fields' => apply_filters( 'betterdocs_migration_tab_sections', [
94 + 'helpscout' => [
95 + 'id' => 'helpscout',
96 + 'name' => 'helpscout',
97 + 'type' => 'section',
98 + 'label' => __( 'Help Scout', 'betterdocs' ),
99 + 'priority' => 2,
100 + 'fields' => [
101 + 'helpscout_api_key' => [
102 + 'name' => 'helpscout_api_key',
103 + 'type' => 'text',
104 + 'label' => __('Docs API Key', 'betterdocs'),
105 + 'label_subtitle' => __('Check out this <a target="_blank" href="'.esc_url('https://betterdocs.co/docs/help-scout-migration-feature-with-betterdocs/').'">documentation</a> to retrieve your Help Scout Docs API Key', 'betterdocs'),
106 + 'default' => '',
107 + 'priority' => 1,
108 + ],
109 + 'helpscout_collection_id' => [
110 + 'name' => 'helpscout_collection_id',
111 + 'type' => 'text',
112 + 'label' => __('Collection ID', 'betterdocs'),
113 + 'label_subtitle' => __('Check out this <a target="_blank" href="'.esc_url('https://betterdocs.co/docs/help-scout-migration-feature-with-betterdocs/').'">documentation</a> to retrieve your Help Scout Collection ID', 'betterdocs'),
114 + 'default' => '',
115 + 'priority' => 2,
116 + ],
117 + 'helpscout_action' => [
118 + 'name' => 'helpscout_action',
119 + 'text' => [
120 + 'normal' => __('Migrate Help Scout Docs', 'betterdocs'),
121 + 'saved' => __('Migrated', 'betterdocs'),
122 + 'loading' => __('Migrating...', 'betterdocs'),
123 + ],
124 + 'type' => 'button',
125 + 'priority' => 3,
126 + 'ajax' => [
127 + 'on' => 'click',
128 + 'api' => '/betterdocs/v1/helpscout-migration',
129 + 'data' => [
130 + 'helpscout_api_key' => '@helpscout_api_key',
131 + 'helpscout_collection_id' => '@helpscout_collection_id'
132 + ],
133 + 'swal' => [
134 + 'text' => __( 'Help Scout Docs are successfully migrated to your site.', 'betterdocs' ),
135 + 'icon' => 'success',
136 + 'autoClose' => 2000
137 + ]
138 + ]
139 + ]
140 + ]
141 + ]
142 + ])
143 + ]
144 + ]
145 + ]
146 + ]
147 + ] );
63 148
64 - public function migration_settings( $args ) {
65 - /**
66 - * License Tab
67 - */
68 - $args['tab-migration'] = apply_filters(
69 - 'betterdocs_settings_tab_migration',
70 - [
71 - 'id' => 'tab-migration',
72 - 'name' => 'tab-migration',
73 - 'classes' => 'tab-migration',
74 - 'label' => __( 'Migration', 'betterdocs' ),
75 - 'priority' => 80,
76 - 'fields' => [
77 - 'sections-migration' => [
78 - 'name' => 'sections-migration',
79 - 'type' => 'section',
80 - 'label' => __( 'Migration', 'betterdocs' ),
81 - 'priority' => 30,
82 - 'fields' => [
83 - 'all-migration' => [
84 - 'id' => 'all-tab-migration',
85 - 'name' => 'all-tab-migration',
86 - 'label' => __( 'Migration Settings', 'betterdocs' ),
87 - 'classes' => 'tab-layout',
88 - 'type' => 'tab',
89 - 'active' => $this->active,
90 - 'completionTrack' => true,
91 - 'sidebar' => false,
92 - 'save' => false,
93 - 'title' => false,
94 - 'config' => [
95 - 'active' => $this->active,
96 - 'sidebar' => false,
97 - 'title' => false
98 - ],
99 - 'submit' => [
100 - 'show' => false
101 - ],
102 - 'step' => [
103 - 'show' => false
104 - ],
105 - 'priority' => 20,
106 - 'fields' => apply_filters(
107 - 'betterdocs_migration_tab_sections',
108 - [
109 - 'helpscout' => [
110 - 'id' => 'helpscout',
111 - 'name' => 'helpscout',
112 - 'type' => 'section',
113 - 'label' => __( 'Help Scout', 'betterdocs' ),
114 - 'priority' => 2,
115 - 'fields' => [
116 - 'helpscout_api_key' => [
117 - 'name' => 'helpscout_api_key',
118 - 'type' => 'text',
119 - 'label' => __( 'Docs API Key', 'betterdocs' ),
120 - 'label_subtitle' => sprintf(
121 - /* translators: %s is a URL to the Help Scout migration documentation. */
122 - __( 'Check out this <a target="_blank" href="%s">documentation</a> to retrieve your Help Scout Docs API Key.', 'betterdocs' ),
123 - esc_url( 'https://betterdocs.co/docs/help-scout-migration-feature-with-betterdocs/' )
124 - ),
125 - 'default' => '',
126 - 'priority' => 1,
127 - ],
128 - 'helpscout_collection_id' => [
129 - 'name' => 'helpscout_collection_id',
130 - 'type' => 'text',
131 - 'label' => __( 'Collection ID', 'betterdocs' ),
132 - 'label_subtitle' => sprintf(
133 - /* translators: %s is a URL to the Help Scout migration documentation. */
134 - __( 'Check out this <a target="_blank" href="%s">documentation</a> to retrieve your Help Scout Collection ID.', 'betterdocs' ),
135 - esc_url( 'https://betterdocs.co/docs/help-scout-migration-feature-with-betterdocs/' )
136 - ),
137 - 'default' => '',
138 - 'priority' => 2,
139 - ],
140 - 'helpscout_action' => apply_filters(
141 - 'betterdocs_helpscout_migration_action',
142 - [
143 - 'name' => 'helpscout_action',
144 - 'text' => [
145 - 'normal' => __( 'Migrate Help Scout Docs', 'betterdocs' ),
146 - 'saved' => __( 'Migrate Help Scout Docs', 'betterdocs' ),
147 - 'loading' => __( 'Migrating...', 'betterdocs' ),
148 - ],
149 - 'type' => 'button',
150 - 'priority' => 3,
151 - 'ajax' => [
152 - 'on' => 'click',
153 - 'api' => '/betterdocs/v1/helpscout-migration',
154 - 'data' => [
155 - 'helpscout_api_key' => '@helpscout_api_key',
156 - 'helpscout_collection_id' => '@helpscout_collection_id'
157 - ],
158 - 'swal' => [
159 - 'text' => __( 'Migration process has started in the background.', 'betterdocs' ),
160 - 'icon' => 'success',
161 - 'autoClose' => 2000
162 - ]
163 - ]
164 - ]
165 - )
166 - ]
167 - ]
168 - ]
169 - )
170 - ]
171 - ]
172 - ]
173 - ]
174 - ]
175 - );
149 + return $args;
150 + }
176 151
177 - return $args;
178 - }
152 + public function kb_migration_settings( $args ) {
153 + /**
154 + * License Tab
155 + */
156 + $args['kb-migration'] = [
157 + 'id' => 'kb-migration',
158 + 'name' => 'kb-migration',
159 + 'type' => 'section',
160 + 'label' => $this->existing_plugins[0][1],
161 + 'priority' => 1,
162 + 'fields' => [
163 + 'kb-migration-section' => [
164 + 'name' => 'kb-migration-section',
165 + 'classes' => 'kb-migration-section',
166 + 'type' => 'section',
167 + 'priority' => 0,
168 + 'save' => false,
169 + 'fields' => apply_filters( 'betterdocs_migration_fields', [
170 + 'migration_header' => [
171 + 'name' => 'migration_header',
172 + 'type' => 'header',
173 + 'title' => __( 'Migration', 'betterdocs' ),
174 + 'direction' => 'column',
175 + 'description' => wp_sprintf(
176 + __( 'We found that there is another Knowledge Base plugin, %s, installed on this website. To migrate your knowledge base to BetterDocs from %s, click the "Start Migration" button and all the documentation will be migrated to BetterDocs.', 'betterdocs' ),
177 + $this->existing_plugins[0][1],
178 + $this->existing_plugins[0][1]
179 + ),
180 + 'icon' => '<img src="' . betterdocs()->assets->icon( 'icons/migration.svg', true ) . '"/>',
181 + 'priority' => 1,
182 + ],
183 + 'migration_action' => [
184 + 'name' => 'migration_action',
185 + 'type' => 'button',
186 + 'text' => [
187 + 'normal' => __('Start Migration', 'betterdocs'),
188 + 'saved' => __('Migrated', 'betterdocs'),
189 + 'loading' => __('Migrating...', 'betterdocs'),
190 + ],
191 + 'ajax' => [
192 + 'on' => 'click',
193 + 'api' => '/betterdocs/v1/migrate',
194 + 'data' => [
195 + 'existing_plugins' => $this->existing_plugins[0][1],
196 + ],
197 + 'swal' => [
198 + 'text' => __('Migration completed successfully.', 'betterdocs'),
199 + 'icon' => 'success',
200 + 'autoClose' => 1000
201 + ],
202 + 'reload' => admin_url('admin.php?page=betterdocs-settings'),
203 + ],
204 + 'priority' => 2,
205 + ]
206 + ])
207 + ]
208 + ]
209 + ];
179 210
180 - public function kb_migration_settings( $args ) {
181 - /**
182 - * License Tab
183 - */
184 - $args['kb-migration'] = [
185 - 'id' => 'kb-migration',
186 - 'name' => 'kb-migration',
187 - 'type' => 'section',
188 - 'label' => $this->existing_plugins[0][1],
189 - 'priority' => 1,
190 - 'fields' => [
191 - 'kb-migration-section' => [
192 - 'name' => 'kb-migration-section',
193 - 'classes' => 'kb-migration-section',
194 - 'type' => 'section',
195 - 'priority' => 0,
196 - 'save' => false,
197 - 'fields' => apply_filters(
198 - 'betterdocs_migration_fields',
199 - [
200 - 'migration_header' => [
201 - 'name' => 'migration_header',
202 - 'type' => 'header',
203 - 'title' => __( 'Migration', 'betterdocs' ),
204 - 'direction' => 'column',
205 - 'description' => sprintf(
206 - /* translators: %1$s is the name of the existing knowledge base plugin. */
207 - __( 'We found that there is another Knowledge Base plugin, %1$s, installed on this website. To migrate your knowledge base to BetterDocs from %2$s, click the "Start Migration" button and all the documentation will be migrated to BetterDocs.', 'betterdocs' ),
208 - esc_html( $this->existing_plugins[0][1] ),
209 - esc_html( $this->existing_plugins[0][1] )
210 - ),
211 - 'icon' => '<img src="' . betterdocs()->assets->icon( 'icons/migration.svg', true ) . '"/>',
212 - 'priority' => 1,
213 - ],
214 - 'migration_action' => [
215 - 'name' => 'migration_action',
216 - 'type' => 'button',
217 - 'text' => [
218 - 'normal' => __( 'Start Migration', 'betterdocs' ),
219 - 'saved' => __( 'Migrated', 'betterdocs' ),
220 - 'loading' => __( 'Migrating...', 'betterdocs' ),
221 - ],
222 - 'ajax' => [
223 - 'on' => 'click',
224 - 'api' => '/betterdocs/v1/migrate',
225 - 'data' => [
226 - 'existing_plugins' => $this->existing_plugins[0][1],
227 - ],
228 - 'swal' => [
229 - 'text' => __( 'Migration completed successfully.', 'betterdocs' ),
230 - 'icon' => 'success',
231 - 'autoClose' => 1000
232 - ],
233 - 'reload' => admin_url( 'admin.php?page=betterdocs-settings' ),
234 - ],
235 - 'priority' => 2,
236 - ]
237 - ]
238 - )
239 - ]
240 - ]
241 - ];
211 + return $args;
212 + }
242 213
243 - return $args;
244 - }
214 + public function migrate() {
215 + $existing_plugins = $this->knowledge_base_plugins();
216 + $existing_plugins_data = $this->existing_plugins_data( $existing_plugins[0][0] );
217 + if ( isset( $existing_plugins_data['name'] ) && isset( $existing_plugins_data['url'] ) ) {
218 + if ( $existing_plugins_data['name'] === 'echo-knowledge-base' ) {
219 + $this->eco_knowledgebase_migration();
220 + } elseif ( $existing_plugins_data['name'] === 'pressapps-knowledge-base' ) {
221 + $this->pressapps_migration();
222 + } elseif ( $existing_plugins_data['name'] === 'wedocs' ) {
223 + $this->wedocs_migration();
224 + }
225 + $this->update_option( $existing_plugins_data['name'] );
226 + deactivate_plugins( $existing_plugins_data['url'] );
227 + }
228 + }
245 229
246 - public function migrate() {
247 - $existing_plugins = $this->knowledge_base_plugins();
248 - $existing_plugins_data = $this->existing_plugins_data( $existing_plugins[0][0] );
249 - if ( isset( $existing_plugins_data['name'] ) && isset( $existing_plugins_data['url'] ) ) {
250 - if ( $existing_plugins_data['name'] === 'echo-knowledge-base' ) {
251 - $this->eco_knowledgebase_migration();
252 - } elseif ( $existing_plugins_data['name'] === 'pressapps-knowledge-base' ) {
253 - $this->pressapps_migration();
254 - } elseif ( $existing_plugins_data['name'] === 'wedocs' ) {
255 - $this->wedocs_migration();
256 - }
257 - $this->update_option( $existing_plugins_data['name'] );
258 - deactivate_plugins( $existing_plugins_data['url'] );
259 - }
260 - }
230 + public function knowledge_base_plugins() {
231 + $plugins = [];
261 232
262 - public function knowledge_base_plugins() {
263 - $plugins = [];
233 + if ( Helper::is_plugin_active( 'wedocs/wedocs.php' ) ) {
234 + $plugins[] = ['wedocs', 'weDocs'];
235 + }
236 + if ( Helper::is_plugin_active( 'bsf-docs/bsf-docs.php' ) ) {
237 + $plugins[] = ['bsf-docs', 'BSF docs'];
238 + }
239 + if ( Helper::is_plugin_active( 'documentor-lite/documentor-lite.php' ) ) {
240 + $plugins[] = ['documentor-lite', 'Documentor'];
241 + }
242 + if ( Helper::is_plugin_active( 'echo-knowledge-base/echo-knowledge-base.php' ) ) {
243 + $plugins[] = ['echo-knowledge-base', 'Echo Knowledge Base'];
244 + }
245 + if ( Helper::is_plugin_active( 'pressapps-knowledge-base/pressapps-knowledge-base.php' ) ) {
246 + $plugins[] = ['pressapps-knowledge-base', 'PressApps Knowledge Base'];
247 + }
264 248
265 - if ( Helper::is_plugin_active( 'wedocs/wedocs.php' ) ) {
266 - $plugins[] = [ 'wedocs', 'weDocs' ];
267 - }
268 - if ( Helper::is_plugin_active( 'bsf-docs/bsf-docs.php' ) ) {
269 - $plugins[] = [ 'bsf-docs', 'BSF docs' ];
270 - }
271 - if ( Helper::is_plugin_active( 'documentor-lite/documentor-lite.php' ) ) {
272 - $plugins[] = [ 'documentor-lite', 'Documentor' ];
273 - }
274 - if ( Helper::is_plugin_active( 'echo-knowledge-base/echo-knowledge-base.php' ) ) {
275 - $plugins[] = [ 'echo-knowledge-base', 'Echo Knowledge Base' ];
276 - }
277 - if ( Helper::is_plugin_active( 'pressapps-knowledge-base/pressapps-knowledge-base.php' ) ) {
278 - $plugins[] = [ 'pressapps-knowledge-base', 'PressApps Knowledge Base' ];
279 - }
249 + return $plugins;
250 + }
280 251
281 - return $plugins;
282 - }
252 + public function insert_terms_hierarchically( $existing_term, $new_term, $parentId = 0 ) {
253 + $into = [];
254 + $cats = get_terms( $existing_term, ['hide_empty' => false] );
255 + if ( $cats ) {
256 + foreach ( $cats as $i => $cat ) {
257 + if ( $cat->parent == $parentId ) {
258 + $into[$cat->term_id] = $cat;
259 + unset( $cats[$i] );
260 + $doc_parent_term = term_exists( $cat->name, $new_term );
261 + wp_insert_term(
262 + $cat->name,
263 + $new_term,
264 + [
265 + 'alias_of' => $cat->slug,
266 + 'description' => $cat->description,
267 + 'slug' => $cat->slug,
268 + 'parent' => $cat->parent
269 + ]
270 + );
271 + }
272 + }
273 + if ( $cats ) {
274 + foreach ( $cats as $i => $cat ) {
275 + $get_existing_term = get_term_by( 'id', $cat->parent, $existing_term );
276 + $doc_parent_term = term_exists( $get_existing_term->name, $new_term );
277 + wp_insert_term(
278 + $cat->name,
279 + $new_term,
280 + [
281 + 'alias_of' => $cat->slug,
282 + 'description' => $cat->description,
283 + 'slug' => $cat->slug,
284 + 'parent' => $doc_parent_term['term_id']
285 + ]
286 + );
287 + }
288 + }
289 + }
290 + }
283 291
284 - public function insert_terms_hierarchically( $existing_term, $new_term, $parentId = 0 ) {
285 - $into = [];
286 - $cats = get_terms(
287 - [
288 - 'taxonomy' => $existing_term,
289 - 'hide_empty' => false
290 - ]
291 - );
292 - if ( $cats ) {
293 - foreach ( $cats as $i => $cat ) {
294 - if ( $cat->parent == $parentId ) {
295 - $into[ $cat->term_id ] = $cat;
296 - unset( $cats[ $i ] );
297 - $doc_parent_term = term_exists( $cat->name, $new_term );
298 - wp_insert_term(
299 - $cat->name,
300 - $new_term,
301 - [
302 - 'alias_of' => $cat->slug,
303 - 'description' => $cat->description,
304 - 'slug' => $cat->slug,
305 - 'parent' => $cat->parent
306 - ]
307 - );
308 - }
309 - }
310 - if ( $cats ) {
311 - foreach ( $cats as $i => $cat ) {
312 - $get_existing_term = get_term_by( 'id', $cat->parent, $existing_term );
313 - $doc_parent_term = term_exists( $get_existing_term->name, $new_term );
314 - wp_insert_term(
315 - $cat->name,
316 - $new_term,
317 - [
318 - 'alias_of' => $cat->slug,
319 - 'description' => $cat->description,
320 - 'slug' => $cat->slug,
321 - 'parent' => $doc_parent_term['term_id']
322 - ]
323 - );
324 - }
325 - }
326 - }
327 - }
292 + public function insert_posts( $existing_post, $existing_cat, $existing_tag ) {
293 + $args = [
294 + 'post_type' => $existing_post,
295 + 'post_status' => 'any',
296 + 'posts_per_page' => -1
297 + ];
298 + $postslist = get_posts( $args );
299 + if ( $postslist ) {
300 + foreach ( $postslist as $post ) {
301 + // Create post object
302 + if ( ! get_page_by_title( $post->post_title, 'OBJECT', 'docs' ) ) {
303 + $post_args = [
304 + 'post_type' => 'docs',
305 + 'post_title' => $post->post_title,
306 + 'post_content' => $post->post_content,
307 + 'post_status' => $post->post_status,
308 + 'post_author' => $post->post_author,
309 + 'post_date' => $post->post_date,
310 + 'post_date_gmt' => $post->post_date_gmt,
311 + 'post_excerpt' => $post->post_excerpt,
312 + 'comment_status' => $post->comment_status,
313 + 'ping_status' => $post->ping_status,
314 + 'post_password' => $post->post_password,
315 + 'post_name' => $post->post_name,
316 + 'to_ping' => $post->to_ping,
317 + 'pinged' => $post->pinged,
318 + 'post_modified' => $post->post_modified,
319 + 'post_modified_gmt' => $post->post_modified_gmt,
320 + 'post_content_filtered' => $post->post_content_filtered,
321 + 'post_parent' => $post->post_parent,
322 + 'post_mime_type' => $post->post_mime_type,
323 + 'comment_count' => $post->comment_count,
324 + 'filter' => $post->filter
325 + ];
326 + // Insert the post into the database
327 + $result = wp_insert_post( $post_args );
328 + if ( $result && ! is_wp_error( $result ) ) {
329 + $cat_list = wp_get_post_terms( $post->ID, $existing_cat, ['fields' => 'all'] );
330 + if ( $cat_list ) {
331 + $post_id = $result;
332 + wp_set_object_terms( $post_id, [$cat_list['0']->name], 'doc_category', false );
333 + }
334 + $tag_list = wp_get_post_terms( $post->ID, $existing_tag, ['fields' => 'all'] );
335 + if ( $tag_list ) {
336 + $post_id = $result;
337 + wp_set_object_terms( $post_id, [$tag_list['0']->name], 'doc_tag', false );
338 + }
339 + }
340 + }
341 + }
342 + }
343 + }
328 344
329 - public function insert_posts( $existing_post, $existing_cat, $existing_tag ) {
330 - $args = [
331 - 'post_type' => $existing_post,
332 - 'post_status' => 'any',
333 - 'posts_per_page' => -1,
334 - ];
335 - $postslist = get_posts( $args );
336 - if ( $postslist ) {
337 - foreach ( $postslist as $post ) {
338 - // Check if a post with the same title exists using WP_Query
339 - $query_args = [
340 - 'post_type' => 'docs',
341 - 'post_status' => 'any',
342 - 'title' => $post->post_title,
343 - 'posts_per_page' => 1,
344 - 'fields' => 'ids',
345 - ];
346 - $existing_doc = new WP_Query( $query_args );
345 + public function existing_plugins_data( $plugins ) {
346 + $plugins_data = [];
347 + if ( $plugins === 'wedocs' ) {
348 + $plugins_data['name'] = 'wedocs';
349 + $plugins_data['url'] = 'wedocs/wedocs.php';
350 + }
351 + if ( $plugins === 'bsf-docs' ) {
352 + $plugins_data['name'] = 'bsf-docs';
353 + $plugins_data['url'] = 'bsf-docs/bsf-docs.php';
354 + }
355 + if ( $plugins === 'documentor-lite' ) {
356 + $plugins_data['name'] = 'documentor-lite';
357 + $plugins_data['url'] = 'documentor-lite/documentor-lite.php';
358 + }
359 + if ( $plugins === 'echo-knowledge-base' ) {
360 + $plugins_data['name'] = 'echo-knowledge-base';
361 + $plugins_data['url'] = 'echo-knowledge-base/echo-knowledge-base.php';
362 + }
363 + if ( $plugins === 'pressapps-knowledge-base' ) {
364 + $plugins_data['name'] = 'pressapps-knowledge-base';
365 + $plugins_data['url'] = 'pressapps-knowledge-base/pressapps-knowledge-base.php';
366 + }
367 + return $plugins_data;
368 + }
347 369
348 - if ( ! $existing_doc->have_posts() ) {
349 - $post_args = [
350 - 'post_type' => 'docs',
351 - 'post_title' => $post->post_title,
352 - 'post_content' => $post->post_content,
353 - 'post_status' => $post->post_status,
354 - 'post_author' => $post->post_author,
355 - 'post_date' => $post->post_date,
356 - 'post_date_gmt' => $post->post_date_gmt,
357 - 'post_excerpt' => $post->post_excerpt,
358 - 'comment_status' => $post->comment_status,
359 - 'ping_status' => $post->ping_status,
360 - 'post_password' => $post->post_password,
361 - 'post_name' => $post->post_name,
362 - 'to_ping' => $post->to_ping,
363 - 'pinged' => $post->pinged,
364 - 'post_modified' => $post->post_modified,
365 - 'post_modified_gmt' => $post->post_modified_gmt,
366 - 'post_content_filtered' => $post->post_content_filtered,
367 - 'post_parent' => $post->post_parent,
368 - 'post_mime_type' => $post->post_mime_type,
369 - 'comment_count' => $post->comment_count,
370 - 'filter' => $post->filter,
371 - ];
372 - // Insert the post into the database
373 - $result = wp_insert_post( $post_args );
374 - if ( $result && ! is_wp_error( $result ) ) {
375 - $cat_list = wp_get_post_terms( $post->ID, $existing_cat, [ 'fields' => 'all' ] );
376 - if ( $cat_list ) {
377 - $post_id = $result;
378 - wp_set_object_terms( $post_id, [ $cat_list[0]->name ], 'doc_category', false );
379 - }
380 - $tag_list = wp_get_post_terms( $post->ID, $existing_tag, [ 'fields' => 'all' ] );
381 - if ( $tag_list ) {
382 - $post_id = $result;
383 - wp_set_object_terms( $post_id, [ $tag_list[0]->name ], 'doc_tag', false );
384 - }
385 - }
386 - }
370 + public function eco_knowledgebase_migration() {
371 + $this->insert_terms_hierarchically( 'epkb_post_type_1_category', 'doc_category' );
372 + $this->insert_terms_hierarchically( 'epkb_post_type_1_tag', 'doc_tag' );
373 + $this->insert_posts( 'epkb_post_type_1', 'epkb_post_type_1_category', 'epkb_post_type_1_tag' );
374 + }
387 375
388 - // Reset post data after query
389 - wp_reset_postdata();
390 - }
391 - }
392 - }
376 + public function pressapps_migration() {
377 + $this->insert_terms_hierarchically( 'knowledgebase_category', 'doc_category' );
378 + $this->insert_terms_hierarchically( 'knowledgebase_tags', 'doc_tag' );
379 + $this->insert_posts( 'knowledgebase', 'knowledgebase_category', 'knowledgebase_tags' );
380 + }
393 381
394 - public function existing_plugins_data( $plugins ) {
395 - $plugins_data = [];
396 - if ( $plugins === 'wedocs' ) {
397 - $plugins_data['name'] = 'wedocs';
398 - $plugins_data['url'] = 'wedocs/wedocs.php';
399 - }
400 - if ( $plugins === 'bsf-docs' ) {
401 - $plugins_data['name'] = 'bsf-docs';
402 - $plugins_data['url'] = 'bsf-docs/bsf-docs.php';
403 - }
404 - if ( $plugins === 'documentor-lite' ) {
405 - $plugins_data['name'] = 'documentor-lite';
406 - $plugins_data['url'] = 'documentor-lite/documentor-lite.php';
407 - }
408 - if ( $plugins === 'echo-knowledge-base' ) {
409 - $plugins_data['name'] = 'echo-knowledge-base';
410 - $plugins_data['url'] = 'echo-knowledge-base/echo-knowledge-base.php';
411 - }
412 - if ( $plugins === 'pressapps-knowledge-base' ) {
413 - $plugins_data['name'] = 'pressapps-knowledge-base';
414 - $plugins_data['url'] = 'pressapps-knowledge-base/pressapps-knowledge-base.php';
415 - }
416 - return $plugins_data;
417 - }
382 + public function wedocs_migration() {
383 + // Step 1: Get posts with no parent (main posts)
384 + $args_step1 = array(
385 + 'post_type' => 'docs',
386 + 'post_status' => 'publish',
387 + 'posts_per_page' => -1,
388 + 'post_parent' => 0,
389 + );
418 390
419 - public function eco_knowledgebase_migration() {
420 - $this->insert_terms_hierarchically( 'epkb_post_type_1_category', 'doc_category' );
421 - $this->insert_terms_hierarchically( 'epkb_post_type_1_tag', 'doc_tag' );
422 - $this->insert_posts( 'epkb_post_type_1', 'epkb_post_type_1_category', 'epkb_post_type_1_tag' );
423 - }
391 + $posts_step1 = get_posts( $args_step1 );
424 392
425 - public function pressapps_migration() {
426 - $this->insert_terms_hierarchically( 'knowledgebase_category', 'doc_category' );
427 - $this->insert_terms_hierarchically( 'knowledgebase_tags', 'doc_tag' );
428 - $this->insert_posts( 'knowledgebase', 'knowledgebase_category', 'knowledgebase_tags' );
429 - }
393 + // Multidimensional array to store posts
394 + $nested_posts = array();
430 395
431 - public function wedocs_migration() {
432 - // Step 1: Get posts with no parent (main posts)
433 - $args_step1 = array(
434 - 'post_type' => 'docs',
435 - 'post_status' => 'publish',
436 - 'posts_per_page' => -1,
437 - 'post_parent' => 0,
438 - );
396 + // First Dimension key: 'multiple_kb'
397 + foreach ( $posts_step1 as $post_step1 ) {
398 + $nested_posts['multiple_kb'][$post_step1->ID] = array(
399 + 'post' => $post_step1,
400 + 'doc_category' => array(), // Initialize the second dimension
401 + );
439 402
440 - $posts_step1 = get_posts( $args_step1 );
403 + // Step 2: 'doc_category'
404 + $args_step2 = array(
405 + 'post_type' => 'docs',
406 + 'post_status' => 'publish',
407 + 'posts_per_page' => -1,
408 + 'post_parent' => $post_step1->ID,
409 + );
441 410
442 - // Multidimensional array to store posts
443 - $nested_posts = array();
411 + $posts_step2 = get_posts($args_step2);
444 412
445 - // First Dimension key: 'multiple_kb'
446 - foreach ( $posts_step1 as $post_step1 ) {
447 - $nested_posts['multiple_kb'][ $post_step1->ID ] = array(
448 - 'post' => $post_step1,
449 - 'doc_category' => array(), // Initialize the second dimension
450 - );
413 + foreach ( $posts_step2 as $post_step2 ) {
414 + $nested_posts['multiple_kb'][$post_step1->ID]['doc_category'][$post_step2->ID] = array(
415 + 'post' => $post_step2,
416 + 'docs' => array(), // Initialize the third dimension
417 + );
451 418
452 - // Step 2: 'doc_category'
453 - $args_step2 = array(
454 - 'post_type' => 'docs',
455 - 'post_status' => 'publish',
456 - 'posts_per_page' => -1,
457 - 'post_parent' => $post_step1->ID,
458 - );
419 + // Step 3: 'docs'
420 + $args_step3 = array(
421 + 'post_type' => 'docs', // Replace with your actual post type
422 + 'post_status' => 'publish',
423 + 'posts_per_page' => -1,
424 + 'post_parent' => $post_step2->ID,
425 + );
459 426
460 - $posts_step2 = get_posts( $args_step2 );
427 + $posts_step3 = get_posts($args_step3);
461 428
462 - foreach ( $posts_step2 as $post_step2 ) {
463 - $nested_posts['multiple_kb'][ $post_step1->ID ]['doc_category'][ $post_step2->ID ] = array(
464 - 'post' => $post_step2,
465 - 'docs' => array(), // Initialize the third dimension
466 - );
429 + foreach ( $posts_step3 as $post_step3 ) {
430 + $nested_posts['multiple_kb'][$post_step1->ID]['doc_category'][$post_step2->ID]['docs'][$post_step3->ID] = array(
431 + 'post' => $post_step3,
432 + );
433 + // Continue with additional dimensions if needed
434 + }
435 + }
436 + }
467 437
468 - // Step 3: 'docs'
469 - $args_step3 = array(
470 - 'post_type' => 'docs', // Replace with your actual post type
471 - 'post_status' => 'publish',
472 - 'posts_per_page' => -1,
473 - 'post_parent' => $post_step2->ID,
474 - );
438 + // Now, $nested_posts contains the posts organized in a multidimensional array with the specified key names
475 439
476 - $posts_step3 = get_posts( $args_step3 );
440 + foreach ( $nested_posts['multiple_kb'] as $multiple_kb_id => $multiple_kb_data ) {
441 + // Step 1: Insert posts as terms under 'knowledge_base'
442 + $knowledge_base_id = wp_insert_term($multiple_kb_data['post']->post_title, 'knowledge_base');
477 443
478 - foreach ( $posts_step3 as $post_step3 ) {
479 - $nested_posts['multiple_kb'][ $post_step1->ID ]['doc_category'][ $post_step2->ID ]['docs'][ $post_step3->ID ] = array(
480 - 'post' => $post_step3,
481 - );
482 - // Continue with additional dimensions if needed
483 - }
484 - }
485 - }
444 + if ( ! is_wp_error($knowledge_base_id) ) {
445 + wp_delete_post($multiple_kb_data['post']->ID, true);
446 + $knowledge_base_term = get_term($knowledge_base_id['term_id'], 'knowledge_base');
447 + }
486 448
487 - // Now, $nested_posts contains the posts organized in a multidimensional array with the specified key names
449 + foreach ( $multiple_kb_data['doc_category'] as $doc_category_id => $doc_category_data ) {
488 450
489 - foreach ( $nested_posts['multiple_kb'] as $multiple_kb_id => $multiple_kb_data ) {
490 - // Step 1: Insert posts as terms under 'knowledge_base'
491 - $knowledge_base_id = wp_insert_term( $multiple_kb_data['post']->post_title, 'knowledge_base' );
451 + // Step 2: Insert posts as terms under 'doc_category' and set term meta
452 + $doc_category_id = wp_insert_term($doc_category_data['post']->post_title, 'doc_category');
492 453
493 - if ( ! is_wp_error( $knowledge_base_id ) ) {
494 - wp_delete_post( $multiple_kb_data['post']->ID, true );
495 - $knowledge_base_term = get_term( $knowledge_base_id['term_id'], 'knowledge_base' );
496 - }
454 + if (!is_wp_error($doc_category_id)) {
455 + $doc_category_term = get_term($doc_category_id['term_id'], 'doc_category');
456 + wp_delete_post($doc_category_data['post']->ID, true);
457 + $doc_category_kb = rest_sanitize_array( array($knowledge_base_term->slug) );
458 + // Set term meta for 'knowledge_base_doc_category'
459 + update_term_meta($doc_category_id['term_id'], 'doc_category_knowledge_base', $doc_category_kb);
460 + }
497 461
498 - foreach ( $multiple_kb_data['doc_category'] as $doc_category_id => $doc_category_data ) {
462 + foreach ( $doc_category_data['docs'] as $docs_id => $docs_data ) {
463 + wp_set_post_terms($docs_data['post']->ID, array($knowledge_base_term->term_id), 'knowledge_base');
464 + wp_set_post_terms($docs_data['post']->ID, array($doc_category_term->term_id), 'doc_category');
465 + // // Step 3: Update posts to assign correct parent terms
466 + // wp_update_post(array(
467 + // 'ID' => $docs_data['post']->ID,
468 + // 'post_parent' => 0,
469 + // ));
470 + }
471 + }
472 + }
473 + }
499 474
500 - // Step 2: Insert posts as terms under 'doc_category' and set term meta
501 - $doc_category_id = wp_insert_term( $doc_category_data['post']->post_title, 'doc_category' );
475 + /**
476 + * Updates the 'betterdocs_migration' option with migrated knowledge base plugin names.
477 + *
478 + * This function checks if the option exists and is a serialized array. If the option exists,
479 + * it unserializes the current value, merges it with the new values, serializes the merged array,
480 + * and updates the option. If the option doesn't exist or is not a serialized array, it creates
481 + * a new option with the serialized format.
482 + *
483 + * @param string $kb_name The name of the knowledge base to be added or updated.
484 + *
485 + * @return void
486 + */
487 + public function update_option( $kb_name ) {
488 + $existing_value = get_option( 'betterdocs_migration' );
489 + $new_values = array( $kb_name );
502 490
503 - if ( ! is_wp_error( $doc_category_id ) ) {
504 - $doc_category_term = get_term( $doc_category_id['term_id'], 'doc_category' );
505 - wp_delete_post( $doc_category_data['post']->ID, true );
506 - $doc_category_kb = rest_sanitize_array( array( $knowledge_base_term->slug ) );
507 - // Set term meta for 'knowledge_base_doc_category'
508 - update_term_meta( $doc_category_id['term_id'], 'doc_category_knowledge_base', $doc_category_kb );
509 - }
491 + if ($existing_value !== false && is_serialized( $existing_value )) {
492 + $existing_values_array = unserialize($existing_value);
493 + $merged_values = array_merge($existing_values_array, $new_values);
510 494
511 - foreach ( $doc_category_data['docs'] as $docs_id => $docs_data ) {
512 - wp_set_post_terms( $docs_data['post']->ID, array( $knowledge_base_term->term_id ), 'knowledge_base' );
513 - wp_set_post_terms( $docs_data['post']->ID, array( $doc_category_term->term_id ), 'doc_category' );
514 - // // Step 3: Update posts to assign correct parent terms
515 - // wp_update_post(array(
516 - // 'ID' => $docs_data['post']->ID,
517 - // 'post_parent' => 0,
518 - // ));
519 - }
520 - }
521 - }
522 - }
495 + update_option('betterdocs_migration', serialize($merged_values));
496 + } else {
497 + update_option('betterdocs_migration', serialize($new_values));
498 + }
499 + }
523 500
524 - /**
525 - * Updates the 'betterdocs_migration' option with migrated knowledge base plugin names.
526 - *
527 - * This function checks if the option exists and is a serialized array. If the option exists,
528 - * it unserializes the current value, merges it with the new values, serializes the merged array,
529 - * and updates the option. If the option doesn't exist or is not a serialized array, it creates
530 - * a new option with the serialized format.
531 - *
532 - * @param string $kb_name The name of the knowledge base to be added or updated.
533 - *
534 - * @return void
535 - */
536 - public function update_option( $kb_name ) {
537 - $existing_value = get_option( 'betterdocs_migration' );
538 - $new_values = array( $kb_name );
501 + public function get_migrated_plugins() {
502 + $existing_value = get_option( 'betterdocs_migration' );
539 503
540 - if ( $existing_value !== false && is_serialized( $existing_value ) ) {
541 - $existing_values_array = unserialize( $existing_value );
542 - $merged_values = array_merge( $existing_values_array, $new_values );
504 + if ( $existing_value ) {
505 + return unserialize($existing_value);
506 + }
543 507
544 - update_option( 'betterdocs_migration', serialize( $merged_values ) );
545 - } else {
546 - update_option( 'betterdocs_migration', serialize( $new_values ) );
547 - }
548 - }
549 -
550 - public function get_migrated_plugins() {
551 - $existing_value = get_option( 'betterdocs_migration' );
552 -
553 - if ( $existing_value ) {
554 - return unserialize( $existing_value );
555 - }
556 -
557 - return [];
558 - }
508 + return [];
509 + }
559 510 }