| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* filters search forms when using permalinks |
| 5 |
* |
| 6 |
* @since 1.2 |
| 7 |
*/ |
| 8 |
class PLL_Frontend_Filters_Search { |
| 9 |
public $links_model, $curlang; |
| 10 |
|
| 11 |
/** |
| 12 |
* constructor |
| 13 |
* |
| 14 |
* @since 1.2 |
| 15 |
* |
| 16 |
* @param object $polylang |
| 17 |
*/ |
| 18 |
public function __construct( &$polylang ) { |
| 19 |
$this->links_model = &$polylang->links_model; |
| 20 |
$this->curlang = &$polylang->curlang; |
| 21 |
|
| 22 |
// adds the language information in the search form |
| 23 |
// low priority in case the search form is created using the same filter as described in http://codex.wordpress.org/Function_Reference/get_search_form |
| 24 |
add_filter( 'get_search_form', array( $this, 'get_search_form' ), 99 ); |
| 25 |
|
| 26 |
// adds the language information in admin bar search form |
| 27 |
add_action( 'add_admin_bar_menus', array( $this, 'add_admin_bar_menus' ) ); |
| 28 |
|
| 29 |
// adds javascript at the end of the document |
| 30 |
// was used for WP < 3.6. kept just in case |
| 31 |
if ( defined( 'PLL_SEARCH_FORM_JS' ) && PLL_SEARCH_FORM_JS ) { |
| 32 |
add_action( 'wp_footer', array( $this, 'wp_print_footer_scripts' ) ); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* adds the language information in the search form |
| 38 |
* does not work if searchform.php ( prior to WP 3.6 ) is used or if the search form is hardcoded in another template file |
| 39 |
* |
| 40 |
* @since 0.1 |
| 41 |
* |
| 42 |
* @param string $form search form |
| 43 |
* @return string modified search form |
| 44 |
*/ |
| 45 |
public function get_search_form( $form ) { |
| 46 |
if ( $form ) { |
| 47 |
if ( $this->links_model->using_permalinks ) { |
| 48 |
// take care to modify only the url in the <form> tag |
| 49 |
preg_match( '#<form.+>#', $form, $matches ); |
| 50 |
$old = reset( $matches ); |
| 51 |
$new = preg_replace( '#' . esc_url( $this->links_model->home ) . '\/?#', esc_url( $this->curlang->search_url ), $old ); |
| 52 |
$form = str_replace( $old, $new, $form ); |
| 53 |
} |
| 54 |
else { |
| 55 |
$form = str_replace( '</form>', '<input type="hidden" name="lang" value="'.esc_attr( $this->curlang->slug ).'" /></form>', $form ); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
return $form; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* adds the language information in admin bar search form |
| 64 |
* |
| 65 |
* @since 1.2 |
| 66 |
*/ |
| 67 |
function add_admin_bar_menus() { |
| 68 |
remove_action( 'admin_bar_menu', 'wp_admin_bar_search_menu', 4 ); |
| 69 |
add_action( 'admin_bar_menu', array( $this, 'admin_bar_search_menu' ), 4 ); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* rewrites the admin bar search form to pass our get_search form filter. See #21342 |
| 74 |
* code base is WP 4.3.1 |
| 75 |
* |
| 76 |
* @since 0.9 |
| 77 |
* |
| 78 |
* @param object $wp_admin_bar |
| 79 |
*/ |
| 80 |
public function admin_bar_search_menu( $wp_admin_bar ) { |
| 81 |
$form = '<form action="' . esc_url( home_url( '/' ) ) . '" method="get" id="adminbarsearch">'; |
| 82 |
$form .= '<input class="adminbar-input" name="s" id="adminbar-search" type="text" value="" maxlength="150" />'; |
| 83 |
$form .= '<label for="adminbar-search" class="screen-reader-text">' . esc_html__( 'Search' ) . '</label>'; |
| 84 |
$form .= '<input type="submit" class="adminbar-button" value="' . esc_attr__( 'Search' ) . '"/>'; |
| 85 |
$form .= '</form>'; |
| 86 |
|
| 87 |
$wp_admin_bar->add_menu( array( |
| 88 |
'parent' => 'top-secondary', |
| 89 |
'id' => 'search', |
| 90 |
'title' => $this->get_search_form( $form ), // pass the get_search_form filter |
| 91 |
'meta' => array( 'class' => 'admin-bar-search', 'tabindex' => -1 ), |
| 92 |
) ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* allows modifying the search form if it does not pass get_search_form |
| 97 |
* |
| 98 |
* @since 0.1 |
| 99 |
*/ |
| 100 |
public function wp_print_footer_scripts() { |
| 101 |
// don't use directly e[0] just in case there is somewhere else an element named 's' |
| 102 |
// check before if the hidden input has not already been introduced by get_search_form ( FIXME: is there a way to improve this ) ? |
| 103 |
// thanks to AndyDeGroo for improving the code for compatility with old browsers |
| 104 |
// http://wordpress.org/support/topic/development-of-polylang-version-08?replies=6#post-2645559 |
| 105 |
$lang = esc_js( $this->curlang->slug ); |
| 106 |
$js = "//<![CDATA[ |
| 107 |
e = document.getElementsByName( 's' ); |
| 108 |
for ( i = 0; i < e.length; i++ ) { |
| 109 |
if ( e[i].tagName.toUpperCase() == 'INPUT' ) { |
| 110 |
s = e[i].parentNode.parentNode.children; |
| 111 |
l = 0; |
| 112 |
for ( j = 0; j < s.length; j++ ) { |
| 113 |
if ( s[j].name == 'lang' ) { |
| 114 |
l = 1; |
| 115 |
} |
| 116 |
} |
| 117 |
if ( l == 0 ) { |
| 118 |
var ih = document.createElement( 'input' ); |
| 119 |
ih.type = 'hidden'; |
| 120 |
ih.name = 'lang'; |
| 121 |
ih.value = '$lang'; |
| 122 |
e[i].parentNode.appendChild( ih ); |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
//]]>"; |
| 127 |
echo '<script type="text/javascript">' . $js . '</script>'; |
| 128 |
} |
| 129 |
} |
| 130 |
|