| 1 |
<?php namespace TierPricingTable\Addons\GlobalTieredPricing\CPT\Columns; |
| 2 |
|
| 3 |
use TierPricingTable\Addons\GlobalTieredPricing\CPT\GlobalTieredPricingCPT; |
| 4 |
use TierPricingTable\Addons\GlobalTieredPricing\CPT\StatusBox; |
| 5 |
use TierPricingTable\Addons\GlobalTieredPricing\CPT\StatusViews; |
| 6 |
use TierPricingTable\Addons\GlobalTieredPricing\GlobalPricingRule; |
| 7 |
use TierPricingTable\Addons\GlobalTieredPricing\RuleOverlaps; |
| 8 |
|
| 9 |
/** |
| 10 |
* The primary column: title, status badge, and a meta line with the priority, the date window and |
| 11 |
* the rules that take precedence over it. Row actions are appended by WordPress. |
| 12 |
*/ |
| 13 |
class Rule { |
| 14 |
|
| 15 |
/** |
| 16 |
* @var RuleOverlaps |
| 17 |
*/ |
| 18 |
protected $overlaps; |
| 19 |
|
| 20 |
public function getName(): string { |
| 21 |
return __( 'Rule', 'tier-pricing-table' ); |
| 22 |
} |
| 23 |
|
| 24 |
public function render( GlobalPricingRule $rule ) { |
| 25 |
$id = $rule->getId(); |
| 26 |
$post = get_post( $id ); |
| 27 |
$status = $this->status( $post ? $post->post_status : 'publish', $rule ); |
| 28 |
?> |
| 29 |
<span class="tpt-rules-list__badge tpt-rules-list__badge--<?php echo esc_attr( $status['key'] ); ?>"><?php echo esc_html( $status['label'] ); ?></span> |
| 30 |
<strong><a class="row-title" href="<?php echo esc_url( get_edit_post_link( $id ) ); ?>"><?php echo esc_html( $rule->getTitle() ); ?></a></strong> |
| 31 |
<?php $this->renderLockInfo( $id ); ?> |
| 32 |
<div class="tpt-rules-list__meta"> |
| 33 |
<?php echo wp_kses_post( implode( ' <span class="tpt-rules-list__sep">·</span> ', $this->metaParts( $rule ) ) ); ?> |
| 34 |
</div> |
| 35 |
<?php |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* @return array{key: string, label: string} |
| 40 |
*/ |
| 41 |
protected function status( string $postStatus, GlobalPricingRule $rule ): array { |
| 42 |
$format = get_option( 'date_format' ); |
| 43 |
|
| 44 |
if ( 'publish' !== $postStatus ) { |
| 45 |
$object = get_post_status_object( $postStatus ); |
| 46 |
|
| 47 |
return array( 'key' => 'draft', 'label' => $object ? $object->label : $postStatus ); |
| 48 |
} |
| 49 |
|
| 50 |
$status = $rule->getEffectiveStatus(); |
| 51 |
|
| 52 |
switch ( $status ) { |
| 53 |
case 'scheduled': |
| 54 |
/* translators: %s: date */ |
| 55 |
$label = sprintf( __( 'Starts %s', 'tier-pricing-table' ), wp_date( $format, $rule->getStartTimestamp() ) ); |
| 56 |
break; |
| 57 |
case 'expired': |
| 58 |
/* translators: %s: date */ |
| 59 |
$label = sprintf( __( 'Ended %s', 'tier-pricing-table' ), wp_date( $format, $rule->getEndTimestamp() ) ); |
| 60 |
break; |
| 61 |
default: |
| 62 |
$label = StatusViews::labels()[ $status ]; |
| 63 |
} |
| 64 |
|
| 65 |
return array( 'key' => $status, 'label' => $label ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* @return string[] Escaped HTML fragments. |
| 70 |
*/ |
| 71 |
protected function metaParts( GlobalPricingRule $rule ): array { |
| 72 |
/* translators: %d: priority number */ |
| 73 |
$parts = array( esc_html( sprintf( __( 'Priority %d', 'tier-pricing-table' ), $rule->getPriority() ) ) ); |
| 74 |
|
| 75 |
if ( $rule->hasSchedule() ) { |
| 76 |
$parts[] = esc_html( StatusBox::scheduleSummary( $rule ) ); |
| 77 |
} |
| 78 |
|
| 79 |
// only a problem for a rule in force, and only rules in force can override it (skipped ones never apply) |
| 80 |
$overriding = 'publish' === get_post_status( $rule->getId() ) && $rule->canBeOverridden() |
| 81 |
? $this->getOverlaps()->findOverriding( $rule, GlobalTieredPricingCPT::getGlobalRules( true, false ) ) |
| 82 |
: array(); |
| 83 |
|
| 84 |
if ( $overriding ) { |
| 85 |
$names = array_map( function ( GlobalPricingRule $other ) { |
| 86 |
return $other->getTitle(); |
| 87 |
}, $overriding ); |
| 88 |
|
| 89 |
$parts[] = sprintf( |
| 90 |
'<span class="tpt-rules-list__warn" title="%s">%s</span>', |
| 91 |
esc_attr( implode( ', ', $names ) ), |
| 92 |
/* translators: %d: number of rules */ |
| 93 |
esc_html( sprintf( _n( 'Overridden by %d rule', 'Overridden by %d rules', count( $overriding ), 'tier-pricing-table' ), count( $overriding ) ) ) |
| 94 |
); |
| 95 |
} |
| 96 |
|
| 97 |
return $parts; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* The "someone is editing" line WordPress's own title column prints; the heartbeat updates it. |
| 102 |
*/ |
| 103 |
protected function renderLockInfo( int $id ) { |
| 104 |
if ( ! current_user_can( 'edit_post', $id ) || 'trash' === get_post_status( $id ) ) { |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
$lockHolder = wp_check_post_lock( $id ); |
| 109 |
$lockHolder = $lockHolder ? get_userdata( $lockHolder ) : null; |
| 110 |
?> |
| 111 |
<div class="locked-info"> |
| 112 |
<span class="locked-avatar"><?php echo $lockHolder ? get_avatar( $lockHolder->ID, 18 ) : ''; ?></span> |
| 113 |
<span class="locked-text"> |
| 114 |
<?php |
| 115 |
if ( $lockHolder ) { |
| 116 |
/* translators: %s: user's display name */ |
| 117 |
echo esc_html( sprintf( __( '%s is currently editing', 'tier-pricing-table' ), $lockHolder->display_name ) ); |
| 118 |
} |
| 119 |
?> |
| 120 |
</span> |
| 121 |
</div> |
| 122 |
<?php |
| 123 |
} |
| 124 |
|
| 125 |
protected function getOverlaps(): RuleOverlaps { |
| 126 |
if ( ! $this->overlaps ) { |
| 127 |
$this->overlaps = new RuleOverlaps(); |
| 128 |
} |
| 129 |
|
| 130 |
return $this->overlaps; |
| 131 |
} |
| 132 |
} |
| 133 |
|