Skip to content

Commit

Permalink
Main: Add a WordPress.org/Search page template.
Browse files Browse the repository at this point in the history
See WordPress/wporg-mu-plugins#38


git-svn-id: https://meta.svn.wordpress.org/sites/trunk@11427 74240141-8908-4e6f-9713-ba540dce6ec7
  • Loading branch information
dd32 committed Jan 11, 2022
1 parent 3886f43 commit e782a5f
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
exit;

// WordPress.org does not have a specific site search, only the global WordPress.org search
} elseif ( is_search() ) {
wp_safe_redirect( '/search/' . urlencode( get_query_var('s') ), 301 );
} elseif ( ! empty( $_GET['s'] ) && false === strpos( $_SERVER['REQUEST_URI'], '/search/' ) ) {
wp_safe_redirect( '/search/' . urlencode( wp_unslash( $_GET['s'] ) ) . '/', 301 );
exit;

} elseif ( is_404() ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
@import "page-hosting";
@import "page-logos";
@import "page-mobile";
@import "page-search";
@import "page-stats";
@import "widget-download";
@import "widget-links";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
body.page-search {
table,
table td {
border: none;
padding: 0;
margin: 0;
}

main {
min-height: 30em;
}

input.gsc-input {
box-shadow: none;
}

.entry-content.google-custom-search {
padding: 0;

* {
box-sizing: initial;
}
}

}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ function custom_open_graph_tags( $tags = [] ) {
return $tags;
}

// These values are not correct for our page templates.
unset( $tags['article:published_time'], $tags['article:modified_time'] );

switch ( $post->page_template ) {
default:
return $tags;
Expand Down Expand Up @@ -185,6 +188,12 @@ function custom_open_graph_tags( $tags = [] ) {
$title = esc_html_x( 'WordPress Mobile Apps', 'Page title', 'wporg' );
$desc = esc_html__( 'Manage your site with our Android, iOS, and desktop apps', 'wporg' );
break;

case 'page-search.php':
// Intentionally not internationalised as these are currently english only.
$title = 'Search';
$desc = 'Search the WordPress.org website for plugins, themes, and support.';
break;
}

$tags['og:title'] = $title;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

/**
* Template Name: Global Search
*
* @package WordPressdotorg\MainTheme
*/

namespace WordPressdotorg\MainTheme;

// Prevent Jetpack from looking for a non-existent featured image.
add_filter( 'jetpack_images_pre_get_images', function() {
return new \WP_Error();
} );

/* See inc/page-meta-descriptions.php for the meta description for this page. */

wp_enqueue_script( 'jquery' );

get_header( 'top-level-page' );
the_post();

$terms = urldecode( wp_unslash( $_GET['s'] ) );
$terms = htmlspecialchars_decode( $terms );
$terms = explode( '?', $terms )[0];
$terms = trim( $terms, "/ \r\n\t" );

$search_config = array(
'div' => 'gsce-search',
'gname' => 'wordpressorg-search',
'attributes' => array(
'queryParameterName' => 'search',
'linkTarget' => '_parent',
'enableHistory' => false,
'enableOrderBy' => true,
),
);

if ( isset( $_REQUEST['in'] ) && in_array( $_REQUEST['in'], [ 'support_forums', 'support_docs', 'developer_documentation' ] ) ) {
$search_config['attributes']['defaultToRefinement'] = $_REQUEST['in'];
}

?>

<main id="main" class="site-main col-12" role="main">
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div id="gsce-search" class="entry-content col-12 google-custom-search">
<p>Loading..</p>
</div>
</article>
</main>

<script>
window.__gcse = {
parsetags: 'explicit',
callback: function() {
var executeSearch = function() {
document.getElementById( 'gsce-search' ).innerHTML = '';
google.search.cse.element.render(<?php echo json_encode( $search_config, true ); ?>);
google.search.cse.element.getElement('wordpressorg-search').execute( <?php echo json_encode( $terms ); ?> );
}

if ( document.readyState == 'complete' ) {
executeSearch();
} else {
google.setOnLoadCallback(function() {
executeSearch();
}, true);
}

},
searchCallbacks: {
web: {
starting: function( gname, searchTerm ) {
wporg_search_update_url( searchTerm, jQuery('.gsc-refinementBlock .gsc-tabhActive') );
},
rendered: function( gname, searchTerm ) {
jQuery('.gsc-refinementBlock .gsc-tabHeader')
.off( 'click.refinement, keypress.refinement' )
.on( 'click.refinement, keypress.refinement', function() {
wporg_search_update_url( searchTerm, jQuery( this ) );
} );
}
}
}
};

function wporg_search_update_url( term, refinement_obj = false ) {
var refinement = 'all',
state;
if ( refinement_obj && refinement_obj.length ) {
refinement = refinement_obj.text().trim().toLowerCase().replace( /[^a-z]/g, '_' );
}
if ( 'all' === refinement ) {
refinement = '';
}

state = {
'search': term,
'refinement': refinement
};

if (
! window.history.state ||
window.history.state.search != state.search ||
window.history.state.refinement != state.refinement
) {
wporg_record_search( term, refinement );
}

window.history.replaceState(
state,
document.title,
'/search/' +
encodeURIComponent( term ).replace( /%20/g, '+' ) + '/' +
( refinement ? '?in=' + refinement : '' )
);
}

function wporg_record_search( term, refinement ) {
jQuery.post(
'https://api.wordpress.org/search/1.0/',
{
term: term,
in: refinement
}
);
}

(function() {
var cx = '012566942813864066925:bnbfebp99hs';
var gcse = document.createElement('script'); gcse.type = 'text/javascript'; gcse.async = true;
gcse.src = 'https://cse.google.com/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(gcse, s);
})();
</script>

<?php

get_footer();

0 comments on commit e782a5f

Please sign in to comment.