diff --git a/code-snippets.php b/code-snippets.php index 7e5c89cb..4e13840c 100644 --- a/code-snippets.php +++ b/code-snippets.php @@ -1,11 +1,10 @@ setup(); // initialise the varables and run the hooks @@ -54,16 +54,13 @@ public function Code_Snippets() { function setup() { global $wpdb; $this->file = __FILE__; - $this->table = $wpdb->prefix . $this->table; + $this->table = apply_filters( 'cs_table', $wpdb->prefix . $this->table ); $this->current_version = get_option( 'cs_db_version' ); - + $this->basename = plugin_basename( $this->file ); $this->plugin_dir = plugin_dir_path( $this->file ); $this->plugin_url = plugin_dir_url ( $this->file ); - - $this->admin_manage_url = admin_url( 'admin.php?page=' . $this->admin_manage_url ); - $this->admin_edit_url = admin_url( 'admin.php?page=' . $this->admin_edit_url ); - + add_action( 'admin_menu', array( $this, 'add_admin_menus' ) ); add_filter( 'plugin_action_links_' . $this->basename, array( $this, 'settings_link' ) ); add_filter( 'plugin_row_meta', array( $this, 'plugin_meta' ), 10, 2 ); @@ -87,125 +84,79 @@ function create_table() { } function upgrade() { - if( $this->current_version < $this->version ) { + if( $this->current_version < 1.2 ) { delete_option( 'cs_complete_uninstall' ); + } + if( $this->current_version < $this->version ) { update_option( 'cs_db_version', $this->version ); } } function add_admin_menus() { - $this->admin_manage_page = add_menu_page( __('Snippets'), __('Snippets'), 'install_plugins', 'snippets', array( $this, 'admin_manage_loader' ), $this->plugin_url . 'images/icon16.png', 67 ); - add_submenu_page('snippets', __('Snippets'), __('Manage Snippets') , 'install_plugins', 'snippets', array( $this, 'admin_manage_loader') ); - $this->admin_edit_page = add_submenu_page( 'snippets', __('Add New Snippet'), __('Add New'), 'install_plugins', 'snippet', array( $this, 'admin_edit_loader' ) ); + $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 ); + add_submenu_page('snippets', __('Snippets'), __('Manage Snippets') , 'install_plugins', $this->admin_manage_url, array( $this, 'admin_manage_loader') ); + $this->admin_edit_page = add_submenu_page( 'snippets', __('Add New Snippet'), __('Add New'), 'install_plugins', $this->admin_edit_url, array( $this, 'admin_edit' ) ); + $this->admin_import_page = add_submenu_page( 'snippets', __('Import Snippets'), __('Import'), 'install_plugins', $this->admin_import_url, array( $this, 'admin_import' ) ); + $this->admin_manage_url = admin_url( 'admin.php?page=' . $this->admin_manage_url ); + $this->admin_edit_url = admin_url( 'admin.php?page=' . $this->admin_edit_url ); + $this->admin_import_url = admin_url( 'admin.php?page=' . $this->admin_import_url ); + add_action( "admin_print_styles-$this->admin_manage_page", array( $this, 'load_stylesheet' ), 5 ); add_action( "admin_print_styles-$this->admin_edit_page", array( $this, 'load_stylesheet' ), 5 ); + add_action( "admin_print_styles-$this->admin_import_page", array( $this, 'load_stylesheet' ), 5 ); add_action( "admin_print_scripts-$this->admin_edit_page", array( $this, 'load_editarea' ), 5 ); - add_action( "load-$this->admin_manage_page", array( $this, 'admin_manage_help' ), 5 ); - add_action( "load-$this->admin_edit_page", array( $this, 'admin_edit_help' ), 5 ); + add_action( "load-$this->admin_manage_page", array( $this, 'admin_manage_loader' ), 5 ); + add_action( "load-$this->admin_edit_page", array( $this, 'admin_edit_loader' ), 5 ); + add_action( "load-$this->admin_import_page", array( $this, 'admin_import_loader' ), 5 ); } function load_stylesheet() { - wp_enqueue_style('code-snippets-admin-style', plugins_url( 'css/style.css', $this->file), false, $this->version ); + wp_enqueue_style( 'code-snippets', plugins_url( 'css/style.css', $this->file ), false, $this->version ); } function load_editarea() { - wp_register_script( 'editarea', plugins_url( 'includes/edit_area/edit_area_full.js', $this->file ), array( 'jquery' ), '0.8.2' ); + wp_register_script( 'editarea', plugins_url( 'includes/edit_area/edit_area_full.js', $this->file ), false, '0.8.2' ); wp_enqueue_script( 'editarea' ); } - function admin_manage_help() { + function admin_manage_loader() { - $screen = get_current_screen(); - $screen->add_help_tab( array( - 'id' => 'overview', - 'title' => 'Overview', - 'content' => - "

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.

