Skip to content

Commit

Permalink
added new filter, removed scheme url from options
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph committed Jun 29, 2016
1 parent c2678cf commit 625f541
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 10 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,26 @@ You can do so with `add_filter( 'ap-ab-type', 'your_filter' );`
### I want more color schemes! ###
Feel free to add more, there are other plugins that do so. Or have a look at [wp_admin_css_color() in the WordPress Codex](https://codex.wordpress.org/Function_Reference/wp_admin_css_color)

### Can I save the color schemes to my theme? ###
Yes, you can. Simply add and alter the following example somewhere to the functions.php of your theme

```
add_filter( 'apermo-adminbar-sites', 'sites_filter' );
function sites_filter( $sites ) {
$sites['dev']['url'] = 'http://dev.your-site.tld';
$sites['staging']['url'] = 'http://staging.your-site.tld';
$sites['live']['url'] = 'https://www.your-site.tld';
return $sites;
}
```

## Changelog ##

### 0.9.3 ###
* Removed Scheme URL from saved options
* added filter 'apermo-adminbar-sites' to give the option of saving the settings in a theme

### 0.9.2 ###
* Some minor code improvements

Expand Down
60 changes: 50 additions & 10 deletions apermo-adminbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* @wordpress-plugin
* Plugin Name: Apermo Admin Bar
* Version: 0.9.1
* Version: 0.9.3
* Description: A simple plugin that allows you to add custom links to the admin bar, navigation between your live and dev systems
* Author: Christoph Daum
* Author URI: http://apermo.de/
Expand Down Expand Up @@ -69,18 +69,24 @@ class ApermoAdminBar {
*/
private $admin_colors = array();

/**
* Indicator if the sites were loaded from a filter.
*
* @var bool
*/
private $is_from_filter = false;

/**
* ApLiveDevAdminBar constructor.
*/
public function __construct() {
$this->sites = get_option( 'apermo_adminbar_sites', array() );
$this->load_translation();

add_action( 'admin_menu', array( $this, 'add_admin_menu' ) );
add_action( 'admin_init', array( $this, 'settings_init' ) );
add_action( 'admin_init', array( $this, 'sort_admin_colors' ), 99 );

add_action( 'init', array( $this, 'init' ) );
add_action( 'init', array( $this, 'sort_admin_colors' ), 99 );

add_action( 'admin_enqueue_scripts', array( $this, 'color_scheme' ), 99 );
add_action( 'wp_enqueue_scripts', array( $this, 'color_scheme' ), 99 );
Expand Down Expand Up @@ -126,13 +132,37 @@ public function init() {

// Allow to add (or remove) further page types via filter.
$this->allowed_page_types = apply_filters( 'apermo-adminbar-types', $types );
$this->load_sites();
if ( count( $this->sites ) ) {
add_action( 'admin_bar_menu', array( $this, 'admin_bar_filter' ), 99 );

$this->set_current();
}
}

/**
* Load Settings from Database
*/
private function load_sites() {
// Check if a filter was added from within the theme.
if ( has_filter( 'apermo-adminbar-sites' ) ) {
$dummysites = array();
foreach ( $this->allowed_page_types as $key => $allowed_page_type ) {
$dummysites[ $key ]['name'] = $allowed_page_type['label'];
$dummysites[ $key ]['color'] = $allowed_page_type['default'];
$dummysites[ $key ]['url'] = '';
}
// Filter against a default set of sites and afterwards use the sanitize function.
$this->is_from_filter = true;
$this->sites = $this->sanitize( apply_filters( 'apermo-adminbar-sites', $dummysites ) );
}
// If the sites are still empty load the settings from the DB.
if ( ! count( $this->sites ) ) {
$this->is_from_filter = false;
$this->sites = get_option( 'apermo_adminbar_sites', array() );
}
}

/**
* Set $this->current for later use
*
Expand All @@ -159,6 +189,8 @@ private function set_current() {
public function sort_admin_colors() {
global $_wp_admin_css_colors;

register_admin_color_schemes();

$this->admin_colors = $_wp_admin_css_colors;

ksort( $this->admin_colors );
Expand All @@ -175,7 +207,7 @@ public function sort_admin_colors() {
* Load the Admin Bar Color Scheme
*/
public function color_scheme() {
$scheme = $this->sites[ $this->current ]['scheme_url'];
$scheme = $this->admin_colors[ $this->sites[ $this->current ]['color'] ]->url;
if ( current_user_can( 'edit_posts' ) && ( is_admin() || is_admin_bar_showing() ) ) {
wp_enqueue_style( 'apermo-adminbar-colors', $scheme, array() );
wp_enqueue_style( 'apermo-adminbar', plugins_url( 'css/style.css', __FILE__ ) );
Expand Down Expand Up @@ -257,12 +289,21 @@ public function options_page() {
<form action='options.php' method='post'>
<h1><?php esc_html_e( 'Apermo Admin Bar', 'apermo-adminbar' ); ?></h1>
<?php
if ( $this->is_from_filter ) {
?>
<div id="setting-error-settings_updated" class="error settings-error notice">
<p><strong><?php printf( __( 'The Filter %s is active, probably within your theme. These settings will have no further effect.', 'apermo-adminbar' ), '<em>"apermo-adminbar-sites"</em>' ); ?></strong></p>
</div>
<?php
}
settings_fields( 'apermo_adminbar' );
do_settings_sections( 'apermo_adminbar' );
submit_button();
?>
</form>
<p class="clear"><strong>*) <?php esc_html_e( 'Sites without URL will not be saved to the database, name and color scheme will be dropped.', 'apermo-adminbar' ); ?></strong></p>
</div>
<div class="clear"></div>
<?php
}

Expand Down Expand Up @@ -344,7 +385,7 @@ public function name_render( $args ) {
*/
public function url_render( $args ) {
$setting = $this->sites[ $args['key'] ]['url'];
echo '<input type="url" id="apermo_adminbar_sites_' . esc_attr( $args['key'] ) . '_url" name="apermo_adminbar_sites[' . $args['key'] . '][url]" placeholder="http://..." value="' . esc_attr( $setting ) . '" class="regular-text">';
echo '<input type="url" id="apermo_adminbar_sites_' . esc_attr( $args['key'] ) . '_url" name="apermo_adminbar_sites[' . $args['key'] . '][url]" placeholder="http://..." value="' . esc_attr( $setting ) . '" class="regular-text">*';
}

/**
Expand Down Expand Up @@ -420,10 +461,10 @@ public function sanitize( $input ) {

$data['url'] = trim( esc_url_raw( $data['url'] ), '/' );

// Store the URL, so that we dont' need to init $_wp_admin_css_colors.
$data['scheme_url'] = $this->admin_colors[ $data['color'] ]->url;

$output[ $key ] = $data;
// It only makes sense to save, if there is a URL, otherwise just drop it.
if ( $data['url'] ) {
$output[ $key ] = $data;
}
}
}

Expand All @@ -435,4 +476,3 @@ public function sanitize( $input ) {
add_action( 'plugins_loaded', function () {
new ApermoAdminBar();
} );

0 comments on commit 625f541

Please sign in to comment.