11<?php
22
33namespace WordCamp \Sunrise ;
4+ use WP_Network , WP_Site ;
5+ use WordCamp_Loader ;
6+
47defined ( 'WPINC ' ) || die ();
58
69// phpcs:disable WordPress.WP.AlternativeFunctions.parse_url_parse_url -- It's not available this early.
@@ -44,6 +47,82 @@ function main() {
4447 'path ' => $ path
4548 ) = guess_requested_domain_path ();
4649
50+ /*
51+ * @todo enable this when design is implemented.
52+ if ( is_flagship_landing_url( $domain, $path ) ) {
53+ if ( setup_flagship_landing_site( $domain ) ) {
54+ return;
55+ }
56+ }
57+ */
58+
59+ redirect_to_site ( $ domain , $ path );
60+ }
61+
62+ /**
63+ * Show the flagship landing page.
64+ */
65+ function setup_flagship_landing_site ( string $ domain ): bool {
66+ $ latest_site = get_latest_site ( $ domain );
67+
68+ if ( ! $ latest_site ) {
69+ return false ;
70+ }
71+
72+ set_network_and_site ( $ latest_site );
73+
74+ remove_action ( 'template_redirect ' , 'redirect_canonical ' );
75+
76+ add_filter (
77+ 'template ' ,
78+ function (): string {
79+ return 'wporg-parent-2021 ' ;
80+ }
81+ );
82+
83+ add_filter (
84+ 'stylesheet ' ,
85+ function (): string {
86+ return 'wporg-flagship-landing ' ;
87+ }
88+ );
89+
90+ add_filter (
91+ 'option_wccsp_settings ' ,
92+ function ( array $ settings ): array {
93+ $ settings ['enabled ' ] = 'off ' ;
94+
95+ return $ settings ;
96+ }
97+ );
98+
99+ return true ;
100+ }
101+
102+ /**
103+ * Set the current network and site when given a site.
104+ *
105+ * phpcs:disable WordPress.WP.GlobalVariablesOverride.Prohibited -- WP is designed in a way that requires this.
106+ * Setting these vars is what `sunrise.php` is designed to do.
107+ */
108+ function set_network_and_site ( object $ site ) {
109+ global $ current_site , $ current_blog , $ blog_id , $ site_id , $ domain , $ path , $ public ;
110+
111+ // Originally WP referred to networks as "sites" and sites as "blogs".
112+ $ current_site = WP_Network::get_instance ( WORDCAMP_NETWORK_ID );
113+ $ site_id = $ current_site ->id ;
114+ $ path = stripslashes ( $ _SERVER ['REQUEST_URI ' ] );
115+ $ current_blog = WP_Site::get_instance ( $ site ->blog_id );
116+
117+ $ blog_id = $ current_blog ->id ;
118+ $ domain = $ current_blog ->domain ;
119+ $ public = $ current_blog ->public ;
120+ }
121+
122+ /**
123+ * Redirect requests to the correct site.
124+ */
125+ function redirect_to_site ( string $ domain , string $ path ): void {
47126 add_action ( 'template_redirect ' , __NAMESPACE__ . '\redirect_duplicate_year_permalinks_to_post_slug ' );
48127
49128 $ status_code = 301 ;
@@ -131,6 +210,20 @@ function guess_requested_domain_path() {
131210 );
132211}
133212
213+ /**
214+ * Check if the given domain/path is a flagship landing page.
215+ */
216+ function is_flagship_landing_url ( string $ domain , string $ path ): bool {
217+ $ flagship_events = array ( 'asia ' , 'centroamerica ' , 'europe ' , 'us ' );
218+ $ third_level_domain = explode ( '. ' , $ domain )[0 ];
219+
220+ if ( in_array ( $ third_level_domain , $ flagship_events ) && '/ ' === $ path ) {
221+ return true ;
222+ }
223+
224+ return false ;
225+ }
226+
134227/**
135228 * Redirect root site front-end requests to Central.
136229 *
@@ -528,8 +621,19 @@ function get_canonical_year_url( $domain, $path ) {
528621 break ;
529622 }
530623
624+ $ latest = get_latest_site ( $ domain );
625+
626+ return $ latest ? 'https:// ' . $ latest ->domain . $ latest ->path : false ;
627+ }
628+
629+ /**
630+ * Get the latest site for a given city.
631+ */
632+ function get_latest_site ( string $ domain ) {
633+ global $ wpdb ;
634+
531635 $ latest = $ wpdb ->get_row ( $ wpdb ->prepare ( "
532- SELECT `domain`, `path`
636+ SELECT `blog_id`, ` domain`, `path`
533637 FROM $ wpdb ->blogs
534638 WHERE
535639 ( domain = %s AND path != '/' ) OR -- Match city/year format.
@@ -540,7 +644,7 @@ function get_canonical_year_url( $domain, $path ) {
540644 "%. {$ domain }"
541645 ) );
542646
543- return $ latest ? ' https:// ' . $ latest -> domain . $ latest -> path : false ;
647+ return $ latest ;
544648}
545649
546650/**
0 commit comments