Making WordPress.org


Ignore:
Timestamp:
05/21/2017 01:51:02 PM (9 years ago)
Author:
ocean90
Message:

Translate: Avoid PHP notices if a translation set doesn't exist.

Also add a few missing esc_url() calls and a custom 404 template with a link back to the homepage.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-routes/inc/routes/class-locale.php

    r4996 r5502  
    1717class Locale extends GP_Route {
    1818
     19        private $cache_group = 'wporg-translate';
     20
    1921        /**
    2022         * Adapter for the rosetta roles plugin.
    … …  
    4951                $locale = GP_Locales::by_slug( $locale_slug );
    5052                if ( ! $locale ) {
     53                        return $this->die_with_404();
     54                }
     55
     56                if ( ! $this->translation_set_slug_exists( $locale, $set_slug ) ) {
    5157                        return $this->die_with_404();
    5258                }
    … …  
    8894                }
    8995
    90 
    9196                $paged_sub_projects = $this->get_paged_active_sub_projects(
    9297                        $project,
    … …  
    136141                $can_create_locale_glossary = GP::$permission->current_user_can( 'admin' );
    137142                $locale_glossary_translation_set = GP::$translation_set->by_project_id_slug_and_locale( 0, $set_slug, $locale_slug );
    138                 $locale_glossary = GP::$glossary->by_set_id( $locale_glossary_translation_set->id );
     143                $locale_glossary = false;
     144                if ( $locale_glossary_translation_set ) {
     145                        $locale_glossary = GP::$glossary->by_set_id( $locale_glossary_translation_set->id );
     146                }
    139147
    140148                $this->tmpl( 'locale-projects', get_defined_vars() );
    … …  
    152160                $locale = GP_Locales::by_slug( $locale_slug );
    153161                if ( ! $locale ) {
     162                        return $this->die_with_404();
     163                }
     164
     165                if ( ! $this->translation_set_slug_exists( $locale, $set_slug ) ) {
    154166                        return $this->die_with_404();
    155167                }
    … …  
    198210
    199211                $this->tmpl( 'locale-project', get_defined_vars() );
     212        }
     213
     214        /**
     215         * Whether a translation set slug exists for a locale.
     216         *
     217         * @param \GP_Locale $locale The locale.
     218         * @param string     $slug   The slug of a translation set.
     219         * @return bool True if slug exists, false if not.
     220         */
     221        private function translation_set_slug_exists( $locale, $slug ) {
     222                $cache_key = "translation_set_slugs:{$locale->slug}";
     223                $slugs = wp_cache_get( $cache_key, $this->cache_group );
     224
     225                if ( false === $slugs ) {
     226                        global $wpdb;
     227                        $slugs = $wpdb->get_col( $wpdb->prepare(
     228                                "SELECT DISTINCT(slug) FROM {$wpdb->gp_translation_sets} WHERE locale = %s",
     229                                $locale->slug
     230                        ) );
     231
     232                        wp_cache_set( $cache_key, $slugs, $this->cache_group, DAY_IN_SECONDS );
     233                }
     234
     235                return in_array( $slug, $slugs, true );
    200236        }
    201237
Note: See TracChangeset for help on using the changeset viewer.