Advertisement
Guest User

Corrections to WP Cross-Theme-Stylesheets 0.3

a guest
Sep 28th, 2013
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 18.29 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Cross-theme Stylesheets
  4. Plugin URI: http://scott.sauyet.com/php/wp-plugins/stylesheets/
  5. Description: Adds stylesheets to all themes.
  6. Author: Scott Sauyet
  7. Version: 0.3
  8. Author URI: http://scott.sauyet.com/
  9. */
  10. ?>
  11. <?php
  12. define("STYLESHEETS_DEBUG", false);
  13. $stylesheets_mgt_page_name = "Cross-theme Stylesheets";
  14. $stylesheets_media_types = array("aural", "braille", "embossed", "handheld", "projection", "print", "screen", "tty", "tv");
  15.  
  16.  
  17. // There must be a better way.  Want "../../wp-config.php", but that doesn't work?!
  18. require dirname(dirname(dirname(dirname(__FILE__)))) . "/wp-config.php";
  19.  
  20. $stylesheets_filename = basename(__FILE__);
  21. $stylesheets_adminurl = "edit.php?page=$stylesheets_filename";
  22. $stylesheets_index = -1;
  23.  
  24. $stylesheets_all = get_option("stylesheets_all_sheets");
  25.  
  26. if (!$stylesheets_all) {
  27.     if (!$_REQUEST['action'] == 'add-new') {
  28.         stylesheets_new_defaults();
  29.     }
  30. }
  31.  
  32. if (!$_REQUEST['action'] || $_REQUEST['action'] != "add-new") {
  33.     $stylesheets_current = stylesheets_choose_custom();
  34. }
  35.  
  36.  
  37. if (stristr(urldecode($_SERVER['REQUEST_URI']), stripslashes($stylesheets_current->name) . '.css')):
  38.     header('Content-type: text/css');
  39.     if ($stylesheets_current) {
  40.         echo(stripslashes($stylesheets_current->text));
  41.     } else {
  42.         echo("/* Stylesheet not found */\n");
  43.     }
  44. else:
  45.  
  46. add_action('wp_head', 'stylesheets_link');
  47. add_action('admin_menu', 'stylesheets_add_pages');
  48. add_action('admin_head','stylesheets_styles');
  49.  
  50. endif;
  51.  
  52. // ======================================================================
  53.  
  54. function stylesheets_choose_custom() {
  55.     global $stylesheets_all, $stylesheets_index;
  56.     $request = urldecode($_SERVER['REQUEST_URI']);
  57.     if (substr($request, strlen($request) - 4) == '.css') {
  58.         $name = urldecode(basename($_SERVER['REQUEST_URI'], ".css"));
  59.         $stylesheets_current = stylesheets_get_custom($name);
  60.     } else if ($_REQUEST['stylesheet_name']) {
  61.         $stylesheets_current = stylesheets_get_custom($_REQUEST['stylesheet_name']);
  62.     } else if (count($stylesheets_all) == 1) {
  63.         $stylesheets_current = $stylesheets_all[0];
  64.         $stylesheets_index = 0;
  65.     }
  66.  
  67.     return $stylesheets_current;
  68. }
  69.  
  70. function stylesheets_new_defaults() {
  71.     global $stylesheets_all;
  72.     $stylesheets_current->name = "my-styles";
  73.     $stylesheets_current->media = "all";
  74.     $stylesheets_current->import = false;
  75.     $stylesheets_current->text = "/* Enter your CSS here */\n";
  76.     $stylesheets_current->visible = true;
  77.     $stylesheets_all = array($stylesheets_current);
  78.     update_option("stylesheets_all_sheets", $stylesheets_all);
  79. }
  80.  
  81. function stylesheets_get_custom($name) {
  82.     global $stylesheets_all, $stylesheets_index;
  83.     for($index = 0; $index < count($stylesheets_all); $index++) {
  84.         $stylesheets_current = $stylesheets_all[$index];
  85.         if ($stylesheets_current->name == $name) {
  86.             $stylesheets_index = $index;
  87.             return $stylesheets_current;
  88.         }
  89.     }
  90.     return false;
  91. }
  92.  
  93. function stylesheets_link($unused) {
  94.     global $stylesheets_all, $stylesheets_filename;
  95.     $dir = get_settings('home') . "/wp-content/plugins/cross-theme-stylesheets";
  96.     if ($stylesheets_all) {
  97.         echo "\n";
  98.     }
  99.     foreach($stylesheets_all as $stylesheets_current) {
  100.         if ($stylesheets_current->visible) {
  101.             $media_list = stylesheets_get_media_list($stylesheets_current);
  102.             $name = urlencode(stripslashes($stylesheets_current->name));
  103.             if ($stylesheets_current->import) {
  104.                 if ($media_list == "all")  {
  105.                     $media_list = "";
  106.                 } else {
  107.                     $media_list = " " . $media_list;
  108.                 }
  109.                 echo "\t<style type='text/css'>\n\t\t@import url($dir/$stylesheets_filename/$name.css)$media_list;\n\t</style>\n";
  110.             } else {
  111.                 echo "\t<link rel='stylesheet' type='text/css' media='$media_list' href='$dir/$stylesheets_filename/$name.css' />\n";
  112.             }
  113.         }
  114.     }
  115. }
  116.  
  117. function stylesheets_get_media_list(&$stylesheets_current) {
  118.     $media = $stylesheets_current->media;
  119.     if (is_array($media)) {
  120.         $result = "";
  121.         $count = count($media);
  122.         if ($count > 0) {
  123.             $result =  $media[0];
  124.         }
  125.         for ($index = 1; $index < $count; $index++) {
  126.             $result .= ", " . $media[$index];
  127.         }
  128.         return $result;
  129.     }
  130.     return $media;
  131. }
  132.  
  133. function stylesheets_get_checked(&$stylesheets_current, $medium) {
  134.     if ($medium == $stylesheets_current->media) {
  135.         return true;
  136.     }
  137.     if ($medium == "choose" && $stylesheets_current->media != "all") {
  138.         return true;
  139.     }
  140.     if (!is_array($stylesheets_current->media)) {
  141.         return false;
  142.     }
  143.     return in_array($medium, $stylesheets_current->media);
  144. }
  145.  
  146. function stylesheets_get_unused_name(&$stylesheets_all) {
  147.     $suffix = 0;
  148.     do {
  149.         $suffix++;
  150.         $matched = false;
  151.         foreach ($stylesheets_all as $stylesheets_current) {
  152.             if ($stylesheets_current->name == "stylesheet-" . $suffix) {
  153.                 $matched = true;
  154.             }
  155.         }
  156.     } while ($matched);
  157.     return "stylesheet-" . $suffix;
  158. }
  159.  
  160. function stylesheets_add_pages() {
  161.     global $stylesheets_mgt_page_name;
  162.     add_management_page($stylesheets_mgt_page_name, 'Stylesheets', 8, 'stylesheets.php', 'stylesheets_management');
  163. }
  164.  
  165. function stylesheets_management() {
  166.     global $stylesheets_current, $stylesheets_media_types, $stylesheets_all, $stylesheets_index;
  167.     if (isset($_REQUEST['action'])) {
  168.         $action = $_REQUEST['action'];
  169.         $changes = false;
  170.         if ($action == "update") {
  171.             $stylesheets_current = stylesheets_get_custom($_REQUEST['current_stylesheet']);
  172.             if ($_REQUEST['stylesheet_new_name']) {
  173.                 $stylesheets_current->name = $_REQUEST['stylesheet_new_name'];
  174.                 $stylesheets_current->name = str_replace("\\", "", $stylesheets_current->name);
  175.                 $stylesheets_current->name = str_replace("/", "", $stylesheets_current->name);
  176.             }
  177.             if ($_REQUEST['stylesheet_text']) {
  178.                 $stylesheets_current->text = $_REQUEST['stylesheet_text'];
  179.             }
  180.             if ($_REQUEST['stylesheet_import']) {
  181.                 $stylesheets_current->import = true;
  182.             } else {
  183.                 $stylesheets_current->import = false;
  184.             }
  185.             if ($_REQUEST['media_types'] == "all") {
  186.                 $stylesheets_current->media = "all";
  187.             } else {
  188.                 $stylesheets_current->media = array();
  189.                 foreach ($stylesheets_media_types as $medium) {
  190.                     if ($_POST['media_type_' . $medium]) {
  191.                         $stylesheets_current->media[] = $medium;
  192.                     }
  193.                 }
  194.             }
  195.         } else if ($action == "edit") {
  196.             // Will show the edit screen since stylesheet_name is selected
  197.         } else if ($action == "hide") {
  198.             $stylesheets_current->visible = false;
  199.             $simple_action = true;
  200.         } else if ($action == "show") {
  201.             $stylesheets_current->visible = true;
  202.             // $simple_action = true;
  203.         } else if ($action == "delete") {
  204.             array_splice($stylesheets_all, $stylesheets_index, 1);
  205.             $delete = true;
  206.             $simple_action = true;
  207.         } else if ($action == "add-new") {
  208.             // unset($stylesheets_current);
  209.             $stylesheets_current=null;
  210.             $stylesheets_current->name = stylesheets_get_unused_name($stylesheets_all);
  211.             $stylesheets_current->media = "all";
  212.             $stylesheets_current->import = false;
  213.             $stylesheets_current->text = "/* Enter your CSS here */\n";
  214.             $stylesheets_current->visible = true;
  215.             $stylesheets_all[] = $stylesheets_current;
  216.         } else if ($action == "reset-defaults") {
  217.             // This is for debugging purposes.  Probably won't keep live, since it's open to abuse.
  218.             stylesheets_new_defaults();
  219.         } else {
  220.             echo("<h2>Unknown Action</h2>");
  221.         }
  222.  
  223.         if ($stylesheets_index >= 0 && !$delete) {
  224.             $stylesheets_all[$stylesheets_index] = $stylesheets_current;
  225.         }
  226.         update_option("stylesheets_all_sheets", $stylesheets_all);
  227.     }
  228.     ?><div class="stylesheets"><?php
  229.     if ($stylesheets_current && !$simple_action) {
  230.         stylesheets_current_controls($stylesheets_current);
  231.     }
  232.     stylesheets_list_controls();
  233.     stylesheets_debug();
  234.     ?></div><?php
  235. }
  236.  
  237. function stylesheets_current_controls(&$stylesheets_current) {
  238.     global $stylesheets_media_types;
  239.     $tabindex = 6;
  240.     ?>
  241.     <form name="update-stylesheet" method="post" action="<?php echo $stylesheets_adminurl; ?>"><div class="wrap">
  242.         <h2>Edit Stylesheet</h2>
  243.         <p>Styles entered here will appear on your blog regardless of the
  244.            template you choose.  This can be used to override specific CSS
  245.            for your chosen theme without making changes to the theme.  Or
  246.            it can add formatting to all themes at once.
  247.         </p>
  248.         <input type="hidden" name="action" value="update"/>
  249.         <div id="stylesheet-main-controls">
  250.             <p id="name-label"><label for="stylesheet_new_name">Name:</label></p>
  251.             <p id="name-holder"">
  252.                 <input type="text" name="stylesheet_new_name" id="stylesheet_new_name" tabindex="1" value="<?php echo stripslashes($stylesheets_current->name); ?>" />
  253.             </p>
  254.             <p id="css-string"><code>.css</code></p>
  255.             <p id="import-holder"><input type="checkbox" name="stylesheet_import" id="stylesheet_import"<?php if ($stylesheets_current->import) echo ' checked="checked"'; ?>/><label for="stylesheet_import"> Import?</label></p>
  256.             <p id="styles-label"><label for="stylesheet_text">Styles:</label></p>
  257.             <p id="styles-holder">
  258.                 <textarea rows="12" name="stylesheet_text" id="stylesheet_text" tabindex="2"><?php echo stripslashes($stylesheets_current->text); ?></textarea>
  259.             </p>
  260.         </div>
  261.         <div id="stylesheet-media-types">
  262.             <h3>Media Types</h3>
  263.             <ul>
  264.                 <li><input type="radio" name="media_types" value="all" id="media_types_all" tabindex="4"<?php if (stylesheets_get_checked($stylesheets_current, "all")) echo ' checked="checked"'; ?>/> <label for="media_types_all">All</label></li>
  265.                 <li><input type="radio" name="media_types" value="choose" id="media_types_choose" tabindex="5"<?php if (stylesheets_get_checked($stylesheets_current, "choose")) echo ' checked="checked"'; ?>/> <label for="media_types_choose">Choose:</label></li>
  266.                 <li>
  267.                     <ul>
  268. <?php foreach($stylesheets_media_types as $medium) { ?>
  269.                         <li><input type="checkbox" name="media_type_<?php echo $medium; ?>" id="media_type_<?php echo $medium; ?>" tabindex="<?php echo($tabindex++); ?>"<?php if (stylesheets_get_checked($stylesheets_current, $medium)) echo ' checked="checked"'; ?>/> <label for="media_type_<?php echo $medium; ?>"><?php echo $medium; ?></label></li>
  270. <?php } ?>
  271.                     </ul>
  272.                 </li>
  273.             </ul>
  274.         </div>
  275.         <div id="stylesheet-buttons" style="clear: both; text-align: center; padding-top: 1em;";>
  276.             <input type="submit" name="submit" value="Update" tabindex="3"/>
  277.         </div>
  278.         <div style="clear: both'">&nbsp;</div>
  279.         <input type="hidden" name="current_stylesheet" value="<?php echo($stylesheets_current->name); ?>"/>
  280.     </div></form>
  281. <?php
  282. }
  283.  
  284. function stylesheets_list_controls() {
  285.     global $stylesheets_all, $stylesheets_adminurl;
  286. ?>
  287.     <form name="all-stylesheets" method="post" action="<?php echo $stylesheets_adminurl; ?>"><div class="wrap">
  288.         <h2>Choose Stylesheet</h2>
  289. <?php
  290.     if ($stylesheets_all):
  291. ?>
  292.         <h3>Existing Stylesheets</h3>
  293.  
  294.         <table style="clear: both; margin-bottom: 1em;">
  295.           <tr>
  296.             <th>Name</th>
  297.             <th>Media Type(s)</th>
  298.             <th>Link / Import</th>
  299.             <th>Visibility</th>
  300.             <th>&nbsp;</th>
  301.             <th>&nbsp;</th>
  302.           </tr>
  303. <?php
  304.     $row = -1;
  305.     foreach ($stylesheets_all as $stylesheets_current) {
  306.         $row++;
  307.         $alternate = ($row %2 == 0) ? '<tr class="alternate">' : '<tr>';
  308.         $media = stylesheets_get_media_list($stylesheets_current);
  309.         $link_import = ($stylesheets_current->import) ? 'Import' : 'Link';
  310.         $visibility = ($stylesheets_current->visible) ? 'Visible' : 'Hidden';
  311.         $switch_visibility = ($stylesheets_current->visible) ? 'hide' : 'show';
  312.         $name = urlencode($stylesheets_current->name);
  313.         echo <<<STYLESHEET_ROW
  314.           $alternate
  315.             <td>$stylesheets_current->name</td>
  316.             <td>$media</td>
  317.             <td>$link_import</td>
  318.             <td>$visibility (<a href="$stylesheets_adminurl&stylesheet_name=$name&action=$switch_visibility">$switch_visibility</a>)</td>
  319.             <td><a href="$stylesheets_adminurl&stylesheet_name=$name&action=edit" class="edit">Edit</a></td>
  320.             <td><a href="$stylesheets_adminurl&stylesheet_name=$name&action=delete" onclick="return confirm('WARNING: You are about to delete this stylesheet.  You cannot undo this operation.\\n  \'Cancel\' to abort, \'OK\' to delete.');" class="delete">Delete</a></td>
  321.           </tr>
  322. STYLESHEET_ROW;
  323.     }
  324. ?>
  325.         </table>
  326. <?php
  327.     endif;
  328. ?>
  329.         <h3>New Stylesheet</h3>
  330.         <p style="float: left; width: 20%;"><input type="submit" name="submit" value="Add Stylesheet"/></p>
  331.         <p style="float: left; width: 70%;">You can keep all your styles
  332.           together in one main sheet.  But if you want to be able to
  333.           turn certain styles on or off, or just to organize the styles
  334.           in some more granular manner, you can maintain separate sheets.
  335.           Remember, though, that each stylesheet is a separate server hit.
  336.         </p>
  337.         <p style="clear:both; margin: 0; padding: 0">&nbsp;</p>
  338.         <input type="hidden" name="action" value="add-new"/>
  339.       </div>
  340.       <div class="wrap">
  341.         <h2>Notes</h2>
  342.         <ul>
  343.           <li>Only stylesheets marked "visible" will be added to the
  344.               document.  But the others are retained in the database
  345.               until you delete them.</li>
  346.           <li>"Import" means that the stylesheet will be called in this
  347.                manner:
  348. <pre>    &lt;style type="text/css">
  349.         @import url(http://my-blog.com/wp-content/plugins/cross-theme-stylesheets/stylesheets.php/my-styles.css) screen, projection;
  350.     &lt;/style></pre>
  351.           "Link" means that they will be called this way:
  352. <pre>   &lt;link rel="stylesheet" type="text/css" media="screen, projection"
  353.            href="http://my-blog.com/wp-content/plugins/cross-theme-stylesheets/stylesheets.php/my-styles.css" /></pre>
  354.            One good reason to importing a stylesheet is to <a
  355.              href="http://css-discuss.incutio.com/?page=ImportHack">hide advanced
  356.              styles</a> from older browsers.</li>
  357.            <li>Remember the <em>cascade</em> in <b>C</b>ascading
  358.                <b>S</b>tyle <b>S</b>heets.  Other styles included in the
  359.                theme might well override your styles here.  These are most
  360.                likely to take effect if you add classes specifically for
  361.                your new styles.  Alternately, you can mark your styles as
  362.                <strong><code>!important</code></strong>, and it's unlikely
  363.                they'll be overridden.</li>
  364.          </ul>
  365.     </div></form>
  366. <?php
  367. }
  368.  
  369. function stylesheets_styles() {
  370.     global $stylesheets_mgt_page_name;
  371.     if ($stylesheets_mgt_page_name != get_admin_page_title()) return;
  372. ?>
  373. <style type="text/css">
  374. .stylesheets pre {
  375.     border: 1px solid #ccc;
  376.     color: #009;
  377. }
  378. .stylesheets .wrap table th {
  379.     text-align: left;
  380. }
  381. .stylesheets .wrap th, .wrap td {
  382.     padding: 3px;
  383. }
  384. .stylesheets h3 {
  385.     clear: both;
  386.     margin-top: 1.5em;
  387. }
  388.  
  389. .stylesheets #stylesheet-main-controls {
  390.     width: 72%;
  391.     float: left;
  392.     padding: .5em;
  393.     margin-top: 1em;
  394.     background: #fff;
  395.     border: 1px solid #ccc;
  396. }
  397. .stylesheets #name-label {
  398.     float: left;
  399.     width: 10%;
  400.     text-align: right;
  401.     padding-right: .5em;
  402. }
  403. .stylesheets #name-holder {
  404.     width: 50%;
  405.     float: left;
  406. }
  407. .stylesheets #stylesheet_new_name {
  408.     width: 100%;
  409. }
  410. .stylesheets #css-string {
  411.     float: left;
  412.     width: 8%;
  413.     padding-left: 1em;
  414. }
  415. .stylesheets #import-holder {
  416.     float: left;
  417.     width: 20%;
  418.     text-align: right;
  419. }
  420. .stylesheets #styles-label {
  421.     float: left;
  422.     width: 10%;
  423.     clear: both;
  424.     text-align: right;
  425.     padding-right: .5em;
  426. }
  427. .stylesheets #styles-holder {
  428.     width: 80%;
  429.     float: left;
  430.     margin-top: 1em;
  431. }
  432. .stylesheets #stylesheet-main-controls textarea {
  433.     width: 100%; font-family: monospace;
  434. }
  435. .stylesheets #stylesheet-media-types {
  436.     width: 20%;
  437.  
  438.     float: left;
  439.     margin-left: 3%;
  440.     margin-top: 1em;
  441.     padding: .5em;
  442.     background: #fff;
  443.     border: 1px solid #ccc;
  444. }
  445. .stylesheets #stylesheet-media-types ul {
  446.     list-style-type: none;
  447.     padding-left: .5em;;
  448.     margin-left: 1em;
  449. }
  450. .stylesheets #stylesheet-media-types h3 {
  451.     margin-top: 0;
  452. }
  453. .stylesheets .wrap h3 {
  454.     border-bottom: 1px dotted #aaa;
  455. }
  456. .stylesheets .wrap table {
  457.     width: 100%;
  458. }
  459. .stylesheets .wrap th, .wrap td {
  460.     padding: 3px;
  461. }
  462.  
  463. </style>
  464. <?php  
  465. }
  466.  
  467. function stylesheets_debug() {
  468.     global $stylesheets_all, $stylesheets_current, $stylesheets_index;
  469.     if (STYLESHEETS_DEBUG) {
  470.         echo "<div class='wrap'>";
  471.         echo "<h2>Debug Info</h2>";
  472.  
  473.         echo "<h3>$" . "_REQUEST:</h3><pre>";
  474.         print_r($_REQUEST);
  475.         echo "</pre>";
  476.  
  477.         echo "<h3>$" . "stylesheets_all:</h3><pre>";
  478.         print_r($stylesheets_all);
  479.         echo "</pre>";
  480.  
  481.         echo "<h3>$" . "stylesheets_index: $stylesheets_index</h3>";
  482.  
  483.         echo "<h3>$" . "stylesheet:</h3><pre>";
  484.         print_r($stylesheets_current);
  485.         echo "</pre>";
  486.  
  487.         echo "<h3>$" . "_SERVER:</h3><pre>";
  488.         print_r($_SERVER);
  489.         echo "</pre>";
  490.  
  491.         echo "</div>";
  492.     }
  493. }
  494. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement