Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Snippets Shortcode plugin. Add code snippets as a Custom Post Type. Display in regular posts and pages using a Shortcode. Rendered with Google Prettify in frontend.
<?php
/**
* Plugin Name: Snippets Shortcode
* Plugin URI: http://wordpress.stackexchange.com/q/116044/12615
* Description: Add code snippets as a Custom Post Type. Display in regular posts and pages using a Shortcode. Rendered with Google Prettify in frontend.
* Version: 1.0
* Author: Rodolfo Buaiz
* Author URI: http://brasofilo.com
* License: GPLv2 or later
*
*
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation. You may NOT assume
* that you can use any other version of the GPL.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
/**
* Based on Plugin Class Demo
* https://gist.github.com/toscho/3804204
*/
add_action(
'plugins_loaded',
array( B5F_Snippets_Shortcode::get_instance(), 'plugin_setup' )
);
/**
* Main class
*
* Fires all classes
* Handles save_post action for other classes
*
*/
class B5F_Snippets_Shortcode
{
protected static $instance = NULL;
public $post_type = 'snippet';
public function plugin_setup()
{
new B5F_SS_Cpt();
new B5F_SS_Posts_Pages_Metabox();
new B5F_SS_Shortcode();
add_action( 'save_post', array( $this, '_save_post' ), 10, 2 );
}
public function __construct() {}
public function get_instance()
{
NULL === self::$instance and self::$instance = new self;
return self::$instance;
}
public function _save_post( $post_id, $post_object )
{
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if(
!isset( $_POST['_nonce_snippets'] )
|| !wp_verify_nonce( $_POST['_nonce_snippets'], plugin_basename( __FILE__ ) )
)
return;
switch( $post_object->post_type )
{
case $this->post_type:
if( isset( $_POST['snipp'] ) )
update_post_meta( $post_id, '_snippet_code', $_POST['snipp'] );
break;
case 'post':
case 'page':
if( isset( $_POST['_snippet_shortcode'] ) )
update_post_meta( $post_id, '_snippet_shortcode', $_POST['_snippet_shortcode'] );
else
delete_post_meta( $post_id, '_snippet_shortcode' );
break;
}
}
}
/**
* Register Snippet post type and render Textarea Field after the Title
*
* CPT is hierarchical
* CPT menu shows up inside Post post type (cool!)
* Custom Metabox for CPT is left empty, can be used as Language Selector
*
*/
class B5F_SS_Cpt
{
public function __construct()
{
add_action( 'init', array( $this, '_cpt' ) );
add_action( 'edit_form_after_title', array( $this, 'input_text_area' ) );
}
public function _cpt()
{
$labels = array(
'menu_name' => 'Snippets',
'singular_name' => 'Snippet',
'add_new' => 'Add snippet',
'add_new_item' => 'Add new snippet',
'edit_item' => 'Edit snippet',
'new_item' => 'New snippet',
'view_item' => 'View snippet',
'search_items' => 'Search snippet',
'not_found' => 'Snippet not fount',
'not_found_in_trash' => 'No snippet in trash',
'parent_item_colon' => 'Parent snippet',
);
$args = array(
'labels' => $labels,
'public' => false,
'publicly_queryable' => false,
'show_ui' => true,
'show_in_menu' => 'edit.php',
'query_var' => true,
'rewrite' => false,
'capability_type' => 'post',
'has_archive' => false,
'hierarchical' => true,
'menu_position' => null,
'supports' => array( 'title', 'page-attributes' ),
'register_meta_box_cb' => array( $this, '_meta_box' )
);
register_post_type( B5F_Snippets_Shortcode::get_instance()->post_type, $args );
}
public function _meta_box() { /* add_meta_box(); */ }
public function input_text_area( $post )
{
if( B5F_Snippets_Shortcode::get_instance()->post_type != $post->post_type )
return;
$option = ( $get = get_post_meta( $post->ID, '_snippet_code', true ) ) ? $get : '';
wp_nonce_field( plugin_basename( __FILE__ ), '_nonce_snippets' );
printf(
'<h3>%s</h3><textarea id="snipp" name="snipp" cols="90" rows="25" class="widefat">%s</textarea>',
__( 'Code' ),
esc_html( $option )
);
}
}
/**
* Metabox to select the Snippet and to display the correspondent Shortcode
*
* Hardcoded post types in _meta_boxes()
* Hardcoded jQuery to listen to the Snippets Dropdown changes and update the sample Text Field
*
*/
class B5F_SS_Posts_Pages_Metabox
{
public function __construct()
{
add_action( 'add_meta_boxes', array( $this, '_meta_boxes' ) );
}
public function _meta_boxes()
{
foreach( array( 'post', 'page' ) as $pt )
add_meta_box(
'snippets_mb',
__( 'Snippets' ),
array( $this, '_snippets_mb' ),
$pt,
'side'
);
}
function _snippets_mb( $post, $box )
{
$args = array(
'numberposts' => -1,
'post_type' => B5F_Snippets_Shortcode::get_instance()->post_type,
'post_status' => 'publish'
);
$get_posts = get_posts( $args );
$saved = get_post_meta( $post->ID, '_snippet_shortcode', true );
wp_nonce_field( plugin_basename( __FILE__ ), '_nonce_snippets' );
echo '<style>#_snippet_shortcode{width:100%}</style>'; # LAME FIX !!!!!
wp_dropdown_pages( array(
'post_type' => 'snippet',
'selected' => $saved,
'name' => '_snippet_shortcode',
'id' => '_snippet_shortcode',
'show_option_none' => '- Select -'
));
echo '<hr /><input type="text" class="widefat" id="render-shortcode" value="" />';
?>
<script type="text/javascript">
jQuery(document).ready(function( $ )
{
function do_the_shortcode( val )
{
var full_shortcode = ( val != '' ) ? '[snippet id="' + val + '"]' : '';
$('#render-shortcode').val( full_shortcode );
}
$('#_snippet_shortcode').change( function()
{
do_the_shortcode( $(this).val() );
});
do_the_shortcode( $('#_snippet_shortcode').val() );
});
</script>
<?php
}
}
/**
* Snippets Shortcode
*
* First gets the Snippet Post ID, then the associated Code Meta Data,
* finally filters the string with http://php.net/manual/en/function.htmlentities.php
*
* Uses Google Code Prettify
* https://code.google.com/p/google-code-prettify/
*/
class B5F_SS_Shortcode
{
public function __construct()
{
add_shortcode( 'snippet', array( $this, '_shortcode' ) );
}
public function _shortcode()
{
global $post;
$file = 'run_prettify.js?lang=css&skin=sons-of-obsidian';
wp_enqueue_script(
'prettify-js',
'https://google-code-prettify.googlecode.com/svn/loader/' . $file,
array()
);
$snippet_id = get_post_meta( $post->ID, '_snippet_shortcode', true );
$code = get_post_meta( $snippet_id, '_snippet_code', true );
$snippet_info = get_post( $snippet_id );
return sprintf(
'<h3>%s</h3><pre class="prettyprint linenums">%s</pre>',
$snippet_info->post_title,
htmlentities( $code, ENT_QUOTES )
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment