-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #385 from BenjaminMedia/VOLD-1889/post-endpoint
VOLD-1899: Added post-endpoint to use with migration, bump version to…
- Loading branch information
Showing
8 changed files
with
221 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
<?php | ||
|
||
namespace Bonnier\WP\ContentHub\Editor\Http\Api; | ||
|
||
use Bonnier\WP\ContentHub\Editor\Models\WpComposite; | ||
use PLL_Admin_Share_Term_Slug; | ||
use wpSiteManager\Services\CategoryService; | ||
use WP_REST_Controller; | ||
use WP_REST_Request; | ||
use WP_REST_Response; | ||
use WP_REST_Server; | ||
use WP_Query; | ||
|
||
class ContentController extends WP_REST_Controller | ||
{ | ||
public function register_routes() | ||
{ | ||
add_action('rest_api_init', function () { | ||
register_rest_route('app/content', '/published', [ | ||
'methods' => WP_REST_Server::READABLE, | ||
'callback' => [$this, 'published'], | ||
'permission_callback' => '__return_true' | ||
]); | ||
|
||
register_rest_route('app/content', '/post/(?P<id>[0-9]+)', [ | ||
'methods' => WP_REST_Server::READABLE, | ||
'callback' => [$this, 'post'], | ||
'args' => ['id'], | ||
'permission_callback' => '__return_true' | ||
]); | ||
}); | ||
} | ||
|
||
public function updateCallback(WP_REST_Request $request): WP_REST_Response | ||
{ | ||
return new WP_REST_Response(['status' => 'OK']); | ||
} | ||
|
||
public function published(WP_REST_Request $request) | ||
{ | ||
if (!$request) { | ||
return false; | ||
} | ||
|
||
$status = $request->get_param('status'); | ||
if (!in_array($status, ['pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash', 'publish'])) { | ||
$status = 'any'; | ||
} | ||
|
||
$perPage = $request->get_param('per_page'); | ||
if (!is_numeric($perPage)) { | ||
$perPage = 500; | ||
} | ||
|
||
$currentPage = $request->get_param('page'); | ||
if (!is_numeric($currentPage)) { | ||
$currentPage = 1; | ||
} | ||
|
||
$postId = $request->get_param('id'); | ||
if (!is_numeric($postId)) { | ||
$postId = null; | ||
} | ||
|
||
$query_args = [ | ||
'post_type' => 'contenthub_composite', | ||
'post_status' => $status, | ||
'posts_per_page' => $perPage, | ||
'paged' => $currentPage, | ||
'orderby' => 'modified', | ||
'order' => 'desc', | ||
]; | ||
|
||
if ($postId) { | ||
$query_args['post__in'] = [$postId]; | ||
} | ||
|
||
$data = []; | ||
$query = new WP_Query($query_args); | ||
foreach ($query->posts as $post) { | ||
$data[] = [ | ||
'id' => $post->ID, | ||
'canonical_url' => get_field('canonical_url', $post->ID), | ||
'exclude_platforms' => get_field('exclude_platforms', $post->ID), | ||
'hide_from_sitemap' => get_field('sitemap', $post->ID), | ||
'post_date' => $post->post_date, | ||
'post_date_gmt' => $post->post_date_gmt, | ||
'modified' => $post->post_modified, | ||
'modified_gmt' => $post->post_modified_gmt, | ||
'status' => $post->post_status, | ||
'title' => $post->post_title, | ||
'url' => parse_url(get_permalink($post), PHP_URL_PATH), | ||
]; | ||
} | ||
|
||
return new WP_REST_Response( | ||
[ | ||
'count' => sizeof($query->posts), | ||
'total' => intval($query->found_posts), | ||
'page' => intval($currentPage), | ||
'pages' => $query->max_num_pages, | ||
'pll_language' => pll_current_language(), | ||
'bloginfo_language' => get_bloginfo("language"), | ||
'locale' => get_locale(), | ||
'home_url' => rtrim(pll_home_url(), '/'), | ||
'version' => '5', | ||
'data' => $data | ||
] | ||
); | ||
} | ||
|
||
public function post(WP_REST_Request $request): WP_REST_Response | ||
{ | ||
$postId = $request['id']; | ||
$post = (array) get_post($postId); | ||
unset($post['post_password']); | ||
|
||
// Flatten all the meta-arrays with only one element | ||
$post['meta'] = array_map(function ($val) { | ||
return $val[0]; | ||
}, get_post_meta($postId)); | ||
|
||
// Add image data | ||
if (isset($post['post_type']) && $post['post_type'] == 'attachment' && substr($post['post_mime_type'], 0, 5) == 'image') { | ||
$imageData = wp_get_attachment_image_src($postId, 'full', false); | ||
$post['file']['url'] = $imageData[0]; | ||
$post['file']['width'] = $imageData[1]; | ||
$post['file']['height'] = $imageData[2]; | ||
$post['file']['resized'] = $imageData[3]; | ||
} | ||
|
||
if (isset($post['post_type']) && $post['post_type'] == 'contenthub_composite') { | ||
$post['composite'] = $this->composite($postId); | ||
} | ||
|
||
return new WP_REST_Response($post); | ||
} | ||
|
||
private function composite($postId) | ||
{ | ||
$post = (array) get_post($postId); | ||
|
||
if ($post['ID'] != $postId) { | ||
exit; | ||
} | ||
|
||
$meta = array_map(function ($val) { | ||
return $val[0]; | ||
}, get_post_meta($postId)); | ||
|
||
$author = []; | ||
$user = get_user_by('id', $post['post_author']); | ||
if ($user) { | ||
$author['id'] = $user->ID; | ||
$author['name'] = $user->display_name; | ||
} | ||
|
||
$category = []; | ||
if ($meta['category']) { | ||
$wpCategory = get_term($meta['category']); | ||
$category['data']['id'] = $meta['category']; | ||
$category['data']['name'] = $wpCategory->name; | ||
$category['data']['url'] = get_category_link($meta['category']); | ||
} | ||
|
||
$tags = []; | ||
$wordpressTags = get_the_tags($postId); | ||
foreach ($wordpressTags as $wordpressTag) { | ||
if ($wordpressTag->taxonomy != 'post_tag') { | ||
continue; | ||
} | ||
$tag = []; | ||
$tag['name'] = $wordpressTag->name; | ||
$tag['slug'] = $wordpressTag->slug; | ||
$tags[] = $tag; | ||
} | ||
|
||
$data = []; | ||
$data['id'] = $post['ID']; | ||
$data['title'] = $post['post_title']; | ||
$data['status'] = $post['post_status']; | ||
$data['language'] = pll_get_post_language($postId); | ||
$data['locale'] = pll_get_post_language($postId); | ||
$data['canonical_url'] = get_permalink($postId); | ||
$data['author'] = $author; | ||
$data['other_authors'] = []; | ||
$data['category'] = $category; | ||
$data['tags']['data'] = $tags; | ||
|
||
$data['translations'] = pll_get_post_translations($postId); | ||
|
||
return $data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters