Created
January 31, 2014 06:24
-
-
Save pommiegranit/8727441 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Global Conditional Notification | |
Description: Provides global notifications with conditional content | |
Version: 1.0 | |
Author: Chris Knowles | |
Plugin URI: http:// | |
Author URI: http://premium.wpmudev.org/blog/author/chrisdknowles/ | |
Author: Bas Matthee | |
License: Free for use and modification | |
This is an amlagmation and tweaking of two existing WP plugins: | |
Global Notify by Bas Matthee, http://wordpress.org/plugins/global-notifications/ | |
WordPress Conditional Content by Super Interactive, http://wordpress.org/plugins/wp-conditional-content/ | |
*/ | |
class GlobalNotify { | |
public function __construct() { | |
add_action( 'admin_menu', array(&$this,'gn_plugin_menu') ); | |
add_action( 'loop_start', array(&$this,'gn_show_notification') ); | |
add_action( 'wp_head', array(&$this,'gn_add_css') ); | |
/* add the shortcodes */ | |
# Base layer | |
add_shortcode( 'if', array( &$this, 'gn_short_code_if' ) ); | |
# Second layer | |
add_shortcode( 'if2', array( &$this, 'gn_short_code_if' ) ); | |
# Third layer | |
add_shortcode( 'if3', array( &$this, 'gn_short_code_if' ) ); | |
# Fourth layer | |
add_shortcode( 'if4', array( &$this, 'gn_short_code_if' ) ); | |
} | |
public function gn_plugin_menu() { | |
add_menu_page('Global Notify', 'Global Notify', 'manage_options', 'gn_settings', array(&$this,'gn_settings')); | |
} | |
public function gn_show_notification() { | |
if ( $this->gn_is_set('bmgn_notification_enabled') ) { | |
$class = 'gn-'.get_option('bmgn_notification_type','notify'); | |
// CK - "unsanitize" the option | |
$message = str_replace('\"' , '"' , get_option('bmgn_notification','')); | |
// CK - run any shortcodes | |
$message = apply_filters( 'the_content' , $message ); | |
if ( trim( $message ) != '' ) echo '<div class="gn '.$class.'">'.$message.'</div>'; | |
} | |
} | |
public function gn_settings() { | |
if ( !current_user_can( 'manage_options' ) ) { | |
wp_die( __( 'You do not have sufficient permissions to access this page.' ) ); | |
} | |
if (isset($_POST['bmgn_notification'])) { | |
$gn_enabled = (isset($_POST['bmgn_notification_enabled']))?1:0; | |
$gn_type = $_POST['bmgn_notification_type']; | |
update_option('bmgn_notification',$_POST['bmgn_notification']); | |
update_option('bmgn_notification_type',$gn_type); | |
update_option('bmgn_notification_enabled',$gn_enabled); | |
/* CK - added CSS option */ | |
update_option('bmgn_notification_css',$_POST['bmgn_notification_css']); | |
echo ' | |
<div class="updated"> | |
<p>Changes saved!</p> | |
</div>'; | |
} | |
echo ' | |
<div class="wrap"> | |
<form action="" method="post"> | |
<h1>Global Notification - Settings</h1> | |
<p> | |
Enter notification content. HTML tags and shortcodes can be used. | |
</p> | |
<textarea style="width: 80%; height: 300px" name="bmgn_notification">'.str_replace('\"', '"', get_option('bmgn_notification','Enter your message here.')).'</textarea><br /> | |
<br /> | |
<p> | |
Notification type: | |
</p> | |
<select name="bmgn_notification_type"> | |
<option value="notify" '.$this->gn_cb_check('notify',get_option('bmgn_notification_type','notify')).'>Notify</option> | |
<option value="alert" '.$this->gn_cb_check('alert',get_option('bmgn_notification_type','notify')).'>Alert</option> | |
<option value="warning" '.$this->gn_cb_check('warning',get_option('bmgn_notification_type','notify')).'>Warning</option> | |
<option value="custom" '.$this->gn_cb_check('custom',get_option('bmgn_notification_type','custom')).'>Custom</option> | |
</select><br /> | |
<br /> | |
<p> | |
Notification is active? | |
</p> | |
<input type="checkbox" name="bmgn_notification_enabled" '.$this->gn_is_enabled().'> Enabled<br /> | |
<p> | |
Enter your CSS for the notifications. Leave blank if you are adding the styles to your theme\'s stylesheet. | |
</p> | |
<textarea style="width: 80%; height: 300px" name="bmgn_notification_css">'.get_option('bmgn_notification_css',' | |
.gn { | |
width: 80%; | |
margin: 20px 30px; | |
padding: 8px 35px 8px 14px; | |
margin-bottom: 18px; | |
-webkit-border-radius: 4px; | |
-moz-border-radius: 4px; | |
border-radius: 4px; | |
} | |
.gn-notify { | |
color: #3A87AD; | |
background-color: #D9EDF7; | |
border: 1px solid #BCE8F1; | |
} | |
.gn-alert { | |
color: #B94A48; | |
background-color: #F2DEDE; | |
border: 1px solid #EED3D7; | |
} | |
.gn-warning { | |
color: #C09853; | |
background-color: #FCF8E3; | |
border: 1px solid #FBEED5; | |
} | |
.gn-custom { | |
} | |
').'</textarea><br /> | |
<br /> | |
<br /> | |
<input style="font-size: 120%" type="submit" value="Save"> | |
</form> | |
</div>'; | |
} | |
public function gn_cb_check($val1,$val2) { | |
return ($val1 == $val2)?'selected="selected"':''; | |
} | |
public function gn_is_enabled() { | |
return (get_option('bmgn_notification_enabled') == 1)?'checked="checked"':''; | |
} | |
public function gn_is_set( $option_name ) { | |
return (get_option( $option_name ) == 1)?true:false; | |
} | |
public function gn_add_css() { | |
$css = get_option('bmgn_notification_css',''); | |
if ( trim( $css ) != '') echo '<style type="text/css" media="screen">' . $css . '</style>'; | |
} | |
/** | |
* Conditional Content | |
* | |
*/ | |
/** | |
* The function that is called by when the shortcode is parsed | |
* @param array $atts The attributes of the shortcode | |
* @param string $content The content between the shortcode tags | |
* @return string If conditions are met $content, otherwise nothing. | |
* | |
*/ | |
public function gn_short_code_if( $atts, $content ) { | |
if ( empty( $atts ) ) | |
return $content; | |
$condition_met = false; | |
$match = ''; | |
if ( isset( $atts['match'] ) ) | |
$match = ( 'contain' == $atts['match'] ) ? 'contain' : 'exact'; | |
foreach ( $atts as $key => $value ) { | |
if ( 'qs' == $key ) | |
$condition_met = $this->condition_query_string( $value, $match ); | |
elseif ( 'referrer' == $key ) | |
$condition_met = $this->condition_referrer( $value, $match ); | |
elseif ( 'role' == $key ) | |
$condition_met = $this->condition_user_role( $value ); | |
// CK - Added | |
elseif ( 'page' == $key ) | |
$condition_met = $this->condition_page_type( $value ); | |
if( ! $condition_met ) | |
return ''; | |
} | |
# Run through do_shortcode to allow nesting | |
return do_shortcode( $content ); | |
} | |
/** | |
* Returns true if specified query string parameters match specified values | |
* @param array $atts The attributes of the shortcode | |
* @return bool True if conditions are met | |
* | |
*/ | |
private function condition_query_string( $value, $match ) { | |
if( ! strstr( $value, ':' ) ) | |
return false; | |
list( $qs_key, $qs_value ) = explode( ':', $value ); | |
$match = ( empty( $match ) ) ? 'exact' : $match; | |
# Check if GET value matches given value | |
if ( ! isset( $_GET[ $qs_key ] ) || false === $this->check_value( $_GET[ $qs_key ], $qs_value, $match ) ) | |
return false; | |
return true; | |
} | |
/** | |
* Returns true if HTTP_REFERER matches (any of) the given string(s) | |
* @param array $atts The attributes of the shortcode | |
* @return bool True if conditions are met | |
* | |
*/ | |
private function condition_referrer( $referrer, $match ) { | |
$match = ( empty( $match ) ) ? 'contain' : $match; | |
# Return false if no referrer set | |
if ( empty( $_SERVER['HTTP_REFERER'] ) ) | |
return false; | |
# Return wether referrer matches | |
if( ! $this->check_value( $_SERVER['HTTP_REFERER'], $referrer, $match ) ) | |
return false; | |
return true; | |
} | |
/** | |
* Returns true if user is logged in and has (any of) the specified role(s) | |
* @param array $atts The attributes of the shortcode | |
* @return bool True if conditions are met | |
*/ | |
private function condition_user_role( $role ) { | |
# If role is empty, a user should not have a role to match | |
if ( empty( $role ) && is_user_logged_in() ) | |
return false; | |
# If role is not empty, user needs to be logged in | |
elseif ( ! is_user_logged_in() ) | |
return false; | |
# Return wether current user role matches | |
if( $this->check_value( $role, $this->get_current_user_role() ) ) | |
return true; | |
return false; | |
} | |
/** CK - Added | |
* Returns true if page type matches specified types | |
* @param array $atts The attributes of the shortcode (type=page|post|home|archive|search) | |
* @return bool True if conditions are met | |
*/ | |
private function condition_page_type( $page ) { | |
# If page is 'home' | |
if ( strpos( $page, 'home') > -1 && is_home() ) | |
return true; | |
# If page is 'post' | |
if ( strpos( $page, 'post') > -1 && is_singular( 'post' ) ) | |
return true; | |
# If page is 'page' | |
if ( strpos( $page, 'page') > -1 && is_singular( 'page' ) ) | |
return true; | |
# If page is 'archive' | |
if ( strpos( $page, 'archive') > -1 && is_archive() ) | |
return true; | |
# If page is 'search' | |
if ( strpos( $page, 'search') > -1 && is_search() ) | |
return true; | |
return false; | |
} | |
/** | |
* Compare value with another value or set of values | |
* @param string $value1 The value to test | |
* @param string $value2 The allowed value(s) | |
* @param string $match Can be exact for exact matches or contain for wildcard | |
* @return bool Returns true if value1 matches value2 | |
*/ | |
private function check_value( $value1, $value2, $match = 'exact' ) { | |
# Check if multiple values | |
if ( strstr( $value2, ';' ) ) | |
$allowed_values = explode( ';', $value2 ); | |
else | |
$allowed_values = array( $value2 ); | |
# loop through available values to check | |
foreach ( $allowed_values as $allowed_value ) { | |
# Exact match | |
if ( 'exact' == $match ) { | |
if ( $allowed_value == $value1 ) | |
return true; | |
} | |
# String contains string (wildcard) | |
elseif ( 'contain' == $match ) { | |
if ( strstr( $value1, $allowed_value ) ) | |
return true; | |
} | |
} | |
return false; | |
} | |
/** | |
* Get the role of the current logged in user | |
* | |
*/ | |
private function get_current_user_role() { | |
# Get current user object | |
$current_user = wp_get_current_user(); | |
# Get first available role | |
if ( ! empty( $current_user->roles ) ) | |
return array_shift( $current_user->roles ); | |
return false; | |
} | |
} | |
$GlobalNotifiy = new GlobalNotify(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice! Thanks for leaving my name in the credits.