Advertisement
designbymerovingi

Hook Skeleton FV Player

Mar 2nd, 2016
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.17 KB | None | 0 0
  1. /**
  2.  * Custom Hook: Register custom hook
  3.  * Register the hook so myCRED can load it.
  4.  * @since 1.0
  5.  * @version 1.0
  6.  */
  7. add_action( 'mycred_setup_hooks', 'mycredpro_register_custom_fv_video_hook' );
  8. function mycredpro_register_custom_fv_video_hook( $installed ) {
  9.  
  10.     $installed['watching_fv'] = array(
  11.         'title'       => 'Points for watching Flowplayer videos',
  12.         'description' => 'Award %_plural% to users for watching videos.',
  13.         'callback'    => array( 'myCRED_Hook_Fv_Views' )
  14.     );
  15.  
  16.     return $installed;
  17.  
  18. }
  19.  
  20. /**
  21.  * Custom Hook: Load custom hook
  22.  * Since 1.6, this would be the proper way to add in the hook class from a theme file
  23.  * or from a plugin file.
  24.  * @since 1.0
  25.  * @version 1.0
  26.  */
  27. add_action( 'mycred_load_hooks', 'mycredpro_load_custom_fv_video_hook', 10 );
  28. function mycredpro_load_custom_fv_video_hook() {
  29.  
  30.     class myCRED_Hook_Fv_Views extends myCRED_Hook {
  31.  
  32.         /**
  33.          * Construct
  34.          */
  35.         function __construct( $hook_prefs, $type = 'mycred_default' ) {
  36.  
  37.             parent::__construct( array(
  38.                 'id'       => 'watching_fv',
  39.                 'defaults' => array(
  40.                     'creds'    => 'post',
  41.                     'limit'    => '0/x',
  42.                     'log'      => '%plural% for watching a video'
  43.                 )
  44.             ), $hook_prefs, $type );
  45.  
  46.         }
  47.  
  48.         /**
  49.          * Run
  50.          * @since 1.0
  51.          * @version 1.0
  52.          */
  53.         public function run() {
  54.  
  55.             add_action( 'wp_ajax_your-ajax-action', array( $this, 'ajax_handler' ), 10, 3 );
  56.  
  57.         }
  58.  
  59.         /**
  60.          * AJAX Handler
  61.          * @since 1.0
  62.          * @version 1.0
  63.          */
  64.         public function ajax_handler() {
  65.  
  66.             // First you probably want some security by verifing a token maybe
  67.  
  68.             // Next we need the users ID
  69.             $user_id = get_current_user_id();
  70.  
  71.             // Should check if the user is excluded from using myCRED
  72.             if ( $this->core->exclude_user( $user_id ) ) die;
  73.  
  74.             // We need: A unique identifier for the video. If this is an int, we can save it as ref_id
  75.             // if it's a string, we can save it under data. Either or, not both
  76.  
  77.             // Give points if we are not over a limit (if used)
  78.             if ( ! $this->over_hook_limit( '', 'watching_fv_video', $user_id ) )
  79.                 $this->core->add_creds(
  80.                     'watching_fv_video',    // a unqiue reference for this hook
  81.                     $user_id,               // the user to get the points
  82.                     $this->prefs['creds'],  // the amount of points to give / take
  83.                     $this->prefs['log'],    // the log entry tempalte
  84.                     $unique_id_as_ref_id,   // numeric identifier is stored here
  85.                     $unique_id_as_data,     // string identifier is stored here
  86.                     $this->mycred_type      // should not be changed unless you understand the consequence of doing so
  87.                 );
  88.  
  89.             die;
  90.  
  91.         }
  92.  
  93.         /**
  94.          * Preference for Publish Content Hook
  95.          * @since 1.0
  96.          * @version 1.0
  97.          */
  98.         public function preferences() {
  99.  
  100.             $prefs = $this->prefs;
  101.  
  102. ?>
  103. <label for="<?php echo $this->field_id( 'creds' ); ?>" class="subheader"><?php _e( 'Referring Visitors', 'mycred' ); ?></label>
  104. <ol>
  105.     <li>
  106.         <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>
  107.     </li>
  108.     <li>
  109.         <label for="<?php echo $this->field_id( 'limit' ); ?>"><?php _e( 'Limit', 'mycred' ); ?></label>
  110.         <?php echo $this->hook_limit_setting( $this->field_name( 'limit' ), $this->field_id( 'limit' ), $prefs['limit'] ); ?>
  111.     </li>
  112.     <li class="empty">&nbsp;</li>
  113.     <li>
  114.         <label for="<?php echo $this->field_id( 'log' ); ?>"><?php _e( 'Log template', 'mycred' ); ?></label>
  115.         <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>
  116.         <span class="description"><?php echo $this->available_template_tags( array( 'general' ) ); ?></span>
  117.     </li>
  118. </ol>
  119. <?php
  120.  
  121.         }
  122.  
  123.         /**
  124.          * Sanitise Preferences
  125.          * @since 1.0
  126.          * @version 1.0
  127.          */
  128.         function sanitise_preferences( $data ) {
  129.  
  130.             if ( isset( $data['limit'] ) && isset( $data['limit_by'] ) ) {
  131.                 $limit = sanitize_text_field( $data['limit'] );
  132.                 if ( $limit == '' ) $limit = 0;
  133.                 $data['limit'] = $limit . '/' . $data['limit_by'];
  134.                 unset( $data['limit_by'] );
  135.             }
  136.  
  137.             return $data;
  138.  
  139.         }
  140.  
  141.     }
  142.  
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement