Skip to content

Instantly share code, notes, and snippets.

@serhiijko
Forked from imath/tickets.php
Created September 10, 2023 09:42
Show Gist options
  • Save serhiijko/6e5dd92602eabb43cb9e5d7cea994960 to your computer and use it in GitHub Desktop.
Save serhiijko/6e5dd92602eabb43cb9e5d7cea994960 to your computer and use it in GitHub Desktop.

Revisions

  1. @imath imath revised this gist Feb 27, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion tickets.php
    Original file line number Diff line number Diff line change
    @@ -138,7 +138,7 @@ function example_insert_using_custom_status( $data = array(), $postarr = array()
    ), true ) ) {
    $data['post_status'] = sanitize_key( $postarr['_wp_statuses_status'] );

    // Default status for the tickets Post Type is bug.
    // Default status for the tickets Post Type is assigned.
    } else {
    $data['post_status'] = 'assigned';
    }
  2. @imath imath revised this gist Feb 27, 2017. 1 changed file with 17 additions and 24 deletions.
    41 changes: 17 additions & 24 deletions tickets.php
    Original file line number Diff line number Diff line change
    @@ -57,47 +57,40 @@ function example_register() {

    /** Statuses ******************************************************************/

    // Public status to report bugs.
    register_post_status( 'bug', array(
    'label' => _x( 'Bug', 'post status label', 'plugin-domain' ),
    register_post_status( 'resolved', array(
    'label' => _x( 'Resolved', 'post status label', 'plugin-domain' ),
    'public' => true,
    'label_count' => _n_noop( 'Bug <span class="count">(%s)</span>', 'Bugs <span class="count">(%s)</span>', 'plugin-domain' ),
    'label_count' => _n_noop( 'Resolved <span class="count">(%s)</span>', 'Resolved <span class="count">(%s)</span>', 'plugin-domain' ),
    'post_type' => array( 'ticket' ), // Define one or more post types the status can be applied to.
    'show_in_admin_all_list' => true,
    'show_in_admin_status_list' => true,
    'show_in_metabox_dropdown' => true,
    'show_in_inline_dropdown' => true,
    'dashicon' => 'dashicons-hammer',
    'dashicon' => 'dashicons-yes',
    ) );

    // Public status to suggest enhacements.
    register_post_status( 'enhancement', array(
    'label' => _x( 'Enhancement', 'post status label', 'plugin-domain' ),
    register_post_status( 'invalid', array(
    'label' => _x( 'Invalid', 'post status label', 'plugin-domain' ),
    'public' => true,
    'label_count' => _n_noop( 'Enhancement <span class="count">(%s)</span>', 'Enhancements <span class="count">(%s)</span>', 'plugin-domain' ),
    'label_count' => _n_noop( 'Invalid <span class="count">(%s)</span>', 'Invalids <span class="count">(%s)</span>', 'plugin-domain' ),
    'post_type' => array( 'ticket' ), // Define one or more post types the status can be applied to.
    'show_in_admin_all_list' => true,
    'show_in_admin_status_list' => true,
    'show_in_metabox_dropdown' => true,
    'show_in_inline_dropdown' => true,
    'dashicon' => 'dashicons-admin-tools',
    'dashicon' => 'dashicons-dismiss',
    ) );

    // Private status to alert about security issues.
    register_post_status( 'security', array(
    'label' => _x( 'Security issue', 'post status label', 'plugin-domain' ),
    'private' => true, // only self, Editor and Administrator can view.
    'label_count' => _n_noop( 'Risque <span class="count">(%s)</span>', 'Risques <span class="count">(%s)</span>', 'plugin-domain' ),
    register_post_status( 'assigned', array(
    'label' => _x( 'Assigned', 'post status label', 'plugin-domain' ),
    'public' => true,
    'label_count' => _n_noop( 'Assigned <span class="count">(%s)</span>', 'Assigned <span class="count">(%s)</span>', 'plugin-domain' ),
    'post_type' => array( 'ticket' ), // Define one or more post types the status can be applied to.
    'show_in_admin_all_list' => true,
    'show_in_admin_status_list' => true,
    'show_in_metabox_dropdown' => true,
    'show_in_inline_dropdown' => true,
    'labels' => array(
    'metabox_dropdown' => __( 'Security issue', 'plugin-domain' ),
    'inline_dropdown' => __( 'Security', 'plugin-domain' ),
    ),
    'dashicon' => 'dashicons-warning',
    'dashicon' => 'dashicons-businessman',
    ) );
    }
    add_action( 'init', 'example_register' );
    @@ -139,15 +132,15 @@ function example_insert_using_custom_status( $data = array(), $postarr = array()
    }

    if ( ! empty( $postarr['_wp_statuses_status'] ) && in_array( $postarr['_wp_statuses_status'], array(
    'bug',
    'enhancement',
    'security',
    'resolved',
    'invalid',
    'assigned',
    ), true ) ) {
    $data['post_status'] = sanitize_key( $postarr['_wp_statuses_status'] );

    // Default status for the tickets Post Type is bug.
    } else {
    $data['post_status'] = 'bug';
    $data['post_status'] = 'assigned';
    }

    return $data;
  3. @imath imath created this gist Feb 25, 2017.
    155 changes: 155 additions & 0 deletions tickets.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,155 @@
    <?php
    /**
    * Using WP Statuses for custom Post Types.
    *
    * @link http://github.com/imath/wp-statuses
    */

    // Exit if accessed directly.
    defined( 'ABSPATH' ) || exit;

    /**
    * Register the 'ticket' Post Type and the ticket's specific statuses.
    *
    * @link http://codex.wordpress.org/Function_Reference/register_post_type
    */
    function example_register() {

    register_post_type( 'ticket', array(
    'labels' => array(
    'name' => _x( 'Tickets', 'post type general name', 'plugin-domain' ),
    'singular_name' => _x( 'Ticket', 'post type singular name', 'plugin-domain' ),
    'menu_name' => _x( 'Tickets', 'admin menu', 'plugin-domain' ),
    'name_admin_bar' => _x( 'Ticket', 'add new on admin bar', 'plugin-domain' ),
    'add_new' => _x( 'Add new', 'book', 'plugin-domain' ),
    'add_new_item' => __( 'Add a new Ticket', 'plugin-domain' ),
    'new_item' => __( 'New Ticket', 'plugin-domain' ),
    'edit_item' => __( 'Edit Ticket', 'plugin-domain' ),
    'view_item' => __( 'View Ticket', 'plugin-domain' ),
    'all_items' => __( 'All Tickets', 'plugin-domain' ),
    'search_items' => __( 'Search Tickets', 'plugin-domain' ),
    'parent_item_colon' => __( 'Parent Ticket:', 'plugin-domain' ),
    'not_found' => __( 'No tickets found', 'plugin-domain' ),
    'not_found_in_trash' => __( 'No tickets found in trash', 'plugin-domain' ),
    'insert_into_item' => __( 'Insert into ticket', 'plugin-domain' ),
    'uploaded_to_this_item' => __( 'Uploaded to this ticket', 'plugin-domain' ),
    'filter_items_list' => __( 'Filter tickets list', 'plugin-domain' ),
    'items_list_navigation' => __( 'Tickets list navigation', 'plugin-domain' ),
    'items_list' => __( 'Tickets list', 'plugin-domain' ),
    ),
    'description' => __( 'WP Statuses\' example for a custom Post Type.', 'plugin-domain' ),
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'tickets' ),
    'capability_type' => 'post',
    'has_archive' => true,
    'hierarchical' => false,
    'menu_position' => null,
    'menu_icon' => 'dashicons-tickets',
    'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
    'delete_with_user' => true,
    'can_export' => true,
    'show_in_rest' => true,
    ) );

    /** Statuses ******************************************************************/

    // Public status to report bugs.
    register_post_status( 'bug', array(
    'label' => _x( 'Bug', 'post status label', 'plugin-domain' ),
    'public' => true,
    'label_count' => _n_noop( 'Bug <span class="count">(%s)</span>', 'Bugs <span class="count">(%s)</span>', 'plugin-domain' ),
    'post_type' => array( 'ticket' ), // Define one or more post types the status can be applied to.
    'show_in_admin_all_list' => true,
    'show_in_admin_status_list' => true,
    'show_in_metabox_dropdown' => true,
    'show_in_inline_dropdown' => true,
    'dashicon' => 'dashicons-hammer',
    ) );

    // Public status to suggest enhacements.
    register_post_status( 'enhancement', array(
    'label' => _x( 'Enhancement', 'post status label', 'plugin-domain' ),
    'public' => true,
    'label_count' => _n_noop( 'Enhancement <span class="count">(%s)</span>', 'Enhancements <span class="count">(%s)</span>', 'plugin-domain' ),
    'post_type' => array( 'ticket' ), // Define one or more post types the status can be applied to.
    'show_in_admin_all_list' => true,
    'show_in_admin_status_list' => true,
    'show_in_metabox_dropdown' => true,
    'show_in_inline_dropdown' => true,
    'dashicon' => 'dashicons-admin-tools',
    ) );

    // Private status to alert about security issues.
    register_post_status( 'security', array(
    'label' => _x( 'Security issue', 'post status label', 'plugin-domain' ),
    'private' => true, // only self, Editor and Administrator can view.
    'label_count' => _n_noop( 'Risque <span class="count">(%s)</span>', 'Risques <span class="count">(%s)</span>', 'plugin-domain' ),
    'post_type' => array( 'ticket' ), // Define one or more post types the status can be applied to.
    'show_in_admin_all_list' => true,
    'show_in_admin_status_list' => true,
    'show_in_metabox_dropdown' => true,
    'show_in_inline_dropdown' => true,
    'labels' => array(
    'metabox_dropdown' => __( 'Security issue', 'plugin-domain' ),
    'inline_dropdown' => __( 'Security', 'plugin-domain' ),
    ),
    'dashicon' => 'dashicons-warning',
    ) );
    }
    add_action( 'init', 'example_register' );

    /**
    * Only keep Draft & Pending statuses but do not use other builtin statuses.
    *
    * PS: you should at least keep Draft & Pending.
    *
    * @param array $post_types The list of registered Post Types for the status.
    * @param string $status_name Name of the status to apply to Post Types.
    * @return array The list of registered Post Types for the status.
    */
    function example_restrict_statuses_for_tickets( $post_types = array(), $status_name = '' ) {
    if ( 'draft' === $status_name || 'pending' === $status_name ) {
    return $post_types;
    }

    // All other statuses (eg: Publish, Private...) won't be applied to tickets
    return array_diff( $post_types, array( 'ticket' ) );
    }
    add_filter( 'wp_statuses_get_registered_post_types', 'example_restrict_statuses_for_tickets', 10, 2 );

    /**
    * Makes sure we can directly "Publish" using custom statuses.
    *
    * @param array $data A list of arguments to use to insert a new Post Type's item.
    * @param array $postarr WordPress' version of posted var.
    * @return array A list of arguments to use to insert a new Post Type's item.
    */
    function example_insert_using_custom_status( $data = array(), $postarr = array() ) {

    if ( empty( $postarr['publish'] ) ) {
    return $data;
    }

    if ( 'ticket' !== $data['post_type'] ) {
    return $data;
    }

    if ( ! empty( $postarr['_wp_statuses_status'] ) && in_array( $postarr['_wp_statuses_status'], array(
    'bug',
    'enhancement',
    'security',
    ), true ) ) {
    $data['post_status'] = sanitize_key( $postarr['_wp_statuses_status'] );

    // Default status for the tickets Post Type is bug.
    } else {
    $data['post_status'] = 'bug';
    }

    return $data;
    }
    add_filter( 'wp_insert_post_data', 'example_insert_using_custom_status', 10, 2 );