Is there a reason that Questions cannot be set to draft? If so, the UI should not allow for setting to the draft status (only to have it overridden).
I had to do some hacky stuff to get around this limitation.
/**
* Handles resetting a question's status back to the requested status, because Sensei is whack.
*
* @see Sensei_Question::save_question()
* @see Sensei_Lesson::lesson_save_question()
* Screenshot: http://b.ustin.co/M09O why??
* Issue Posted: https://github.com/Automattic/sensei/issues/1701
*/
class QP_Sensei_Allow_Non_Published_Questions {
protected static $post_status = null;
public static function maybe_reset_status( $post_id, $post ) {
if (
isset( $post->post_status, $post->post_type )
&& 'question' === $post->post_type
&& ! wp_is_post_autosave( $post )
&& ! wp_is_post_revision( $post )
) {
// Cache the requested status.
self::$post_status = $post->post_status;
// Unhook to avoid recursion.
remove_action( 'save_post_question', array( __CLASS__, 'maybe_reset_status' ), 10, 2 );
// Hook in before the second question-update completes & modify the post data before the update.
add_filter( 'wp_insert_post_data', array( __CLASS__, 'reset_status' ) );
}
}
public static function reset_status( $post_data ) {
if (
isset( $post_data['post_status'], $post_data['post_type'] )
&& 'publish' === $post_data['post_status']
&& 'question' === $post_data['post_type']
) {
// Put the post status back to the requested status.
$post_data['post_status'] = self::$post_status;
// Remove for recursion.
remove_filter( 'wp_insert_post_data', array( __CLASS__, 'reset_status' ) );
}
return $post_data;
}
}
add_action( 'save_post_question', array( 'QP_Sensei_Allow_Non_Published_Questions', 'maybe_reset_status' ), 10, 2 );
Would love to know if there is some unknown, undocumented reasoning for always setting to publish.
Most helpful comment
I had to do some hacky stuff to get around this limitation.
Would love to know if there is some unknown, undocumented reasoning for always setting to
publish.