PLL_Frontend_Filters_Search

Filters search forms when using permalinks.

Defined (1)

The class is defined in the following location(s).

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