| 1 |
<?php namespace TierPricingTable\Addons\NonLoggedInUsers\Wholesale\CPT; |
| 2 |
|
| 3 |
use Automattic\WooCommerce\Admin\PageController; |
| 4 |
use TierPricingTable\Addons\NonLoggedInUsers\Wholesale\Models\WholesaleApplication; |
| 5 |
|
| 6 |
class ApplicationCPT { |
| 7 |
|
| 8 |
public const POST_TYPE = 'tpt-wholesale-app'; |
| 9 |
|
| 10 |
public function __construct() { |
| 11 |
add_action( 'init', array( $this, 'registerPostType' ) ); |
| 12 |
add_action( 'admin_menu', array( $this, 'addAdminMenu' ), 99 ); |
| 13 |
add_action( 'deleted_user', array( $this, 'deleteApplicationsOfUser' ) ); |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* A deleted customer leaves no application behind. |
| 18 |
*/ |
| 19 |
public function deleteApplicationsOfUser( $userId ) { |
| 20 |
$applications = get_posts( array( |
| 21 |
'post_type' => self::POST_TYPE, |
| 22 |
'post_status' => 'any', |
| 23 |
'posts_per_page' => -1, |
| 24 |
'fields' => 'ids', |
| 25 |
'meta_key' => WholesaleApplication::FIELDS['user_id'], |
| 26 |
'meta_value' => (int) $userId, |
| 27 |
) ); |
| 28 |
|
| 29 |
foreach ( $applications as $applicationId ) { |
| 30 |
wp_delete_post( (int) $applicationId, true ); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
public function registerPostType() { |
| 35 |
$labels = array( |
| 36 |
'name' => _x( 'Wholesale Applications', 'post type general name', 'tier-pricing-table' ), |
| 37 |
'singular_name' => _x( 'Wholesale Application', 'post type singular name', 'tier-pricing-table' ), |
| 38 |
'menu_name' => _x( 'Wholesale Applications', 'admin menu', 'tier-pricing-table' ), |
| 39 |
'edit_item' => __( 'Wholesale Application', 'tier-pricing-table' ), |
| 40 |
'view_item' => __( 'View Application', 'tier-pricing-table' ), |
| 41 |
'all_items' => __( 'All Applications', 'tier-pricing-table' ), |
| 42 |
'search_items' => __( 'Search Applications', 'tier-pricing-table' ), |
| 43 |
'not_found' => __( 'No wholesale applications yet.', 'tier-pricing-table' ), |
| 44 |
'not_found_in_trash' => __( 'No wholesale applications found in Trash.', 'tier-pricing-table' ), |
| 45 |
); |
| 46 |
|
| 47 |
register_post_type( self::POST_TYPE, array( |
| 48 |
'labels' => $labels, |
| 49 |
'description' => __( 'Wholesale account applications.', 'tier-pricing-table' ), |
| 50 |
'public' => false, |
| 51 |
'publicly_queryable' => false, |
| 52 |
'exclude_from_search' => true, |
| 53 |
'show_ui' => true, |
| 54 |
'show_in_menu' => false, // added as a WooCommerce submenu below |
| 55 |
'show_in_rest' => false, |
| 56 |
'query_var' => false, |
| 57 |
'rewrite' => false, |
| 58 |
'capability_type' => 'post', |
| 59 |
'capabilities' => array( 'create_posts' => 'do_not_allow' ), |
| 60 |
'map_meta_cap' => true, |
| 61 |
'has_archive' => false, |
| 62 |
'hierarchical' => false, |
| 63 |
'supports' => false, |
| 64 |
) ); |
| 65 |
|
| 66 |
$this->registerPostStatuses(); |
| 67 |
|
| 68 |
if ( class_exists( PageController::class ) ) { |
| 69 |
PageController::get_instance()->connect_page( array( |
| 70 |
'id' => self::POST_TYPE, |
| 71 |
'title' => __( 'Wholesale Applications', 'tier-pricing-table' ), |
| 72 |
'screen_id' => 'edit-' . self::POST_TYPE, |
| 73 |
'path' => 'edit.php?post_type=' . self::POST_TYPE, |
| 74 |
) ); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
protected function registerPostStatuses() { |
| 79 |
$statuses = array( |
| 80 |
WholesaleApplication::STATUS_PENDING => array( |
| 81 |
_x( 'Pending', 'wholesale application status', 'tier-pricing-table' ), |
| 82 |
/* translators: %s: number of applications */ |
| 83 |
_n_noop( 'Pending <span class="count">(%s)</span>', 'Pending <span class="count">(%s)</span>', 'tier-pricing-table' ), |
| 84 |
), |
| 85 |
WholesaleApplication::STATUS_APPROVED => array( |
| 86 |
_x( 'Approved', 'wholesale application status', 'tier-pricing-table' ), |
| 87 |
/* translators: %s: number of applications */ |
| 88 |
_n_noop( 'Approved <span class="count">(%s)</span>', 'Approved <span class="count">(%s)</span>', 'tier-pricing-table' ), |
| 89 |
), |
| 90 |
WholesaleApplication::STATUS_REJECTED => array( |
| 91 |
_x( 'Rejected', 'wholesale application status', 'tier-pricing-table' ), |
| 92 |
/* translators: %s: number of applications */ |
| 93 |
_n_noop( 'Rejected <span class="count">(%s)</span>', 'Rejected <span class="count">(%s)</span>', 'tier-pricing-table' ), |
| 94 |
), |
| 95 |
); |
| 96 |
|
| 97 |
foreach ( $statuses as $status => $labels ) { |
| 98 |
register_post_status( $status, array( |
| 99 |
'label' => $labels[0], |
| 100 |
'public' => false, |
| 101 |
'internal' => false, |
| 102 |
// a protected status is listed in the admin "All" view; "any" queries skip statuses excluded from search |
| 103 |
'protected' => true, |
| 104 |
'exclude_from_search' => false, |
| 105 |
'show_in_admin_all_list' => true, |
| 106 |
'show_in_admin_status_list' => true, |
| 107 |
'label_count' => $labels[1], |
| 108 |
) ); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
public function addAdminMenu() { |
| 113 |
$pending = WholesaleApplication::countPending(); |
| 114 |
$menuTitle = __( 'Wholesale Applications', 'tier-pricing-table' ); |
| 115 |
|
| 116 |
if ( $pending > 0 ) { |
| 117 |
$menuTitle .= ' <span class="update-plugins count-' . esc_attr( $pending ) . '"><span class="plugin-count" aria-hidden="true">' . number_format_i18n( $pending ) . '</span><span class="screen-reader-text">' . sprintf( |
| 118 |
/* translators: %s: number of pending applications */ |
| 119 |
_n( '%s pending application', '%s pending applications', $pending, 'tier-pricing-table' ), |
| 120 |
number_format_i18n( $pending ) |
| 121 |
) . '</span></span>'; |
| 122 |
} |
| 123 |
|
| 124 |
add_submenu_page( |
| 125 |
'woocommerce', |
| 126 |
__( 'Wholesale Applications', 'tier-pricing-table' ), |
| 127 |
$menuTitle, |
| 128 |
'manage_woocommerce', |
| 129 |
'edit.php?post_type=' . self::POST_TYPE |
| 130 |
); |
| 131 |
} |
| 132 |
} |
| 133 |
|