| @@ -32,21 +32,8 @@ | ||
| 32 | 32 | }, |
| 33 | 33 | ], |
| 34 | 34 | ] |
| 35 | 35 | ); |
| 36 | - | |
| 37 | - $this->route( | |
| 38 | - '/posts/(?P<id>\d+)/export', | |
| 39 | - [ | |
| 40 | - [ | |
| 41 | - 'methods' => WP_REST_Server::DELETABLE, | |
| 42 | - 'callback' => [ $this, 'delete_export' ], | |
| 43 | - 'permission_callback' => function () { | |
| 44 | - return current_user_can( 'export' ); | |
| 45 | - }, | |
| 46 | - ], | |
| 47 | - ] | |
| 48 | - ); | |
| 49 | 36 | } |
| 50 | 37 | |
| 51 | 38 | /** |
| 52 | 39 | * Export a single post. |
| @@ -51,21 +38,19 @@ | ||
| 51 | 38 | /** |
| 52 | 39 | * Export a single post. |
| 53 | 40 | */ |
| 54 | 41 | public function export_post( $request ) { |
| 42 | + $this->delete_exports(); | |
| 43 | + | |
| 55 | 44 | /* Get requested post data */ |
| 56 | 45 | $post_id = absint( $request->get_param( 'id' ) ); |
| 57 | 46 | $post = get_post( $post_id ); |
| 58 | 47 | |
| 59 | 48 | /* Create temporary file */ |
| 60 | - $file_url = WP_CONTENT_URL . '/' . sanitize_file_name( $post->post_title ) . '_' . $post->ID . '.xml'; | |
| 61 | - $file = WP_CONTENT_DIR . '/' . sanitize_file_name( $post->post_title ) . '_' . $post->ID . '.xml'; | |
| 62 | - $current = ''; | |
| 49 | + $upload_dir = wp_upload_dir( null, false ); | |
| 50 | + $file_url = $upload_dir['baseurl'] . '/assistant_export_' . $post->ID . '.xml'; | |
| 51 | + $file = $upload_dir['basedir'] . '/assistant_export_' . $post->ID . '.xml'; | |
| 63 | 52 | |
| 64 | - if ( file_exists( $file ) ) { | |
| 65 | - $current = file_get_contents( $file ); | |
| 66 | - } | |
| 67 | - | |
| 68 | 53 | /* Creates taxonomies string for xml export */ |
| 69 | 54 | $taxonomies = get_taxonomies( '', 'names' ); |
| 70 | 55 | $terms = wp_get_object_terms( $post->ID, $taxonomies ); |
| 71 | 56 | |
| @@ -146,37 +131,22 @@ | ||
| 146 | 131 | 'post_type' => $post_type, |
| 147 | 132 | ] |
| 148 | 133 | ); |
| 149 | 134 | |
| 150 | - file_put_contents( $file, $current . $export_data ); | |
| 135 | + file_put_contents( $file, $export_data ); | |
| 151 | 136 | |
| 152 | 137 | return $file_url; |
| 153 | 138 | } |
| 154 | 139 | |
| 155 | 140 | /** |
| 156 | - * Remove temporary exported file. | |
| 141 | + * Remove temporary export files. | |
| 157 | 142 | */ |
| 158 | - public function delete_export( $request ) { | |
| 143 | + public function delete_exports() { | |
| 144 | + $upload_dir = wp_upload_dir( null, false ); | |
| 145 | + $files = glob( trailingslashit( $upload_dir['basedir'] ) . 'assistant_export_*.xml' ); | |
| 159 | 146 | |
| 160 | - $post_id = absint( $request->get_param( 'id' ) ); | |
| 161 | - $post = get_post( $post_id ); | |
| 162 | - | |
| 163 | - /* Remove temporary file */ | |
| 164 | - $file = WP_CONTENT_DIR . '/' . sanitize_file_name( $post->post_title ) . '_' . $post->ID . '.xml'; | |
| 165 | - | |
| 166 | - if ( file_exists( $file ) ) { | |
| 147 | + foreach ( $files as $file ) { | |
| 167 | 148 | unlink( $file ); |
| 168 | - return rest_ensure_response( | |
| 169 | - [ | |
| 170 | - 'success' => true, | |
| 171 | - ] | |
| 172 | - ); | |
| 173 | - } else { | |
| 174 | - return rest_ensure_response( | |
| 175 | - [ | |
| 176 | - 'error' => true, | |
| 177 | - ] | |
| 178 | - ); | |
| 179 | 149 | } |
| 180 | 150 | } |
| 181 | 151 | |
| 182 | 152 | |