" - ) ); - $screen->add_help_tab( array( - 'id' => 'compatibility-problems', - 'title' => 'Troubleshooting', - 'content' => - "

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.

" . - "

If something goes wrong with a snippet and you can’t use WordPress, you can use a database manager like phpMyAdmin to access the $this->table 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.
You can also delete or rename the $this->table table and the table will automaticly be reconstructed so you can re-add snippets one at a time.

" - ) ); + require_once $this->plugin_dir . 'includes/export.php'; - $screen->add_help_tab( array( - 'id' => 'uninstall', - 'title' => 'Uninstall', - 'content' => - "

When you delete Code Snippets through the Plugins menu in WordPress it will clear up the $this->table 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 ".dirname(__FILE__)." folder using FTP." . - "

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 $this->table table in the database. You can later use phpMyAdmin to import it back.

" - ) ); - - $screen->set_help_sidebar( - "

For more information:

" . - "

WordPress Extend

" . - "

Support Forums

" . - "

SheaPress

" - ); + if( isset( $_POST['action'] ) && isset( $_POST['ids'] ) ) + if( $_POST['action'] == 'export' && is_array( $_POST['ids'] ) ) + cs_export( $_POST['ids'], $this->table ); + + if( isset( $_POST['action2'] ) && isset( $_POST['ids'] ) ) + if( $_POST['action2'] == 'export' && is_array( $_POST['ids'] ) ) + cs_export( $_POST['ids'], $this->table ); + + if( isset( $_GET['action'] ) && isset( $_GET['id'] ) ) + if( $_GET['action'] == 'export' ) + cs_export( $_GET['id'], $this->table ); + + require_once $this->plugin_dir . 'includes/help/admin-manage-help.php'; } function admin_edit_title( $title ) { return str_ireplace( 'Add New Snippet', 'Edit Snippet', $title ); } - function admin_edit_help() { + function admin_edit_loader() { if( isset( $_GET['action'] ) && @$_GET['action'] == 'edit' ) add_filter( 'admin_title', array( $this, 'admin_edit_title' ) ); - $screen = get_current_screen(); - $screen->add_help_tab( array( - 'id' => 'overview', - 'title' => 'Overview', - 'content' => - "

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.

" - ) ); - $screen->add_help_tab( array( - 'id' => 'finding', - 'title' => 'Finding Snippets', - 'content' => - "

Here are some links to websites which host a large number of snippets that you can add to your site. -

- And below is a selection of snippets to get you started: - - Snippets can be installed through the Add New Snippet page or by addng them to the $this->table table in the database (Warning: for advanced users only). Once a snippet has been installed, you can activate it here.

" - ) ); - $screen->add_help_tab( array( - 'id' => 'adding', - 'title' => 'Adding Snippets', - 'content' => - "

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.

" . - "

Make sure that you don't add the <?php, <? or ?> the beginning and end of the code. You can however use these tags in the code to stop and start PHP sections

" . - "

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.

" - ) ); - - $screen->set_help_sidebar( - "

For more information:

" . - "

WordPress Extend

" . - "

Support Forums

" . - "

SheaPress

