Created
March 22, 2015 19:17
-
-
Save gabrielmerovingi/e734ac4c009d7c8299e8 to your computer and use it in GitHub Desktop.
Custom myCRED Hook for WP Postviews. Points can be awarded or deducted for each post view registered by the plugin with an option to set a limit. Requires myCRED 1.6 or higher.
This file contains 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
/** | |
* WP Postviews Hook | |
* @version 1.0 | |
* @author Gabriel S Merovingi | |
*/ | |
add_filter( 'mycred_setup_hooks', 'register_wp_postviews_hook_in_mycred' ); | |
function register_wp_postviews_hook_in_mycred( $installed ) { | |
$installed['wp_postviews'] = array( | |
'title' => __( 'WP Postviews', 'textdomain' ), | |
'description' => __( 'This hook awards / deducts points for post views via the WP Postviews plugin.', 'textdomain' ), | |
'callback' => array( 'myCRED_Hook_WP_Postviews' ) | |
); | |
return $installed; | |
} | |
if ( class_exists( 'myCRED_Hook' ) ) : | |
class myCRED_Hook_WP_Postviews extends myCRED_Hook { | |
/** | |
* Construct | |
*/ | |
function __construct( $hook_prefs, $type = 'mycred_default' ) { | |
parent::__construct( array( | |
'id' => 'wp_postviews', | |
'defaults' => array( | |
'creds' => 1, | |
'log' => '%plural% for post view', | |
'limit' => 0 | |
) | |
), $hook_prefs, $type ); | |
} | |
/** | |
* Hook into WP Postviews | |
* @since 1.0 | |
* @version 1.0 | |
*/ | |
public function run() { | |
add_action( 'postviews_increment_views', array( $this, 'new_view' ) ); | |
add_action( 'postviews_increment_views_ajax', array( $this, 'new_view_ajax' ) ); | |
} | |
/** | |
* New View | |
* @since 1.0 | |
* @version 1.0 | |
*/ | |
public function new_view() { | |
global $post; | |
$this->process_new_view( $post ); | |
} | |
/** | |
* New View Ajax | |
* @since 1.0 | |
* @version 1.0 | |
*/ | |
public function new_view_ajax() { | |
$post_id = intval( $_GET['postviews_id'] ); | |
$post = get_post( $post_id ); | |
if ( isset( $post->ID ) ) | |
$this->process_new_view( $post ); | |
} | |
/** | |
* Process View | |
* @since 1.0 | |
* @version 1.0 | |
*/ | |
protected function process_new_view( $post ) { | |
// Check for exclusions | |
if ( $this->core->exclude_user( $post->post_author ) ) return; | |
// Payout if not over limit | |
if ( ! $this->over_hook_limit( '', 'postview', $post->post_author ) ) | |
$this->core->add_creds( | |
'postview', | |
$post->post_author, | |
$this->prefs['creds'], | |
$this->prefs['log'], | |
$post->ID, | |
array( 'ref_type' => 'post' ), | |
$this->mycred_type | |
); | |
} | |
/** | |
* Preference for this Hook | |
* @since 1.0 | |
* @version 1.0 | |
*/ | |
public function preferences() { | |
$prefs = $this->prefs; | |
?> | |
<label class="subheader" for="<?php echo $this->field_id( 'creds' ); ?>"><?php _e( 'Post View', 'textdomain' ); ?></label> | |
<ol> | |
<li> | |
<div class="h2"><input type="text" name="<?php echo $this->field_name( 'creds' ); ?>" id="<?php echo $this->field_id( 'creds' ); ?>" value="<?php echo $this->core->format_number( $prefs['creds'] ); ?>" size="8" /></div> | |
</li> | |
<li> | |
<label for="<?php echo $this->field_id( 'limit' ); ?>"><?php _e( 'Limit', 'textdomain' ); ?></label> | |
<?php echo $this->hook_limit_setting( $this->field_name( 'limit' ), $this->field_id( 'limit' ), $prefs['limit'] ); ?> | |
</li> | |
<li class="empty"> </li> | |
<li> | |
<label for="<?php echo $this->field_id( 'log' ); ?>"><?php _e( 'Log template', 'textdomain' ); ?></label> | |
<div class="h2"><input type="text" name="<?php echo $this->field_name( 'log' ); ?>" id="<?php echo $this->field_id( 'log' ); ?>" value="<?php echo esc_attr( $prefs['log'] ); ?>" class="long" /></div> | |
<span class="description"><?php echo $this->core->available_template_tags( array( 'general', 'post' ) ); ?></span> | |
</li> | |
</ol> | |
<?php | |
} | |
/** | |
* Sanitise Preferences | |
* @since 1.0 | |
* @version 1.0 | |
*/ | |
public function sanitise_preferences( $data ) { | |
if ( isset( $data['limit'] ) && isset( $data['limit_by'] ) ) { | |
$limit = sanitize_text_field( $data['limit'] ); | |
if ( $limit == '' ) $limit = 0; | |
$data['limit'] = $limit . '/' . $data['limit_by']; | |
unset( $data['limit_by'] ); | |
} | |
return $data; | |
} | |
} | |
endif; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment