Skip to content
Merged
12 changes: 9 additions & 3 deletions functions.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
<?php

// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- Renaming breaks Phar compat.
function wp_export( $args = array() ) {
$defaults = array(
/**
* @param array{filters?: array<mixed>, format?: class-string<WP_Export_WXR_Formatter>, writer?: class-string<WP_Export_Base_Writer>, writer_args?: mixed} $args
*/
function wp_export( $args = array() ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- Renaming breaks Phar compat.
$defaults = array(
'filters' => array(),
'format' => 'WP_Export_WXR_Formatter',
'writer' => 'WP_Export_Returner',
'writer_args' => null,
);

/**
* @var array{filters: array<mixed>, format: class-string<WP_Export_WXR_Formatter>, writer: class-string<WP_Export_Base_Writer>, writer_args: mixed} $args
*/
$args = wp_parse_args( $args, $defaults );
$export_query = new WP_Export_Query( $args['filters'] );
$formatter = new $args['format']( $export_query );
Expand Down
16 changes: 16 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
parameters:
level: 9
paths:
- src
- export-command.php
- functions.php
scanDirectories:
- vendor/wp-cli/wp-cli/php
scanFiles:
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
treatPhpDocTypesAsCertain: false
ignoreErrors:
- identifier: missingType.iterableValue
- identifier: missingType.property
- identifier: missingType.parameter
- identifier: missingType.return
96 changes: 85 additions & 11 deletions src/Export_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public function __invoke( $_, $assoc_args ) {
'wp_export_new_file',
static function ( $file_path ) {
WP_CLI::log( sprintf( 'Writing to file %s', $file_path ) );
Utils\wp_clear_object_cache();
Utils\wp_clear_object_cache(); // phpcs:ignore PHPCompatibility.FunctionUse.RemovedFunctions.wp_clear_object_cacheDeprecatedRemoved @phpstan-ignore-line
}
);

Expand All @@ -190,15 +190,15 @@ static function ( $file_path ) {
wp_export(
[
'filters' => $this->export_args,
'writer' => 'WP_Export_File_Writer',
'writer' => WP_Export_File_Writer::class,
'writer_args' => 'php://output',
]
);
} else {
wp_export(
[
'filters' => $this->export_args,
'writer' => 'WP_Export_Split_Files_Writer',
'writer' => WP_Export_Split_Files_Writer::class,
'writer_args' => [
'max_file_size' => $this->max_file_size,
'destination_directory' => $this->wxr_path,
Expand Down Expand Up @@ -234,6 +234,7 @@ private function validate_args( $args ) {

foreach ( $args as $key => $value ) {
if ( is_callable( [ $this, 'check_' . $key ] ) ) {
/** @phpstan-ignore argument.type */
$result = call_user_func( [ $this, 'check_' . $key ], $value );
if ( false === $result ) {
$has_errors = true;
Expand All @@ -251,9 +252,14 @@ private function validate_args( $args ) {
}
}

/**
* @param string $path
*
* @phpstan-ignore method.unused
*/
private function check_dir( $path ) {
if ( empty( $path ) ) {
$path = getcwd();
$path = (string) getcwd();
} elseif ( ! is_dir( $path ) ) {
WP_CLI::error( sprintf( "The directory '%s' does not exist.", $path ) );
} elseif ( ! is_writable( $path ) ) {
Expand All @@ -265,34 +271,49 @@ private function check_dir( $path ) {
return true;
}

/**
* @param string $date
*
* @phpstan-ignore method.unused
*/
private function check_start_date( $date ) {
if ( null === $date ) {
return true;
}

$time = strtotime( $date );
if ( ! empty( $date ) && ! $time ) {
if ( ! empty( $date ) && false === $time ) {
WP_CLI::warning( sprintf( 'The start_date %s is invalid.', $date ) );
return false;
}
$this->export_args['start_date'] = date( 'Y-m-d', $time ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
$this->export_args['start_date'] = date( 'Y-m-d', (int) $time ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
return true;
}

/**
* @param string $date
*
* @phpstan-ignore method.unused
*/
private function check_end_date( $date ) {
if ( null === $date ) {
return true;
}

$time = strtotime( $date );
if ( ! empty( $date ) && ! $time ) {
if ( ! empty( $date ) && false === $time ) {
WP_CLI::warning( sprintf( 'The end_date %s is invalid.', $date ) );
return false;
}
$this->export_args['end_date'] = date( 'Y-m-d', $time ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
$this->export_args['end_date'] = date( 'Y-m-d', (int) $time ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
return true;
}

/**
* @param string $post_type
*
* @phpstan-ignore method.unused
*/
private function check_post_type( $post_type ) {
if ( null === $post_type || 'any' === $post_type ) {
return true;
Expand All @@ -317,6 +338,11 @@ private function check_post_type( $post_type ) {
return true;
}

/**
* @param string $post_type
*
* @phpstan-ignore method.unused
*/
private function check_post_type__not_in( $post_type ) {
if ( null === $post_type ) {
return true;
Expand All @@ -341,6 +367,11 @@ private function check_post_type__not_in( $post_type ) {
return true;
}

/**
* @param string $post__in
*
* @phpstan-ignore method.unused
*/
private function check_post__in( $post__in ) {
if ( null === $post__in ) {
return true;
Expand All @@ -357,6 +388,11 @@ private function check_post__in( $post__in ) {
return true;
}

/**
* @param string $start_id
*
* @phpstan-ignore method.unused
*/
private function check_start_id( $start_id ) {
if ( null === $start_id ) {
return true;
Expand All @@ -374,14 +410,19 @@ private function check_start_id( $start_id ) {
return true;
}

/**
* @param string $author
*
* @phpstan-ignore method.unused
*/
private function check_author( $author ) {
if ( null === $author ) {
return true;
}

// 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 ) ) {
$authors = function_exists( 'get_users' ) ? get_users() : get_users_of_blog(); // @phpstan-ignore-line
if ( empty( $authors ) ) {
WP_CLI::warning( 'Could not find any authors in this blog.' );
return false;
}
Expand All @@ -407,6 +448,9 @@ private function check_author( $author ) {
return true;
}

/**
* @phpstan-ignore method.unused
*/
private function check_max_num_posts( $num ) {
if ( null !== $num && ( ! is_numeric( $num ) || $num <= 0 ) ) {
WP_CLI::warning( 'max_num_posts should be a positive integer.' );
Expand All @@ -418,6 +462,11 @@ private function check_max_num_posts( $num ) {
return true;
}

/**
* @param string $category
*
* @phpstan-ignore method.unused
*/
private function check_category( $category ) {
if ( null === $category ) {
return true;
Expand All @@ -428,14 +477,19 @@ private function check_category( $category ) {
}

$term = category_exists( $category );
if ( empty( $term ) || is_wp_error( $term ) ) {
if ( empty( $term ) ) {
WP_CLI::warning( sprintf( 'Could not find a category matching %s.', $category ) );
return false;
}
$this->export_args['category'] = $category;
return true;
}

/**
* @param string $status
*
* @phpstan-ignore method.unused
*/
private function check_post_status( $status ) {
if ( null === $status ) {
return true;
Expand All @@ -456,6 +510,11 @@ private function check_post_status( $status ) {
return true;
}

/**
* @param string|null $skip
*
* @phpstan-ignore method.unused
*/
private function check_skip_comments( $skip ) {
if ( null === $skip ) {
return true;
Expand All @@ -469,6 +528,11 @@ private function check_skip_comments( $skip ) {
return true;
}

/**
* @param string $size
*
* @phpstan-ignore method.unused
*/
private function check_max_file_size( $size ) {
if ( ! is_numeric( $size ) ) {
WP_CLI::warning( 'max_file_size should be numeric.' );
Expand All @@ -480,6 +544,11 @@ private function check_max_file_size( $size ) {
return true;
}

/**
* @param string $once
*
* @phpstan-ignore method.unused
*/
private function check_include_once( $once ) {
if ( null === $once ) {
return true;
Expand All @@ -498,6 +567,11 @@ private function check_include_once( $once ) {
return true;
}

/**
* @param string $allow_orphan_terms
*
* @phpstan-ignore method.unused
*/
private function check_allow_orphan_terms( $allow_orphan_terms ) {
if ( null === $allow_orphan_terms ) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/WP_Export_Oxymel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public function cdata( $text ) {
if ( ! wp_is_valid_utf8( $text ) ) {
$text = mb_convert_encoding( $text, 'UTF-8' );
}
// @phpstan-ignore function.deprecated
} elseif ( ! seems_utf8( $text ) ) { // phpcs:ignore WordPress.WP.DeprecatedFunctions.seems_utf8Found
$text = mb_convert_encoding( $text, 'UTF-8' );

}
}
return parent::cdata( $text );
Expand Down
18 changes: 13 additions & 5 deletions src/WP_Export_Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,13 @@ public function nav_menu_terms() {
}

public function exportify_post( $post ) {
$GLOBALS['wp_query']->in_the_loop = true;
$previous_global_post = Utils\get_flag_value( $GLOBALS, 'post' );
/**
* @var \WP_Query $wp_query
*/
global $wp_query;

$wp_query->in_the_loop = true;
$previous_global_post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Temporary override.
$GLOBALS['post'] = $post;
setup_postdata( $post );
Expand Down Expand Up @@ -208,6 +213,9 @@ private function post_type_where() {
if ( isset( $post_types_filters['name'] ) && is_array( $post_types_filters['name'] ) ) {
$post_types = [];
foreach ( $post_types_filters['name'] as $post_type ) {
/**
* @var string $post_type
*/
if ( post_type_exists( $post_type ) ) {
$post_types[] = $post_type;
}
Expand Down Expand Up @@ -327,7 +335,7 @@ private function bloginfo_rss( $section ) {

private function find_user_from_any_object( $user ) {
if ( is_numeric( $user ) ) {
return get_user_by( 'id', $user );
return get_user_by( 'id', (int) $user );
} elseif ( is_string( $user ) ) {
return get_user_by( 'login', $user );
} elseif ( isset( $user->ID ) ) {
Expand All @@ -338,10 +346,10 @@ private function find_user_from_any_object( $user ) {

private function find_category_from_any_object( $category ) {
if ( is_numeric( $category ) ) {
return get_term( $category, 'category' );
return get_term( (int) $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( (int) $term['term_id'], 'category' ) : false;
} elseif ( isset( $category->term_id ) ) {
return get_term( $category->term_id, 'category' );
}
Expand Down
1 change: 0 additions & 1 deletion src/WP_Export_Returner.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ class WP_Export_Returner extends WP_Export_Base_Writer {
private $result = '';

public function export() {
$this->private = '';
try {
parent::export();
} catch ( WP_Export_Exception $e ) {
Expand Down
Loading