| 1 |
<?php |
| 2 |
/** |
| 3 |
* Tax IDs Settings Section. |
| 4 |
* |
| 5 |
* @package WCPOS\WooCommercePOS |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WCPOS\WooCommercePOS\Services\Settings; |
| 9 |
|
| 10 |
use WCPOS\WooCommercePOS\Services\Tax_Id_Types; |
| 11 |
|
| 12 |
/** |
| 13 |
* The Tax IDs Settings Section: per-type meta-key write overrides. Empty by |
| 14 |
* default — the composed write_map (defaults + plugin detection + scan) is |
| 15 |
* used when no override exists. |
| 16 |
*/ |
| 17 |
class Tax_Ids_Section extends Abstract_Section { |
| 18 |
/** |
| 19 |
* Section id. |
| 20 |
*/ |
| 21 |
public function id(): string { |
| 22 |
return 'tax_ids'; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Section defaults. |
| 27 |
*/ |
| 28 |
public function defaults(): array { |
| 29 |
return array( |
| 30 |
'write_map' => array(), |
| 31 |
); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Migrate write_map from its legacy home inside the general option, in memory only. |
| 36 |
* |
| 37 |
* @param array $raw Raw option value. |
| 38 |
*/ |
| 39 |
protected function migrate( array $raw ): array { |
| 40 |
if ( ! \array_key_exists( 'write_map', $raw ) ) { |
| 41 |
$legacy_general = get_option( self::DB_PREFIX . 'general', array() ); |
| 42 |
if ( |
| 43 |
\is_array( $legacy_general ) |
| 44 |
&& isset( $legacy_general['tax_ids']['write_map'] ) |
| 45 |
&& \is_array( $legacy_general['tax_ids']['write_map'] ) |
| 46 |
) { |
| 47 |
$raw['write_map'] = $legacy_general['tax_ids']['write_map']; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
return $raw; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Write_map is intentionally a full replacement (not deep-merged) so |
| 56 |
* users can remove entries by sending the trimmed map. |
| 57 |
* |
| 58 |
* @param array $existing Existing settings view. |
| 59 |
* @param array $patch Incoming partial payload. |
| 60 |
*/ |
| 61 |
public function merge( array $existing, array $patch ): array { |
| 62 |
$settings = array_replace_recursive( $existing, $patch ); |
| 63 |
if ( isset( $patch['write_map'] ) && \is_array( $patch['write_map'] ) ) { |
| 64 |
$settings['write_map'] = $patch['write_map']; |
| 65 |
} |
| 66 |
|
| 67 |
return $settings; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* REST endpoint args (formerly the legacy API\Settings controller's |
| 72 |
* get_tax_ids_endpoint_args(), removed in 1.10). |
| 73 |
*/ |
| 74 |
public function endpoint_args(): array { |
| 75 |
return array( |
| 76 |
'write_map' => array( |
| 77 |
'validate_callback' => function ( $param, $request, $key ) { |
| 78 |
if ( ! \is_array( $param ) ) { |
| 79 |
return false; |
| 80 |
} |
| 81 |
foreach ( $param as $type => $meta_key ) { |
| 82 |
if ( ! \is_string( $type ) || ! Tax_Id_Types::is_valid_type( $type ) ) { |
| 83 |
return false; |
| 84 |
} |
| 85 |
if ( ! \is_string( $meta_key ) ) { |
| 86 |
return false; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
return true; |
| 91 |
}, |
| 92 |
), |
| 93 |
); |
| 94 |
} |
| 95 |
} |
| 96 |
|