Skip to content

Commit 71aa8ef

Browse files
authored
Merge pull request #494 from ProgressPlanner/filip/wp-cli
Add basic WP CLI commands
2 parents 53a5aa9 + ef1103f commit 71aa8ef

16 files changed

Lines changed: 679 additions & 188 deletions

classes/activities/class-suggested-task.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,8 @@ public function get_points( $date ) {
6666
$points = 1;
6767
$tasks = \progress_planner()->get_suggested_tasks_db()->get_tasks_by( [ 'task_id' => $this->data_id ] );
6868

69-
if ( ! empty( $tasks ) && $tasks[0]->get_provider_id() ) {
70-
$task_provider = \progress_planner()->get_suggested_tasks()->get_tasks_manager()->get_task_provider( $tasks[0]->get_provider_id() );
71-
72-
if ( $task_provider ) {
73-
$points = $task_provider->get_points();
74-
}
69+
if ( ! empty( $tasks ) ) {
70+
$points = $tasks[0]->points;
7571
}
7672
$this->points[ $date_ymd ] = $points;
7773

classes/class-base.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ public function init() {
146146
* Redirect on login.
147147
*/
148148
\add_action( 'wp_login', [ $this, 'redirect_on_login' ], 10, 2 );
149+
150+
if ( \defined( 'WP_CLI' ) && \WP_CLI ) {
151+
$this->get_wp_cli__get_stats_command();
152+
$this->get_wp_cli__task_command();
153+
}
149154
}
150155

151156
/**

classes/class-suggested-tasks-db.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ public function get_post( $id ) {
307307
*
308308
* @param array $params The parameters to filter by ([ 'provider' => 'provider_id' ] etc).
309309
*
310-
* @return array
310+
* @return \Progress_Planner\Suggested_Tasks\Task[]
311311
*/
312312
public function get_tasks_by( $params ) {
313313
$args = [];
@@ -353,7 +353,7 @@ public function get_tasks_by( $params ) {
353353
*
354354
* @param array $args The arguments.
355355
*
356-
* @return array
356+
* @return \Progress_Planner\Suggested_Tasks\Task[]
357357
*/
358358
public function get( $args = [] ) {
359359
$args = \wp_parse_args(

classes/class-suggested-tasks.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public function __construct() {
6666

6767
// Filter the REST API response.
6868
\add_filter( 'rest_prepare_prpl_recommendations', [ $this, 'rest_prepare_recommendation' ], 10, 2 );
69+
70+
\add_filter( 'wp_trash_post_days', [ $this, 'change_trashed_posts_lifetime' ], 10, 2 );
6971
}
7072

7173
/**
@@ -154,7 +156,7 @@ public function on_automatic_updates_complete(): void {
154156
\progress_planner()->get_suggested_tasks_db()->update_recommendation( $pending_tasks[0]->ID, [ 'post_status' => 'trash' ] );
155157

156158
// Insert an activity.
157-
$this->insert_activity( $pending_tasks[0]->ID );
159+
$this->insert_activity( $pending_tasks[0]->task_id );
158160
}
159161

160162
/**
@@ -340,6 +342,18 @@ public function register_post_type() {
340342
}
341343
}
342344

345+
/**
346+
* Custom trash lifetime by post type.
347+
*
348+
* @param int $days The number of days to keep in trash.
349+
* @param \WP_Post $post The post.
350+
*
351+
* @return int
352+
*/
353+
public function change_trashed_posts_lifetime( $days, $post ) {
354+
return 'prpl_recommendations' === $post->post_type ? 60 : $days;
355+
}
356+
343357
/**
344358
* Register a custom taxonomies for suggested tasks.
345359
*

classes/class-todo.php

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -25,52 +25,6 @@ public function __construct() {
2525
\add_action( 'rest_after_insert_prpl_recommendations', [ $this, 'handle_creating_user_task' ], 10, 3 );
2626
}
2727

28-
/**
29-
* Get the points for a new task.
30-
*
31-
* @return int
32-
*/
33-
public function calc_points_for_new_task() {
34-
$items = \progress_planner()->get_suggested_tasks_db()->get_tasks_by( [ 'provider_id' => 'user' ] );
35-
36-
// If this is the first user task ever, return 1.
37-
if ( ! count( $items ) ) {
38-
return 1;
39-
}
40-
41-
// Get the task IDs from the todos.
42-
$task_ids = array_column( $items, 'task_id' );
43-
44-
// Get the completed activities for this week that are in the todos.
45-
$activities = array_filter(
46-
\progress_planner()->get_activities__query()->query_activities(
47-
[
48-
'start_date' => new \DateTime( 'monday this week' ),
49-
'end_date' => new \DateTime( 'sunday this week' ),
50-
'category' => 'suggested_task',
51-
'type' => 'completed',
52-
]
53-
),
54-
function ( $activity ) use ( $task_ids ) {
55-
return in_array( $activity->data_id, $task_ids, true );
56-
}
57-
);
58-
59-
// If there are completed todos this week, we already have set the golden task and it was completed.
60-
if ( count( $activities ) ) {
61-
return 0;
62-
}
63-
64-
// Check if there are already published user tasks with a points value other than 0.
65-
foreach ( $items as $item ) {
66-
if ( 'publish' === $item['post_status'] && isset( $item['points'] ) && $item['points'] !== 0 ) {
67-
return 0;
68-
}
69-
}
70-
71-
return 1;
72-
}
73-
7428
/**
7529
* Maybe change the points of the first item in the todo list on Monday.
7630
*

classes/rest/class-stats.php

Lines changed: 3 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -56,137 +56,12 @@ public function register_rest_endpoint() {
5656
*
5757
* This method handles a REST request and returns a REST response.
5858
*
59-
* @param \WP_REST_Request $request The REST request object.
60-
*
6159
* @return \WP_REST_Response The REST response object containing the stats.
6260
*/
63-
public function get_stats( \WP_REST_Request $request ) {
64-
$data = $request->get_json_params();
65-
66-
$data = [];
67-
68-
// Get the number of pending updates.
69-
$data['pending_updates'] = \wp_get_update_data()['counts']['total'];
70-
71-
// Get number of content from any public post-type, published in the past week.
72-
$data['weekly_posts'] = count(
73-
\get_posts(
74-
[
75-
'post_status' => 'publish',
76-
'post_type' => 'post',
77-
'date_query' => [ [ 'after' => '1 week ago' ] ],
78-
'posts_per_page' => 10,
79-
]
80-
)
81-
);
82-
83-
// Get the number of activities in the past week.
84-
$data['activities'] = count(
85-
\progress_planner()->get_activities__query()->query_activities(
86-
[
87-
'start_date' => new \DateTime( '-7 days' ),
88-
]
89-
)
90-
);
91-
92-
// Get the website activity score.
93-
$activity_score = new Activity_Scores();
94-
$data['website_activity'] = [
95-
'score' => $activity_score->get_score(),
96-
'checklist' => $activity_score->get_checklist_results(),
97-
];
98-
99-
// Get the badges.
100-
$badges = array_merge(
101-
\progress_planner()->get_badges()->get_badges( 'content' ),
102-
\progress_planner()->get_badges()->get_badges( 'maintenance' ),
103-
\progress_planner()->get_badges()->get_badges( 'monthly_flat' )
104-
);
105-
106-
$data['badges'] = [];
107-
foreach ( $badges as $badge ) {
108-
$data['badges'][ $badge->get_id() ] = array_merge(
109-
[
110-
'id' => $badge->get_id(),
111-
'name' => $badge->get_name(),
112-
],
113-
$badge->progress_callback()
114-
);
115-
}
116-
117-
$data['latest_badge'] = \progress_planner()->get_badges()->get_latest_completed_badge();
118-
119-
$scores = \progress_planner()->get_ui__chart()->get_chart_data(
120-
[
121-
'items_callback' => function ( $start_date, $end_date ) {
122-
return \progress_planner()->get_activities__query()->query_activities(
123-
[
124-
'start_date' => $start_date,
125-
'end_date' => $end_date,
126-
]
127-
);
128-
},
129-
'dates_params' => [
130-
'start_date' => \DateTime::createFromFormat( 'Y-m-d', \gmdate( 'Y-m-01' ) )->modify( '-6 months' ),
131-
'end_date' => new \DateTime(),
132-
'frequency' => 'monthly',
133-
'format' => 'M',
134-
],
135-
'count_callback' => function ( $activities, $date ) {
136-
$score = 0;
137-
foreach ( $activities as $activity ) {
138-
$score += $activity->get_points( $date );
139-
}
140-
return $score * 100 / Base::SCORE_TARGET;
141-
},
142-
'normalized' => true,
143-
'max' => 100,
144-
]
145-
);
146-
147-
$data['scores'] = [];
148-
foreach ( $scores as $item ) {
149-
$data['scores'][] = [
150-
'label' => $item['label'],
151-
'value' => $item['score'],
152-
];
153-
}
154-
155-
// The website URL.
156-
$data['website'] = \home_url();
157-
158-
// Timezone offset.
159-
$data['timezone_offset'] = \wp_timezone()->getOffset( new \DateTime( 'midnight' ) ) / 3600;
160-
$ravis_recommendations = \progress_planner()->get_suggested_tasks_db()->get_tasks_by( [ 'post_status' => 'publish' ] );
161-
$data['recommendations'] = [];
162-
foreach ( $ravis_recommendations as $recommendation ) {
163-
$r = [
164-
'id' => $recommendation->task_id,
165-
'title' => $recommendation->post_title,
166-
'url' => $recommendation->url,
167-
'provider_id' => $recommendation->get_provider_id(),
168-
];
169-
170-
if ( 'user' === $recommendation->get_provider_id() ) {
171-
$r['points'] = (int) $recommendation->points;
172-
}
173-
$data['recommendations'][] = $r;
174-
}
175-
176-
$data['plugin_url'] = \esc_url( \get_admin_url( null, 'admin.php?page=progress-planner' ) );
177-
178-
$active_plugins = \get_option( 'active_plugins' );
179-
$data['plugins'] = [];
180-
foreach ( $active_plugins as $plugin ) {
181-
$plugin_data = \get_plugin_data( \WP_PLUGIN_DIR . '/' . $plugin );
182-
$data['plugins'][] = [
183-
'plugin' => $plugin,
184-
'name' => $plugin_data['Name'] ?? 'N/A', // @phpstan-ignore-line nullCoalesce.offset
185-
'version' => $plugin_data['Version'] ?? 'N/A', // @phpstan-ignore-line nullCoalesce.offset
186-
];
187-
}
61+
public function get_stats() {
62+
$system_status = new \Progress_Planner\Utils\System_Status();
18863

189-
return new \WP_REST_Response( $data );
64+
return new \WP_REST_Response( $system_status->get_system_status() );
19065
}
19166

19267
/**

classes/suggested-tasks/class-task.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* @property int|null $target_term_id The target term ID for the task
3030
* @property string|null $target_taxonomy The target taxonomy for the task
3131
* @property string|null $target_term_name The target term name for the task
32+
* @property string|null $date The task date in YW format (year-week)
3233
*/
3334
class Task {
3435
/**

classes/suggested-tasks/class-tasks-manager.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Progress_Planner\Suggested_Tasks\Providers\Fewer_Tags;
3131
use Progress_Planner\Suggested_Tasks\Providers\Remove_Terms_Without_Posts;
3232
use Progress_Planner\Suggested_Tasks\Providers\Update_Term_Description;
33+
use Progress_Planner\Suggested_Tasks\Providers\Collaborator;
3334

3435
/**
3536
* Tasks_Manager class.
@@ -70,6 +71,7 @@ public function __construct() {
7071
new Remove_Terms_Without_Posts(),
7172
new Fewer_Tags(),
7273
new Update_Term_Description(),
74+
new Collaborator(),
7375
];
7476

7577
// Add the plugin integration.
@@ -208,7 +210,7 @@ public function inject_tasks() {
208210
/**
209211
* Evaluate tasks stored in the option.
210212
*
211-
* @return array<\Progress_Planner\Suggested_Tasks\Task>
213+
* @return \Progress_Planner\Suggested_Tasks\Task[]
212214
*/
213215
public function evaluate_tasks(): array {
214216
$tasks = (array) \progress_planner()->get_suggested_tasks_db()->get_tasks_by( [ 'post_status' => 'publish' ] );

0 commit comments

Comments
 (0)