| 1 |
<?php |
| 2 |
/** |
| 3 |
* Shared Club Selector markup renderer. |
| 4 |
* |
| 5 |
* @package wp-commerce7 |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
return; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Render club selector HTML. |
| 14 |
* |
| 15 |
* @param array $args { |
| 16 |
* @type array $clubs Club rows with club_slug, club_name, button_text. |
| 17 |
* @type string $display_type radio|select. |
| 18 |
* @type string $radio_group_name Radio input group name. |
| 19 |
* @type string $widget_id Optional unique suffix for input ids. |
| 20 |
* @type string $button_link_class CSS class for the button anchor. |
| 21 |
* @type string $wrapper_class Extra wrapper classes. |
| 22 |
* } |
| 23 |
* @return string |
| 24 |
*/ |
| 25 |
function c7wp_render_clubselector( $args ) { |
| 26 |
$defaults = array( |
| 27 |
'clubs' => array(), |
| 28 |
'display_type' => 'radio', |
| 29 |
'radio_group_name' => 'club-selector', |
| 30 |
'widget_id' => uniqid( 'c7', false ), |
| 31 |
'button_link_class' => 'club-selector-button', |
| 32 |
'wrapper_class' => '', |
| 33 |
'echo' => true, |
| 34 |
); |
| 35 |
|
| 36 |
$args = wp_parse_args( $args, $defaults ); |
| 37 |
|
| 38 |
$valid_clubs = array(); |
| 39 |
foreach ( $args['clubs'] as $club ) { |
| 40 |
if ( ! empty( $club['club_slug'] ) && ! empty( $club['club_name'] ) && ! empty( $club['button_text'] ) ) { |
| 41 |
$valid_clubs[] = $club; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
$slugs = array_map( |
| 46 |
static function ( $club ) { |
| 47 |
return strtolower( $club['club_slug'] ); |
| 48 |
}, |
| 49 |
$valid_clubs |
| 50 |
); |
| 51 |
$unique_slugs = array_unique( $slugs ); |
| 52 |
|
| 53 |
if ( empty( $valid_clubs ) || count( $slugs ) !== count( $unique_slugs ) ) { |
| 54 |
$message = empty( $valid_clubs ) |
| 55 |
? __( 'Please add at least one valid club with slug, name, and button text.', 'wp-commerce7' ) |
| 56 |
: __( 'Duplicate club slugs found. Please ensure each club has a unique slug.', 'wp-commerce7' ); |
| 57 |
|
| 58 |
$html = '<div class="c7-clubselector-error">' . esc_html( $message ) . '</div>'; |
| 59 |
if ( $args['echo'] ) { |
| 60 |
echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 61 |
return ''; |
| 62 |
} |
| 63 |
return $html; |
| 64 |
} |
| 65 |
|
| 66 |
$options = get_option( 'c7wp_settings', array() ); |
| 67 |
$club_route = isset( $options['c7wp_frontend_routes']['club'] ) ? $options['c7wp_frontend_routes']['club'] : 'club'; |
| 68 |
$first_club = $valid_clubs[0]; |
| 69 |
$button_url = '/' . trailingslashit( ltrim( $club_route, '/' ) ) . trailingslashit( $first_club['club_slug'] ); |
| 70 |
|
| 71 |
ob_start(); |
| 72 |
?> |
| 73 |
<div class="club-selector-wrapper <?php echo esc_attr( $args['wrapper_class'] ); ?>" data-c7-widget-id="<?php echo esc_attr( $args['widget_id'] ); ?>"> |
| 74 |
<?php if ( 'select' === $args['display_type'] ) : ?> |
| 75 |
<select class="club-select" aria-label="<?php esc_attr_e( 'Choose your desired club', 'wp-commerce7' ); ?>"> |
| 76 |
<?php foreach ( $valid_clubs as $index => $club ) : ?> |
| 77 |
<option value="<?php echo esc_attr( $club['club_slug'] ); ?>" data-button-text="<?php echo esc_attr( $club['button_text'] ); ?>" <?php selected( $index, 0 ); ?>> |
| 78 |
<?php echo esc_html( $club['club_name'] ); ?> |
| 79 |
</option> |
| 80 |
<?php endforeach; ?> |
| 81 |
</select> |
| 82 |
<?php else : ?> |
| 83 |
<div class="club-radio-buttons"> |
| 84 |
<fieldset> |
| 85 |
<legend class="screen-reader-text"><?php esc_html_e( 'Choose your desired club', 'wp-commerce7' ); ?></legend> |
| 86 |
<div class="radios"> |
| 87 |
<?php foreach ( $valid_clubs as $index => $club ) : ?> |
| 88 |
<div class="row"> |
| 89 |
<input |
| 90 |
type="radio" |
| 91 |
name="<?php echo esc_attr( $args['radio_group_name'] ); ?>" |
| 92 |
id="club-<?php echo esc_attr( $club['club_slug'] ); ?>-<?php echo esc_attr( $args['widget_id'] ); ?>" |
| 93 |
value="<?php echo esc_attr( $club['club_slug'] ); ?>" |
| 94 |
data-button-text="<?php echo esc_attr( $club['button_text'] ); ?>" |
| 95 |
class="choice" |
| 96 |
<?php checked( $index, 0 ); ?> |
| 97 |
aria-checked="<?php echo 0 === $index ? 'true' : 'false'; ?>" |
| 98 |
aria-label="<?php echo esc_attr( $club['club_name'] ); ?>" |
| 99 |
> |
| 100 |
<label for="club-<?php echo esc_attr( $club['club_slug'] ); ?>-<?php echo esc_attr( $args['widget_id'] ); ?>"> |
| 101 |
<?php echo esc_html( $club['club_name'] ); ?> |
| 102 |
</label> |
| 103 |
</div> |
| 104 |
<?php endforeach; ?> |
| 105 |
</div> |
| 106 |
</fieldset> |
| 107 |
</div> |
| 108 |
<?php endif; ?> |
| 109 |
|
| 110 |
<div class="club-button"> |
| 111 |
<a href="<?php echo esc_url( $button_url ); ?>" class="<?php echo esc_attr( $args['button_link_class'] ); ?>"> |
| 112 |
<?php echo esc_html( $first_club['button_text'] ); ?> |
| 113 |
</a> |
| 114 |
</div> |
| 115 |
</div> |
| 116 |
<?php |
| 117 |
$html = ob_get_clean(); |
| 118 |
|
| 119 |
if ( $args['echo'] ) { |
| 120 |
echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 121 |
return ''; |
| 122 |
} |
| 123 |
|
| 124 |
return $html; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Normalize club rows from builder-specific structures. |
| 129 |
* |
| 130 |
* @param array $clubs Raw club rows. |
| 131 |
* @return array |
| 132 |
*/ |
| 133 |
function c7wp_normalize_clubselector_clubs( $clubs ) { |
| 134 |
$normalized = array(); |
| 135 |
|
| 136 |
if ( ! is_array( $clubs ) ) { |
| 137 |
return $normalized; |
| 138 |
} |
| 139 |
|
| 140 |
foreach ( $clubs as $club ) { |
| 141 |
if ( is_object( $club ) ) { |
| 142 |
$club = (array) $club; |
| 143 |
} |
| 144 |
|
| 145 |
$normalized[] = array( |
| 146 |
'club_slug' => isset( $club['club_slug'] ) ? $club['club_slug'] : ( isset( $club['slug'] ) ? $club['slug'] : '' ), |
| 147 |
'club_name' => isset( $club['club_name'] ) ? $club['club_name'] : ( isset( $club['name'] ) ? $club['name'] : '' ), |
| 148 |
'button_text' => isset( $club['button_text'] ) ? $club['button_text'] : ( isset( $club['button'] ) ? $club['button'] : '' ), |
| 149 |
); |
| 150 |
} |
| 151 |
|
| 152 |
return $normalized; |
| 153 |
} |
| 154 |
|