Skip to content

Commit

Permalink
tidy up CMB2_Ajax.php
Browse files Browse the repository at this point in the history
  • Loading branch information
gregrickaby committed Aug 26, 2014
1 parent 805dc31 commit a91464d
Showing 1 changed file with 38 additions and 24 deletions.
62 changes: 38 additions & 24 deletions includes/CMB2_Ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,42 @@
*/
class CMB2_Ajax {


// Whether to hijack the oembed cache system
protected $hijack = false;
protected $object_id = 0;
protected $embed_args = array();
protected $object_type = 'post';
protected $ajax_update = false;


/**
* Handles our oEmbed ajax request
* @since 0.9.5
* @return object oEmbed embed code | fallback | error message
*/
public function oembed_handler() {

// verify our nonce
// Verify our nonce
if ( ! ( isset( $_REQUEST['cmb2_ajax_nonce'], $_REQUEST['oembed_url'] ) && wp_verify_nonce( $_REQUEST['cmb2_ajax_nonce'], 'ajax_nonce' ) ) ) {
die();
}

// sanitize our search string
// Sanitize our search string
$oembed_string = sanitize_text_field( $_REQUEST['oembed_url'] );

// send back error if empty
// Send back error if empty
if ( empty( $oembed_string ) ) {
wp_send_json_error( '<p class="ui-state-error-text">'. __( 'Please Try Again', 'cmb2' ) .'</p>' );
}

// Set width of embed
$embed_width = isset( $_REQUEST['oembed_width'] ) && intval( $_REQUEST['oembed_width'] ) < 640 ? intval( $_REQUEST['oembed_width'] ) : '640';

// set url
// Set url
$oembed_url = esc_url( $oembed_string );
// set args

// Set args
$embed_args = array( 'width' => $embed_width );

$this->ajax_update = true;
Expand All @@ -57,13 +60,15 @@ public function oembed_handler() {
wp_send_json_success( $html );
}


/**
* Retrieves oEmbed from url/object ID
* @since 0.9.5
* @param array $args Arguments for method
* @return string html markup with embed or fallback
*/
public function get_oembed( $args ) {

global $wp_embed;

$oembed_url = esc_url( $args['url'] );
Expand All @@ -80,52 +85,60 @@ public function get_oembed( $args ) {

$this->embed_args =& $args;

// set the post_ID so oEmbed won't fail
// wp-includes/class-wp-embed.php, WP_Embed::shortcode(), line 162

/**
* Set the post_ID so oEmbed won't fail
* wp-includes/class-wp-embed.php, WP_Embed::shortcode(), line 162
*/
$wp_embed->post_ID = $this->object_id;

// Special scenario if NOT a post object
if ( isset( $args['object_type'] ) && $args['object_type'] != 'post' ) {

if ( 'options-page' == $args['object_type'] ) {
// bogus id to pass some numeric checks
// Issue with a VERY large WP install?

// Bogus id to pass some numeric checks. Issue with a VERY large WP install?
$wp_embed->post_ID = 1987645321;

// Use our own cache key to correspond to this field (vs one cache key per url)
$args['cache_key'] = $args['field_id'] .'_cache';
$args['cache_key'] = $args['field_id'] . '_cache';
}

// Ok, we need to hijack the oembed cache system
$this->hijack = true;
$this->object_type = $args['object_type'];

// Gets ombed cache from our object's meta (vs postmeta)
add_filter( 'get_post_metadata', array( $this, 'hijack_oembed_cache_get' ), 10, 3 );

// Sets ombed cache in our object's meta (vs postmeta)
add_filter( 'update_post_metadata', array( $this, 'hijack_oembed_cache_set' ), 10, 4 );

}

$embed_args = '';

foreach ( $args['oembed_args'] as $key => $val ) {
$embed_args .= " $key=\"$val\"";
}

// ping WordPress for an embed
$check_embed = $wp_embed->run_shortcode( '[embed'. $embed_args .']'. $oembed_url .'[/embed]' );
// Ping WordPress for an embed
$check_embed = $wp_embed->run_shortcode( '[embed' . $embed_args . ']' . $oembed_url . '[/embed]' );

// fallback that WordPress creates when no oEmbed was found
// Fallback that WordPress creates when no oEmbed was found
$fallback = $wp_embed->maybe_make_link( $oembed_url );

// Send back our embed
if ( $check_embed && $check_embed != $fallback ) {
return '<div class="embed_status">'. $check_embed .'<p class="cmb2_remove_wrapper"><a href="#" class="cmb2_remove_file_button" rel="'. $args['field_id'] .'">'. __( 'Remove Embed', 'cmb2' ) .'</a></p></div>';
return '<div class="embed_status">' . $check_embed . '<p class="cmb2_remove_wrapper"><a href="#" class="cmb2_remove_file_button" rel="' . $args['field_id'] . '">' . __( 'Remove Embed', 'cmb2' ) . '</a></p></div>';
}

// Otherwise, send back error info that no oEmbeds were found
return '<p class="ui-state-error-text">'. sprintf( __( 'No oEmbed Results Found for %s. View more info at', 'cmb2' ), $fallback ) .' <a href="http://codex.wordpress.org/Embeds" target="_blank">codex.wordpress.org/Embeds</a>.</p>';
return '<p class="ui-state-error-text">' . sprintf( __( 'No oEmbed Results Found for %s. View more info at', 'cmb2' ), $fallback ) . ' <a href="http://codex.wordpress.org/Embeds" target="_blank">codex.wordpress.org/Embeds</a>.</p>';

}


/**
* Hijacks retrieving of cached oEmbed.
* Returns cached data from relevant object metadata (vs postmeta)
Expand All @@ -146,14 +159,15 @@ public function hijack_oembed_cache_get( $check, $object_id, $meta_key ) {
return false;
}

// get cached data
$data = 'options-page' === $this->object_type
// Get cached data
return ( 'options-page' === $this->object_type )
? cmb2_options( $this->object_id )->get( $this->embed_args['cache_key'] )
: get_metadata( $this->object_type, $this->object_id, $meta_key, true );

return $data;
}


/**
* Hijacks saving of cached oEmbed.
* Saves cached data to relevant object metadata (vs postmeta)
Expand All @@ -166,7 +180,8 @@ public function hijack_oembed_cache_get( $check, $object_id, $meta_key ) {
* @return boolean Whether to continue setting
*/
public function hijack_oembed_cache_set( $check, $object_id, $meta_key, $meta_value ) {
if ( ! $this->hijack || ( $this->object_id != $object_id && 1987645321 !== $object_id ) ){

if ( ! $this->hijack || ( $this->object_id != $object_id && 1987645321 !== $object_id ) ) {
return $check;
}

Expand All @@ -176,6 +191,7 @@ public function hijack_oembed_cache_set( $check, $object_id, $meta_key, $meta_va
return true;
}


/**
* Saves the cached oEmbed value to relevant object metadata (vs postmeta)
*
Expand All @@ -184,13 +200,11 @@ public function hijack_oembed_cache_set( $check, $object_id, $meta_key, $meta_va
* @param mixed $meta_value Value of the postmeta to be saved
*/
public function oembed_cache_set( $meta_key, $meta_value ) {

// Cache the result to our metadata
if ( 'options-page' !== $this->object_type ) {
update_metadata( $this->object_type, $this->object_id, $meta_key, $meta_value );
} else {
// or site option
cmb2_options( $this->object_id )->update( $this->embed_args['cache_key'], $meta_value, true );
}
return ( 'options-page' !== $this->object_type )
? update_metadata( $this->object_type, $this->object_id, $meta_key, $meta_value );
: cmb2_options( $this->object_id )->update( $this->embed_args['cache_key'], $meta_value, true );
}

}

0 comments on commit a91464d

Please sign in to comment.