API
2 days ago
Admin
3 weeks ago
Ajax
3 days ago
ExportImport
3 days ago
FormValidator
2 months ago
Frontend
3 days ago
Manager
1 week ago
API.php
3 days ago
Admin.php
2 months ago
Ajax.php
3 days 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 days ago
KirkiBase.php
3 weeks ago
PostsQueryUtils.php
2 months ago
Staging.php
3 days ago
View.php
1 month ago
API.php
120 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 (!isset($_GET['download_file_nonce']) || !wp_verify_nonce($_GET['download_file_nonce'], 'download_file_action')) { |
| 76 | wp_send_json_error(esc_html__('Not authorized.', 'kirki')); |
| 77 | } |
| 78 | |
| 79 | if (!HelperFunctions::has_access(KIRKI_ACCESS_LEVELS['FULL_ACCESS'])) { |
| 80 | wp_send_json_error(esc_html__('Not authorized', 'kirki'), 401); |
| 81 | } |
| 82 | |
| 83 | // TODO: need to check nonce |
| 84 | $this->downloadZIP(); |
| 85 | } |
| 86 | |
| 87 | private function downloadZIP() |
| 88 | { |
| 89 | $upload_dir = wp_upload_dir(); |
| 90 | $file_name = sanitize_file_name($_GET['file-name']); |
| 91 | $file_name = basename($file_name); |
| 92 | // Check if the file has a .zip extension |
| 93 | if (pathinfo($file_name, PATHINFO_EXTENSION) !== 'zip') { |
| 94 | echo esc_html__('Invalid file type.', 'kirki'); |
| 95 | die(); |
| 96 | } |
| 97 | $zipFilePath = $upload_dir['basedir'] . "/$file_name"; |
| 98 | // Send the zip file to the client. |
| 99 | header('Content-Type: application/zip'); |
| 100 | header('Content-Disposition: attachment; filename="' . $file_name . '"'); |
| 101 | header('Content-Length: ' . filesize($zipFilePath)); |
| 102 | $this->output_file_and_cleanup($zipFilePath, $file_name); |
| 103 | exit; |
| 104 | } |
| 105 | |
| 106 | private function output_file_and_cleanup($path, $name) |
| 107 | { |
| 108 | global $wp_filesystem; |
| 109 | if (empty($wp_filesystem)) { |
| 110 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 111 | WP_Filesystem(); |
| 112 | } |
| 113 | |
| 114 | if ($wp_filesystem->exists($path)) { |
| 115 | echo $wp_filesystem->get_contents($path); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 116 | wp_delete_file($path); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 |