API
3 weeks ago
Admin
3 weeks ago
Ajax
3 weeks ago
ExportImport
3 weeks ago
FormValidator
2 months ago
Frontend
3 weeks ago
Manager
3 weeks ago
API.php
3 weeks ago
Admin.php
2 months ago
Ajax.php
3 weeks ago
Apps.php
3 weeks ago
ContentManager.php
2 months ago
DbQueryUtils.php
2 months ago
ElementVisibilityConditions.php
2 months ago
Frontend.php
2 months ago
HelperFunctions.php
3 weeks ago
KirkiBase.php
3 weeks ago
PostsQueryUtils.php
2 months ago
Staging.php
3 weeks ago
View.php
1 month ago
API.php
116 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Register routes for Media and Frontend |
| 5 | * |
| 6 | * @package kirki |
| 7 | */ |
| 8 | |
| 9 | namespace Kirki; |
| 10 | |
| 11 | use Kirki\API\ContentManager\ContentManagerRest; |
| 12 | |
| 13 | if (!defined('ABSPATH')) { |
| 14 | exit; // Exit if accessed directly. |
| 15 | } |
| 16 | |
| 17 | // use Kirki\API\ContentManager\ContentManagerRest; // Deprecated: routes now handled by Kirki\App\Http\Controllers\Api\CollectionController |
| 18 | use Kirki\API\KirkiComments\KirkiCommentsRest; |
| 19 | use Kirki\API\Media; |
| 20 | use Kirki\API\Frontend\FrontendApi; |
| 21 | |
| 22 | /** |
| 23 | * API Class |
| 24 | */ |
| 25 | class API |
| 26 | { |
| 27 | |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * Initialize the class |
| 32 | * |
| 33 | * @return void |
| 34 | */ |
| 35 | public function __construct() |
| 36 | { |
| 37 | add_action('rest_api_init', array($this, 'register_api')); |
| 38 | add_action('init', array($this, 'download_zip_endpoint')); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Register_api |
| 43 | * |
| 44 | * @return void |
| 45 | */ |
| 46 | public function register_api() |
| 47 | { |
| 48 | // @todo: remove media from here |
| 49 | // Media apis. |
| 50 | // $media = new Media(); |
| 51 | // $media->register_routes(); |
| 52 | |
| 53 | // @todo: remove them later |
| 54 | // ContentManagerRest routes are now registered via routes/api.php |
| 55 | // through the new framework (CollectionController + CollectionItemController). |
| 56 | // $content_manager = new ContentManagerRest(); |
| 57 | // $content_manager->register_routes(); |
| 58 | |
| 59 | // @todo: remove them later |
| 60 | // $kirki_comments = new KirkiCommentsRest(); |
| 61 | // $kirki_comments->register_routes(); |
| 62 | |
| 63 | FrontendApi::register(); |
| 64 | } |
| 65 | |
| 66 | public function download_zip_endpoint() |
| 67 | { |
| 68 | if ( |
| 69 | !isset($_GET['page-export'], $_GET['file-name']) || |
| 70 | 'true' !== $_GET['page-export'] |
| 71 | ) { |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | if (!HelperFunctions::has_access(KIRKI_ACCESS_LEVELS['FULL_ACCESS'])) { |
| 76 | wp_send_json_error('Not authorized', 401); |
| 77 | } |
| 78 | |
| 79 | // TODO: need to check nonce |
| 80 | $this->downloadZIP(); |
| 81 | } |
| 82 | |
| 83 | private function downloadZIP() |
| 84 | { |
| 85 | $upload_dir = wp_upload_dir(); |
| 86 | $file_name = HelperFunctions::sanitize_text($_GET['file-name']); |
| 87 | $file_name = basename($file_name); |
| 88 | // Check if the file has a .zip extension |
| 89 | if (pathinfo($file_name, PATHINFO_EXTENSION) !== 'zip') { |
| 90 | echo 'Invalid file type.'; |
| 91 | die(); |
| 92 | } |
| 93 | $zipFilePath = $upload_dir['basedir'] . "/$file_name"; |
| 94 | // Send the zip file to the client. |
| 95 | header('Content-Type: application/zip'); |
| 96 | header('Content-Disposition: attachment; filename="' . $file_name . '"'); |
| 97 | header('Content-Length: ' . filesize($zipFilePath)); |
| 98 | $this->output_file_and_cleanup($zipFilePath, $file_name); |
| 99 | exit; |
| 100 | } |
| 101 | |
| 102 | private function output_file_and_cleanup($path, $name) |
| 103 | { |
| 104 | global $wp_filesystem; |
| 105 | if (empty($wp_filesystem)) { |
| 106 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 107 | WP_Filesystem(); |
| 108 | } |
| 109 | |
| 110 | if ($wp_filesystem->exists($path)) { |
| 111 | echo $wp_filesystem->get_contents($path); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 112 | wp_delete_file($path); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 |