WordPress.org

Making WordPress.org

Changeset 8776


Ignore:
Timestamp:
05/10/2019 06:43:32 PM (5 months ago)
Author:
coffee2code
Message:

Markdown Importer: Support relative paths for per-page markdown source.

The markdown_source values defined in manifests can continue to be fully qualified URLs, but now they can alternatively also be paths relative to the URL path for the manifest file. Parent path notation can be used (e.g. "../file.md"), but attempts to refer to higher level paths (e.g. "../../file.md") will not be honored beyond the parent.

Fixes #4244.
See #4388.
See https://github.com/WordPress/gutenberg/pull/14739.
See https://github.com/WordPress/gutenberg/pull/15254.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-markdown/inc/class-importer.php

    r8720 r8776  
    234234            WP_CLI::log( "Created post {$post_id} for {$doc['slug']}." );
    235235        }
    236         update_post_meta( $post_id, $this->meta_key, esc_url_raw( $doc['markdown_source'] ) );
     236        update_post_meta( $post_id, $this->meta_key, esc_url_raw( $this->generate_markdown_source_url( $doc['markdown_source'] ) ) );
    237237        update_post_meta( $post_id, $this->manifest_entry_meta_key, $doc );
    238238        return get_post( $post_id );
     
    249249        update_post_meta( $post_id, $this->manifest_entry_meta_key, $doc );
    250250
    251         $did_update = update_post_meta( $post_id, $this->meta_key, esc_url_raw( $doc['markdown_source'] ) );
     251        $did_update = update_post_meta( $post_id, $this->meta_key, esc_url_raw( $this->generate_markdown_source_url( $doc['markdown_source'] ) ) );
    252252        if ( ! $did_update ) {
    253253            return false;
     
    422422
    423423    /**
     424     * Generates a fully qualified markdown source URL in the event a relative
     425     * path was defined.
     426     *
     427     * @param string $markdown_source The markdwon_source value defined for a
     428     *                                document in the manifest.
     429     * @return string
     430     */
     431    public function generate_markdown_source_url( $markdown_source ) {
     432        // If source is not explicit URL, then it is relative.
     433        if ( false === strpos( $markdown_source, 'https://' ) ) {
     434            // Base URL is the location of the manifest.
     435            $base = $this->get_manifest_url();
     436            $base = rtrim( dirname( $base ), '/' );
     437
     438            // Markdown source can relatively refer to manifest's parent directory,
     439            // but no higher.
     440            if ( false !== strpos( $markdown_source, '../' ) ) {
     441                $base = rtrim( dirname( $base ), '/' );
     442                $markdown_source = str_replace( '../', '/', $markdown_source );
     443            }
     444
     445            $markdown_source = $base . '/' . ltrim( $markdown_source, '/' );
     446        }
     447
     448        return $markdown_source;
     449    }
     450
     451    /**
    424452     * Retrieve the markdown source URL for a given post.
    425453     */
Note: See TracChangeset for help on using the changeset viewer.