PressBooks\Modules\Import\Import::formSubmit PHP Метод

formSubmit() публичный статический Метод

Catch form submissions
public static formSubmit ( )
    public static function formSubmit()
    {
        // --------------------------------------------------------------------------------------------------------
        // Sanity check
        if (false == static::isFormSubmission() || false == current_user_can('edit_posts')) {
            // Don't do anything in this function, bail.
            return;
        }
        // --------------------------------------------------------------------------------------------------------
        // Determine at what stage of the import we are and do something about it
        $redirect_url = get_admin_url(get_current_blog_id(), '/tools.php?page=pb_import');
        $current_import = get_option('pressbooks_current_import');
        // Revoke
        if (@$_GET['revoke'] && check_admin_referer('pb-revoke-import')) {
            self::revokeCurrentImport();
            \Pressbooks\Redirect\location($redirect_url);
        }
        // only html import uses a url, not a file path
        if (0 !== strcmp($current_import['type_of'], 'html')) {
            // Appends 'last part' of the path to the dynamic first part of the path ($upload_dir)
            $upload_dir = wp_upload_dir();
            $current_import['file'] = trailingslashit($upload_dir['path']) . basename($current_import['file']);
        }
        if (@$_GET['import'] && is_array(@$_POST['chapters']) && is_array($current_import) && isset($current_import['file']) && check_admin_referer('pb-import')) {
            // --------------------------------------------------------------------------------------------------------
            // Do Import
            @set_time_limit(300);
            $ok = false;
            switch ($current_import['type_of']) {
                case 'epub':
                    $importer = new Epub\Epub201();
                    $ok = $importer->import($current_import);
                    break;
                case 'wxr':
                    $importer = new Wordpress\Wxr();
                    $ok = $importer->import($current_import);
                    break;
                case 'odt':
                    $importer = new Odf\Odt();
                    $ok = $importer->import($current_import);
                    break;
                case 'docx':
                    $importer = new Ooxml\Docx();
                    $ok = $importer->import($current_import);
                    break;
                case 'html':
                    $importer = new Html\Xhtml();
                    $ok = $importer->import($current_import);
            }
            $msg = "Tried to import a file of type {$current_import['type_of']} and ";
            $msg .= $ok ? 'succeeded :)' : 'failed :(';
            self::log($msg, $current_import);
            if ($ok) {
                // Success! Redirect to organize page
                $success_url = get_admin_url(get_current_blog_id(), '/admin.php?page=pressbooks');
                \Pressbooks\Redirect\location($success_url);
            }
        } elseif (@$_GET['import'] && !@empty($_FILES['import_file']['name']) && @$_POST['type_of'] && check_admin_referer('pb-import')) {
            // --------------------------------------------------------------------------------------------------------
            // Set the 'pressbooks_current_import' option
            $allowed_file_types = array('epub' => 'application/epub+zip', 'xml' => 'application/xml', 'odt' => 'application/vnd.oasis.opendocument.text', 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
            $overrides = array('test_form' => false, 'mimes' => $allowed_file_types);
            if (!function_exists('wp_handle_upload')) {
                require_once ABSPATH . 'wp-admin/includes/file.php';
            }
            $upload = wp_handle_upload($_FILES['import_file'], $overrides);
            if (!empty($upload['error'])) {
                // Error, redirect back to form
                $_SESSION['pb_notices'][] = $upload['error'];
                \Pressbooks\Redirect\location($redirect_url);
            }
            $ok = false;
            switch ($_POST['type_of']) {
                case 'wxr':
                    $importer = new Wordpress\Wxr();
                    $ok = $importer->setCurrentImportOption($upload);
                    break;
                case 'epub':
                    $importer = new Epub\Epub201();
                    $ok = $importer->setCurrentImportOption($upload);
                    break;
                case 'odt':
                    $importer = new Odf\Odt();
                    $ok = $importer->setCurrentImportOption($upload);
                    break;
                case 'docx':
                    $importer = new Ooxml\Docx();
                    $ok = $importer->setCurrentImportOption($upload);
                    break;
            }
            $msg = "Tried to upload a file of type {$_POST['type_of']} and ";
            $msg .= $ok ? 'succeeded :)' : 'failed :(';
            self::log($msg, $upload);
            if (!$ok) {
                // Not ok?
                $_SESSION['pb_errors'][] = sprintf(__('Your file does not appear to be a valid %s.', 'pressbooks'), strtoupper($_POST['type_of']));
                unlink($upload['file']);
            }
        } elseif (@$_GET['import'] && 'html' === @$_POST['type_of'] && check_admin_referer('pb-import')) {
            // check if it's a valid url
            if (false == filter_var($_POST['import_html'], FILTER_VALIDATE_URL)) {
                $_SESSION['pb_errors'][] = __('Your URL does not appear to be valid', 'pressbooks');
                \Pressbooks\Redirect\location($redirect_url);
            }
            // HEAD request, check for a valid response from server
            $remote_head = wp_remote_head($_POST['import_html']);
            // Something failed
            if (is_wp_error($remote_head)) {
                error_log('\\Pressbooks\\Modules\\Import::formSubmit html import error, wp_remote_head()' . $remote_head->get_error_message());
                $_SESSION['pb_errors'][] = $remote_head->get_error_message();
                \Pressbooks\Redirect\location($redirect_url);
            }
            // weebly.com (and likely some others) prevent HEAD requests, but allow GET requests
            if (200 !== $remote_head['response']['code'] && 405 !== $remote_head['response']['code']) {
                $_SESSION['pb_errors'][] = __('The website you are attempting to reach is not returning a successful response header on a HEAD request: ', 'pressbooks') . $remote_head['response']['code'];
                \Pressbooks\Redirect\location($redirect_url);
            }
            // ensure the media type is HTML (not JSON, or something we can't deal with)
            if (false === strpos($remote_head['headers']['content-type'], 'text/html') && false === strpos($remote_head['headers']['content-type'], 'application/xhtml+xml')) {
                $_SESSION['pb_errors'][] = __('The website you are attempting to reach is not returning HTML content', 'pressbooks');
                \Pressbooks\Redirect\location($redirect_url);
            }
            // GET http request
            $body = wp_remote_get($_POST['import_html']);
            // check for wp error
            if (is_wp_error($body)) {
                $error_message = $body->get_error_message();
                error_log('\\Pressbooks\\Modules\\Import::formSubmit error, import_html' . $error_message);
                $_SESSION['pb_errors'][] = $error_message;
                \Pressbooks\Redirect\location($redirect_url);
            }
            // check for a successful response code on GET request
            if (200 !== $body['response']['code']) {
                $_SESSION['pb_errors'][] = __('The website you are attempting to reach is not returning a successful response on a GET request: ', 'pressbooks') . $body['response']['code'];
                \Pressbooks\Redirect\location($redirect_url);
            }
            // add our url
            $body['url'] = $_POST['import_html'];
            $importer = new Html\Xhtml();
            $ok = $importer->setCurrentImportOption($body);
            $msg = "Tried to upload a file of type {$_POST['type_of']} and ";
            $msg .= $ok ? 'succeeded :)' : 'failed :(';
            self::log($msg, $body['headers']);
            if (!$ok) {
                // Not ok?
                $_SESSION['pb_errors'][] = sprintf(__('Your file does not appear to be a valid %s.', 'pressbooks'), strtoupper($_POST['type_of']));
            }
        }
        // Default, back to form
        \Pressbooks\Redirect\location($redirect_url);
    }