| 1 |
<?php namespace TierPricingTable\Addons\NonLoggedInUsers\Visibility\Admin; |
| 2 |
|
| 3 |
use TierPricingTable\Addons\NonLoggedInUsers\Visibility\VisibilityMeta; |
| 4 |
use TierPricingTable\Addons\NonLoggedInUsers\Visibility\VisibilityRule; |
| 5 |
|
| 6 |
/** |
| 7 |
* The "Who can see" column of the products list: the product's own rule, or the category rules it |
| 8 |
* follows when it has none. |
| 9 |
*/ |
| 10 |
class ProductListColumn { |
| 11 |
|
| 12 |
const COLUMN = 'tpt_visibility'; |
| 13 |
|
| 14 |
public function __construct() { |
| 15 |
add_filter( 'manage_edit-product_columns', array( $this, 'addColumn' ), 20 ); |
| 16 |
add_action( 'manage_product_posts_custom_column', array( $this, 'renderColumn' ), 10, 2 ); |
| 17 |
add_action( 'admin_head-edit.php', array( $this, 'printStyles' ) ); |
| 18 |
} |
| 19 |
|
| 20 |
public function addColumn( $columns ) { |
| 21 |
$columns = is_array( $columns ) ? $columns : array(); |
| 22 |
$result = array(); |
| 23 |
|
| 24 |
foreach ( $columns as $key => $label ) { |
| 25 |
$result[ $key ] = $label; |
| 26 |
|
| 27 |
if ( 'product_cat' === $key ) { |
| 28 |
$result[ self::COLUMN ] = __( 'Who can see', 'tier-pricing-table' ); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
if ( ! isset( $result[ self::COLUMN ] ) ) { |
| 33 |
$result[ self::COLUMN ] = __( 'Who can see', 'tier-pricing-table' ); |
| 34 |
} |
| 35 |
|
| 36 |
return $result; |
| 37 |
} |
| 38 |
|
| 39 |
public function renderColumn( $column, $productId ) { |
| 40 |
if ( self::COLUMN !== $column ) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
echo wp_kses_post( $this->describe( (int) $productId ) ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @return string HTML |
| 49 |
*/ |
| 50 |
public function describe( int $productId ): string { |
| 51 |
$rule = VisibilityMeta::getProductRule( $productId ); |
| 52 |
|
| 53 |
if ( VisibilityRule::MODE_INHERIT !== $rule['mode'] ) { |
| 54 |
return RuleText::ownLine( $rule ); |
| 55 |
} |
| 56 |
|
| 57 |
$lines = array(); |
| 58 |
|
| 59 |
foreach ( RuleText::restrictingCategoryRules( $this->getCategoryIds( $productId ) ) as $termId => $categoryRule ) { |
| 60 |
$lines[] = RuleText::line( $categoryRule, RuleText::categoryName( $termId ) ); |
| 61 |
} |
| 62 |
|
| 63 |
return $lines ? implode( '<br>', $lines ) : RuleText::defaultLine(); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* @return int[] the product's categories with their ancestors |
| 68 |
*/ |
| 69 |
protected function getCategoryIds( int $productId ): array { |
| 70 |
$termIds = wp_get_post_terms( $productId, 'product_cat', array( 'fields' => 'ids' ) ); |
| 71 |
|
| 72 |
if ( is_wp_error( $termIds ) ) { |
| 73 |
return array(); |
| 74 |
} |
| 75 |
|
| 76 |
$all = array(); |
| 77 |
|
| 78 |
foreach ( $termIds as $termId ) { |
| 79 |
$all[] = (int) $termId; |
| 80 |
|
| 81 |
foreach ( get_ancestors( (int) $termId, 'product_cat', 'taxonomy' ) as $ancestor ) { |
| 82 |
$all[] = (int) $ancestor; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
return $all; |
| 87 |
} |
| 88 |
|
| 89 |
public function printStyles() { |
| 90 |
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; |
| 91 |
|
| 92 |
if ( $screen && 'edit-product' === $screen->id ) { |
| 93 |
RuleText::printStyles(); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|