" - ); + require_once $this->plugin_dir . 'includes/help/admin-edit-help.php'; + } + + function admin_import_loader() { + require_once $this->plugin_dir . 'includes/help/admin-import-help.php'; } function bulk_action( $action, $ids ) { - if( !isset( $action ) && !isset( $ids ) && !is_array( $ids ) ) + if( ! isset( $action ) && ! isset( $ids ) && ! is_array( $ids ) ) return false; global $wpdb; $count = 0; @@ -237,7 +188,7 @@ function bulk_action( $action, $ids ) { } } - function admin_manage_loader() { + function admin_manage() { global $wpdb; $this->bulk_action( @$_POST['action'], @$_POST['ids'] ); @@ -261,7 +212,7 @@ function admin_manage_loader() { require_once $this->plugin_dir . 'includes/admin-manage.php'; } - function admin_edit_loader() { + function admin_edit() { global $wpdb; if( isset( $_POST['save_snippet'] ) ) { $name = mysql_real_escape_string( htmlspecialchars( $_POST['snippet_name' ] ) ); @@ -269,8 +220,8 @@ function admin_edit_loader() { $code = mysql_real_escape_string( htmlspecialchars( $_POST['snippet_code'] ) ); if( strlen( $name ) && strlen( $code ) ) { - if( isset($_POST['edit_id'] ) ) { - $wpdb-> query( "update $this->table set name='".$name."', + if( isset( $_POST['edit_id'] ) ) { + $wpdb->query( "update $this->table set name='".$name."', description='".$description."', code='".$code."' where id=" . intval($_POST["edit_id"]." limit 1")); @@ -288,6 +239,19 @@ function admin_edit_loader() { require_once $this->plugin_dir . 'includes/admin-edit.php'; } + function admin_import() { + if( file_exists( $_FILES['cs_import_file']['tmp_name'] ) ) { + global $wpdb; + $xml = simplexml_load_string( file_get_contents( $_FILES['cs_import_file']['tmp_name'] ) ); + foreach( $xml->children() as $child ) { + $wpdb->query( "insert into $this->table (name,description,code) VALUES ('$child->name','$child->description','$child->code')" ); + } + + $msg = 'Imported ' . $xml->count() . ' snippets'; + } + require_once( $this->plugin_dir . 'includes/admin-import.php'); + } + function settings_link( $links ) { array_unshift( $links, '' . __('Manage') . '' ); return $links; @@ -304,6 +268,7 @@ function plugin_meta( $links, $file ) { } function run_snippets() { + if( defined( 'CS_SAFE_MODE' ) ) if( CS_SAFE_MODE ) return; global $wpdb; // grab the active snippets from the database $active_snippets = $wpdb->get_results( 'select * FROM `' . $this->table . '` WHERE `active` = 1;' ); diff --git a/includes/admin-edit.php b/includes/admin-edit.php index 12b0a38c..8a8f8277 100644 --- a/includes/admin-edit.php +++ b/includes/admin-edit.php @@ -1,9 +1,9 @@

- get_row( "SELECT * FROM `$this->table` WHERE `id` = '$id';" ); ?> + get_row( "SELECT * FROM `$this->table` WHERE `id` = '$id';" ); ?> name = ''; - $record->description = ''; - $record->code = ''; + $snippet = new stdClass(); + $snippet->name = ''; + $snippet->description = ''; + $snippet->code = ''; ?>
- +

- +

- description ) ), 'description', array( 'textarea_name' => 'snippet_description', 'textarea_rows' => 10 ) ); ?> + description ) ), 'description', array( 'textarea_name' => 'snippet_description', 'textarea_rows' => 10 ) ); ?>

Cancel diff --git a/includes/admin-import.php b/includes/admin-import.php new file mode 100644 index 00000000..0ae9f279 --- /dev/null +++ b/includes/admin-import.php @@ -0,0 +1,19 @@ +

+

Import Snippets

+ +

+ +
+

Howdy! Upload your Code Snippets export file and we’ll import the snippets to this site.

+

You will need to go to the Manage Snippets page to activate the imported snippets.

+

Choose a Code Snippets (.xml) file to upload, then click Upload file and import.

+ +

+ (Maximum size: 8MB) + + + +

+

+
+
\ No newline at end of file diff --git a/includes/admin-manage.php b/includes/admin-manage.php index e86feb2e..96de5a79 100644 --- a/includes/admin-manage.php +++ b/includes/admin-manage.php @@ -15,6 +15,7 @@ + @@ -45,6 +46,7 @@ Deactivate | Edit | + Export | Delete

