Skip to content

Commit 03de263

Browse files
committed
Added Priority attribute to define the priority of the post type or taxonomy declaration
1 parent 432bc2a commit 03de263

13 files changed

Lines changed: 135 additions & 22 deletions

File tree

documentation

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Pollora\Attributes\PostType;
6+
7+
use Attribute;
8+
use Pollora\PostType\Domain\Contracts\PostTypeAttributeInterface;
9+
10+
/**
11+
* Attribute to set the priority a post type declaration.
12+
*
13+
* @Attribute
14+
*/
15+
#[Attribute(Attribute::TARGET_CLASS)]
16+
class Priority extends PostTypeAttribute
17+
{
18+
/**
19+
* Constructor.
20+
*
21+
* @param init $priority The post type priority declaration
22+
*/
23+
public function __construct(
24+
public readonly int $priority = 5
25+
) {}
26+
27+
/**
28+
* Configure the post type priority declaration parameter.
29+
*
30+
* @param PostTypeAttributeInterface $postType The post type to configure
31+
*/
32+
protected function configure(PostTypeAttributeInterface $postType): void
33+
{
34+
$postType->attributeArgs['priority'] = $this->priority;
35+
}
36+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Pollora\Attributes\Taxonomy;
6+
7+
use Attribute;
8+
use Pollora\Taxonomy\Domain\Contracts\TaxonomyAttributeInterface;
9+
10+
/**
11+
* Attribute to set the priority a post type declaration.
12+
*
13+
* @Attribute
14+
*/
15+
#[Attribute(Attribute::TARGET_CLASS)]
16+
class Priority extends TaxonomyAttribute
17+
{
18+
/**
19+
* Constructor.
20+
*
21+
* @param init $priority The taxonomy priority declaration
22+
*/
23+
public function __construct(
24+
public readonly int $priority = 5
25+
) {
26+
}
27+
28+
/**
29+
* Configure the post type priority declaration parameter.
30+
*
31+
* @param TaxonomyAttributeInterface $taxonomy The post type to configure
32+
*/
33+
protected function configure(TaxonomyAttributeInterface $taxonomy): void
34+
{
35+
$taxonomy->attributeArgs['priority'] = $this->priority;
36+
}
37+
}

src/PostType/Application/Services/PostTypeService.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ public function __construct(
3131
* @param string|null $singular The singular label for the post type
3232
* @param string|null $plural The plural label for the post type
3333
* @param array<string, mixed> $args Additional arguments
34+
* @param int $priority Declaration priority
3435
* @return object The created post type instance
3536
*/
36-
public function create(string $slug, ?string $singular = null, ?string $plural = null, array $args = []): object
37+
public function create(string $slug, ?string $singular = null, ?string $plural = null, array $args = [], int $priority = 5): object
3738
{
38-
return $this->factory->make($slug, $singular, $plural, $args);
39+
return $this->factory->make($slug, $singular, $plural, $args, $priority);
3940
}
4041

4142
/**
@@ -45,12 +46,13 @@ public function create(string $slug, ?string $singular = null, ?string $plural =
4546
* @param string|null $singular The singular label for the post type
4647
* @param string|null $plural The plural label for the post type
4748
* @param array<string, mixed> $args Additional arguments
49+
* @param int $priority Declaration priority
4850
* @return object The registered post type instance
4951
*/
50-
public function register(string $slug, ?string $singular = null, ?string $plural = null, array $args = []): object
52+
public function register(string $slug, ?string $singular = null, ?string $plural = null, array $args = [], int $priority = 5): object
5153
{
5254
// The factory creates and registers the post type (pollora/entity handles register_post_type)
53-
return $this->factory->make($slug, $singular, $plural, $args);
55+
return $this->factory->make($slug, $singular, $plural, $args, $priority);
5456
}
5557

5658
/**

src/PostType/Domain/Contracts/PostTypeServiceInterface.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ interface PostTypeServiceInterface
2020
* @param string|null $singular The singular label for the post type
2121
* @param string|null $plural The plural label for the post type
2222
* @param array<string, mixed> $args Additional arguments
23+
* @param int $priority Declaration priority
2324
* @return object The created post type instance
2425
*/
25-
public function create(string $slug, ?string $singular = null, ?string $plural = null, array $args = []): object;
26+
public function create(string $slug, ?string $singular = null, ?string $plural = null, array $args = [], int $priority = 5): object;
2627

2728
/**
2829
* Register a post type with the system.
@@ -31,9 +32,10 @@ public function create(string $slug, ?string $singular = null, ?string $plural =
3132
* @param string|null $singular The singular label for the post type
3233
* @param string|null $plural The plural label for the post type
3334
* @param array<string, mixed> $args Additional arguments
35+
* @param int $priority Declaration priority
3436
* @return object The registered post type instance
3537
*/
36-
public function register(string $slug, ?string $singular = null, ?string $plural = null, array $args = []): object;
38+
public function register(string $slug, ?string $singular = null, ?string $plural = null, array $args = [], int $priority = 5): object;
3739

3840
/**
3941
* Check if a post type exists.

src/PostType/Infrastructure/Factories/PostTypeFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ class PostTypeFactory implements PostTypeFactoryInterface
3030
* @param string|null $singular The singular label for the post type
3131
* @param string|null $plural The plural label for the post type
3232
* @param array<string, mixed> $args Additional arguments
33+
* @param int $priority Declaration priority
3334
* @return object The created PostType instance
3435
*/
35-
public function make(string $slug, ?string $singular = null, ?string $plural = null, array $args = []): object
36+
public function make(string $slug, ?string $singular = null, ?string $plural = null, array $args = [], int $priority = 5): object
3637
{
3738
// Generate singular name if not provided
3839
if ($singular === null) {
@@ -47,6 +48,7 @@ public function make(string $slug, ?string $singular = null, ?string $plural = n
4748
// Create the Entity PostType instance directly (without auto-registration)
4849
$postType = new EntityPostType($slug, $singular, $plural);
4950
$postType->init();
51+
$postType->priority($priority);
5052

5153
// Apply additional arguments if provided
5254
if ($args !== []) {

src/PostType/Infrastructure/Services/PostTypeConfiguration.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,20 @@ public function mergeArgs(array $args): void
9494
*/
9595
public function getArgs(): array
9696
{
97-
return $this->attributeArgs;
97+
$args = $this->attributeArgs;
98+
unset($args["priority"]);
99+
100+
return $args;
101+
}
102+
103+
/**
104+
* Get the post type priority declaration
105+
*
106+
* @return int $priority Declaration priority
107+
*/
108+
public function getPriority(): int
109+
{
110+
return $this->attributeArgs['priority'] ?? 5;
98111
}
99112

100113
/**
@@ -106,7 +119,8 @@ public function toArray(): array
106119
'slug' => $this->slug,
107120
'singular' => $this->singular,
108121
'plural' => $this->plural,
109-
'args' => $this->attributeArgs,
122+
'args' => $this->getArgs(),
123+
'priority' => $this->getPriority(),
110124
];
111125
}
112126
}

src/PostType/Infrastructure/Services/PostTypeDiscovery.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ private function processPostType(string $className): void
147147
$config->getSlug(),
148148
$config->getName(),
149149
$config->getPluralName(),
150-
$config->getArgs()
150+
$config->getArgs(),
151+
$config->getPriority()
151152
);
152153

153154
} catch (\ReflectionException $e) {

src/Taxonomy/Application/Services/TaxonomyService.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ public function __construct(
3232
* @param string|null $singular The singular label for the taxonomy
3333
* @param string|null $plural The plural label for the taxonomy
3434
* @param array<string, mixed> $args Additional arguments
35+
* @param int $priority Declaration priority
3536
* @return object The created taxonomy instance
3637
*/
37-
public function create(string $slug, string|array $objectType, ?string $singular = null, ?string $plural = null, array $args = []): object
38+
public function create(string $slug, string|array $objectType, ?string $singular = null, ?string $plural = null, array $args = [], int $priority = 5): object
3839
{
39-
return $this->factory->make($slug, $objectType, $singular, $plural, $args);
40+
return $this->factory->make($slug, $objectType, $singular, $plural, $args, $priority);
4041
}
4142

4243
/**
@@ -47,12 +48,13 @@ public function create(string $slug, string|array $objectType, ?string $singular
4748
* @param string|null $singular The singular label for the taxonomy
4849
* @param string|null $plural The plural label for the taxonomy
4950
* @param array<string, mixed> $args Additional arguments
51+
* @param int $priority Declaration priority
5052
* @return object The registered taxonomy instance
5153
*/
52-
public function register(string $slug, string|array $objectType, ?string $singular = null, ?string $plural = null, array $args = []): object
54+
public function register(string $slug, string|array $objectType, ?string $singular = null, ?string $plural = null, array $args = [], int $priority = 5): object
5355
{
5456
// The factory creates and registers the taxonomy (pollora/entity handles register_taxonomy)
55-
return $this->factory->make($slug, $objectType, $singular, $plural, $args);
57+
return $this->factory->make($slug, $objectType, $singular, $plural, $args, $priority);
5658
}
5759

5860
/**

src/Taxonomy/Domain/Contracts/TaxonomyServiceInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ interface TaxonomyServiceInterface
2323
* @param array<string, mixed> $args Additional arguments
2424
* @return object The created post type instance
2525
*/
26-
public function create(string $slug, string|array $objectType, ?string $singular = null, ?string $plural = null, array $args = []): object;
26+
public function create(string $slug, string|array $objectType, ?string $singular = null, ?string $plural = null, array $args = [], int $priority = 5): object;
2727

2828
/**
2929
* Register a post type with the system.
@@ -34,7 +34,7 @@ public function create(string $slug, string|array $objectType, ?string $singular
3434
* @param array<string, mixed> $args Additional arguments
3535
* @return object The registered post type instance
3636
*/
37-
public function register(string $slug, string|array $objectType, ?string $singular = null, ?string $plural = null, array $args = []): object;
37+
public function register(string $slug, string|array $objectType, ?string $singular = null, ?string $plural = null, array $args = [], int $priority = 5): object;
3838

3939
/**
4040
* Check if a post type exists.

0 commit comments

Comments
 (0)