Making WordPress.org


Ignore:
Timestamp:
03/13/2015 11:55:01 PM (10 years ago)
Author:
iandunn
Message:

Central Theme: Display WordCamp stats programatically with [wcc_about_stats].

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/themes/wordcamp-central-2012/functions.php

    r1144 r1400  
    4343        add_filter( 'nav_menu_css_class', array( __CLASS__, 'nav_menu_css_class' ), 10, 3 );
    4444        add_filter( 'wp_nav_menu_items', array( __CLASS__, 'add_rss_links_to_footer_menu' ), 10, 2 );
     45
     46        add_shortcode( 'wcc_about_stats', array( __CLASS__, 'shortcode_about_stats' ) );
    4547    }
    4648
     
    645647        return $url;
    646648    }
     649
     650    /**
     651     * Render the [wcc_about_stats] shortcode
     652     *
     653     * @param array $attributes
     654     *
     655     * @return string
     656     */
     657    public static function shortcode_about_stats( $attributes ) {
     658        $map_stats = self::get_map_stats();
     659
     660        ob_start();
     661        require( __DIR__ . '/shortcode-about-stats.php' );
     662        return ob_get_clean();
     663    }
     664
     665    /**
     666     * Gather the stats for the [wcc_about_stats] shortcode
     667     *
     668     * There isn't an easy way to collect the country stats programmatically, but it just takes a minute to
     669     * manually count the number countries on the map that have pins.
     670     *
     671     * @return array
     672     */
     673    protected static function get_map_stats() {
     674        $transient_key = 'wcc_about_map_stats';
     675
     676        if ( ! $map_stats = get_transient( $transient_key ) ) {
     677            $cities    = array();
     678            $wordcamps = new WP_Query( array(
     679                'post_type'      => 'wordcamp',
     680                'posts_per_page' => -1,
     681            ) );
     682
     683            // Count the number of cities
     684            foreach ( $wordcamps->posts as $wordcamp ) {
     685                $url = get_post_meta( $wordcamp->ID, 'URL', true );
     686
     687                if ( $hostname = parse_url( $url, PHP_URL_HOST ) ) {
     688                    $city = explode( '.', $hostname );
     689                    $cities[ $city[0] ] = true;
     690                }
     691            }
     692
     693            // Compile the results
     694            $map_stats = array(
     695                'wordcamps'  => $wordcamps->found_posts,
     696                'cities'     => count( $cities ),
     697                'countries'  => 48,
     698                'continents' => 6,
     699            );
     700
     701            // todo set_transient( $transient_key, $map_stats, 2 * WEEK_IN_DAYS );
     702        }
     703
     704        return $map_stats;
     705    }
    647706}
    648707
Note: See TracChangeset for help on using the changeset viewer.