PluginProbe
Tiered Pricing Table for WooCommerce / trunk
Tiered Pricing Table for WooCommerce vtrunk
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 2.3.4 All 90 releases
tier-pricing-table / src / Addons / CustomColumns / CustomColumnsManager.php

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

174 lines 3.6 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 $dataType = $customColumn['data_type'] ?? 'text';
118
119 $type = Schema::isValidColumnType( $type ) ? $type : false;
120 $dataType = Schema::isValidDataType( $dataType ) ? $dataType : 'text';
121
122 if ( ! $name || ! $type ) {
123 return false;
124 }
125
126 $customColumn['data_type'] = $dataType;
127
128 return $customColumn;
129 }
130
131 /**
132 * Get custom column instance
133 *
134 * @param $slug
135 * @param $data
136 *
137 * @return AbstractCustomColumn|null
138 */
139 public function getColumnInstance( $slug, $data ): ?AbstractCustomColumn {
140 $class = Schema::getClassForType( $data['type'] );
141
142 try {
143 return new $class( $slug, $data );
144 } catch ( \Exception $exception ) {
145 return null;
146 }
147 }
148
149 protected function sanitizeAndFilterRawColumns( $columns ): array {
150
151 if ( ! array( $columns ) ) {
152 return array();
153 }
154
155 $validRawColumns = array();
156
157 foreach ( $columns as $slug => $column ) {
158 $column = is_array( $column ) ? $column : array();
159
160 if ( empty( $column ) ) {
161 continue;
162 }
163
164 $data = $this->sanitizeColumnData( $column );
165
166 if ( $data ) {
167 $validRawColumns[ $slug ] = $data;
168 }
169 }
170
171 return $validRawColumns;
172 }
173 }
174