
Untitled
By: a guest on
Jun 22nd, 2012 | syntax:
None | size: 1.73 KB | hits: 17 | expires: Never
<?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
) ) );
}