| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace FL\Assistant\Data\Transformers; |
| 5 |
|
| 6 |
use FL\Assistant\Data\Repository\NotationsRepository; |
| 7 |
use FL\Assistant\Data\Repository\TermsRepository; |
| 8 |
use FL\Assistant\System\Integrations\BeaverBuilder; |
| 9 |
|
| 10 |
|
| 11 |
/** |
| 12 |
* Class PostTransformer |
| 13 |
* |
| 14 |
* Convert a WP_Post object to array suitable for REST output |
| 15 |
* |
| 16 |
* @package FL\Assistant\RestApi\Transformers |
| 17 |
*/ |
| 18 |
class PostTransformer { |
| 19 |
|
| 20 |
protected $notations; |
| 21 |
protected $terms; |
| 22 |
protected $beaver_builder; |
| 23 |
|
| 24 |
/** |
| 25 |
* PostTransformer constructor. |
| 26 |
* |
| 27 |
* @param NotationsRepository $notations |
| 28 |
* @param BeaverBuilder $beaver_builder |
| 29 |
*/ |
| 30 |
public function __construct( |
| 31 |
NotationsRepository $notations, |
| 32 |
TermsRepository $terms, |
| 33 |
BeaverBuilder $beaver_builder |
| 34 |
) { |
| 35 |
$this->notations = $notations; |
| 36 |
$this->terms = $terms; |
| 37 |
$this->beaver_builder = $beaver_builder; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Allow this class to be used as a callback for pagination |
| 42 |
* @param \WP_Post $post |
| 43 |
* |
| 44 |
* @return array |
| 45 |
*/ |
| 46 |
public function __invoke( \WP_Post $post ) { |
| 47 |
return $this->transform( $post ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* @param \WP_Post $post |
| 52 |
* |
| 53 |
* @return array |
| 54 |
*/ |
| 55 |
public function transform( \WP_Post $post ) { |
| 56 |
$author = get_the_author_meta( 'display_name', $post->post_author ); |
| 57 |
$date = get_the_date( '', $post ); |
| 58 |
$template = get_post_meta( $post->ID, '_wp_page_template', true ); |
| 59 |
|
| 60 |
$thumb_id = get_post_thumbnail_id( $post ); |
| 61 |
$thumb_data = wp_prepare_attachment_for_js( $thumb_id ); |
| 62 |
|
| 63 |
$response = [ |
| 64 |
'author' => $author, |
| 65 |
'commentsAllowed' => 'open' === $post->comment_status ? true : false, |
| 66 |
'excerpt' => $post->post_excerpt, |
| 67 |
'date' => $date, |
| 68 |
'editUrl' => get_edit_post_link( $post->ID, '' ), |
| 69 |
'id' => $post->ID, |
| 70 |
'labels' => [], |
| 71 |
'order' => $post->menu_order, |
| 72 |
'parent' => $post->post_parent, |
| 73 |
'password' => $post->post_password, |
| 74 |
'pingbacksAllowed' => 'open' === $post->ping_status ? true : false, |
| 75 |
'slug' => $post->post_name, |
| 76 |
'status' => $post->post_status, |
| 77 |
'template' => empty( $template ) ? 'default' : $template, |
| 78 |
'terms' => [], |
| 79 |
'thumbnail' => get_the_post_thumbnail_url( $post, 'thumbnail' ), |
| 80 |
'thumbnailData' => $thumb_data, |
| 81 |
'title' => empty( $post->post_title ) ? __( '(no title)', 'fl-assistant' ) : $post->post_title, |
| 82 |
'trashedStatus' => get_post_meta( $post->ID, '_wp_trash_meta_status', true ), |
| 83 |
'type' => $post->post_type, |
| 84 |
'url' => get_permalink( $post ), |
| 85 |
'visibility' => 'public', |
| 86 |
'commentsCount' => get_comments_number( $post->ID ), |
| 87 |
]; |
| 88 |
|
| 89 |
// Post visibility. |
| 90 |
if ( 'private' === $post->post_status ) { |
| 91 |
$response['visibility'] = 'private'; |
| 92 |
} elseif ( ! empty( $post->post_password ) ) { |
| 93 |
$response['visibility'] = 'protected'; |
| 94 |
} |
| 95 |
|
| 96 |
// Beaver Builder data. |
| 97 |
if ( $this->beaver_builder->is_installed() ) { |
| 98 |
$response['bbCanEdit'] = $this->beaver_builder->can_edit_post( $post->ID ); |
| 99 |
$response['bbIsEnabled'] = \FLBuilderModel::is_builder_enabled( $post->ID ); |
| 100 |
$response['bbBranding'] = \FLBuilderModel::get_branding(); |
| 101 |
$response['bbEditUrl'] = \FLBuilderModel::get_edit_url( $post->ID ); |
| 102 |
} |
| 103 |
|
| 104 |
// Favorites |
| 105 |
$favorites = $this->notations->get_favorites( 'post', $post->ID, get_current_user_id() ); |
| 106 |
$response['isFavorite'] = ! ! count( $favorites ); |
| 107 |
|
| 108 |
// Labels |
| 109 |
$labels = $this->notations->get_labels( 'post', $post->ID ); |
| 110 |
foreach ( $labels as $label ) { |
| 111 |
$response['labels'][] = $label['label_id']; |
| 112 |
} |
| 113 |
|
| 114 |
// Terms |
| 115 |
$taxonomies = get_object_taxonomies( $post->post_type, 'objects' ); |
| 116 |
foreach ( $taxonomies as $tax_slug => $tax ) { |
| 117 |
if ( ! $tax->public || ! $tax->show_ui ) { |
| 118 |
continue; |
| 119 |
} |
| 120 |
$response['terms'][ $tax_slug ] = $this->terms->find_where( |
| 121 |
[ |
| 122 |
'taxonomy' => $tax_slug, |
| 123 |
'object_ids' => [ $post->ID ], |
| 124 |
'orderby' => 'name', |
| 125 |
'order' => 'ASC', |
| 126 |
'fields' => 'ids', |
| 127 |
] |
| 128 |
); |
| 129 |
} |
| 130 |
|
| 131 |
return $response; |
| 132 |
} |
| 133 |
|
| 134 |
} |
| 135 |
|