Making WordPress.org

Changeset 8786


Ignore:
Timestamp:
05/13/2019 10:05:27 PM (7 years ago)
Author:
coffee2code
Message:

Markdown Importer: Update manifest-provided page fields if they've changed but the page's source content hasn't changed.

In cases where a 304 response indicates the source for an imported page hasn't changed since the last fetch, the manifest itself may have been updated with new page-related field values. These currently include menu_order, post_parent, and post_title. Ensure these page attributes get updated.

See #4388.

File:
1 edited

Legend:

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

    r8784 r8786  
    298298
    299299        /**
     300         * Updates a post for fields that are derived from the manifest.
     301         *
     302         * A manifest specifies post-related values that may be updated even if the
     303         * post's markdown page is not.
     304         *
     305         * A manifest can set or affect the following post fields:
     306         * - menu_order
     307         * - post_parent
     308         * - post_title
     309         *
     310         * @param int    $post_id   Page ID to update.
     311         * @param mixed  $title     Page title from markdown. If false, then no title
     312         *                          handling will occur. If null, then title defined in
     313         *                          manifest will be used. If string, then that's the
     314         *                          title and its value will be compared to manifest value.
     315         * @param bool   $do_update Optional. Should the post actually be updated?
     316         *                          Default true.
     317         * @return array The array of post data that would be used for an update.
     318         */
     319        protected function update_post_from_manifest( $post_id, $title = false, $do_update = true ) {
     320                $post_data = [];
     321
     322                $manifest_entry = get_post_meta( $post_id, $this->manifest_entry_meta_key, true );
     323
     324                // Determine value for 'menu_order'.
     325                if ( ! empty( $manifest_entry['order'] ) ) {
     326                        $post_data['menu_order'] = $manifest_entry['order'];
     327                }
     328
     329                // If no title was extracted from markdown doc, use the value defined in manifest.
     330                if ( is_null( $title ) ) {
     331                        if ( ! empty( $manifest_entry['title'] ) ) {
     332                                $post_data['post_title'] = sanitize_text_field( wp_slash( $manifest_entry['title'] ) );
     333                        }
     334                } elseif ( $title ) {
     335                        $post_data['post_title'] = sanitize_text_field( wp_slash( $title ) );
     336                }
     337
     338                $parent_id = wp_get_post_parent_id( $post_id );
     339
     340                // Determine value for 'post_parent'.
     341                if ( ! $manifest_entry ) {
     342                        // Do nothing with regards to possibly changing post parent as we know
     343                        // nothing about previous import.
     344                }
     345                // If post had a parent...
     346                elseif ( $parent_id ) {
     347                        $parent = $parent_id ? get_post( $parent_id ) : null;
     348                        // ...but no parent is now defined, unset parent.
     349                        if ( empty( $manifest_entry['parent'] ) ) {
     350                                $post_data['post_parent'] = '';
     351                        }
     352                        // ...and it appears to differ from parent now defined, find new parent.
     353                        elseif ( $manifest_entry['parent'] !== $parent->post_name ) {
     354                                $find_parent = get_page_by_path( $manifest_entry['parent'], OBJECT, $this->get_post_type() );
     355                                if ( $find_parent ) {
     356                                        $post_data['post_parent'] = $find_parent->ID;
     357                                }
     358                        }
     359                }
     360                // Does this parentless post now have one newly defined?
     361                elseif ( ! empty( $manifest_entry['parent'] ) ) {
     362                        $find_parent = get_page_by_path( $manifest_entry['parent'], OBJECT, $this->get_post_type() );
     363                        if ( $find_parent ) {
     364                                $post_data['post_parent'] = $find_parent->ID;
     365                        }
     366                }
     367
     368                if ( $do_update && $post_data ) {
     369                        $post_data['ID'] = $post_id;
     370
     371                        WP_CLI::log( "Updated {$post_id} from manifest source" );
     372                        wp_update_post( $post_data );
     373                }
     374
     375                return $post_data;
     376        }
     377
     378        /**
    300379         * Update a post from its Markdown source.
    301380         *
     
    329408                        return $response;
    330409                } elseif ( 304 === wp_remote_retrieve_response_code( $response ) ) {
    331                         // No update required!
     410                        // No content update required. Though certain meta fields that are defined
     411                        // in the manifest may have been updated.
     412                        $this->update_post_from_manifest( $post_id );
    332413                        return false;
    333414                } elseif ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
     
    373454                );
    374455
    375                 $manifest_entry = get_post_meta( $post_id, $this->manifest_entry_meta_key, true );
    376 
    377                 if ( ! empty( $manifest_entry['order'] ) ) {
    378                         $post_data['menu_order'] = $manifest_entry['order'];
    379                 }
    380 
    381                 // If no title was extracted from markdown doc, use the value defined in manifest.
    382                 if ( is_null( $title ) ) {
    383                         if ( ! empty( $manifest_entry['title'] ) ) {
    384                                 $post_data['post_title'] = sanitize_text_field( wp_slash( $manifest_entry['title'] ) );
    385                         }
    386                 } else {
    387                         $post_data['post_title'] = sanitize_text_field( wp_slash( $title ) );
    388                 }
    389 
    390                 $parent_id = wp_get_post_parent_id( $post_id );
    391 
    392                 if ( ! $manifest_entry ) {
    393                         // Do nothing with regards to possibly changing post parent as we know
    394                         // nothing about previous import.
    395                 }
    396                 // If post had a parent...
    397                 elseif ( $parent_id ) {
    398                         $parent = $parent_id ? get_post( $parent_id ) : null;
    399                         // ...but no parent is now defined, unset parent.
    400                         if ( empty( $manifest_entry['parent'] ) ) {
    401                                 $post_data['post_parent'] = '';
    402                         }
    403                         // ...and it appears to differ from parent now defined, find new parent.
    404                         elseif ( $manifest_entry['parent'] !== $parent->post_name ) {
    405                                 $find_parent = get_page_by_path( $manifest_entry['parent'], OBJECT, $this->get_post_type() );
    406                                 if ( $find_parent ) {
    407                                         $post_data['post_parent'] = $find_parent->ID;
    408                                 }
    409                         }
    410                 }
    411                 // Does this parentless post now have one newly defined?
    412                 elseif ( ! empty( $manifest_entry['parent'] ) ) {
    413                         $find_parent = get_page_by_path( $manifest_entry['parent'], OBJECT, $this->get_post_type() );
    414                         if ( $find_parent ) {
    415                                 $post_data['post_parent'] = $find_parent->ID;
    416                         }
     456                $fields_from_manifest = $this->update_post_from_manifest( $post_id, $title, false );
     457                if ( $fields_from_manifest ) {
     458                        $post_data = array_merge( $post_data, $fields_from_manifest );
    417459                }
    418460
Note: See TracChangeset for help on using the changeset viewer.