|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * PostMeta class |
| 4 | + * |
| 5 | + * Deprecated: Use Required\Common\PostMeta instead. |
| 6 | + * |
| 7 | + * @since 0.3.1 |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Required\Common\Legacy; |
| 11 | + |
| 12 | +use Required\Common\Contracts\Registrable; |
| 13 | + |
| 14 | +/** |
| 15 | + * Class used to implement post meta. |
| 16 | + * |
| 17 | + * @since 0.1.0 |
| 18 | + */ |
| 19 | +abstract class PostMeta implements Registrable { |
| 20 | + |
| 21 | + /** |
| 22 | + * Post meta key. |
| 23 | + */ |
| 24 | + public const KEY = null; |
| 25 | + |
| 26 | + /** |
| 27 | + * Registers the object. |
| 28 | + * |
| 29 | + * @since 0.1.0 |
| 30 | + * |
| 31 | + * @return bool Whether the post meta was registered successfully. |
| 32 | + */ |
| 33 | + public function register(): bool { |
| 34 | + return register_post_meta( |
| 35 | + $this->post_type(), |
| 36 | + static::KEY, |
| 37 | + $this->get_args() |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Gets post meta arguments for this post meta object. |
| 43 | + * |
| 44 | + * @since 0.1.0 |
| 45 | + * |
| 46 | + * @see register_meta() For the supported arguments. |
| 47 | + * |
| 48 | + * @return array Post meta arguments. |
| 49 | + */ |
| 50 | + protected function get_args(): array { |
| 51 | + return [ |
| 52 | + 'type' => $this->type(), |
| 53 | + 'description' => $this->description(), |
| 54 | + 'single' => $this->is_single(), |
| 55 | + 'default' => $this->default(), |
| 56 | + 'sanitize_callback' => [ $this, 'sanitize' ], |
| 57 | + 'auth_callback' => [ $this, 'auth' ], |
| 58 | + 'show_in_rest' => $this->show_in_rest(), |
| 59 | + ]; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * The post type to register a meta key for. |
| 64 | + * |
| 65 | + * Pass an empty string to register the meta key across all existing post types. |
| 66 | + * |
| 67 | + * @since 0.1.0 |
| 68 | + * |
| 69 | + * @return string Post type to register a meta key for. |
| 70 | + */ |
| 71 | + protected function post_type(): string { |
| 72 | + return ''; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * The type of data associated with this meta key. |
| 77 | + * |
| 78 | + * Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'. |
| 79 | + * |
| 80 | + * @since 0.1.0 |
| 81 | + * |
| 82 | + * @return string Type of the data. |
| 83 | + */ |
| 84 | + protected function type(): string { |
| 85 | + return 'string'; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * A description of the data attached to this meta key. |
| 90 | + * |
| 91 | + * @since 0.1.0 |
| 92 | + * |
| 93 | + * @return string Description of the data. |
| 94 | + */ |
| 95 | + protected function description(): string { |
| 96 | + return ''; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Whether the meta key has one value per object, or an array of values per object. |
| 101 | + * |
| 102 | + * @since 0.1.0 |
| 103 | + * |
| 104 | + * @return bool Whether the meta key has one value per object. |
| 105 | + */ |
| 106 | + protected function is_single(): bool { |
| 107 | + return false; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * The default value if no value has been set yet. |
| 112 | + * |
| 113 | + * @since 0.1.0 |
| 114 | + * |
| 115 | + * @return mixed The default value. |
| 116 | + */ |
| 117 | + protected function default() { |
| 118 | + return ''; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Sanitization of the post meta data. |
| 123 | + * |
| 124 | + * @since 0.1.0 |
| 125 | + * |
| 126 | + * @param mixed $meta_value Meta value to sanitize. |
| 127 | + * @param string $meta_key Meta key. |
| 128 | + * @param string $object_type Object type. |
| 129 | + * @return mixed Sanitized meta value. |
| 130 | + */ |
| 131 | + public function sanitize( $meta_value, $meta_key, $object_type ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 132 | + return $meta_value; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Whether the user is allowed to edit meta. |
| 137 | + * |
| 138 | + * @since 0.1.0 |
| 139 | + * |
| 140 | + * @param bool $allowed Whether the user can add the post meta. Default false. |
| 141 | + * @param string $meta_key The meta key. |
| 142 | + * @param int $object_id Object ID. |
| 143 | + * @param int $user_id User ID. |
| 144 | + * @param string $cap Capability name. |
| 145 | + * @param array $caps User capabilities. |
| 146 | + * @return bool False if the key is protected, true otherwise. |
| 147 | + */ |
| 148 | + public function auth( $allowed, $meta_key, $object_id, $user_id, $cap, $caps ): bool { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 149 | + return ! is_protected_meta( $meta_key, 'post' ); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Whether data associated with this meta key can be considered public and |
| 154 | + * should be accessible via the REST API. |
| 155 | + * |
| 156 | + * When registering complex meta values this argument may optionally be an |
| 157 | + * array with 'schema' or 'prepare_callback' keys instead of a boolean. |
| 158 | + * |
| 159 | + * @since 0.1.0 |
| 160 | + * |
| 161 | + * @return bool|array Whether the data is public. |
| 162 | + */ |
| 163 | + protected function show_in_rest() { |
| 164 | + return false; |
| 165 | + } |
| 166 | +} |
0 commit comments