Skip to content

Commit

Permalink
Merge pull request #385 from BenjaminMedia/VOLD-1889/post-endpoint
Browse files Browse the repository at this point in the history
VOLD-1899: Added post-endpoint to use with migration, bump version to…
  • Loading branch information
SorenThorup committed May 16, 2024
2 parents 56cc6b5 + b6d5d59 commit 0e2ab26
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 14 deletions.
6 changes: 5 additions & 1 deletion contenthub-editor.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Plugin Name: ContentHub Editor
* Version: 1.18.4
* Version: 1.19.0
* Plugin URI: https://github.com/BenjaminMedia/contenthub-editor
* Description: This plugin integrates Bonnier Contenthub and adds a custom post type Composite
* Author: Bonnier - Alf Henderson
Expand All @@ -21,6 +21,7 @@
use Bonnier\WP\ContentHub\Editor\Models\WpTaxonomy;
use Bonnier\WP\ContentHub\Editor\Rss\FeedRss;
use Bonnier\WP\ContentHub\Editor\Settings\SettingsPage;
use Bonnier\WP\ContentHub\Editor\Http\Api\ContentController;

// Do not access this file directly
if (!defined('ABSPATH')) {
Expand Down Expand Up @@ -113,6 +114,9 @@ private function __construct()
$updateEndpoint = new UpdateEndpointController();
$updateEndpoint->register_routes();

$contentController = new ContentController();
$contentController->register_routes();

add_action('admin_enqueue_scripts', [$this, 'loadAdminScripts']);
}

Expand Down
6 changes: 4 additions & 2 deletions src/Commands/Composites.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,13 +336,15 @@ private function handle_locked_content($postId, $composite)
{
$accessRules = collect($composite->accessRules->edges)->pluck('node');
if (!$accessRules->isEmpty()) {
update_field('locked_content',
update_field(
'locked_content',
$accessRules->first(function ($rule) {
return $rule->domain === 'All' && $rule->kind === 'Deny';
}) ? true : false,
$postId
);
update_field('required_user_role',
update_field(
'required_user_role',
$accessRules->first(function ($rule) {
return in_array($rule->domain, ['Subscriber', 'RegUser']) && $rule->kind === 'Allow';
})->domain,
Expand Down
1 change: 0 additions & 1 deletion src/Helpers/PermalinkHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ public function custom_rules($rules)

// add our $permalink_structures scoped to the post types - overwriting any keys that already exist
foreach ($permalink_structures as $struct => $post_types) {

// if a struct is %postname% only then we need page rules first - if not found wp tries again with later rules
if (preg_match('/^\/?%postname%\/?$/', $struct)) {
$wp_rewrite->use_verbose_page_rules = true;
Expand Down
4 changes: 3 additions & 1 deletion src/Helpers/PolylangConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public static function register()

public static function polylang_options($defaultOptions)
{
return array_merge($defaultOptions, [
return array_merge(
$defaultOptions,
[
'post_types' => [
WpComposite::POST_TYPE, // Tell polylang to enable translation for our custom content type
],
Expand Down
194 changes: 194 additions & 0 deletions src/Http/Api/ContentController.php
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;
}
}
15 changes: 9 additions & 6 deletions src/Models/WpAttachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,17 @@ private static function update_attachment($attachmentId, $file)
update_post_meta($attachmentId, '_wp_attachment_image_alt', static::getAlt($file));
update_post_meta($attachmentId, static::POST_META_COPYRIGHT, $file->copyright ?? '');
global $wpdb;
$wpdb->update('wp_posts', [
'post_title' => $file->title ?? $file->caption ?? '',
'post_content' => '',
'post_excerpt' => $file->caption ?? '',
],
$wpdb->update(
'wp_posts',
[
'post_title' => $file->title ?? $file->caption ?? '',
'post_content' => '',
'post_excerpt' => $file->caption ?? '',
],
[
'ID' => $attachmentId
]);
]
);
}

private static function set_s3_object_visibility($bucket, $key, $acl)
Expand Down
3 changes: 2 additions & 1 deletion src/Models/WpComposite.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public static function register()
static::register_permalink();

add_action('init', function () {
register_post_type(static::POST_TYPE,
register_post_type(
static::POST_TYPE,
[
'labels' => [
'name' => __(static::POST_TYPE_NAME),
Expand Down
6 changes: 4 additions & 2 deletions src/Rss/MsnFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ class MsnFeed extends Feed
*/
public function render()
{
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><rss xmlns:atom="http://www.w3.org/2005/Atom"
$xml = new SimpleXMLElement(
'<?xml version="1.0" encoding="UTF-8" ?><rss xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:media="http://search.yahoo.com/mrss/"
xmlns:mi="http://schemas.ingestion.microsoft.com/common/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dcterms="http://purl.org/dc/terms/"
version="2.0">',
LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL);
LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL
);

foreach ($this->channels as $channel) {
$toDom = dom_import_simplexml($xml);
Expand Down

0 comments on commit 0e2ab26

Please sign in to comment.