Making WordPress.org

Changeset 1371


Ignore:
Timestamp:
03/03/2015 11:51:49 PM (11 years ago)
Author:
iandunn
Message:

WordCamp Post Types: Make get_{speakers|sessions|organizers}_permalink DRY.

They were exactly the same except the for the shortcode value, so a single DRY method that handles all three cases is easier to maintain and less prone to errors.

Location:
sites/trunk/wordcamp.org/public_html/wp-content/plugins/wc-post-types
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wc-post-types/inc/widgets.php

    r758 r1371  
    149149        echo $widget_content;
    150150
    151         $speakers_permalink = $wcpt_plugin->get_speakers_permalink();
     151        $speakers_permalink = $wcpt_plugin->get_wcpt_permalink( 'speakers' );
    152152        if ( ! empty( $speakers_permalink ) )
    153153            printf( '<a class="wcpt-speakers-link" href="%s">%s</a>', esc_url( $speakers_permalink ), esc_html( __( 'View all speakers &rarr;', 'wordcamporg' ) ) );
     
    255255        echo $widget_content;
    256256
    257         $sessions_permalink = $wcpt_plugin->get_sessions_permalink();
     257        $sessions_permalink = $wcpt_plugin->get_wcpt_permalink( 'sessions' );
    258258        if ( ! empty( $sessions_permalink ) )
    259259            printf( '<a class="wcpt-sessions-link" href="%s">%s</a>', esc_url( $sessions_permalink ), esc_html( __( 'View all sessions &rarr;', 'wordcamporg' ) ) );
     
    356356        echo $widget_content;
    357357
    358         $organizers_permalink = $wcpt_plugin->get_organizers_permalink();
     358        $organizers_permalink = $wcpt_plugin->get_wcpt_permalink( 'organizers' );
    359359        if ( ! empty( $organizers_permalink ) )
    360360            printf( '<a class="wcpt-organizers-link" href="%s">%s</a>', esc_url( $organizers_permalink ), esc_html( __( 'Organizing team &rarr;', 'wordcamporg' ) ) );
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wc-post-types/wc-post-types.php

    r1340 r1371  
    88
    99class WordCamp_Post_Types_Plugin {
     10    protected $wcpt_permalinks;
    1011
    1112    /**
     
    1314     */
    1415    function __construct() {
     16        $this->wcpt_permalinks = array();
     17
    1518        add_action( 'init', array( $this, 'register_post_types' ) );
    1619        add_action( 'init', array( $this, 'register_taxonomies' ) );
     
    748751        switch( $post->post_type ) {
    749752            case 'wcb_speaker':
    750                 $permalink = $this->get_speakers_permalink();
     753                $permalink = $this->get_wcpt_permalink( 'speakers' );
    751754                $anchor_id = $post->post_name;
    752755                break;
    753756
    754757            case 'wcb_session':
    755                 $permalink = $this->get_sessions_permalink();
     758                $permalink = $this->get_wcpt_permalink( 'sessions' );
    756759                $anchor_id = $post->ID;
    757760                break;
     
    775778
    776779    /**
    777      * Returns the speakers page permalink
    778      *
    779      * Fetches for a post or page with the [speakers] shortcode and
     780     * Returns the page permalink for speakers, sessions or organizers
     781     *
     782     * Fetches for a post or page with the [speakers | sessions | organizers] shortcode and
    780783     * returns the permalink of whichever comes first.
    781      */
    782     function get_speakers_permalink() {
    783         // todo combine this, get_sessions_permalink, and get_organizers_permalink into a DRY function
    784        
    785         if ( isset( $this->speakers_permalink ) )
    786             return $this->speakers_permalink;
    787 
    788         $this->speakers_permalink = false;
    789 
    790         $speakers_post = get_posts( array(
    791             'post_type' => array( 'post', 'page' ),
    792             'post_status' => 'publish',
    793             's' => '[speakers',
     784     *
     785     * @param string $type
     786     *
     787     * @return false | string
     788     */
     789    function get_wcpt_permalink( $type ) {
     790        if ( ! in_array( $type, array( 'speakers', 'sessions', 'organizers' ) ) ) {
     791            return false;
     792        }
     793
     794        /*
     795         * The [schedule] shortcode can call this for each session and speaker, so cache the result to avoid
     796         * dozens of SQL queries.
     797         */
     798        if ( isset( $this->wcpt_permalinks[ $type ] ) ) {
     799            return $this->wcpt_permalinks[ $type ];
     800        }
     801
     802        $this->wcpt_permalinks[ $type ] = false;
     803
     804        $wcpt_post = get_posts( array(
     805            'post_type'      => array( 'post', 'page' ),
     806            'post_status'    => 'publish',
     807            's'              => '[' . $type,
    794808            'posts_per_page' => 1,
    795809        ) );
    796810
    797         if ( ! empty( $speakers_post ) )
    798             $this->speakers_permalink = get_permalink( $speakers_post[0] );
    799 
    800         return $this->speakers_permalink;
    801     }
    802 
    803     /**
    804      * Returns the sessions page permalink
    805      */
    806     function get_sessions_permalink() {
    807         if ( isset( $this->sessions_permalink ) )
    808             return $this->sessions_permalink;
    809 
    810         $this->sessions_permalink = false;
    811 
    812         $sessions_post = get_posts( array(
    813             'post_type' => array( 'post', 'page' ),
    814             'post_status' => 'publish',
    815             's' => '[sessions',
    816             'posts_per_page' => 1,
    817         ) );
    818 
    819         if ( ! empty( $sessions_post ) )
    820             $this->sessions_permalink = get_permalink( $sessions_post[0] );
    821 
    822         return $this->sessions_permalink;
    823     }
    824 
    825     /**
    826      * Returns the organizers page permalink
    827      */
    828     function get_organizers_permalink() {
    829         if ( isset( $this->organizers_permalink ) )
    830             return $this->organizers_permalink;
    831 
    832         $this->organizers_permalink = false;
    833 
    834         $organizers_post = get_posts( array(
    835             'post_type' => array( 'post', 'page' ),
    836             'post_status' => 'publish',
    837             's' => '[organizers',
    838             'posts_per_page' => 1,
    839         ) );
    840 
    841         if ( ! empty( $organizers_post ) )
    842             $this->organizers_permalink = get_permalink( $organizers_post[0] );
    843 
    844         return $this->organizers_permalink;
     811        if ( ! empty( $wcpt_post ) ) {
     812            $this->wcpt_permalinks[ $type ] = get_permalink( $wcpt_post[0] );
     813        }
     814
     815        return $this->wcpt_permalinks[ $type ];
    845816    }
    846817
Note: See TracChangeset for help on using the changeset viewer.