Making WordPress.org


Ignore:
Timestamp:
02/12/2018 05:35:19 PM (8 years ago)
Author:
iandunn
Message:

WordCamp Helpers: Break wordcamp post type helpers into separate file.

File:
1 moved

Legend:

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

    r6598 r6599  
    11<?php
    22
     3defined( 'WPINC' ) or die();
    34use WordCamp\Logger;
    45
    5 /**
    6  * Retrieves the `wordcamp` post and postmeta associated with the current site.
    7  *
    8  * `Site ID` is the most reliable way to associate a site with it's corresponding `wordcamp` post,
    9  * but wasn't historically assigned when new sites are created. For older sites, we fallback to
    10  * using the `URL` to associate them. That will only work if the site's site_url() exactly
    11  * matches the `wordcamp` post's `URL` meta field, though. It could also fail if we ever migrate
    12  * to a different URL structure.
    13  *
    14  * @return false|WP_Post
     6/*
     7 * Miscellaneous helper functions.
    158 */
    16 function get_wordcamp_post() {
    17     $current_site_id  = get_current_blog_id();
    18     $current_site_url = site_url();
    199
    20     switch_to_blog( BLOG_ID_CURRENT_SITE ); // central.wordcamp.org
    21 
    22     $wordcamp = get_posts( array(
    23         'post_type'   => 'wordcamp',
    24         'post_status' => 'any',
    25 
    26         'meta_query' => array(
    27             'relation'  => 'OR',
    28 
    29             array(
    30                 'key'   => '_site_id',
    31                 'value' => $current_site_id,
    32             ),
    33 
    34             array(
    35                 'key'   => 'URL',
    36                 'value' => $current_site_url,
    37             ),
    38         ),
    39     ) );
    40 
    41     if ( isset( $wordcamp[0]->ID ) ) {
    42         $wordcamp = $wordcamp[0];
    43         $wordcamp->meta = get_post_custom( $wordcamp->ID );
    44     } else {
    45         $wordcamp = false;
    46     }
    47 
    48     restore_current_blog();
    49 
    50     return $wordcamp;
    51 }
    52 
    53 /**
    54  * Find the site that corresponds to the given `wordcamp` post
    55  *
    56  * @param WP_Post $wordcamp_post
    57  *
    58  * @return mixed An integer if successful, or boolean false if failed
    59  */
    60 function get_wordcamp_site_id( $wordcamp_post ) {
    61     switch_to_blog( BLOG_ID_CURRENT_SITE ); // central.wordcamp.org
    62 
    63     if ( ! $site_id = get_post_meta( $wordcamp_post->ID, '_site_id', true ) ) {
    64         $url = parse_url( get_post_meta( $wordcamp_post->ID, 'URL', true ) );
    65 
    66         if ( isset( $url['host'] ) && isset( $url['path'] ) ) {
    67             if ( $site = get_site_by_path( $url['host'], $url['path'] ) ) {
    68                 $site_id = $site->blog_id;
    69             }
    70         }
    71     }
    72 
    73     restore_current_blog();
    74 
    75     return $site_id;
    76 }
    77 
    78 /**
    79  * Get a consistent WordCamp name in the 'WordCamp [Location] [Year]' format.
    80  *
    81  * The results of bloginfo( 'name' ) don't always contain the year, but the title of the site's corresponding
    82  * `wordcamp` post is usually named 'WordCamp [Location]', so we can get a consistent name most of the time
    83  * by using that and adding the year (if available).
    84  *
    85  * @param int $site_id Optionally, get the name for a site other than the current one.
    86  *
    87  * @return string
    88  */
    89 function get_wordcamp_name( $site_id = 0 ) {
    90     $name = false;
    91 
    92     switch_to_blog( $site_id );
    93 
    94     if ( $wordcamp = get_wordcamp_post() ) {
    95         if ( ! empty( $wordcamp->meta['Start Date (YYYY-mm-dd)'][0] ) ) {
    96             $name = $wordcamp->post_title .' '. date( 'Y', $wordcamp->meta['Start Date (YYYY-mm-dd)'][0] );
    97         }
    98     }
    99 
    100     if ( ! $name ) {
    101         $name = get_bloginfo( 'name' );
    102     }
    103 
    104     restore_current_blog();
    105 
    106     return $name;
    107 }
    10810
    10911/**
     
    14648
    14749    return $user;
    148 }
    149 
    150 /**
    151  * Extract pieces from a WordCamp.org URL
    152  *
    153  * @todo find other code that's doing this same task in an ad-hoc manner, and convert it to use this instead
    154  *
    155  * @param string $url
    156  * @param string $part 'city', 'city-domain' (without the year, e.g. seattle.wordcamp.org), 'year'
    157  *
    158  * @return false|string|int False on errors; an integer for years; a string for city and city-domain
    159  */
    160 function wcorg_get_url_part( $url, $part ) {
    161     $url_parts = explode( '.', parse_url( $url, PHP_URL_HOST ) );
    162     $result    = false;
    163 
    164     // Make sure it matches the typical year.city.wordcamp.org structure
    165     if ( 4 !== count( $url_parts ) ) {
    166         return $result;
    167     }
    168 
    169     switch( $part ) {
    170         case 'city':
    171             $result = $url_parts[1];
    172             break;
    173 
    174         case 'city-domain':
    175             $result = ltrim( strstr( $url, '.' ), '.' );
    176             break;
    177 
    178         case 'year':
    179             $result = absint( $url_parts[0] );
    180             break;
    181     }
    182 
    183     return $result;
    18450}
    18551
     
    243109
    244110/**
    245  * Take the start and end dates for a WordCamp and calculate how many days it lasts.
    246  *
    247  * @param WP_Post $wordcamp
    248  *
    249  * @return int
    250  */
    251 function wcord_get_wordcamp_duration( WP_Post $wordcamp ) {
    252     // @todo Make sure $wordcamp is the correct post type
    253 
    254     $start = get_post_meta( $wordcamp->ID, 'Start Date (YYYY-mm-dd)', true );
    255     $end   = get_post_meta( $wordcamp->ID, 'End Date (YYYY-mm-dd)', true );
    256 
    257     // Assume 1 day duration if there is no end date
    258     if ( ! $end ) {
    259         return 1;
    260     }
    261 
    262     $duration_raw = $end - $start;
    263 
    264     // Add one second and round up to ensure the end date counts as a day as well
    265     $duration_days = ceil( ( $duration_raw + 1 ) / DAY_IN_SECONDS );
    266 
    267     return absint( $duration_days );
    268 }
    269 
    270 /**
    271111 * Register post meta so that it only appears on a specific REST API endpoint
    272112 *
Note: See TracChangeset for help on using the changeset viewer.