Advertisement
Guest User

raw-html-snippets

a guest
May 10th, 2011
957
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.06 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Raw HTML Snippets
  4. Plugin URI: http://theandystratton.com
  5. Author: theandystratton
  6. Author URI: http://theandystratton.com
  7. Description: Uses a shortcode to give users multiple methods of properly inserting RAW HTML content without disabling core WordPress content filters.
  8. */
  9. add_shortcode('raw_html_snippet', 'rhs_raw_html_snippet_shortcode');
  10. function rhs_raw_html_snippet_shortcode( $atts, $content = '' ) {
  11. extract( shortcode_atts(array(
  12. 'id' => false
  13. ), $atts) );
  14.  
  15. if ( !$id )
  16. return '';
  17.  
  18. $snippet = get_option('rhs_snippet-' . $id);
  19.  
  20. return $snippet;
  21. }
  22.  
  23. add_action('admin_menu', 'rhs_raw_html_snippet_admin_menu');
  24. function rhs_raw_html_snippet_admin_menu() {
  25. add_submenu_page('options-general.php', 'Raw HTML Snippets', 'Raw HTML Snippets', 'edit_posts', 'raw-html-snippets', 'rhs_raw_html_snippet_settings');
  26. }
  27.  
  28. function rhs_raw_html_snippet_settings() {
  29.  
  30. if ( $_GET['edit'] )
  31. return rhs_raw_html_snippet_editor();
  32.  
  33. if ( $_GET['add'] )
  34. return rhs_raw_html_snippet_add();
  35.  
  36. $errors = array();
  37. $clean = array();
  38.  
  39. if ( $_GET['rhs_del'] && wp_verify_nonce($_GET['rhs_nonce'], 'rhs_delete') ) {
  40. delete_option('rhs_snippet-' . $_GET['rhs_del']);
  41. $snippet_list = get_option('rhs_snippet_list');
  42. if ( is_array($snippet_list) && in_array($_GET['rhs_del'], $snippet_list) ) {
  43. $snippet_list = array_diff( $snippet_list, array( $_GET['rhs_del'] ) );
  44. update_option('rhs_snippet_list', $snippet_list);
  45. $success = 'Snippet with ID &quot;' . esc_html($_GET['rhs_del']) . '&quot; successfully deleted.';
  46. }
  47. }
  48.  
  49.  
  50. $snippet_list = get_option('rhs_snippet_list');
  51. if ( !is_array($snippet_list) )
  52. $snippet_list = array();
  53.  
  54. ?>
  55. <div class="wrap">
  56. <h2>Manage Raw HTML Snippets</h2>
  57. <p>
  58. Create and manage your HTML snippets here. Name them with an id (letters, numbers, and dashes only) and then
  59. call them within your content via shortcode: <br />
  60. <code>[raw_html_snippet id="my-unique-id"]</code>
  61. </p>
  62. <p>
  63. This will use maintain core WordPress content filtering and autoformatting for all other elements within your content while
  64. allowing you to easily insert managed RAW HTML.
  65. </p>
  66. <p>
  67. <strong>WARNGING:</strong> This does not filter your HTML for errors or
  68. for malicious scripts. Use at your own risk.
  69. </p>
  70.  
  71. <?php if ( count($snippet_list) > 0 ) : ?>
  72.  
  73.  
  74. <form method="get" action="">
  75. <p class="alignright">
  76. <input type="hidden" name="page" value="raw-html-snippets" />
  77. <input type="hidden" name="add" value="1" />
  78. <input type="submit" class="button-primary" value="Add a New Raw HTML Snippet &raquo;" />
  79. </p>
  80. </form>
  81.  
  82. <h2>Your Snippet Library</h2>
  83.  
  84. <table class="widefat fixed">
  85. <thead>
  86. <tr>
  87. <th>Snippet Name</th>
  88. <th>Shortcode</th>
  89. <th>Actions</th>
  90. </tr>
  91. </thead>
  92. <tbody>
  93.  
  94. <?php foreach ( $snippet_list as $snippet_id ) : ?>
  95. <tr>
  96. <td>
  97. <?php echo esc_html($snippet_id);?>
  98. <div class="row-actions">
  99. <a href="?page=raw-html-snippets&amp;edit=<?php echo esc_attr($snippet_id); ?>">Edit</a> |
  100. <span class="trash"><a class="submitdelete" onclick="return confirm('Are you sure you want to delete this snippet?');" href="?page=raw-html-snippets&amp;rhs_nonce=<?php echo esc_attr(wp_create_nonce('rhs_delete')); ?>&amp;rhs_del=<?php echo esc_attr($snippet_id); ?>">Delete</a></span>
  101. </div>
  102. </td>
  103. <td>
  104. <code>[raw_html_snippet id="<?php echo esc_html($snippet_id); ?>"]</code>
  105. </td>
  106. <td>
  107. <a href="?page=raw-html-snippets&amp;edit=<?php echo esc_attr($snippet_id); ?>">Edit Snippet</a> |
  108. <span class="trash"><a onclick="return confirm('Are you sure you want to delete this snippet?');" href="?page=raw-html-snippets&amp;rhs_nonce=<?php echo esc_attr(wp_create_nonce('rhs_delete')); ?>&amp;rhs_del=<?php echo esc_attr($snippet_id); ?>">Delete Snippet</a></span>
  109. </td>
  110. </tr>
  111. <?php endforeach; ?>
  112.  
  113. </tbody>
  114. </table>
  115.  
  116. <?php else : ?>
  117. <h2>Your Snippets Library is Empty</h2>
  118. <p>You have no snippets, please <a href="?page=raw-html-snippets&amp;add=1">please add one</a>.</p>
  119. <?php endif; ?>
  120. </div>
  121. <?php
  122. }
  123.  
  124. function rhs_raw_html_snippet_editor() {
  125. $snippet_id = $_GET['edit'];
  126. $errors = array();
  127. if ( !empty($_POST) && wp_verify_nonce($_POST['rhs_nonce'], 'rhs_nonce') ) {
  128. $snippet = stripslashes($_POST['snippet_code']);
  129. if ( empty($snippet) )
  130. $errors[] = 'Enter some HTML for this snippet.';
  131. if ( count($errors) <= 0 ) {
  132. update_option('rhs_snippet-' . $snippet_id, $snippet);
  133. $success = 'Your changes have been saved.';
  134. }
  135. }
  136. $snippet = get_option('rhs_snippet-' . $snippet_id);
  137. $clean = array(
  138. 'snippet_code' => $snippet
  139. );
  140. ?>
  141. <div class="wrap">
  142. <h2>Edit Raw HTML Snippet: &quot;<?php echo esc_html($snippet_id); ?>&quot;</h2>
  143. <p><a href="?page=raw-html-snippets">&laquo; Back to main page</a></p>
  144. <form method="post" action="">
  145.  
  146. <?php if ( count($errors) > 0 ) : ?>
  147. <div class="message error"><?php echo wpautop(implode("\n", $errors)); ?></div>
  148. <?php endif; ?>
  149. <?php if ( $success ) : ?>
  150. <div class="message updated"><?php echo wpautop($success); ?></div>
  151. <?php endif; ?>
  152.  
  153. <?php wp_nonce_field('rhs_nonce', 'rhs_delete'); ?>
  154.  
  155. <p><label for="snippet_code">Snippet Code:</label></p>
  156. <textarea id="snippet_code" name="snippet_code" rows="10" style="font-family:Monaco,'Courier New',Courier,monospace;font-size:12px;width:80%;color:#555;"><?php
  157. if ( isset($clean['snippet_code']) )
  158. echo esc_attr($clean['snippet_code']);
  159. ?></textarea>
  160.  
  161. <p>
  162. <input type="submit" class="button-primary" value="Save Snippet &raquo;" />
  163. <input type="button" class="button" value="Delete This Snippet" onclick="if ( confirm('Are you sure you want to delete this snippet?') ) window.location = '?page=raw-html-snippets&amp;rhs_del=<?php echo esc_attr($snippet_id); ?>&amp;rhs_nonce=<?php echo esc_attr(wp_create_nonce('rhs_delete')); ?>';" />
  164. </p>
  165. </form>
  166. </div>
  167. <?php
  168. }
  169.  
  170. function rhs_raw_html_snippet_add() {
  171. $errors = array();
  172. $clean = array();
  173.  
  174. if ( !empty($_POST) && wp_verify_nonce($_POST['rhs_nonce'], 'rhs_nonce') ) {
  175.  
  176. foreach ( $_POST as $k => $v )
  177. $clean[$k] = stripslashes($v);
  178.  
  179. if ( empty($clean['snippet_id']) )
  180. $errors[] = 'Please enter a unique snippet ID.';
  181. elseif ( in_array(strtolower($clean['snippet_id']), $snippet_list) )
  182. $errors[] = 'You have entered a snippet ID that already exists. IDs are NOT case-sensitive.';
  183.  
  184. if ( empty($clean['snippet_code']) )
  185. $errors[] = 'Enter some HTML for this snippet.';
  186.  
  187. if ( count($errors) <= 0 ) {
  188. // save snippet
  189. $snippet_id = strtolower($clean['snippet_id']);
  190. $snippet_list[] = $snippet_id;
  191. update_option('rhs_snippet_list', $snippet_list);
  192. update_option('rhs_snippet-' . $snippet_id, $clean['snippet_code']);
  193. $success = 'Your snippet has been saved.';
  194. $clean = array();
  195. }
  196. }
  197.  
  198. ?>
  199. <div class="wrap">
  200. <h2>Add Raw HTML Snippet:</h2>
  201.  
  202. <p><a href="?page=raw-html-snippets">&laquo; Back to main page</a></p>
  203.  
  204. <form method="post" action="" style="margin: 1em 0;padding: 1px 1em;background: #fff;border: 1px solid #ccc;">
  205.  
  206. <?php if ( count($errors) > 0 ) : ?>
  207. <div class="message error"><?php echo wpautop(implode("\n", $errors)); ?></div>
  208. <?php endif; ?>
  209. <?php if ( $success ) : ?>
  210. <div class="message updated"><?php echo wpautop($success); ?></div>
  211. <?php endif; ?>
  212.  
  213. <?php wp_nonce_field('rhs_nonce', 'rhs_nonce'); ?>
  214.  
  215. <p>
  216. <label for="snippet_id">Snippet ID:</label>
  217. <br />
  218. <input type="text" name="snippet_id" id="snippet_id" size="40" value="<?php
  219. if ( isset($clean['snippet_id']) )
  220. echo esc_attr($clean['snippet_id']);
  221. ?>" />
  222. </p>
  223.  
  224. <p><label for="snippet_code">Snippet Code:</label></p>
  225. <textarea id="snippet_code" name="snippet_code" rows="10" style="font-family:Monaco,'Courier New',Courier,monospace;font-size:12px;width:80%;color:#555;"><?php
  226. if ( isset($clean['snippet_code']) )
  227. echo esc_attr($clean['snippet_code']);
  228. ?></textarea>
  229.  
  230. <p><input type="submit" class="button-primary" value="Add Snippet &raquo;" />
  231. </form>
  232. </div>
  233. <?php
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement