| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDeveloper\BetterDocs\REST; |
| 4 |
|
| 5 |
use WPDeveloper\BetterDocs\Admin\ReportEmail; |
| 6 |
use WP_REST_Request; |
| 7 |
use WPDeveloper\BetterDocs\Core\BaseAPI; |
| 8 |
use WPDeveloper\BetterDocs\Admin\WPExporter; |
| 9 |
use WPDeveloper\BetterDocs\Admin\Importer\WPImport; |
| 10 |
use WPDeveloper\BetterDocs\Admin\Importer\Parsers\WXR_Parser_XML; |
| 11 |
use WP_Query; |
| 12 |
|
| 13 |
class Settings extends BaseAPI { |
| 14 |
|
| 15 |
public function permission_check() { |
| 16 |
return true; // current_user_can( 'edit_docs_settings' ); |
| 17 |
} |
| 18 |
|
| 19 |
public function register() { |
| 20 |
$this->get( 'settings', [$this, 'get_settings'] ); |
| 21 |
$this->post( 'settings', [$this, 'save_settings'] ); |
| 22 |
$this->post( 'plugin_insights', [$this, 'plugin_insights'] ); |
| 23 |
|
| 24 |
$this->post( 'reporting-test', [$this, 'test_reporting'] ); |
| 25 |
$this->post( 'export-docs', [$this, 'export_docs'] ); |
| 26 |
$this->post( 'export-settings', [$this, 'export_settings'] ); |
| 27 |
$this->post( 'import-docs', [$this, 'import_docs'] ); |
| 28 |
$this->post( 'import-settings', [$this, 'import_settings'] ); |
| 29 |
$this->post( 'perse-file', [$this, 'perse_xml'] ); |
| 30 |
} |
| 31 |
|
| 32 |
public function export_docs( WP_REST_Request $request ) { |
| 33 |
$args = [ |
| 34 |
'include_post_featured_image_as_attachment' => true, |
| 35 |
'status' => 'publish', |
| 36 |
'content' => 'docs' |
| 37 |
]; |
| 38 |
|
| 39 |
$data = $request->get_params(); |
| 40 |
|
| 41 |
if ( $data['export_type'] == 'docs' && $data['export_docs'][0] != 'all' ) { |
| 42 |
$args['post__in'] = $data['export_docs']; |
| 43 |
} |
| 44 |
|
| 45 |
if ( $data['export_type'] == 'doc_category' && $data['export_categories'][0] != 'all' ) { |
| 46 |
$args['category_terms'] = $data['export_categories']; |
| 47 |
} |
| 48 |
|
| 49 |
if ( $data['export_type'] == 'knowledge_base' && $data['export_kbs'][0] != 'all' ) { |
| 50 |
$args['kb_terms'] = $data['export_kbs']; |
| 51 |
} |
| 52 |
|
| 53 |
$exporter = new WPExporter( $args ); |
| 54 |
|
| 55 |
return $exporter->run(); |
| 56 |
} |
| 57 |
|
| 58 |
public function export_settings( WP_REST_Request $request ) { |
| 59 |
$betterdocs_settings = get_option('betterdocs_settings'); |
| 60 |
$json_str = json_encode($betterdocs_settings, JSON_PRETTY_PRINT); |
| 61 |
$file_name = 'betterdocs-settings.json'; |
| 62 |
return [ |
| 63 |
'success' => true, |
| 64 |
'data' => [ |
| 65 |
'filename' => $file_name, |
| 66 |
'filetype' => 'text/json', |
| 67 |
'download' => $json_str, |
| 68 |
] |
| 69 |
]; |
| 70 |
} |
| 71 |
|
| 72 |
public function import_settings( WP_REST_Request $request ) { |
| 73 |
$settings = $request->get_param('settings'); |
| 74 |
// Decode the JSON data into a PHP array |
| 75 |
$settings = json_decode($settings, true); |
| 76 |
$save = update_option( 'betterdocs_settings', $settings ); |
| 77 |
|
| 78 |
if ( $save == true ) { |
| 79 |
return [ |
| 80 |
'status' => 'success' |
| 81 |
]; |
| 82 |
} else { |
| 83 |
return [ |
| 84 |
'status' => 'failed' |
| 85 |
]; |
| 86 |
} |
| 87 |
} |
| 88 |
public function import_docs( WP_REST_Request $request ) { |
| 89 |
$existing_slug = $request->get_param('existing_slug'); |
| 90 |
$action = $request->get_param('action'); |
| 91 |
|
| 92 |
$files = $request->get_file_params(); |
| 93 |
|
| 94 |
$args = [ |
| 95 |
'fetch_attachments' => true, |
| 96 |
'existing_slug' => $existing_slug, |
| 97 |
'action' => $action |
| 98 |
]; |
| 99 |
|
| 100 |
$file = $files['file']['tmp_name']; |
| 101 |
|
| 102 |
$wp_importer = new WPImport( $file, $args ); |
| 103 |
|
| 104 |
return $wp_importer->run(); |
| 105 |
} |
| 106 |
|
| 107 |
public function perse_xml( WP_REST_Request $request ) { |
| 108 |
$data = $request->get_params(); |
| 109 |
|
| 110 |
// Initialize the array for existing post slugs |
| 111 |
$existing_post_slugs = []; |
| 112 |
|
| 113 |
// Initialize the query arguments |
| 114 |
$args = [ |
| 115 |
'post_type' => 'docs', // Change 'post' to your custom post type if applicable |
| 116 |
'post_status' => 'publish', |
| 117 |
'posts_per_page' => -1, // Retrieve all posts |
| 118 |
'fields' => 'ids', // Retrieve only post IDs to reduce memory usage |
| 119 |
]; |
| 120 |
|
| 121 |
// Create a new instance of WP_Query |
| 122 |
$query = new WP_Query($args); |
| 123 |
|
| 124 |
// Get an array of post slugs from the query |
| 125 |
$all_post_slugs = array_map(function ($post_id) { |
| 126 |
return get_post_field('post_name', $post_id); |
| 127 |
}, $query->posts); |
| 128 |
|
| 129 |
// Find the intersection of the two arrays to get existing post slugs |
| 130 |
$existing_post_slugs = array_intersect($data['posts'], $all_post_slugs); |
| 131 |
|
| 132 |
// Output the array of existing post slugs |
| 133 |
return [ |
| 134 |
'status' => 'success', |
| 135 |
'posts' => $existing_post_slugs, |
| 136 |
]; |
| 137 |
|
| 138 |
// Restore original post data |
| 139 |
wp_reset_postdata(); |
| 140 |
} |
| 141 |
|
| 142 |
public function insights(){ |
| 143 |
return true; |
| 144 |
} |
| 145 |
|
| 146 |
public function get_settings() { |
| 147 |
return betterdocs()->settings->get_all( true ); |
| 148 |
} |
| 149 |
|
| 150 |
public function save_settings( WP_REST_Request $request ) { |
| 151 |
if ( betterdocs()->settings->save_settings( $request->get_params() ) ) { |
| 152 |
return $this->success( __( 'Settings Saved!', 'betterdocs' ) ); |
| 153 |
} |
| 154 |
|
| 155 |
return $this->error( 'nothing_changed', __( 'There are no changes to be saved.', 'betterdocs' ), 200 ); |
| 156 |
} |
| 157 |
|
| 158 |
public function test_reporting( $request ){ |
| 159 |
return $this->container->get(ReportEmail::class)->test_email_report( $request ); |
| 160 |
} |
| 161 |
|
| 162 |
public function do_wizard_tracking() { |
| 163 |
$insights = betterdocs()->admin->plugin_insights( true ); |
| 164 |
// Get our data |
| 165 |
$insights->schedule_tracking(); |
| 166 |
$insights->set_is_tracking_allowed( true, 'betterdocs' ); |
| 167 |
if ( $insights->do_tracking( true ) ) { |
| 168 |
$insights->update_block_notice( 'betterdocs' ); |
| 169 |
} |
| 170 |
|
| 171 |
return true; |
| 172 |
} |
| 173 |
|
| 174 |
public function plugin_insights( $request ){ |
| 175 |
if ( $this->do_wizard_tracking() ) { |
| 176 |
wp_send_json_success('done'); |
| 177 |
} |
| 178 |
wp_send_json_error('Something went wrong.'); |
| 179 |
} |
| 180 |
} |
| 181 |
|