Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 22nd, 2012  |  syntax: None  |  size: 1.73 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /*
  3.  * Replace {Filename} with associated snippet from the
  4.  * custom fields.
  5.  */
  6. function ds_gistify() {
  7.         $post_id = intval( $_POST['post_id'] );
  8.         $gist_id = sanitize_key( $_POST['gist_id'] );
  9.         if ( empty( $post_id ) || empty( $gist_id ) )
  10.                 die( json_encode( array(
  11.                         'id'    => $gist_id,
  12.                         'error' => 'Empty Post ID or Gist ID.'
  13.                 ) ) );
  14.  
  15.         check_ajax_referer( 'ds_gist' );
  16.  
  17.         $gist_body = wp_remote_retrieve_body(
  18.                 wp_remote_get(
  19.                                 $gist_url = sprintf( "https://gist.github.com/%s.json", $gist_id )
  20.                 )
  21.         );
  22.         $gist_body = json_decode( $gist_body );
  23.  
  24.         if ( empty( $gist_body ) )
  25.                 die( json_encode( array(
  26.                         'id'    => $gist_id,
  27.                         'error' => 'Empty Body'
  28.                 ) ) );
  29.  
  30.         if ( ! empty( $gist_body->error ) ) {
  31.                 die( json_encode( array(
  32.                         'id'    => $gist_id,
  33.                         'error' => $gist_body->error
  34.                 ) ) );
  35.         }
  36.  
  37.         update_post_meta( $post_id, '_gist_id', $gist_id );
  38.  
  39.         $gist_files = $gist_body->files;
  40.  
  41.         preg_match_all( '/<pre>(.+?)<\/pre>/is', $gist_body->div, $gist_divs );
  42.         $gist_divs = $gist_divs[0];
  43.  
  44.         foreach ( $gist_files as $i => $gist_file )
  45.                 $gist_data[ $gist_file ] = '<div class="gist-syntax">' . $gist_divs[$i] . '</div>';
  46.  
  47.         foreach ( $gist_data as $file => $gist_item ) {
  48.                 $gist_new_meta = sprintf(
  49.                         '<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>',
  50.                         esc_url( "https://gist.github.com/raw/{$gist_id}/{$file}" ),
  51.                         esc_url( "https://gist.github.com/gists/{$gist_id}/download"),
  52.                         esc_url( "https://gist.github.com/{$gist_id}")
  53.                 );
  54.                 $gist_data[ $file ] .= $gist_new_meta;
  55.         }
  56.  
  57.         update_post_meta( $post_id, '_gist_data', $gist_data );
  58.  
  59.         die( json_encode( array(
  60.                 'id'    => $gist_id,
  61.                 'files' => $gist_files
  62.         ) ) );
  63. }