| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the WindPress package. |
| 5 |
* |
| 6 |
* (c) Joshua Gugun Siagian <suabahasa@gmail.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
declare (strict_types=1); |
| 12 |
namespace WindPress\WindPress\Api\Admin; |
| 13 |
|
| 14 |
use WindPressDeps\Symfony\Component\Finder\Finder; |
| 15 |
use WindPressDeps\Symfony\Component\Finder\Glob; |
| 16 |
use WindPress\WindPress\Api\AbstractApi; |
| 17 |
use WindPress\WindPress\Api\ApiInterface; |
| 18 |
use WP_REST_Request; |
| 19 |
use WP_REST_Response; |
| 20 |
use WP_REST_Server; |
| 21 |
class LocalFileProvider extends AbstractApi implements ApiInterface |
| 22 |
{ |
| 23 |
public function __construct() |
| 24 |
{ |
| 25 |
} |
| 26 |
public function get_prefix(): string |
| 27 |
{ |
| 28 |
return 'admin/local-file-provider'; |
| 29 |
} |
| 30 |
public function register_custom_endpoints(): void |
| 31 |
{ |
| 32 |
register_rest_route(self::API_NAMESPACE, $this->get_prefix() . '/scan', ['methods' => WP_REST_Server::CREATABLE, 'callback' => fn(WP_REST_Request $wprestRequest): WP_REST_Response => $this->scan($wprestRequest), 'permission_callback' => fn(WP_REST_Request $wprestRequest): bool => $this->permission_callback($wprestRequest)]); |
| 33 |
} |
| 34 |
public function scan(WP_REST_Request $wprestRequest): WP_REST_Response |
| 35 |
{ |
| 36 |
$payload = $wprestRequest->get_json_params(); |
| 37 |
$path = $payload['path']; |
| 38 |
$contents = []; |
| 39 |
$finder = new Finder(); |
| 40 |
$finder->ignoreUnreadableDirs()->in(\WP_CONTENT_DIR)->files()->followLinks()->path(Glob::toRegex($path)); |
| 41 |
foreach ($finder as $file) { |
| 42 |
if (!is_readable($file->getPathname())) { |
| 43 |
continue; |
| 44 |
} |
| 45 |
$contents[] = ['name' => $file->getFilename(), 'relative_path' => $file->getRelativePathname(), 'content' => $file->getContents()]; |
| 46 |
} |
| 47 |
return new WP_REST_Response(['contents' => $contents]); |
| 48 |
} |
| 49 |
} |
| 50 |
|