Advertisement
Guest User

Untitled

a guest
May 30th, 2015
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.76 KB | None | 0 0
  1. /**
  2. * Register myCRED Hook
  3. * First we need to tell myCRED that there is this custom hook.
  4. * We need to give a unique ID for our hook and define the Hook class's
  5. * name. The hook class is loaded seperatly.
  6. * @requires myCRED 1.6 or higher
  7. * @version 1.0.2
  8. */
  9. add_filter( 'mycred_setup_hooks', 'register_schreikasten_mycred_hook' );
  10. function register_schreikasten_mycred_hook( $installed ) {
  11.  
  12. $installed['schreikasten'] = array(
  13. 'title' => __( 'Schreikasten', 'mycred' ),
  14. 'description' => __( 'Awards %_plural% for comments.', 'mycred' ),
  15. 'callback' => array( 'myCRED_Schreikasten' )
  16. );
  17.  
  18. return $installed;
  19.  
  20. }
  21.  
  22. /**
  23. * Load Schreikasten Hook
  24. * @version 1.0
  25. */
  26. if ( ! class_exists( 'myCRED_Schreikasten' ) && class_exists( 'myCRED_Hook' ) ) :
  27.  
  28. /**
  29. * The Custom Hook Class
  30. * Built of the abstract myCRED_Hook class, we only need to define
  31. * a contruct, a run method, hook execution methods and if needed settings
  32. * and setting sanitation.
  33. * @version 1.0
  34. */
  35. class myCRED_Schreikasten extends myCRED_Hook {
  36.  
  37. /**
  38. * Construct
  39. */
  40. function __construct( $hook_prefs, $type = 'mycred_default' ) {
  41.  
  42. parent::__construct( array(
  43. 'id' => 'schreikasten',
  44. 'defaults' => array(
  45. 'new_comment' => array(
  46. 'creds' => 0,
  47. 'log' => '%plural% for new comment',
  48. 'limit' => '0/x'
  49. ),
  50. 'remove_comment' => array(
  51. 'creds' => 0,
  52. 'log' => '%plural% for removed comment',
  53. 'limit' => '0/x'
  54. )
  55. )
  56. ), $hook_prefs, $type );
  57.  
  58. }
  59.  
  60. /**
  61. * Run
  62. * This method runs during "init" if the hook has been enabled.
  63. * If the hook is not enabled, this class will never be constructed.
  64. * This methos should be used to hook into a third party plugin or load
  65. * core features.
  66. * @version 1.0.1
  67. */
  68. public function run() {
  69.  
  70. // If we reward affiliate signups
  71. add_action( 'sk_add', array( $this, 'new_comment' ) );
  72.  
  73. // If we reward visit referrals
  74. add_action( 'sk_delete', array( $this, 'delete_comment' ) );
  75.  
  76. // If we reward referrals
  77. add_filter( 'sk_points', array( $this, 'show_points' ), 10, 2 );
  78.  
  79. }
  80.  
  81. /**
  82. * New Comment
  83. * @version 1.0
  84. */
  85. public function new_comment( $comment ) {
  86.  
  87. if ( $comment->user_id == 0 ) return;
  88.  
  89. // Check for exclusion
  90. if ( $this->core->exclude_user( $comment->user_id ) ) return;
  91.  
  92. // Limit
  93. if ( $this->over_hook_limit( 'new_comment', 'approved_comment', $comment->user_id ) ) return;
  94.  
  95. // Execute
  96. $this->core->add_creds(
  97. 'approved_comment',
  98. $comment->user_id,
  99. $this->prefs['new_comment']['creds'],
  100. $this->prefs['new_comment']['log'],
  101. $comment->comment_ID,
  102. array( 'ref_id' => 'comment' ),
  103. $this->mycred_type
  104. );
  105.  
  106. }
  107.  
  108. /**
  109. * Delete Comment
  110. * @version 1.0
  111. */
  112. public function delete_comment( $comment ) {
  113.  
  114. if ( $comment->user_id == 0 ) return;
  115.  
  116. // Check for exclusion
  117. if ( $this->core->exclude_user( $comment->user_id ) ) return;
  118.  
  119. // Limit
  120. if ( $this->over_hook_limit( 'remove_comment', 'remove_comment', $comment->user_id ) ) return;
  121.  
  122. // Execute
  123. $this->core->add_creds(
  124. 'remove_comment',
  125. $comment->user_id,
  126. $this->prefs['remove_comment']['creds'],
  127. $this->prefs['remove_comment']['log'],
  128. $comment->comment_ID,
  129. array( 'ref_id' => 'comment' ),
  130. $this->mycred_type
  131. );
  132.  
  133. }
  134.  
  135. /**
  136. * Show Points
  137. * @version 1.0.1
  138. */
  139. public function show_points( $points, $comment ) {
  140.  
  141. if ( ! isset( $comment->user_id ) || $comment->user_id == 0 ) return $points;
  142.  
  143. // Check for exclusion
  144. if ( $this->core->exclude_user( $comment->user_id ) ) return $points;
  145.  
  146. // Get users balance
  147. $balance = $this->core->get_users_balance( $comment->user_id, $this->mycred_type );
  148.  
  149. return $this->core->format_creds( $balance );
  150.  
  151. }
  152.  
  153. /**
  154. * Preferences
  155. * Our hooks settings. This is optional and if this method is not defined
  156. * the hook will show "This hook has no settings" in the admin area.
  157. * In this case the only option available is to enable / disable the hook.
  158. * @version 1.0
  159. */
  160. public function preferences() {
  161.  
  162. $prefs = $this->prefs;
  163.  
  164. ?>
  165. <label for="<?php echo $this->field_id( array( 'new_comment', 'creds' ) ); ?>" class="subheader"><?php _e( 'New Comment', 'mycred' ); ?></label>
  166. <ol>
  167. <li>
  168. <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'new_comment', 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'new_comment', 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['new_comment']['creds'] ); ?>" size="8" /></div>
  169. </li>
  170. <li>
  171. <label for="<?php echo $this->field_id( array( 'new_comment' => 'limit' ) ); ?>"><?php _e( 'Limit', 'mycred' ); ?></label>
  172. <?php echo $this->hook_limit_setting( $this->field_name( array( 'new_comment' => 'limit' ) ), $this->field_id( array( 'new_comment' => 'limit' ) ), $prefs['new_comment']['limit'] ); ?>
  173. </li>
  174. <li class="empty">&nbsp;</li>
  175. <li>
  176. <label for="<?php echo $this->field_id( array( 'new_comment', 'log' ) ); ?>"><?php _e( 'Log template', 'mycred' ); ?></label>
  177. <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'new_comment', 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'new_comment', 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['new_comment']['log'] ); ?>" class="long" /></div>
  178. <span class="description"><?php echo $this->available_template_tags( array( 'general', 'comment' ) ); ?></span>
  179. </li>
  180. </ol>
  181.  
  182. <label for="<?php echo $this->field_id( array( 'remove_comment', 'creds' ) ); ?>" class="subheader"><?php _e( 'Delete Comment', 'mycred' ); ?></label>
  183. <ol>
  184. <li>
  185. <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'remove_comment', 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'remove_comment', 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['remove_comment']['creds'] ); ?>" size="8" /></div>
  186. </li>
  187. <li>
  188. <label for="<?php echo $this->field_id( array( 'remove_comment' => 'limit' ) ); ?>"><?php _e( 'Limit', 'mycred' ); ?></label>
  189. <?php echo $this->hook_limit_setting( $this->field_name( array( 'remove_comment' => 'limit' ) ), $this->field_id( array( 'remove_comment' => 'limit' ) ), $prefs['remove_comment']['limit'] ); ?>
  190. </li>
  191. <li class="empty">&nbsp;</li>
  192. <li>
  193. <label for="<?php echo $this->field_id( array( 'remove_comment', 'log' ) ); ?>"><?php _e( 'Log template', 'mycred' ); ?></label>
  194. <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'remove_comment', 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'remove_comment', 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['remove_comment']['log'] ); ?>" class="long" /></div>
  195. <span class="description"><?php echo $this->available_template_tags( array( 'general', 'comment' ) ); ?></span>
  196. </li>
  197. </ol>
  198. <?php
  199.  
  200. }
  201.  
  202. /**
  203. * Sanitise Preferences
  204. * If we are using the hook limit feature we must sanitize our settings.
  205. * This is because limits are stored as a string but submitted in the form
  206. * as two seperate items. These two values "limit" and "limit_by" must be combined
  207. * into limit/limit_by. Example (no limit) 0/x.
  208. * @version 1.0
  209. */
  210. function sanitise_preferences( $data ) {
  211.  
  212. if ( isset( $data['new_comment']['limit'] ) && isset( $data['new_comment']['limit_by'] ) ) {
  213. $limit = sanitize_text_field( $data['new_comment']['limit'] );
  214. if ( $limit == '' ) $limit = 0;
  215. $data['new_comment']['limit'] = $limit . '/' . $data['new_comment']['limit_by'];
  216. unset( $data['new_comment']['limit_by'] );
  217. }
  218.  
  219. if ( isset( $data['remove_comment']['limit'] ) && isset( $data['remove_comment']['limit_by'] ) ) {
  220. $limit = sanitize_text_field( $data['remove_comment']['limit'] );
  221. if ( $limit == '' ) $limit = 0;
  222. $data['remove_comment']['limit'] = $limit . '/' . $data['remove_comment']['limit_by'];
  223. unset( $data['remove_comment']['limit_by'] );
  224. }
  225.  
  226. return $data;
  227.  
  228. }
  229.  
  230. }
  231.  
  232. endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement