'stf-adsense',
'description' => __( 'Display Adsense Ads.', 'adsense-widget' )
);
parent::__construct(
'adsense-widget',
__( 'Adsense', 'adsense-widget' ),
$widget_ops
);
// Default widget options
$this->widget_defaults = array(
'title' => '',
'adsense_id' => '',
'slot' => '',
'format' => 'display',
'responsive' => 'on',
'layout_key' => '',
'sizes' => '',
'is_shortcode' => false
);
$this->ad_formats = $adsense_formats;
}
function widget( $args, $instance ) {
global $adsense_widget;
// extract widget options
extract( $args );
$widget_options = wp_parse_args( $instance, $this->widget_defaults );
extract( $widget_options, EXTR_SKIP );
$nl = "\n";
$responsive = (bool)('on' == $responsive);
// load plugin settings
$settings = $adsense_widget->get_settings();
$user_cap = current_user_can( $settings[ 'hide_ads_for' ] );
$publisher_id = $adsense_id;
// load publisher id from settings if possible
if( empty( $publisher_id )
&& array_key_exists( 'adsense_id', $settings )
&& $settings[ 'adsense_id' ] != '' ){
$publisher_id = $settings[ 'adsense_id' ] ;
}
// ad slot
if( empty( $slot ) && array_key_exists( 'shortcode_slot', $settings ) ){
$slot = $settings['shortcode_slot'];
}
if( empty( $publisher_id ) && current_user_can( 'administrator' ) ) {
echo '
Publisher Id is blank. Please add Publisher Id in
Adsense Widget settings page.
';
} else {
if( !$is_shortcode ){
echo $before_widget;
if ( !empty( $instance[ 'title' ] ) ) {
echo $before_title . apply_filters( 'widget_title', $instance[ 'title' ] ) . $after_title;
} /* title check */
}
// wrapper
$ad_class = 'format-' . $format;
if( $responsive ){ $ad_class .= ' responsive'; }
if( 'on' == $settings[ 'show_placeholders' ] && $user_cap ){
$ad_class .= ' placeholder';
}
// remove sizing for in_article format
if( 'in_article' === $format ){
$sizes = '';
}
echo "\n";
if( 'on' == $settings[ 'show_placeholders' ] && $user_cap ){
// placeholder
echo '
';
echo '
';
echo __( "Adsense", 'adsense-widget' );
echo ' ' . $this->ad_formats[ $format ] . '';
echo ' |
';
echo '
';
} else {
if ( adsense_widget_is_amp() ) {
echo $nl . "
";
echo $nl . " ";
echo $nl . "";
} else {
// adsbygoogle script
echo '';
echo '
';
// push script
echo "\n";
} /* is_amp_endpoint check */
} /* placeholder check */
echo "\n
";
if( !$is_shortcode ){
echo $after_widget;
}
} /* publisher id check */
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = isset( $new_instance['title'] ) ? wp_strip_all_tags( $new_instance['title'] ) : '';
$instance['adsense_id'] = isset( $new_instance['adsense_id'] ) ? wp_strip_all_tags( $new_instance['adsense_id'] ) : '';
$instance['format'] = isset( $new_instance['format'] ) ? $new_instance['format'] : '';
$instance['responsive'] = isset( $new_instance['responsive'] ) ? 'on' : 'off';
$instance['slot'] = isset( $new_instance['slot'] ) ? wp_strip_all_tags( $new_instance['slot'] ) : '';
$instance['layout_key'] = isset( $new_instance['layout_key'] ) ? wp_strip_all_tags( $new_instance['layout_key'] ) : '';
$instance['sizes'] = isset( $new_instance['sizes'] ) ? wp_strip_all_tags( $new_instance['sizes'] ) : '';
return $instance;
}
function form( $instance ) {
global $adsense_widget;
$widget_options = wp_parse_args( $instance, $this->widget_defaults );
extract( $widget_options, EXTR_SKIP );
if ( !empty( $instance['title'] ) ) {
$title = esc_attr($instance['title']);
}
if ( !empty( $instance['type'] ) ) {
$type = $instance['type'];
} else {
$type = null;
}
if( empty( $adsense_id ) ) {
$adsense_id = $adsense_widget->get_setting( 'adsense_id' );
}
?>
Slot id for the ad you created.( Eg. 1234567890 )
Sizing CSS classes.
Layout key is required for In-Feed ads.
Adsense Widget Settings >