Making WordPress.org


Ignore:
Timestamp:
03/05/2024 02:45:07 AM (2 years ago)
Author:
adamwood
Message:

Support Theme: Redesign iteration 1

Replace wp4 with wporg-parent-2021
Redesign forums homepage
Reskin templates

File:
1 edited

Legend:

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

    r13168 r13272  
    1111 */
    1212add_filter( 'bbp_show_lead_topic', '__return_true' );
     13
     14/**
     15 * Provide a list of local navigation menus.
     16 */
     17function add_site_navigation_menus( $menus ) {
     18    return array(
     19        'forums' => array(
     20            array(
     21                'label' => __( 'Guidelines', 'wporg' ),
     22                'url' => '/guidelines/',
     23            ),
     24            array(
     25                'label' => __( 'Welcome to Support', 'wporg' ),
     26                'url' => '/welcome/',
     27            ),
     28            array(
     29                'label' => __( 'Get Involved', 'wporg' ),
     30                'url' => 'https://make.wordpress.org/support/handbook/',
     31            )
     32        ),
     33    );
     34}
     35add_filter( 'wporg_block_navigation_menus', '\add_site_navigation_menus' );
     36
     37/**
     38 * Register patterns from the patterns directory.
     39 */
     40function register_patterns() {
     41    $pattern_directory = new DirectoryIterator( get_template_directory() . '/patterns/' );
     42    foreach ( $pattern_directory as $file ) {
     43        if ( $file->isFile() ) {
     44            require $file->getPathname();
     45        }
     46    }
     47}
     48add_action( 'init', 'register_patterns' );
    1349
    1450/**
     
    3672 */
    3773function wporg_support_scripts() {
    38 
    39     wp_enqueue_style( 'forum-wp4-style', get_stylesheet_uri(), [ 'dashicons' ], filemtime( __DIR__ . '/style.css' ) );
    40     wp_style_add_data( 'forum-wp4-style', 'rtl', 'replace' );
     74    wp_dequeue_style( 'wp4-styles' );
     75
     76    wp_enqueue_style( 'wporg-parent-2021-style', get_theme_root_uri() . '/wporg-parent-2021/build/style.css', [ 'wporg-global-fonts' ] );
     77    wp_enqueue_style( 'wporg-parent-2021-block-styles', get_theme_root_uri() . '/wporg-parent-2021/build/block-styles.css', [ 'wporg-global-fonts' ] );
     78
     79    wp_enqueue_style( 'support-style', get_stylesheet_uri(), [ 'dashicons' ], filemtime( __DIR__ . '/style.css' ) );
     80    wp_style_add_data( 'support-style', 'rtl', 'replace' );
     81
     82    // Preload the heading font(s).
     83    if ( is_callable( 'global_fonts_preload' ) ) {
     84        /* translators: Subsets can be any of cyrillic, cyrillic-ext, greek, greek-ext, vietnamese, latin, latin-ext. */
     85        $subsets = _x( 'Latin', 'Heading font subsets, comma separated', 'wporg' );
     86        // All headings.
     87        global_fonts_preload( 'EB Garamond, Inter', $subsets );
     88    }
    4189
    4290    wp_enqueue_script( 'wporg-support-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '20181209', true );
     
    57105}
    58106add_action( 'wp_enqueue_scripts', 'wporg_support_scripts' );
     107
     108/**
     109 * Merge the support theme's theme.json into the parent theme.json.
     110 *
     111 * @param WP_Theme_JSON_Data $theme_json Parsed support theme.json.
     112 *
     113 * @return WP_Theme_JSON_Data The updated theme.json settings.
     114 */
     115function merge_parent_support_theme_json( $theme_json ) {
     116    $support_theme_json_data = $theme_json->get_data();
     117    $parent_theme_json_data = json_decode( file_get_contents( get_theme_root_uri() . '/wporg-parent-2021/theme.json' ), true );
     118
     119    if ( ! $parent_theme_json_data ) {
     120        return $theme_json;
     121    }
     122
     123    $parent_theme = class_exists( 'WP_Theme_JSON_Gutenberg' )
     124        ? new \WP_Theme_JSON_Gutenberg( $parent_theme_json_data )
     125        : new \WP_Theme_JSON( $parent_theme_json_data );
     126
     127    // Build a new theme.json object based on the parent.
     128    $new_data = $parent_theme_json_data;
     129    $support_settings = $support_theme_json_data['settings'];
     130    $support_styles = $support_theme_json_data['styles'];
     131
     132    if ( ! empty( $support_settings ) ) {
     133        $parent_settings = $parent_theme->get_settings();
     134
     135        $new_data['settings'] = _recursive_array_merge( $parent_settings, $support_settings );
     136    }
     137
     138    if ( ! empty( $support_styles ) ) {
     139        $parent_styles = $parent_theme_json_data['styles'];
     140
     141        $new_data['styles'] = _recursive_array_merge( $parent_styles, $support_styles );
     142    }
     143
     144    return $theme_json->update_with( $new_data );
     145
     146}
     147add_filter( 'wp_theme_json_data_theme', 'merge_parent_support_theme_json' );
     148
     149/**
     150 * Merge two arrays recursively, overwriting keys in the first array with keys from the second array.
     151 *
     152 * @param array $array1
     153 * @param array $array2
     154 *
     155 * @return array
     156 */
     157function _recursive_array_merge( $array1, $array2 ) {
     158    foreach ( $array2 as $key => $value ) {
     159        // If the key exists in the first array and both values are arrays, recursively merge them
     160        if ( isset( $array1[ $key ] ) && is_array( $value ) && is_array( $array1[ $key ] ) ) {
     161            // Check if both arrays are indexed (not associative)
     162            if ( array_values( $array1[ $key ] ) === $array1[ $key ] && array_values( $value ) === $value ) {
     163                // Use _merge_by_slug for indexed arrays
     164                $array1[ $key ] = _merge_by_slug( $array1[ $key ], $value );
     165            } else {
     166                // Use recursive merge for associative arrays
     167                $array1[ $key ] = _recursive_array_merge( $array1[ $key ], $value );
     168            }
     169        } else {
     170            $array1[ $key ] = $value;
     171        }
     172    }
     173
     174    return $array1;
     175}
     176
     177/**
     178 * Merge two (or more) arrays, de-duplicating by the `slug` key.
     179 *
     180 * If any values in later arrays have slugs matching earlier items, the earlier
     181 * items are overwritten with the later value.
     182 *
     183 * @param array ...$arrays A list of arrays of associative arrays, each item
     184 *                         must have a `slug` key.
     185 *
     186 * @return array The combined array, unique by `slug`. Empty if any item is
     187 *               missing a slug.
     188 */
     189function _merge_by_slug( ...$arrays ) {
     190    $combined = array_merge( ...$arrays );
     191    $result   = [];
     192
     193    foreach ( $combined as $value ) {
     194        if ( ! isset( $value['slug'] ) ) {
     195            return [];
     196        }
     197
     198        $found = array_search( $value['slug'], wp_list_pluck( $result, 'slug' ), true );
     199        if ( false !== $found ) {
     200            $result[ $found ] = $value;
     201        } else {
     202            $result[] = $value;
     203        }
     204    }
     205
     206    return $result;
     207}
    59208
    60209/**
     
    222371
    223372/**
     373 * Get a list of archive posts as card grid items.
     374 *
     375 * @return string
     376 */
     377function wporg_support_get_archive_posts() {
     378    $output = '';
     379
     380    while ( have_posts() ) : the_post();
     381
     382        $output .= sprintf(
     383            '<a id="post-%1$s" class="wp-block-wporg-link-wrapper is-layout-flow wp-block-wporg-link-wrapper-is-layout-flow" href="%2$s">
     384                <h2 class="wp-block-heading has-inter-font-family has-normal-font-size">%3$s</h2>
     385                <div>%4$s</div>
     386            </a>',
     387            get_the_ID(),
     388            esc_url( get_the_permalink() ),
     389            get_the_title(),
     390            get_the_excerpt(),
     391        );
     392
     393    endwhile;
     394
     395    return $output;
     396}
     397
     398/**
     399 * Get the list of forums for the front page.
     400 *
     401 * @return string
     402 */
     403function wporg_support_get_forums_list() {
     404    ob_start();
     405
     406    while ( bbp_forums() ) : bbp_the_forum();
     407
     408        bbp_get_template_part( 'loop', 'single-forum-homepage' );
     409
     410    endwhile;
     411
     412    return ob_get_clean();
     413}
     414
     415/**
    224416 * Display an ordered list of bbPress views
    225417 */
     
    232424    );
    233425
    234     $output = array();
     426    $output = '';
    235427
    236428    foreach ( $views as $view ) {
     
    239431        }
    240432
    241         $output[] = sprintf( '<li class="view"><a href="%s">%s</a></li>',
     433        $output .= sprintf( '<a class="wp-block-wporg-link-wrapper is-layout-flow wp-block-wporg-link-wrapper-is-layout-flow" href="%s"><h3 class="wp-block-heading has-inter-font-family has-normal-font-size">%s</h3></a>',
    242434            esc_url( bbp_get_view_url( $view ) ),
    243435            bbp_get_view_title( $view )
     
    245437    }
    246438
    247     echo implode( ' | ', $output );
     439    return $output;
    248440}
    249441
Note: See TracChangeset for help on using the changeset viewer.