Instantly share code, notes, and snippets.
Last active
December 14, 2015 13:49
-
Star
2
(2)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save claudiosanches/5096573 to your computer and use it in GitHub Desktop.
Adsense by author with middle content.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Adsense by Author | |
* Plugin URI: http://claudiosmweb.com/ | |
* Description: Adsense by Author. | |
* Author: claudiosanches | |
* Author URI: http://claudiosmweb.com/ | |
* Version: 1.1 | |
* 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ício do post.', 'adbyauthor' ); ?></span> | |
</td> | |
</tr> | |
<tr> | |
<th><label for="banner_content"><?php _e( 'Banner conteúdo', 'adbyauthor' ); ?></label></th> | |
<td> | |
<textarea cols="30" rows="5" id="banner_content" name="banner_content"><?php echo esc_attr( get_the_author_meta( 'banner_content', $user->ID ) ); ?></textarea> | |
<br /><span class="description"><?php _e( 'Banner do meio do conteúdo.', 'adbyauthor' ); ?></span> | |
</td> | |
</tr> | |
<tr> | |
<th><label for="banner_bottom"><?php _e( 'Banner rodapé', '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é do post.', '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_content', $_POST['banner_content'] ); | |
} | |
/** | |
* Insert banners in the_content. | |
*/ | |
public function insert_blocks( $content ) { | |
if ( is_single() ) { | |
$banner_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>'; | |
$banner_content = '<div class="banner-author" id="banner-content" style="clear: both; margin-bottom: 20px;">' . get_the_author_meta( 'banner_content' ) . '<div style="clear: both"></div></div>'; | |
$banner_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>'; | |
$new_content = ''; | |
$content = explode( '</p>', $content ); | |
$total = count( $content ); | |
$middle = floor( $total / 2 ); | |
for ( $i = 0; $i < $total; $i++ ) { | |
if ( $i == $middle ) { | |
$new_content .= $banner_content; | |
} | |
// Prevents extra closure of paragraph. | |
if ( $i >= ( $total - 1 ) ) { | |
$new_content .= $content[$i]; | |
} else { | |
$new_content .= $content[$i] . '</p>'; | |
} | |
} | |
// Set the new content. | |
$content = $banner_top . $new_content . $banner_bottom; | |
} | |
return $content; | |
} | |
} // close Adsense_By_Author class. | |
new Adsense_By_Author; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment