From 8d2f60c4ef7399b0ed9b79835f1d558d12472f3c Mon Sep 17 00:00:00 2001 From: Debarghya Banerjee Date: Mon, 7 Oct 2024 20:33:01 +0530 Subject: [PATCH 1/3] Enhance export_wp function documentation and error handling to return errors for invalid or non-exportable custom post types. --- src/wp-admin/includes/export.php | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/wp-admin/includes/export.php b/src/wp-admin/includes/export.php index 277231ad52ea6..7cdd5178a8d3a 100644 --- a/src/wp-admin/includes/export.php +++ b/src/wp-admin/includes/export.php @@ -21,9 +21,14 @@ * Default behavior is to export all content, however, note that post content will only * be exported for post types with the `can_export` argument enabled. Any posts with the * 'auto-draft' status will be skipped. + * + * If an invalid custom post type is supplied, an error will be returned instead of exporting any content. + * If a valid custom post type is supplied but its `can_export` property is false, no posts from that type will be exported. * * @since 2.1.0 * @since 5.7.0 Added the `post_modified` and `post_modified_gmt` fields to the export file. + * @since 6.8.0 Updated error handling to return `WP_Error` for invalid or non-exportable post types. + * * * @global wpdb $wpdb WordPress database abstraction object. * @global WP_Post $post Global post object. @@ -33,10 +38,10 @@ * * @type string $content Type of content to export. If set, only the post content of this post type * will be exported. Accepts 'all', 'post', 'page', 'attachment', or a defined - * custom post. If an invalid custom post type is supplied, every post type for - * which `can_export` is enabled will be exported instead. If a valid custom post - * type is supplied but `can_export` is disabled, then 'posts' will be exported - * instead. When 'all' is supplied, only post types with `can_export` enabled will + * custom post. If an invalid custom post type is supplied, an error will be returned + * If a valid custom post type is supplied but `can_export` is disabled, no posts + * from that type will be exported and an error will be returned instead. When 'all' + * is supplied, only post types with `can_export` enabled will * be exported. Default 'all'. * @type string $author Author to export content for. Only used when `$content` is 'post', 'page', or * 'attachment'. Accepts false (all) or a specific author ID. Default false (all). @@ -99,12 +104,24 @@ function export_wp( $args = array() ) { if ( 'all' !== $args['content'] && post_type_exists( $args['content'] ) ) { $ptype = get_post_type_object( $args['content'] ); + + // Check if the post type is valid and can be exported + if ( ! $ptype ) { + // Invalid post type supplied + return new WP_Error( 'invalid_post_type', __( 'Invalid post type supplied.' ) ); + } + if ( ! $ptype->can_export ) { - $args['content'] = 'post'; + return new WP_Error( 'cannot_export', __( 'This post type cannot be exported.' ) ); } $where = $wpdb->prepare( "{$wpdb->posts}.post_type = %s", $args['content'] ); } else { + // If 'all' or invalid type, stop execution + if ( ! $args['content'] || ! in_array( $args['content'], get_post_types(), true ) ) { + return new WP_Error( 'invalid_post_type', __( 'Invalid or no post type specified.' ) ); + } + $post_types = get_post_types( array( 'can_export' => true ) ); $esses = array_fill( 0, count( $post_types ), '%s' ); From bd41878d46eb8de076945ec8d6692a287916a799 Mon Sep 17 00:00:00 2001 From: Debarghya Banerjee Date: Mon, 7 Oct 2024 20:52:14 +0530 Subject: [PATCH 2/3] Fix PHPCS errors --- src/wp-admin/includes/export.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wp-admin/includes/export.php b/src/wp-admin/includes/export.php index 7cdd5178a8d3a..f9df0f2bbde1b 100644 --- a/src/wp-admin/includes/export.php +++ b/src/wp-admin/includes/export.php @@ -21,7 +21,7 @@ * Default behavior is to export all content, however, note that post content will only * be exported for post types with the `can_export` argument enabled. Any posts with the * 'auto-draft' status will be skipped. - * + * * If an invalid custom post type is supplied, an error will be returned instead of exporting any content. * If a valid custom post type is supplied but its `can_export` property is false, no posts from that type will be exported. * @@ -39,8 +39,8 @@ * @type string $content Type of content to export. If set, only the post content of this post type * will be exported. Accepts 'all', 'post', 'page', 'attachment', or a defined * custom post. If an invalid custom post type is supplied, an error will be returned - * If a valid custom post type is supplied but `can_export` is disabled, no posts - * from that type will be exported and an error will be returned instead. When 'all' + * If a valid custom post type is supplied but `can_export` is disabled, no posts + * from that type will be exported and an error will be returned instead. When 'all' * is supplied, only post types with `can_export` enabled will * be exported. Default 'all'. * @type string $author Author to export content for. Only used when `$content` is 'post', 'page', or From 73b7764623279ff8216285506c0f15e54420b831 Mon Sep 17 00:00:00 2001 From: Debarghya Banerjee Date: Mon, 7 Oct 2024 23:46:56 +0530 Subject: [PATCH 3/3] Fix the logic and allow 'all' --- src/wp-admin/includes/export.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/wp-admin/includes/export.php b/src/wp-admin/includes/export.php index f9df0f2bbde1b..d422fd7f9b226 100644 --- a/src/wp-admin/includes/export.php +++ b/src/wp-admin/includes/export.php @@ -105,9 +105,9 @@ function export_wp( $args = array() ) { if ( 'all' !== $args['content'] && post_type_exists( $args['content'] ) ) { $ptype = get_post_type_object( $args['content'] ); - // Check if the post type is valid and can be exported + // Check if the post type is valid and can be exported. if ( ! $ptype ) { - // Invalid post type supplied + // Invalid post type supplied. return new WP_Error( 'invalid_post_type', __( 'Invalid post type supplied.' ) ); } @@ -116,11 +116,10 @@ function export_wp( $args = array() ) { } $where = $wpdb->prepare( "{$wpdb->posts}.post_type = %s", $args['content'] ); + } elseif ( 'all' !== $args['content'] && ( ! $args['content'] || ! post_type_exists( $args['content'] ) ) ) { + // If invalid post_type, stop execution. + return new WP_Error( 'invalid_post_type', __( 'Invalid or no post type specified.' ) ); } else { - // If 'all' or invalid type, stop execution - if ( ! $args['content'] || ! in_array( $args['content'], get_post_types(), true ) ) { - return new WP_Error( 'invalid_post_type', __( 'Invalid or no post type specified.' ) ); - } $post_types = get_post_types( array( 'can_export' => true ) ); $esses = array_fill( 0, count( $post_types ), '%s' );