Making WordPress.org


Ignore:
Timestamp:
11/18/2022 01:48:31 AM (2 years ago)
Author:
dd32
Message:

Openverse: Prepare for a migration to a standalone Openverse domain.

Props dhruvkb, zackkrida, sarayourfriend, olgabulat.
Closes https://github.com/WordPress/wordpress.org/pull/98.
Fixes #6578.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-openverse/functions.php

    r11592 r12252  
    1818 * This is subdirectory on WordPress.org which loads the Openverse site. This is
    1919 * prefixed in front of all path changes sent by the embedded `iframe`.
     20 *
     21 * When used with the standalone redirect, it is removed from the path forwarded
     22 * to the standalone site.
    2023 */
    2124if ( !defined( 'OPENVERSE_SUBPATH' ) ) {
     
    8386
    8487/**
     88 * Get the slug of the given WP locale. This function returns a blank string if
     89 * the locale is `en_US` because that is considered the default and is not
     90 * prefixed in the URL paths. It also returns a blank string if `$wp_locale` is
     91 * not found in the locales list.
     92 */
     93function get_locale_slug( $curr_locale ) {
     94    if ( $curr_locale === 'en_US' ) {
     95        return '';
     96    }
     97
     98    return get_locales()[ $curr_locale ]->slug ?? '';
     99}
     100
     101/**
    85102 * Enqueue styles & scripts.
    86103 *
     
    106123
    107124    $use_path_based_locale_forwarding = get_theme_mod( 'ov_path_based_i18n', false );
    108     $wp_locale = get_locale();
    109     $locale = get_locales()[ $wp_locale ];
    110     $locale_slug = $use_path_based_locale_forwarding && $wp_locale !== 'en_US' ? $locale->slug : '';
     125    $curr_locale = get_locale();
     126    $locale_slug = '';
     127    if ( $use_path_based_locale_forwarding ) {
     128        $locale_slug = get_locale_slug( $curr_locale );
     129    }
    111130
    112131    wp_add_inline_script(
     
    114133        /* JS       */ 'const openverseUrl = ' . wp_json_encode( get_theme_mod( 'ov_src_url', OPENVERSE_URL ) ) . ";\n" .
    115134        /* JS       */ 'const openverseSubpath = ' . wp_json_encode( OPENVERSE_SUBPATH ) . ";\n" .
    116         /* JS       */ 'const currentLocale = ' . wp_json_encode( $wp_locale ) . ";\n" . /* Used for legacy cookie based locale forwarding */
     135        /* JS       */ 'const currentLocale = ' . wp_json_encode( $curr_locale ) . ";\n" . /* Used for legacy cookie based locale forwarding */
    117136        /* JS       */ 'const localeSlug = ' . wp_json_encode( $locale_slug ) . ";\n",
    118137        /* position */ 'before'
     
    151170}
    152171add_filter( 'document_title_parts', __NAMESPACE__ . '\title_no_title' );
     172
     173/*
     174    TODO: Delete this and everything related to it
     175    ======================
     176    Openverse iframe embed
     177    ======================
     178 */
    153179
    154180/**
     
    204230}
    205231add_action( 'customize_register', __NAMESPACE__ . '\wporg_ov_customizer' );
     232
     233/*
     234    =====================================
     235    Openverse standalone site redirection
     236    =====================================
     237 */
     238
     239/**
     240 * This is the URL at which the standalone Openverse site is hosted. When
     241 * redirect is enabled (see setting `ov_is_redirect_enabled`), the theme
     242 * redirects all incoming requests to the right URL on this domain.
     243 *
     244 * Note: Do not put a trailing slash '/' in this URL. Paths start with a leading
     245 * slash so a trailing slash here will lead to two slashes in the final URL.
     246 */
     247if ( !defined( 'OPENVERSE_STANDALONE_URL' ) ) {
     248    define( 'OPENVERSE_STANDALONE_URL', 'https://openverse.wordpress.net' );
     249}
     250
     251/**
     252 * Determine the target URL of the redirect based on the Openverse standalone
     253 * URL, the requested path and the current locale.
     254 *
     255 * Examples:
     256 * - https://ru.wordpress.org/openverse → {ov_redirect_url}/ru/
     257 * - https://wordpress.org/openverse/search/?q=dog → {ov_redirect_url}/search/?q=dog
     258 */
     259function get_target_url() {
     260    $target_url = get_theme_mod( 'ov_redirect_url', OPENVERSE_STANDALONE_URL );
     261
     262    $curr_locale = get_locale();
     263    $locale = get_locale_slug( $curr_locale );
     264    if ( $locale !== '' ) {
     265        $target_url .= '/' . $locale;
     266    }
     267
     268    $path = $_SERVER['REQUEST_URI'];
     269    if ( $path ) {
     270        $count = 1; // Only replace the leading Openverse subpath.
     271        $target_url .= str_replace( OPENVERSE_SUBPATH, '', $path, $count );
     272    }
     273
     274    return $target_url;
     275}
     276
     277/**
     278* Provide configuration for the theme to redirect to the given standalone
     279* Openverse site. The destination URL can be configured and the behaviour can
     280* be dormant unless enabled.
     281*
     282* @param \WP_Customize_Manager $wp_customize Theme Customizer object.
     283*/
     284function wporg_ov_redir_customizer( $wp_customize ) {
     285    $wp_customize->add_section( 'ov_redir', array(
     286        'priority' => 10,
     287        'capability' => 'edit_theme_options',
     288        'title' => 'Openverse Redirect',
     289        'description' => 'Configure the redirection to the standalone Openverse site.'
     290    ) );
     291
     292    $wp_customize->add_setting( 'ov_redirect_url', array(
     293        'type' => 'theme_mod',
     294        'capability' => 'edit_theme_options',
     295        'default' => OPENVERSE_STANDALONE_URL,
     296        'sanitize_callback' => function( $val, $setting ) {
     297            if ( substr( $val, -1 ) == '/' ) { // If the last character is a slash '/',...
     298                $val = substr( $val, 0, -1 );    // ...remove it.
     299            }
     300            if ( empty( $val ) ) {
     301                return $setting->default;
     302            }
     303            return $val;
     304        }
     305    ) );
     306
     307    $wp_customize->add_control( 'ov_redirect_url', array(
     308        'section' => 'ov_redir',
     309        'type' => 'url',
     310        'id' => 'ov_redirect_url',
     311        'label' => 'Redirect URL',
     312        'description' => '<b>Note</b>: '
     313                                    . 'Do not put a trailing slash \'/\' in this URL.<br/>'
     314                                    . '<b>Default</b>: '
     315                                    . esc_html( OPENVERSE_STANDALONE_URL ),
     316        'priority' => 10,
     317        'input_attrs' => array(
     318            'placeholder' => 'URL'
     319        )
     320    ) );
     321
     322    $wp_customize->add_setting( 'ov_is_redirect_enabled', array(
     323        'type' => 'theme_mod',
     324        'capability' => 'edit_theme_options',
     325        'default' => false
     326    ) );
     327
     328    $wp_customize->add_control( 'ov_is_redirect_enabled', array(
     329        'section' => 'ov_redir',
     330        'type' => 'checkbox',
     331        'id' => 'ov_is_redirect_enabled',
     332        'label' => 'Redirect to the standalone Openverse site.',
     333        'priority' => 10
     334    ) );
     335}
     336add_action( 'customize_register', __NAMESPACE__ . '\wporg_ov_redir_customizer' );
Note: See TracChangeset for help on using the changeset viewer.