-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass-data-collector-manager.php
More file actions
149 lines (126 loc) · 3.56 KB
/
class-data-collector-manager.php
File metadata and controls
149 lines (126 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/**
* Data collector manager.
*
* @package Progress_Planner
*/
namespace Progress_Planner\Suggested_Tasks\Data_Collector;
use Progress_Planner\Suggested_Tasks\Data_Collector\Yoast_Orphaned_Content;
use Progress_Planner\Suggested_Tasks\Data_Collector\Unpublished_Content;
use Progress_Planner\Suggested_Tasks\Data_Collector\SEO_Plugin;
/**
* Manages the collection and initialization of data collectors.
*/
class Data_Collector_Manager {
/**
* The data collectors.
*
* @var array<Base_Data_Collector>
*/
protected array $data_collectors = [];
/**
* Files to exclude from automatic loading.
*
* @var array<string>
*/
protected const EXCLUDED_FILES = [
'class-data-collector-manager.php',
'class-base-data-collector.php',
];
/**
* Constructor.
*/
public function __construct() {
$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.
// Update the cache once per day.
\add_action( 'admin_init', [ $this, 'update_data_collectors_cache' ] );
}
/**
* 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
*/
public function add_plugin_integration() {
// Yoast SEO integration.
if ( \function_exists( 'YoastSEO' ) ) {
$this->data_collectors[] = new Yoast_Orphaned_Content();
}
}
/**
* 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();
}
}
/**
* Update the data collectors cache once per day.
*
* @return void
*/
public function update_data_collectors_cache() {
if ( \progress_planner()->get_utils__cache()->get( 'update_data_collectors_cache' ) ) {
return;
}
foreach ( $this->data_collectors as $collector ) {
$collector->update_cache();
}
\progress_planner()->get_utils__cache()->set( 'update_data_collectors_cache', true, DAY_IN_SECONDS );
}
}