Make WordPress Core

Changes between Version 2 and Version 3 of Ticket #50787, comment 9


Ignore:
Timestamp:
10/15/2020 11:47:26 AM (4 years ago)
Author:
hellofromTonya
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #50787, comment 9

    v2 v3  
    1 Revising my performance note above.
     1Revising my performance note above. [https://wordpress.slack.com/archives/C02RQBWTW/p1602711273329800?thread_ts=1602708630.274400&cid=C02RQBWTW See slack conversation].
    22
    3 The last 3 string elements are dynamic, i.e. build from the injected `$name`. if no name is passed to the function, then we can skip building those last 3 elements. It's a tiny tiny performance boost. Pulling it all together, the function might look like this:
     3The last 3 string elements are dynamic, i.e. build from the injected `$name`. if no name is passed to the function, then we can skip building those last 3 elements.
    44
     5For the non-dynamic strings, we can leverage memoization to generate the `$strings` array once and then reuse the same array each time the function is invoked.
    56
    6 {{{
    7 /**
    8  * Return the requested compatibility string, when available.
    9  *
    10  * @since 5.6.0
    11  *
    12  * @param string $key  The key for the particular string.
    13  *                     Default is false.
    14  * @param string $name Plugin or theme name.
    15  *
    16  * @return string The appropriate compatibilty string.
    17  */
    18 function wp_get_compatibility_string( $key = false, $name = '' ) {
    19         static $strings = array();
    20 
    21         if ( empty( $strings ) ) {
    22                 $strings = array(
    23                         'theme_incompatible_wp_php'       => __( 'This theme doesn’t work with your versions of WordPress and PHP.' ),
    24                         'plugin_incompatible_wp_php'      => __( 'This plugin doesn’t work with your versions of WordPress and PHP.' ),
    25                         'core_update_incompatible_wp_php' => __( 'This update doesn’t work with your versions of WordPress and PHP.' ),
    26                         'theme_incompatible_wp'           => __( 'This theme doesn’t work with your version of WordPress.' ),
    27                         'plugin_incompatible_wp'          => __( 'This plugin doesn’t work with your version of WordPress.' ),
    28                         'core_update_incompatible_wp'     => __( 'This update doesn’t work with your version of WordPress.' ),
    29                         'theme_incompatible_php'          => __( 'This theme doesn’t work with your version of PHP.' ),
    30                         'plugin_incompatible_php'         => __( 'This plugin doesn’t work with your version of PHP.' ),
    31                         'core_update_incompatible_php'    => __( 'This update doesn’t work with your version of PHP.' ),
    32                 );
    33         }
    34 
    35         if ( array_key_exists( $key, $strings ) ) {
    36                 return $strings[ $key ];
    37         }
    38 
    39         if ( $name ) {
    40                 $names = array(
    41                         /* translators: 1: plugin or theme name */
    42                         'update_incompatible_wp_php' => sprintf( __( 'There is a new version of %s available, but it doesn’t work with your versions of WordPress and PHP.' ), $name ),
    43                         /* translators: 1: plugin or theme name */
    44                         'update_incompatible_wp'     => sprintf( __( 'There is a new version of %s available, but it doesn’t work with your version of WordPress.' ), $name ),
    45                         /* translators: 1: plugin or theme name */
    46                         'update_incompatible_php'    => sprintf( __( 'There is a new version of %s available, but it doesn’t work with your version of PHP.' ), $name ),
    47                 );
    48 
    49                 if ( array_key_exists( $key, $names ) ) {
    50                         return $names[ $key ];
    51                 }
    52         }
    53 
    54         return '';
    55 }
    56 }}}
     7It's a tiny tiny performance boost.