| 1 |
<?php namespace TierPricingTable\Addons\RequestAQuote\CPT; |
| 2 |
|
| 3 |
use Automattic\WooCommerce\Admin\PageController; |
| 4 |
|
| 5 |
class QuoteRequestCPT { |
| 6 |
|
| 7 |
public const POST_TYPE = 'tpt-quote-request'; |
| 8 |
|
| 9 |
public function __construct() { |
| 10 |
add_action( 'init', array( $this, 'registerPostType' ) ); |
| 11 |
add_action( 'admin_menu', array( $this, 'addAdminMenu' ), 99 ); |
| 12 |
add_action( 'before_delete_post', array( $this, 'deleteAssociatedFiles' ) ); |
| 13 |
} |
| 14 |
|
| 15 |
public function deleteAssociatedFiles( $postId ) { |
| 16 |
if ( get_post_type( $postId ) !== self::POST_TYPE ) { |
| 17 |
return; |
| 18 |
} |
| 19 |
|
| 20 |
$quote = \TierPricingTable\Addons\RequestAQuote\Models\QuoteRequest::get( $postId ); |
| 21 |
if ( ! $quote ) { |
| 22 |
return; |
| 23 |
} |
| 24 |
|
| 25 |
$customFields = $quote->getCustomFields(); |
| 26 |
if ( is_array( $customFields ) ) { |
| 27 |
foreach ( $customFields as $field ) { |
| 28 |
if ( ! empty( $field['file_path'] ) && file_exists( $field['file_path'] ) ) { |
| 29 |
unlink( $field['file_path'] ); |
| 30 |
} |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
public function registerPostType() { |
| 36 |
$labels = array( |
| 37 |
'name' => _x( 'Quote Requests', 'post type general name', 'tier-pricing-table' ), |
| 38 |
'singular_name' => _x( 'Quote Request', 'post type singular name', 'tier-pricing-table' ), |
| 39 |
'menu_name' => _x( 'Quote Requests', 'admin menu', 'tier-pricing-table' ), |
| 40 |
'name_admin_bar' => _x( 'Quote Request', 'add new on admin bar', 'tier-pricing-table' ), |
| 41 |
'add_new' => _x( 'Add New', 'quote request', 'tier-pricing-table' ), |
| 42 |
'add_new_item' => __( 'Add New Quote Request', 'tier-pricing-table' ), |
| 43 |
'new_item' => __( 'New Quote Request', 'tier-pricing-table' ), |
| 44 |
'edit_item' => __( 'Edit Quote Request', 'tier-pricing-table' ), |
| 45 |
'view_item' => __( 'View Quote Request', 'tier-pricing-table' ), |
| 46 |
'all_items' => __( 'All Quote Requests', 'tier-pricing-table' ), |
| 47 |
'search_items' => __( 'Search Quote Requests', 'tier-pricing-table' ), |
| 48 |
'not_found' => __( 'No quote requests found.', 'tier-pricing-table' ), |
| 49 |
'not_found_in_trash' => __( 'No quote requests found in Trash.', 'tier-pricing-table' ), |
| 50 |
); |
| 51 |
|
| 52 |
$args = array( |
| 53 |
'labels' => $labels, |
| 54 |
'description' => __( 'Quote Requests.', 'tier-pricing-table' ), |
| 55 |
'public' => false, |
| 56 |
'publicly_queryable' => false, |
| 57 |
'show_ui' => true, |
| 58 |
'show_in_menu' => false, // We will add it manually as a WooCommerce submenu |
| 59 |
'query_var' => true, |
| 60 |
'rewrite' => array( 'slug' => 'tpt-quote-request' ), |
| 61 |
'capability_type' => 'post', |
| 62 |
'has_archive' => false, |
| 63 |
'hierarchical' => false, |
| 64 |
'menu_position' => null, |
| 65 |
'supports' => array( 'title', 'custom-fields' ), |
| 66 |
); |
| 67 |
|
| 68 |
register_post_type( self::POST_TYPE, $args ); |
| 69 |
|
| 70 |
$this->registerPostStatuses(); |
| 71 |
|
| 72 |
// Add this CPT to WC Navigation |
| 73 |
if ( class_exists( PageController::class ) ) { |
| 74 |
PageController::get_instance()->connect_page( |
| 75 |
array( |
| 76 |
'id' => 'tpt-quote-request', |
| 77 |
'title' => __( 'Quote Requests', 'tier-pricing-table' ), |
| 78 |
'screen_id' => 'edit-tpt-quote-request', |
| 79 |
'path' => 'edit.php?post_type=' . self::POST_TYPE, |
| 80 |
) |
| 81 |
); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
private function registerPostStatuses() { |
| 86 |
register_post_status( 'unread', array( |
| 87 |
'label' => _x( 'Unread', 'quote request status', 'tier-pricing-table' ), |
| 88 |
'public' => true, |
| 89 |
'exclude_from_search' => false, |
| 90 |
'show_in_admin_all_list' => true, |
| 91 |
'show_in_admin_status_list' => true, |
| 92 |
// translators: %s: number of requests. |
| 93 |
'label_count' => _n_noop( 'Unread <span class="count">(%s)</span>', 'Unread <span class="count">(%s)</span>', 'tier-pricing-table' ), |
| 94 |
) ); |
| 95 |
|
| 96 |
register_post_status( 'read', array( |
| 97 |
'label' => _x( 'Read', 'quote request status', 'tier-pricing-table' ), |
| 98 |
'public' => true, |
| 99 |
'exclude_from_search' => false, |
| 100 |
'show_in_admin_all_list' => true, |
| 101 |
'show_in_admin_status_list' => true, |
| 102 |
// translators: %s: number of requests. |
| 103 |
'label_count' => _n_noop( 'Read <span class="count">(%s)</span>', 'Read <span class="count">(%s)</span>', 'tier-pricing-table' ), |
| 104 |
) ); |
| 105 |
|
| 106 |
register_post_status( 'converted', array( |
| 107 |
'label' => _x( 'Converted', 'quote request status', 'tier-pricing-table' ), |
| 108 |
'public' => true, |
| 109 |
'exclude_from_search' => false, |
| 110 |
'show_in_admin_all_list' => true, |
| 111 |
'show_in_admin_status_list' => true, |
| 112 |
// translators: %s: number of requests. |
| 113 |
'label_count' => _n_noop( 'Converted <span class="count">(%s)</span>', 'Converted <span class="count">(%s)</span>', 'tier-pricing-table' ), |
| 114 |
) ); |
| 115 |
|
| 116 |
register_post_status( 'rejected', array( |
| 117 |
'label' => _x( 'Rejected', 'quote request status', 'tier-pricing-table' ), |
| 118 |
'public' => true, |
| 119 |
'exclude_from_search' => false, |
| 120 |
'show_in_admin_all_list' => true, |
| 121 |
'show_in_admin_status_list' => true, |
| 122 |
// translators: %s: number of requests. |
| 123 |
'label_count' => _n_noop( 'Rejected <span class="count">(%s)</span>', 'Rejected <span class="count">(%s)</span>', 'tier-pricing-table' ), |
| 124 |
) ); |
| 125 |
} |
| 126 |
|
| 127 |
public function addAdminMenu() { |
| 128 |
$unreadCount = wp_count_posts( self::POST_TYPE )->unread ?? 0; |
| 129 |
|
| 130 |
$menuTitle = __( 'Quote Requests', 'tier-pricing-table' ); |
| 131 |
|
| 132 |
if ( $unreadCount > 0 ) { |
| 133 |
// translators: %s: number of unread requests. |
| 134 |
$menuTitle .= ' <span class="update-plugins count-' . esc_attr( $unreadCount ) . '"><span class="plugin-count" aria-hidden="true">' . number_format_i18n( $unreadCount ) . '</span><span class="screen-reader-text">' . sprintf( _n( '%s unread request', '%s unread requests', $unreadCount, 'tier-pricing-table' ), number_format_i18n( $unreadCount ) ) . '</span></span>'; |
| 135 |
} |
| 136 |
|
| 137 |
add_submenu_page( |
| 138 |
'woocommerce', |
| 139 |
__( 'Quote Requests', 'tier-pricing-table' ), |
| 140 |
$menuTitle, |
| 141 |
'manage_woocommerce', |
| 142 |
'edit.php?post_type=' . self::POST_TYPE |
| 143 |
); |
| 144 |
} |
| 145 |
} |
| 146 |
|