PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.6.2
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.6.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 3.5.2 All 199 releases
betterdocs / includes / Admin / HelpScoutMigration.php

HelpScoutMigration.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.6.2, at includes/Admin/HelpScoutMigration.php

206 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace WPDeveloper\BetterDocs\Admin;
3
4 use WPDeveloper\BetterDocs\Admin\Importer\WPImport;
5 use WPDeveloper\BetterDocs\Admin\BackgroundProcess\WP_Background_Process;
6
7 class HelpScoutMigration extends WP_Background_Process {
8 /**
9 * @var string
10 */
11 protected $action = 'betterdocs_helpscout_migration';
12
13 /**
14 * Task
15 *
16 * Override this method to perform any actions required on each
17 * queue item. Return the modified item for further processing
18 * in the next pass through. Or, return false to remove the
19 * item from the queue.
20 *
21 * @param mixed $item Queue item to iterate over
22 *
23 * @return mixed
24 */
25 protected function task( $item ) {
26 if ( empty( $item ) ) {
27 return false;
28 }
29
30 $headers = $item['headers'];
31 $initial_response = $item['initial_response'];
32 $api_key = $item['api_key'];
33 $collection_id = $item['collection_id'];
34 // Implement your migration logic here
35 $this->migrateFromHelpScout( $headers, $initial_response, $api_key, $collection_id );
36
37 return false;
38 }
39
40 /**
41 * Complete
42 *
43 * Override if applicable, but ensure that the below actions are
44 * performed, or, call parent::complete().
45 */
46 protected function complete() {
47 parent::complete();
48
49 // Perform any cleanup tasks after all items are processed
50 }
51
52 /**
53 * Migrate articles from Help Scout to WordPress
54 *
55 * @param string $api_key Help Scout API key
56 * @param int $collection_id Help Scout collection ID
57 * @return bool True if migration is successful, false otherwise
58 */
59 protected function migrateFromHelpScout( $headers, $initial_response, $api_key, $collection_id ) {
60 $total_pages = get_option( 'betterdocs_helpscout_total_pages' );
61
62 if ( ! $total_pages ) {
63 if ( is_wp_error( $initial_response ) ) {
64 return false;
65 }
66 $initial_body = wp_remote_retrieve_body( $initial_response );
67 $initial_data = json_decode( $initial_body, true );
68 $total_pages = isset( $initial_data['articles']['pages'] ) ? $initial_data['articles']['pages'] : 0;
69 update_option( 'betterdocs_helpscout_total_pages', $total_pages );
70 }
71
72 $current_page = get_option( 'betterdocs_helpscout_current_page', 1 );
73 $allArticleIds = [];
74
75 try {
76 // Fetch articles page by page
77 while ( $current_page <= $total_pages ) {
78 $api_endpoint = 'https://docsapi.helpscout.net/v1/collections/' . $collection_id . '/articles?pageSize=1&page=' . $current_page;
79
80 $response = wp_remote_get( $api_endpoint, [ 'headers' => $headers ] );
81
82 if ( is_wp_error( $response ) ) {
83 return false;
84 }
85
86 $body = wp_remote_retrieve_body( $response );
87 $data = json_decode( $body, true );
88
89 if ( ! isset( $data['articles'] ) || ! isset( $data['articles']['items'] ) ) {
90 return false;
91 }
92
93 $articles = $data['articles']['items'];
94
95 // Fetch detailed information for each article
96 $detailedArticles = [];
97 foreach ( $articles as $article ) {
98 $articleId = $article['id'];
99 $articleNumber = $article['number'];
100
101 if ( ! $this->articleIdExists( $articleId ) ) {
102 $articleEndpoint = "https://docsapi.helpscout.net/v1/articles/{$articleId}";
103 $articleResponse = wp_remote_get( $articleEndpoint, [ 'headers' => $headers ] );
104
105 if ( ! is_wp_error( $articleResponse ) ) {
106 $articleBody = wp_remote_retrieve_body( $articleResponse );
107 $articleData = json_decode( $articleBody, true );
108
109 if ( isset( $articleData['article'] ) && isset( $articleData['article']['text'] ) ) {
110 $article['text'] = $articleData['article']['text'];
111 $categories = $articleData['article']['categories'];
112 $categoryDetails = [];
113 $categories_cache = get_transient( 'betterdocs_helpscout_categories' );
114
115 // Initialize $categories_cache as an array if it's not set
116 if ( ! is_array( $categories_cache ) ) {
117 $categories_cache = [];
118 }
119
120 foreach ( $categories as $categoryId ) {
121 if ( isset( $categories_cache[ $categoryId ] ) ) {
122 $categoryDetails[] = $categories_cache[ $categoryId ];
123 } else {
124 $categoryInfo = $this->fetchCategoryInfo( $categoryId, $headers );
125 if ( $categoryInfo ) {
126 $categoryDetails[] = $categoryInfo;
127 $categories_cache[ $categoryId ] = $categoryInfo;
128 set_transient( 'betterdocs_helpscout_categories', $categories_cache, DAY_IN_SECONDS ); // Cache for a day
129 }
130 }
131 }
132
133 $article['categories'] = $categoryDetails;
134 $detailedArticles[] = $article;
135 $allArticleIds[] = $articleId;
136 }
137 }
138 }
139 }
140
141 if ( ! empty( $detailedArticles ) ) {
142 $wp_importer = new WPImport( '' );
143 $wp_importer->import_helpscout_data( $detailedArticles );
144 $existingArticleIds = get_option( 'betterdocs_helpscout_migrated_article_ids', [] );
145 $existingArticleIds = is_array( $existingArticleIds ) ? $existingArticleIds : [];
146 $allArticleIds = array_merge( $existingArticleIds, $allArticleIds );
147 update_option( 'betterdocs_helpscout_migrated_article_ids', serialize( $allArticleIds ) );
148 }
149
150 ++$current_page;
151
152 update_option( 'betterdocs_helpscout_current_page', $current_page );
153
154 // Check if we have processed all pages
155 if ( $current_page > $total_pages ) {
156 update_option( 'betterdocs_helpscout_current_page', 1 );
157 delete_option( 'betterdocs_helpscout_total_pages' );
158 }
159 }
160 } catch ( \WP_Error $wp_error ) {
161 return false;
162 } catch ( \Exception $e ) {
163 return false;
164 }
165
166 return true; // Indicate success
167 }
168
169 /**
170 * Fetch category information from Help Scout API.
171 *
172 * @param string $categoryId Category ID
173 * @param array $headers Headers for API requests
174 * @return array|null Category information
175 */
176 protected function fetchCategoryInfo( $categoryId, $headers ) {
177 $categoryEndpoint = "https://docsapi.helpscout.net/v1/categories/{$categoryId}";
178 $categoryResponse = wp_remote_get( $categoryEndpoint, [ 'headers' => $headers ] );
179
180 if ( ! is_wp_error( $categoryResponse ) ) {
181 $categoryBody = wp_remote_retrieve_body( $categoryResponse );
182 $categoryData = json_decode( $categoryBody, true );
183
184 if ( isset( $categoryData['category']['name'] ) && isset( $categoryData['category']['slug'] ) ) {
185 return [
186 'name' => $categoryData['category']['name'],
187 'slug' => $categoryData['category']['slug']
188 ];
189 }
190 }
191 return null;
192 }
193
194 /**
195 * Check if article ID exists in the option.
196 *
197 * @param int $articleId Article ID
198 * @return bool True if article ID exists, false otherwise
199 */
200 protected function articleIdExists( $articleId ) {
201 $existingArticleIds = get_option( 'betterdocs_helpscout_migrated_article_ids' );
202 $existingArticleIds = $existingArticleIds ? unserialize( $existingArticleIds ) : [];
203 return in_array( $articleId, $existingArticleIds );
204 }
205 }
206