fix: add missing methods to includes/Tools/PostTools.php

This commit is contained in:
2026-05-29 12:20:30 +00:00
parent 5c314a103c
commit 93230aa500
+33
View File
@@ -135,4 +135,37 @@ class PostTools {
return ['id' => $postId, 'deleted' => true, 'permanent' => $force];
}
public static function getPostMeta(array $args): array {
$postId = (int) $args['post_id'];
$key = $args['meta_key'] ?? '';
if ($key) {
return ['post_id' => $postId, 'meta_key' => $key, 'meta_value' => get_post_meta($postId, $key, true)];
}
$all = get_post_meta($postId);
$flat = [];
foreach ($all as $k => $v) $flat[$k] = is_array($v) && count($v)===1 ? $v[0] : $v;
return ['post_id' => $postId, 'meta' => $flat];
}
public static function updatePostMeta(array $args): array {
$postId = (int) $args['post_id'];
update_post_meta($postId, sanitize_key($args['meta_key']), $args['meta_value']);
return ['post_id' => $postId, 'meta_key' => $args['meta_key'], 'updated' => true];
}
public static function setFeaturedImage(array $args): array {
$postId = (int) $args['post_id'];
$mediaId = (int) $args['media_id'];
$result = set_post_thumbnail($postId, $mediaId);
if (!$result) throw new \RuntimeException("Failed to set featured image on post {$postId}");
return ['post_id' => $postId, 'media_id' => $mediaId, 'set' => true];
}
public static function countPosts(array $args): array {
$type = $args['post_type'] ?? 'post';
$counts = wp_count_posts($type);
return ['post_type' => $type, 'counts' => (array) $counts];
}
}