-
-
Save GaryJones/a2756ebe6ba340689b13 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Search form. | |
* | |
* @package Gamajo | |
* @author Gary Jones | |
* @copyright (c) 2015 Gary Jones, Gamajo Tech | |
* @link http://gamajo.com/ | |
* @license GPL2-0+ | |
*/ | |
/** | |
* Search form class. | |
* | |
* @package Gamajo | |
* @author Gary Jones | |
*/ | |
class Gamajo_Search_Form { | |
protected $unique_id; | |
protected $args; | |
/** | |
* Merge args and assign to property. | |
* | |
* @since 1.0.0 | |
* | |
* @param array $args Optional. Array of args. Default is an empty array. | |
*/ | |
public function __construct( array $args = array() ) { | |
$defaults = array( | |
/** This filter is documented in genesis/lib/structure/search.php */ | |
'label' => apply_filters( 'genesis_search_form_label', 'Search site' ), | |
// Placeholder should be empty: http://www.nngroup.com/articles/form-design-placeholders/ | |
/** This filter is documented in genesis/lib/structure/search.php */ | |
'placeholder' => apply_filters( 'genesis_search_text', '' ), | |
/** This filter is documented in genesis/lib/structure/search.php */ | |
'button' => apply_filters( 'genesis_search_button_text', 'Search' ), | |
/** | |
* Filter the ARIA label for the search form button. | |
* | |
* @since 1.0.0 | |
* | |
* @param string $button_label Default label is "Search". | |
*/ | |
'button_label' => apply_filters( 'genesis_search_button_label', 'Search' ), | |
'url' => home_url( '/' ), | |
'form-class' => 'search-form', | |
'input-name' => 's', | |
); | |
$this->args = wp_parse_args( $args, $defaults ); | |
} | |
/** | |
* Get one or all args. | |
* | |
* @since 1.0.0 | |
* | |
* @param string $key Optional. Identifier for which string to return. Default is 'all'. | |
* | |
* @return string|array String if key is not 'all', and key exists. Array of all args otherwise. | |
*/ | |
protected function args( $key = 'all' ) { | |
if ( 'all' !== $key && isset( $this->args[ $key ] ) ) { | |
return $this->args[ $key ]; | |
} | |
return $this->args; | |
} | |
/** | |
* Get form markup. | |
* | |
* @since 1.0.0 | |
* | |
* @return string Form markup. | |
*/ | |
public function get_form() { | |
return sprintf( | |
'<form method="get" action="%s" role="search" class="%s">%s</form>', | |
esc_url( $this->args( 'url' ) ), | |
esc_attr( $this->args( 'form-class' ) ), | |
$this->get_label() . $this->get_input() . $this->get_button() | |
); | |
} | |
/** | |
* Get label markup. | |
* | |
* @since 1.0.0 | |
* | |
* @return string Label markup. | |
*/ | |
protected function get_label() { | |
return sprintf( | |
'<label for="%s" class="screen-reader-text">%s</label>', | |
esc_attr( $this->get_id() ), | |
esc_attr( $this->args( 'label' ) ) | |
); | |
} | |
/** | |
* Get input markup. | |
* | |
* @since 1.0.0 | |
* | |
* @return string Input field markup. | |
*/ | |
protected function get_input() { | |
$value = get_search_query() ? apply_filters( 'the_search_query', get_search_query() ) : ''; | |
// Support Google Custom Search submission | |
if ( empty( $value ) && $query = filter_input( INPUT_GET, 'q', FILTER_SANITIZE_STRING ) ) { | |
$value = $query; | |
} | |
return sprintf( | |
'<input type="search" name="%s" id="%s" value="%s" placeholder="%s" autocomplete="off" />', | |
esc_attr( $this->args( 'input-name' ) ), | |
esc_attr( $this->get_id() ), | |
esc_attr( $value ), | |
esc_attr( $this->args( 'placeholder' ) ) | |
); | |
} | |
/** | |
* Get button markup. | |
* | |
* @since 1.0.0 | |
* | |
* @return string Button markup. | |
*/ | |
protected function get_button() { | |
return sprintf( | |
'<button type="submit" aria-label="%s"><span class="search-button-text">%s</span></button>', | |
esc_attr( $this->args( 'button_label' ) ), | |
$this->args( 'button' ) | |
); | |
} | |
/** | |
* Get unique ID. | |
* | |
* @since 1.0.0 | |
* | |
* @return string Unique ID. | |
*/ | |
protected function get_id() { | |
if ( ! isset( $this->unique_id ) ) { | |
$this->unique_id = 'searchform-' . uniqid( '', true ); | |
} | |
return $this->unique_id; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adding an aria-label here is not necessary because the HTML already provides enough information: