| 1 |
<?php |
| 2 |
|
| 3 |
namespace TierPricingTable\Addons\CustomColumns\Columns; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use TierPricingTable\Addons\CustomColumns\CustomColumnsManager; |
| 7 |
use TierPricingTable\Addons\CustomColumns\Schema; |
| 8 |
use TierPricingTable\PricingRule; |
| 9 |
abstract class AbstractCustomColumn { |
| 10 |
protected $slug; |
| 11 |
|
| 12 |
/** |
| 13 |
* Data |
| 14 |
* |
| 15 |
* @var array |
| 16 |
*/ |
| 17 |
protected $data; |
| 18 |
|
| 19 |
/** |
| 20 |
* Column constructor |
| 21 |
* |
| 22 |
* @throws Exception |
| 23 |
*/ |
| 24 |
public function __construct( $slug, array $data = array() ) { |
| 25 |
$this->slug = $slug; |
| 26 |
$this->data = $data; |
| 27 |
if ( !$this->isValid() ) { |
| 28 |
throw new Exception('Wrong data to create column instance'); |
| 29 |
} |
| 30 |
$this->hooks(); |
| 31 |
} |
| 32 |
|
| 33 |
public function isValid() : bool { |
| 34 |
if ( !is_string( $this->slug ) ) { |
| 35 |
return false; |
| 36 |
} |
| 37 |
if ( empty( $this->data['name'] ) ) { |
| 38 |
return false; |
| 39 |
} |
| 40 |
return true; |
| 41 |
} |
| 42 |
|
| 43 |
protected function hooks() { |
| 44 |
} |
| 45 |
|
| 46 |
public function getData() : array { |
| 47 |
return $this->data; |
| 48 |
} |
| 49 |
|
| 50 |
public function getSlug( ?string $prefix = null ) : string { |
| 51 |
// Prefix is used to store custom field for percentage and fixed type |
| 52 |
return ( $prefix ? $prefix . '_' . $this->slug : $this->slug ); |
| 53 |
} |
| 54 |
|
| 55 |
public function getName() : string { |
| 56 |
return (string) apply_filters( 'tiered_pricing_table/custom_columns/name', $this->data['name'], $this ); |
| 57 |
} |
| 58 |
|
| 59 |
public function addColumnHeaderColumn() { |
| 60 |
?> |
| 61 |
<th> |
| 62 |
<span class="nobr"> |
| 63 |
<?php |
| 64 |
echo esc_attr( $this->getName() ); |
| 65 |
?> |
| 66 |
</span> |
| 67 |
</th> |
| 68 |
<?php |
| 69 |
} |
| 70 |
|
| 71 |
public function renderColumn( PricingRule $pricingRule, $currentTierQuantity = null ) { |
| 72 |
?> |
| 73 |
<td> |
| 74 |
<?php |
| 75 |
echo wp_kses_post( $this->getSingleRowValue( $pricingRule, $currentTierQuantity ) ); |
| 76 |
?> |
| 77 |
</td> |
| 78 |
<?php |
| 79 |
} |
| 80 |
|
| 81 |
public function getSingleRowValue( PricingRule $pricingRule, $currentTierQuantity = null ) { |
| 82 |
$value = $this->_getSingleRowValue( $pricingRule, $currentTierQuantity ); |
| 83 |
return apply_filters( |
| 84 |
'tiered_pricing_table/custom_columns/value', |
| 85 |
$value, |
| 86 |
$pricingRule, |
| 87 |
$currentTierQuantity, |
| 88 |
$this |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
public function isColumnVisible() : bool { |
| 93 |
return true; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Save custom column |
| 98 |
* |
| 99 |
* @throws Exception |
| 100 |
*/ |
| 101 |
public function save() : bool { |
| 102 |
if ( !$this->isValid() ) { |
| 103 |
throw new Exception('Wrong data to save column'); |
| 104 |
} |
| 105 |
$columnsManager = CustomColumnsManager::getInstance(); |
| 106 |
return $columnsManager->saveColumn( $this ); |
| 107 |
} |
| 108 |
|
| 109 |
public function remove() { |
| 110 |
CustomColumnsManager::getInstance()->removeColumn( $this->getSlug() ); |
| 111 |
} |
| 112 |
|
| 113 |
public function getFormattedType() { |
| 114 |
$types = Schema::getAvailableCustomColumnsTypes(); |
| 115 |
return ( isset( $types[$this->getType()] ) ? $types[$this->getType()] : '' ); |
| 116 |
} |
| 117 |
|
| 118 |
public function getFormattedDataType() { |
| 119 |
$types = Schema::getAvailableDataTypes(); |
| 120 |
return ( isset( $types[$this->getDataType()] ) ? $types[$this->getDataType()] : '' ); |
| 121 |
} |
| 122 |
|
| 123 |
public abstract function getType() : string; |
| 124 |
|
| 125 |
public abstract function getDataType() : string; |
| 126 |
|
| 127 |
protected abstract function _getSingleRowValue( PricingRule $pricingRule, $currentTierQuantity = null ); |
| 128 |
|
| 129 |
} |
| 130 |
|