Skip to content
Open
Show file tree
Hide file tree
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
53 changes: 45 additions & 8 deletions classes/activities/class-content-helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion classes/admin/class-page-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down
55 changes: 0 additions & 55 deletions classes/class-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
);
Expand Down
2 changes: 1 addition & 1 deletion classes/suggested-tasks/providers/class-content-review.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ] );

Expand Down
4 changes: 2 additions & 2 deletions views/page-settings/post-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) {
Expand Down
Loading