Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 65 additions & 36 deletions class-export-one-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ function init() {
add_action( 'post_submitbox_misc_actions', array( $this, 'post_submitbox_misc_actions' ) );
add_filter( 'export_args', array( $this, 'export_args' ) );
add_filter( 'query', array( $this, 'query' ) );
add_filter( 'post_row_actions', array( $this, 'post_row_export_link'), 10, 2);
add_filter( 'page_row_actions', array( $this, 'post_row_export_link'), 10, 2);
}
}
/**
Expand All @@ -50,16 +52,43 @@ function post_submitbox_misc_actions() {
}
</style>
<div class="misc-pub-section export-one-post">
<?php
$export_url = add_query_arg( array(
'download' => '',
'export_single' => get_the_ID(),
), admin_url( 'export.php' ) );
?>
<a href="<?php echo esc_url( $export_url ); ?>"><?php esc_html_e( 'Export This', 'export-one-post' ); ?></a>
<a href="<?php echo esc_url( $this->get_export_url() ); ?>">
<?php esc_html_e( 'Export XML', 'export-one-post' ); ?>
</a>
</div><?php
}

/**
* Insert export link into post/page rows
*
*/
function post_row_export_link($actions, $post) {
if ( current_user_can( 'edit_posts' ) ) {
$actions['export'] = '<a href="' . $this->get_export_url($post->ID) . '">' . esc_html__( 'Export XML', 'export-one-post' ) . '</a>';
}

return $actions;
}

/**
* Generate export url
*
*/
function get_export_url( $post_id = null ) {

if ( $post_id === null ) {
$post_id = get_the_ID();
}

$export_url = add_query_arg( array(
'download' => '',
'export_single' => $post_id,
'_wpnonce' => wp_create_nonce( 'export_single' ),
), admin_url( 'export.php' ) );

return $export_url;
}

/**
* Modify export arguments
* except if normal export
Expand All @@ -69,14 +98,14 @@ function post_submitbox_misc_actions() {
*/
function export_args( $args ) {
// if no export_single var, it's a normal export - don't interfere
if ( ! isset( $_GET['export_single'] ) ) {
return $args;
}
if ( isset( $_GET['export_single'] ) ) {
check_admin_referer('export_single');

// use our fake date so the query is easy to find (because we don't have a good hook to use)
$args['content'] = 'post';
$args['start_date'] = $this->fake_date;
$args['end_date'] = $this->fake_date;
// use our fake date so the query is easy to find (because we don't have a good hook to use)
$args['content'] = 'post';
$args['start_date'] = $this->fake_date;
$args['end_date'] = $this->fake_date;
}

return $args;
}
Expand All @@ -89,32 +118,32 @@ function export_args( $args ) {
* @return string Modified SQL query
*/
function query( $query ) {
if ( ! isset( $_GET['export_single'] ) ) {
return $query;
}
if (isset($_GET['export_single'])) {
check_admin_referer('export_single');

global $wpdb;
global $wpdb;

// This is the query WP will build (given our arg filtering above)
// Since the current_filter isn't narrow, we'll check each query
// to see if it matches, then if it is we replace it
// @see https://github.com/wordpress/wordpress/blob/5.4.1/wp-admin/includes/export.php#L144
$test = $wpdb->prepare(
"SELECT ID FROM {$wpdb->posts} WHERE {$wpdb->posts}.post_type = 'post' AND {$wpdb->posts}.post_status != 'auto-draft' AND {$wpdb->posts}.post_date >= %s AND {$wpdb->posts}.post_date < %s",
date( 'Y-m-d', strtotime( $this->fake_date ) ),
date( 'Y-m-d', strtotime( '+1 month', strtotime( $this->fake_date ) ) )
);
// This is the query WP will build (given our arg filtering above)
// Since the current_filter isn't narrow, we'll check each query
// to see if it matches, then if it is we replace it
// @see https://github.com/wordpress/wordpress/blob/5.4.1/wp-admin/includes/export.php#L144
$test = $wpdb->prepare(
"SELECT ID FROM {$wpdb->posts} WHERE {$wpdb->posts}.post_type = 'post' AND {$wpdb->posts}.post_status != 'auto-draft' AND {$wpdb->posts}.post_date >= %s AND {$wpdb->posts}.post_date < %s",
date('Y-m-d', strtotime($this->fake_date)),
date('Y-m-d', strtotime('+1 month', strtotime($this->fake_date)))
);

if ( $test !== $query ) {
return $query;
}
if ($test !== $query) {
return $query;
}

// divide query
$split = explode( 'WHERE', $query );
// replace WHERE clause
$split[1] = $wpdb->prepare( " {$wpdb->posts}.ID = %d", intval( $_GET['export_single'] ) );
// put query back together
$query = implode( 'WHERE', $split );
// divide query
$split = explode('WHERE', $query);
// replace WHERE clause
$split[1] = $wpdb->prepare(" {$wpdb->posts}.ID = %d", intval($_GET['export_single']));
// put query back together
$query = implode('WHERE', $split);
}

return $query;
}
Expand Down
8 changes: 4 additions & 4 deletions export-one-post.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
/*
* Plugin Name: Export One Post
* Plugin URI: https://github.com/trepmal/export-one-post
* Description:
* Plugin URI: https://github.com/drawbackwards/export-one-post
* Description: Export single posts and pages from the edit screen.
* Version: 1.0
* Author: Kailey Lampert
* Author URI: kaileylampert.com
* Author: Drawbackwards
* Author URI: drawbackwards.com
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* TextDomain: export-one-post
Expand Down
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
Fork of: https://github.com/trepmal/export-one-post

# Export One Post

Works on pages and custom post types!

![screenshot](screenshot.png)


Confirmed working on WordPress 5.4.1, single and multisite. Gutenberg not (yet...?) supported.

## WP-CLI
Expand Down