Changeset 7867
- Timestamp:
- 11/16/2018 10:46:15 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordcamp.org/public_html/wp-content/plugins/wc-canonical-years/wc-canonical-years.php
r7866 r7867 1 1 <?php 2 2 3 /* 3 4 * Plugin Name: WordCamp.org Canonical Years … … 7 8 8 9 class WordCamp_Canonical_Years_Plugin { 9 function __construct() { 10 /** 11 * Constructor. 12 */ 13 public function __construct() { 10 14 add_action( 'init', array( $this, 'init' ) ); 11 15 } 12 16 13 function init() { 17 /** 18 * Initialize plugin 19 */ 20 public function init() { 14 21 // Only on the home page. 15 if ( isset( $_SERVER['REQUEST_URI'] ) && $_SERVER['REQUEST_URI'] == '/' )22 if ( isset( $_SERVER['REQUEST_URI'] ) && '/' === $_SERVER['REQUEST_URI'] ) { 16 23 add_action( 'wp_head', array( $this, 'wp_head' ), 9 ); 24 } 17 25 18 26 // WordPress will print rel=conanical for singular posts by default, we don't want that. … … 24 32 * WordCamp in the same city as the current site. 25 33 */ 26 function wp_head() {34 public function wp_head() { 27 35 global $wpdb; 28 36 29 37 $matches = array(); 30 38 31 // match xxxx.city.wordcamp.org32 if ( ! preg_match( '/^([0-9]{4})+\.((.+)\.wordcamp\.(lo|dev|test|org))$/i', $_SERVER['HTTP_HOST'], $matches ) ) 39 // Match `xxxx.city.wordcamp.org` pattern. 40 if ( ! preg_match( '/^([0-9]{4})+\.((.+)\.wordcamp\.(lo|dev|test|org))$/i', $_SERVER['HTTP_HOST'], $matches ) ) { 33 41 return; 42 } 34 43 35 44 $wordcamp = get_wordcamp_post(); … … 39 48 * In rare cases, the site for next year's camp will be created before this year's camp is over. When that 40 49 * happens, we should wait to add the canonical link until after the current year's camp is over. 50 * 51 * This won't prevent the link from being added to past years, but that edge case isn't significant enough 52 * to warrant the extra complexity. 41 53 */ 42 54 if ( $end_date && time() < ( (int) $end_date + DAY_IN_SECONDS ) ) { … … 45 57 46 58 $current_domain = $matches[0]; 47 $city_domain = $matches[2];59 $city_domain = $matches[2]; 48 60 49 61 $latest_domain = $wpdb->get_var( $wpdb->prepare( " … … 58 70 ) ); 59 71 60 if ( $latest_domain != $current_domain && $latest_domain ) 61 printf( '<link rel="canonical" href="%s" />' . "\n", trailingslashit( esc_url( $latest_domain ) ) ); 72 if ( $latest_domain !== $current_domain && $latest_domain ) { 73 printf( 74 '<link rel="canonical" href="%s" />' . "\n", 75 esc_url( trailingslashit( $latest_domain ) ) 76 ); 77 } 62 78 } 63 79 } 64 80 65 81 // Initialize the plugin. 66 new WordCamp_Canonical_Years_Plugin;82 $GLOBALS['wc_canonical_years'] = new WordCamp_Canonical_Years_Plugin();
Note: See TracChangeset
for help on using the changeset viewer.