| 1 |
<?php |
| 2 |
/** |
| 3 |
* Setup Wizard progressive step-save registry. |
| 4 |
* |
| 5 |
* @package Booking Calendar |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Resolve exact step handlers without embedding domain rules in navigation. |
| 14 |
*/ |
| 15 |
final class WPBC_Setup_Wizard_Step_Save_Registry { |
| 16 |
|
| 17 |
/** @var array<string,WPBC_Setup_Wizard_Step_Save_Handler> */ |
| 18 |
private $handlers = array(); |
| 19 |
|
| 20 |
/** |
| 21 |
* Register an ordered set of unique save handlers. |
| 22 |
* |
| 23 |
* Invalid and duplicate handlers are ignored. Callers can then fail closed |
| 24 |
* when no exact handler exists for the current step. |
| 25 |
* |
| 26 |
* @param WPBC_Setup_Wizard_Step_Save_Handler[] $handlers Save handlers. |
| 27 |
*/ |
| 28 |
public function __construct( array $handlers = array() ) { |
| 29 |
foreach ( $handlers as $handler ) { |
| 30 |
if ( ! $handler instanceof WPBC_Setup_Wizard_Step_Save_Handler ) { |
| 31 |
continue; |
| 32 |
} |
| 33 |
|
| 34 |
$step_id = sanitize_key( $handler->get_step_id() ); |
| 35 |
if ( '' === $step_id || isset( $this->handlers[ $step_id ] ) ) { |
| 36 |
continue; |
| 37 |
} |
| 38 |
|
| 39 |
$this->handlers[ $step_id ] = $handler; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Return one exact registered handler. |
| 45 |
* |
| 46 |
* @param string $step_id Proposed step identifier. |
| 47 |
* |
| 48 |
* @return WPBC_Setup_Wizard_Step_Save_Handler|null Handler or null. |
| 49 |
*/ |
| 50 |
public function get_handler( $step_id ) { |
| 51 |
$step_id = sanitize_key( is_scalar( $step_id ) ? (string) $step_id : '' ); |
| 52 |
|
| 53 |
return isset( $this->handlers[ $step_id ] ) ? $this->handlers[ $step_id ] : null; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Return the server-owned action label for one step. |
| 58 |
* |
| 59 |
* @param string $step_id Stable step identifier. |
| 60 |
* @param array<string,mixed> $checkpoint Current normalized checkpoint. |
| 61 |
* @param array<string,mixed> $step_context Authorized presentation context. |
| 62 |
* |
| 63 |
* @return string Translated action label, or an empty string when unavailable. |
| 64 |
*/ |
| 65 |
public function get_primary_action_label( $step_id, array $checkpoint, array $step_context = array() ) { |
| 66 |
$handler = $this->get_handler( $step_id ); |
| 67 |
|
| 68 |
return null === $handler ? '' : $handler->get_primary_action_label( $checkpoint, $step_context ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Normalize a handler result before checkpoint persistence. |
| 73 |
* |
| 74 |
* @param mixed $handler_result Domain handler result candidate. |
| 75 |
* |
| 76 |
* @return array<string,mixed>|WP_Error Normalized result or contract error. |
| 77 |
*/ |
| 78 |
public function normalize_result( $handler_result ) { |
| 79 |
if ( ! is_array( $handler_result ) || 'saved' !== ( isset( $handler_result['status'] ) ? $handler_result['status'] : '' ) ) { |
| 80 |
return new WP_Error( 'wpbc_setup_wizard_save_result_invalid', __( 'The Setup Wizard step returned an invalid save result.', 'booking' ) ); |
| 81 |
} |
| 82 |
|
| 83 |
return array( |
| 84 |
'status' => 'saved', |
| 85 |
'summary' => isset( $handler_result['summary'] ) && is_scalar( $handler_result['summary'] ) ? sanitize_text_field( (string) $handler_result['summary'] ) : '', |
| 86 |
'canonical_fingerprint' => isset( $handler_result['canonical_fingerprint'] ) && is_scalar( $handler_result['canonical_fingerprint'] ) ? sanitize_text_field( (string) $handler_result['canonical_fingerprint'] ) : '', |
| 87 |
'created_ids' => $this->normalize_identifiers( isset( $handler_result['created_ids'] ) ? $handler_result['created_ids'] : array() ), |
| 88 |
'updated_ids' => $this->normalize_identifiers( isset( $handler_result['updated_ids'] ) ? $handler_result['updated_ids'] : array() ), |
| 89 |
'warnings' => $this->normalize_warnings( isset( $handler_result['warnings'] ) ? $handler_result['warnings'] : array() ), |
| 90 |
'writes_canonical_settings' => ! empty( $handler_result['writes_canonical_settings'] ), |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Normalize bounded scalar record identifiers. |
| 96 |
* |
| 97 |
* @param mixed $identifiers Identifier list candidate. |
| 98 |
* |
| 99 |
* @return array<int,int|string> Normalized identifiers. |
| 100 |
*/ |
| 101 |
private function normalize_identifiers( $identifiers ) { |
| 102 |
$normalized_identifiers = array(); |
| 103 |
|
| 104 |
foreach ( is_array( $identifiers ) ? array_slice( $identifiers, 0, 100 ) : array() as $identifier ) { |
| 105 |
if ( is_int( $identifier ) || ( is_string( $identifier ) && '' !== trim( $identifier ) ) ) { |
| 106 |
$normalized_identifiers[] = is_int( $identifier ) ? $identifier : sanitize_text_field( $identifier ); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
return $normalized_identifiers; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Normalize bounded user-facing warning strings. |
| 115 |
* |
| 116 |
* @param mixed $warnings Warning list candidate. |
| 117 |
* |
| 118 |
* @return string[] Normalized warnings. |
| 119 |
*/ |
| 120 |
private function normalize_warnings( $warnings ) { |
| 121 |
$normalized_warnings = array(); |
| 122 |
|
| 123 |
foreach ( is_array( $warnings ) ? array_slice( $warnings, 0, 20 ) : array() as $warning ) { |
| 124 |
if ( is_scalar( $warning ) && '' !== trim( (string) $warning ) ) { |
| 125 |
$normalized_warnings[] = sanitize_text_field( (string) $warning ); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
return $normalized_warnings; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|