Grav\Plugin\Admin\AdminController::taskDelmedia PHP Метод

taskDelmedia() защищенный Метод

Handles deleting a media file from a page
protected taskDelmedia ( ) : boolean
Результат boolean True if the action was performed.
    protected function taskDelmedia()
    {
        if (!$this->authorizeTask('delete media', ['admin.pages', 'admin.super'])) {
            return false;
        }
        $page = $this->admin->page(true);
        if (!$page) {
            $this->admin->json_response = ['status' => 'error', 'message' => $this->admin->translate('PLUGIN_ADMIN.NO_PAGE_FOUND')];
            return false;
        }
        $filename = !empty($this->post['filename']) ? $this->post['filename'] : null;
        if (!$filename) {
            $this->admin->json_response = ['status' => 'error', 'message' => $this->admin->translate('PLUGIN_ADMIN.NO_FILE_FOUND')];
            return false;
        }
        $targetPath = $page->path() . '/' . $filename;
        if (!file_exists($targetPath)) {
            $this->admin->json_response = ['status' => 'error', 'message' => $this->admin->translate('PLUGIN_ADMIN.FILE_NOT_FOUND') . ': ' . $filename];
            return false;
        }
        $fileParts = pathinfo($filename);
        $result = unlink($targetPath);
        if (!$result) {
            $this->admin->json_response = ['status' => 'error', 'message' => $this->admin->translate('PLUGIN_ADMIN.FILE_COULD_NOT_BE_DELETED') . ': ' . $filename];
            return false;
        }
        foreach (scandir($page->path()) as $file) {
            if (preg_match("/{$fileParts['filename']}@\\d+x\\.{$fileParts['extension']}\$/", $file)) {
                unlink($page->path() . '/' . $file);
            }
        }
        $this->grav->fireEvent('onAdminAfterDelMedia', new Event(['page' => $page]));
        $this->admin->json_response = ['status' => 'success', 'message' => $this->admin->translate('PLUGIN_ADMIN.FILE_DELETED') . ': ' . $filename];
        return true;
    }