From bb1edc9707f6c762fa09a0956286621f7a23b171 Mon Sep 17 00:00:00 2001 From: Jonathan Bardo Date: Sun, 29 Dec 2013 16:43:32 -0500 Subject: [PATCH] Add plugin dependency function --- stream-notifications.php | 108 +++++++++++++++++++++++++++++++-------- 1 file changed, 88 insertions(+), 20 deletions(-) diff --git a/stream-notifications.php b/stream-notifications.php index 0dac0c98d..78529156f 100644 --- a/stream-notifications.php +++ b/stream-notifications.php @@ -1,6 +1,7 @@ is_dependency_satisfied() ) { + return; } // Load all classes in /classes folder @@ -134,7 +144,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( @@ -295,6 +305,8 @@ public function page() { /** * Admin page callback for form actions * + * @param null $id + * * @return void */ public function page_form( $id = null ) { @@ -305,7 +317,7 @@ public function page_form( $id = null ) { public function page_form_save() { // TODO add nonce, check author/user permission to update record $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 ); @@ -343,7 +355,7 @@ public function form_ajax_ep() { // BIG TODO: Make the request context-aware, // ie: get other rules, 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' ); + $type = filter_input( INPUT_POST, 'type' ); $query = filter_input( INPUT_POST, 'q' ); switch ( $type ) { @@ -355,7 +367,7 @@ public function form_ajax_ep() { case 'action': $actions = WP_Stream_Connectors::$term_labels['stream_action']; $actions = preg_grep( sprintf( '/%s/i', $query ), $actions ); - $data = $this->format_json_for_select2( $actions ); + $data = $this->format_json_for_select2( $actions ); break; } if ( isset( $data ) ) { @@ -389,6 +401,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( '

%s

', __( 'Stream Notifications requires Stream plugin to be present and activated.', 'stream' ) ); + } else if ( version_compare( WP_Stream::VERSION, self::STREAM_MIN_VERSION, '<' ) ) { + $message .= sprintf( '

%s

', sprintf( __( 'Stream Notifications requires Stream version %s or higher', 'stream' ), self::STREAM_MIN_VERSION ) ); + } + + if ( ! empty( $message ) ) { + self::$messages['wp_stream_db_error'] = sprintf( + '
%s

%s

', + $message, + sprintf( + __( 'Please install 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();