diff --git a/classes/activities/class-content-helpers.php b/classes/activities/class-content-helpers.php index 239db57ef..f96492f81 100644 --- a/classes/activities/class-content-helpers.php +++ b/classes/activities/class-content-helpers.php @@ -21,17 +21,54 @@ class Content_Helpers { */ public function get_post_types_names() { static $include_post_types; + + if ( ! doing_action( 'init' ) && ! did_action( 'init' ) ) { + \trigger_error( // phpcs:ignore + sprintf( + '%1$s was called too early. Wait for init hook to be called to have access to the post types.', + \esc_html( get_class() . '::' . __FUNCTION__ ) + ), + E_USER_WARNING + ); + } + + // Since we're working with CPTs, dont cache until init. if ( isset( $include_post_types ) && ! empty( $include_post_types ) ) { return $include_post_types; } - $default = [ 'post', 'page' ]; - $include_post_types = \array_filter( - \progress_planner()->get_settings()->get( [ 'include_post_types' ], $default ), - function ( $post_type ) { - return $post_type && \post_type_exists( $post_type ) && \is_post_type_viewable( $post_type ); - } - ); - return empty( $include_post_types ) ? $default : \array_values( $include_post_types ); + + $public_post_types = $this->get_public_post_types(); + + // Post or pages can be deregistered. + $default = array_intersect( [ 'post', 'page' ], $public_post_types ); + + // Filter the saved post types. + $include_post_types = array_intersect( progress_planner()->get_settings()->get( [ 'include_post_types' ], $default ), $public_post_types ); + + $include_post_types = ! empty( $include_post_types ) ? $include_post_types : $default; + + return $include_post_types; + } + + /** + * Get the public post types. + * + * @return string[] + */ + public function get_public_post_types() { + $public_post_types = \array_filter( \get_post_types( [ 'public' => true ] ), 'is_post_type_viewable' ); + + unset( $public_post_types['attachment'] ); + unset( $public_post_types['elementor_library'] ); // Elementor templates are not a post type we want to track. + + /** + * Filter the public post types. + * + * @param string[] $public_post_types The public post types. + * + * @return string[] + */ + return \apply_filters( 'progress_planner_public_post_types', $public_post_types ); } /** diff --git a/classes/admin/class-page-settings.php b/classes/admin/class-page-settings.php index 07b6d69a2..69fcc5fab 100644 --- a/classes/admin/class-page-settings.php +++ b/classes/admin/class-page-settings.php @@ -209,7 +209,7 @@ public function save_post_types() { $include_post_types = isset( $_POST['prpl-post-types-include'] ) ? array_map( 'sanitize_text_field', \wp_unslash( $_POST['prpl-post-types-include'] ) ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized // If no post types are selected, use the default post types (post and page can be deregistered). - : array_intersect( [ 'post', 'page' ], \progress_planner()->get_settings()->get_public_post_types() ); + : array_intersect( [ 'post', 'page' ], \progress_planner()->get_activities__content_helpers()->get_public_post_types() ); \progress_planner()->get_settings()->set( 'include_post_types', $include_post_types ); } diff --git a/classes/class-settings.php b/classes/class-settings.php index 963c8cdfe..7b2bc930b 100644 --- a/classes/class-settings.php +++ b/classes/class-settings.php @@ -110,59 +110,4 @@ public function delete_all() { self::$settings = []; return $this->save_settings(); } - - /** - * Get an array of post-types names for the stats. - * - * @return string[] - */ - public function get_post_types_names() { - static $include_post_types; - - if ( ! doing_action( 'init' ) && ! did_action( 'init' ) ) { - \trigger_error( // phpcs:ignore - sprintf( - '%1$s was called too early. Wait for init hook to be called to have access to the post types.', - \esc_html( get_class() . '::' . __FUNCTION__ ) - ), - E_USER_WARNING - ); - } - - // Since we're working with CPTs, dont cache until init. - if ( isset( $include_post_types ) && ! empty( $include_post_types ) ) { - return $include_post_types; - } - - $public_post_types = $this->get_public_post_types(); - - // Post or pages can be deregistered. - $default = array_intersect( [ 'post', 'page' ], $public_post_types ); - - // Filter the saved post types. - $include_post_types = array_intersect( $this->get( [ 'include_post_types' ], $default ), $public_post_types ); - - return empty( $include_post_types ) ? $default : \array_values( $include_post_types ); - } - - /** - * Get the public post types. - * - * @return string[] - */ - public function get_public_post_types() { - $public_post_types = \array_filter( \get_post_types( [ 'public' => true ] ), 'is_post_type_viewable' ); - - unset( $public_post_types['attachment'] ); - unset( $public_post_types['elementor_library'] ); // Elementor templates are not a post type we want to track. - - /** - * Filter the public post types. - * - * @param string[] $public_post_types The public post types. - * - * @return string[] - */ - return \apply_filters( 'progress_planner_public_post_types', $public_post_types ); - } } diff --git a/classes/suggested-tasks/data-collector/class-last-published-post.php b/classes/suggested-tasks/data-collector/class-last-published-post.php index f56679800..d8ab39e12 100644 --- a/classes/suggested-tasks/data-collector/class-last-published-post.php +++ b/classes/suggested-tasks/data-collector/class-last-published-post.php @@ -44,7 +44,7 @@ public function init() { * @return void */ public function set_include_post_types() { - $this->include_post_types = \progress_planner()->get_settings()->get_post_types_names(); + $this->include_post_types = \progress_planner()->get_activities__content_helpers()->get_post_types_names(); } /** diff --git a/classes/suggested-tasks/data-collector/class-yoast-orphaned-content.php b/classes/suggested-tasks/data-collector/class-yoast-orphaned-content.php index 0e925fb90..877cd3a46 100644 --- a/classes/suggested-tasks/data-collector/class-yoast-orphaned-content.php +++ b/classes/suggested-tasks/data-collector/class-yoast-orphaned-content.php @@ -65,13 +65,13 @@ protected function calculate_data() { $where_clause = "1=1 AND p.post_status = 'publish'"; // Get the public post types. - $public_post_types = \progress_planner()->get_settings()->get_public_post_types(); + $public_post_types = \progress_planner()->get_activities__content_helpers()->get_public_post_types(); $post_types_in = ''; if ( ! empty( $public_post_types ) ) { $post_types_in = array_map( function ( $type ) { - return (string) esc_sql( $type ); + return (string) esc_sql( $type ); // @phpstan-ignore-line }, array_values( $public_post_types ) ); diff --git a/classes/suggested-tasks/providers/class-content-review.php b/classes/suggested-tasks/providers/class-content-review.php index 3d30f51a1..4f70333a1 100644 --- a/classes/suggested-tasks/providers/class-content-review.php +++ b/classes/suggested-tasks/providers/class-content-review.php @@ -100,7 +100,7 @@ class Content_Review extends Tasks { * @return void */ public function init() { - $this->include_post_types = \progress_planner()->get_settings()->get_post_types_names(); // Wait for the post types to be initialized. + $this->include_post_types = \progress_planner()->get_activities__content_helpers()->get_post_types_names(); // Wait for the post types to be initialized. \add_filter( 'progress_planner_update_posts_tasks_args', [ $this, 'filter_update_posts_args' ] ); diff --git a/views/page-settings/post-types.php b/views/page-settings/post-types.php index 34737db34..64e12a9bf 100644 --- a/views/page-settings/post-types.php +++ b/views/page-settings/post-types.php @@ -10,8 +10,8 @@ exit; } -$prpl_saved_settings = \progress_planner()->get_settings()->get_post_types_names(); -$prpl_post_types = \progress_planner()->get_settings()->get_public_post_types(); +$prpl_saved_settings = \progress_planner()->get_activities__content_helpers()->get_post_types_names(); +$prpl_post_types = \progress_planner()->get_activities__content_helpers()->get_public_post_types(); // Early exit if there are no public post types. if ( empty( $prpl_post_types ) ) {