Skip to content

Commit abebb26

Browse files
committed
Updated to version 1.3
1 parent 36c0e2f commit abebb26

9 files changed

+281
-118
lines changed

code-snippets.php

+66-101
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
<?php
2-
32
/*
43
Plugin Name: Code Snippets
54
Plugin URI: http://bungeshea.wordpress.com/plugins/code-snippets/
65
Description: Provides an easy-to-manage GUI interface for adding code snippets to your blog.
76
Author: Shea Bunge
8-
Version: 1.2
7+
Version: 1.3
98
Author URI: http://bungeshea.wordpress.com/
109
License: GPLv3 or later
1110
@@ -33,16 +32,17 @@
3332

3433
class Code_Snippets {
3534

36-
public $table = 'snippets';
37-
public $version = '1.2';
35+
public $table = 'snippets';
36+
public $version = '1.3';
3837

3938
public $file;
4039
public $plugin_dir;
4140
public $plugin_url;
4241
public $basename;
4342

44-
var $admin_manage_url = 'snippets';
45-
var $admin_edit_url = 'snippet';
43+
var $admin_manage_url = 'snippets';
44+
var $admin_edit_url = 'snippet';
45+
var $admin_import_url = 'import-snippets';
4646

4747
public function Code_Snippets() {
4848
$this->setup(); // initialise the varables and run the hooks
@@ -54,16 +54,13 @@ public function Code_Snippets() {
5454
function setup() {
5555
global $wpdb;
5656
$this->file = __FILE__;
57-
$this->table = $wpdb->prefix . $this->table;
57+
$this->table = apply_filters( 'cs_table', $wpdb->prefix . $this->table );
5858
$this->current_version = get_option( 'cs_db_version' );
59-
59+
6060
$this->basename = plugin_basename( $this->file );
6161
$this->plugin_dir = plugin_dir_path( $this->file );
6262
$this->plugin_url = plugin_dir_url ( $this->file );
63-
64-
$this->admin_manage_url = admin_url( 'admin.php?page=' . $this->admin_manage_url );
65-
$this->admin_edit_url = admin_url( 'admin.php?page=' . $this->admin_edit_url );
66-
63+
6764
add_action( 'admin_menu', array( $this, 'add_admin_menus' ) );
6865
add_filter( 'plugin_action_links_' . $this->basename, array( $this, 'settings_link' ) );
6966
add_filter( 'plugin_row_meta', array( $this, 'plugin_meta' ), 10, 2 );
@@ -87,125 +84,79 @@ function create_table() {
8784
}
8885

8986
function upgrade() {
90-
if( $this->current_version < $this->version ) {
87+
if( $this->current_version < 1.2 ) {
9188
delete_option( 'cs_complete_uninstall' );
89+
}
90+
if( $this->current_version < $this->version ) {
9291
update_option( 'cs_db_version', $this->version );
9392
}
9493
}
9594

9695
function add_admin_menus() {
97-
$this->admin_manage_page = add_menu_page( __('Snippets'), __('Snippets'), 'install_plugins', 'snippets', array( $this, 'admin_manage_loader' ), $this->plugin_url . 'images/icon16.png', 67 );
98-
add_submenu_page('snippets', __('Snippets'), __('Manage Snippets') , 'install_plugins', 'snippets', array( $this, 'admin_manage_loader') );
99-
$this->admin_edit_page = add_submenu_page( 'snippets', __('Add New Snippet'), __('Add New'), 'install_plugins', 'snippet', array( $this, 'admin_edit_loader' ) );
96+
$this->admin_manage_page = add_menu_page( __('Snippets'), __('Snippets'), 'install_plugins', $this->admin_manage_url, array( $this, 'admin_manage' ), $this->plugin_url . 'images/icon16.png', 67 );
97+
add_submenu_page('snippets', __('Snippets'), __('Manage Snippets') , 'install_plugins', $this->admin_manage_url, array( $this, 'admin_manage_loader') );
98+
$this->admin_edit_page = add_submenu_page( 'snippets', __('Add New Snippet'), __('Add New'), 'install_plugins', $this->admin_edit_url, array( $this, 'admin_edit' ) );
99+
$this->admin_import_page = add_submenu_page( 'snippets', __('Import Snippets'), __('Import'), 'install_plugins', $this->admin_import_url, array( $this, 'admin_import' ) );
100100

101+
$this->admin_manage_url = admin_url( 'admin.php?page=' . $this->admin_manage_url );
102+
$this->admin_edit_url = admin_url( 'admin.php?page=' . $this->admin_edit_url );
103+
$this->admin_import_url = admin_url( 'admin.php?page=' . $this->admin_import_url );
104+
101105
add_action( "admin_print_styles-$this->admin_manage_page", array( $this, 'load_stylesheet' ), 5 );
102106
add_action( "admin_print_styles-$this->admin_edit_page", array( $this, 'load_stylesheet' ), 5 );
107+
add_action( "admin_print_styles-$this->admin_import_page", array( $this, 'load_stylesheet' ), 5 );
103108
add_action( "admin_print_scripts-$this->admin_edit_page", array( $this, 'load_editarea' ), 5 );
104-
add_action( "load-$this->admin_manage_page", array( $this, 'admin_manage_help' ), 5 );
105-
add_action( "load-$this->admin_edit_page", array( $this, 'admin_edit_help' ), 5 );
109+
add_action( "load-$this->admin_manage_page", array( $this, 'admin_manage_loader' ), 5 );
110+
add_action( "load-$this->admin_edit_page", array( $this, 'admin_edit_loader' ), 5 );
111+
add_action( "load-$this->admin_import_page", array( $this, 'admin_import_loader' ), 5 );
106112
}
107113

108114
function load_stylesheet() {
109-
wp_enqueue_style('code-snippets-admin-style', plugins_url( 'css/style.css', $this->file), false, $this->version );
115+
wp_enqueue_style( 'code-snippets', plugins_url( 'css/style.css', $this->file ), false, $this->version );
110116
}
111117

112118
function load_editarea() {
113-
wp_register_script( 'editarea', plugins_url( 'includes/edit_area/edit_area_full.js', $this->file ), array( 'jquery' ), '0.8.2' );
119+
wp_register_script( 'editarea', plugins_url( 'includes/edit_area/edit_area_full.js', $this->file ), false, '0.8.2' );
114120
wp_enqueue_script( 'editarea' );
115121
}
116122

117-
function admin_manage_help() {
123+
function admin_manage_loader() {
118124

119-
$screen = get_current_screen();
120-
$screen->add_help_tab( array(
121-
'id' => 'overview',
122-
'title' => 'Overview',
123-
'content' =>
124-
"<p>Snippets are similar to plugins - they both extend and expand the functionality of WordPress. Snippets are more light-weight, just a few lines of code, and do not put as much load on your server. Here you can manage your existing snippets and preform tasks on them such as activating, deactivating, deleting and exporting.</p>"
125-
) );
126-
$screen->add_help_tab( array(
127-
'id' => 'compatibility-problems',
128-
'title' => 'Troubleshooting',
129-
'content' =>
130-
"<p>Be sure to check your snippets for errors before you activate them as a faulty snippet could bring your whole blog down. If your site starts doing strange things, deactivate all your snippets and activate them one at a time.</p>" .
131-
"<p>If something goes wrong with a snippet and you can&#8217;t use WordPress, you can use a database manager like phpMyAdmin to access the <code>$this->table</code> table in your WordPress database. Locate the offending snippet (if you know which one is the trouble) and change the 1 in the 'active' column into a 0. If this doesn't work try doing this for all snippets.<br/>You can also delete or rename the <code>$this->table</code> table and the table will automaticly be reconstructed so you can re-add snippets one at a time.</p>"
132-
) );
125+
require_once $this->plugin_dir . 'includes/export.php';
133126

134-
$screen->add_help_tab( array(
135-
'id' => 'uninstall',
136-
'title' => 'Uninstall',
137-
'content' =>
138-
"<p>When you delete Code Snippets through the Plugins menu in WordPress it will clear up the <code>$this->table</code> table and a few other bits of data stored in the database. If you want to keep this data (ie you are only temporally uninstalling Code Snippets) then remove the <code>".dirname(__FILE__)."</code> folder using FTP." .
139-
"<p>Even if you're sure that you don't want to use Code Snippets ever again on this WordPress installaion, you may want to use phpMyAdmin to back up the <code>$this->table</code> table in the database. You can later use phpMyAdmin to import it back.</p>"
140-
) );
141-
142-
$screen->set_help_sidebar(
143-
"<p><strong>For more information:</strong></p>" .
144-
"<p><a href='http://wordpress.org/extend/plugins/code-snippets' target='_blank'>WordPress Extend</a></p>" .
145-
"<p><a href='http://wordpress.org/support/plugin/code-snippets' target='_blank'>Support Forums</a></p>" .
146-
"<p><a href='http://bungeshea.wordpress.com/plugins/code-snippets' target='_blank'>SheaPress</a></p>"
147-
);
127+
if( isset( $_POST['action'] ) && isset( $_POST['ids'] ) )
128+
if( $_POST['action'] == 'export' && is_array( $_POST['ids'] ) )
129+
cs_export( $_POST['ids'], $this->table );
130+
131+
if( isset( $_POST['action2'] ) && isset( $_POST['ids'] ) )
132+
if( $_POST['action2'] == 'export' && is_array( $_POST['ids'] ) )
133+
cs_export( $_POST['ids'], $this->table );
134+
135+
if( isset( $_GET['action'] ) && isset( $_GET['id'] ) )
136+
if( $_GET['action'] == 'export' )
137+
cs_export( $_GET['id'], $this->table );
138+
139+
require_once $this->plugin_dir . 'includes/help/admin-manage-help.php';
148140
}
149141

150142
function admin_edit_title( $title ) {
151143
return str_ireplace( 'Add New Snippet', 'Edit Snippet', $title );
152144
}
153145

154-
function admin_edit_help() {
146+
function admin_edit_loader() {
155147

156148
if( isset( $_GET['action'] ) && @$_GET['action'] == 'edit' )
157149
add_filter( 'admin_title', array( $this, 'admin_edit_title' ) );
158150

159-
$screen = get_current_screen();
160-
$screen->add_help_tab( array(
161-
'id' => 'overview',
162-
'title' => 'Overview',
163-
'content' =>
164-
"<p>Snippets are similar to plugins - they both extend and expand the functionality of WordPress. Snippets are more light-weight, just a few lines of code, and do not put as much load on your server. Here you can add a new snippet or edit an existing one.</p>"
165-
) );
166-
$screen->add_help_tab( array(
167-
'id' => 'finding',
168-
'title' => 'Finding Snippets',
169-
'content' =>
170-
"<p>Here are some links to websites which host a large number of snippets that you can add to your site.
171-
<ul>
172-
<li><a href='http://wp-snippets.com' title='WordPress Snippets'>WP-Snippets</a></li>
173-
<li><a href='http://wpsnipp.com' title='WP Snipp'>WP Snipp</a></li>
174-
<li><a href='http://www.catswhocode.com/blog/snippets' title='Cats Who Code Snippet Library'>Cats Who Code</a></li>
175-
<li><a href='http://wpmu.org'>WPMU - The WordPress Experts</a></li>
176-
</ul>
177-
And below is a selection of snippets to get you started:
178-
<ul>
179-
<li><a title='Track post views using post meta' href='http://wpsnipp.com/index.php/functions-php/track-post-views-without-a-plugin-using-post-meta/' >Track post views using post meta</a></li>
180-
<li><a title='Disable Admin Bar' href='http://wp-snippets.com/disable-wp-3-1-admin-bar/'>Disable Admin Bar</a></li>
181-
<li><a title='Disable the Visual Editor' href='http://wp-snippets.com/disable-the-visual-editor/'>Disable the Visual Editor</a></li>
182-
<li><a title='Change Admin Logo' href='http://wp-snippets.com/change-admin-logo/'>Change Admin Logo</a></li>
183-
<li><a title='Display Code in Posts' href='http://wp-snippets.com/code-in-posts/'>Display Code in Posts</a></li>
184-
<li><a title='Grab Tweets from Twitter Feed' href='http://www.catswhocode.com/blog/snippets/grab-tweets-from-twitter-feed'>Grab Tweets from Twitter Feed</a></li>
185-
<li><a title='Watermark images on the fly' href='http://www.catswhocode.com/blog/snippets/watermark-images-on-the-fly'>Watermark images on the fly</a></li>
186-
<li><a title='Display number of Facebook fans in full text' href='http://www.catswhocode.com/blog/snippets/display-number-of-facebook-fans-in-full-text'>Display number of Facebook fans in full text</a></li>
187-
</ul>
188-
Snippets can be installed through the <a href='$this->admin_edit_url'>Add New Snippet</a> page or by addng them to the <code>$this->table</code> table in the database (Warning: for advanced users only). Once a snippet has been installed, you can activate it here.</p>"
189-
) );
190-
$screen->add_help_tab( array(
191-
'id' => 'adding',
192-
'title' => 'Adding Snippets',
193-
'content' =>
194-
"<p>You need to fill out the name and code fields for your snippet to be added. While the description field will add more information about how your snippet works, what is does and where you found it, it is completely optional.</p>" .
195-
"<p>Make sure that you don't add the <code>&lt;?php</code>, <code>&lt;?</code> or <code>?&gt;</code> the beginning and end of the code. You can however use these tags in the code to stop and start PHP sections</p>" .
196-
"<p>Please be sure to check thst your snippet is valid PHP code and will not produce errors before adding it through this page. While doing so will not become active straght away, it will help to minimise the chance of a faulty snippet becoming active on your site.</p>"
197-
) );
198-
199-
$screen->set_help_sidebar(
200-
"<p><strong>For more information:</strong></p>" .
201-
"<p><a href='http://wordpress.org/extend/plugins/code-snippets' target='_blank'>WordPress Extend</a></p>" .
202-
"<p><a href='http://wordpress.org/support/plugin/code-snippets' target='_blank'>Support Forums</a></p>" .
203-
"<p><a href='http://bungeshea.wordpress.com/plugins/code-snippets' target='_blank'>SheaPress</a></p>"
204-
);
151+
require_once $this->plugin_dir . 'includes/help/admin-edit-help.php';
152+
}
153+
154+
function admin_import_loader() {
155+
require_once $this->plugin_dir . 'includes/help/admin-import-help.php';
205156
}
206157

207158
function bulk_action( $action, $ids ) {
208-
if( !isset( $action ) && !isset( $ids ) && !is_array( $ids ) )
159+
if( ! isset( $action ) && ! isset( $ids ) && ! is_array( $ids ) )
209160
return false;
210161
global $wpdb;
211162
$count = 0;
@@ -237,7 +188,7 @@ function bulk_action( $action, $ids ) {
237188
}
238189
}
239190

240-
function admin_manage_loader() {
191+
function admin_manage() {
241192
global $wpdb;
242193

243194
$this->bulk_action( @$_POST['action'], @$_POST['ids'] );
@@ -261,16 +212,16 @@ function admin_manage_loader() {
261212
require_once $this->plugin_dir . 'includes/admin-manage.php';
262213
}
263214

264-
function admin_edit_loader() {
215+
function admin_edit() {
265216
global $wpdb;
266217
if( isset( $_POST['save_snippet'] ) ) {
267218
$name = mysql_real_escape_string( htmlspecialchars( $_POST['snippet_name' ] ) );
268219
$description = mysql_real_escape_string( htmlspecialchars( $_POST['snippet_description'] ) );
269220
$code = mysql_real_escape_string( htmlspecialchars( $_POST['snippet_code'] ) );
270221

271222
if( strlen( $name ) && strlen( $code ) ) {
272-
if( isset($_POST['edit_id'] ) ) {
273-
$wpdb-> query( "update $this->table set name='".$name."',
223+
if( isset( $_POST['edit_id'] ) ) {
224+
$wpdb->query( "update $this->table set name='".$name."',
274225
description='".$description."',
275226
code='".$code."'
276227
where id=" . intval($_POST["edit_id"]." limit 1"));
@@ -288,6 +239,19 @@ function admin_edit_loader() {
288239
require_once $this->plugin_dir . 'includes/admin-edit.php';
289240
}
290241

242+
function admin_import() {
243+
if( file_exists( $_FILES['cs_import_file']['tmp_name'] ) ) {
244+
global $wpdb;
245+
$xml = simplexml_load_string( file_get_contents( $_FILES['cs_import_file']['tmp_name'] ) );
246+
foreach( $xml->children() as $child ) {
247+
$wpdb->query( "insert into $this->table (name,description,code) VALUES ('$child->name','$child->description','$child->code')" );
248+
}
249+
250+
$msg = 'Imported ' . $xml->count() . ' snippets';
251+
}
252+
require_once( $this->plugin_dir . 'includes/admin-import.php');
253+
}
254+
291255
function settings_link( $links ) {
292256
array_unshift( $links, '<a href="' . $this->admin_manage_url . '" title="Manage your existing snippets">' . __('Manage') . '</a>' );
293257
return $links;
@@ -304,6 +268,7 @@ function plugin_meta( $links, $file ) {
304268
}
305269

306270
function run_snippets() {
271+
if( defined( 'CS_SAFE_MODE' ) ) if( CS_SAFE_MODE ) return;
307272
global $wpdb;
308273
// grab the active snippets from the database
309274
$active_snippets = $wpdb->get_results( 'select * FROM `' . $this->table . '` WHERE `active` = 1;' );

includes/admin-edit.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22
if( !class_exists( 'Code_Snippets' ) ) exit;
3-
$edit = isset( $_GET['action'] ) && $_GET['action'] == 'edit';
3+
$edit = isset( $_GET['id'] ) && intval( @$_GET['id'] );
44

55
if( $edit )
6-
$id = intval( $_GET['id'] );
6+
$id = intval( $_GET['id'] );
77
?>
88
<div class="wrap">
99
<div id="icon-snippets" class="icon32"><br /></div><h2><?php
@@ -19,33 +19,33 @@
1919
<?php endif; ?>
2020
<form method="post" action="">
2121
<?php if( $edit ) : ?>
22-
<?php $record = $wpdb->get_row( "SELECT * FROM `$this->table` WHERE `id` = '$id';" ); ?>
22+
<?php $snippet = $wpdb->get_row( "SELECT * FROM `$this->table` WHERE `id` = '$id';" ); ?>
2323
<input type="hidden" name="edit_id" value="<?php echo $id;?>" />
2424
<?php else: ?>
2525
<?php
2626
// define a empty object (or one with default values)
27-
$record = new stdClass();
28-
$record->name = '';
29-
$record->description = '';
30-
$record->code = '';
27+
$snippet = new stdClass();
28+
$snippet->name = '';
29+
$snippet->description = '';
30+
$snippet->code = '';
3131
?>
3232
<?php endif; ?>
3333

3434
<div id="titlediv">
3535
<div id="titlewrap">
3636
<label for="title" style="display:none">Name (short title)</label>
37-
<input id="title" type="text" autocomplete="off" size="30" name="snippet_name" value="<?php echo stripslashes( $record->name ); ?>" placeholder="Name (short title)">
37+
<input id="title" type="text" autocomplete="off" size="30" maxlength="36" name="snippet_name" value="<?php echo stripslashes( $snippet->name ); ?>" placeholder="Name (short title)" required>
3838
</div>
3939
</div>
4040

4141
<label for="snippet_code"><h3 style="display:inline">Code</h3>
4242
<span style="float:right">Enter or paste the snippet code without the <code>&lt;?php</code> and <code>?&gt;</code> tags.</span></label><br />
43-
<textarea id="snippet_code" name="snippet_code" rows="20" spellcheck="false" style="font-family:monospace;width:100%"><?php echo stripslashes( $record->code ); ?></textarea>
43+
<textarea id="snippet_code" name="snippet_code" rows="20" spellcheck="false" style="font-family:monospace;width:100%"><?php echo stripslashes( $snippet->code ); ?></textarea>
4444
<br style="margin: 20px;" />
4545
<div id="desclabel">
4646
<label for="description" style="text-align:center; margin: 10px auto"><h3 style="display:inline">Description</h3> (Optional)</label><br />
4747
</div>
48-
<?php wp_editor( htmlspecialchars_decode( stripslashes( $record->description ) ), 'description', array( 'textarea_name' => 'snippet_description', 'textarea_rows' => 10 ) ); ?>
48+
<?php wp_editor( htmlspecialchars_decode( stripslashes( $snippet->description ) ), 'description', array( 'textarea_name' => 'snippet_description', 'textarea_rows' => 10 ) ); ?>
4949
<p class="submit">
5050
<input tabindex="15" type="submit" name="save_snippet" class="button-primary" value="Save" />
5151
<a href="<?php echo $this->admin_manage_url; ?>" class="button">Cancel</a>

includes/admin-import.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<div class="wrap">
2+
<div id="icon-snippets" class="icon32"><br /></div><h2>Import Snippets</h2>
3+
<?php if ( isset( $msg ) ) : ?>
4+
<div id="message" class="updated fade"><p><?php echo $msg; ?></p></div>
5+
<?php endif; ?>
6+
<div class="narrow">
7+
<p>Howdy! Upload your Code Snippets export file and we&#8217;ll import the snippets to this site.</p>
8+
<p>You will need to go to the <a href="<?php echo $this->admin_manage_url; ?>">Manage Snippets</a> page to activate the imported snippets.</p>
9+
<p>Choose a Code Snippets (.xml) file to upload, then click Upload file and import.</p>
10+
<form enctype="multipart/form-data" id="import-upload-form" method="post" action="" name="cs_import">
11+
<p>
12+
<label for="upload">Choose a file from your computer:</label> (Maximum size: 8MB)
13+
<input type="file" id="upload" name="cs_import_file" size="25" accept="text/xml" />
14+
<input type="hidden" name="action" value="save" />
15+
<input type="hidden" name="max_file_size" value="8388608" />
16+
</p>
17+
<p class="submit"><input type="submit" name="submit" id="submit" class="button" value="Upload file and import" /></p></form>
18+
</div>
19+
</div>

0 commit comments

Comments
 (0)