Making WordPress.org

Changeset 12252


Ignore:
Timestamp:
11/18/2022 01:48:31 AM (4 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.

Location:
sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-openverse
Files:
4 edited

Legend:

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

    r11559 r12252  
    11{
    2   "core": "WordPress/WordPress#master",
    32  "themes": [
    43    ".",
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-openverse/README.md

    r11559 r12252  
    990.  Install all the prerequisites.
    1010
    11     1.  **Required:** Node.js 14.
    12     2.  **Required:** Composer.
    13     3.  **Recommended:** Docker (to use the automatic setup)
     11    1.  **Required:** Node.js 14
     12    2.  **Required:** Composer
     13    3.  **Required:** Subversion
     14    4.  **Recommended:** Docker (to use the automatic setup)
    1415
    15161.  Build the parent theme WordPress.org theme.
     
    1920    2.  Install all the required `npm` packages.
    2021        ```bash
     22        $ npm install
     23        ```
     24        If you face issues installing Sass, try the following command, and
     25        `npm install` again.
     26        ```bash
     27        $ npm install node-sass@npm:sass
    2128        $ npm install
    2229        ```
     
    7784        `wporg-openverse` (child) themes installed. For detailed instructions,
    7885        please read [the wp-env docs](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-env/).
     86    4.  Edit the `.htaccess` file to prevent Apache 404 errors.
     87        ```bash
     88        $ wp-env run cli bash
     89        bash-5.1$ printf "RewriteEngine on\nFallbackResource /index.php\n" > .htaccess
     90        bash-5.1$ exit
     91        ```
    7992
    8093    **Manual:** 
     
    971106.  Activate and customize the theme.
    98111
    99     1.  Log into `/wp-admin`.
     112    1.  Log into `/wp-admin`. If you used `@wordpress/env`, your username will
     113        be 'admin' and password will be 'password'.
    100114    2.  Under Appearance > Themes, activate the theme 'WordPress.org Openverse'.
    101115    3.  To change the embed URL, open the customizer at Appearance > Customize
     
    108122    2.  Visit the site and interactively test the messages.
    109123
     1248. Test redirects.
     125
     126    1.  Visit some test URLs.
     127        1.  https://ru.wordpress.org/openverse → ...
     128        2.  https://wordpress.org/openverse/search/?q=dog → ...
     129    2.  See the target redirect URL as a comment inside the dev tools.
     130        1.  ... → https://openverse.wordpress.net/ru/
     131        2.  ... → https://openverse.wordpress.net/search/?q=dog
     132    3.  Change the language in Settings > General to see how the locale factors
     133        into the redirect path.
     134
    110135## Related links
    111136
  • 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' );
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-openverse/index.php

    r11217 r12252  
    1515namespace WordPressdotorg\Openverse\Theme;
    1616
     17/*
     18        If the theme mod `ov_is_redirect_enabled` is set to `true`, redirect to the
     19        standalone site and exit immediately. If not, print what would have been the
     20        redirect URL to the HTML as a comment.
     21 */
     22
     23$is_redirect_enabled = get_theme_mod( 'ov_is_redirect_enabled' );
     24$target_url = get_target_url();
     25
     26if ( $is_redirect_enabled ) {
     27        wp_redirect( $target_url );
     28        exit;
     29} else {
     30        echo "<!-- " . $target_url . " -->";
     31}
     32
    1733get_header();
    1834?>
Note: See TracChangeset for help on using the changeset viewer.