| 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 |
// Handle WP_Error exceptions |
| 162 |
//error_log('WP_Error occurred during migration: ' . $wp_error->get_error_message()); |
| 163 |
return false; // Indicate failure to process the current item |
| 164 |
} catch ( \Exception $e ) { |
| 165 |
// Handle other exceptions |
| 166 |
//error_log('Error occurred during migration: ' . $e->getMessage()); |
| 167 |
return false; // Indicate failure to process the current item |
| 168 |
} |
| 169 |
|
| 170 |
return true; // Indicate success |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Fetch category information from Help Scout API. |
| 175 |
* |
| 176 |
* @param string $categoryId Category ID |
| 177 |
* @param array $headers Headers for API requests |
| 178 |
* @return array|null Category information |
| 179 |
*/ |
| 180 |
protected function fetchCategoryInfo( $categoryId, $headers ) { |
| 181 |
$categoryEndpoint = "https://docsapi.helpscout.net/v1/categories/{$categoryId}"; |
| 182 |
$categoryResponse = wp_remote_get( $categoryEndpoint, [ 'headers' => $headers ] ); |
| 183 |
|
| 184 |
if ( ! is_wp_error( $categoryResponse ) ) { |
| 185 |
$categoryBody = wp_remote_retrieve_body( $categoryResponse ); |
| 186 |
$categoryData = json_decode( $categoryBody, true ); |
| 187 |
|
| 188 |
if ( isset( $categoryData['category']['name'] ) && isset( $categoryData['category']['slug'] ) ) { |
| 189 |
return [ |
| 190 |
'name' => $categoryData['category']['name'], |
| 191 |
'slug' => $categoryData['category']['slug'] |
| 192 |
]; |
| 193 |
} |
| 194 |
} |
| 195 |
return null; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Check if article ID exists in the option. |
| 200 |
* |
| 201 |
* @param int $articleId Article ID |
| 202 |
* @return bool True if article ID exists, false otherwise |
| 203 |
*/ |
| 204 |
protected function articleIdExists( $articleId ) { |
| 205 |
$existingArticleIds = get_option( 'betterdocs_helpscout_migrated_article_ids' ); |
| 206 |
$existingArticleIds = $existingArticleIds ? unserialize( $existingArticleIds ) : []; |
| 207 |
return in_array( $articleId, $existingArticleIds ); |
| 208 |
} |
| 209 |
} |
| 210 |
|