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

Untitled

By: a guest on Jun 22nd, 2012  |  syntax: None  |  size: 4.11 KB  |  hits: 13  |  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.  * Hook into the backend and load scripts and
  4.  * init metabox.
  5.  */
  6. function ds_gist2wordpress_admin() {
  7.                 add_action( 'wp_ajax_gist', 'ds_gistify' );
  8.  
  9.                 add_action( 'admin_print_scripts-post.php', 'ds_gist_js' );
  10.                 add_action( 'admin_print_scripts-post-new.php', 'ds_gist_js' );
  11.  
  12.                 wp_register_script(
  13.                         'gist-js',
  14.                         get_bloginfo( 'template_url' ) . "/gist.js",
  15.                         array( 'jquery' ),
  16.                         0.1,
  17.                         true
  18.                 );
  19.  
  20.                 add_meta_box(
  21.                                 'ds_gist',
  22.                                 'Gist',
  23.                                 'ds_gist_metabox_cb',
  24.                                 'post'
  25.                 );
  26. }
  27. add_action( 'admin_init', 'ds_gist2wordpress_admin' );
  28.  
  29. /*
  30.  * Hook into the frontend and load scripts.
  31.  */
  32. function ds_gist2wordpress() {
  33.                 wp_register_style(
  34.                         'gist-css',
  35.                         'https://gist.github.com/stylesheets/gist/embed.css'
  36.                 );
  37.  
  38.                 add_filter( 'the_content', 'ds_convert_to_gist', 99 );
  39. }
  40. add_action( 'init', 'ds_gist2wordpress' );
  41.  
  42. /*
  43.  * Helper for enqeueing script
  44.  */
  45. function ds_gist_js() {
  46.         wp_enqueue_script( 'gist-js' );
  47. }
  48.  
  49. /*
  50.  * Add a metabox the post edit screen with an input field for the gist ID
  51.  * and show the file list.
  52.  */
  53. function ds_gist_metabox_cb( $data ) {
  54.         wp_nonce_field( 'ds_gist', 'ds-gist-nonce' );
  55.  
  56.         $gist_id   = get_post_meta( $data->ID, '_gist_id', true ) ;
  57.         $gist_id   = ! empty( $gist_id ) ? $gist_id : '';
  58.         $gist_data = get_post_meta( $data->ID, '_gist_data', true );
  59.         $gist_data = ! empty( $gist_data ) ? $gist_data : array();
  60.  
  61.         $files = '';
  62.         if ( ! empty( $gist_data ) ) {
  63.                 foreach ( $gist_data as $file => $data )
  64.                         $files .= '<li><code>{' . $file . '}</code></li>';
  65.         }
  66.         ?>
  67.         <p>
  68.                 <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>
  69.                 <input type="button" value="Fetch" id="gist-update" class="button" />
  70.                 <img src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" class="ajax-loading" id="gist-ajax-loading" alt="" />
  71.         </p>
  72.         <ul id="gist-files">
  73.                 <?php echo $files;?>
  74.         </ul>
  75.         <?php
  76. }
  77.  
  78. /*
  79.  * Replace {Filename} with associated snippet from the
  80.  * custom fields.
  81.  */
  82. function ds_gistify() {
  83.         $post_id = intval( $_POST['post_id'] );
  84.         $gist_id = sanitize_key( $_POST['gist_id'] );
  85.         if ( empty( $post_id ) || empty( $gist_id ) )
  86.                 die( json_encode( array(
  87.                         'id'    => $gist_id,
  88.                         'error' => 'Empty Post ID or Gist ID.'
  89.                 ) ) );
  90.  
  91.         check_ajax_referer( 'ds_gist' );
  92.  
  93.         $gist_body = wp_remote_retrieve_body(
  94.                 wp_remote_get(
  95.                                 $gist_url = sprintf( "https://gist.github.com/%s.json", $gist_id )
  96.                 )
  97.         );
  98.         $gist_body = json_decode( $gist_body );
  99.  
  100.         if ( empty( $gist_body ) )
  101.                 die( json_encode( array(
  102.                         'id'    => $gist_id,
  103.                         'error' => 'Empty Body'
  104.                 ) ) );
  105.  
  106.         if ( ! empty( $gist_body->error ) ) {
  107.                 die( json_encode( array(
  108.                         'id'    => $gist_id,
  109.                         'error' => $gist_body->error
  110.                 ) ) );
  111.         }
  112.  
  113.         update_post_meta( $post_id, '_gist_id', $gist_id );
  114.  
  115.         $gist_files = $gist_body->files;
  116.  
  117.         preg_match_all( '/<pre>(.+?)<\/pre>/is', $gist_body->div, $gist_divs );
  118.         $gist_divs = $gist_divs[0];
  119.  
  120.         foreach ( $gist_files as $i => $gist_file )
  121.                 $gist_data[ $gist_file ] = '<div class="gist-syntax">' . $gist_divs[$i] . '</div>';
  122.  
  123.         foreach ( $gist_data as $file => $gist_item ) {
  124.                 $gist_new_meta = sprintf(
  125.                         '<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>',
  126.                         esc_url( "https://gist.github.com/raw/{$gist_id}/{$file}" ),
  127.                         esc_url( "https://gist.github.com/gists/{$gist_id}/download"),
  128.                         esc_url( "https://gist.github.com/{$gist_id}")
  129.                 );
  130.                 $gist_data[ $file ] .= $gist_new_meta;
  131.         }
  132.  
  133.         update_post_meta( $post_id, '_gist_data', $gist_data );
  134.  
  135.         die( json_encode( array(
  136.                 'id'    => $gist_id,
  137.                 'files' => $gist_files
  138.         ) ) );
  139. }
  140.  
  141. /*
  142.  * AJAX callback function which handles the API response. Saves the data
  143.  * into custom fields
  144.  */
  145. function ds_convert_to_gist( $content ) {
  146.         $gist_data = get_post_meta( get_the_ID(), '_gist_data', true );
  147.  
  148.         if ( empty( $gist_data ) )
  149.                 return $content;
  150.  
  151.         wp_enqueue_style('gist-css');
  152.  
  153.         foreach ( $gist_data as $file => $file_data ) {
  154.                 $files[] = '/{' . $file . '}/';
  155.                 $data[]  = $file_data;
  156.         }
  157.  
  158.         $content = preg_replace(
  159.                 $files,
  160.                 $data,
  161.                 $content
  162.         );
  163.  
  164.         return $content;
  165. }