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