Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement CMB2_Ajax as a singleton. #602

Merged
merged 1 commit into from
Mar 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 23 additions & 17 deletions includes/CMB2_Ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,34 @@ class CMB2_Ajax {
protected $ajax_update = false;

/**
* Constructor
* @since 2.2.0
* Instance of this class
* @since 2.2.2
* @var object
*/
public function __construct() {
self::hooks( $this );
}
protected static $instance;

/**
* Hook in the oembed ajax handlers
* @since 2.2.0
* @param CMB2_Ajax $self This object (for hooking)
* Get the singleton instance of this class
* @since 2.2.2
* @return object
*/
public static function hooks( $self ) {
static $hooked = false;

if ( ! $hooked ) {
add_action( 'wp_ajax_cmb2_oembed_handler', array( $self, 'oembed_handler' ) );
add_action( 'wp_ajax_nopriv_cmb2_oembed_handler', array( $self, 'oembed_handler' ) );
// Need to occasionally clean stale oembed cache data from the option value.
add_action( 'cmb2_save_options-page_fields', array( __CLASS__, 'clean_stale_options_page_oembeds' ) );
$hooked = true;
public static function get_instance() {
if ( ! ( self::$instance instanceof self ) ) {
self::$instance = new self();
}

return self::$instance;
}

/**
* Constructor
* @since 2.2.0
*/
protected function __construct() {
add_action( 'wp_ajax_cmb2_oembed_handler', array( $this, 'oembed_handler' ) );
add_action( 'wp_ajax_nopriv_cmb2_oembed_handler', array( $this, 'oembed_handler' ) );
// Need to occasionally clean stale oembed cache data from the option value.
add_action( 'cmb2_save_options-page_fields', array( __CLASS__, 'clean_stale_options_page_oembeds' ) );
}

/**
Expand Down
4 changes: 1 addition & 3 deletions includes/helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ function cmb2_utils() {
* @return CMB2_Ajax object CMB2 utilities class
*/
function cmb2_ajax() {
static $cmb2_ajax;
$cmb2_ajax = $cmb2_ajax ? $cmb2_ajax : new CMB2_Ajax();
return $cmb2_ajax;
return CMB2_Ajax::get_instance();
}

/**
Expand Down