Skip to content

Instantly share code, notes, and snippets.

@panoslyrakis
Last active January 29, 2017 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save panoslyrakis/9623ed33d05ffdb8400460f78e3b1886 to your computer and use it in GitHub Desktop.
Save panoslyrakis/9623ed33d05ffdb8400460f78e3b1886 to your computer and use it in GitHub Desktop.
Creates a shortcode and displays Pro Site ads on sidebar. Requires Pro Sites plugin
<?php
/*
Plugin Name: WPMUDEV Pro Site Ads on sidebar
Plugin URI: https://premium.wpmudev.org/
Description: Creates a shortcode and displays Pro Site ads on sidebar
Author: Panos Lyrakis @ WPMUDEV
Author URI: https://premium.wpmudev.org/
License: GPLv2 or later
*/
if( ! class_exists( 'WPMUDEV_PS_Sidebar_Ads' ) ){
class WPMUDEV_PS_Sidebar_Ads
{
var $ad_counter;
private $_shown_ads = false;
private static $_instance = null;
public static function get_instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new WPMUDEV_PS_Sidebar_Ads();
}
return self::$_instance;
}
private function __construct(){
add_shortcode( 'wpmudev_ps_sidebar_ads', array( $this, 'ps_sidebar_ads_sh' ) );
add_action( 'get_sidebar', array( $this, 'show_sidebar_ads' ), 1 );
}
public function ps_sidebar_ads_sh( $atts, $content ){
global $psts;
if( ! class_exists( 'ProSites' ) || ! $this->ok_to_show_ads() || is_feed() ){
return $content;
}
$per_page = $psts->get_setting( 'ads_count', 3 );
if ( $this->ad_counter < $per_page ) {
$content = $psts->get_setting( 'ads_before_code' ) . $content;
$this->ad_counter ++;
}
if ( $this->ad_counter < $per_page ) {
$content = $content . $psts->get_setting( 'ads_after_code' );
$this->ad_counter ++;
}
return "<div class=\"ps_sidebar_ads\">{$content}</div>";
}
public function show_sidebar_ads( $name ){
if( ! $this->_shown_ads ){
// OR
// if( $name == '' ){
echo do_shortcode( '[wpmudev_ps_sidebar_ads]' );
}
$this->_shown_ads = true;
}
function ok_to_show_ads( $blog_id = null ) {
global $ProSites_Module_Ads;
if ( isset( $ProSites_Module_Ads ) && is_object( $ProSites_Module_Ads ) ) {
return $ProSites_Module_Ads->show_ads( $blog_id );
} else {
return true;
}
}
}
add_action( 'plugins_loaded', function(){
WPMUDEV_PS_Sidebar_Ads::get_instance();
}, 10 );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment