adsns_options ) {
$this->adsns_activate();
}
/* 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 */
}
}
/* End checking */
}
/* 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_add_general_menu( 'adsense-plugin/adsense-plugin.php' );
add_submenu_page( 'bws_plugins', __( 'Google AdSense Settings', 'adsense' ), 'Google AdSense', 'manage_options', "adsense-plugin.php", array( $this, 'adsns_settings_page' ) );
}
/* Add a link for settings page */
function adsns_plugin_action_links( $links, $file ) {
if ( ! is_network_admin() ) {
if ( $file == 'adsense-plugin/adsense-plugin.php' ) {
$settings_link = '' . __( 'Settings', 'adsense' ) . '';
array_unshift( $links, $settings_link );
}
}
return $links;
}
function adsns_register_plugin_links( $links, $file ) {
if ( $file == 'adsense-plugin/adsense-plugin.php' ) {
if ( ! is_network_admin() )
$links[] = '' . __( 'Settings', 'adsense' ) . '';
$links[] = '' . __( 'FAQ', 'adsense' ) . '';
$links[] = '' . __( 'Support', 'adsense' ) . '';
}
return $links;
}
function adsns_plugin_init() {
/* Internationalization */
load_plugin_textdomain( 'adsense', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
require_once( dirname( __FILE__ ) . '/bws_menu/bws_functions.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_version_check( 'adsense-plugin/adsense-plugin.php', $this->adsns_plugin_info, '3.3' );
/* Call register settings function */
if ( ! is_admin() || ( isset( $_GET['page'] ) && "adsense-plugin.php" == $_GET['page'] ) ) {
$this->adsns_activate();
}
}
function adsns_plugin_admin_init() {
global $bws_plugin_info;
if ( isset( $_GET['page'] ) && "adsense-plugin.php" == $_GET['page'] ) {
if ( ! session_id() ) {
session_start();
}
}
if ( ! isset( $bws_plugin_info ) || 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
);
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"] ) {
$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 ( ! 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'][ $adsns_area ] ) ) {
$adsns_ad_units = $this->adsns_options['adunits'][ $adsns_area ];
for ( $i = 0; $i < count( $adsns_ad_units ); $i++ ) {
if ( isset( $adsns_ad_units[ $adsns_count ] ) ) {
$adsns_ad_unit = $adsns_ad_units[ $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( '