Skip to content

Commit 59fbffe

Browse files
committed
add task when new public post type is detected
1 parent 80c42c4 commit 59fbffe

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

classes/suggested-tasks/providers/class-set-valuable-post-types.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,58 @@ protected function get_url() {
6464
public function init() {
6565
\add_action( 'progress_planner_settings_form_options_stored', [ $this, 'remove_upgrade_option' ] );
6666
\add_action( 'wp_ajax_prpl_interactive_task_submit_set-valuable-post-types', [ $this, 'handle_interactive_task_specific_submit' ] );
67+
68+
// On late init hook we need to check if the public post types are changed.
69+
\add_action( 'init', [ $this, 'check_public_post_types' ], PHP_INT_MAX - 1 );
70+
}
71+
72+
/**
73+
* Check if the public post types are changed.
74+
*
75+
* @return void
76+
*/
77+
public function check_public_post_types() {
78+
$previosly_set_public_post_types = \array_unique( \get_option( 'progress_planner_public_post_types', [] ) );
79+
$public_post_types = \array_unique( \progress_planner()->get_settings()->get_public_post_types() );
80+
81+
// Sort the public post types.
82+
\sort( $previosly_set_public_post_types );
83+
\sort( $public_post_types );
84+
85+
// Compare the previously set public post types with the current public post types.
86+
if ( $previosly_set_public_post_types === $public_post_types ) {
87+
return;
88+
}
89+
90+
// Update the previously set public post types.
91+
\update_option( 'progress_planner_public_post_types', $public_post_types );
92+
93+
// Exit if post type was removed, or it is not public anymore, since the user will not to able to make different selection.
94+
if ( count( $public_post_types ) < count( $previosly_set_public_post_types ) ) {
95+
return;
96+
}
97+
98+
// If we're here that means that there is new public post type.
99+
100+
// Check if the task exists, if it does and it is published do nothing.
101+
$task = \progress_planner()->get_suggested_tasks_db()->get_tasks_by( [ 'provider_id' => static::PROVIDER_ID ] );
102+
if ( ! empty( $task ) && 'publish' === $task[0]->post_status ) {
103+
return;
104+
}
105+
106+
// If it is trashed, change it's status to publish.
107+
if ( ! empty( $task ) && 'trash' === $task[0]->post_status ) {
108+
\wp_update_post(
109+
[
110+
'ID' => $task[0]->ID,
111+
'post_status' => 'publish',
112+
]
113+
);
114+
return;
115+
}
116+
117+
// If we're here then we need to add it.
118+
\progress_planner()->get_suggested_tasks_db()->add( $this->modify_injection_task_data( $this->get_task_details() ) );
67119
}
68120

69121
/**

0 commit comments

Comments
 (0)