| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDeveloper\BetterDocs\REST; |
| 4 |
|
| 5 |
use WP_Query; |
| 6 |
use WP_User; |
| 7 |
use WP_REST_Request; |
| 8 |
use WPDeveloper\BetterDocs\Core\BaseAPI; |
| 9 |
use WPDeveloper\BetterDocs\Admin\WPExporter; |
| 10 |
use WPDeveloper\BetterDocs\Admin\CSVExporter; |
| 11 |
use WPDeveloper\BetterDocs\Admin\Importer\WPImport; |
| 12 |
use WPDeveloper\BetterDocs\Admin\Importer\Parsers\CSV_Parser; |
| 13 |
|
| 14 |
class Settings extends BaseAPI { |
| 15 |
|
| 16 |
public function permission_check() { |
| 17 |
return current_user_can( 'edit_docs_settings' ); |
| 18 |
} |
| 19 |
|
| 20 |
public function register() { |
| 21 |
$this->get( 'settings', [$this, 'get_settings'] ); |
| 22 |
$this->post( 'settings', [$this, 'save_settings'] ); |
| 23 |
$this->post( 'plugin_insights', [$this, 'plugin_insights'] ); |
| 24 |
|
| 25 |
$this->post( 'reporting-test', [$this, 'test_reporting'] ); |
| 26 |
$this->post( 'export-docs', [$this, 'export_docs'] ); |
| 27 |
$this->post( 'export-settings', [$this, 'export_settings'] ); |
| 28 |
$this->post( 'import-docs', [$this, 'import_docs'] ); |
| 29 |
$this->post( 'import-settings', [$this, 'import_settings'] ); |
| 30 |
$this->post( 'parse-xml', [$this, 'parse_xml'] ); |
| 31 |
$this->post( 'parse-csv', [$this, 'parse_csv'] ); |
| 32 |
$this->post( 'migrate', [$this, 'migrate_plugins'] ); |
| 33 |
$this->post( 'create-sample-docs', [$this, 'sample_docs'] ); |
| 34 |
$this->post( 'helpscout-migration', [$this, 'helpscout_migration'] ); |
| 35 |
} |
| 36 |
|
| 37 |
public function sample_docs( WP_REST_Request $request ) { |
| 38 |
$action = $request->get_param( 'action' ); |
| 39 |
|
| 40 |
if ( $action == 'create-dummy-data' ) { |
| 41 |
$file = BETTERDOCS_ABSPATH . 'assets/admin/images/BetterDocs-sample-data.csv'; |
| 42 |
$args = [ |
| 43 |
'fetch_attachments' => true, |
| 44 |
'action' => '', |
| 45 |
'existing_slug' => '', |
| 46 |
'file_type' => 'text/csv' |
| 47 |
]; |
| 48 |
$wp_import_object = new WPImport( $file, $args ); |
| 49 |
return $wp_import_object->run(); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
public function export_docs( WP_REST_Request $request ) { |
| 54 |
$args = [ |
| 55 |
'include_post_featured_image_as_attachment' => true, |
| 56 |
'status' => 'publish', |
| 57 |
'content' => 'docs' |
| 58 |
]; |
| 59 |
|
| 60 |
$data = $request->get_params(); |
| 61 |
|
| 62 |
if ( $data['export_type'] == 'docs' && $data['export_docs'][0] != 'all' ) { |
| 63 |
$args['post__in'] = $data['export_docs']; |
| 64 |
} |
| 65 |
|
| 66 |
if ( $data['export_type'] == 'doc_category' && $data['export_categories'][0] != 'all' ) { |
| 67 |
$args['category_terms'] = $data['export_categories']; |
| 68 |
} |
| 69 |
|
| 70 |
if ( $data['export_type'] == 'knowledge_base' && $data['export_kbs'][0] != 'all' ) { |
| 71 |
$args['kb_terms'] = $data['export_kbs']; |
| 72 |
} |
| 73 |
|
| 74 |
if ($data['file_type'] == 'xml') { |
| 75 |
$exporter = new WPExporter( $args ); |
| 76 |
} else if ($data['file_type'] == 'csv') { |
| 77 |
$exporter = new CSVExporter( $args ); |
| 78 |
} |
| 79 |
|
| 80 |
return $exporter->run(); |
| 81 |
} |
| 82 |
|
| 83 |
public function export_settings( WP_REST_Request $request ) { |
| 84 |
$betterdocs_settings = get_option( 'betterdocs_settings' ); |
| 85 |
$json_str = json_encode( $betterdocs_settings, JSON_PRETTY_PRINT ); |
| 86 |
$file_name = 'betterdocs-settings.json'; |
| 87 |
return [ |
| 88 |
'success' => true, |
| 89 |
'data' => [ |
| 90 |
'filename' => $file_name, |
| 91 |
'filetype' => 'text/json', |
| 92 |
'download' => $json_str |
| 93 |
] |
| 94 |
]; |
| 95 |
} |
| 96 |
|
| 97 |
public function import_settings( WP_REST_Request $request ) { |
| 98 |
$settings = $request->get_param( 'settings' ); |
| 99 |
// Decode the JSON data into a PHP array |
| 100 |
$settings = json_decode( $settings, true ); |
| 101 |
$save = update_option( 'betterdocs_settings', $settings ); |
| 102 |
|
| 103 |
if ( $save == true ) { |
| 104 |
return [ |
| 105 |
'status' => 'success' |
| 106 |
]; |
| 107 |
} else { |
| 108 |
return [ |
| 109 |
'status' => 'failed' |
| 110 |
]; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
public function import_docs( WP_REST_Request $request ) { |
| 115 |
$existing_slug = $request->get_param( 'existing_slug' ); |
| 116 |
$action = $request->get_param( 'action' ); |
| 117 |
|
| 118 |
$files = $request->get_file_params(); |
| 119 |
|
| 120 |
$file = $files['file']['tmp_name']; |
| 121 |
|
| 122 |
$args = [ |
| 123 |
'fetch_attachments' => true, |
| 124 |
'existing_slug' => $existing_slug, |
| 125 |
'action' => $action, |
| 126 |
'file_type' => $files['file']['type'] |
| 127 |
]; |
| 128 |
|
| 129 |
$wp_importer = new WPImport( $file, $args ); |
| 130 |
|
| 131 |
return $wp_importer->run(); |
| 132 |
} |
| 133 |
|
| 134 |
public function parse_xml( WP_REST_Request $request ) { |
| 135 |
$data = $request->get_params(); |
| 136 |
$existing_post_slugs = $this->get_existing_slugs($data['posts']); |
| 137 |
|
| 138 |
// Output the array of existing post slugs |
| 139 |
return [ |
| 140 |
'status' => 'success', |
| 141 |
'data' => $existing_post_slugs, |
| 142 |
]; |
| 143 |
} |
| 144 |
|
| 145 |
public function parse_csv( WP_REST_Request $request ) { |
| 146 |
$files = $request->get_file_params(); |
| 147 |
|
| 148 |
$file = $files['file']['tmp_name']; |
| 149 |
|
| 150 |
$parser = new CSV_Parser(); |
| 151 |
$parsed = $parser->parse( $file ); |
| 152 |
|
| 153 |
// if ( $parsed['type'] == 'sample/csv' ) { |
| 154 |
// return [ |
| 155 |
// 'status' => 'success' |
| 156 |
// ]; |
| 157 |
// } |
| 158 |
|
| 159 |
if ( isset( $parsed['posts'] ) && is_array( $parsed['posts'] ) ) { |
| 160 |
$post_names = array_map(function ($post) { |
| 161 |
return $post['post_name']; |
| 162 |
}, $parsed['posts']); |
| 163 |
} |
| 164 |
|
| 165 |
$existing_post_slugs = $this->get_existing_slugs( $post_names) ; |
| 166 |
|
| 167 |
return [ |
| 168 |
'status' => 'success', |
| 169 |
'data' => $existing_post_slugs, |
| 170 |
]; |
| 171 |
} |
| 172 |
|
| 173 |
public function migrate_plugins( WP_REST_Request $request ) { |
| 174 |
betterdocs()->kbmigration->migrate(); |
| 175 |
|
| 176 |
return [ |
| 177 |
'status' => 'success' |
| 178 |
]; |
| 179 |
} |
| 180 |
|
| 181 |
public function helpscout_migration( WP_REST_Request $request ) { |
| 182 |
$api_key = $request->get_param('helpscout_api_key'); |
| 183 |
$collection_id = $request->get_param('helpscout_collection_id'); |
| 184 |
|
| 185 |
if ( ! $api_key || ! $collection_id ) { |
| 186 |
return [ |
| 187 |
'status' => 'error', |
| 188 |
'message' => esc_html__('Please provide both API Key and Collection ID', 'betterdocs') |
| 189 |
]; |
| 190 |
} |
| 191 |
|
| 192 |
$api_endpoint = 'https://docsapi.helpscout.net/v1/collections/' . $collection_id . '/articles'; |
| 193 |
|
| 194 |
$headers = array( |
| 195 |
'Authorization' => 'Basic ' . base64_encode($api_key . ':X'), |
| 196 |
'Content-Type' => 'application/json', |
| 197 |
); |
| 198 |
|
| 199 |
$response = wp_remote_get($api_endpoint, array('headers' => $headers)); |
| 200 |
|
| 201 |
if ( is_wp_error( $response ) ) { |
| 202 |
return [ |
| 203 |
'status' => 'error', |
| 204 |
'message' => $response |
| 205 |
]; |
| 206 |
} else { |
| 207 |
$body = wp_remote_retrieve_body($response); |
| 208 |
$data = json_decode($body, true); |
| 209 |
|
| 210 |
// Check if "articles" key exists in the response |
| 211 |
if (isset($data['articles']) && isset($data['articles']['items'])) { |
| 212 |
// Extract the articles |
| 213 |
$articles = $data['articles']['items']; |
| 214 |
|
| 215 |
// Fetch detailed information for each article |
| 216 |
$detailedArticles = []; |
| 217 |
|
| 218 |
foreach ($articles as $article) { |
| 219 |
$articleId = $article['id']; |
| 220 |
$articleNumber = $article['number']; |
| 221 |
|
| 222 |
// Make request to get detailed information for each article |
| 223 |
$articleEndpoint = "https://docsapi.helpscout.net/v1/articles/{$articleNumber}"; |
| 224 |
$articleResponse = wp_remote_get($articleEndpoint, array('headers' => $headers)); |
| 225 |
|
| 226 |
if (!is_wp_error($articleResponse)) { |
| 227 |
$articleBody = wp_remote_retrieve_body($articleResponse); |
| 228 |
$articleData = json_decode($articleBody, true); |
| 229 |
|
| 230 |
if (isset($articleData['article']) && isset($articleData['article']['text'])) { |
| 231 |
// Extract the article text |
| 232 |
$article['text'] = $articleData['article']['text']; |
| 233 |
|
| 234 |
// Extract category details |
| 235 |
$categories = $articleData['article']['categories']; |
| 236 |
$categoryDetails = []; |
| 237 |
|
| 238 |
foreach ($categories as $categoryId) { |
| 239 |
$categoryEndpoint = "https://docsapi.helpscout.net/v1/categories/{$categoryId}"; |
| 240 |
$categoryResponse = wp_remote_get($categoryEndpoint, array('headers' => $headers)); |
| 241 |
|
| 242 |
if (!is_wp_error($categoryResponse)) { |
| 243 |
$categoryBody = wp_remote_retrieve_body($categoryResponse); |
| 244 |
$categoryData = json_decode($categoryBody, true); |
| 245 |
|
| 246 |
if (isset($categoryData['category']['name']) && isset($categoryData['category']['slug'])) { |
| 247 |
// Add category details to the article |
| 248 |
$categoryDetails[] = [ |
| 249 |
'name' => $categoryData['category']['name'], |
| 250 |
'slug' => $categoryData['category']['slug'], |
| 251 |
]; |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
// Add category details to the article |
| 257 |
$article['categories'] = $categoryDetails; |
| 258 |
|
| 259 |
// Add the article to the detailed articles array |
| 260 |
$detailedArticles[] = $article; |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
$wp_importer = new WPImport(''); |
| 266 |
|
| 267 |
$result = $wp_importer->import_helpscout_data( $detailedArticles ); |
| 268 |
|
| 269 |
return [ |
| 270 |
'status' => 'success', |
| 271 |
'articles' => $result |
| 272 |
]; |
| 273 |
} else { |
| 274 |
return [ |
| 275 |
'status' => 'error', |
| 276 |
'message' => esc_html__('Invalid API response format', 'betterdocs') |
| 277 |
]; |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
public function get_existing_slugs( $slugs ) { |
| 285 |
// Initialize the array for existing post slugs |
| 286 |
$existing_post_slugs = []; |
| 287 |
|
| 288 |
// Initialize the query arguments |
| 289 |
$args = [ |
| 290 |
'post_type' => 'docs', // Change 'post' to your custom post type if applicable |
| 291 |
'post_status' => 'publish', |
| 292 |
'posts_per_page' => -1, // Retrieve all posts |
| 293 |
'fields' => 'ids' // Retrieve only post IDs to reduce memory usage |
| 294 |
]; |
| 295 |
|
| 296 |
// Create a new instance of WP_Query |
| 297 |
$query = new WP_Query( $args ); |
| 298 |
|
| 299 |
// Get an array of post slugs from the query |
| 300 |
$all_post_slugs = array_map(function ($post_id) { |
| 301 |
return get_post_field('post_name', $post_id); |
| 302 |
}, $query->posts); |
| 303 |
|
| 304 |
// Restore original post data |
| 305 |
wp_reset_postdata(); |
| 306 |
|
| 307 |
// Find the intersection of the two arrays to get existing post slugs |
| 308 |
$existing_post_slugs = array_intersect($slugs, $all_post_slugs); |
| 309 |
|
| 310 |
return $existing_post_slugs; |
| 311 |
|
| 312 |
} |
| 313 |
public function insights(){ |
| 314 |
return true; |
| 315 |
} |
| 316 |
|
| 317 |
public function get_settings() { |
| 318 |
return betterdocs()->settings->get_all( true ); |
| 319 |
} |
| 320 |
|
| 321 |
public function save_settings( WP_REST_Request $request ) { |
| 322 |
if ( betterdocs()->settings->save_settings( $request->get_params() ) ) { |
| 323 |
return $this->success( __( 'Settings Saved!', 'betterdocs' ) ); |
| 324 |
} |
| 325 |
|
| 326 |
return $this->error( 'nothing_changed', __( 'There are no changes to be saved.', 'betterdocs' ), 200 ); |
| 327 |
} |
| 328 |
|
| 329 |
public function test_reporting( $request ) { |
| 330 |
return $this->container->get( ReportEmail::class )->test_email_report( $request ); |
| 331 |
} |
| 332 |
|
| 333 |
public function do_wizard_tracking() { |
| 334 |
$insights = betterdocs()->admin->plugin_insights( true ); |
| 335 |
// Get our data |
| 336 |
$insights->schedule_tracking(); |
| 337 |
$insights->set_is_tracking_allowed( true, 'betterdocs' ); |
| 338 |
if ( $insights->do_tracking( true ) ) { |
| 339 |
$insights->update_block_notice( 'betterdocs' ); |
| 340 |
} |
| 341 |
|
| 342 |
return true; |
| 343 |
} |
| 344 |
|
| 345 |
public function plugin_insights( $request ) { |
| 346 |
if ( $this->do_wizard_tracking() ) { |
| 347 |
wp_send_json_success( 'done' ); |
| 348 |
} |
| 349 |
wp_send_json_error( 'Something went wrong.' ); |
| 350 |
} |
| 351 |
} |
| 352 |
|