Advertisement
jrevillini

Untitled

Mar 6th, 2012
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.35 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Improved Simpler CSS
  4. Plugin URI:
  5. Description: Simplifies custom CSS on WordPress.
  6. Version: 1.01
  7. Author: CTLT Dev
  8. Author URI: http://ctlt.ubc.ca
  9. Forked from Jeremiah Orem's Custom User CSS plugin and then Frederick Ding http://simplerplugins.wordpress.com/
  10. */
  11.  
  12. /*  Copyright 2011  Enej
  13.  
  14.     This program is free software; you can redistribute it and/or modify
  15.     it under the terms of the GNU General Public License as published by
  16.     the Free Software Foundation; either version 2 of the License, or
  17.     (at your option) any later version.
  18.  
  19.     This program is distributed in the hope that it will be useful,
  20.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  21.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22.     GNU General Public License for more details.
  23.  
  24.     You should have received a copy of the GNU General Public License
  25.     along with this program; if not, write to the Free Software
  26.     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  27. */
  28. if($_GET['code'] != 'none'):
  29.     add_action('admin_print_scripts-appearance_page_imporved-simpler-css/improved-simpler-css', 'improved_simpler_css_admin_print_scripts');
  30. endif;
  31.  
  32. add_action( 'admin_print_scripts-appearance_page_imporved-simpler-css/improved-simpler-css', 'improved_simpler_css_admin_enqueue_scripts');
  33. add_action( 'admin_print_styles-appearance_page_imporved-simpler-css/improved-simpler-css', 'improved_simpler_css_admin_print_styles');
  34. add_action( 'admin_init', 'improved_simpler_css_init' );
  35. add_action( 'wp_before_admin_bar_render', 'improved_simpler_css_admin_bar_render' );
  36.  
  37.  
  38. // Override the edit link, the default link causes a redirect loop
  39. add_filter('get_edit_post_link', 'improved_simpler_css_revision_post_link');
  40.  
  41. /**
  42.  * improved_simpler_css_admin_print_styles function.
  43.  * adds styles to the admin page
  44.  * @access public
  45.  * @return void
  46.  */
  47. function improved_simpler_css_admin_print_styles() {
  48.     wp_enqueue_style('simpler-css', plugins_url('/imporved-simpler-css/css/styles.css'));
  49. }
  50. /**
  51.  * improved_simpler_css_admin_enqueue_scripts function.
  52.  * adds the pos
  53.  * @access public
  54.  * @return void
  55.  */
  56. function improved_simpler_css_admin_enqueue_scripts() {
  57.     // wp_enqueue_script( 'postbox' );
  58. }
  59. /**
  60.  * improved_simpler_css_admin_print_scripts function.
  61.  *
  62.  * @access public
  63.  * @return void
  64.  */
  65. function improved_simpler_css_admin_print_scripts() {
  66.     wp_enqueue_script('simpler-css', plugins_url('/imporved-simpler-css/js/codemirror.js'));
  67. }
  68.  
  69.  
  70.  
  71. /**
  72.  * improved_simpler_css_admin_bar_render function.
  73.  * Add the menu to the admin bar
  74.  * @access public
  75.  * @return void
  76.  */
  77. function improved_simpler_css_admin_bar_render() {
  78.     global $wp_admin_bar;
  79.     // we can remove a menu item, like the COMMENTS link
  80.     // just by knowing the right $id
  81.    
  82.     // we can add a submenu item too
  83.     $wp_admin_bar->add_menu( array(
  84.         'parent' => 'appearance',
  85.         'id' => 'custom-css',
  86.         'title' => __('Custom CSS'),
  87.         'href' => admin_url( 'themes.php?page=imporved-simpler-css/improved-simpler-css.php')
  88.     ) );
  89. }
  90.  
  91.  
  92.  
  93.  
  94. /**
  95.  * improved_simpler_css_revision_post_link function.
  96.  * Override the edit link, the default link causes a redirect loop
  97.  * @access public
  98.  * @param mixed $post_link
  99.  * @return void
  100.  */
  101. function improved_simpler_css_revision_post_link($post_link)
  102. {
  103.     global $post;
  104.    
  105.     if ( isset( $post ) && ( 's-custom-css' == $post->post_type ) )
  106.         if ( strstr( $post_link, 'action=edit' ) && !strstr( $post_link, 'revision=' ) )
  107.             $post_link = 'themes.php?page=imporved-simpler-css/improved-simpler-css.php';
  108.    
  109.     return $post_link;
  110.  
  111. }
  112.  
  113.  
  114. /**
  115.  * improved_simpler_css_init function.
  116.  * Init plugin options to white list our options
  117.  * @access public
  118.  * @return void
  119.  */
  120. function improved_simpler_css_init(){
  121.     /*
  122.     register_setting( 'simpler_css_options', 'simpler_css_css');
  123.     */
  124.     $args = array(
  125.         'public' => false,
  126.         'query_var' => true,
  127.         'capability_type' => 'nav_menu_item',
  128.         'supports'      => array('revisions')
  129.     );
  130.     register_post_type( 's-custom-css', array(
  131.         'supports' => array( 'revisions' )
  132.     ) );
  133.    
  134. }
  135.  
  136. /**
  137.  * improved_simpler_css_save_revision function.
  138.  * safe the revisoin
  139.  * @access public
  140.  * @param mixed $css
  141.  * @return void
  142.  */
  143. function improved_simpler_css_save_revision( $css ) {
  144.     file_put_contents(dirname(__FILE__) . '/generated.css', improved_simpler_css_filter ( $css ));
  145.     // If null, there was no original safecss record, so create one
  146.     if ( !$safecss_post = improved_simpler_css_get_css() ) {
  147.         $post = array();
  148.         $post['post_content'] = $css;
  149.         $post['post_title']   = 'Custom CSS';
  150.         $post['post_status']  = 'publish';
  151.         $post['post_type']    = 's-custom-css';
  152.        
  153.         // check if there are any settings data
  154.         $simpler_css_css = get_option ( 'simpler_css_css' );
  155.         if($simpler_css_css): // option settings exist
  156.             if(!is_array($simpler_css_css))
  157.                 $simpler_css_css = array($simpler_css_css);
  158.            
  159.             array_reverse($simpler_css_css);
  160.             $count = 0;
  161.             foreach($simpler_css_css  as $css_from_option):
  162.                 $post['post_content'] = $css_from_option;
  163.                 if($count == 0):
  164.                     $post_id = wp_insert_post( $post );
  165.                 else:  
  166.                     $post['ID'] = $post_id;
  167.                     wp_update_post( $post );
  168.                 endif;
  169.                
  170.                
  171.                 $count++; // increment the count
  172.             endforeach;
  173.            
  174.             // ok lets update the post for real this time
  175.             $post['post_content'] = $css; // really update the stuff
  176.             wp_update_post( $post );
  177.            
  178.             // time to delete the options that exits
  179.             delete_option('simpler_css_css');
  180.            
  181.         else: // there is no settins data lets save this stuff
  182.             $post_id = wp_insert_post( $post );
  183.         endif;
  184.        
  185.         return true;
  186.     } // there is a styles store in the custom post type
  187.    
  188.     $safecss_post['post_content'] = $css;
  189.    
  190.     wp_update_post( $safecss_post );
  191.     return true;
  192. }
  193. /**
  194.  * improved_simpler_css_get_css function.
  195.  * Get the custom css from posts table
  196.  * @access public
  197.  * @return void
  198.  */
  199. function improved_simpler_css_get_css()
  200. {
  201.     if ( $a = array_shift( get_posts( array( 'numberposts' => 1, 'post_type' => 's-custom-css', 'post_status' => 'publish' ) ) ) )
  202.         $safecss_post = get_object_vars( $a );
  203.     else
  204.         $safecss_post = false;
  205.  
  206.     return $safecss_post;
  207. }
  208. /**
  209.  * improved_simpler_css_always_get_css function.
  210.  * return the
  211.  * @access public
  212.  * @return void
  213.  */
  214. function improved_simpler_css_always_get_css()
  215. {
  216.    
  217.     if ( $a = array_shift( get_posts( array( 'numberposts' => 1, 'post_type' => 's-custom-css', 'post_status' => 'publish' ) ) ) ):
  218.         $safecss_post = get_object_vars( $a );
  219.         return $safecss_post['post_content'];
  220.     // if there is no
  221.     else:
  222.    
  223.         $simpler_css_css = get_option ( 'simpler_css_css' );
  224.         if(!empty($simpler_css_css)):
  225.             if(!is_array($simpler_css_css))
  226.                 $simpler_css_css = array($simpler_css_css);
  227.        
  228.             return $simpler_css_css[0];
  229.         else:
  230.             // return an empty string
  231.             return false;
  232.         endif;
  233.     endif;
  234.  
  235. }
  236.  
  237.  
  238.  
  239. function improved_simpler_css_addcss() {
  240.     $simpler_css_css = get_option ( 'simpler_css_css' );
  241.     if(!is_array($simpler_css_css))
  242.         $simpler_css_css = array($simpler_css_css);
  243.        
  244.     //echo '<style type="text/css">' . "\n";
  245.     //echo improved_simpler_css_filter ( improved_simpler_css_always_get_css() ) . "\n";
  246.     //echo '</style>' . "\n";
  247.     wp_enqueue_style('improved_simpler_css', plugins_url('generated.css', __FILE__));
  248. }
  249.  
  250. function improved_simpler_css_filter($_content) {
  251.     $_return = $_content;
  252.     $_return = preg_replace ( '/@import.+;/i', '', $_return ); // rem import stmts
  253.     $_return = preg_replace ( '/(\t|\n|\r)/', '', $_return ); // rem tabs & linebreaks
  254.     $_return = preg_replace ( '#/\*.+?\*/#s', '', $_return ); // rem comments
  255.     $_return = htmlspecialchars ( strip_tags($_return), ENT_NOQUOTES, 'UTF-8' );
  256.     return $_return;
  257. }
  258.  
  259. function improved_simpler_css_menu() {
  260.     add_theme_page ( 'Custom CSS', 'Custom CSS', 8, __FILE__, 'improved_simpler_css_options' );
  261. }
  262.  
  263. function improved_simpler_css_options() {
  264.    
  265. $simpler_css_default = '/* Welcome to Improved Simpler CSS!
  266.  
  267. If you are familiar with CSS, you may delete these comments and get started. CSS (Cascading Style Sheets) is a kind of code that tells the browser how to render a web page. Here\'s an example:
  268.  
  269. img { border: 1px dotted red; }
  270.  
  271. That line basically means "give images a dotted red border one pixel thick."
  272.  
  273. CSS is not very hard to learn. There are many free references to help you get started, like http://www.w3schools.com/css/default.asp
  274.  
  275. We hope you enjoy developing your custom CSS. Here are a few things to keep in mind:
  276. - You cannot edit the stylesheets of your theme. Your stylesheet will be loaded after the theme stylesheets, which means that your rules can take precedence and override the theme CSS rules.
  277. - CSS comments will be stripped from your stylesheet when outputted. */
  278.  
  279. /* This is a comment.*/
  280.  
  281. /*
  282. Things we strip out include:
  283. * HTML code
  284. * @import rules
  285. * comments (upon output)
  286.  
  287. Things we encourage include:
  288. * testing in several browsers!
  289. * trying things out!
  290.  
  291. (adapted from WordPress.com)
  292. */';
  293.  
  294.  
  295.     $updated = false;
  296.     $opt_name = 'simpler_css_css';
  297.  
  298.    
  299.     $css_val = improved_simpler_css_always_get_css();
  300.     if (!$css_val)
  301.         $css_val = array($simpler_css_default);
  302.     elseif(!is_array($css_val))
  303.         $css_val = array($css_val);
  304.    
  305.     // the form has been submited save the options
  306.     if (!empty($_POST) && check_admin_referer('update_simpler_css_css','update_simpler_css_css_field') ):
  307.        
  308.        
  309.         $css_form = stripslashes ( $_POST [$opt_name] );
  310.         improved_simpler_css_save_revision( $css_form );
  311.         $css_val[0] = $css_form;
  312.         $updated = true;
  313.         $message_number = 1;
  314.         ?>
  315.        
  316. <?php endif; // end of update  
  317.        
  318.     if(isset($_GET['message']))
  319.         $message_number = (int) $_GET['message'];
  320.            
  321.     if($message_number):
  322.        
  323.         $messages['s-custom-css'] = array(
  324.          1 => "Custom CSS Saved",
  325.          5 => isset($_GET['revision']) ? sprintf( __('Custom CSS restored to revision from %s, <em>Save Changes for the revision to take effect</em>'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false
  326.          );
  327.          $messages = apply_filters( 'post_updated_messages', $messages );
  328.          ?>
  329.         <div class="updated"><p><strong><?php echo $messages['s-custom-css'][$message_number]; ?></strong></p></div>       
  330.     <?php endif ?>
  331.  
  332.  
  333. <div class="wrap">
  334. <div class="icon32" id="icon-themes"><br></div>
  335. <h2>Custom CSS</h2>
  336. <div id="poststuff" class="metabox-holder<?php echo 2 == $screen_layout_columns ? ' has-right-sidebar' : ''; ?>">
  337. <ul class="subsubsub">
  338.     <?php if($_GET['code'] !="none" ): ?>
  339.     <li ><strong>Advanced Editor</strong> switch to: <a  href="?page=imporved-simpler-css/improved-simpler-css.php&code=none">simpler</a></li>
  340.     <?php else: ?>
  341.     <li ><strong>Simple Editor</strong> switch to: <a  href="?page=imporved-simpler-css/improved-simpler-css.php">advance</a></li>
  342.     <?php endif; ?>
  343. </ul>
  344. <div id="code-version-toggle">
  345. <h3 style="clear:both;">Edit CSS</h3>
  346.    
  347.     <?php if($_GET['code'] !="none"): ?>
  348.         <p class="search-box"><input type='text' style="width: 15em" id='query' value=''> <input type="button" onclick="search()" value="Search" class="button">  and replace with <input type='text' style="width: 15em" id="replace">
  349.         <input onclick="replace1();" type="button" class="button" value="Replace All">
  350.         </p>
  351.     <?php endif; ?>
  352.     <form method="post" action="themes.php?page=imporved-simpler-css/improved-simpler-css.php<?php if($_GET['code'] == "none") {echo "&code=none"; } ?>">
  353.         <?php settings_fields('simpler_css_options'); ?>
  354.         <?php wp_nonce_field( 'update_simpler_css_css','update_simpler_css_css_field' ); ?>
  355.         <textarea cols="80" rows="25" id="simpler_css_css" name="<?php echo $opt_name; ?>"><?php echo $css_val[0]; ?></textarea>
  356.         <input type="hidden" name="action" value="update" /> <input type="hidden" name="page_options" value="<?php echo $opt_name?>" />
  357.         <p class="submit"><input type="submit" class="button-primary" value="<?php _e ( 'Save Changes' )?>" /> <span id="unsaved_changes" <?php if(!isset($_GET['revision'])) { ?>style="display:none;"<?php } ?> >There are some unsaved changes</span> </p>
  358.     </form>
  359.     </div>
  360.  
  361. <?php if($_GET['code'] !="none"): ?>
  362. <script>
  363.     var css_change = 0;
  364.     var editor = CodeMirror.fromTextArea(document.getElementById("simpler_css_css"), {
  365.         mode: "text/css", lineNumbers: true, indentUnit:2,
  366.         onChange: function(){
  367.             css_change++;
  368.             if(css_change > 1)
  369.             {
  370.              jQuery("#unsaved_changes").show();
  371.            
  372.             }
  373.        
  374.         }
  375.         });
  376.     var lastPos = null, lastQuery = null, marked = []; 
  377.     function unmark() {
  378.       for (var i = 0; i < marked.length; ++i) marked[i]();
  379.       marked.length = 0;
  380.     }
  381.    
  382.     function search() {
  383.       unmark();                    
  384.       var text = document.getElementById("query").value;
  385.       if (!text) return;
  386.       for (var cursor = editor.getSearchCursor(text); cursor.findNext();)
  387.         marked.push(editor.markText(cursor.from(), cursor.to(), "searched"));
  388.    
  389.       if (lastQuery != text) lastPos = null;
  390.       var cursor = editor.getSearchCursor(text, lastPos || editor.getCursor());
  391.       if (!cursor.findNext()) {
  392.         cursor = editor.getSearchCursor(text);
  393.         if (!cursor.findNext()) return;
  394.       }
  395.       editor.setSelection(cursor.from(), cursor.to());
  396.       lastQuery = text; lastPos = cursor.to();
  397.     }
  398.    
  399.     function replace1() {
  400.       unmark();
  401.       var text = document.getElementById("query").value,
  402.           replace = document.getElementById("replace").value;
  403.       if (!text) return;
  404.       for (var cursor = editor.getSearchCursor(text); cursor.findNext();)
  405.         editor.replaceRange(replace, cursor.from(), cursor.to());
  406.     }
  407.  
  408. </script>
  409.  
  410. <?php endif;
  411.  
  412. $safecss_post = improved_simpler_css_get_css();
  413.  
  414. if ( 0 < $safecss_post['ID'] && wp_get_post_revisions( $safecss_post['ID'] ) ) {
  415.     function post_revisions_meta_box( $safecss_post ) {
  416.         // Specify numberposts and ordering args
  417.         $args = array( 'numberposts' => 5, 'orderby' => 'ID', 'order' => 'DESC' );
  418.         // Remove numberposts from args if show_all_rev is specified
  419.         if ( isset( $_GET['show_all_rev'] ) )
  420.             unset( $args['numberposts'] );
  421.  
  422.         wp_list_post_revisions( $safecss_post['ID'], $args );
  423.     }
  424.  
  425.     add_meta_box( 'revisionsdiv', __( 'CSS Revisions', 'safecss' ), 'post_revisions_meta_box', 's-custom-css', 'normal' );
  426.     do_meta_boxes( 's-custom-css', 'normal', $safecss_post );
  427. }
  428. ?>
  429. </div></div>
  430. <?php
  431. }
  432.  
  433. add_action ( 'admin_menu', 'improved_simpler_css_menu' );
  434. //add_action ( 'wp_head', 'improved_simpler_css_addcss' );
  435. add_action('wp_enqueue_scripts', 'improved_simpler_css_addcss');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement