Make WordPress Core

Ticket #22435: export.2.diff

File export.2.diff, 4.7 KB (added by dllh, 11 years ago)
  • wp-includes/export/class-wp-export-query.php

     
    2727        private $author;
    2828        private $category;
    2929
     30        public $missing_parents = false;
     31
    3032        public function __construct( $filters = array() ) {
    3133                $this->filters = wp_parse_args( $filters, self::$defaults );
    3234                $this->post_ids = $this->calculate_post_ids();
     
    7678                        return array();
    7779                }
    7880                $categories = (array) get_categories( array( 'get' => 'all' ) );
     81
     82                $this->check_for_orphaned_terms( $categories );
     83
    7984                $categories = self::topologically_sort_terms( $categories );
     85
    8086                return $categories;
    8187        }
    8288
     
    8591                        return array();
    8692                }
    8793                $tags = (array) get_tags( array( 'get' => 'all' ) );
     94
     95                $this->check_for_orphaned_terms( $tags );
     96
    8897                return $tags;
    8998        }
    9099
     
    94103                }
    95104                $custom_taxonomies = get_taxonomies( array( '_builtin' => false ) );
    96105                $custom_terms = (array) get_terms( $custom_taxonomies, array( 'get' => 'all' ) );
     106                $this->check_for_orphaned_terms( $custom_terms );
    97107                $custom_terms = self::topologically_sort_terms( $custom_terms );
    98108                return $custom_terms;
    99109        }
     
    271281                return $sorted;
    272282        }
    273283
     284        private function check_for_orphaned_terms( $terms ) {
     285                $term_ids = array();
     286                $have_parent = array();
     287
     288                foreach ( $terms as $term ) {
     289                        $term_ids[ $term->term_id ] = true;
     290                        if ( $term->parent != 0 )
     291                                $have_parent[] = $term;
     292                }
     293
     294                foreach ( $have_parent as $has_parent ) {
     295                        if ( ! isset( $term_ids[ $has_parent->parent ] ) ) {
     296                                $this->missing_parents = $has_parent;
     297                                throw new WP_Export_Term_Exception( __( 'Term is missing a parent.' ) );
     298                        }
     299                }
     300        }
     301
    274302        private static function get_terms_for_post( $post ) {
    275303                $taxonomies = get_object_taxonomies( $post->post_type );
    276304                if ( empty( $taxonomies ) )
     
    309337
    310338class WP_Export_Exception extends RuntimeException {
    311339}
     340
     341class WP_Export_Term_Exception extends RuntimeException {
     342}
  • wp-includes/export/functions.export.php

     
    2727                }
    2828        }
    2929        return $args;
    30 }
     30}
     31 No newline at end of file
  • wp-includes/export/writers.php

     
    2626        }
    2727
    2828        public function export() {
     29                try {
     30                        $export = $this->get_export();
     31                        $this->send_headers();
     32                        echo $export;
     33                } catch ( WP_Export_Exception $e ) {
     34                        $message = apply_filters( 'export_error_message', $e->getMessage() );
     35                        wp_die( $message, __( 'Export Error' ), array( 'back_link' => true ) );
     36                } catch ( WP_Export_Term_Exception $e ) {
     37                        do_action( 'export_term_orphaned', $this->formatter->export->missing_parents );
     38                        $message = apply_filters( 'export_term_error_message', $e->getMessage() );
     39                        wp_die( $message, __( 'Export Error' ), array( 'back_link' => true ) );
     40                }
     41        }
     42
     43        protected function write( $xml ) {
     44                $this->result .= $xml;
     45        }
     46
     47        protected function get_export() {
     48                $this->result = '';
     49                parent::export();
     50                return $this->result;
     51        }
     52
     53        protected function send_headers() {
    2954                header( 'Content-Description: File Transfer' );
    3055                header( 'Content-Disposition: attachment; filename=' . $this->file_name );
    3156                header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true );
    32                 parent::export();
    3357        }
    34 
    35         protected function write( $xml ) {
    36                 echo $xml;
    37         }
    3858}
    3959
    4060class WP_Export_Returner extends WP_Export_Base_Writer {
     
    4262
    4363        public function export() {
    4464                $this->private = '';
    45                 parent::export();
     65                try {
     66                        parent::export();
     67                } catch ( WP_Export_Exception $e ) {
     68                        $message = apply_filters( 'export_error_message', $e->getMessage() );
     69                        return new WP_Error( 'wp-export-error', $message );
     70                       
     71                } catch ( WP_Export_Term_Exception $e ) {
     72                        do_action( 'export_term_orphaned', $this->formatter->export->missing_parents );
     73                        $message = apply_filters( 'export_term_error_message', $e->getMessage() );
     74                        return new WP_Error( 'wp-export-error', $message );
     75                }
    4676                return $this->result;
    4777        }
    4878        protected function write( $xml ) {
     
    6494                if ( !$this->f ) {
    6595                        throw new WP_Export_Exception( sprintf( __( 'WP Export: error opening %s for writing.' ), $this->file_name ) );
    6696                }
    67                 parent::export();
     97
     98                try {
     99                        parent::export();
     100                } catch ( WP_Export_Exception $e ) {
     101                        throw $e;
     102                } catch ( WP_Export_Term_Exception $e ) {
     103                        throw $e;
     104                }
     105
    68106                fclose( $this->f );
    69107        }
    70108