Created
January 8, 2012 23:28
-
-
Save ocean90/1580098 to your computer and use it in GitHub Desktop.
Gist to WordPress
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
/** | |
* Hook into the backend and load scripts and | |
* init metabox. | |
*/ | |
function ds_gist2wordpress_admin() { | |
add_action( 'wp_ajax_gist', 'ds_gistify' ); | |
add_action( 'admin_print_scripts-post.php', 'ds_gist_js' ); | |
add_action( 'admin_print_scripts-post-new.php', 'ds_gist_js' ); | |
wp_register_script( | |
'gist-js', | |
get_bloginfo( 'template_url' ) . "/gist.js", | |
array( 'jquery' ), | |
0.1, | |
true | |
); | |
add_meta_box( | |
'ds_gist', | |
'Gist', | |
'ds_gist_metabox_cb', | |
'post' | |
); | |
} | |
add_action( 'admin_init', 'ds_gist2wordpress_admin' ); | |
/* | |
* Hook into the frontend and load scripts. | |
*/ | |
function ds_gist2wordpress() { | |
wp_register_style( | |
'gist-css', | |
'https://gist.github.com/stylesheets/gist/embed.css' | |
); | |
add_filter( 'the_content', 'ds_convert_to_gist', 99 ); | |
} | |
add_action( 'init', 'ds_gist2wordpress' ); | |
/* | |
* Helper for enqeueing script | |
*/ | |
function ds_gist_js() { | |
wp_enqueue_script( 'gist-js' ); | |
} | |
/* | |
* Add a metabox the post edit screen with an input field for the gist ID | |
* and show the file list. | |
*/ | |
function ds_gist_metabox_cb( $data ) { | |
wp_nonce_field( 'ds_gist', 'ds-gist-nonce' ); | |
$gist_id = get_post_meta( $data->ID, '_gist_id', true ) ; | |
$gist_id = ! empty( $gist_id ) ? $gist_id : ''; | |
$gist_data = get_post_meta( $data->ID, '_gist_data', true ); | |
$gist_data = ! empty( $gist_data ) ? $gist_data : array(); | |
$files = ''; | |
if ( ! empty( $gist_data ) ) { | |
foreach ( $gist_data as $file => $data ) | |
$files .= '<li><code>{' . $file . '}</code></li>'; | |
} | |
?> | |
<p> | |
<label>Gist ID: <input type="text" class="small-text" style="width: 180px;" name="gist-id" id="gist-id" value="<?php echo esc_attr( $gist_id ); ?>" /></label> | |
<input type="button" value="Fetch" id="gist-update" class="button" /> | |
<img src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" class="ajax-loading" id="gist-ajax-loading" alt="" /> | |
</p> | |
<ul id="gist-files"> | |
<?php echo $files;?> | |
</ul> | |
<?php | |
} | |
/* | |
* Replace {Filename} with associated snippet from the | |
* custom fields. | |
*/ | |
function ds_gistify() { | |
$post_id = intval( $_POST['post_id'] ); | |
$gist_id = sanitize_key( $_POST['gist_id'] ); | |
if ( empty( $post_id ) || empty( $gist_id ) ) | |
die( json_encode( array( | |
'id' => $gist_id, | |
'error' => 'Empty Post ID or Gist ID.' | |
) ) ); | |
check_ajax_referer( 'ds_gist' ); | |
$gist_body = wp_remote_retrieve_body( | |
wp_remote_get( | |
$gist_url = sprintf( "https://gist.github.com/%s.json", $gist_id ) | |
) | |
); | |
$gist_body = json_decode( $gist_body ); | |
if ( empty( $gist_body ) ) | |
die( json_encode( array( | |
'id' => $gist_id, | |
'error' => 'Empty Body' | |
) ) ); | |
if ( ! empty( $gist_body->error ) ) { | |
die( json_encode( array( | |
'id' => $gist_id, | |
'error' => $gist_body->error | |
) ) ); | |
} | |
update_post_meta( $post_id, '_gist_id', $gist_id ); | |
$gist_files = $gist_body->files; | |
preg_match_all( '/<pre>(.+?)<\/pre>/is', $gist_body->div, $gist_divs ); | |
$gist_divs = $gist_divs[0]; | |
foreach ( $gist_files as $i => $gist_file ) | |
$gist_data[ $gist_file ] = '<div class="gist-syntax">' . $gist_divs[$i] . '</div>'; | |
foreach ( $gist_data as $file => $gist_item ) { | |
$gist_new_meta = sprintf( | |
'<div><p class="gist-meta"><a class="gist-raw" href="%s">RAW</a> · <a class="gist-download" href="%s">Download</a> · <a class="gist-github" href="%s">Gist@GitHub</a></p></div>', | |
esc_url( "https://gist.github.com/raw/{$gist_id}/{$file}" ), | |
esc_url( "https://gist.github.com/gists/{$gist_id}/download"), | |
esc_url( "https://gist.github.com/{$gist_id}") | |
); | |
$gist_data[ $file ] .= $gist_new_meta; | |
} | |
update_post_meta( $post_id, '_gist_data', $gist_data ); | |
die( json_encode( array( | |
'id' => $gist_id, | |
'files' => $gist_files | |
) ) ); | |
} | |
/* | |
* AJAX callback function which handles the API response. Saves the data | |
* into custom fields | |
*/ | |
function ds_convert_to_gist( $content ) { | |
$gist_data = get_post_meta( get_the_ID(), '_gist_data', true ); | |
if ( empty( $gist_data ) ) | |
return $content; | |
wp_enqueue_style('gist-css'); | |
foreach ( $gist_data as $file => $file_data ) { | |
$files[] = '/{' . $file . '}/'; | |
$data[] = $file_data; | |
} | |
$content = preg_replace( | |
$files, | |
$data, | |
$content | |
); | |
return $content; | |
} |
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
// Handle AJAX call | |
( function( $ ) { | |
$( '#gist-update' ).click( function( e ) { | |
e.preventDefault(); | |
$( '#gist-ajax-loading' ).css( 'visibility', 'visible' ); | |
data = { | |
'action' : 'gist', | |
'_ajax_nonce' : $('#ds-gist-nonce').val(), | |
'post_id' : $('#post_ID').val(), | |
'gist_id' : $('#gist-id').val() | |
}; | |
$.post( ajaxurl, data, | |
function( res ) { | |
res = $.parseJSON( res ); | |
if ( res.error ) { | |
$( '#gist-ajax-loading' ).css( 'visibility', 'hidden' ); | |
alert( 'Gist ID "' + res.id + '": ' + res.error ); | |
return; | |
} | |
if ( res.files ) { | |
$( '#gist-files' ).empty(); | |
$.each( res.files, function( key, val ) { | |
$( '#gist-files' ).append( '<li><code>{' + val + '}</code></li>' ); | |
}); | |
$( '#gist-ajax-loading' ).css( 'visibility', 'hidden' ); | |
} | |
} | |
) | |
}); | |
} )( jQuery ); |
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
<?php | |
/* | |
* AJAX callback function which handles the API response. Saves the data | |
* into custom fields | |
*/ | |
function ds_convert_to_gist( $content ) { | |
$gist_data = get_post_meta( get_the_ID(), '_gist_data', true ); | |
if ( empty( $gist_data ) ) | |
return $content; | |
wp_enqueue_style('gist-css'); | |
foreach ( $gist_data as $file => $file_data ) { | |
$files[] = '/{' . $file . '}/'; | |
$data[] = $file_data; | |
} | |
$content = preg_replace( | |
$files, | |
$data, | |
$content | |
); | |
return $content; | |
} |
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
<?php | |
/* | |
* Hook into the backend and load scripts and | |
* init metabox. | |
*/ | |
function ds_gist2wordpress_admin() { | |
add_action( 'wp_ajax_gist', 'ds_gistify' ); | |
add_action( 'admin_print_scripts-post.php', 'ds_gist_js' ); | |
add_action( 'admin_print_scripts-post-new.php', 'ds_gist_js' ); | |
wp_register_script( | |
'gist-js', | |
get_bloginfo( 'template_url' ) . "/gist.js", // Path, needs some update from you | |
array( 'jquery' ), | |
0.1, | |
true | |
); | |
add_meta_box( | |
'ds_gist', | |
'Gist', | |
'ds_gist_metabox_cb', | |
'post' | |
); | |
} | |
add_action( 'admin_init', 'ds_gist2wordpress_admin' ); | |
/* | |
* Hook into the frontend and load scripts. | |
*/ | |
function ds_gist2wordpress() { | |
wp_register_style( | |
'gist-css', | |
'https://gist.github.com/stylesheets/gist/embed.css' | |
); | |
add_filter( 'the_content', 'ds_convert_to_gist', 99 ); | |
} | |
add_action( 'init', 'ds_gist2wordpress' ); | |
function ds_gist_js() { | |
wp_enqueue_script( 'gist-js' ); | |
} |
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
<?php | |
/* | |
* Add a metabox the post edit screen with an input field for the gist ID | |
* and show the file list. | |
*/ | |
function ds_gist_metabox_cb( $data ) { | |
wp_nonce_field( 'ds_gist', 'ds-gist-nonce' ); | |
$gist_id = get_post_meta( $data->ID, '_gist_id', true ) ; | |
$gist_id = ! empty( $gist_id ) ? $gist_id : ''; | |
$gist_data = get_post_meta( $data->ID, '_gist_data', true ); | |
$gist_data = ! empty( $gist_data ) ? $gist_data : array(); | |
$files = ''; | |
if ( ! empty( $gist_data ) ) { | |
foreach ( $gist_data as $file => $data ) | |
$files .= '<li><code>{' . $file . '}</code></li>'; | |
} | |
?> | |
<p> | |
<label>Gist ID: <input type="text" class="small-text" style="width: 180px;" name="gist-id" id="gist-id" value="<?php echo esc_attr( $gist_id ); ?>" /></label> | |
<input type="button" value="Fetch" id="gist-update" class="button" /> | |
<img src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" class="ajax-loading" id="gist-ajax-loading" alt="" /> | |
</p> | |
<ul id="gist-files"> | |
<?php echo $files;?> | |
</ul> | |
<?php | |
} |
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
<?php | |
/* | |
* Replace {Filename} with associated snippet from the | |
* custom fields. | |
*/ | |
function ds_gistify() { | |
$post_id = intval( $_POST['post_id'] ); | |
$gist_id = sanitize_key( $_POST['gist_id'] ); | |
if ( empty( $post_id ) || empty( $gist_id ) ) | |
die( json_encode( array( | |
'id' => $gist_id, | |
'error' => 'Empty Post ID or Gist ID.' | |
) ) ); | |
check_ajax_referer( 'ds_gist' ); | |
$gist_body = wp_remote_retrieve_body( | |
wp_remote_get( | |
$gist_url = sprintf( "https://gist.github.com/%s.json", $gist_id ) | |
) | |
); | |
$gist_body = json_decode( $gist_body ); | |
if ( empty( $gist_body ) ) | |
die( json_encode( array( | |
'id' => $gist_id, | |
'error' => 'Empty Body' | |
) ) ); | |
if ( ! empty( $gist_body->error ) ) { | |
die( json_encode( array( | |
'id' => $gist_id, | |
'error' => $gist_body->error | |
) ) ); | |
} | |
update_post_meta( $post_id, '_gist_id', $gist_id ); | |
$gist_files = $gist_body->files; | |
preg_match_all( '/<pre>(.+?)<\/pre>/is', $gist_body->div, $gist_divs ); | |
$gist_divs = $gist_divs[0]; | |
foreach ( $gist_files as $i => $gist_file ) | |
$gist_data[ $gist_file ] = '<div class="gist-syntax">' . $gist_divs[$i] . '</div>'; | |
foreach ( $gist_data as $file => $gist_item ) { | |
$gist_new_meta = sprintf( | |
'<div><p class="gist-meta"><a class="gist-raw" href="%s">RAW</a> · <a class="gist-download" href="%s">Download</a> · <a class="gist-github" href="%s">Gist@GitHub</a></p></div>', | |
esc_url( "https://gist.github.com/raw/{$gist_id}/{$file}" ), | |
esc_url( "https://gist.github.com/gists/{$gist_id}/download"), | |
esc_url( "https://gist.github.com/{$gist_id}") | |
); | |
$gist_data[ $file ] .= $gist_new_meta; | |
} | |
update_post_meta( $post_id, '_gist_data', $gist_data ); | |
die( json_encode( array( | |
'id' => $gist_id, | |
'files' => $gist_files | |
) ) ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
havent got this to work yet with 3.5.2