Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear recorded post ids feature #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/alley/wp/class-registry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Registry class file.
*
* @package wp-type-extensions
*/

namespace Alley\WP;

/**
* Registry pattern class.
*/
final class Registry {
/**
* Store all instances of every class.
*
* @var object[][]
*/
private static array $collection = [];

/**
* Add a new instance to the collection.
*
* @param object $instance The class instance.
* @return void
*/
public static function add( object $instance ): void {
if ( class_exists( $instance::class ) ) {
self::$collection[ $instance::class ] [] = $instance;
}
}

/**
* Retrieve all instances of a class.
*
* @param string $class_name The class name.
* @return object[]
*/
public static function get( string $class_name ): array {
if ( ! self::contains( $class_name ) ) {
return [];
}

return self::$collection[ $class_name ];
}

/**
* Check if a class is registered in the collection.
*
* @param string $class_name The class name.
* @return bool
*/
public static function contains( string $class_name ): bool {
if ( isset( self::$collection[ $class_name ] ) ) {
return true;
}

return false;
}
}
14 changes: 13 additions & 1 deletion src/alley/wp/post-ids/class-used-post-ids.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Alley\WP\Post_IDs;

use Alley\WP\Registry;
use Alley\WP\Types\Post_IDs;

/**
Expand All @@ -27,7 +28,9 @@ final class Used_Post_IDs implements Post_IDs {
*/
public function __construct(
private readonly Post_IDs $seed = new Empty_Post_IDs()
) {}
) {
Registry::add( $this );
}

/**
* Post IDs.
Expand All @@ -54,4 +57,13 @@ public function record( int|array $post_ids ): void {
}
}
}

/**
* Clear recorded post IDs.
*
* @return void
*/
public function clear_recorded_post_ids(): void {
$this->ids = [];
}
}