PluginProbe
Assistant – Every Day Productivity Apps / 1.0.2
Assistant – Every Day Productivity Apps v1.0.2
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 / Data / Transformers / PostTransformer.php

PostTransformer.php in Assistant – Every Day Productivity Apps 1.0.2, at backend/src/Data/Transformers/PostTransformer.php

150 lines 4.5 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\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 array
53 */
54 public function transform( \WP_Post $post ) {
55 $author = get_the_author_meta( 'display_name', $post->post_author );
56 $date = get_the_date( '', $post );
57 $template = get_post_meta( $post->ID, '_wp_page_template', true );
58
59 $thumb_id = get_post_thumbnail_id( $post );
60 $thumb_data = wp_prepare_attachment_for_js( $thumb_id );
61
62 $all_users = get_users( [ 'who' => 'authors' ] );
63 $users = [];
64
65 foreach ( $all_users as $user ) {
66 $users[ $user->ID ] = $user->display_name;
67 }
68
69 if ( ! function_exists( 'wp_check_post_lock' ) ) {
70 require_once ABSPATH . 'wp-admin/includes/post.php';
71 }
72
73 $response = [
74 'author' => $author,
75 'post_author' => $post->post_author,
76 'commentsAllowed' => 'open' === $post->comment_status ? true : false,
77 'excerpt' => $post->post_excerpt,
78 'date' => $date,
79 'editUrl' => get_edit_post_link( $post->ID, '' ),
80 'previewUrl' => PreviewHelper::get_post_preview_url( $post ),
81 'id' => $post->ID,
82 'labels' => [],
83 'order' => $post->menu_order,
84 'parent' => $post->post_parent,
85 'password' => $post->post_password,
86 'pingbacksAllowed' => 'open' === $post->ping_status ? true : false,
87 'slug' => $post->post_name,
88 'status' => $post->post_status,
89 'template' => empty( $template ) ? 'default' : $template,
90 'terms' => [],
91 'thumbnail' => get_the_post_thumbnail_url( $post, 'thumbnail' ),
92 'thumbnailData' => $thumb_data,
93 'title' => empty( $post->post_title ) ? __( '(no title)', 'fl-assistant' ) : $post->post_title,
94 'trashedStatus' => get_post_meta( $post->ID, '_wp_trash_meta_status', true ),
95 'type' => $post->post_type,
96 'url' => get_permalink( $post ),
97 'visibility' => 'public',
98 'commentsCount' => get_comments_number( $post->ID ),
99 'isSticky' => is_sticky( $post->ID ),
100 'authorList' => $users,
101 'hasLock' => wp_check_post_lock( $post->ID ),
102 ];
103
104 // Post visibility.
105 if ( 'private' === $post->post_status ) {
106 $response['visibility'] = 'private';
107 } elseif ( ! empty( $post->post_password ) ) {
108 $response['visibility'] = 'protected';
109 }
110
111 // Beaver Builder data.
112 if ( $this->beaver_builder->is_installed() ) {
113 $response['bbCanEdit'] = $this->beaver_builder->can_edit_post( $post->ID );
114 $response['bbIsEnabled'] = \FLBuilderModel::is_builder_enabled( $post->ID );
115 $response['bbBranding'] = \FLBuilderModel::get_branding();
116 $response['bbEditUrl'] = \FLBuilderModel::get_edit_url( $post->ID );
117 }
118
119 // Favorites
120 $favorites = $this->notations->get_favorites( 'post', $post->ID, get_current_user_id() );
121 $response['isFavorite'] = ! ! count( $favorites );
122
123 // Labels
124 $labels = $this->notations->get_labels( 'post', $post->ID );
125 foreach ( $labels as $label ) {
126 $response['labels'][] = $label['label_id'];
127 }
128
129 // Terms
130 $taxonomies = get_object_taxonomies( $post->post_type, 'objects' );
131 foreach ( $taxonomies as $tax_slug => $tax ) {
132 if ( ! $tax->public || ! $tax->show_ui ) {
133 continue;
134 }
135 $response['terms'][ $tax_slug ] = $this->terms->find_where(
136 [
137 'taxonomy' => $tax_slug,
138 'object_ids' => [ $post->ID ],
139 'orderby' => 'name',
140 'order' => 'ASC',
141 'fields' => 'ids',
142 ]
143 );
144 }
145
146 return $response;
147 }
148
149 }
150