Skip to content

Instantly share code, notes, and snippets.

@claudiosanches
Created January 19, 2013 20:32
Show Gist options
  • Save claudiosanches/4574909 to your computer and use it in GitHub Desktop.
Save claudiosanches/4574909 to your computer and use it in GitHub Desktop.
WordPress plugin - Adsense by Author
<?php
/**
* Plugin Name: Adsense by Author
* Plugin URI: http://claudiosmweb.com/
* Description: Adsense by Author.
* Author: claudiosanches
* Author URI: http://claudiosmweb.com/
* Version: 1.0
* License: GPLv2 or later
* Text Domain: adbyauthor
* Domain Path: /languages/
*/
class Adsense_By_Author {
public function __construct() {
// Actions.
add_action( 'show_user_profile', array( &$this, 'add_profile_fields' ) );
add_action( 'edit_user_profile', array( &$this, 'add_profile_fields' ) );
add_action( 'personal_options_update', array( &$this, 'save_profile_fields' ) );
add_action( 'edit_user_profile_update', array( &$this, 'save_profile_fields' ) );
// Filters.
add_filter( 'the_content', array( &$this, 'insert_blocks' ) );
}
/**
* Add profile fields.
*/
public function add_profile_fields( $user ) {
?>
<h3><?php _e( 'Banners de publicidade', 'adbyauthor' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="banner_top"><?php _e( 'Banner topo', 'adbyauthor' ); ?></label></th>
<td>
<textarea cols="30" rows="5" id="banner_top" name="banner_top"><?php echo esc_attr( get_the_author_meta( 'banner_top', $user->ID ) ); ?></textarea>
<br /><span class="description"><?php _e( 'Banner da in&iacute;cio do post.', 'adbyauthor' ); ?></span>
</td>
</tr>
<tr>
<th><label for="banner_bottom"><?php _e( 'Banner rodap&eacute;', 'adbyauthor' ); ?></label></th>
<td>
<textarea cols="30" rows="5" id="banner_bottom" name="banner_bottom"><?php echo esc_attr( get_the_author_meta( 'banner_bottom', $user->ID ) ); ?></textarea>
<br /><span class="description"><?php _e( 'Banner do rodap&eacute; do post.', 'adbyauthor' ); ?></span>
</td>
</tr>
<tr>
<th><label for="banner_sidebar"><?php _e( 'Banner sidebar', 'adbyauthor' ); ?></label></th>
<td>
<textarea cols="30" rows="5" id="banner_sidebar" name="banner_sidebar"><?php echo esc_attr( get_the_author_meta( 'banner_sidebar', $user->ID ) ); ?></textarea>
<br /><span class="description"><?php _e( 'Banner da sidebar.', 'adbyauthor' ); ?></span>
</td>
</tr>
</table>
<?php
}
/**
* Save profile fields.
*/
public function save_profile_fields( $user_id ) {
if ( ! current_user_can( 'edit_user', $user_id ) ) {
return false;
}
update_usermeta( $user_id, 'banner_top', $_POST['banner_top'] );
update_usermeta( $user_id, 'banner_bottom', $_POST['banner_bottom'] );
update_usermeta( $user_id, 'banner_sidebar', $_POST['banner_sidebar'] );
}
/**
* Insert banners in the_content.
*/
public function insert_blocks( $content ) {
if ( is_single() ) {
$top = '<div class="banner-author" id="banner-top" style="clear: both; margin-bottom: 20px;">' . get_the_author_meta( 'banner_top' ) . '<div style="clear: both"></div></div>';
$bottom = '<div class="banner-author" id="banner-bottom" style="clear: both; margin-bottom: 20px;">' . get_the_author_meta( 'banner_bottom' ) . '<div style="clear: both"></div></div>';
$content = $top . $content . $bottom;
}
return $content;
}
} // close Adsense_By_Author class.
$adsense_by_author = new Adsense_By_Author;
/**
* Register Widget.
*/
class Adsense_By_Author_Widget extends WP_Widget {
function Adsense_By_Author_Widget() {
$widget_ops = array( 'adsense_by_author_widget' => 'Adsense_By_Author_Widget', 'description' => 'Exibe um banner do autor.' );
$this->WP_Widget( 'Adsense_By_Author_Widget', 'Banner do autor', $widget_ops );
}
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'banner' => '' ) );
$title = $instance['title'];
$banner = $instance['banner'];
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'adbyauthor' ); ?>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'banner' ); ?>"><?php _e( 'Banner padrão:', 'adbyauthor' ); ?>
<textarea name="<?php echo $this->get_field_name( 'banner' ); ?>" id="widget-text-2-text" id="<?php echo $this->get_field_id( 'banner' ); ?>" cols="20" rows="16" class="widefat"><?php echo esc_attr( $banner ); ?></textarea>
</label>
<br /><span class="description"><?php _e( 'Adicione um banner padr&atilde;o que ser&aacute; usado nas categorias e outras p&aacute;ginas do site.', 'adbyauthor' ); ?></span>
</p>
<?php
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['banner'] = $new_instance['banner'];
return $instance;
}
function widget( $args, $instance ) {
extract( $args, EXTR_SKIP );
echo $before_widget;
// Display widget.
if ( is_single() ) {
$banner_sidebar = get_the_author_meta( 'banner_sidebar' );
if ( $banner_sidebar ){
echo '<div class="banner-author banner-sidebar">' . $banner_sidebar . '</div>';
} else {
$title = empty( $instance['title'] ) ? '' : apply_filters( 'widget_title', $instance['title'] );
if ( ! empty( $title ) ) {
echo $before_title . $title . $after_title;
}
$banner = empty( $instance['banner'] ) ? '' : apply_filters( 'widget_banner', $instance['banner'] );
if ( ! empty( $banner ) ) {
echo '<div class="author-banner">' . $banner . '</div>';
}
}
} else {
$title = empty( $instance['title'] ) ? '' : apply_filters( 'widget_title', $instance['title'] );
if ( ! empty( $title ) ) {
echo $before_title . $title . $after_title;
}
$banner = empty( $instance['banner'] ) ? '' : apply_filters( 'widget_banner', $instance['banner'] );
if ( ! empty( $banner ) ) {
echo '<div class="author-banner">' . $banner . '</div>';
}
}
echo $after_widget;
}
}
add_action( 'widgets_init', create_function( '', 'return register_widget( "Adsense_By_Author_Widget" );' ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment