Facades
2 weeks ago
Canvas.php
2 weeks ago
CollectionItem.php
2 weeks ago
ContentManager.php
2 weeks ago
DateTime.php
2 weeks ago
EditorPreview.php
2 weeks ago
FileHandler.php
2 weeks ago
FilterHooks.php
2 weeks ago
PageUrl.php
2 weeks ago
Role.php
2 weeks ago
Template.php
2 weeks ago
ContentManager.php
89 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kirki\App\Supports; |
| 4 | |
| 5 | use Kirki\App\Models\Post; |
| 6 | use Kirki\App\Models\PostMeta; |
| 7 | use function Kirki\Framework\with_prefix; |
| 8 | |
| 9 | /** |
| 10 | * Pure, reusable helpers for the Content Manager. |
| 11 | * |
| 12 | * This class holds only side-effect-free helpers (constants, key/string |
| 13 | * builders and the slug check). All fetching/checking/saving business logic |
| 14 | * lives in the service layer, and reference-table access goes through the |
| 15 | * {@see \Kirki\App\Models\Reference} model. |
| 16 | */ |
| 17 | class ContentManager |
| 18 | { |
| 19 | public const RESERVED_SLUG_WORDS = ['attachment', 'author', 'category', 'comment', 'feed', 'page', 'post', 'tag', 'profile', 'index']; |
| 20 | |
| 21 | /** |
| 22 | * Get available custom fields for a collection. |
| 23 | * |
| 24 | * @param int $collection_id |
| 25 | * @return array |
| 26 | */ |
| 27 | public static function get_available_custom_fields(int $collection_id) |
| 28 | { |
| 29 | $fields = PostMeta::where('post_id', $collection_id) |
| 30 | ->where('meta_key', with_prefix('cm_fields')) |
| 31 | ->first(); |
| 32 | |
| 33 | if (empty($fields)) { |
| 34 | return []; |
| 35 | } |
| 36 | |
| 37 | return $fields->meta_value ? $fields->meta_value : []; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Validate a post slug against reserved words and WordPress rules. |
| 42 | * |
| 43 | * @param int $post_id |
| 44 | * @param string $post_type |
| 45 | * @param string $post_name |
| 46 | * @return bool |
| 47 | */ |
| 48 | public static function validate_slug(int $post_id, string $post_type, string $post_name) |
| 49 | { |
| 50 | if (in_array($post_name, static::RESERVED_SLUG_WORDS, true)) { |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | $post_query = Post::where('post_type', $post_type) |
| 55 | ->where('post_name', $post_name); |
| 56 | |
| 57 | if (!empty($post_id)) { |
| 58 | $post_query->where('ID', '!=', $post_id); |
| 59 | } |
| 60 | |
| 61 | $post = $post_query->first(); |
| 62 | |
| 63 | return empty($post); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Build the child post type value for a parent collection. |
| 68 | * |
| 69 | * @param int $post_parent |
| 70 | * @return string |
| 71 | */ |
| 72 | public static function get_child_post_post_type_value($post_parent) |
| 73 | { |
| 74 | return with_prefix('cm_' . $post_parent); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Build the child post meta key for a parent collection field. |
| 79 | * |
| 80 | * @param int $post_parent |
| 81 | * @param string $field_id |
| 82 | * @return string |
| 83 | */ |
| 84 | public static function get_child_post_meta_key_using_field_id($post_parent, $field_id) |
| 85 | { |
| 86 | return with_prefix('cm_field_' . $post_parent . '_' . $field_id); |
| 87 | } |
| 88 | } |
| 89 |