Skip to content

Autoload data collectors #419

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

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -30,68 +17,89 @@ 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' ] );

// 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();
}
}

Expand All @@ -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 );
Expand Down
Loading