Skip to content

Commit

Permalink
Merge pull request #4 from x-team/issue-1
Browse files Browse the repository at this point in the history
Add plugin dependency function
  • Loading branch information
shadyvb committed Jan 27, 2014
2 parents cbe92d1 + 886bb13 commit 81b05c1
Showing 1 changed file with 90 additions and 21 deletions.
111 changes: 90 additions & 21 deletions stream-notifications.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/**
* Plugin Name: Stream Notifications
* Depends: Stream
* Plugin URI: http://x-team.com
* Description: TBD
* Version: 0.1
Expand Down Expand Up @@ -33,13 +34,34 @@

class WP_Stream_Notifications {

/**
* Holds plugin minimum version
*
* @const string
*/
const STREAM_MIN_VERSION = '0.9.5';

/**
* Hold Stream instance
*
* @var string
*/
public static $instance;

/**
* Screen ID for my admin page
* @var string
*/
public static $screen_id;

/**
* Holds admin notices messages
*
* @var array
*/
public static $messages = array();

/*
* List of registered adapters
* @var array
*/
Expand All @@ -48,8 +70,7 @@ class WP_Stream_Notifications {
/**
* Class constructor
*/
public function __construct() {

private function __construct() {
define( 'WP_STREAM_NOTIFICATIONS_DIR', plugin_dir_path( __FILE__ ) );
define( 'WP_STREAM_NOTIFICATIONS_URL', plugin_dir_url( __FILE__ ) );
define( 'WP_STREAM_NOTIFICATIONS_INC_DIR', WP_STREAM_NOTIFICATIONS_DIR . 'includes/' );
Expand All @@ -65,20 +86,10 @@ public function __construct() {
* @return void
*/
public function load() {
add_action( 'admin_notices', array( $this, 'admin_notices' ) );

// Brothers in arms!, live together, or die together!
if ( ! class_exists( 'WP_Stream' ) ) {
add_action(
'plugins_loaded',
function() {
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
deactivate_plugins( __FILE__ );
wp_die(
__( 'Stream plugin has not been found, please install/activate it first. Plugin will now be deactivated automatically.', 'stream' ),
__( 'Stream Notifications was Deactivated', 'stream' )
);
}
);
if ( ! $this->is_dependency_satisfied() ) {
return;
}

// Load all classes in /classes folder
Expand Down Expand Up @@ -152,7 +163,7 @@ public function get_js_options() {
global $wp_roles;
$args = array();

$roles = $wp_roles->roles;
$roles = $wp_roles->roles;
$roles_arr = array_combine( array_keys( $roles ), wp_list_pluck( $roles, 'name' ) );

$args['types'] = array(
Expand Down Expand Up @@ -322,6 +333,8 @@ public function page() {
/**
* Admin page callback for form actions
*
* @param null $id
*
* @return void
*/
public function page_form( $id = null ) {
Expand All @@ -333,7 +346,7 @@ public function page_form_save() {
// TODO add nonce, check author/user permission to update record
// TODO Do not save if no triggers are added
$action = filter_input( INPUT_GET, 'action' );
$id = filter_input( INPUT_GET, 'id' );
$id = filter_input( INPUT_GET, 'id' );

$rule = new WP_Stream_Notification_Rule( $id );

Expand Down Expand Up @@ -368,9 +381,9 @@ public function page_list() {
* @return void
*/
public function form_ajax_ep() {
// BIG TODO: Make the request context-aware,
// ie: get other rules ( maybe in the same group only ? ), so an author
// query would check if there is a author_role rule available to limit
// BIG @TODO: Make the request context-aware,
// ie: get other rules ( maybe in the same group only ? ), so an author
// query would check if there is a author_role rule available to limit
// the results according to it
$type = filter_input( INPUT_POST, 'type' );
$is_single = filter_input( INPUT_POST, 'single' );
Expand Down Expand Up @@ -427,6 +440,62 @@ public function format_json_for_select2( $data, $key = null, $val = null ) {
}
return $return;
}

/**
* Check if plugin dependencies are satisfied and add an admin notice if not
*
* @return bool
*/
public function is_dependency_satisfied() {
$message = '';

if ( ! class_exists( 'WP_Stream' ) ) {
$message .= sprintf( '<p>%s</p>', __( 'Stream Notifications requires Stream plugin to be present and activated.', 'stream' ) );
} else if ( version_compare( WP_Stream::VERSION, self::STREAM_MIN_VERSION, '<' ) ) {
$message .= sprintf( '<p>%s</p>', sprintf( __( 'Stream Notifications requires Stream version %s or higher', 'stream' ), self::STREAM_MIN_VERSION ) );
}

if ( ! empty( $message ) ) {
self::$messages['wp_stream_db_error'] = sprintf(
'<div class="error">%s<p>%s</p></div>',
$message,
sprintf(
__( 'Please <a href="%s" target="_blank">install</a> Stream plugin version %s or higher for Stream Notifications to work properly.', 'stream' ),
esc_url( 'http://wordpress.org/plugins/stream/' ),
self::STREAM_MIN_VERSION
)
); // xss okay

return false;
}

return true;
}

/**
* Display all messages on admin board
*
* @return void
*/
public static function admin_notices() {
foreach ( self::$messages as $message ) {
echo wp_kses_post( $message );
}
}

/**
* Return active instance of WP_Stream, create one if it doesn't exist
*
* @return WP_Stream
*/
public static function get_instance() {
if ( empty( self::$instance ) ) {
$class = __CLASS__;
self::$instance = new $class;
}
return self::$instance;
}

}

$GLOBALS['wp_stream_notifications'] = new WP_Stream_Notifications;
$GLOBALS['wp_stream_notifications'] = WP_Stream_Notifications::get_instance();

0 comments on commit 81b05c1

Please sign in to comment.