Making WordPress.org


Ignore:
Timestamp:
07/29/2016 07:34:47 PM (9 years ago)
Author:
iandunn
Message:

WordCamp Helpers: Add wcorg_get_url_part() for easy URL parsing.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/helper-functions.php

    r2888 r3749  
    147147
    148148/**
     149 * Extract pieces from a WordCamp.org URL
     150 *
     151 * @todo find other code that's doing this same task in an ad-hoc manner, and convert it to use this instead
     152 *
     153 * @param string $url
     154 * @param string $part 'city', 'city-domain' (without the year, e.g. seattle.wordcamp.org), 'year'
     155 *
     156 * @return false|string|int False on errors; an integer for years; a string for city and city-domain
     157 */
     158function wcorg_get_url_part( $url, $part ) {
     159    $url_parts = explode( '.', parse_url( $url, PHP_URL_HOST ) );
     160    $result    = false;
     161
     162    // Make sure it matches the typical year.city.wordcamp.org structure
     163    if ( 4 !== count( $url_parts ) ) {
     164        return $result;
     165    }
     166
     167    switch( $part ) {
     168        case 'city':
     169            $result = $url_parts[1];
     170            break;
     171
     172        case 'city-domain':
     173            $result = ltrim( strstr( $url, '.' ), '.' );
     174            break;
     175
     176        case 'year':
     177            $result = absint( $url_parts[0] );
     178            break;
     179    }
     180
     181    return $result;
     182}
     183
     184/**
    149185 * Get ISO-3166 country names and codes
    150186 *
Note: See TracChangeset for help on using the changeset viewer.