Skip to content

Commit 01ed90f

Browse files
committed
Restore previous classes in a Legacy namespace for easier transition to typed classes
1 parent 6255c73 commit 01ed90f

File tree

4 files changed

+464
-0
lines changed

4 files changed

+464
-0
lines changed

src/Legacy/PostMeta.php

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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+
}

src/Legacy/PostType.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* PostType class
4+
*
5+
* Deprecated: Use Required\Common\PostType 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 custom post types.
16+
*
17+
* @since 0.1.0
18+
*/
19+
abstract class PostType implements Registrable {
20+
21+
public const NAME = 'post';
22+
23+
/**
24+
* Creates a post type object.
25+
*
26+
* @since 0.1.0
27+
*
28+
* @return bool Whether the post type was registered successfully.
29+
*/
30+
public function register(): bool {
31+
if ( ! post_type_exists( static::NAME ) ) {
32+
$post_type = register_post_type(
33+
static::NAME,
34+
$this->get_args()
35+
);
36+
return ! is_wp_error( $post_type );
37+
}
38+
39+
return true;
40+
}
41+
42+
/**
43+
* Gets post type arguments for this post type object.
44+
*
45+
* @since 0.1.0
46+
*
47+
* @return array Post type arguments.
48+
*/
49+
abstract protected function get_args(): array;
50+
}

src/Legacy/Taxonomy.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* Taxonomy class
4+
*
5+
* Deprecated: Use Required\Common\Taxonomy 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 custom taxonomies.
16+
*
17+
* @since 0.1.0
18+
*/
19+
abstract class Taxonomy implements Registrable {
20+
21+
public const NAME = 'category';
22+
23+
/**
24+
* Object types the taxonomy is associated with.
25+
*
26+
* @since 0.1.0
27+
*
28+
* @var array
29+
*/
30+
protected $object_types;
31+
32+
/**
33+
* Creates a taxonomy object.
34+
*
35+
* @since 0.1.0
36+
*
37+
* @return bool Whether the taxonomy was registered successfully.
38+
*/
39+
public function register(): bool {
40+
if ( ! taxonomy_exists( static::NAME ) ) {
41+
$taxonomy = register_taxonomy(
42+
static::NAME,
43+
$this->get_object_types(),
44+
$this->get_args()
45+
);
46+
return ! is_wp_error( $taxonomy );
47+
}
48+
49+
return true;
50+
}
51+
52+
/**
53+
* Sets object types the taxonomy is associated with.
54+
*
55+
* @since 0.1.0
56+
*
57+
* @param array|string $object_type Object type or array of object types with which the taxonomy should be associated.
58+
*/
59+
public function set_object_types( $object_type ) {
60+
$this->object_types = (array) $object_type;
61+
}
62+
63+
/**
64+
* Gets object types the taxonomy is associated with.
65+
*
66+
* @since 0.1.0
67+
*
68+
* @return array Object types the taxonomy is associated with.
69+
*/
70+
public function get_object_types() {
71+
return $this->object_types;
72+
}
73+
74+
/**
75+
* Gets taxonomy arguments for this taxonomy object.
76+
*
77+
* @since 0.1.0
78+
*
79+
* @return array Taxonomy arguments.
80+
*/
81+
abstract protected function get_args();
82+
}

0 commit comments

Comments
 (0)