diff --git a/.distignore b/.distignore index b964b40c7..95b52fb02 100644 --- a/.distignore +++ b/.distignore @@ -6,6 +6,8 @@ .travis.yml behat.yml circle.yml +phpcs.xml.dist +phpunit.xml.dist bin/ features/ utils/ diff --git a/.gitignore b/.gitignore index ff4941991..bcf211b32 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,6 @@ vendor/ *.tar.gz composer.lock *.log +phpunit.xml +phpcs.xml +.phpcs.xml diff --git a/composer.json b/composer.json index 449206cf4..6593ac354 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "wp-cli/entity-command": "^1.3 || ^2", "wp-cli/extension-command": "^1.2 || ^2", "wp-cli/import-command": "^1 || ^2", - "wp-cli/wp-cli-tests": "^2.0.7" + "wp-cli/wp-cli-tests": "^2.1" }, "config": { "process-timeout": 7200, diff --git a/export-command.php b/export-command.php index 4add2dfca..cdc79f006 100644 --- a/export-command.php +++ b/export-command.php @@ -4,9 +4,9 @@ return; } -$autoload = dirname( __FILE__ ) . '/vendor/autoload.php'; -if ( file_exists( $autoload ) ) { - require_once $autoload; +$wpcli_export_autoloader = dirname( __FILE__ ) . '/vendor/autoload.php'; +if ( file_exists( $wpcli_export_autoloader ) ) { + require_once $wpcli_export_autoloader; } WP_CLI::add_command( 'export', 'Export_Command' ); diff --git a/features/export.feature b/features/export.feature index c4e28552b..9a393ab87 100644 --- a/features/export.feature +++ b/features/export.feature @@ -454,31 +454,32 @@ Feature: Export content. Scenario: Export posts using --max_num_posts Given a WP install + And I run `wp site empty --yes` And a count-instances.php file: """ ' . $args[0] . '<\/wp:post_type>#', file_get_contents( 'php://stdin' ), $matches ); + echo 'count=' . preg_match_all( '#' . $args[0] . '<\/wp:post_type>#', file_get_contents( 'php://stdin' ), $matches ); """ When I run `wp post generate --post_type=post --count=10` And I run `wp export --post_type=post --max_num_posts=1 --stdout | wp --skip-wordpress eval-file count-instances.php post` - Then STDOUT should be: + Then STDOUT should contain: """ - 1 + count=1 """ When I run `wp post generate --post_type=post --count=10` And I run `wp post generate --post_type=attachment --count=10` And I run `wp export --max_num_posts=1 --stdout | wp --skip-wordpress eval-file count-instances.php "(post|attachment)"` - Then STDOUT should be: + Then STDOUT should contain: """ - 1 + count=1 """ When I run `wp export --max_num_posts=5 --stdout | wp --skip-wordpress eval-file count-instances.php "(post|attachment)"` - Then STDOUT should be: + Then STDOUT should contain: """ - 5 + count=5 """ Scenario: Export a site with a custom filename format diff --git a/functions.php b/functions.php index 492e97e9b..8716c1972 100644 --- a/functions.php +++ b/functions.php @@ -1,16 +1,17 @@ array(), - 'format' => 'WP_Export_WXR_Formatter', - 'writer' => 'WP_Export_Returner', + $defaults = array( + 'filters' => array(), + 'format' => 'WP_Export_WXR_Formatter', + 'writer' => 'WP_Export_Returner', 'writer_args' => null, ); - $args = wp_parse_args( $args, $defaults ); + $args = wp_parse_args( $args, $defaults ); $export_query = new WP_Export_Query( $args['filters'] ); - $formatter = new $args['format']( $export_query ); - $writer = new $args['writer']( $formatter, $args['writer_args'] ); + $formatter = new $args['format']( $export_query ); + $writer = new $args['writer']( $formatter, $args['writer_args'] ); try { return $writer->export(); } catch ( WP_Export_Exception $e ) { @@ -18,24 +19,14 @@ function wp_export( $args = array() ) { } } -function wp_export_new_style_args_from_old_style_args( $args ) { - if ( isset( $args['content'] ) ) { - if ( 'all' == $args['content'] ) { - unset( $args['content'] ); - } else { - $args['post_type'] = $args['content']; - } - } - return $args; -} - -// TEMPORARY +// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound,WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid -- Renaming breaks Phar compat. function _wp_export_build_IN_condition( $column_name, $values, $format = '%s' ) { global $wpdb; - if ( !is_array( $values ) || empty( $values ) ) { + if ( ! is_array( $values ) || empty( $values ) ) { return ''; } $formats = implode( ', ', array_fill( 0, count( $values ), $format ) ); - return $wpdb->prepare( "$column_name IN ($formats)", $values ); + // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- $column_name_sql escaped as ident, $formats hardcoded value. + return $wpdb->prepare( "{$column_name} IN ({$formats})", $values ); } diff --git a/phpcs.xml.dist b/phpcs.xml.dist new file mode 100644 index 000000000..7e5bf5fce --- /dev/null +++ b/phpcs.xml.dist @@ -0,0 +1,71 @@ + + + Custom ruleset for WP-CLI export-command + + + + + . + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */src/Export_Command\.php$ + */src/WP_(Map|Post_IDs)_Iterator\.php$ + */src/WP_(Export_Term|Iterator)_Exception\.php$ + */src/WP_Export_WXR_Formatter\.php$ + */src/WP_Export_XML_Over_HTTP\.php$ + */src/WP_Export_(Base|File|Split_Files|Stdout)?_Writer\.php$ + */src/WP_Export_(Exception|Oxymel|Query|Returner)\.php$ + + + + + + */src/Export_Command\.php$ + + + diff --git a/src/Export_Command.php b/src/Export_Command.php index 07a950e15..0039e346c 100644 --- a/src/Export_Command.php +++ b/src/Export_Command.php @@ -1,5 +1,8 @@ NULL, - 'stdout' => FALSE, - 'start_date' => NULL, - 'end_date' => NULL, - 'post_type' => NULL, - 'post_type__not_in' => NULL, - 'max_num_posts' => NULL, - 'author' => NULL, - 'category' => NULL, - 'post_status' => NULL, - 'post__in' => NULL, - 'with_attachments' => TRUE, // or FALSE if user requested some post__in - 'start_id' => NULL, - 'skip_comments' => NULL, + $defaults = [ + 'dir' => null, + 'stdout' => false, + 'start_date' => null, + 'end_date' => null, + 'post_type' => null, + 'post_type__not_in' => null, + 'max_num_posts' => null, + 'author' => null, + 'category' => null, + 'post_status' => null, + 'post__in' => null, + 'with_attachments' => true, // or FALSE if user requested some post__in + 'start_id' => null, + 'skip_comments' => null, 'max_file_size' => 15, 'filename_format' => '{site}.wordpress.{date}.{n}.xml', - ); - + ]; - if (! empty( $assoc_args['stdout'] ) && ( ! empty( $assoc_args['dir'] ) || ! empty( $assoc_args['filename_format'] ) ) ) { + if ( ! empty( $assoc_args['stdout'] ) && ( ! empty( $assoc_args['dir'] ) || ! empty( $assoc_args['filename_format'] ) ) ) { WP_CLI::error( '--stdout and --dir cannot be used together.' ); } - if ( !empty( $assoc_args['post__in'] ) && empty( $assoc_args['with_attachments'] ) ) { - $defaults['with_attachments'] = FALSE; + if ( ! empty( $assoc_args['post__in'] ) && empty( $assoc_args['with_attachments'] ) ) { + $defaults['with_attachments'] = false; } $assoc_args = wp_parse_args( $assoc_args, $defaults ); $this->validate_args( $assoc_args ); - $this->export_args['with_attachments'] = WP_CLI\Utils\get_flag_value( + $this->export_args['with_attachments'] = Utils\get_flag_value( $assoc_args, 'with_attachments', $defaults['with_attachments'] ); - if ( !function_exists( 'wp_export' ) ) { + if ( ! function_exists( 'wp_export' ) ) { self::load_export_api(); } @@ -164,28 +166,35 @@ public function __invoke( $_, $assoc_args ) { WP_CLI::log( 'Starting export process...' ); } - add_action( 'wp_export_new_file', function( $file_path ) { - WP_CLI::log( sprintf( "Writing to file %s", $file_path ) ); - WP_CLI\Utils\wp_clear_object_cache(); - } ); + add_action( + 'wp_export_new_file', + function( $file_path ) { + WP_CLI::log( sprintf( 'Writing to file %s', $file_path ) ); + Utils\wp_clear_object_cache(); + } + ); try { if ( $this->stdout ) { - wp_export( array( - 'filters' => $this->export_args, - 'writer' => 'WP_Export_Stdout_Writer', - 'writer_args' => NULL - ) ); + wp_export( + [ + 'filters' => $this->export_args, + 'writer' => 'WP_Export_Stdout_Writer', + 'writer_args' => null, + ] + ); } else { - wp_export( array( - 'filters' => $this->export_args, - 'writer' => 'WP_Export_Split_Files_Writer', - 'writer_args' => array( - 'max_file_size' => $this->max_file_size, - 'destination_directory' => $this->wxr_path, - 'filename_template' => self::get_filename_template( $assoc_args['filename_format'] ), - ) - ) ); + wp_export( + [ + 'filters' => $this->export_args, + 'writer' => 'WP_Export_Split_Files_Writer', + 'writer_args' => [ + 'max_file_size' => $this->max_file_size, + 'destination_directory' => $this->wxr_path, + 'filename_template' => self::get_filename_template( $assoc_args['filename_format'] ), + ], + ] + ); } } catch ( Exception $e ) { WP_CLI::error( $e->getMessage() ); @@ -201,7 +210,7 @@ private static function get_filename_template( $filename_format ) { if ( empty( $sitename ) ) { $sitename = 'site'; } - return str_replace( array( '{site}', '{date}', '{n}' ), array( $sitename, date( 'Y-m-d' ), '%03d' ), $filename_format ); + return str_replace( [ '{site}', '{date}', '{n}' ], [ $sitename, date( 'Y-m-d' ), '%03d' ], $filename_format ); } private static function load_export_api() { @@ -212,32 +221,31 @@ private function validate_args( $args ) { $has_errors = false; foreach ( $args as $key => $value ) { - if ( is_callable( array( $this, 'check_' . $key ) ) ) { - $result = call_user_func( array( $this, 'check_' . $key ), $value ); - if ( false === $result ) + if ( is_callable( [ $this, 'check_' . $key ] ) ) { + $result = call_user_func( [ $this, 'check_' . $key ], $value ); + if ( false === $result ) { $has_errors = true; + } } } if ( $args['stdout'] ) { - $this->wxr_path = NULL; - $this->stdout = TRUE; + $this->wxr_path = null; + $this->stdout = true; } if ( $has_errors ) { - WP_CLI::halt(1); + WP_CLI::halt( 1 ); } } private function check_dir( $path ) { if ( empty( $path ) ) { $path = getcwd(); - } elseif ( !is_dir( $path ) ) { + } elseif ( ! is_dir( $path ) ) { WP_CLI::error( sprintf( "The directory '%s' does not exist.", $path ) ); - return false; } elseif ( ! is_writable( $path ) ) { WP_CLI::error( sprintf( "The directory '%s' is not writable.", $path ) ); - return false; } $this->wxr_path = trailingslashit( $path ); @@ -246,12 +254,13 @@ private function check_dir( $path ) { } private function check_start_date( $date ) { - if ( is_null( $date ) ) + if ( null === $date ) { return true; + } $time = strtotime( $date ); - if ( !empty( $date ) && !$time ) { - WP_CLI::warning( sprintf( "The start_date %s is invalid.", $date ) ); + if ( ! empty( $date ) && ! $time ) { + WP_CLI::warning( sprintf( 'The start_date %s is invalid.', $date ) ); return false; } $this->export_args['start_date'] = date( 'Y-m-d', $time ); @@ -259,12 +268,13 @@ private function check_start_date( $date ) { } private function check_end_date( $date ) { - if ( is_null( $date ) ) + if ( null === $date ) { return true; + } $time = strtotime( $date ); - if ( !empty( $date ) && !$time ) { - WP_CLI::warning( sprintf( "The end_date %s is invalid.", $date ) ); + if ( ! empty( $date ) && ! $time ) { + WP_CLI::warning( sprintf( 'The end_date %s is invalid.', $date ) ); return false; } $this->export_args['end_date'] = date( 'Y-m-d', $time ); @@ -272,19 +282,22 @@ private function check_end_date( $date ) { } private function check_post_type( $post_type ) { - if ( is_null( $post_type ) || 'any' === $post_type ) + if ( null === $post_type || 'any' === $post_type ) { return true; + } - $post_type = array_unique( array_filter( explode( ',', $post_type ) ) ); + $post_type = array_unique( array_filter( explode( ',', $post_type ) ) ); $post_types = get_post_types(); foreach ( $post_type as $type ) { - if ( ! in_array( $type, $post_types ) ) { - WP_CLI::warning( sprintf( - 'The post type %s does not exist. Choose "any" or any of these existing post types instead: %s', - $type, - implode( ", ", $post_types ) - ) ); + if ( ! in_array( $type, $post_types, true ) ) { + WP_CLI::warning( + sprintf( + 'The post type %s does not exist. Choose "any" or any of these existing post types instead: %s', + $type, + implode( ', ', $post_types ) + ) + ); return false; } } @@ -293,21 +306,23 @@ private function check_post_type( $post_type ) { } private function check_post_type__not_in( $post_type ) { - if ( is_null( $post_type ) ) { + if ( null === $post_type ) { return true; } - $post_type = array_unique( array_filter( explode( ',', $post_type ) ) ); + $post_type = array_unique( array_filter( explode( ',', $post_type ) ) ); $post_types = get_post_types(); - $keep_post_types = array(); + $keep_post_types = []; foreach ( $post_type as $type ) { - if ( ! in_array( $type, $post_types ) ) { - WP_CLI::warning( sprintf( - 'The post type %s does not exist. Use any of these existing post types instead: %s', - $type, - implode( ", ", $post_types ) - ) ); + if ( ! in_array( $type, $post_types, true ) ) { + WP_CLI::warning( + sprintf( + 'The post type %s does not exist. Use any of these existing post types instead: %s', + $type, + implode( ', ', $post_types ) + ) + ); return false; } } @@ -316,13 +331,14 @@ private function check_post_type__not_in( $post_type ) { } private function check_post__in( $post__in ) { - if ( is_null( $post__in ) ) + if ( null === $post__in ) { return true; + } $separator = false !== stripos( $post__in, ' ' ) ? ' ' : ','; - $post__in = array_filter( array_unique( array_map( 'intval', explode( $separator, $post__in ) ) ) ); + $post__in = array_filter( array_unique( array_map( 'intval', explode( $separator, $post__in ) ) ) ); if ( empty( $post__in ) ) { - WP_CLI::warning( "post__in should be comma-separated post IDs." ); + WP_CLI::warning( 'post__in should be comma-separated post IDs.' ); return false; } // New exporter uses a different argument. @@ -331,7 +347,7 @@ private function check_post__in( $post__in ) { } private function check_start_id( $start_id ) { - if ( is_null( $start_id ) ) { + if ( null === $start_id ) { return true; } @@ -339,7 +355,7 @@ private function check_start_id( $start_id ) { // Post IDs must be greater than 0. if ( 0 >= $start_id ) { - WP_CLI::warning( sprintf( __( 'Invalid start ID: %d' ), $start_id ) ); + WP_CLI::warning( "Invalid start ID: {$start_id}" ); return false; } @@ -348,26 +364,31 @@ private function check_start_id( $start_id ) { } private function check_author( $author ) { - if ( is_null( $author ) ) + if ( null === $author ) { return true; + } - $authors = get_users_of_blog(); + // phpcs:ignore WordPress.WP.DeprecatedFunctions.get_users_of_blogFound -- Fallback. + $authors = function_exists( 'get_users' ) ? get_users() : get_users_of_blog(); if ( empty( $authors ) || is_wp_error( $authors ) ) { - WP_CLI::warning( sprintf( "Could not find any authors in this blog." ) ); + WP_CLI::warning( sprintf( 'Could not find any authors in this blog.' ) ); return false; } $hit = false; - foreach( $authors as $user ) { - if ( $hit ) + foreach ( $authors as $user ) { + if ( $hit ) { break; - if ( (int) $author == $user->ID || $author == $user->user_login ) + } + if ( (int) $author === $user->ID || $author === $user->user_login ) { $hit = $user->ID; + } } if ( false === $hit ) { - $authors_nice = array(); - foreach( $authors as $_author ) + $authors_nice = []; + foreach ( $authors as $_author ) { $authors_nice[] = sprintf( '%s (%s)', $_author->user_login, $_author->display_name ); - WP_CLI::warning( sprintf( 'Could not find a matching author for %s. The following authors exist: %s', $author, implode( ", ", $authors_nice ) ) ); + } + WP_CLI::warning( sprintf( 'Could not find a matching author for %s. The following authors exist: %s', $author, implode( ', ', $authors_nice ) ) ); return false; } @@ -376,19 +397,20 @@ private function check_author( $author ) { } private function check_max_num_posts( $num ) { - if ( ! is_null( $num ) && ( ! is_numeric( $num ) || $num <= 0) ) { - WP_CLI::warning( sprintf( "max_num_posts should be a positive integer.", $num ) ); + if ( null !== $num && ( ! is_numeric( $num ) || $num <= 0 ) ) { + WP_CLI::warning( sprintf( 'max_num_posts should be a positive integer.', $num ) ); return false; } - $this->export_args['max_num_posts'] = (int)$num; + $this->export_args['max_num_posts'] = (int) $num; return true; } private function check_category( $category ) { - if ( is_null( $category ) ) + if ( null === $category ) { return true; + } $term = category_exists( $category ); if ( empty( $term ) || is_wp_error( $term ) ) { @@ -400,8 +422,9 @@ private function check_category( $category ) { } private function check_post_status( $status ) { - if ( is_null( $status ) ) + if ( null === $status ) { return true; + } $stati = get_post_statuses(); if ( empty( $stati ) || is_wp_error( $stati ) ) { @@ -409,8 +432,8 @@ private function check_post_status( $status ) { return false; } - if ( !isset( $stati[$status] ) ) { - WP_CLI::warning( sprintf( 'Could not find a post_status matching %s. Here is a list of available stati: %s', $status, implode( ", ", array_keys( $stati ) ) ) ); + if ( ! isset( $stati[ $status ] ) ) { + WP_CLI::warning( sprintf( 'Could not find a post_status matching %s. Here is a list of available stati: %s', $status, implode( ', ', array_keys( $stati ) ) ) ); return false; } $this->export_args['status'] = $status; @@ -418,10 +441,11 @@ private function check_post_status( $status ) { } private function check_skip_comments( $skip ) { - if ( is_null( $skip ) ) + if ( null === $skip ) { return true; + } - if ( (int) $skip <> 0 && (int) $skip <> 1 ) { + if ( 0 !== (int) $skip && 1 !== (int) $skip ) { WP_CLI::warning( 'skip_comments needs to be 0 (no) or 1 (yes).' ); return false; } @@ -430,8 +454,8 @@ private function check_skip_comments( $skip ) { } private function check_max_file_size( $size ) { - if ( !is_numeric( $size ) ) { - WP_CLI::warning( sprintf( "max_file_size should be numeric.", $size ) ); + if ( ! is_numeric( $size ) ) { + WP_CLI::warning( sprintf( 'max_file_size should be numeric.', $size ) ); return false; } diff --git a/src/WP_Export_Base_Writer.php b/src/WP_Export_Base_Writer.php index 21f7fd35e..28e6583d0 100644 --- a/src/WP_Export_Base_Writer.php +++ b/src/WP_Export_Base_Writer.php @@ -3,13 +3,13 @@ abstract class WP_Export_Base_Writer { protected $formatter; - function __construct( $formatter ) { + public function __construct( $formatter ) { $this->formatter = $formatter; } public function export() { $this->write( $this->formatter->before_posts() ); - foreach( $this->formatter->posts() as $post_in_wxr ) { + foreach ( $this->formatter->posts() as $post_in_wxr ) { $this->write( $post_in_wxr ); } $this->write( $this->formatter->after_posts() ); diff --git a/src/WP_Export_File_Writer.php b/src/WP_Export_File_Writer.php index 82be6fae5..6bbde8572 100644 --- a/src/WP_Export_File_Writer.php +++ b/src/WP_Export_File_Writer.php @@ -11,11 +11,11 @@ public function __construct( $formatter, $file_name ) { public function export() { $this->f = fopen( $this->file_name, 'w' ); - if ( !$this->f ) { - throw new WP_Export_Exception( sprintf( __( 'WP Export: error opening %s for writing.' ), $this->file_name ) ); + if ( ! $this->f ) { + throw new WP_Export_Exception( "WP Export: error opening {$this->file_name} for writing." ); } - try { + try { parent::export(); } catch ( WP_Export_Exception $e ) { throw $e; @@ -27,7 +27,7 @@ public function export() { } protected function write( $xml ) { - $res = fwrite( $this->f, $xml); + $res = fwrite( $this->f, $xml ); if ( false === $res ) { throw new WP_Export_Exception( __( 'WP Export: error writing to export file.' ) ); } diff --git a/src/WP_Export_Oxymel.php b/src/WP_Export_Oxymel.php index 74c1a7cf1..501dca0fe 100644 --- a/src/WP_Export_Oxymel.php +++ b/src/WP_Export_Oxymel.php @@ -16,7 +16,7 @@ public function optional_cdata( $tag_name, $contents ) { } public function cdata( $text ) { - if ( !seems_utf8( $text ) ) { + if ( ! seems_utf8( $text ) ) { $text = utf8_encode( $text ); } return parent::cdata( $text ); diff --git a/src/WP_Export_Query.php b/src/WP_Export_Query.php index 914ef93a6..856fbe024 100644 --- a/src/WP_Export_Query.php +++ b/src/WP_Export_Query.php @@ -1,4 +1,7 @@ null, - 'post_type' => null, - 'status' => null, - 'author' => null, - 'start_date' => null, - 'end_date' => null, - 'start_id' => null, - 'max_num_posts' => NULL, - 'category' => null, + 'post_ids' => null, + 'post_type' => null, + 'status' => null, + 'author' => null, + 'start_date' => null, + 'end_date' => null, + 'start_id' => null, + 'max_num_posts' => null, + 'category' => null, ); private $post_ids; @@ -24,7 +27,7 @@ class WP_Export_Query { private $xml_gen; private $wheres = array(); - private $joins = array(); + private $joins = array(); private $author; private $category; @@ -32,7 +35,7 @@ class WP_Export_Query { public $missing_parents = false; public function __construct( $filters = array() ) { - $this->filters = wp_parse_args( $filters, self::$defaults ); + $this->filters = wp_parse_args( $filters, self::$defaults ); $this->post_ids = $this->calculate_post_ids(); } @@ -46,24 +49,25 @@ public function charset() { public function site_metadata() { $metadata = array( - 'name' => $this->bloginfo_rss( 'name' ), - 'url' => $this->bloginfo_rss( 'url' ), - 'language' => $this->bloginfo_rss( 'language' ), + 'name' => $this->bloginfo_rss( 'name' ), + 'url' => $this->bloginfo_rss( 'url' ), + 'language' => $this->bloginfo_rss( 'language' ), 'description' => $this->bloginfo_rss( 'description' ), - 'pubDate' => date( 'D, d M Y H:i:s +0000' ), - 'site_url' => is_multisite()? network_home_url() : $this->bloginfo_rss( 'url' ), - 'blog_url' => $this->bloginfo_rss( 'url' ), + 'pubDate' => date( 'D, d M Y H:i:s +0000' ), + 'site_url' => is_multisite() ? network_home_url() : $this->bloginfo_rss( 'url' ), + 'blog_url' => $this->bloginfo_rss( 'url' ), ); return $metadata; } public function wp_generator_tag() { + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Calling native WordPress hook. return apply_filters( 'the_generator', get_the_generator( 'export' ), 'export' ); } public function authors() { global $wpdb; - $authors = array(); + $authors = array(); $author_ids = $wpdb->get_col( "SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_status != 'auto-draft'" ); foreach ( (array) $author_ids as $author_id ) { $authors[] = get_userdata( $author_id ); @@ -104,7 +108,7 @@ public function custom_taxonomies_terms() { return array(); } $custom_taxonomies = get_taxonomies( array( '_builtin' => false ) ); - $custom_terms = (array) get_terms( $custom_taxonomies, array( 'get' => 'all' ) ); + $custom_terms = (array) get_terms( $custom_taxonomies, array( 'get' => 'all' ) ); $this->check_for_orphaned_terms( $custom_terms ); $custom_terms = self::topologically_sort_terms( $custom_terms ); return $custom_terms; @@ -112,7 +116,7 @@ public function custom_taxonomies_terms() { public function nav_menu_terms() { $nav_menus = wp_get_nav_menus(); - foreach( $nav_menus as &$term ) { + foreach ( $nav_menus as &$term ) { $term->description = ''; } return $nav_menus; @@ -120,15 +124,19 @@ public function nav_menu_terms() { public function exportify_post( $post ) { $GLOBALS['wp_query']->in_the_loop = true; - $previous_global_post = \WP_CLI\Utils\get_flag_value( $GLOBALS, 'post' ); + $previous_global_post = Utils\get_flag_value( $GLOBALS, 'post' ); + // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Temporary override. $GLOBALS['post'] = $post; setup_postdata( $post ); + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Calling native WordPress hook. $post->post_content = apply_filters( 'the_content_export', $post->post_content ); + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Calling native WordPress hook. $post->post_excerpt = apply_filters( 'the_excerpt_export', $post->post_excerpt ); - $post->is_sticky = is_sticky( $post->ID ) ? 1 : 0; - $post->terms = self::get_terms_for_post( $post ); - $post->meta = self::get_meta_for_post( $post ); - $post->comments = $this->get_comments_for_post( $post ); + $post->is_sticky = is_sticky( $post->ID ) ? 1 : 0; + $post->terms = self::get_terms_for_post( $post ); + $post->meta = self::get_meta_for_post( $post ); + $post->comments = $this->get_comments_for_post( $post ); + // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Revert back to original. $GLOBALS['post'] = $previous_global_post; return $post; } @@ -142,7 +150,7 @@ private function calculate_post_ids() { global $wpdb; if ( is_array( $this->filters['post_ids'] ) ) { if ( $this->filters['with_attachments'] ) { - $attachment_post_ids = $this->include_attachment_ids( $this->filters['post_ids'] ); + $attachment_post_ids = $this->include_attachment_ids( $this->filters['post_ids'] ); $this->filters['post_ids'] = array_merge( $this->filters['post_ids'], $attachment_post_ids ); } return $this->filters['post_ids']; @@ -156,9 +164,12 @@ private function calculate_post_ids() { $this->category_where(); $where = implode( ' AND ', array_filter( $this->wheres ) ); - if ( $where ) $where = "WHERE $where"; + if ( $where ) { + $where = "WHERE $where"; + } $join = implode( ' ', array_filter( $this->joins ) ); + // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Individual where clauses run through $wpdb->prepare(). $post_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} AS p $join $where {$this->max_num_posts()}" ); if ( $this->filters['post_type'] ) { $post_ids = array_merge( $post_ids, $this->include_attachment_ids( $post_ids ) ); @@ -196,7 +207,7 @@ private function post_type_where() { return; } - if ( $this->filters['with_attachments'] == FALSE && ( !$this->filters['post_type'] || !in_array( 'attachment', $this->filters['post_type'], TRUE ) ) ) { + if ( false === $this->filters['with_attachments'] && ( ! $this->filters['post_type'] || ! in_array( 'attachment', $this->filters['post_type'], true ) ) ) { unset( $post_types['attachment'] ); } @@ -205,7 +216,7 @@ private function post_type_where() { private function status_where() { global $wpdb; - if ( !$this->filters['status'] ) { + if ( ! $this->filters['status'] ) { $this->wheres[] = "p.post_status != 'auto-draft'"; return; } @@ -215,17 +226,17 @@ private function status_where() { private function author_where() { global $wpdb; $user = $this->find_user_from_any_object( $this->filters['author'] ); - if ( !$user || is_wp_error( $user ) ) { + if ( ! $user || is_wp_error( $user ) ) { return; } - $this->author = $user; + $this->author = $user; $this->wheres[] = $wpdb->prepare( 'p.post_author = %d', $user->ID ); } private function start_date_where() { global $wpdb; $timestamp = strtotime( $this->filters['start_date'] ); - if ( !$timestamp ) { + if ( ! $timestamp ) { return; } $this->wheres[] = $wpdb->prepare( 'p.post_date >= %s', date( 'Y-m-d 00:00:00', $timestamp ) ); @@ -238,7 +249,7 @@ private function end_date_where() { } else { $timestamp = strtotime( $this->filters['end_date'] ); } - if ( !$timestamp ) { + if ( ! $timestamp ) { return; } $this->wheres[] = $wpdb->prepare( 'p.post_date <= %s', date( 'Y-m-d 23:59:59', $timestamp ) ); @@ -260,41 +271,43 @@ private function get_timestamp_for_the_last_day_of_a_month( $yyyy_mm ) { private function category_where() { global $wpdb; - if ( 'post' != $this->filters['post_type'] && ! in_array( 'post', (array) $this->filters['post_type'] ) ) { + if ( 'post' !== $this->filters['post_type'] && ! in_array( 'post', (array) $this->filters['post_type'], true ) ) { return; } $category = $this->find_category_from_any_object( $this->filters['category'] ); - if ( !$category ) { + if ( ! $category ) { return; } $this->category = $category; - $this->joins[] = "INNER JOIN {$wpdb->term_relationships} AS tr ON (p.ID = tr.object_id)"; + $this->joins[] = "INNER JOIN {$wpdb->term_relationships} AS tr ON (p.ID = tr.object_id)"; $this->wheres[] = $wpdb->prepare( 'tr.term_taxonomy_id = %d', $category->term_taxonomy_id ); } private function max_num_posts() { if ( $this->filters['max_num_posts'] > 0 ) { return "LIMIT {$this->filters['max_num_posts']}"; - } - else { - return ""; + } else { + return ''; } } private function include_attachment_ids( $post_ids ) { global $wpdb; - if ( !$post_ids ) { + if ( ! $post_ids ) { return array(); } - $attachment_ids = array(); + $attachment_ids = []; + // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition -- Assigment is part of the break condition. while ( $batch_of_post_ids = array_splice( $post_ids, 0, self::QUERY_CHUNK ) ) { $post_parent_condition = _wp_export_build_IN_condition( 'post_parent', $batch_of_post_ids ); - $attachment_ids = array_merge( $attachment_ids, (array)$wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'attachment' AND $post_parent_condition" ) ); + // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Escaped in wpcli_export_build_in_condition() function. + $attachment_ids = array_merge( $attachment_ids, (array) $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'attachment' AND {$post_parent_condition}" ) ); } return array_map( 'intval', $attachment_ids ); } private function bloginfo_rss( $section ) { + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Calling native WordPress hook. return apply_filters( 'bloginfo_rss', get_bloginfo_rss( $section ), $section ); } @@ -314,7 +327,7 @@ private function find_category_from_any_object( $category ) { return get_term( $category, 'category' ); } elseif ( is_string( $category ) ) { $term = term_exists( $category, 'category' ); - return isset( $term['term_id'] )? get_term( $term['term_id'], 'category' ) : false; + return isset( $term['term_id'] ) ? get_term( $term['term_id'], 'category' ) : false; } elseif ( isset( $category->term_id ) ) { return get_term( $category->term_id, 'category' ); } @@ -322,51 +335,57 @@ private function find_category_from_any_object( $category ) { } private static function topologically_sort_terms( $terms ) { - $sorted = array(); + $sorted = []; + // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition -- assignment is used as break condition. while ( $term = array_shift( $terms ) ) { - if ( $term->parent == 0 || isset( $sorted[$term->parent] ) ) - $sorted[$term->term_id] = $term; - else + if ( 0 === (int) $term->parent || isset( $sorted[ $term->parent ] ) ) { + $sorted[ $term->term_id ] = $term; + } else { $terms[] = $term; + } } return $sorted; } private function check_for_orphaned_terms( $terms ) { - $term_ids = array(); + $term_ids = array(); $have_parent = array(); foreach ( $terms as $term ) { $term_ids[ $term->term_id ] = true; - if ( $term->parent != 0 ) + if ( 0 !== (int) $term->parent ) { $have_parent[] = $term; + } } foreach ( $have_parent as $has_parent ) { if ( ! isset( $term_ids[ $has_parent->parent ] ) ) { $this->missing_parents = $has_parent; - throw new WP_Export_Term_Exception( sprintf( __( 'Term is missing a parent: %s (%d)' ), $has_parent->slug, $has_parent->term_taxonomy_id ) ); + throw new WP_Export_Term_Exception( "Term is missing a parent: {$has_parent->slug} ({$has_parent->term_taxonomy_id})" ); } } } private static function get_terms_for_post( $post ) { $taxonomies = get_object_taxonomies( $post->post_type ); - if ( empty( $taxonomies ) ) + if ( empty( $taxonomies ) ) { return array(); + } $terms = wp_get_object_terms( $post->ID, $taxonomies ); - $terms = $terms? $terms : array(); + $terms = $terms ? $terms : array(); return $terms; } private static function get_meta_for_post( $post ) { global $wpdb; $meta_for_export = array(); - $meta_from_db = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE post_id = %d", $post->ID ) ); + $meta_from_db = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE post_id = %d", $post->ID ) ); foreach ( $meta_from_db as $meta ) { - if ( apply_filters( 'wxr_export_skip_postmeta', false, $meta->meta_key, $meta ) ) + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Calling native WordPress hook. + if ( apply_filters( 'wxr_export_skip_postmeta', false, $meta->meta_key, $meta ) ) { continue; - if ( in_array( $meta->meta_key, array( '_edit_lock', '_wp_attachment_metadata', '_wp_attached_file' ) ) ) { + } + if ( in_array( $meta->meta_key, [ '_edit_lock', '_wp_attachment_metadata', '_wp_attached_file' ], true ) ) { continue; } $meta_for_export[] = $meta; @@ -382,9 +401,9 @@ private function get_comments_for_post( $post ) { } $comments = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved <> 'spam'", $post->ID ) ); - foreach( $comments as $comment ) { - $meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->commentmeta WHERE comment_id = %d", $comment->comment_ID ) ); - $meta = $meta? $meta : array(); + foreach ( $comments as $comment ) { + $meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->commentmeta WHERE comment_id = %d", $comment->comment_ID ) ); + $meta = $meta ? $meta : array(); $comment->meta = $meta; } return $comments; diff --git a/src/WP_Export_Returner.php b/src/WP_Export_Returner.php index 24baadfab..6327e1357 100644 --- a/src/WP_Export_Returner.php +++ b/src/WP_Export_Returner.php @@ -5,14 +5,17 @@ class WP_Export_Returner extends WP_Export_Base_Writer { public function export() { $this->private = ''; - try { + try { parent::export(); } catch ( WP_Export_Exception $e ) { + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Possibly used by third party extension. $message = apply_filters( 'export_error_message', $e->getMessage() ); return new WP_Error( 'wp-export-error', $message ); - + } catch ( WP_Export_Term_Exception $e ) { + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Possibly used by third party extensions. do_action( 'export_term_orphaned', $this->formatter->export->missing_parents ); + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Possibly used by third party extension. $message = apply_filters( 'export_term_error_message', $e->getMessage() ); return new WP_Error( 'wp-export-error', $message ); } diff --git a/src/WP_Export_Split_Files_Writer.php b/src/WP_Export_Split_Files_Writer.php index cea93b5dd..7d0e3d475 100644 --- a/src/WP_Export_Split_Files_Writer.php +++ b/src/WP_Export_Split_Files_Writer.php @@ -7,36 +7,36 @@ class WP_Export_Split_Files_Writer extends WP_Export_Base_Writer { private $before_posts_xml; private $after_posts_xml; - private $result = ''; private $f; - private $next_file_number = 0; + private $next_file_number = 0; private $current_file_size = 0; - function __construct( $formatter, $writer_args = array() ) { + public function __construct( $formatter, $writer_args = [] ) { parent::__construct( $formatter ); if ( ! defined( 'MB_IN_BYTES' ) ) { + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- WordPress native constants. define( 'MB_IN_BYTES', 1024 * 1024 ); } //TODO: check if args are not missing if ( is_null( $writer_args['max_file_size'] ) ) { $this->max_file_size = 15 * MB_IN_BYTES; - } elseif ( WP_CLI_EXPORT_COMMAND_NO_SPLIT === $writer_args['max_file_size'] ) { + } elseif ( WP_CLI_EXPORT_COMMAND_NO_SPLIT === (string) $writer_args['max_file_size'] ) { $this->max_file_size = WP_CLI_EXPORT_COMMAND_NO_SPLIT; } else { $this->max_file_size = $writer_args['max_file_size'] * MB_IN_BYTES; } $this->destination_directory = $writer_args['destination_directory']; - $this->filename_template = $writer_args['filename_template']; - $this->before_posts_xml = $this->formatter->before_posts(); - $this->after_posts_xml = $this->formatter->after_posts(); + $this->filename_template = $writer_args['filename_template']; + $this->before_posts_xml = $this->formatter->before_posts(); + $this->after_posts_xml = $this->formatter->after_posts(); } public function export() { $this->start_new_file(); - foreach( $this->formatter->posts() as $post_xml ) { + foreach ( $this->formatter->posts() as $post_xml ) { if ( WP_CLI_EXPORT_COMMAND_NO_SPLIT !== $this->max_file_size && $this->current_file_size + strlen( $post_xml ) > $this->max_file_size ) { $this->start_new_file(); } @@ -46,7 +46,7 @@ public function export() { } protected function write( $xml ) { - $res = fwrite( $this->f, $xml); + $res = fwrite( $this->f, $xml ); if ( false === $res ) { throw new WP_Export_Exception( __( 'WP Export: error writing to export file.' ) ); } @@ -58,17 +58,18 @@ private function start_new_file() { $this->close_current_file(); } $file_path = $this->next_file_path(); - $this->f = fopen( $file_path, 'w' ); - if ( !$this->f ) { - throw new WP_Export_Exception( sprintf( __( 'WP Export: error opening %s for writing.' ), $file_path ) ); + $this->f = fopen( $file_path, 'w' ); + if ( ! $this->f ) { + throw new WP_Export_Exception( "WP Export: error opening {$file_path} for writing." ); } + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Possibly used by third party extension. do_action( 'wp_export_new_file', $file_path ); $this->current_file_size = 0; $this->write( $this->before_posts_xml ); } private function close_current_file() { - if ( !$this->f ) { + if ( ! $this->f ) { return; } $this->write( $this->after_posts_xml ); diff --git a/src/WP_Export_Stdout_Writer.php b/src/WP_Export_Stdout_Writer.php index 6bb81cf7e..096a3aff2 100644 --- a/src/WP_Export_Stdout_Writer.php +++ b/src/WP_Export_Stdout_Writer.php @@ -2,15 +2,15 @@ class WP_Export_Stdout_Writer extends WP_Export_Base_Writer { - function __construct( $formatter, $writer_args ) { + public function __construct( $formatter, $writer_args ) { parent::__construct( $formatter ); $this->before_posts_xml = $this->formatter->before_posts(); - $this->after_posts_xml = $this->formatter->after_posts(); + $this->after_posts_xml = $this->formatter->after_posts(); } public function export() { fwrite( STDOUT, $this->before_posts_xml ); - foreach( $this->formatter->posts() as $post_xml ) { + foreach ( $this->formatter->posts() as $post_xml ) { fwrite( STDOUT, $post_xml ); } fwrite( STDOUT, $this->after_posts_xml ); diff --git a/src/WP_Export_WXR_Formatter.php b/src/WP_Export_WXR_Formatter.php index ca874c6ff..46b057188 100644 --- a/src/WP_Export_WXR_Formatter.php +++ b/src/WP_Export_WXR_Formatter.php @@ -6,19 +6,19 @@ * * @since 2.5.0 */ -define( 'WXR_VERSION', '1.2' ); +define( 'WXR_VERSION', '1.2' ); //phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- WordPress constant. /** * Responsible for formatting the data in WP_Export_Query to WXR */ class WP_Export_WXR_Formatter { public function __construct( $export ) { - $this->export = $export; + $this->export = $export; $this->wxr_version = WXR_VERSION; } public function before_posts() { - $before_posts_xml = ''; + $before_posts_xml = ''; $before_posts_xml .= $this->header(); $before_posts_xml .= $this->site_metadata(); $before_posts_xml .= $this->authors(); @@ -39,10 +39,10 @@ public function after_posts() { } public function header() { - $oxymel = new Oxymel; - $charset = $this->export->charset(); + $oxymel = new Oxymel(); + $charset = $this->export->charset(); $wp_generator_tag = $this->export->wp_generator_tag(); - $comment = <<xml ->comment( $comment ) ->raw( $wp_generator_tag ) - ->open_rss( array( - 'version' => '2.0', - 'xmlns:excerpt' => "http://wordpress.org/export/{$this->wxr_version}/excerpt/", - 'xmlns:content' => "http://purl.org/rss/1.0/modules/content/", - 'xmlns:wfw' => "http://wellformedweb.org/CommentAPI/", - 'xmlns:dc' => "http://purl.org/dc/elements/1.1/", - 'xmlns:wp' => "http://wordpress.org/export/{$this->wxr_version}/", - ) ) + ->open_rss( + array( + 'version' => '2.0', + 'xmlns:excerpt' => "http://wordpress.org/export/{$this->wxr_version}/excerpt/", + 'xmlns:content' => 'http://purl.org/rss/1.0/modules/content/', + 'xmlns:wfw' => 'http://wellformedweb.org/CommentAPI/', + 'xmlns:dc' => 'http://purl.org/dc/elements/1.1/', + 'xmlns:wp' => "http://wordpress.org/export/{$this->wxr_version}/", + ) + ) ->open_channel ->to_string(); } public function site_metadata() { - $oxymel = new Oxymel; + $oxymel = new Oxymel(); $metadata = $this->export->site_metadata(); return $oxymel ->title( $metadata['name'] ) @@ -95,7 +97,7 @@ public function site_metadata() { } public function authors() { - $oxymel = new Oxymel; + $oxymel = new Oxymel(); $authors = $this->export->authors(); foreach ( $authors as $author ) { $oxymel @@ -112,10 +114,10 @@ public function authors() { } public function categories() { - $oxymel = new WP_Export_Oxymel; + $oxymel = new WP_Export_Oxymel(); $categories = $this->export->categories(); - foreach( $categories as $term_id => $category ) { - $category->parent_slug = $category->parent? $categories[$category->parent]->slug : ''; + foreach ( $categories as $term_id => $category ) { + $category->parent_slug = $category->parent ? $categories[ $category->parent ]->slug : ''; $oxymel->tag( 'wp:category' )->contains ->tag( 'wp:term_id', $category->term_id ) ->tag( 'wp:category_nicename', $category->slug ) @@ -128,9 +130,9 @@ public function categories() { } public function tags() { - $oxymel = new WP_Export_Oxymel; - $tags = $this->export->tags(); - foreach( $tags as $tag ) { + $oxymel = new WP_Export_Oxymel(); + $tags = $this->export->tags(); + foreach ( $tags as $tag ) { $oxymel->tag( 'wp:tag' )->contains ->tag( 'wp:term_id', $tag->term_id ) ->tag( 'wp:tag_slug', $tag->slug ) @@ -151,20 +153,22 @@ public function custom_taxonomies_terms() { public function rss2_head_action() { ob_start(); + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WordPress native hook. do_action( 'rss2_head' ); $action_output = ob_get_clean(); return $action_output; } public function post( $post ) { - $oxymel = new WP_Export_Oxymel; + $oxymel = new WP_Export_Oxymel(); $GLOBALS['wp_query']->in_the_loop = true; + // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Intentional. $GLOBALS['post'] = $post; setup_postdata( $post ); $oxymel->item->contains - ->title( apply_filters( 'the_title_rss', $post->post_title ) ) - ->link( esc_url( apply_filters('the_permalink_rss', get_permalink() ) ) ) + ->title( apply_filters( 'the_title_rss', $post->post_title ) ) // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WordPress native hook. + ->link( esc_url( apply_filters( 'the_permalink_rss', get_permalink() ) ) ) // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WordPress native hook. ->pubDate( mysql2date( 'D, d M Y H:i:s +0000', get_post_time( 'Y-m-d H:i:s', true ), false ) ) ->tag( 'dc:creator', get_the_author_meta( 'login' ) ) ->guid( esc_url( get_the_guid() ), array( 'isPermaLink' => 'false' ) ) @@ -184,18 +188,23 @@ public function post( $post ) { ->tag( 'wp:post_password', $post->post_password ) ->tag( 'wp:is_sticky', $post->is_sticky ) ->optional( 'wp:attachment_url', wp_get_attachment_url( $post->ID ) ); - foreach( $post->terms as $term ) { + foreach ( $post->terms as $term ) { $oxymel - ->category( array( 'domain' => $term->taxonomy, 'nicename' => $term->slug ) )->contains->cdata( $term->name )->end; + ->category( + array( + 'domain' => $term->taxonomy, + 'nicename' => $term->slug, + ) + )->contains->cdata( $term->name )->end; } - foreach( $post->meta as $meta ) { + foreach ( $post->meta as $meta ) { $oxymel ->tag( 'wp:postmeta' )->contains ->tag( 'wp:meta_key', $meta->meta_key ) ->tag( 'wp:meta_value' )->contains->cdata( $meta->meta_value )->end ->end; } - foreach( $post->comments as $comment ) { + foreach ( $post->comments as $comment ) { $oxymel ->tag( 'wp:comment' )->contains ->tag( 'wp:comment_id', $comment->comment_ID ) @@ -219,19 +228,19 @@ public function post( $post ) { } public function footer() { - $oxymel = new Oxymel; + $oxymel = new Oxymel(); return $oxymel->close_channel->close_rss->to_string(); } protected function terms( $terms ) { - $oxymel = new WP_Export_Oxymel; - foreach( $terms as $term ) { - $term->parent_slug = $term->parent? $terms[$term->parent]->slug : ''; + $oxymel = new WP_Export_Oxymel(); + foreach ( $terms as $term ) { + $term->parent_slug = $term->parent ? $terms[ $term->parent ]->slug : ''; $oxymel->tag( 'wp:term' )->contains ->tag( 'wp:term_id', $term->term_id ) ->tag( 'wp:term_taxonomy', $term->taxonomy ) ->tag( 'wp:term_slug', $term->slug ); - if ( 'nav_menu' != $term->taxonomy ) { + if ( 'nav_menu' !== $term->taxonomy ) { $oxymel ->tag( 'wp:term_parent', $term->parent_slug ); } @@ -246,11 +255,11 @@ protected function terms( $terms ) { protected function comment_meta( $comment ) { global $wpdb; $metas = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->commentmeta WHERE comment_id = %d", $comment->comment_ID ) ); - if ( !$metas ) { - return new Oxymel; + if ( ! $metas ) { + return new Oxymel(); } - $oxymel = new WP_Export_Oxymel; - foreach( $metas as $meta ) { + $oxymel = new WP_Export_Oxymel(); + foreach ( $metas as $meta ) { $oxymel->tag( 'wp:commentmeta' )->contains ->tag( 'wp:meta_key', $meta->meta_key ) ->tag( 'wp:meta_value' )->contains->cdata( $meta->meta_value )->end diff --git a/src/WP_Export_XML_Over_HTTP.php b/src/WP_Export_XML_Over_HTTP.php index 1a7ee37a5..dfb148500 100644 --- a/src/WP_Export_XML_Over_HTTP.php +++ b/src/WP_Export_XML_Over_HTTP.php @@ -4,7 +4,7 @@ class WP_Export_XML_Over_HTTP extends WP_Export_Base_Writer { private $file_name; - function __construct( $formatter, $file_name ) { + public function __construct( $formatter, $file_name ) { parent::__construct( $formatter ); $this->file_name = $file_name; } @@ -15,11 +15,13 @@ public function export() { $this->send_headers(); echo $export; } catch ( WP_Export_Exception $e ) { + // phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Possibly used by third party extension. $message = apply_filters( 'export_error_message', $e->getMessage() ); wp_die( $message, __( 'Export Error' ), array( 'back_link' => true ) ); } catch ( WP_Export_Term_Exception $e ) { do_action( 'export_term_orphaned', $this->formatter->export->missing_parents ); $message = apply_filters( 'export_term_error_message', $e->getMessage() ); + // phpcs:enable wp_die( $message, __( 'Export Error' ), array( 'back_link' => true ) ); } } diff --git a/src/WP_Map_Iterator.php b/src/WP_Map_Iterator.php index 74daf2986..a128419fa 100644 --- a/src/WP_Map_Iterator.php +++ b/src/WP_Map_Iterator.php @@ -1,12 +1,12 @@ callback = $callback; parent::__construct( $iterator ); } - function current() { + public function current() { $original_current = parent::current(); return call_user_func( $this->callback, $original_current ); } diff --git a/src/WP_Post_IDs_Iterator.php b/src/WP_Post_IDs_Iterator.php index 49ad84d38..a2001c1f6 100644 --- a/src/WP_Post_IDs_Iterator.php +++ b/src/WP_Post_IDs_Iterator.php @@ -7,16 +7,16 @@ class WP_Post_IDs_Iterator implements Iterator { private $results = array(); public function __construct( $post_ids, $limit = null ) { - $this->db = $GLOBALS['wpdb']; + $this->db = $GLOBALS['wpdb']; $this->post_ids = $post_ids; $this->ids_left = $post_ids; - if ( !is_null( $limit ) ) { + if ( ! is_null( $limit ) ) { $this->limit = $limit; } } public function current() { - return $this->results[$this->index_in_results]; + return $this->results[ $this->index_in_results ]; } public function key() { @@ -29,21 +29,21 @@ public function next() { } public function rewind() { - $this->results = array(); - $this->global_index = 0; + $this->results = array(); + $this->global_index = 0; $this->index_in_results = 0; - $this->ids_left = $this->post_ids; + $this->ids_left = $this->post_ids; } public function valid() { - if ( isset( $this->results[$this->index_in_results] ) ) { + if ( isset( $this->results[ $this->index_in_results ] ) ) { return true; } if ( empty( $this->ids_left ) ) { return false; } $has_posts = $this->load_next_posts_from_db(); - if ( !$has_posts ) { + if ( ! $has_posts ) { return false; } $this->index_in_results = 0; @@ -52,9 +52,9 @@ public function valid() { private function load_next_posts_from_db() { $next_batch_post_ids = array_splice( $this->ids_left, 0, $this->limit ); - $in_post_ids_sql = _wp_export_build_IN_condition( 'ID', $next_batch_post_ids ); - $this->results = $this->db->get_results( "SELECT * FROM {$this->db->posts} WHERE $in_post_ids_sql" ); - if ( !$this->results ) { + $in_post_ids_sql = _wp_export_build_IN_condition( 'ID', $next_batch_post_ids ); + $this->results = $this->db->get_results( "SELECT * FROM {$this->db->posts} WHERE $in_post_ids_sql" ); + if ( ! $this->results ) { if ( $this->db->last_error ) { throw new WP_Iterator_Exception( 'Database error: ' . $this->db->last_error ); } else {