diff --git a/classes/suggested-tasks/data-collector/class-data-collector-manager.php b/classes/suggested-tasks/data-collector/class-data-collector-manager.php index 576c3afa8..03872e9c3 100644 --- a/classes/suggested-tasks/data-collector/class-data-collector-manager.php +++ b/classes/suggested-tasks/data-collector/class-data-collector-manager.php @@ -7,21 +7,8 @@ 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 { @@ -30,68 +17,89 @@ class Data_Collector_Manager { * * @var array */ - protected $data_collectors = []; + protected array $data_collectors = []; /** - * Constructor. + * Files to exclude from automatic loading. * - * @return void + * @var array + */ + 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' ] ); - - // 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. + $this->load_data_collectors(); + $this->initialize_collectors(); + + // 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 */ - public function add_plugin_integration() { + 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(); - // Yoast SEO integration. - if ( function_exists( 'YoastSEO' ) ) { - $this->data_collectors[] = new Yoast_Orphaned_Content(); + if ( ! $collector instanceof Base_Data_Collector ) { + continue; + } + + $this->data_collectors[] = $collector; } } /** - * 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 */ - public function init() { - - /** - * Filter the data collectors. - * - * @param array $data_collectors The data collectors. - */ - $this->data_collectors = \apply_filters( 'progress_planner_data_collectors', $this->data_collectors ); - - // Initialize (add hooks) the data collectors. - foreach ( $this->data_collectors as $data_collector ) { - $data_collector->init(); + protected function initialize_collectors() { + foreach ( $this->data_collectors as $collector ) { + $collector->init(); } } @@ -101,15 +109,14 @@ public function init() { * @return void */ public function update_data_collectors_cache() { - $update_recently_performed = \progress_planner()->get_utils__cache()->get( 'update_data_collectors_cache' ); if ( $update_recently_performed ) { 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 );