← All changes
|
backend/src/Controllers/PostsExportController.php
+151
-191
0.7.0
→
1.5.1
View file →
| @@ -1,191 +1,151 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace FL\Assistant\Controllers; | |
| 4 | - | |
| 5 | -use FL\Assistant\System\Contracts\ControllerAbstract; | |
| 6 | -use FL\Assistant\System\View; | |
| 7 | -use WP_REST_Server; | |
| 8 | - | |
| 9 | -/** | |
| 10 | - * REST API logic for exporting posts. | |
| 11 | - */ | |
| 12 | -class PostsExportController extends ControllerAbstract { | |
| 13 | - | |
| 14 | - protected $view; | |
| 15 | - | |
| 16 | - public function __construct( View $view ) { | |
| 17 | - $this->view = $view; | |
| 18 | - } | |
| 19 | - | |
| 20 | - /** | |
| 21 | - * Register routes. | |
| 22 | - */ | |
| 23 | - public function register_routes() { | |
| 24 | - $this->route( | |
| 25 | - '/posts/(?P<id>\d+)/export', | |
| 26 | - [ | |
| 27 | - [ | |
| 28 | - 'methods' => WP_REST_Server::CREATABLE, | |
| 29 | - 'callback' => [ $this, 'export_post' ], | |
| 30 | - 'permission_callback' => function () { | |
| 31 | - return current_user_can( 'export' ); | |
| 32 | - }, | |
| 33 | - ], | |
| 34 | - ] | |
| 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 | - } | |
| 50 | - | |
| 51 | - /** | |
| 52 | - * Export a single post. | |
| 53 | - */ | |
| 54 | - public function export_post( $request ) { | |
| 55 | - /* Get requested post data */ | |
| 56 | - $post_id = absint( $request->get_param( 'id' ) ); | |
| 57 | - $post = get_post( $post_id ); | |
| 58 | - | |
| 59 | - /* 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 = ''; | |
| 63 | - | |
| 64 | - if ( file_exists( $file ) ) { | |
| 65 | - $current = file_get_contents( $file ); | |
| 66 | - } | |
| 67 | - | |
| 68 | - /* Creates taxonomies string for xml export */ | |
| 69 | - $taxonomies = get_taxonomies( '', 'names' ); | |
| 70 | - $terms = wp_get_object_terms( $post->ID, $taxonomies ); | |
| 71 | - | |
| 72 | - $taxonomies_str = ''; | |
| 73 | - foreach ( $terms as $term ) { | |
| 74 | - $taxonomies_str .= '<category domain="' . $term->taxonomy . '" nicename="' . $term->slug . '">' . $this->wxr_cdata( $term->name ) . '</category>'; | |
| 75 | - } | |
| 76 | - | |
| 77 | - /* Creates comments string */ | |
| 78 | - $comments = get_comments( [ 'post_id' => $post->ID ] ); | |
| 79 | - | |
| 80 | - $comment_str = ''; | |
| 81 | - | |
| 82 | - foreach ( $comments as $comment ) { | |
| 83 | - | |
| 84 | - $comment_str .= '<wp:comment> | |
| 85 | - <wp:comment_id>' . $comment->comment_ID . '</wp:comment_id> | |
| 86 | - <wp:comment_author>' . $this->wxr_cdata( $comment->comment_author ) . '</wp:comment_author> | |
| 87 | - <wp:comment_author_email>' . $this->wxr_cdata( $comment->comment_author_email ) . '</wp:comment_author_email> | |
| 88 | - <wp:comment_author_url></wp:comment_author_url> | |
| 89 | - <wp:comment_author_IP>' . $this->wxr_cdata( $comment->comment_author_ip ) . '</wp:comment_author_IP> | |
| 90 | - <wp:comment_date>' . $this->wxr_cdata( $comment->comment_date ) . '</wp:comment_date> | |
| 91 | - <wp:comment_date_gmt>' . $this->wxr_cdata( $comment->comment_date_gmt ) . '</wp:comment_date_gmt> | |
| 92 | - <wp:comment_content>' . $this->wxr_cdata( $comment->comment_content ) . '</wp:comment_content> | |
| 93 | - <wp:comment_approved>' . $this->wxr_cdata( $comment->comment_approved ) . '</wp:comment_approved> | |
| 94 | - <wp:comment_type><![CDATA[]]></wp:comment_type> | |
| 95 | - <wp:comment_parent>' . $comment->comment_parent . '</wp:comment_parent> | |
| 96 | - <wp:comment_user_id>' . $comment->user_id . '</wp:comment_user_id> | |
| 97 | - </wp:comment> | |
| 98 | - '; | |
| 99 | - } | |
| 100 | - | |
| 101 | - /* Creates post meta string */ | |
| 102 | - $meta_str = ''; | |
| 103 | - $meta_values = get_post_meta( $post->ID ); | |
| 104 | - foreach ( $meta_values as $key => $values ) { | |
| 105 | - foreach ( $values as $value ) { | |
| 106 | - $meta_str .= '<wp:postmeta> | |
| 107 | - <wp:meta_key>' . $this->wxr_cdata( $key ) . '</wp:meta_key> | |
| 108 | - <wp:meta_value>' . $this->wxr_cdata( $value ) . '</wp:meta_value> | |
| 109 | - </wp:postmeta> | |
| 110 | - '; | |
| 111 | - } | |
| 112 | - } | |
| 113 | - | |
| 114 | - /* Author data */ | |
| 115 | - | |
| 116 | - $author_display_name = $this->wxr_cdata( get_the_author_meta( 'display_name', $post->post_author ) ); | |
| 117 | - | |
| 118 | - /* Post content */ | |
| 119 | - | |
| 120 | - $post_content = $this->wxr_cdata( $post->post_author ); | |
| 121 | - $post_excerpt = $this->wxr_cdata( $post->post_excerpt ); | |
| 122 | - $post_date = $this->wxr_cdata( $post->post_date ); | |
| 123 | - $post_date_gmt = $this->wxr_cdata( $post->post_date_gmt ); | |
| 124 | - $comment_status = $this->wxr_cdata( $post->comment_status ); | |
| 125 | - $ping_status = $this->wxr_cdata( $post->ping_status ); | |
| 126 | - $post_title = $this->wxr_cdata( $post->post_title ); | |
| 127 | - $post_status = $this->wxr_cdata( $post->post_status ); | |
| 128 | - $post_type = $this->wxr_cdata( $post->post_type ); | |
| 129 | - | |
| 130 | - $export_data = $this->view->render_to_string( | |
| 131 | - 'post-export', | |
| 132 | - [ | |
| 133 | - 'post' => $post, | |
| 134 | - 'taxonomy_data' => $taxonomies_str, | |
| 135 | - 'comment_data' => $comment_str, | |
| 136 | - 'meta_data' => $meta_str, | |
| 137 | - 'author_name' => $author_display_name, | |
| 138 | - 'post_content' => $post_content, | |
| 139 | - 'post_excerpt' => $post_excerpt, | |
| 140 | - 'post_date' => $post_date, | |
| 141 | - 'post_date_gmt' => $post_date_gmt, | |
| 142 | - 'comment_status' => $comment_status, | |
| 143 | - 'ping_status' => $ping_status, | |
| 144 | - 'post_title' => $post_title, | |
| 145 | - 'post_status' => $post_status, | |
| 146 | - 'post_type' => $post_type, | |
| 147 | - ] | |
| 148 | - ); | |
| 149 | - | |
| 150 | - file_put_contents( $file, $current . $export_data ); | |
| 151 | - | |
| 152 | - return $file_url; | |
| 153 | - } | |
| 154 | - | |
| 155 | - /** | |
| 156 | - * Remove temporary exported file. | |
| 157 | - */ | |
| 158 | - public function delete_export( $request ) { | |
| 159 | - | |
| 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 ) ) { | |
| 167 | - 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 | - } | |
| 180 | - } | |
| 181 | - | |
| 182 | - | |
| 183 | - function wxr_cdata( $str ) { | |
| 184 | - if ( ! seems_utf8( $str ) ) { | |
| 185 | - $str = utf8_encode( $str ); | |
| 186 | - } | |
| 187 | - $str = '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>'; | |
| 188 | - | |
| 189 | - return $str; | |
| 190 | - } | |
| 191 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace FL\Assistant\Controllers; | |
| 4 | + | |
| 5 | +use FL\Assistant\System\Contracts\ControllerAbstract; | |
| 6 | +use FL\Assistant\System\View; | |
| 7 | +use WP_REST_Server; | |
| 8 | + | |
| 9 | +/** | |
| 10 | + * REST API logic for exporting posts. | |
| 11 | + */ | |
| 12 | +class PostsExportController extends ControllerAbstract { | |
| 13 | + | |
| 14 | + protected $view; | |
| 15 | + | |
| 16 | + public function __construct( View $view ) { | |
| 17 | + $this->view = $view; | |
| 18 | + } | |
| 19 | + | |
| 20 | + /** | |
| 21 | + * Register routes. | |
| 22 | + */ | |
| 23 | + public function register_routes() { | |
| 24 | + $this->route( | |
| 25 | + '/posts/(?P<id>\d+)/export', | |
| 26 | + [ | |
| 27 | + [ | |
| 28 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 29 | + 'callback' => [ $this, 'export_post' ], | |
| 30 | + 'permission_callback' => function () { | |
| 31 | + return current_user_can( 'export' ); | |
| 32 | + }, | |
| 33 | + ], | |
| 34 | + ] | |
| 35 | + ); | |
| 36 | + } | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * Export a single post. | |
| 40 | + */ | |
| 41 | + public function export_post( $request ) { | |
| 42 | + | |
| 43 | + /* Get requested post data */ | |
| 44 | + $post_id = absint( $request->get_param( 'id' ) ); | |
| 45 | + $post = get_post( $post_id ); | |
| 46 | + | |
| 47 | + /* Create temporary file */ | |
| 48 | + $upload_dir = wp_upload_dir( null, false ); | |
| 49 | + $file_url = $upload_dir['baseurl'] . '/assistant_export_' . $post->ID . '.xml'; | |
| 50 | + $file = $upload_dir['basedir'] . '/assistant_export_' . $post->ID . '.xml'; | |
| 51 | + | |
| 52 | + /* Creates taxonomies string for xml export */ | |
| 53 | + $taxonomies = get_taxonomies( '', 'names' ); | |
| 54 | + $terms = wp_get_object_terms( $post->ID, $taxonomies ); | |
| 55 | + | |
| 56 | + $taxonomies_str = ''; | |
| 57 | + foreach ( $terms as $term ) { | |
| 58 | + $taxonomies_str .= '<category domain="' . $term->taxonomy . '" nicename="' . $term->slug . '">' . $this->wxr_cdata( $term->name ) . '</category>'; | |
| 59 | + } | |
| 60 | + | |
| 61 | + /* Creates comments string */ | |
| 62 | + $comments = get_comments( [ 'post_id' => $post->ID ] ); | |
| 63 | + | |
| 64 | + $comment_str = ''; | |
| 65 | + | |
| 66 | + foreach ( $comments as $comment ) { | |
| 67 | + | |
| 68 | + $comment_str .= '<wp:comment> | |
| 69 | + <wp:comment_id>' . $comment->comment_ID . '</wp:comment_id> | |
| 70 | + <wp:comment_author>' . $this->wxr_cdata( $comment->comment_author ) . '</wp:comment_author> | |
| 71 | + <wp:comment_author_email>' . $this->wxr_cdata( $comment->comment_author_email ) . '</wp:comment_author_email> | |
| 72 | + <wp:comment_author_url></wp:comment_author_url> | |
| 73 | + <wp:comment_author_IP>' . $this->wxr_cdata( $comment->comment_author_ip ) . '</wp:comment_author_IP> | |
| 74 | + <wp:comment_date>' . $this->wxr_cdata( $comment->comment_date ) . '</wp:comment_date> | |
| 75 | + <wp:comment_date_gmt>' . $this->wxr_cdata( $comment->comment_date_gmt ) . '</wp:comment_date_gmt> | |
| 76 | + <wp:comment_content>' . $this->wxr_cdata( $comment->comment_content ) . '</wp:comment_content> | |
| 77 | + <wp:comment_approved>' . $this->wxr_cdata( $comment->comment_approved ) . '</wp:comment_approved> | |
| 78 | + <wp:comment_type><![CDATA[]]></wp:comment_type> | |
| 79 | + <wp:comment_parent>' . $comment->comment_parent . '</wp:comment_parent> | |
| 80 | + <wp:comment_user_id>' . $comment->user_id . '</wp:comment_user_id> | |
| 81 | + </wp:comment> | |
| 82 | + '; | |
| 83 | + } | |
| 84 | + | |
| 85 | + /* Creates post meta string */ | |
| 86 | + $meta_str = ''; | |
| 87 | + $meta_values = get_post_meta( $post->ID ); | |
| 88 | + foreach ( $meta_values as $key => $values ) { | |
| 89 | + foreach ( $values as $value ) { | |
| 90 | + /* Ignore BB history meta */ | |
| 91 | + if ( false !== strpos( $key, '_fl_builder_history' ) ) { | |
| 92 | + continue; | |
| 93 | + } | |
| 94 | + $meta_str .= '<wp:postmeta> | |
| 95 | + <wp:meta_key>' . $this->wxr_cdata( $key ) . '</wp:meta_key> | |
| 96 | + <wp:meta_value>' . $this->wxr_cdata( $value ) . '</wp:meta_value> | |
| 97 | + </wp:postmeta> | |
| 98 | + '; | |
| 99 | + } | |
| 100 | + } | |
| 101 | + | |
| 102 | + /* Author data */ | |
| 103 | + | |
| 104 | + $author_display_name = $this->wxr_cdata( get_the_author_meta( 'display_name', $post->post_author ) ); | |
| 105 | + | |
| 106 | + /* Post content */ | |
| 107 | + | |
| 108 | + $post_content = $this->wxr_cdata( $post->post_author ); | |
| 109 | + $post_excerpt = $this->wxr_cdata( $post->post_excerpt ); | |
| 110 | + $post_date = $this->wxr_cdata( $post->post_date ); | |
| 111 | + $post_date_gmt = $this->wxr_cdata( $post->post_date_gmt ); | |
| 112 | + $comment_status = $this->wxr_cdata( $post->comment_status ); | |
| 113 | + $ping_status = $this->wxr_cdata( $post->ping_status ); | |
| 114 | + $post_title = $this->wxr_cdata( $post->post_title ); | |
| 115 | + $post_status = $this->wxr_cdata( $post->post_status ); | |
| 116 | + $post_type = $this->wxr_cdata( $post->post_type ); | |
| 117 | + | |
| 118 | + $export_data = $this->view->render_to_string( | |
| 119 | + 'post-export', | |
| 120 | + [ | |
| 121 | + 'post' => $post, | |
| 122 | + 'taxonomy_data' => $taxonomies_str, | |
| 123 | + 'comment_data' => $comment_str, | |
| 124 | + 'meta_data' => $meta_str, | |
| 125 | + 'author_name' => $author_display_name, | |
| 126 | + 'post_content' => $post_content, | |
| 127 | + 'post_excerpt' => $post_excerpt, | |
| 128 | + 'post_date' => $post_date, | |
| 129 | + 'post_date_gmt' => $post_date_gmt, | |
| 130 | + 'comment_status' => $comment_status, | |
| 131 | + 'ping_status' => $ping_status, | |
| 132 | + 'post_title' => $post_title, | |
| 133 | + 'post_status' => $post_status, | |
| 134 | + 'post_type' => $post_type, | |
| 135 | + ] | |
| 136 | + ); | |
| 137 | + | |
| 138 | + file_put_contents( $file, $export_data ); | |
| 139 | + update_option( 'fl_assistant_export_file', true ); | |
| 140 | + return $file_url; | |
| 141 | + } | |
| 142 | + | |
| 143 | + function wxr_cdata( $str ) { | |
| 144 | + if ( ! seems_utf8( $str ) ) { | |
| 145 | + $str = utf8_encode( $str ); | |
| 146 | + } | |
| 147 | + $str = '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>'; | |
| 148 | + | |
| 149 | + return $str; | |
| 150 | + } | |
| 151 | +} | |