$label ) { $result[ $key ] = $label; if ( 'product_cat' === $key ) { $result[ self::COLUMN ] = __( 'Who can see', 'tier-pricing-table' ); } } if ( ! isset( $result[ self::COLUMN ] ) ) { $result[ self::COLUMN ] = __( 'Who can see', 'tier-pricing-table' ); } return $result; } public function renderColumn( $column, $productId ) { if ( self::COLUMN !== $column ) { return; } echo wp_kses_post( $this->describe( (int) $productId ) ); } /** * @return string HTML */ public function describe( int $productId ): string { $rule = VisibilityMeta::getProductRule( $productId ); if ( VisibilityRule::MODE_INHERIT !== $rule['mode'] ) { return RuleText::ownLine( $rule ); } $lines = array(); foreach ( RuleText::restrictingCategoryRules( $this->getCategoryIds( $productId ) ) as $termId => $categoryRule ) { $lines[] = RuleText::line( $categoryRule, RuleText::categoryName( $termId ) ); } return $lines ? implode( '
', $lines ) : RuleText::defaultLine(); } /** * @return int[] the product's categories with their ancestors */ protected function getCategoryIds( int $productId ): array { $termIds = wp_get_post_terms( $productId, 'product_cat', array( 'fields' => 'ids' ) ); if ( is_wp_error( $termIds ) ) { return array(); } $all = array(); foreach ( $termIds as $termId ) { $all[] = (int) $termId; foreach ( get_ancestors( (int) $termId, 'product_cat', 'taxonomy' ) as $ancestor ) { $all[] = (int) $ancestor; } } return $all; } public function printStyles() { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( $screen && 'edit-product' === $screen->id ) { RuleText::printStyles(); } } }