Last active
August 29, 2015 14:23
-
-
Save barryhughes/e50376648e9fd0088377 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Helper to let users comment on venues. | |
* | |
* - Discussion must be enabled via WP settings | |
* - Default page template is recommended | |
* - Success is, to a degree, theme dependent! | |
* | |
* To launch, call VenueCommments::enable(). Note that this was written against | |
* The Events Calendar/Events Calendar PRO 3.9.3 - in future releases this may | |
* not be required or different strategies may need to be employed. | |
*/ | |
class VenueComments { | |
protected $template = ''; | |
public static function enable() { | |
return new self(); | |
} | |
public function __construct() { | |
add_action( 'init', array( $this, 'setup' ), 20 ); | |
} | |
public function setup() { | |
if ( ! class_exists( 'TribeEvents' ) ) return; | |
add_post_type_support( TribeEvents::VENUE_POST_TYPE, 'comments' ); | |
add_action( 'wp_head', array( $this, 'on_venue_post' ) ); | |
} | |
public function on_venue_post() { | |
if ( ! is_singular( TribeEvents::VENUE_POST_TYPE ) ) return; | |
add_filter( 'comments_open', array( $this, 'restore_venue_object' ) ); | |
add_filter( 'comments_template', array( $this, 'capture' ), 5 ); | |
add_filter( 'comments_template', array( $this, 'restore' ), 50 ); | |
add_filter( 'comments_template', array( $this, 'restore' ), 500 ); // Double hooked deliberately! | |
} | |
public function capture( $path ) { | |
$this->template = $path; | |
} | |
public function restore( $path ) { | |
return empty( $this->template ) ? $path : $this->template; | |
} | |
public function restore_venue_object( $status ) { | |
$GLOBALS['post'] = get_post( tribe_get_venue_id() ); | |
return $status; | |
} | |
} | |
VenueComments::enable(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Notes:
comments_template
because TEC code unhooks a callback on the same action - a core WP bug means our owncomments_template
callback would otherwise be skipped