$columns['cb'] ?? '', 'applicant' => __( 'Applicant', 'tier-pricing-table' ), 'company' => __( 'Company', 'tier-pricing-table' ), 'contact' => __( 'Contact', 'tier-pricing-table' ), 'status' => __( 'Status', 'tier-pricing-table' ), 'submitted' => __( 'Submitted', 'tier-pricing-table' ), 'decision' => __( 'Decision', 'tier-pricing-table' ), ); } public function columnData( string $column, int $postId ) { $application = WholesaleApplication::get( $postId ); if ( ! $application ) { return; } switch ( $column ) { case 'applicant': $user = $application->getUser(); ?> getName() ?: $application->getEmail() ); ?>
getEmail() ); ?> getEmail() ); ?>
getCompany() ?: '—' ); if ( $application->getField( 'tax_id' ) ) { ?>
getField( 'tax_id' ) ); ?>
getField( 'phone' ) ?: '—' ); if ( $application->getField( 'website' ) ) { ?>
getField( 'website' ) ) ); ?>
statusBadge( $application ); break; case 'submitted': echo esc_html( $application->getDate() ? date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( $application->getDate() ) ) : '—' ); break; case 'decision': if ( $application->isPending() ) { ?> getField( 'decided_at' ); $decidedBy = (int) $application->getField( 'decided_by' ); $by = $decidedBy ? get_user_by( 'id', $decidedBy ) : null; echo esc_html( $decidedAt ? date_i18n( get_option( 'date_format' ), strtotime( $decidedAt ) ) : '—' ); if ( $by ) { ?>
display_name ); ?>
isApproved() ) { ?>
getStatus() ); ?> getStatusLabel() ); ?> post_type ) { return $actions; } $application = WholesaleApplication::get( $post->ID ); $new = array(); if ( isset( $actions['edit'] ) ) { $new['edit'] = '' . esc_html__( 'View', 'tier-pricing-table' ) . ''; } if ( $application && ! $application->isApproved() ) { $new['approve'] = '' . esc_html__( 'Approve', 'tier-pricing-table' ) . ''; } if ( $application && ! $application->isRejected() ) { $new['reject'] = '' . esc_html__( 'Reject', 'tier-pricing-table' ) . ''; } if ( isset( $actions['trash'] ) ) { $new['trash'] = $actions['trash']; } return $new; } public function bulkActions( array $actions ): array { unset( $actions['edit'] ); return array_merge( array( 'tpt_approve' => __( 'Approve', 'tier-pricing-table' ), 'tpt_reject' => __( 'Reject', 'tier-pricing-table' ), ), $actions ); } public function handleBulkActions( string $redirect, string $action, array $postIds ): string { if ( ! in_array( $action, array( 'tpt_approve', 'tpt_reject' ), true ) || ! current_user_can( 'manage_woocommerce' ) ) { return $redirect; } $service = new ApplicationService(); $done = 0; foreach ( $postIds as $postId ) { $application = WholesaleApplication::get( (int) $postId ); if ( ! $application ) { continue; } $ok = 'tpt_approve' === $action ? $service->approve( $application, get_current_user_id() ) : $service->reject( $application, get_current_user_id() ); $done += $ok ? 1 : 0; } return add_query_arg( array( 'tpt-wholesale-notice' => 'tpt_approve' === $action ? 'approved' : 'rejected', 'tpt-wholesale-count' => $done, ), $redirect ); } public function removeUnusedViews( array $views ): array { unset( $views['publish'], $views['draft'], $views['future'], $views['private'] ); return $views; } public function postStates( array $states, WP_Post $post ): array { if ( ApplicationCPT::POST_TYPE === $post->post_type ) { return array(); // the status column is enough } return $states; } /* --- approve / reject links -------------------------------------------------------------- */ protected function actionUrl( string $do, int $postId ): string { return wp_nonce_url( add_query_arg( array( 'action' => self::ACTION, 'do' => $do, 'id' => $postId, ), admin_url( 'admin-post.php' ) ), self::ACTION . '_' . $postId, self::NONCE ); } public function handleAction() { $postId = isset( $_GET['id'] ) ? (int) $_GET['id'] : 0; $do = isset( $_GET['do'] ) ? sanitize_key( $_GET['do'] ) : ''; if ( ! $postId || ! current_user_can( 'manage_woocommerce' ) || ! isset( $_GET[ self::NONCE ] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET[ self::NONCE ] ) ), self::ACTION . '_' . $postId ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'tier-pricing-table' ) ); } $application = WholesaleApplication::get( $postId ); $service = new ApplicationService(); $notice = 'error'; if ( $application && 'approve' === $do && $service->approve( $application, get_current_user_id() ) ) { $notice = 'approved'; } elseif ( $application && 'reject' === $do && $service->reject( $application, get_current_user_id() ) ) { $notice = 'rejected'; } $back = wp_get_referer() ?: admin_url( 'edit.php?post_type=' . ApplicationCPT::POST_TYPE ); wp_safe_redirect( add_query_arg( array( 'tpt-wholesale-notice' => $notice, 'tpt-wholesale-count' => 1 ), remove_query_arg( array( 'tpt-wholesale-notice', 'tpt-wholesale-count' ), $back ) ) ); exit; } public function notices() { if ( empty( $_GET['tpt-wholesale-notice'] ) ) { return; } $notice = sanitize_key( $_GET['tpt-wholesale-notice'] ); $count = isset( $_GET['tpt-wholesale-count'] ) ? (int) $_GET['tpt-wholesale-count'] : 1; if ( 'approved' === $notice ) { /* translators: %s: number of applications */ $message = sprintf( _n( '%s application approved. The customer now has the wholesale role.', '%s applications approved. The customers now have the wholesale role.', $count, 'tier-pricing-table' ), number_format_i18n( $count ) ); $class = 'notice-success'; } elseif ( 'rejected' === $notice ) { /* translators: %s: number of applications */ $message = sprintf( _n( '%s application rejected.', '%s applications rejected.', $count, 'tier-pricing-table' ), number_format_i18n( $count ) ); $class = 'notice-info'; } else { $message = __( 'The application could not be updated. Check that the wholesale role exists and that the customer account still exists.', 'tier-pricing-table' ); $class = 'notice-error'; } ?>

getUser(); ?> getDetails() as $label => $value ) : ?>
getField( 'website' ) && '' !== $value ) : ?>
display_name ); ?> (roles[ $r ]['name'] ?? $r; }, (array) $user->roles ) ) ) ); ?>)
getDate() ) ) ); ?>
getRequestedRole() ?: Settings::getRole(); $roleName = $role && isset( wp_roles()->roles[ $role ] ) ? translate_user_role( wp_roles()->roles[ $role ]['name'] ) : $role; wp_nonce_field( self::ACTION . '_decision', self::NONCE ); ?>

statusBadge( $application ); ?>

isRejected() && $application->getRejectionReason() ) : ?>

getRejectionReason() ); ?>

isApproved() ) : ?>

isRejected() ) : ?>

approve( $application, get_current_user_id() ); } else { $ok = $service->reject( $application, get_current_user_id(), $reason ); } add_filter( 'redirect_post_location', function ( $location ) use ( $decision, $ok ) { return add_query_arg( array( 'tpt-wholesale-notice' => $ok ? ( 'approve' === $decision ? 'approved' : 'rejected' ) : 'error', 'tpt-wholesale-count' => 1, ), remove_query_arg( 'message', $location ) ); } ); } public function titlePlaceholder( string $placeholder, WP_Post $post ): string { return ApplicationCPT::POST_TYPE === $post->post_type ? __( 'Application', 'tier-pricing-table' ) : $placeholder; } public function styles() { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( ! $screen || ApplicationCPT::POST_TYPE !== $screen->post_type ) { return; } ?>