Advertisement
designbymerovingi

Custom Hook Limit / 4 hours

Sep 9th, 2017
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.34 KB | None | 0 0
  1. /**
  2.  * Add Custom Hook Limit
  3.  * @version 1.0
  4.  */
  5. function mycred_pro_add_custom_hook_limit( $limits ) {
  6.  
  7.     $limits['h'] = '4 hours';
  8.  
  9.     return $limits;
  10.  
  11. }
  12. add_filter( 'mycred_hook_limits', 'mycred_pro_add_custom_hook_limit' );
  13.  
  14. /**
  15.  * Enforce Custom Hook Limit
  16.  * @version 1.0
  17.  */
  18. function mycred_pro_run_custom_hook_limit( $over_limit, $instance, $reference, $user_id, $ref_id, $hook ) {
  19.  
  20.     // Only applicable for the "Viewing Content" hook
  21.     if ( $reference != 'view_content' ) return $over_limit;
  22.  
  23.     // Prefs
  24.     $prefs             = $this->prefs[ $instance ]['limit'];
  25.  
  26.     // No limit is set, bail
  27.     if ( $prefs == '0/x' ) return $over_limit;
  28.  
  29.     $prefs             = explode( '/', $prefs );
  30.  
  31.     // Some other limit is used, bail
  32.     if ( $prefs[1] != 'h' ) return $over_limit;
  33.  
  34.     $maximum           = $prefs[0];
  35.  
  36.     // Get the timestamp for 4 hours ago
  37.     $four_hours_ago    = current_time( 'timestamp' ) - ( 4 * HOUR_IN_SECONDS );
  38.  
  39.     global $wpdb, $mycred_log_table;
  40.  
  41.     $total_last4_hours = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$mycred_log_table} WHERE ref = %s AND user_id = %d AND time >= %d;", $reference, $user_id, $four_hours_ago ) );
  42.  
  43.     // Check if we are over the limit
  44.     if ( $total_last4_hours >= $maximum ) return false;
  45.  
  46.     return true;
  47.  
  48. }
  49. add_filter( 'mycred_over_hook_limit', 'mycred_pro_run_custom_hook_limit', 10, 6 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement