plug_color_admin_posts.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. if (!class_exists("plug_color_admin_posts")) {
  3. class plug_color_admin_posts {
  4. private $options = array(); // Set $options in array
  5. private $settings = array(); // Set $setting in array
  6. var $hasform = true;
  7. var $startet = false;
  8. function __construct() {
  9. $this->title = __("Color admin posts", "dagsopt");
  10. }
  11. function start() {
  12. $this->startet = true;
  13. add_action('admin_menu', array(&$this, 'add_submenu'));
  14. add_action('admin_init', array(&$this, 'settings_api_init'));
  15. $this->options = get_option('color_admin_posts');
  16. add_action('admin_print_styles-edit.php', array(&$this, 'load_color'));
  17. add_action('admin_head', array(&$this, 'load_farbtastic'));
  18. if (!is_array(get_option('color_admin_posts'))) {$this->activate();}
  19. }
  20. function activate() {
  21. $options = array(
  22. 'color_draft' => '#FFFF99',
  23. 'color_pending' => '#87C5D6',
  24. 'color_published' => '#',
  25. 'color_future' => '#CCFF99',
  26. 'color_private' => '#FFCC99',
  27. );
  28. add_option('color_admin_posts', $options);
  29. }
  30. function deactivate() {
  31. delete_option('color_admin_posts');
  32. }
  33. function load_color() {
  34. $options = $this->options;
  35. ?>
  36. <style>
  37. .status-draft {
  38. background-color: <?php echo $options['color_draft']; ?> !important;
  39. }
  40. .status-future {
  41. background-color: <?php echo $options['color_future']; ?> !important;
  42. }
  43. .status-publish {
  44. background-color: <?php echo $options['color_published']; ?> !important;
  45. }
  46. .status-pending {
  47. background-color: <?php echo $options['color_pending']; ?> !important;
  48. }
  49. .status-private {
  50. background-color: <?php echo $options['color_private']; ?> !important;
  51. }
  52. </style>
  53. <?php
  54. }
  55. function load_farbtastic() {
  56. global $current_screen;
  57. // if($current_screen->id == 'appearance_page_color-admin-post') {
  58. wp_enqueue_style('farbtastic');
  59. wp_enqueue_script('farbtastic');
  60. // }
  61. }
  62. function get_color_admin_post_settings() {
  63. $this->settings['color_draft'] = array(
  64. 'title' => __('Drafts Posts', 'dagsopt'),
  65. );
  66. $this->settings['color_pending'] = array(
  67. 'section' => 'general',
  68. 'title' => __('Pendings Posts', 'dagsopt'),
  69. );
  70. $this->settings['color_published'] = array(
  71. 'title' => __('Published Posts', 'dagsopt'),
  72. );
  73. $this->settings['color_future'] = array(
  74. 'title' => __('Futures Posts', 'dagsopt'),
  75. );
  76. $this->settings['color_private'] = array(
  77. 'title' => __('Privates Posts', 'dagsopt'),
  78. );
  79. }
  80. function create_settings($args = array()) {
  81. extract($args);
  82. $field_args = array(
  83. 'id' => $id,
  84. 'label_for' => $id,
  85. );
  86. add_settings_field($id, $title, array(
  87. $this,
  88. 'display_settings',
  89. ), __FILE__, 'general', $field_args);
  90. }
  91. public function display_settings($args = array()) {
  92. extract($args);
  93. $options = $this->options;
  94. echo '<input class="regular-text" type="text" maxlength="7" id="' . $id . '" name="color_admin_posts[' . $id . ']" value="' . esc_attr($options[$id]) . '" />
  95. <br /><span class="description">' . '</span>
  96. <div id="farbtastic-' . $id . '" class="farbtastic"></div>';
  97. }
  98. function settings_api_init() {
  99. register_setting('color_admin_posts', 'color_admin_posts', array(
  100. &$this,
  101. 'validate_settings',
  102. ));
  103. add_settings_section('general', '', array(
  104. &$this,
  105. 'general_section_callback',
  106. ), __FILE__);
  107. // Get the configuration of fields
  108. $this->get_color_admin_post_settings();
  109. // Generate fields
  110. foreach ($this->settings as $id => $setting) {
  111. $setting['id'] = $id;
  112. $this->create_settings($setting);
  113. }
  114. }
  115. function general_section_callback() {
  116. echo '<p>' . __('Leave "#" for the default color.', 'dagsopt') . '</p>';
  117. }
  118. function validate_settings($input) {
  119. $input['color_draft'] = (preg_match('/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/', $input['color_draft'])) ? $input['color_draft'] : '#';
  120. $input['color_pending'] = (preg_match('/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/', $input['color_pending'])) ? $input['color_pending'] : '#';
  121. $input['color_published'] = (preg_match('/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/', $input['color_published'])) ? $input['color_published'] : '#';
  122. $input['color_future'] = (preg_match('/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/', $input['color_future'])) ? $input['color_future'] : '#';
  123. $input['color_private'] = (preg_match('/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/', $input['color_private'])) ? $input['color_private'] : '#';
  124. return $input;
  125. }
  126. function add_submenu() {
  127. $page = add_submenu_page('xwpplugins', __("Color Admin Posts"), __("Color Admin Posts"), 'edit_theme_options', 'color-admin-post', array(&$this, 'display_page'));
  128. }
  129. function display_page() {
  130. // Check if user can access to the plugin
  131. if (!current_user_can('administrator')) {
  132. wp_die(__('You do not have sufficient permissions to access this page.', 'dagsopt'));
  133. }
  134. ?>
  135. <div class="wrap">
  136. <div id="icon-options-general" class="icon32"></div>
  137. <h2>Color Admin Posts</h2>
  138. <form id="form-color-admin-posts" method="post" action="options.php">
  139. <?php
  140. settings_fields('color_admin_posts');
  141. do_settings_sections(__FILE__);
  142. submit_button(__('Save Changes', 'dagsopt'));
  143. ?>
  144. </form>
  145. </div>
  146. <script type="text/javascript">
  147. jQuery(function(){
  148. jQuery(document).ready(function() {
  149. jQuery('.regular-text').each(function() {
  150. jQuery('#farbtastic-'+this.id).hide();
  151. jQuery('#farbtastic-'+this.id).farbtastic(this);
  152. jQuery(this).click(function(){
  153. jQuery('#farbtastic-'+this.id).fadeIn();
  154. });
  155. jQuery(this).keyup(function() {
  156. if( jQuery(this).val() == '' ) {
  157. jQuery(this).val('#');
  158. }
  159. });
  160. });
  161. jQuery(document).mousedown(function() {
  162. jQuery('.farbtastic').each(function() {
  163. var display = jQuery('#'+this.id).css('display');
  164. if ( display == 'block' ) {
  165. jQuery('#'+this.id).fadeOut();
  166. }
  167. });
  168. });
  169. });
  170. });
  171. </script>
  172. <?php
  173. }
  174. function help() {
  175. ?>
  176. <?php echo (__("Adds colors to", "dagsopt")) ?> <a href="/wp-admin/edit.php"><?php echo (__("Posts", "dagsopt")) ?></a> <?php echo (__("and", "dagsopt")) ?> <a href="/wp-admin/edit.php?post_type=page"><?php echo (__("Pages", "dagsopt")) ?></a><br>
  177. <?php echo ($this->startet ? __('See below', 'dagsopt') : ''); ?>
  178. <?php
  179. }
  180. function admin_line() {
  181. ?>
  182. <a href="/wp-admin/admin.php?page=color-admin-post"><?php echo (__("Settings", "dagsopt")) ?></a>
  183. <?php
  184. $this->display_page();
  185. ?>
  186. <?php
  187. }
  188. }
  189. // Start this plugin once all other plugins are fully loaded
  190. global $plug_color_admin_posts;
  191. $plug_color_admin_posts = new plug_color_admin_posts();
  192. $this->dagsopt['plug_color_admin_posts'] = $plug_color_admin_posts;
  193. }