PluginProbe
Tiered Pricing Table for WooCommerce / 6.1.0
Tiered Pricing Table for WooCommerce v6.1.0
8.0.2 7.1.7 7.1.5 7.1.4 7.1.1 6.5.0 6.4.0 6.1.0 trunk 1.0 1.1 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 2.3.1 2.3.2 2.3.3 All 91 releases
tier-pricing-table / src / Addons / CustomColumns / CustomColumnsManager.php

CustomColumnsManager.php in Tiered Pricing Table for WooCommerce 6.1.0, at src/Addons/CustomColumns/CustomColumnsManager.php

170 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace TierPricingTable\Addons\CustomColumns;
2
3 use TierPricingTable\Addons\CustomColumns\Columns\AbstractCustomColumn;
4 use TierPricingTable\Forms\Form;
5
6 class CustomColumnsManager {
7
8 const CUSTOM_COLUMNS_OPTION_NAME = 'tpt_custom_table_columns';
9
10 protected static $instance = null;
11
12 /**
13 * Custom columns
14 *
15 * @var AbstractCustomColumn[]
16 */
17 public $columns = null;
18
19 /**
20 * Raw custom columns data
21 *
22 * @var array
23 */
24 public $rawColumns = null;
25
26 public static function getInstance(): self {
27 if ( is_null( self::$instance ) ) {
28 self::$instance = new self();
29 }
30
31 return self::$instance;
32 }
33
34 private function __construct() {
35 add_action( 'init', function () {
36 $this->columns = $this->getColumns();
37 } );
38 }
39
40 public function getColumns(): array {
41
42 if ( is_null( $this->columns ) ) {
43
44 $rawColumns = $this->getRawColumns();
45
46 $instances = array();
47
48 foreach ( $rawColumns as $slug => $columnData ) {
49 $instances[] = $this->getColumnInstance( $slug, $columnData );
50 }
51
52 $instances = array_values( array_filter( $instances ) );
53
54 $this->columns = $instances;
55 }
56
57 return $this->columns;
58 }
59
60 public function getRawColumns(): array {
61
62 if ( is_null( $this->rawColumns ) ) {
63 $columns = (array) get_option( self::CUSTOM_COLUMNS_OPTION_NAME, array() );
64
65 $this->rawColumns = $this->sanitizeAndFilterRawColumns( $columns );
66 }
67
68 return $this->rawColumns;
69 }
70
71 public function updateRawColumns( array $columns ) {
72 $columns = $this->sanitizeAndFilterRawColumns( $columns );
73
74 update_option( self::CUSTOM_COLUMNS_OPTION_NAME, $columns );
75
76 $this->rawColumns = $columns;
77 $this->columns = null;
78 }
79
80 public function saveColumn( AbstractCustomColumn $column ): bool {
81
82 $columnData = $this->sanitizeColumnData( $column->getData() );
83
84 if ( ! $columnData ) {
85 return false;
86 }
87
88 $columns = $this->getRawColumns();
89 $columns[ $column->getSlug() ] = $columnData;
90
91 $this->updateRawColumns( $columns );
92
93 return true;
94 }
95
96 public function removeColumn( $slug ): bool {
97
98 $columns = $this->getRawColumns();
99
100 if ( array_key_exists( $slug, $columns ) ) {
101 unset( $columns[ $slug ] );
102
103 delete_post_meta_by_key( Form::getFieldName( $slug ) );
104 } else {
105 return false;
106 }
107
108 $this->updateRawColumns( $columns );
109
110 return true;
111 }
112
113 public function sanitizeColumnData( array $customColumn ) {
114
115 $name = $customColumn['name'] ?? false;
116 $type = $customColumn['type'] ?? false;
117
118 $type = Schema::isValidColumnType( $type ) ? $type : false;
119
120 if ( ! $name || ! $type ) {
121 return false;
122 }
123
124 return $customColumn;
125 }
126
127 /**
128 * Get custom column instance
129 *
130 * @param $slug
131 * @param $data
132 *
133 * @return AbstractCustomColumn|null
134 */
135 public function getColumnInstance( $slug, $data ): ?AbstractCustomColumn {
136 $class = Schema::getClassForType( $data['type'] );
137
138 try {
139 return new $class( $slug, $data );
140 } catch ( \Exception $exception ) {
141 return null;
142 }
143 }
144
145 protected function sanitizeAndFilterRawColumns( $columns ): array {
146
147 if ( ! array( $columns ) ) {
148 return array();
149 }
150
151 $validRawColumns = array();
152
153 foreach ( $columns as $slug => $column ) {
154 $column = is_array( $column ) ? $column : array();
155
156 if ( empty( $column ) ) {
157 continue;
158 }
159
160 $data = $this->sanitizeColumnData( $column );
161
162 if ( $data ) {
163 $validRawColumns[ $slug ] = $data;
164 }
165 }
166
167 return $validRawColumns;
168 }
169 }
170