Skip to content

Commit

Permalink
Merge pull request #507 from 10up/upkeep/500
Browse files Browse the repository at this point in the history
replace get_post_statuses() with get_post_stati()
  • Loading branch information
dkotter authored Jun 27, 2023
2 parents a3fd3ad + 5c83abc commit adef168
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
58 changes: 57 additions & 1 deletion includes/Classifai/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ function get_post_types_for_language_settings() {
* @return array
*/
function get_post_statuses_for_language_settings() {
$post_statuses = get_post_statuses();
$post_statuses = get_all_post_statuses();

/**
* Filter post statuses shown in language processing settings.
Expand Down Expand Up @@ -693,3 +693,59 @@ function find_provider_class( array $provider_classes = [], string $service_name

return $provider;
}

/**
* Get core and custom post statuses.
*
* @return array
*/
function get_all_post_statuses() {
$all_statuses = wp_list_pluck(
get_post_stati(
[],
'objects'
),
'label'
);

/*
* We unset the following because we want to limit the post
* statuses to the ones returned by `get_post_statuses()` and
* any custom post statuses registered using `register_post_status()`
*/
unset(
$all_statuses['future'],
$all_statuses['trash'],
$all_statuses['auto-draft'],
$all_statuses['inherit'],
$all_statuses['request-pending'],
$all_statuses['request-confirmed'],
$all_statuses['request-failed'],
$all_statuses['request-completed']
);

/*
* There is a minor difference in the label for 'pending' status between
* `get_post_statuses()` and `get_post_stati()`.
*
* `get_post_stati()` has the label as `Pending` whereas
* `get_post_statuses()` has the label as `Pending Review`.
*
* So we normalise it here.
*/
if ( isset( $all_statuses['pending'] ) ) {
$all_statuses['pending'] = esc_html__( 'Pending Review', 'classifai' );
}

/**
* Hook to filter post statuses.
*
* @since 2.2.2
* @hook classifai_all_post_statuses
*
* @param {array} $all_statuses Array of post statuses.
*
* @return {array} Array of post statuses.
*/
return apply_filters( 'classifai_all_post_statuses', $all_statuses );
}
4 changes: 3 additions & 1 deletion includes/Classifai/Providers/OpenAI/OpenAI.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Classifai\Providers\OpenAI\APIRequest;
use WP_Error;

use function Classifai\get_all_post_statuses;

trait OpenAI {

/**
Expand Down Expand Up @@ -188,7 +190,7 @@ public function get_post_types_for_settings() {
* @return array
*/
public function get_post_statuses_for_settings() {
$post_statuses = get_post_statuses();
$post_statuses = get_all_post_statuses();

/**
* Filter post statuses shown in settings.
Expand Down
31 changes: 31 additions & 0 deletions tests/Classifai/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@
* @group helpers
*/
class HelpersTest extends \WP_UnitTestCase {

/**
* Set up method.
*/
public function set_up() {
register_post_status( 'unread', array(
'label' => _x( 'Unread', 'post' ),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Unread <span class="count">(%s)</span>', 'Unread <span class="count">(%s)</span>' ),
) );

parent::set_up();
}

/**
* Tear down method.
*/
Expand Down Expand Up @@ -245,4 +262,18 @@ public function test_clean_input() {
$sanitized_int = clean_input( 'classify_test_int', true, 'absint' );
$this->assertEquals( $sanitized_int, 2 );
}

/**
* Tests for the get_post_statuses method.
*/
public function test_get_post_statuses() {
$all_statuses = get_all_post_statuses();
$core_statuses = get_post_statuses();

// This tells that $all_status contains all statuses that
// are present in $core_statuses.
$statuses_diff = array_diff( $core_statuses, $all_statuses );
$this->assertEquals( 0, count( $statuses_diff ) );
$this->assertArrayHasKey( 'unread', $all_statuses );
}
}

0 comments on commit adef168

Please sign in to comment.