adsns_options )
$this->adsns_activate();
if ( ! is_admin() ) {
/* Use Google AdSense API? */
if ( $this->adsns_adsense_api == true ) {
add_filter( 'the_content', array( $this, 'adsns_content' ) );
add_filter( 'comment_id_fields', array( $this, 'adsns_comments' ) );
} else {
$this->adsns_options['code'] = stripslashes( $this->adsns_options['code'] );
$this->adsns_options['num_show'] = 0;
update_option( 'adsns_settings', $this->adsns_options );
/* Checking in what position we should show an ads */
if ( 'postend' == $this->adsns_options['position'] ) { /* If we choose ad position after post(single page) */
add_filter( 'the_content', array( $this, 'adsns_end_post_ad' ) ); /* Adding ad after post */
} else if ( 'homepostend' == $this->adsns_options['position'] ) { /* If we choose ad position after post(home page) */
add_filter( 'the_content', array( $this, 'adsns_end_home_post_ad' ) ); /* Adding ad after post */
} else if ( 'homeandpostend' == $this->adsns_options['position'] ) { /* If we choose ad position after post(home page) */
add_filter( 'the_content', array( $this, 'adsns_end_home_post_ad' ) ); /* Adding ad after post */
add_filter( 'the_content', array( $this, 'adsns_end_post_ad' ) ); /* Adding ad after post */
} else if ( 'commentform' == $this->adsns_options['position'] ) { /* If we choose ad position after comment form */
add_filter( 'comment_id_fields', array( $this, 'adsns_end_comment_ad' ) ); /* Adding ad after comment form */
} else if ( 'footer' == $this->adsns_options['position'] ) { /* If we choose ad position in a footer */
add_filter( 'get_footer', array( $this, 'adsns_end_footer_ad' ) ); /* Adding footer ad */
}
}
}
}
/* Show ads after comment form */
function adsns_end_comment_ad() {
global $adsns_count;
if ( ! is_feed() && $adsns_count < $this->adsns_options['max_ads'] && $adsns_count < $this->adsns_options['max_homepostads'] ) {
echo '
' . $this->adsns_options['code'] . '
';
$this->adsns_options['num_show'] ++; /* Counting views */
update_option( 'adsns_settings', $this->adsns_options );
$adsns_count = $this->adsns_options['num_show'];
}
}
/* Show ads after post on a single page */
function adsns_end_post_ad( $content ) {
global $adsns_count;
if ( ! is_feed() && is_single() && $adsns_count < $this->adsns_options['max_ads'] && $adsns_count < $this->adsns_options['max_homepostads'] ) { /* Checking if we are on a single page */
$content.= '
' . $this->adsns_options['code'] . '
'; /* Adding an ad code on page */
$this->adsns_options['num_show'] ++; /* Counting views */
update_option( 'adsns_settings', $this->adsns_options );
$adsns_count = $this->adsns_options['num_show'];
}
return $content;
}
/* Show ads after post on home page */
function adsns_end_home_post_ad( $content ) {
global $adsns_count;
if ( $adsns_count < $this->adsns_options['max_ads'] && $adsns_count < $this->adsns_options['max_homepostads'] ) {
if ( ! is_feed() && ( is_home() || is_front_page() ) ) {
$content .= '
' . $this->adsns_options['code'] . '
';
$this->adsns_options['num_show'] ++; /* Counting views */
update_option( 'adsns_settings', $this->adsns_options );
$adsns_count = $this->adsns_options['num_show']; /* Restore count value */
}
}
return $content;
}
/* Show ads in footer */
function adsns_end_footer_ad() {
global $adsns_count;
if ( ! is_feed() && $adsns_count < $this->adsns_options['max_ads'] && $adsns_count < $this->adsns_options['max_homepostads'] ) {
echo '';
$this->adsns_options['num_show'] ++; /* Counting views */
update_option( 'adsns_settings', $this->adsns_options );
$adsns_count = $this->adsns_options['num_show']; /* Restore count value */
}
}
/* Add 'BWS Plugins' menu at the left side in administer panel */
function adsns_add_admin_menu() {
bws_general_menu();
$settings = add_submenu_page( 'bws_panel', __( 'Google AdSense Settings', 'adsense-plugin' ), 'Google AdSense', 'manage_options', "adsense-plugin.php", array( $this, 'adsns_settings_page' ) );
add_action( 'load-' . $settings, array( $this, 'adsns_add_tabs' ) );
}
function adsns_plugin_init() {
require_once( dirname( __FILE__ ) . '/bws_menu/bws_include.php' );
bws_include_init( 'adsense-plugin/adsense-plugin.php' );
if ( empty( $this->adsns_plugin_info ) ) {
if ( ! function_exists( 'get_plugin_data' ) )
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
$this->adsns_plugin_info = get_plugin_data( dirname( __FILE__ ) . '/adsense-plugin.php' );
}
/* Function check if plugin is compatible with current WP version */
bws_wp_min_version_check( 'adsense-plugin/adsense-plugin.php', $this->adsns_plugin_info, '3.8' );
/* Call register settings function */
if ( ! is_admin() || ( isset( $_GET['page'] ) && 'adsense-plugin.php' == $_GET['page'] ) ) {
$this->adsns_activate();
}
}
/* Plugin localization */
function adsns_localization() {
/* Internationalization */
load_plugin_textdomain( 'adsense-plugin', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
function adsns_plugin_admin_init() {
global $bws_plugin_info;
if ( isset( $_GET['page'] ) && "adsense-plugin.php" == $_GET['page'] ) {
if ( ! session_id() ) {
session_start();
}
}
if ( empty( $bws_plugin_info ) )
$bws_plugin_info = array( 'id' => '80', 'version' => $this->adsns_plugin_info["Version"] );
}
/* Creating a default options for showing ads. Starts on plugin activation. */
function adsns_activate() {
global $adsns_count;
if ( ! function_exists( 'get_plugin_data' ) )
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
$this->adsns_plugin_info = get_plugin_data( dirname( __FILE__ ) . '/adsense-plugin.php' );
$adsns_options_defaults = array(
'plugin_option_version' => $this->adsns_plugin_info["Version"],
'widget_title' => '',
'use_new_api' => false,
'publisher_id' => '',
'include_inactive_ads' => 1,
'display_settings_notice' => 1,
'first_install' => strtotime( "now" ),
'suggest_feature_banner' => 1
);
if ( ! get_option( 'adsns_settings' ) ) {
$adsns_options_defaults['use_new_api'] = true;
add_option( 'adsns_settings', $adsns_options_defaults );
}
$this->adsns_options = get_option( 'adsns_settings' );
$adsns_count = 0; /* Number of posts on home page */
/* Array merge incase this version has added new options */
if ( ! isset( $this->adsns_options['plugin_option_version'] ) || $this->adsns_options['plugin_option_version'] != $this->adsns_plugin_info["Version"] ) {
$adsns_options_defaults['display_settings_notice'] = 0;
$this->adsns_options = array_merge( $adsns_options_defaults, $this->adsns_options );
$this->adsns_options['plugin_option_version'] = $this->adsns_plugin_info["Version"];
update_option( 'adsns_settings', $this->adsns_options );
}
$this->adsns_adsense_api = ( $this->adsns_options['use_new_api'] == true ) ? true : false;
}
/* Google Asense API */
function adsns_client() {
require_once( dirname( __FILE__ ) . '/google_api/autoload.php' );
$client = new Google_Client();
$client->setClientId( '903234641369-4mm0lqt76r0rracrdn2on3qrk6c554aa.apps.googleusercontent.com' );
$client->setClientSecret( 'Twlx072svotXexK5rvqC5bb-' );
$client->setScopes( array( 'https://www.googleapis.com/auth/adsense' ) );
$client->setRedirectUri( 'urn:ietf:wg:oauth:2.0:oob' );
$client->setAccessType( 'offline' );
$client->setDeveloperKey( 'AIzaSyBa4vT_9do8e7Yxv88EXle6546nFVGLHI8' );
$client->setApplicationName( $this->adsns_plugin_info['Name'] );
return $client;
}
/* Show ads on the home page / single page / post / custom post / categories page / tags page via Google AdSense API */
function adsns_content( $content ) {
global $adsns_count;
if ( $this->adsns_is_main_query && ! is_feed() && ( is_home() || is_front_page() || is_category() || is_tag() ) ) {
$adsns_count = empty( $adsns_count ) ? 0 : $adsns_count;
if ( $adsns_count > 2 ) {
return $content;
}
if ( is_home() || is_front_page() ) {
$adsns_area = 'home';
}
if ( is_category() || is_tag() ) {
$adsns_area = 'categories+tags';
}
if ( isset( $this->adsns_options['adunits'][ $this->adsns_options['publisher_id'] ][ $adsns_area ][ $adsns_count ] ) ) {
$adsns_ad_unit = $this->adsns_options['adunits'][ $this->adsns_options['publisher_id'] ][ $adsns_area ][ $adsns_count ];
$adsns_ad_unit_id = $adsns_ad_unit['id'];
$adsns_ad_unit_position = $adsns_ad_unit['position'];
$adsns_ad_unit_code = htmlspecialchars_decode( $adsns_ad_unit['code'] );
$adsns_count++;
switch ( $adsns_ad_unit_position ) {
case 'after':
$adsns_ads = sprintf( '
',
__( 'Attention:', 'adsense-plugin' ),
sprintf( __( 'Google AdSense by BestWebSoft plugin was updated to use Google AdSense API, which is not compatible with the old settings. For further plugin usage, you will need to %s', 'adsense-plugin' ), sprintf( '%s', __( 're-configure it.', 'adsense-plugin' ) ) )
);
echo ob_get_clean();
}
if ( 'plugins.php' == $hook_suffix ) {
if ( isset( $this->adsns_options['first_install'] ) && strtotime( '-1 week' ) > $this->adsns_options['first_install'] )
bws_plugin_banner( $this->adsns_plugin_info, 'adsns', 'google-adsense', '6057da63c4951b1a7b03296e54ed6d02', '80', '//ps.w.org/adsense-plugin/assets/icon-128x128.png' );
bws_plugin_banner_to_settings( $this->adsns_plugin_info, 'adsns_settings', 'adsense-plugin', 'admin.php?page=adsense-plugin.php' );
}
if ( isset( $_GET['page'] ) && 'adsense-plugin.php' == $_GET['page'] ) {
bws_plugin_suggest_feature_banner( $this->adsns_plugin_info, 'adsns_settings', 'adsense-plugin' );
}
}
/*
*displays AdSense in widget
*@return array()
*/
function adsns_widget_display() {
global $adsns_count;
$title = $this->adsns_options['widget_title'];
if ( ! $this->adsns_adsense_api ) {
echo '";
} else {
if ( ! empty( $this->adsns_options['adunits'][ $this->adsns_options['publisher_id'] ]['widget'] ) ) {
$adsns_ad_unit_id = $this->adsns_options['adunits'][ $this->adsns_options['publisher_id'] ]['widget'][0]['id'];
$adsns_ad_unit_code = htmlspecialchars_decode( $this->adsns_options['adunits'][ $this->adsns_options['publisher_id'] ]['widget'][0]['code'] );
printf( '', $title, $adsns_ad_unit_id, $adsns_ad_unit_code );
}
}
}
/*
*Register widget for use in sidebars.
*Registers widget control callback for customizing options
*/
function adsns_register_widget() {
if ( isset( $this->adsns_options['publisher_id'] ) && isset( $this->adsns_options['adunits'][ $this->adsns_options['publisher_id'] ]['widget'] ) && count( $this->adsns_options['adunits'][ $this->adsns_options['publisher_id'] ]['widget'] ) > 0 ) {
$adsns_widget_positions = array(
'static' => __( 'Static', 'adsense-plugin' ),
'fixed' => __( 'Fixed', 'adsense-plugin' ),
);
$adsns_widget = $this->adsns_options['adunits'][ $this->adsns_options['publisher_id'] ]['widget'][0];
$adsns_id = substr( strstr( $adsns_widget['id'], ':' ), 1 );
$adsns_widget_position = isset( $adsns_widget['position'] ) ? $adsns_widget['position'] : 'static';
wp_register_sidebar_widget(
'adsns_widget', /* Unique widget id */
sprintf( 'AdSense: ID: %s, %s', $adsns_id, $adsns_widget_positions[ $adsns_widget_position ] ),
array( $this, 'adsns_widget_display' ), /* Callback function */
array( 'description' => sprintf( '%s ID: %s, %s', __( 'Widget displays Google AdSense.', 'adsense-plugin' ), $adsns_id, $adsns_widget_positions[ $adsns_widget_position ] ) ) /* Options */
);
wp_register_widget_control(
'adsns_widget', /* Unique widget id */
sprintf( 'AdSense: ID: %s, %s', $adsns_id, $adsns_widget_positions[ $adsns_widget_position ] ),
array( $this, 'adsns_widget_control' ) /* Callback function */
);
}
}
/*
*Registers widget control callback for customizing options
*@return array
*/
function adsns_widget_control() {
if ( isset( $_POST["adsns-widget-submit"] ) ) {
$this->adsns_options['widget_title'] = strip_tags( stripslashes( $_POST["adsns-widget-title"] ) );
update_option( 'adsns_settings', $this->adsns_options );
}
$title = isset( $this->adsns_options['widget_title'] ) ? $this->adsns_options['widget_title'] : '' ;
printf( '', __( 'Title', 'adsense-plugin' ), $title ); ?>
%s %s', __( 'Please note:', 'adsense-plugin' ), sprintf( '%s', __( "Select ad block to display in the widget you can on the plugin settings page in the 'Widget' tab.", 'adsense-plugin' ) ) ); ?>