PluginProbe
Assistant – Every Day Productivity Apps / 1.4.7
Assistant – Every Day Productivity Apps v1.4.7
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
← All changes | backend/src/Data/Transformers/PostTransformer.php +164 -180 1.5.51.4.7 View file →
@@ -1,180 +1,164 @@
1 -<?php
2 -
3 -namespace FL\Assistant\Data\Transformers;
4 -
5 -use FL\Assistant\Data\Repository\NotationsRepository;
6 -use FL\Assistant\Data\Repository\TermsRepository;
7 -use FL\Assistant\System\Integrations\BeaverBuilder;
8 -use FL\Assistant\Helpers\PreviewHelper;
9 -
10 -/**
11 - * Class PostTransformer
12 - *
13 - * Convert a WP_Post object to array suitable for REST output
14 - *
15 - * @package FL\Assistant\RestApi\Transformers
16 - */
17 -class PostTransformer {
18 -
19 - protected $notations;
20 - protected $terms;
21 - protected $beaver_builder;
22 -
23 - /**
24 - * PostTransformer constructor.
25 - *
26 - * @param NotationsRepository $notations
27 - * @param BeaverBuilder $beaver_builder
28 - */
29 - public function __construct(
30 - NotationsRepository $notations,
31 - TermsRepository $terms,
32 - BeaverBuilder $beaver_builder
33 - ) {
34 - $this->notations = $notations;
35 - $this->terms = $terms;
36 - $this->beaver_builder = $beaver_builder;
37 - }
38 -
39 - /**
40 - * Allow this class to be used as a callback for pagination
41 - * @param \WP_Post $post
42 - *
43 - * @return array
44 - */
45 - public function __invoke( \WP_Post $post ) {
46 - return $this->transform( $post );
47 - }
48 -
49 - /**
50 - * @param \WP_Post $post
51 - *
52 - * @return string
53 - */
54 - public function get_edit_post_link( \WP_Post $post ) {
55 -
56 - if ( strstr( $post->post_type, 'wp_template' ) ) {
57 - $id = get_stylesheet() . '//' . $post->post_name;
58 - return admin_url( "/site-editor.php?postType={$post->post_type}&postId={$id}" );
59 - }
60 -
61 - return get_edit_post_link( $post->ID, '' );
62 - }
63 -
64 - /**
65 - * @param \WP_Post $post
66 - *
67 - * @return array
68 - */
69 - public function transform( \WP_Post $post ) {
70 - $author = get_the_author_meta( 'display_name', $post->post_author );
71 - $date = get_the_date( '', $post );
72 - $template = get_post_meta( $post->ID, '_wp_page_template', true );
73 -
74 - $thumb_id = get_post_thumbnail_id( $post );
75 - $thumb_data = wp_prepare_attachment_for_js( $thumb_id );
76 -
77 - $registered_post_types = get_post_types();
78 -
79 - $all_users = get_users( [ 'who' => 'authors' ] );
80 - $users = [];
81 -
82 - foreach ( $all_users as $user ) {
83 - $users[ $user->ID ] = $user->display_name;
84 - }
85 -
86 - if ( ! function_exists( 'wp_check_post_lock' ) ) {
87 - require_once ABSPATH . 'wp-admin/includes/post.php';
88 - }
89 -
90 - $response = [
91 - 'author' => $author,
92 - 'post_author' => $post->post_author,
93 - 'commentsAllowed' => 'open' === $post->comment_status ? true : false,
94 - 'excerpt' => $post->post_excerpt,
95 - 'date' => $date,
96 - 'editUrl' => $this->get_edit_post_link( $post ),
97 - 'previewUrl' => PreviewHelper::get_post_preview_url( $post ),
98 - 'id' => $post->ID,
99 - 'labels' => [],
100 - 'order' => $post->menu_order,
101 - 'parent' => $post->post_parent,
102 - 'password' => $post->post_password,
103 - 'pingbacksAllowed' => 'open' === $post->ping_status ? true : false,
104 - 'slug' => $post->post_name,
105 - 'status' => $post->post_status,
106 - 'template' => empty( $template ) ? 'default' : $template,
107 - 'terms' => [],
108 - 'thumbnail' => get_the_post_thumbnail_url( $post, 'thumbnail' ),
109 - 'thumbnailData' => $thumb_data,
110 - 'title' => empty( $post->post_title ) ? __( '(no title)', 'assistant' ) : $post->post_title,
111 - 'trashedStatus' => get_post_meta( $post->ID, '_wp_trash_meta_status', true ),
112 - 'type' => $post->post_type,
113 - 'url' => get_permalink( $post ),
114 - 'visibility' => 'public',
115 - 'commentsCount' => get_comments_number( $post->ID ),
116 - 'isSticky' => is_sticky( $post->ID ),
117 - 'authorList' => $users,
118 - 'hasLock' => wp_check_post_lock( $post->ID ),
119 - 'postTypeRegistered' => in_array( $post->post_type, $registered_post_types ),
120 - ];
121 -
122 - // Code App
123 - if ( 'fl_code' === $post->post_type ) {
124 - $code = get_post_meta( $post->ID, '_fl_asst_code', true );
125 - $code_type = get_post_meta( $post->ID, '_fl_asst_code_type', true );
126 - $locations = get_post_meta( $post->ID, '_fl_asst_code_locations', true );
127 - $enabled = get_post_meta( $post->ID, '_fl_asst_enable', true );
128 - $response['code'] = $code;
129 - $response['description'] = $post->post_content;
130 - $response['enable'] = $enabled;
131 - $response['subtype'] = $code_type;
132 - $response['locations'] = $locations;
133 - }
134 -
135 - // Post visibility.
136 - if ( 'private' === $post->post_status ) {
137 - $response['visibility'] = 'private';
138 - } elseif ( ! empty( $post->post_password ) ) {
139 - $response['visibility'] = 'protected';
140 - }
141 -
142 - // Beaver Builder data.
143 - if ( $this->beaver_builder->is_installed() ) {
144 - $response['bbCanEdit'] = $this->beaver_builder->can_edit_post( $post->ID );
145 - $response['bbIsEnabled'] = \FLBuilderModel::is_builder_enabled( $post->ID );
146 - $response['bbBranding'] = \FLBuilderModel::get_branding();
147 - $response['bbEditUrl'] = \FLBuilderModel::get_edit_url( $post->ID );
148 - }
149 -
150 - // Favorites
151 - $favorites = $this->notations->get_favorites( 'post', $post->ID, get_current_user_id() );
152 - $response['isFavorite'] = ! ! count( $favorites );
153 -
154 - // Labels
155 - $labels = $this->notations->get_labels( 'post', $post->ID );
156 - foreach ( $labels as $label ) {
157 - $response['labels'][] = $label['label_id'];
158 - }
159 -
160 - // Terms
161 - $taxonomies = get_object_taxonomies( $post->post_type, 'objects' );
162 - foreach ( $taxonomies as $tax_slug => $tax ) {
163 - if ( ! $tax->public || ! $tax->show_ui ) {
164 - continue;
165 - }
166 - $response['terms'][ $tax_slug ] = $this->terms->find_where(
167 - [
168 - 'taxonomy' => $tax_slug,
169 - 'object_ids' => [ $post->ID ],
170 - 'orderby' => 'name',
171 - 'order' => 'ASC',
172 - 'fields' => 'ids',
173 - ]
174 - );
175 - }
176 -
177 - return $response;
178 - }
179 -
180 -}
1 +<?php
2 +
3 +namespace FL\Assistant\Data\Transformers;
4 +
5 +use FL\Assistant\Data\Repository\NotationsRepository;
6 +use FL\Assistant\Data\Repository\TermsRepository;
7 +use FL\Assistant\System\Integrations\BeaverBuilder;
8 +use FL\Assistant\Helpers\PreviewHelper;
9 +
10 +/**
11 + * Class PostTransformer
12 + *
13 + * Convert a WP_Post object to array suitable for REST output
14 + *
15 + * @package FL\Assistant\RestApi\Transformers
16 + */
17 +class PostTransformer {
18 +
19 + protected $notations;
20 + protected $terms;
21 + protected $beaver_builder;
22 +
23 + /**
24 + * PostTransformer constructor.
25 + *
26 + * @param NotationsRepository $notations
27 + * @param BeaverBuilder $beaver_builder
28 + */
29 + public function __construct(
30 + NotationsRepository $notations,
31 + TermsRepository $terms,
32 + BeaverBuilder $beaver_builder
33 + ) {
34 + $this->notations = $notations;
35 + $this->terms = $terms;
36 + $this->beaver_builder = $beaver_builder;
37 + }
38 +
39 + /**
40 + * Allow this class to be used as a callback for pagination
41 + * @param \WP_Post $post
42 + *
43 + * @return array
44 + */
45 + public function __invoke( \WP_Post $post ) {
46 + return $this->transform( $post );
47 + }
48 +
49 + /**
50 + * @param \WP_Post $post
51 + *
52 + * @return string
53 + */
54 + public function get_edit_post_link( \WP_Post $post ) {
55 +
56 + if ( strstr( $post->post_type, 'wp_template' ) ) {
57 + $id = get_stylesheet() . '//' . $post->post_name;
58 + return admin_url( "/site-editor.php?postType={$post->post_type}&postId={$id}" );
59 + }
60 +
61 + return get_edit_post_link( $post->ID, '' );
62 + }
63 +
64 + /**
65 + * @param \WP_Post $post
66 + *
67 + * @return array
68 + */
69 + public function transform( \WP_Post $post ) {
70 + $author = get_the_author_meta( 'display_name', $post->post_author );
71 + $date = get_the_date( '', $post );
72 + $template = get_post_meta( $post->ID, '_wp_page_template', true );
73 +
74 + $thumb_id = get_post_thumbnail_id( $post );
75 + $thumb_data = wp_prepare_attachment_for_js( $thumb_id );
76 +
77 + $all_users = get_users( [ 'who' => 'authors' ] );
78 + $users = [];
79 +
80 + foreach ( $all_users as $user ) {
81 + $users[ $user->ID ] = $user->display_name;
82 + }
83 +
84 + if ( ! function_exists( 'wp_check_post_lock' ) ) {
85 + require_once ABSPATH . 'wp-admin/includes/post.php';
86 + }
87 +
88 + $response = [
89 + 'author' => $author,
90 + 'post_author' => $post->post_author,
91 + 'commentsAllowed' => 'open' === $post->comment_status ? true : false,
92 + 'excerpt' => $post->post_excerpt,
93 + 'date' => $date,
94 + 'editUrl' => $this->get_edit_post_link( $post ),
95 + 'previewUrl' => PreviewHelper::get_post_preview_url( $post ),
96 + 'id' => $post->ID,
97 + 'labels' => [],
98 + 'order' => $post->menu_order,
99 + 'parent' => $post->post_parent,
100 + 'password' => $post->post_password,
101 + 'pingbacksAllowed' => 'open' === $post->ping_status ? true : false,
102 + 'slug' => $post->post_name,
103 + 'status' => $post->post_status,
104 + 'template' => empty( $template ) ? 'default' : $template,
105 + 'terms' => [],
106 + 'thumbnail' => get_the_post_thumbnail_url( $post, 'thumbnail' ),
107 + 'thumbnailData' => $thumb_data,
108 + 'title' => empty( $post->post_title ) ? __( '(no title)', 'assistant' ) : $post->post_title,
109 + 'trashedStatus' => get_post_meta( $post->ID, '_wp_trash_meta_status', true ),
110 + 'type' => $post->post_type,
111 + 'url' => get_permalink( $post ),
112 + 'visibility' => 'public',
113 + 'commentsCount' => get_comments_number( $post->ID ),
114 + 'isSticky' => is_sticky( $post->ID ),
115 + 'authorList' => $users,
116 + 'hasLock' => wp_check_post_lock( $post->ID ),
117 + ];
118 +
119 + // Post visibility.
120 + if ( 'private' === $post->post_status ) {
121 + $response['visibility'] = 'private';
122 + } elseif ( ! empty( $post->post_password ) ) {
123 + $response['visibility'] = 'protected';
124 + }
125 +
126 + // Beaver Builder data.
127 + if ( $this->beaver_builder->is_installed() ) {
128 + $response['bbCanEdit'] = $this->beaver_builder->can_edit_post( $post->ID );
129 + $response['bbIsEnabled'] = \FLBuilderModel::is_builder_enabled( $post->ID );
130 + $response['bbBranding'] = \FLBuilderModel::get_branding();
131 + $response['bbEditUrl'] = \FLBuilderModel::get_edit_url( $post->ID );
132 + }
133 +
134 + // Favorites
135 + $favorites = $this->notations->get_favorites( 'post', $post->ID, get_current_user_id() );
136 + $response['isFavorite'] = ! ! count( $favorites );
137 +
138 + // Labels
139 + $labels = $this->notations->get_labels( 'post', $post->ID );
140 + foreach ( $labels as $label ) {
141 + $response['labels'][] = $label['label_id'];
142 + }
143 +
144 + // Terms
145 + $taxonomies = get_object_taxonomies( $post->post_type, 'objects' );
146 + foreach ( $taxonomies as $tax_slug => $tax ) {
147 + if ( ! $tax->public || ! $tax->show_ui ) {
148 + continue;
149 + }
150 + $response['terms'][ $tax_slug ] = $this->terms->find_where(
151 + [
152 + 'taxonomy' => $tax_slug,
153 + 'object_ids' => [ $post->ID ],
154 + 'orderby' => 'name',
155 + 'order' => 'ASC',
156 + 'fields' => 'ids',
157 + ]
158 + );
159 + }
160 +
161 + return $response;
162 + }
163 +
164 +}