register_plugin ('sniplets', __FILE__);
if (is_admin ())
{
$this->register_activation (__FILE__);
$this->add_action( 'wp_print_scripts' );
$this->add_action( 'admin_head', 'wp_print_styles' );
$this->add_action( 'admin_print_styles', 'wp_print_styles' );
$this->add_filter( 'contextual_help', 'contextual_help', 10, 2 );
$this->add_action( 'admin_menu' );
$this->add_action( 'admin_footer' );
$this->register_plugin_settings( __FILE__ );
}
else
{
$this->add_filter ('the_posts');
$this->add_action ('the_sniplet');
$this->add_action ('the_sniplet_place', 'the_sniplet_place', 1, 3);
// Do we need to process PHP
$options = $this->get_options ();
if ($options['php'])
{
$this->add_filter ('the_content', 'run_php');
$this->add_filter ('the_excerpt', 'run_php');
}
$this->add_filter ('the_real_content', 'the_real_content', 10, 2);
}
$this->widget = new Sniplet_Widget ('Sniplet', 10);
}
function plugin_settings ($links) {
$settings_link = ''.__('Settings', 'sniplets').'';
array_unshift( $links, $settings_link );
return $links;
}
function version ()
{
$plugin_data = implode ('', file (__FILE__));
if (preg_match ('|Version:(.*)|i', $plugin_data, $version))
return trim ($version[1]);
return '';
}
function the_posts ($posts)
{
if ($this->loaded == false)
{
if ((is_single () || is_page ()) && !is_404 ())
$sniplets = Snip::get_for (count($posts) > 0 ? array ($posts[0]) : array(), 'post', true);
else if (is_front_page ())
$sniplets = Snip::get_for ($posts, 'home', true);
else if (is_archive ())
$sniplets = Snip::get_for ($posts, 'archive', true);
else if (is_feed ())
$sniplets = Snip::get_for ($posts, 'rss', true);
else
$sniplets = Snip::get_shared ();
if (count ($sniplets) > 0)
{
$options = $this->get_options ();
// Run through the sniplets and decide what needs to be done
foreach ($sniplets AS $snip)
{
if ($snip->placement)
{
foreach ($snip->placement AS $place)
{
$parts = explode ('/', $place);
if (count ($parts) == 2)
{
if ($parts[1] == 'root')
echo $snip->process ($options['search']); // do it now
else if ($parts[1])
{
$this->sniplets[$parts[1]][] = $snip;
$this->sniplets[$place][] = $snip;
}
$this->place = $parts[0];
}
}
}
$this->sniplets['manual'][$snip->name][] = $snip;
}
// Setup any WP hooks
if (isset ($this->sniplets['header']))
$this->add_action ('wp_head');
if (isset ($this->sniplets['footer']))
$this->add_action ('wp_footer');
if (isset ($this->sniplets['comment']))
$this->add_action ('comment_form');
if (is_feed ())
{
$this->add_action ('the_content_rss', 'the_content', 1);
$this->add_action ('the_excerpt_rss', 'the_content', 1);
}
else
{
$this->add_action ('the_content', 'the_content', 1);
$this->add_action ('the_excerpt', 'the_content', 1);
}
}
$this->loaded = true;
}
return $posts;
}
function is_excluded ($sniplet)
{
if (is_single () || is_page () || is_feed () || is_archive ())
{
global $post;
if ($sniplet->post_id > 0 && $post->ID != $sniplet->post_id)
return true;
$options = $this->get_options ();
$parts = explode (',', $options['exclude']);
if (count ($parts) > 0)
{
if (in_array ($post->ID, $parts))
return true;
}
}
return false;
}
/**
* Called when the plugin is activated, and installs database tables
*
* @return void
**/
function activate ()
{
include (dirname (__FILE__).'/model/database.php');
$db = new SN_Database;
$db->upgrade (2);
$db->install ();
$options = $this->get_options ();
update_option ('sniplets', $options);
}
function upgrade ()
{
if (get_option ('sniplets_version') != 4)
{
include (dirname (__FILE__).'/model/database.php');
$db = new SN_Database;
$db->upgrade (get_option ('sniplets_version'), 4);
}
}
/**
* Inserts JS & CSS into administration pages
*
* @return void
**/
function contextual_help ($help, $screen)
{
if ($screen == 'tools_page_sniplets')
{
$help .= '
' . __('Sniplets Help') . '
';
}
return $help;
}
function wp_print_scripts() {
if ( (isset($_GET['page']) && $_GET['page'] == 'sniplets.php') || strpos ($_SERVER['REQUEST_URI'], 'page.php') || strpos ($_SERVER['REQUEST_URI'], 'post.php')) {
wp_enqueue_script ('sniplets', $this->url ().'/resource/admin.js', array ('jquery', 'jquery-ui-sortable'), $this->version ());
$this->render_admin( 'head' );
}
}
function wp_print_styles() {
if ( (isset($_GET['page']) && $_GET['page'] == 'sniplets.php') || strpos ($_SERVER['REQUEST_URI'], 'page.php') || strpos ($_SERVER['REQUEST_URI'], 'post.php')) {
echo '';
if (!function_exists ('wp_enqueue_style'))
echo '';
}
}
/**
* Injects a Sniplets menu into administration pages
*
* @return void
**/
function admin_menu ()
{
add_management_page (__ ("Sniplets", 'sniplets'), __ ("Sniplets", 'sniplets'), "administrator", basename (__FILE__), array ($this, "admin_screen"));
add_meta_box ('snipletstuff', __ ('Sniplets', 'headspace'), array (&$this, 'metabox'), 'post', 'normal', 'high');
add_meta_box ('snipletstuff', __ ('Sniplets', 'headspace'), array (&$this, 'metabox'), 'page', 'normal', 'high');
}
/**
* Displays the nice animated support logo
*
* @return void
**/
function admin_footer() {
if ( isset($_GET['page']) && $_GET['page'] == basename( __FILE__ ) ) {
$options = $this->get_options();
if ( !$options['support'] ) {
?>
upgrade ();
$sub = isset ($_GET['sub']) ? $_GET['sub'] : '';
$url = explode ('&', $_SERVER['REQUEST_URI']);
$url = $url[0];
if (isset($_GET['sub'])) {
if ($_GET['sub'] == 'options')
return $this->admin_options ($url);
else if ($_GET['sub'] == 'limits')
return $this->admin_limits ($url);
else if ($_GET['sub'] == 'export')
return $this->admin_export ($url);
else if ($_GET['sub'] == 'support')
$this->render_admin('support', array( 'url' => $url ) );
}
else
return $this->admin_sniplets ($url);
}
function admin_export ($url)
{
global $wpdb;
if (isset ($_POST['import']) && is_uploaded_file ($_FILES['importer']['tmp_name']) && check_admin_referer ('sniplets-import'))
{
// Import the XML file
$xml = simplexml_load_file ($_FILES['importer']['tmp_name']);
if ($xml)
{
if (count ($xml->sniplet) > 0)
{
foreach ($xml->sniplet AS $sniplet)
{
$snip = new Snip;
$snip->name = (string)$sniplet['name'];
$snip->placement = explode ('|', (string)$sniplet->placement[0]);
$snip->template = (string)$sniplet['template'];
$snip->modules = unserialize ((string)$sniplet->modules[0]);
$snip->contents = (string)$sniplet->content[0];
$snip->create (intval ($_POST['post_id']));
}
}
}
else
$this->render_error ('Sorry, but that file could not be loaded');
}
// Get list of posts with sniplets
$snipposts = $wpdb->get_results ("select {$wpdb->posts}.id,{$wpdb->posts}.post_title from {$wpdb->prefix}sniplets LEFT JOIN {$wpdb->posts} ON {$wpdb->posts}.ID={$wpdb->prefix}sniplets.post_id GROUP BY {$wpdb->prefix}sniplets.post_id ORDER BY {$wpdb->posts}.post_name");
$this->render_admin ('export', array ('posts' => $snipposts, 'url' => $url));
}
function admin_limits ($url)
{
if (isset ($_POST['save']) && check_admin_referer ('sniplets-limits'))
{
$options = $this->get_options ();
$options['limits'] = stripslashes( $_POST['limits'] );
$options['rand'] = stripslashes( $_POST['rand'] );
update_option ('sniplets', $options);
$this->render_message (__ ('Your limits were successfully saved', 'sniplets'));
}
$places = Snip::automatic_types ();
if (!is_array ($places))
$places = array ();
array_shift ($places);
$this->render_admin ('limits', array ('places' => $places, 'options' => $this->get_options (), 'url' => $url));
}
/**
* Options page
*
* @return void
**/
function admin_options ($url)
{
if (isset ($_POST['save']) && check_admin_referer ('sniplets-options'))
{
$options = $this->get_options ();
$options['php'] = isset ($_POST['php']) ? true : false;
$options['content'] = isset ($_POST['content']) ? true : false;
$options['search'] = trim (rtrim (stripslashes( $_POST['search'] ), DIRECTORY_SEPARATOR));
$options['custom_places'] = trim (stripslashes( $_POST['custom_places']) );
$options['custom_templates'] = trim (stripslashes( $_POST['custom_templates']));
$options['exclude'] = trim (preg_replace ('/[^0-9,]/', '', $_POST['exclude']));
update_option ('sniplets', $options);
$this->render_message (__ ('Your options have been saved', 'sniplets'));
}
else if (isset ($_POST['delete']) && check_admin_referer ('sniplets-delete_plugin'))
{
include (dirname (__FILE__).'/model/database.php');
$db = new SN_Database;
$db->remove (__FILE__);
$this->render_message (__ ('Sniplets has been successfully removed', 'sniplets'));
}
$this->render_admin ('options', array ('options' => $this->get_options (), 'url' => $url));
}
/**
* Get options, setting values to defaults if not present
*
* @return array Options
**/
function get_options ()
{
$options = get_option ('sniplets');
if (!is_array ($options))
$options = array ();
$defaults = array
(
'php' => false,
'content' => false,
'support' => false,
'exclude' => '',
'limits' => array(),
'rand' => array(),
'searchpath' => ''
);
return array_merge( $defaults, $options );
}
/**
* Displays the main sniplets page
*
* @return void
**/
function admin_sniplets ($url)
{
if (isset ($_POST['add']) && check_admin_referer ('sniplets-add_sniplet'))
{
$snip = new Snip ($_POST);
if ($snip->create ())
$this->render_message (__ ('Your Sniplet was successfully added', 'sniplets'));
else
$this->render_error (__ ('Failed to add Sniplet - please give a valid name', 'sniplets'));
}
$pager = new SNI_Pager ($_GET, $_SERVER['REQUEST_URI'], 'position', 'ASC');
$this->render_admin ('sniplets', array ('snips' => Snip::get_all_shared ($pager), 'pager' => $pager, 'url' => $url));
}
/**
* Replaces sniplets
*
* @param array $string Array of matches from regex
* @return string Replaced text
**/
function replace_sniplets ($matches)
{
if (isset ($this->sniplets['manual'][$matches[1]]))
return $this->get_display ($this->sniplets['manual'][$matches[1]], 'manual');
else
return sprintf (__ ('No sniplet called %s', 'sniplets'), htmlspecialchars ($matches[1]));
}
/**
* Replaces content sniplets
*
* @param array $string Array of matches from regex
* @return string Replaced text
**/
function replace_content ($matches)
{
if (isset ($this->sniplets['manual'][$matches[1]]))
return $this->get_display ($this->sniplets['manual'][$matches[1]], 'manual', trim (preg_replace ('@
$@', '', $matches[2])));
else if (substr ($matches[1], 0, 7) == 'module:')
{
if (preg_match ('/args="(.*?)"/', $matches[1], $args) > 0)
$args = $args[1];
else
$args = '';
return $this->get_display (Snip::anonymous (substr ($matches[1], 7), $args), 'manual', trim (preg_replace ('@
$@', '', $matches[2])));
}
}
function wp_head ()
{
echo $this->get_display ($this->sniplets['header'], 'header');
}
function wp_footer ()
{
echo $this->get_display ($this->sniplets['footer'], 'footer');
}
function comment_form ($id)
{
echo $this->get_display ($this->sniplets['comment'], 'comment');
}
/**
* Replaces PHP
*
* @param array $string Array of matches from regex
* @return string Replaced text
**/
function execute_php ($matches)
{
ob_start ();
eval ($matches[1]);
$output = ob_get_contents ();
ob_end_clean ();
return $output;
}
function get_display ($sniplets, $place, $content = '')
{
$text = '';
if (count ($sniplets) > 0)
{
$options = $this->get_options ();
if (isset ($options['rand']) && isset ($place, $options['rand']))
shuffle ($sniplets);
$count = 0;
foreach ($sniplets AS $snip)
{
if (!$this->is_excluded ($snip))
{
$text .= $snip->process ($options['searchpath'], $content);
$count++;
if (isset ($options['limits'][$place]) && $options['limits'][$place] > 0 && $count >= $options['limits'][$place])
break;
}
}
}
if (strpos ($text, '[snip') !== false)
$text = $this->the_content ($text);
return $text;
}
/**
* Filter that replaces all sniplets in post data
*
* @param array $string Array of matches from regex
* @return string Replaced text
**/
function the_content ($text)
{
global $post;
if (is_single () || is_page () || is_feed ())
{
global $post, $multipage, $page, $numpages;
$after = $before = '';
if (($multipage && $page == 1) || !$multipage)
$before = $this->get_display (isset($this->sniplets['beforefirst']) ? $this->sniplets['beforefirst'] : array(), 'beforefirst');
if (($multipage && $page == $numpages) || !$multipage)
$after = $this->get_display (isset($this->sniplets['afterfirst']) ? $this->sniplets['afterfirst'] : array(), 'afterfirst');
if (isset ($this->sniplets['more']))
$text = preg_replace ('@@', $this->get_display ($this->sniplets['more'], 'more'), $text);
$text = $before.$this->get_display (isset($this->sniplets['beforeall']) ? $this->sniplets['beforeall'] : array(), 'beforeall').$text.$this->get_display (isset($this->sniplets['afterall']) ? $this->sniplets['afterall'] : array(), 'afterall').$after;
}
// Now process manual sniplets
if (isset ($this->sniplets['manual']))
{
$options = $this->get_options ();
// Replace [snip NAME]something[/snip]
if ($options['content'])
$text = preg_replace_callback ('/\[snip\s+(.*?)\](.*?)\[\/snip\]/s', array ($this, 'replace_content'), $text);
// Replace [sniplet NAME]
$text = preg_replace_callback ('/\[sniplet\s+(.*?)\]/', array ($this, 'replace_sniplets'), $text);
}
return $text;
}
function run_php ($text)
{
// Replace any PHP functions
return preg_replace_callback ('@<\?php(.*)\?>@', array ($this, 'execute_php'), $text);
}
function metabox ($post)
{
global $wp_meta_boxes;
if (isset ($wp_meta_boxes['post']['normal']['sorted']['headspacestuff']))
unset ($wp_meta_boxes['post']['normal']['sorted']['headspacestuff']);
if (current_user_can ('administrator'))
{
global $post;
$snips = Snip::get_for (array ($post), '', false, true);
$this->render_admin ('post-form-inside', array ('sniplets' => $snips));
}
}
function post_form ()
{
if (current_user_can ('administrator'))
{
global $post;
$snips = Snip::get_for (array ($post), '', false, true);
$this->render_admin ('post-form', array ('sniplets' => $snips));
}
}
function the_sniplet ($sniplet)
{
if (!$this->loaded)
$this->the_posts (array ());
if (isset ($this->sniplets['manual'][$sniplet]))
echo $this->get_display ($this->sniplets['manual'][$sniplet], 'manual');
}
function the_sniplet_place ($place, $before = '', $after = '')
{
if (!$this->loaded)
$this->the_posts (array ());
if (isset ($this->sniplets[$place]))
echo $before.$this->get_display ($this->sniplets[$place], $place).$after;
}
function the_real_content ($text, $post)
{
$sniplets = Snip::get_for (array ($post->ID), 'post', true);
if (count ($sniplets) > 0)
{
$options = $this->get_options ();
foreach ($sniplets AS $snip)
$this->sniplets['manual'][$snip->name][] = $snip;
}
return $this->the_content ($text);
}
}
/**
* Template tag to allow a sniplet to be displayed in a template
*
* @param string Sniplet name
* @return string Text
**/
function the_sniplet ($sniplet)
{
do_action ('the_sniplet', $sniplet);
}
function the_sniplet_place ($place, $before = '', $after = '')
{
do_action ('the_sniplet_place', $place, $before, $after);
}
function have_sniplet ($sniplet)
{
global $sniplets;
return isset ($sniplets->sniplets['manual'][$sniplet]);
}
function have_sniplet_place ($place)
{
global $sniplets;
return isset ($sniplets->sniplets[$place]);
}
function sniplet_transform_text ($text)
{
global $sniplets;
return $sniplets->the_content ($text);
}
/**
* The Sniplet plugin object
*
* @global
**/
$sniplets = new Sniplets;