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