<?php
|
namespace JVBase\rest\routes;
|
|
use JVBase\JVB;
|
use JVBase\rest\RestRouteManager;
|
use JVBase\meta\MetaManager;
|
use JVBase\managers\UploadManager;
|
use JVBase\managers\ImageGenerator;
|
use WP_REST_Request;
|
use WP_REST_Response;
|
use WP_Error;
|
|
if (!defined('ABSPATH')) {
|
exit; // Exit if accessed directly
|
}
|
//TODO: Invalidate feed caches on updates
|
class BioRoutes extends RestRouteManager
|
{
|
protected int $count;
|
public function __construct()
|
{
|
$this->cache_name = 'user_bio';
|
parent::__construct();
|
add_filter(BASE.'handle_bulk_operation', [$this, 'generateThumbnail'], 10, 3);
|
add_filter(BASE.'handle_bulk_operation', [$this, 'processOperation'], 10, 3);
|
$this->content_type = 'artist';
|
$this->type = 'post';
|
$this->action = 'dash-';
|
$this->count = 1;
|
$this->operation_type = 'bio_update';
|
}
|
|
/**
|
* Register bio routes
|
* @return void
|
*/
|
public function registerRoutes():void
|
{
|
register_rest_route($this->namespace, '/bio', [
|
[
|
'methods' => 'POST',
|
'callback' => [ $this, 'handleBioRequest' ],
|
'permission_callback' => [ $this, 'checkPermission' ]
|
]
|
]);
|
}
|
|
/**
|
* @param WP_REST_Request $request
|
*
|
* @return WP_REST_Response
|
*/
|
public function handleBioRequest(WP_REST_Request $request)
|
{
|
error_log('Bio Request: '.print_r($request->get_params(), true));
|
|
$data = $request->get_params();
|
error_log('Data: '.print_r($data, true));
|
$user = $data['user'];
|
$operationID = $data['id'];
|
unset($data['user']);
|
unset($data['id']);
|
if (!$this->userCheck($user)) {
|
return new WP_REST_Response([
|
'success' => false,
|
'message' => 'Looks like you may not be who you say you are...'
|
]);
|
}
|
// $data = json_encode($data);
|
if (array_key_exists('term_name', $data)) {
|
unset($data['term_name']);
|
unset($data['select_parent']);
|
}
|
|
$queue = JVB()->queue();
|
$queue->queueOperation(
|
'bio_update',
|
$user,
|
$data,
|
[
|
'operation_id' => $operationID,
|
'priority' => 'high',
|
'notification' => true,
|
]
|
);
|
|
return new WP_REST_Response([
|
'success' => true,
|
'message' => 'Updates queued for processing'
|
]);
|
}
|
|
|
/**
|
* @param WP_Error|array $result
|
* @param object $operation
|
* @param array $data
|
*
|
* @return WP_Error|array
|
*/
|
public function processOperation(WP_Error|array $result, object $operation, array $data):WP_Error|array
|
{
|
if ($operation->type !== 'bio_update') {
|
return $result;
|
}
|
$user_id = $operation->user_id;
|
$userLink = get_user_meta($user_id, BASE.'link', true);
|
$type = get_post_type((int)$userLink);
|
|
if (!in_array($type, [BASE.'artist', BASE.'partner'])) {
|
return [
|
'success' => false,
|
'error' => 'Invalid User Type'
|
];
|
}
|
|
$meta = new MetaManager($userLink, 'post');
|
$results = [];
|
|
error_log('processing data: '.print_r($data, true));
|
$progress = false;
|
if (array_key_exists('progress', $data)) {
|
$progress = $data['progress'];
|
unset($data['progress']);
|
}
|
$fields = jvbGetFields($type, 'post');
|
foreach ($data as $name=>$value) {
|
if ($name === 'shop') {
|
JVB()->routes('shop')->requestShopAdmission($operation->user_id, $value);
|
$data['requested_shop'] = $value;
|
unset($data['shop']);
|
}
|
}
|
$allowedFields = array_filter(
|
$data,
|
function ($key) use ($fields) {
|
return array_key_exists($key, $fields);
|
},
|
ARRAY_FILTER_USE_KEY
|
);
|
$meta->setAll($allowedFields);
|
|
if ($progress) {
|
$data['progress'] = $progress;
|
}
|
if (array_intersect(
|
array_keys($data),
|
[
|
'image_portrait',
|
'shop',
|
'city',
|
'type',
|
'top_style',
|
'display_name'
|
]
|
)) {
|
if ((array_key_exists('image_portrait', $data) && $data['image_portrait']!=='') || $meta->getValue('image_portrait') !== '') {
|
$this->checkGenerateThumbnail($user_id, $this->buildThumbnailData($user_id));
|
}
|
}
|
|
return [
|
'success' => true,
|
'result' => $results,
|
];
|
}
|
|
|
/**
|
* Queues a featured image generator
|
* @param int $user_id
|
* @param array $data
|
*
|
* @return void
|
*/
|
protected function checkGenerateThumbnail(int $user_id, array $data):void
|
{
|
if (!$this->checkUser($user_id)) {
|
return;
|
}
|
if (empty($data)) {
|
return;
|
}
|
if (array_key_exists('image_portrait', $data) && !is_int($data['image_portrait'])) {
|
return;
|
}
|
$queue = JVB()->queue();
|
$queue->queueOperation(
|
'featured_image',
|
$user_id,
|
$data
|
);
|
}
|
|
/**
|
* Processes the featured image generation operation
|
* @param WP_Error|array $result
|
* @param object $operation
|
* @param array $data
|
*
|
* @return WP_Error|array
|
*/
|
public function generateThumbnail(WP_Error|array $result, object $operation, array $data):WP_Error|array
|
{
|
if ($operation->type !== 'featured_image') {
|
return $result;
|
}
|
$data['imageType'] = 'artist';
|
$fileGenerator = new UploadManager('artist', $operation->user_id);
|
$generator = new ImageGenerator($data, $fileGenerator);
|
$result = $generator->generate();
|
if ($result['success']) {
|
set_post_thumbnail(get_user_meta($operation->user_id, BASE.'link', true), $result['attachment_id']);
|
}
|
return $result;
|
}
|
|
/**
|
* @param int $user_id
|
*
|
* @return array
|
*/
|
protected function buildThumbnailData(int $user_id):array
|
{
|
if (!$this->checkUser($user_id)) {
|
return [];
|
}
|
//TODO: Check we're clearing this cache
|
$key = sprintf('user_%c_thumbnail_data', $user_id);
|
$cache = $this->cache->get($key);
|
if ($cache) {
|
return $cache;
|
}
|
|
$link = get_user_meta($user_id, BASE.'link', true);
|
$userMeta = new MetaManager($user_id, 'user');
|
$postMeta = new MetaManager($link, 'post');
|
$styles = explode(',', $postMeta->getValue('top_style'));
|
$temp = [];
|
if (!empty($styles)) {
|
foreach ($styles as $style) {
|
if (term_exists($style, BASE.'style')) {
|
$temp[] = get_term((int)$style, BASE.'style')->name;
|
}
|
}
|
$styles = (empty($temp)) ? '' : implode(',', $temp);
|
} else {
|
$styles = '';
|
}
|
|
|
$shop = get_term((int)$postMeta->getValue('shop'), BASE.'shop')->name??'';
|
|
$data = [
|
'user_id' => $user_id,
|
'display_name' => $userMeta->getValue('display_name'),
|
'image' => $postMeta->getValue('image_portrait'),
|
'shop' => $shop,
|
'styles' => $styles,
|
'city' => jvbArtistCity($link),
|
'type' => jvbArtistType($link),
|
];
|
|
$this->cache->set($key, $data);
|
|
return $data;
|
}
|
}
|