WordPress.org

Making WordPress.org


Ignore:
Timestamp:
06/20/2016 05:34:18 PM (4 years ago)
Author:
obenland
Message:

Plugin Directory: Documentation and formatting updates.

See #1584.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-i18n.php

    r3468 r3511  
    1414    public $i18n_cache_group = 'plugins-i18n';
    1515
     16    /**
     17     * @var string
     18     */
    1619    public $master_project;
    1720
     21    /**
     22     * @static
     23     *
     24     * @var bool
     25     */
    1826    public static $use_cache = true;
     27
     28    /**
     29     * @static
     30     *
     31     * @var bool
     32     */
    1933    public static $set_cache = true;
    2034
    2135    /**
     36     * @access protected
     37     *
    2238     * @var \wpdb WordPress database abstraction object.
    2339     */
     
    2642    /**
    2743     * Fetch the instance of the Plugin_I18n class.
     44     *
     45     * @static
    2846     */
    2947    public static function instance() {
     
    3149
    3250        global $wpdb;
     51
    3352        return ! is_null( $instance ) ? $instance : $instance = new Plugin_I18n( $wpdb );
    3453    }
    3554
    36     function __construct( $db, $tracker = null ) {
    37         if ( !empty( $db ) && is_object( $db ) ) {
     55    /**
     56     * Plugin_I18n constructor.
     57     *
     58     * @access private
     59     *
     60     * @param \wpdb $db WordPress database abstraction object.
     61     * @param null $tracker
     62     */
     63    private function __construct( $db, $tracker = null ) {
     64        if ( ! empty( $db ) && is_object( $db ) ) {
    3865            $this->db = $db;
    3966        }
     
    4370
    4471    /**
    45      * Generates and returns a standard cache key format, for consistency
    46      *
    47      * @param string $slug Plugin slug
    48      * @param string $branch dev|stable
    49      * @param string $suffix Arbitrary cache key suffix, if needed for uniqueness
    50      *
     72     * Generates and returns a standard cache key format, for consistency.
     73     *
     74     * @param string $slug   Plugin slug
     75     * @param string $branch dev|stable
     76     * @param string $suffix Optional. Arbitrary cache key suffix, if needed for uniqueness.
    5177     * @return string Cache key
    5278     */
    53     function cache_key( $slug, $branch, $suffix = null ) {
    54         // EG keys
    55         // plugin:press-this:stable-readme:originals
    56         // plugin:press-this:stable-readme:original:title
    57         // plugin:press-this:stable-readme:fr:title
     79    public function cache_key( $slug, $branch, $suffix = '' ) {
     80
     81        /*
     82         * EG keys
     83         * plugin:press-this:stable-readme:originals
     84         * plugin:press-this:stable-readme:original:title
     85         * plugin:press-this:stable-readme:fr:title
     86         */
    5887        $key = "{$this->master_project}:{$slug}:{$branch}";
    59         if ( !empty( $suffix ) ) {
     88        if ( ! empty( $suffix ) ) {
    6089            $key .= ":{$suffix}";
    6190        }
     91
    6292        return $key;
    6393    }
    6494
    6595    /**
    66      * Cache getting, with proper global cache group
    67      *
    68      * @param string $slug Plugin slug
    69      * @param string $branch dev|stable
    70      * @param string $suffix Arbitrary cache key suffix, if needed for uniqueness
    71      *
     96     * Cache getting, with proper global cache group.
     97     *
     98     * @param string $slug   Plugin slug
     99     * @param string $branch dev|stable
     100     * @param string $suffix Optional. Arbitrary cache key suffix, if needed for uniqueness.
    72101     * @return bool|mixed As returned by wp_cache_set()
    73102     */
    74     function cache_get( $slug, $branch, $suffix = null ) {
     103    public function cache_get( $slug, $branch, $suffix = '' ) {
    75104        if ( ! self::$use_cache ) {
    76105            return false;
     
    78107
    79108        $key = $this->cache_key( $slug, $branch, $suffix );
     109
    80110        return wp_cache_get( $key, $this->i18n_cache_group );
    81111    }
     
    88118     * @param mixed  $content Content to be cached.
    89119     * @param string $suffix  Optional. Arbitrary cache key suffix, if needed for uniqueness.
    90      *
    91120     * @return bool As returned by wp_cache_set()
    92121     */
    93     function cache_set( $slug, $branch, $content, $suffix = null ) {
     122    public function cache_set( $slug, $branch, $content, $suffix = '' ) {
    94123        if ( ! self::$set_cache ) {
    95124            return false;
     
    97126
    98127        $key = $this->cache_key( $slug, $branch, $suffix );
     128
    99129        return wp_cache_set( $key, $content, $this->i18n_cache_group );
    100130    }
    101131
    102132    /**
    103      * Gets a GlotPress branch ID
    104      *
    105      * @param string $slug Plugin slug
     133     * Gets a GlotPress branch ID.
     134     *
     135     * @param string $slug   Plugin slug
    106136     * @param string $branch dev|stable
    107137     *
    108138     * @return bool|int|mixed
    109139     */
    110     function get_gp_branch_id( $slug, $branch ) {
     140    public function get_gp_branch_id( $slug, $branch ) {
    111141        $cache_suffix = "branch_id";
    112142
     
    130160
    131161    /**
    132      * Gets GlotPress "originals" based on passed parameters
    133      *
    134      * @param string $slug Plugin slug
    135      * @param string $branch dev|stable
    136      * @param string $key Unique key
    137      * @param string $str String to match in GP
    138      *
     162     * Gets GlotPress "originals" based on passed parameters.
     163     *
     164     * @param string $slug   Plugin slug
     165     * @param string $branch dev|stable
     166     * @param string $key    Unique key
     167     * @param string $str    String to match in GP
    139168     * @return array|bool|mixed|null
    140169     */
    141     function get_gp_originals( $slug, $branch, $key, $str ) {
     170    public function get_gp_originals( $slug, $branch, $key, $str ) {
     171
    142172        // Try to get a single original with the whole content first (title, etc), if passed, or get them all otherwise.
    143         if ( !empty( $key ) && !empty( $str ) ) {
     173        if ( ! empty( $key ) && ! empty( $str ) ) {
    144174            $originals = $this->search_gp_original( $slug, $branch, $key, $str );
    145             if ( !empty( $originals ) ) {
     175
     176            // Do not cache this as originals, search_gp_original() does its own caching.
     177            if ( ! empty( $originals ) ) {
    146178                return array( $originals );
    147179            }
    148             // Do not cache this as originals, search_gp_original() does its own caching
    149180        }
    150181
     
    167198
    168199        if ( empty( $originals ) ) {
    169             $originals = array(); // still cache if empty, but as array, never false
     200
     201            // Still cache if empty, but as array, never false.
     202            $originals = array();
    170203        }
    171204
     
    176209
    177210    /**
    178      * Get GlotPress translation set ID based on passed params
    179      *
    180      * @param string $slug Plugin slug
     211     * Get GlotPress translation set ID based on passed params.
     212     *
     213     * @param string $slug   Plugin slug
    181214     * @param string $branch dev|stable
    182215     * @param string $locale EG: fr
    183      *
    184216     * @return bool|int|mixed
    185217     */
    186     function get_gp_translation_set_id( $slug, $branch, $locale ) {
     218    public function get_gp_translation_set_id( $slug, $branch, $locale ) {
    187219        $cache_suffix = "{$locale}:translation_set_id";
    188220
     
    202234
    203235        if ( empty( $translation_set_id ) ) {
     236
    204237            // Don't give up yet. Might be given fr_FR, which actually exists as locale=fr in GP.
    205238            $translation_set_id = $this->db->get_var( $this->db->prepare(
    206239                'SELECT id FROM ' . GLOTPRESS_TABLE_PREFIX . 'translation_sets WHERE project_id = %d AND locale = %s',
    207                 $branch_id, preg_replace( '/^([^-]+)(-.+)?$/', '\1', $locale ) ) );
     240                $branch_id, preg_replace( '/^([^-]+)(-.+)?$/', '\1', $locale )
     241            ) );
    208242        }
    209243
     
    218252
    219253    /**
    220      * Searches GlotPress "originals" for the passed string
    221      *
    222      * @param string $slug Plugin slug
    223      * @param string $branch dev|stable
    224      * @param string $key Unique key
    225      * @param string $str String to be searched for
    226      *
     254     * Searches GlotPress "originals" for the passed string.
     255     *
     256     * @param string $slug   Plugin slug
     257     * @param string $branch dev|stable
     258     * @param string $key    Unique key
     259     * @param string $str    String to be searched for
    227260     * @return bool|mixed|null
    228261     */
    229     function search_gp_original( $slug, $branch, $key, $str ) {
     262    public function search_gp_original( $slug, $branch, $key, $str ) {
    230263        $cache_suffix = "original:{$key}";
    231264
     
    255288
    256289    /**
    257      * Somewhat emulated equivalent of __() for content translation drawn directly from the GlotPress DB
    258      *
    259      * @param string $key Unique key, used for caching
     290     * Somewhat emulated equivalent of __() for content translation drawn directly from the GlotPress DB.
     291     *
     292     * @param string $key     Unique key, used for caching
    260293     * @param string $content Content to be translated
    261      * @param array $args Misc arguments, such as BBPress topic id (otherwise acquired from global $topic_id)
    262      *
    263      * @return mixed
    264      */
    265     function translate( $key, $content, $args = array() ) {
     294     * @param array  $args    Optional. Misc arguments, such as BBPress topic id
     295     *                        (otherwise acquired from global $topic_id).
     296     * @return string
     297     */
     298    public function translate( $key, $content, $args = array() ) {
    266299        if ( empty( $key ) || empty( $content ) ) {
    267300            return $content;
     
    285318        }
    286319
    287         $server_name  = strtolower( $_SERVER['SERVER_NAME'] );
     320        $server_name = strtolower( $_SERVER['SERVER_NAME'] );
    288321        if ( 'api.wordpress.org' == $server_name ) {
    289             // Support formats like fr, haz, and en_GB
     322
     323            // Support formats like fr, haz, and en_GB.
    290324            if ( ! empty( $_REQUEST['locale'] ) ) {
    291325                $wp_locale = preg_replace( '/[^a-zA-Z_]/', '', $_REQUEST['locale'] );
     
    309343        }
    310344
    311         $locale = $gp_locale->slug; // The slug is the locale of a translation set.
    312         $slug = $post->post_name;
     345        // The slug is the locale of a translation set.
     346        $locale = $gp_locale->slug;
     347        $slug   = $post->post_name;
    313348
    314349        $post->stable_tag = get_post_meta( $post->ID, 'stable_tag', true );
     
    326361        $cache_suffix = "{$locale}:{$key}";
    327362
    328         // Try the cache
     363        // Try the cache.
    329364        if ( false !== ( $cache = $this->cache_get( $slug, $branch, $cache_suffix ) ) ) {
    330365            // DEBUG
     
    368403
    369404    /**
    370      * Takes content, searches for $original, and replaces it by $translation
    371      *
    372      * @param string $original English string
    373      * @param string $translation Translation
    374      * @param string $content Content to be searched
    375      *
     405     * Takes content, searches for $original, and replaces it by $translation.
     406     *
     407     * @param string $original    English string.
     408     * @param string $translation Translation.
     409     * @param string $content     Content to be searched.
    376410     * @return mixed
    377411     */
    378     function translate_gp_original( $original, $translation, $content ) {
     412    public function translate_gp_original( $original, $translation, $content ) {
    379413        if ( false === strpos( $content, '<' ) ) {
    380414            $content = str_replace( $original, $translation, $content );
    381415        } else {
    382416            $original = preg_quote( $original, '/' );
    383             $content = preg_replace( "/(<([a-z0-9]*)\b[^>]*>){$original}(<\/\\2>)/m", "\\1{$translation}\\3", $content );
     417            $content  = preg_replace( "/(<([a-z0-9]*)\b[^>]*>){$original}(<\/\\2>)/m", "\\1{$translation}\\3", $content );
    384418        }
    385419
Note: See TracChangeset for help on using the changeset viewer.