@@ -71,6 +73,7 @@ + diff --git a/includes/export.php b/includes/export.php new file mode 100644 index 00000000..298403e8 --- /dev/null +++ b/includes/export.php @@ -0,0 +1,59 @@ +prefix . 'snippets' ); + + if( ! is_array( $ids ) ) { + $ids = array( $ids ); + } + + $sitename = sanitize_key( get_bloginfo( 'name' ) ); + + $filename = 'code-snippets.' . $sitename; + + if( count( $ids ) < 2 ) { + $entry = $wpdb->get_row( "select * from $table where id=" . intval( $ids ) ); + $filename = sanitize_title( $entry->name, 'snippet' ) . '.code-snippets'; + } + + $filename = apply_filters( 'cs_export_filename', $filename ); + + header( 'Content-Disposition: attachment; filename='.$filename.'.xml;' ); + header( 'Content-Type: text/xml; charset=utf-8' ); + + echo '' . "\n"; + echo ''; + + foreach( $ids as $id ) { + + $id = intval( mysql_real_escape_string( $id ) ); + + if( ! $id > 0 ) continue; // skip this one if we don't have a valid ID + + $snippet = $wpdb->get_row( "select * from $table where id=$id" ); + + echo "\n\t" . ''; + echo "\n\t\t" . "$snippet->name"; + echo "\n\t\t" . "$snippet->description"; + echo "\n\t\t" . "$snippet->code"; + echo "\n\t" . ''; + } + + echo "\n"; + exit; +} + +endif; // function exists check \ No newline at end of file diff --git a/includes/help/admin-edit-help.php b/includes/help/admin-edit-help.php new file mode 100644 index 00000000..cd1e905d --- /dev/null +++ b/includes/help/admin-edit-help.php @@ -0,0 +1,47 @@ +add_help_tab( array( + 'id' => 'overview', + 'title' => 'Overview', + 'content' => + "

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.

" +) ); +$screen->add_help_tab( array( + 'id' => 'finding', + 'title' => 'Finding Snippets', + 'content' => + "

Here are some links to websites which host a large number of snippets that you can add to your site. +

+ And below is a selection of snippets to get you started: + + Snippets can be installed through the Add New Snippet page or by addng them to the $this->table table in the database (Warning: for advanced users only). Once a snippet has been installed, you can activate it here.

" +) ); +$screen->add_help_tab( array( + 'id' => 'adding', + 'title' => 'Adding Snippets', + 'content' => + "

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.

" . + "

Make sure that you don't add the <?php, <? or ?> the beginning and end of the code. You can however use these tags in the code to stop and start PHP sections

" . + "

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.

" +) ); + +$screen->set_help_sidebar( + "

For more information:

" . + "

WordPress Extend

" . + "

Support Forums

" . + "

SheaPress

" +); \ No newline at end of file diff --git a/includes/help/admin-import-help.php b/includes/help/admin-import-help.php new file mode 100644 index 00000000..204dbd72 --- /dev/null +++ b/includes/help/admin-import-help.php @@ -0,0 +1,29 @@ +add_help_tab( array( + 'id' => 'overview', + 'title' => 'Overview', + 'content' => + "

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 load snippets from a Code Snippets (.xml) import file into the database with your existing snippets.

" +) ); + +$screen->add_help_tab( array( + 'id' => 'import', + 'title' => 'Importing', + 'content' => + "

You can load your snippets from a Code Snippets (.xml) import file using this page. Snippets will be added to the database along with your existing snippets. Regardless of whether the snippets were active on the previous site, imported snippets are always inactive until activated using the Manage Snippets page.

" +) ); + +$screen->add_help_tab( array( + 'id' => 'export', + 'title' => 'Exporting', + 'content' => + "

You can save your snippets to a Code Snippets (.xml) export file using the Manage Snippets page.

" +) ); + +$screen->set_help_sidebar( + "

For more information:

" . + "

WordPress Extend

" . + "

Support Forums

" . + "

SheaPress

" +); \ No newline at end of file diff --git a/includes/help/admin-manage-help.php b/includes/help/admin-manage-help.php new file mode 100644 index 00000000..fc05df97 --- /dev/null +++ b/includes/help/admin-manage-help.php @@ -0,0 +1,30 @@ +add_help_tab( array( + 'id' => 'overview', + 'title' => 'Overview', + 'content' => + "

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.

" +) ); +$screen->add_help_tab( array( + 'id' => 'compatibility-problems', + 'title' => 'Troubleshooting', + 'content' => + "

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.

" . + "

If something goes wrong with a snippet and you can’t use WordPress, you can use a database manager like phpMyAdmin to access the $this->table 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.
You can also delete or rename the $this->table table and the table will automaticly be reconstructed so you can re-add snippets one at a time.

" +) ); + +$screen->add_help_tab( array( + 'id' => 'uninstall', + 'title' => 'Uninstall', + 'content' => + "

When you delete Code Snippets through the Plugins menu in WordPress it will clear up the $this->table 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 ".dirname(__FILE__)." folder using FTP." . + "

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 $this->table table in the database. You can later use phpMyAdmin to import it back.

" +) ); + +$screen->set_help_sidebar( + "

For more information:

" . + "

WordPress Extend

" . + "

Support Forums

" . + "

SheaPress

" +); \ No newline at end of file diff --git a/readme.txt b/readme.txt index 110b9ee6..00ee6a8b 100644 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Donate link: http://bungeshea.wordpress.com/donate/ Tags: snippets, code, php Requires at least: 3.3 Tested up to: 3.4.1 -Stable tag: 1.2 +Stable tag: 1.3 License: GPLv3 or later License URI: http://www.gnu.org/copyleft/gpl.html @@ -24,7 +24,7 @@ Further information and screenshots are available on the [plugin homepage](http: Code Snippets was featured on WPMU: [WordPress Code Snippets: Keep them Organized with this Plugin!](http://wpmu.org/wordpress-code-snippets) -If you have any feedback, issues or suggestions for improvements please start a topic in the [Support Forum](http://wordpress.org/support/plugin/code-snippets) and if you like the plugin please give it a perfect rating at [WordPress.org](http://wordpress.org/extend/plugins/code-snippets) :-) +If you have any feedback, issues or suggestions for improvements please leave start a topic in the [Support Forum](http://wordpress.org/support/plugin/code-snippets) and if you like the plugin please give it a perfect rating at [WordPress.org](http://wordpress.org/extend/plugins/code-snippets) :-) You can also contribute to the code at [GitHub](https://github.com/bungeshea/code-snippets). @@ -54,11 +54,14 @@ No, the snippets are added to the WordPress database so are independent of the t Yes, when you delete Code Snippets using the 'Plugins' menu in WordPress it will clean up the database table and a few other bits of data. Be careful not to remove Code Snippets using the Plugins menu unless you want this to happen. = Can I copy any snippets I've created to another WordPress site? = -The import/export feature is currently in development. You can however, use the export feature of phpMyAdmin to copy the `wp_snippets` table to another WordPress database. +Yes! You can individualy export a single snippet using the link below the snippet name on the 'Manage Snippets' page or bulk export multiple snippets using the 'Bulk Actions' feature. Snippets can later be imported using the 'Import Snippets' page by uploading the export file. = Can I run network-wide snippets on a multisite installation? = No, this feature is currently not avalible and will be coming in a future release. In the mean time activate Code Snippets individualy on the desired sites. += I have an idea for a cool feature for Code Snippets! = +That's great! Let me know by starting (or adding to) a topic in the [Support Forums](http://wordpress.org/support/plugin/code-snippets/). + == Screenshots == 1. The Manage Snippets page @@ -68,10 +71,15 @@ No, this feature is currently not avalible and will be coming in a future releas == Changelog == += 1.3 = +* Added export option to 'Manage Snippets' page +* Added 'Import Snippets' page + = 1.2 = * Minor improvements * Added code highlighting -* Removed 'Uninstall Plugin' page. Data will now be cleaned up when plugin is deleted through WordPress admin. +* Removed 'Uninstall Plugin' page +* Data will now be cleaned up when plugin is deleted through WordPress admin. = 1.1 = * Fixed a permissions bug with `DISALLOW_FILE_EDIT` being set to true @@ -82,10 +90,13 @@ No, this feature is currently not avalible and will be coming in a future releas == Upgrade Notice == += 1.3 = +Added import/export feature + = 1.2 = -* Minor improvments -* Added code highlighting -* Plugin data will now be cleaned up when you delete the plugin. +Minor improvments | +Added code highlighting | +Plugin data will now be cleaned up when you delete the plugin. = 1.1 = * Minor bug fixes and improvements on the the 'Edit Snippet' page \ No newline at end of file