Skip to content

Commit

Permalink
Merge pull request #310 from 10up/feature/309-add-post-status-filter
Browse files Browse the repository at this point in the history
Added function to retrieve filterable post statuses for classification.
  • Loading branch information
jeffpaul authored Sep 24, 2021
2 parents c745744 + 9ad2911 commit 8dc621f
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 5 deletions.
26 changes: 21 additions & 5 deletions includes/Classifai/Admin/SavePostHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,28 @@ public function did_save_post( $post_id ) {
return;
}

$supported = \Classifai\get_supported_post_types();
$post_type = get_post_type( $post_id );
$post_status = get_post_status( $post_id );
$supported = \Classifai\get_supported_post_types();
$post_type = get_post_type( $post_id );
$post_status = get_post_status( $post_id );
$post_statuses = \Classifai\get_supported_post_statuses();

// Only process published, supported items and only if features are enabled
if ( 'publish' === $post_status && in_array( $post_type, $supported, true ) && \Classifai\language_processing_features_enabled() ) {

/**
* Filter post statuses for post type or ID.
*
* @since 1.0.0
* @hook classifai_post_statuses_for_post_type_or_id
*
* @param {array} $post_statuses Array of post statuses to be classified with language processing.
* @param {string} $post_type The post type.
* @param {int} $post_id The post ID.
*
* @return {array} Array of post statuses.
*/
$post_statuses = apply_filters( 'classifai_post_statuses_for_post_type_or_id', $post_statuses, $post_type, $post_id );

// Process posts in allowed post statuses, supported items and only if features are enabled
if ( in_array( $post_status, $post_statuses, true ) && in_array( $post_type, $supported, true ) && \Classifai\language_processing_features_enabled() ) {
$this->classify( $post_id );
}
}
Expand Down
58 changes: 58 additions & 0 deletions includes/Classifai/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,29 @@ function get_post_types_for_language_settings() {
return apply_filters( 'classifai_language_settings_post_types', $post_types );
}

/**
* Get post types we want to show in the language processing settings
*
* @since 1.6.0
*
* @return array
*/
function get_post_statuses_for_language_settings() {
$post_statuses = get_post_statuses();

/**
* Filter post statuses shown in language processing settings.
*
* @since 1.7.1 @TODO: Ensure this is updated to the correct version
* @hook classifai_language_settings_post_statuses
*
* @param {array} $post_statuses Array of post statuses to show in language processing settings.
*
* @return {array} Array of post statuses.
*/
return apply_filters( 'classifai_language_settings_post_statuses', $post_statuses );
}

/**
* The list of post types that get the ClassifAI taxonomies. Defaults
* to 'post'.
Expand Down Expand Up @@ -232,6 +255,41 @@ function get_supported_post_types() {
return $post_types;
}

/**
* The list of post statuses that get the ClassifAI taxonomies. Defaults
* to 'publish'.
*
* return array
*/
function get_supported_post_statuses() {
$classifai_settings = get_plugin_settings( 'language_processing' );

if ( empty( $classifai_settings ) ) {
$post_statuses = [ 'publish' ];
} else {
$post_statuses = [];
foreach ( $classifai_settings['post_statuses'] as $post_status => $enabled ) {
if ( ! empty( $enabled ) ) {
$post_statuses[] = $post_status;
}
}
}

/**
* Filter post statuses supported for language processing.
*
* @since 1.0.0
* @hook classifai_post_statuses
*
* @param {array} $post_types Array of post statuses to be classified with language processing.
*
* @return {array} Array of post statuses.
*/
$post_statuses = apply_filters( 'classifai_post_statuses', $post_statuses );

return $post_statuses;
}

/**
* Returns a bool based on whether the specified feature is enabled
*
Expand Down
49 changes: 49 additions & 0 deletions includes/Classifai/Providers/Watson/NLU.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Classifai\Providers\Provider;
use Classifai\Taxonomy\TaxonomyFactory;
use function Classifai\get_post_types_for_language_settings;
use function Classifai\get_post_statuses_for_language_settings;

class NLU extends Provider {

Expand Down Expand Up @@ -324,6 +325,17 @@ protected function do_nlu_features_sections() {
]
);

add_settings_field(
'post-statuses',
esc_html__( 'Post Statuses to Classify', 'classifai' ),
[ $this, 'render_post_statuses_checkboxes' ],
$this->get_option_name(),
$this->get_option_name(),
[
'option_index' => 'post_statuses',
]
);

foreach ( $this->nlu_features as $feature => $labels ) {
add_settings_field(
$feature,
Expand Down Expand Up @@ -408,6 +420,33 @@ public function render_post_types_checkboxes( $args ) {
echo '</ul>';
}


/**
* Render the post statuses checkbox array.
*
* @param array $args Settings for the input
*
* @return void
*/
public function render_post_statuses_checkboxes( $args ) {
echo '<ul>';
$post_statuses = get_post_statuses_for_language_settings();
foreach ( $post_statuses as $post_status_key => $post_status_label ) {
$args = [
'label_for' => $post_status_key,
'option_index' => 'post_statuses',
'input_type' => 'checkbox',
];

echo '<li>';
$this->render_input( $args );
echo '<label for="classifai-settings-' . esc_attr( $post_status_key ) . '">' . esc_html( $post_status_label ) . '</label>';
echo '</li>';
}

echo '</ul>';
}

/**
* Render the NLU features settings.
*
Expand Down Expand Up @@ -575,6 +614,16 @@ public function sanitize_settings( $settings ) {
}
}

// Sanitize the post statuses checkboxes
$post_statuses = get_post_statuses_for_language_settings();
foreach ( $post_statuses as $post_status_key => $post_status_value ) {
if ( isset( $settings['post_statuses'][ $post_status_key ] ) ) {
$new_settings['post_statuses'][ $post_status_key ] = absint( $settings['post_statuses'][ $post_status_key ] );
} else {
$new_settings['post_statuses'][ $post_status_key ] = null;
}
}

foreach ( $this->nlu_features as $feature => $labels ) {
// Set the enabled flag.
if ( isset( $settings['features'][ $feature ] ) ) {
Expand Down
36 changes: 36 additions & 0 deletions tests/Classifai/Admin/SavePostHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,42 @@ function setUp() {
$this->save_post_handler = new SavePostHandler();
}

function test_get_post_statuses() {
global $wp_filter;

$saved_filters = $wp_filter['classifai_post_statuses'] ?? null;
unset( $wp_filter['classifai_post_statuses'] );

$default_post_statuses_array = array(
'publish',
);

$post_type = 'post';
$post_id = 1;

$this->assertEqualSets( $default_post_statuses_array, $this->save_post_handler->get_post_statuses( $post_type, $post_id ) );

$filtered_post_statuses_array = array(
'publish',
'draft',
'future',
);

add_filter(
'classifai_post_statuses',
function( $post_statuses, $post_type, $post_id ) use ( $filtered_post_statuses_array ) {
return $filteredpost_statuses_array;
},
10,
3
);
$this->assertEqualSets( $post_statuses_array, $this->save_post_handler->get_post_statuses( $post_type, $post_id ) );

if ( ! is_null( $saved_filters ) ) {
$wp_filter['classifai_post_statuses'] = $saved_filters;
}
}

function test_is_rest_route() {
global $wp_filter;

Expand Down

0 comments on commit 8dc621f

Please sign in to comment.