PluginProbe
Assistant – Every Day Productivity Apps / 1.4.3
Assistant – Every Day Productivity Apps v1.4.3
1.5.5 trunk 0.1.0 0.2.0 0.2.1 0.2.2 0.3.0 0.3.1 0.4 0.4.1 0.4.2 0.5.0 0.5.1 0.6.0 0.7.0 0.7.0.2 0.7.0.3 0.7.0.4 0.7.0.5 0.7.0.6 0.7.0.7 0.7.0.8 0.7.0.9 0.7.1 1.0 All 71 releases
assistant / backend / src / Controllers / PostsExportController.php

PostsExportController.php in Assistant – Every Day Productivity Apps 1.4.3, at backend/src/Controllers/PostsExportController.php

166 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $this->delete_exports();
43
44 /* Get requested post data */
45 $post_id = absint( $request->get_param( 'id' ) );
46 $post = get_post( $post_id );
47
48 /* Create temporary file */
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';
52
53 /* Creates taxonomies string for xml export */
54 $taxonomies = get_taxonomies( '', 'names' );
55 $terms = wp_get_object_terms( $post->ID, $taxonomies );
56
57 $taxonomies_str = '';
58 foreach ( $terms as $term ) {
59 $taxonomies_str .= '<category domain="' . $term->taxonomy . '" nicename="' . $term->slug . '">' . $this->wxr_cdata( $term->name ) . '</category>';
60 }
61
62 /* Creates comments string */
63 $comments = get_comments( [ 'post_id' => $post->ID ] );
64
65 $comment_str = '';
66
67 foreach ( $comments as $comment ) {
68
69 $comment_str .= '<wp:comment>
70 <wp:comment_id>' . $comment->comment_ID . '</wp:comment_id>
71 <wp:comment_author>' . $this->wxr_cdata( $comment->comment_author ) . '</wp:comment_author>
72 <wp:comment_author_email>' . $this->wxr_cdata( $comment->comment_author_email ) . '</wp:comment_author_email>
73 <wp:comment_author_url></wp:comment_author_url>
74 <wp:comment_author_IP>' . $this->wxr_cdata( $comment->comment_author_ip ) . '</wp:comment_author_IP>
75 <wp:comment_date>' . $this->wxr_cdata( $comment->comment_date ) . '</wp:comment_date>
76 <wp:comment_date_gmt>' . $this->wxr_cdata( $comment->comment_date_gmt ) . '</wp:comment_date_gmt>
77 <wp:comment_content>' . $this->wxr_cdata( $comment->comment_content ) . '</wp:comment_content>
78 <wp:comment_approved>' . $this->wxr_cdata( $comment->comment_approved ) . '</wp:comment_approved>
79 <wp:comment_type><![CDATA[]]></wp:comment_type>
80 <wp:comment_parent>' . $comment->comment_parent . '</wp:comment_parent>
81 <wp:comment_user_id>' . $comment->user_id . '</wp:comment_user_id>
82 </wp:comment>
83 ';
84 }
85
86 /* Creates post meta string */
87 $meta_str = '';
88 $meta_values = get_post_meta( $post->ID );
89 foreach ( $meta_values as $key => $values ) {
90 foreach ( $values as $value ) {
91 /* Ignore BB history meta */
92 if ( false !== strpos( $key, '_fl_builder_history' ) ) {
93 continue;
94 }
95 $meta_str .= '<wp:postmeta>
96 <wp:meta_key>' . $this->wxr_cdata( $key ) . '</wp:meta_key>
97 <wp:meta_value>' . $this->wxr_cdata( $value ) . '</wp:meta_value>
98 </wp:postmeta>
99 ';
100 }
101 }
102
103 /* Author data */
104
105 $author_display_name = $this->wxr_cdata( get_the_author_meta( 'display_name', $post->post_author ) );
106
107 /* Post content */
108
109 $post_content = $this->wxr_cdata( $post->post_author );
110 $post_excerpt = $this->wxr_cdata( $post->post_excerpt );
111 $post_date = $this->wxr_cdata( $post->post_date );
112 $post_date_gmt = $this->wxr_cdata( $post->post_date_gmt );
113 $comment_status = $this->wxr_cdata( $post->comment_status );
114 $ping_status = $this->wxr_cdata( $post->ping_status );
115 $post_title = $this->wxr_cdata( $post->post_title );
116 $post_status = $this->wxr_cdata( $post->post_status );
117 $post_type = $this->wxr_cdata( $post->post_type );
118
119 $export_data = $this->view->render_to_string(
120 'post-export',
121 [
122 'post' => $post,
123 'taxonomy_data' => $taxonomies_str,
124 'comment_data' => $comment_str,
125 'meta_data' => $meta_str,
126 'author_name' => $author_display_name,
127 'post_content' => $post_content,
128 'post_excerpt' => $post_excerpt,
129 'post_date' => $post_date,
130 'post_date_gmt' => $post_date_gmt,
131 'comment_status' => $comment_status,
132 'ping_status' => $ping_status,
133 'post_title' => $post_title,
134 'post_status' => $post_status,
135 'post_type' => $post_type,
136 ]
137 );
138
139 file_put_contents( $file, $export_data );
140
141 return $file_url;
142 }
143
144 /**
145 * Remove temporary export files.
146 */
147 public function delete_exports() {
148 $upload_dir = wp_upload_dir( null, false );
149 $files = glob( trailingslashit( $upload_dir['basedir'] ) . 'assistant_export_*.xml' );
150
151 foreach ( $files as $file ) {
152 unlink( $file );
153 }
154 }
155
156
157 function wxr_cdata( $str ) {
158 if ( ! seems_utf8( $str ) ) {
159 $str = utf8_encode( $str );
160 }
161 $str = '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>';
162
163 return $str;
164 }
165 }
166