Created
July 1, 2017 14:57
-
-
Save panoslyrakis/78da5ebc76b883f2c67f93792e646fcc to your computer and use it in GitHub Desktop.
Pro Sites - Notification for pro site owners
This file contains 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: Pro Sites - Notification for pro site owners | |
Plugin URI: https://premium.wpmudev.org/ | |
Description: Displays different notifications for users if they are logged out, logged in but don't have a pro site, logged in and have at least one pro site. | |
Author: Panos Lyrakis @ WPMUDEV | |
Author URI: https://premium.wpmudev.org/ | |
License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WPMUDEV_PS_Notice_By_Level' ) ) { | |
class WPMUDEV_PS_Notice_By_Level { | |
private static $_instance = null; | |
public static function get_instance() { | |
if( is_null( self::$_instance ) ){ | |
self::$_instance = new WPMUDEV_PS_Notice_By_Level(); | |
} | |
return self::$_instance; | |
} | |
public function __construct() { | |
add_shortcode( 'wpmudev_ps_notice', array( $this, 'ps_notice' ) ); | |
} | |
public function get_logged_out_message(){ | |
return __( 'Message for loggedout visitors', 'psts' ); | |
} | |
public function get_user_message_pro_level(){ | |
return __( 'Message for users with at least one pro site', 'psts' ); | |
} | |
public function get_user_message_not_pro_level(){ | |
return __( 'Message for users with NO pro site', 'psts' ); | |
} | |
public function ps_notice( $atts ){ | |
$atts = shortcode_atts( | |
array( | |
'user_id' => null | |
), $atts, 'ps_notice' | |
); | |
if( ! function_exists( 'is_pro_site' ) ){ | |
return; | |
} | |
if( ! is_user_logged_in() && $atts['user_id'] == null ){ | |
return $this->get_logged_out_message(); | |
} | |
$user_id = ( is_numeric( $atts['user_id'] ) && $atts['user_id'] != null ) ? (int)$atts['user_id'] : get_current_user_id(); | |
$user_blogs = get_blogs_of_user( $user_id ); | |
$user_has_pro_site = false; | |
foreach ( $user_blogs as $blog ) { | |
if ( is_pro_site( $blog->userblog_id ) && ! is_pro_trial( $blog->userblog_id ) ) { | |
$mail = get_blog_option( $blog->userblog_id, 'admin_email' ); | |
$user_from_email = get_user_by( 'email', $mail ); | |
if( $user_id == $user_from_email->ID ){ | |
return $this->get_user_message_pro_level(); | |
} | |
} | |
} | |
return $this->get_user_message_not_pro_level(); | |
} | |
} | |
add_action( 'plugins_loaded', function(){ | |
$GLOBALS['WPMUDEV_PS_Notice_By_Level'] = WPMUDEV_PS_Notice_By_Level::get_instance(); | |
}, 10 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment