-
Notifications
You must be signed in to change notification settings - Fork 5
/
class-export-one-post.php
175 lines (153 loc) · 4.66 KB
/
class-export-one-post.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/**
* Export One Post
*
* Class for namespacing purposes
*
*/
class Export_One_Post {
/**
* Get hooked in
* Define var
*
*/
function __construct() {
// due to a lack of hooks, we're using what we hope is an unlikely date match
$this->fake_date = '1970-01-05'; // Y-m-d
add_action( 'init', array( $this, 'init' ) );
}
/**
* Get hooked in: Part II
*
*/
function init() {
if ( current_user_can( 'export' ) ) {
// block-editor support for button
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) );
// classic editor support
add_action( 'post_submitbox_misc_actions', array( $this, 'post_submitbox_misc_actions' ) );
add_filter( 'post_row_actions', array( $this, 'row_action' ), 10, 2 );
add_filter( 'page_row_actions', array( $this, 'row_action' ), 10, 2 );
add_filter( 'export_args', array( $this, 'export_args' ) );
add_filter( 'query', array( $this, 'query' ) );
}
}
/**
* Enqueue script necessary for Block Editor (aka Gutenberg) support
*/
function enqueue_block_editor_assets() {
wp_enqueue_script(
'exportone',
plugins_url( 'assets/sidebar-button.build.js', __FILE__ ),
array( 'wp-plugins', 'wp-edit-post', 'wp-element' )
);
wp_localize_script( 'exportone', 'exportOne', array(
'export_url' => $this->get_export_url(),
) );
}
/**
* Insert our action link into the submit box
*
*/
function post_submitbox_misc_actions() {
?>
<style>
.export-one-post:before {
content: "\f316";
color: #82878c;
font: normal 20px/1 dashicons;
speak: none;
display: inline-block;
padding: 0 3px 0 0;
vertical-align: top;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
</style>
<div class="misc-pub-section export-one-post">
<a href="<?php echo esc_url( $this->get_export_url() ); ?>"><?php esc_html_e( 'Export This', 'export-one-post' ); ?></a>
</div><?php
}
/**
* Page/post row action
*
* @param string[] $actions An array of row action links. Defaults are
* 'Edit', 'Quick Edit', 'Restore', 'Trash',
* 'Delete Permanently', 'Preview', and 'View'.
* @param WP_Post $post The post object.
* @return array $actions
*/
function row_action( $actions, $post ) {
$actions['export'] = sprintf( '<a href="%s">Export</a>', $this->get_export_url( $post->ID ) );
return $actions;
}
/**
* Modify export arguments
* except if normal export
*
* @param array $args Query args for determining what should be exported
* @return $args Modified query
*/
function export_args( $args ) {
// if no export_single var, it's a normal export - don't interfere
if ( ! isset( $_GET['export_single'] ) ) {
return $args;
}
// 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;
}
/**
* Filter query
* Look for 'tagged' query, replace with one matching the needs
*
* @param string $query SQL query
* @return string Modified SQL query
*/
function query( $query ) {
if ( ! isset( $_GET['export_single'] ) ) {
return $query;
}
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 ) ) )
);
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 );
return $query;
}
/**
* Build the export url
*
* @param int $post_id
* @return string|bool URL or false if no valid post ID
*/
private function get_export_url( $post_id = null ) {
if ( is_null( $post_id ) ) {
$post_id = get_the_id();
}
// still null?
if ( is_null( $post_id ) ) {
return false;
}
return add_query_arg( array(
'download' => '',
'export_single' => $post_id,
), admin_url( 'export.php' ) );
}
}