Skip to content

Commit

Permalink
New oembed field
Browse files Browse the repository at this point in the history
  • Loading branch information
funkatron82 committed Mar 28, 2013
1 parent 304c5b4 commit 5e32d2e
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 1 deletion.
23 changes: 23 additions & 0 deletions demo/demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,29 @@
'value2' => 'Label2',
),
),
// EMAIL
array(
'name' => 'Email',
'id' => "{$prefix}email",
'desc' => 'email description',
'type' => 'email',
'std' => 'http://google.com',
),
// URL
array(
'name' => 'URL',
'id' => "{$prefix}url",
'desc' => 'url description',
'type' => 'url',
'std' => 'http://google.com',
),
// OEMBED
array(
'name' => 'oEmbed',
'id' => "{$prefix}oembed",
'desc' => 'oembed description',
'type' => 'oembed',
),
// TAXONOMY
array(
'name' => 'Taxonomy',
Expand Down
3 changes: 2 additions & 1 deletion inc/classes/meta-box.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,8 @@ static function normalize( $meta_box )
'format' => '',
'before' => '',
'after' => '',
'field_name' => $field['id']
'field_name' => $field['id'],
'required' => false
) );

// Allow field class add/change default field values
Expand Down
2 changes: 2 additions & 0 deletions inc/fields/image-advanced.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ static function wp_ajax_attach_media()
add_post_meta( $post_id, $field_id, $attachment_id, false );

RW_Meta_Box::ajax_response( self::img_html( $attachment_id ), 'success' );

exit;
}

/**
Expand Down
99 changes: 99 additions & 0 deletions inc/fields/oembed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
// Prevent loading this file directly
defined( 'ABSPATH' ) || exit;

// Make sure "text" field is loaded
require_once RWMB_FIELDS_DIR . 'url.php';

if ( ! class_exists( 'RWMB_OEmbed_Field' ) )
{
class RWMB_OEmbed_Field extends RWMB_URL_Field
{
/**
* Enqueue scripts and styles
*
* @return void
*/
static function admin_enqueue_scripts()
{
wp_enqueue_script( 'rwmb-oembed', RWMB_JS_URL . 'oembed.js', array( ), RWMB_VER, true );
wp_enqueue_style( 'rwmb-oembed', RWMB_CSS_URL . 'oembed.css', array( ), RWMB_VER );
wp_localize_script( 'rwmb-plupload-image', 'RWMB', array( 'url' => RWMB_URL ) );
}

/**
* Add actions
*
* @return void
*/
static function add_actions()
{
// Attach images via Ajax
add_action( 'wp_ajax_rwmb_get_embed', array( __CLASS__, 'wp_ajax_get_embed' ) );
}

/**
* Ajax callback for returning oEmbed HTML
*
* @return void
*/
static function wp_ajax_get_embed()
{
global $post;
$url = isset( $_POST['oembed_url'] ) ? $_POST['oembed_url'] : 0;
$post_id = is_numeric( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : 0;
if ( isset( $_REQUEST['post_id'] ) )
$post = get_post( $_REQUEST['post_id'] );
$embed = self::get_embed( $url );
RW_Meta_Box::ajax_response( $embed, 'success' );
exit;
}

/***
* Get embed html from url
* @param string $url
* $return string
*/

static function get_embed( $url )
{
global $wp_embed;

$embed = $wp_embed->run_shortcode( '[embed]' . esc_url( $url ) . '[/embed]' );

This comment has been minimized.

Copy link
@GwendolenLynch

GwendolenLynch Mar 29, 2013

Contributor

Would it be better to use wp_oembed_get() here?

This comment has been minimized.

Copy link
@GwendolenLynch

GwendolenLynch Mar 29, 2013

Contributor

Sorry, I should justify my thinking for those without ESP... It would make it easier to pass arguments such as height and width.

This comment has been minimized.

Copy link
@funkatron82

funkatron82 Mar 29, 2013

Author Contributor

Makes more sense, I agree


if( $embed )
{
return $embed;
}
else
{
return 'Embed not available.';
}

}

/**
* Get field HTML
*
* @param string $html
* @param mixed $meta
* @param array $field
*
* @return string
*/
static function html( $html, $meta, $field )
{
return sprintf(
'<input type="url" class="rwmb-oembed" name="%s" id="%s" value="%s" size="%s" />
<span class="spinner" style="display: none;"></span>
<a href="#" class="show-embed button-secondary">Show embed</a>
<div class="embed-code"> %s </div>',
$field['field_name'],
$field['id'],
$meta,
$field['size'],
self::get_embed( $meta )
);
}
}
}
28 changes: 28 additions & 0 deletions js/oembed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
jQuery( document ).ready( function( $ )
{
$( '.rwmb-input' ).on( 'click', 'a.show-embed', function() {
var $this = $( this ),
$input = $this.siblings( ':input.rwmb-oembed' );
$embed_container = $this.siblings( '.embed-code' ),
data = {
action : 'rwmb_get_embed',
oembed_url: $input.val(),
post_id : $( '#post_ID' ).val()
};
$embed_container.html( "<img class='rwmb-loader' height='64' width='64' src='" + RWMB.url + "img/loader.gif'/>" );
$.post( ajaxurl, data, function( r )
{
var res = wpAjax.parseAjaxResponse( r, 'ajax-response' );

if ( res.errors )
alert( res.responses[0].errors[0].message );
else
$embed_container.html( res.responses[0].data );

}, 'xml' );


return false;

});
} );

3 comments on commit 5e32d2e

@djevrek
Copy link

@djevrek djevrek commented on 5e32d2e Apr 4, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And you forgot .css file for this or is not needed ?

@funkatron82
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@djevrek nice catch. I was intending to add some css to tidy it up and completely forgot

@djevrek
Copy link

@djevrek djevrek commented on 5e32d2e Apr 4, 2013 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.