Sensei: Cannot set Question to 'draft' status

Created on 16 Jan 2017  路  1Comment  路  Source: Automattic/sensei

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).

Quizzes [Type] Bug

Most helpful comment

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.

>All comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kraftbj picture kraftbj  路  4Comments

Protohominid picture Protohominid  路  6Comments

donnapep picture donnapep  路  5Comments

merkushin picture merkushin  路  3Comments

danjjohnson picture danjjohnson  路  5Comments