Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,10 @@

namespace Progress_Planner\Suggested_Tasks\Data_Collector;

use Progress_Planner\Suggested_Tasks\Data_Collector\Hello_World;
use Progress_Planner\Suggested_Tasks\Data_Collector\Sample_Page;
use Progress_Planner\Suggested_Tasks\Data_Collector\Inactive_Plugins;
use Progress_Planner\Suggested_Tasks\Data_Collector\Uncategorized_Category;
use Progress_Planner\Suggested_Tasks\Data_Collector\Post_Author;
use Progress_Planner\Suggested_Tasks\Data_Collector\Last_Published_Post;
use Progress_Planner\Suggested_Tasks\Data_Collector\Archive_Format;
use Progress_Planner\Suggested_Tasks\Data_Collector\Terms_Without_Posts;
use Progress_Planner\Suggested_Tasks\Data_Collector\Terms_Without_Description;
use Progress_Planner\Suggested_Tasks\Data_Collector\Post_Tag_Count;
use Progress_Planner\Suggested_Tasks\Data_Collector\Published_Post_Count;
use Progress_Planner\Suggested_Tasks\Data_Collector\Yoast_Orphaned_Content;

/**
* Base data collector.
* Manages the collection and initialization of data collectors.
*/
class Data_Collector_Manager {

Expand All @@ -30,40 +19,62 @@ class Data_Collector_Manager {
*
* @var array<Base_Data_Collector>
*/
protected $data_collectors = [];
protected array $data_collectors = [];

/**
* Constructor.
* Files to exclude from automatic loading.
*
* @return void
* @var array<string>
*/
protected const EXCLUDED_FILES = [
'class-data-collector-manager.php',
'class-base-data-collector.php',
];

/**
* Constructor.
*/
public function __construct() {
$this->data_collectors = [
new Hello_World(),
new Sample_Page(),
new Inactive_Plugins(),
new Uncategorized_Category(),
new Post_Author(),
new Last_Published_Post(),
new Archive_Format(),
new Terms_Without_Posts(),
new Terms_Without_Description(),
new Post_Tag_Count(),
new Published_Post_Count(),
];

// Add the plugin integration.
\add_action( 'plugins_loaded', [ $this, 'add_plugin_integration' ] );
$this->load_data_collectors();
$this->add_plugin_integration();

// At all all CPTs and taxonomies are initialized, init the data collectors.
\add_action( 'init', [ $this, 'init' ], 99 ); // Wait for the post types to be initialized.

// Add the update action.
// Update the cache once per day.
\add_action( 'admin_init', [ $this, 'update_data_collectors_cache' ] );
}

/**
* Add the data collectors for the plugins we integrate with.
* Load all data collectors from the directory.
*
* @return void
*/
protected function load_data_collectors() {
$files = \glob( __DIR__ . '/*.php' );

if ( ! $files ) {
return;
}

foreach ( $files as $file ) {
if ( $this->should_skip_file( $file ) ) {
continue;
}

$class_name = $this->get_class_name_from_file( $file );
$collector = new $class_name();

if ( ! $collector instanceof Base_Data_Collector ) {
continue;
}

$this->data_collectors[] = $collector;
}
}

/**
* Add plugin integration.
*
* @return void
*/
Expand All @@ -75,7 +86,31 @@ public function add_plugin_integration() {
}

/**
* Initialize the task providers.
* Check if a file should be skipped during loading.
*
* @param string $file_path The full path to the file.
* @return bool
*/
protected function should_skip_file( string $file_path ) {
return in_array( \basename( $file_path ), self::EXCLUDED_FILES, true );
}

/**
* Convert a file path to its corresponding class name.
*
* @param string $file_path The full path to the file.
* @return string The fully qualified class name.
*/
protected function get_class_name_from_file( string $file_path ) {
$class_name = \basename( $file_path );
$class_name = \str_replace( [ 'class-', '.php' ], '', $class_name );
$class_name = implode( '_', array_map( 'ucfirst', explode( '-', $class_name ) ) );

return '\\Progress_Planner\\Suggested_Tasks\\Data_Collector\\' . $class_name;
}

/**
* Initialize all loaded data collectors.
*
* @return void
*/
Expand Down Expand Up @@ -103,8 +138,8 @@ public function update_data_collectors_cache() {
return;
}

foreach ( $this->data_collectors as $data_collector ) {
$data_collector->update_cache();
foreach ( $this->data_collectors as $collector ) {
$collector->update_cache();
}

\progress_planner()->get_utils__cache()->set( 'update_data_collectors_cache', true, DAY_IN_SECONDS );
Expand Down
Loading