Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Custom Hook: Register custom hook
- * Register the hook so myCRED can load it.
- * @since 1.0
- * @version 1.0
- */
- add_action( 'mycred_setup_hooks', 'mycredpro_register_custom_fv_video_hook' );
- function mycredpro_register_custom_fv_video_hook( $installed ) {
- $installed['watching_fv'] = array(
- 'title' => 'Points for watching Flowplayer videos',
- 'description' => 'Award %_plural% to users for watching videos.',
- 'callback' => array( 'myCRED_Hook_Fv_Views' )
- );
- return $installed;
- }
- /**
- * Custom Hook: Load custom hook
- * Since 1.6, this would be the proper way to add in the hook class from a theme file
- * or from a plugin file.
- * @since 1.0
- * @version 1.0
- */
- add_action( 'mycred_load_hooks', 'mycredpro_load_custom_fv_video_hook', 10 );
- function mycredpro_load_custom_fv_video_hook() {
- class myCRED_Hook_Fv_Views extends myCRED_Hook {
- /**
- * Construct
- */
- function __construct( $hook_prefs, $type = 'mycred_default' ) {
- parent::__construct( array(
- 'id' => 'watching_fv',
- 'defaults' => array(
- 'creds' => 'post',
- 'limit' => '0/x',
- 'log' => '%plural% for watching a video'
- )
- ), $hook_prefs, $type );
- }
- /**
- * Run
- * @since 1.0
- * @version 1.0
- */
- public function run() {
- add_action( 'wp_ajax_your-ajax-action', array( $this, 'ajax_handler' ), 10, 3 );
- }
- /**
- * AJAX Handler
- * @since 1.0
- * @version 1.0
- */
- public function ajax_handler() {
- // First you probably want some security by verifing a token maybe
- // Next we need the users ID
- $user_id = get_current_user_id();
- // Should check if the user is excluded from using myCRED
- if ( $this->core->exclude_user( $user_id ) ) die;
- // We need: A unique identifier for the video. If this is an int, we can save it as ref_id
- // if it's a string, we can save it under data. Either or, not both
- // Give points if we are not over a limit (if used)
- if ( ! $this->over_hook_limit( '', 'watching_fv_video', $user_id ) )
- $this->core->add_creds(
- 'watching_fv_video', // a unqiue reference for this hook
- $user_id, // the user to get the points
- $this->prefs['creds'], // the amount of points to give / take
- $this->prefs['log'], // the log entry tempalte
- $unique_id_as_ref_id, // numeric identifier is stored here
- $unique_id_as_data, // string identifier is stored here
- $this->mycred_type // should not be changed unless you understand the consequence of doing so
- );
- die;
- }
- /**
- * Preference for Publish Content Hook
- * @since 1.0
- * @version 1.0
- */
- public function preferences() {
- $prefs = $this->prefs;
- ?>
- <label for="<?php echo $this->field_id( 'creds' ); ?>" class="subheader"><?php _e( 'Referring Visitors', 'mycred' ); ?></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->number( $prefs['creds'] ); ?>" size="8" /></div>
- </li>
- <li>
- <label for="<?php echo $this->field_id( 'limit' ); ?>"><?php _e( 'Limit', 'mycred' ); ?></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', 'mycred' ); ?></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->available_template_tags( array( 'general' ) ); ?></span>
- </li>
- </ol>
- <?php
- }
- /**
- * Sanitise Preferences
- * @since 1.0
- * @version 1.0
- */
- 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;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement