Advertisement
Guest User

Untitled

a guest
Mar 28th, 2018
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. <?php
  2. /**
  3. * Plugin Name: TypeCRED
  4. * Plugin URI: https://www.edtechify.com/
  5. * Description: This addon enables you to set myCRED points for Typing Activities
  6. * Version: 1.0
  7. * Author: Robert Petitto
  8. * Author URI: https://www.edtechify.com/
  9. * Text Domain: wmca
  10. */
  11. // If this file is called directly, abort.
  12. if (!defined('WPINC')) {
  13. die;
  14. }
  15. add_filter('mycred_setup_hooks', 'mycredtyping_register');
  16.  
  17. function mycredtyping_register($installed) {
  18. $installed['mycredtyping'] = array(
  19. 'title' => __('myCRED Typing', 'mycred'),
  20. 'description' => __('Adds a myCRED hook for tracking points scored in Typing Activities.', 'mycred'),
  21. 'callback' => array('myCRED_Hook_Typing')
  22. );
  23. return $installed;
  24. }
  25. /**
  26. *
  27. */
  28. function mycredtyping_badge($references) {
  29. $references['completing_typing'] = __('Completing Typing', 'mycred');
  30. return $references;
  31. }
  32. add_filter('mycred_all_references', 'mycredtyping_badge');
  33. /**
  34. *
  35. */
  36. function mycredtyping_init() {
  37. /**
  38. * Class
  39. */
  40. class myCRED_Hook_Typing extends myCRED_Hook {
  41. /**
  42. * Construct
  43. * USING MYCRED_CURRENCY WHICH IS A SECOND POINT TYPE
  44. */
  45. function __construct($hook_prefs, $type = 'mycred_currency' ) {
  46. parent::__construct(array(
  47. 'id' => 'mycredtyping',
  48. 'defaults' => array(
  49. 'completing_typing' => array(
  50. 'creds' => 0,
  51. 'log' => '%plural% for Completing a Typing Activity',
  52. )
  53. )
  54. ), $hook_prefs, $type);
  55. }
  56. /**
  57. * Hook into Typing
  58. */
  59. public function run() {
  60. // Type Completed
  61. if ( $this->prefs['completing_typing']['creds'] != 0 )
  62. add_action('finished_typing', array($this, 'typing_result'), 10, 4);
  63. }
  64. /**
  65. * Give points for Typing result
  66. */
  67. public function typing_result($data, $user_id) {
  68. $user_id = get_current_user_id();
  69. $average = ($data['length'] - $data['errors'])/$data['length'];
  70. $this->prefs['completing_typing']['creds'] = ceil( $data['length']/100 * $average ) ;
  71. if ($average < .8 ) $this->prefs['completing_typing']['creds'] = 0;
  72.  
  73. // Execute
  74. $this->core->add_creds(
  75. 'completing_typing',
  76. $user_id,
  77. $this->prefs['completing_typing']['creds'],
  78. $this->prefs['completing_typing']['log'],
  79. array( 'ref_type' => 'post' ),
  80. $this->mycred_type
  81. );
  82. }
  83. public function preferences() {
  84. $prefs = $this->prefs;
  85. ?>
  86. <label class="subheader" for="<?php echo $this->field_id( array( 'completing_typing' => 'creds' ) ); ?>"><?php _e( 'Completing a Typing Activity', 'mycred' ); ?></label>
  87. <ol>
  88. <li>
  89. <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'completing_typing' => 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'completing_typing' => 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['completing_typing']['creds'] ); ?>" size="8" /></div>
  90. </li>
  91. </ol>
  92. <label class="subheader" for="<?php echo $this->field_id( array( 'completing_typing' => 'log' ) ); ?>"><?php _e( 'Log Template', 'mycred' ); ?></label>
  93. <ol>
  94. <li>
  95. <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'completing_typing' => 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'completing_typing' => 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['completing_typing']['log'] ); ?>" class="long" /></div>
  96. <span class="description"><?php echo $this->available_template_tags( array( 'general', 'post' ) ); ?></span>
  97. </li>
  98. </ol>
  99. <?php
  100. }
  101. /**
  102. * Sanitize Preferences
  103. */
  104. public function sanitise_preferences( $data ) {
  105. $new_data = $data;
  106. // Apply defaults if any field is left empty
  107. $new_data['creds'] = ( !empty( $data['creds'] ) ) ? $data['creds'] : $this->defaults['creds'];
  108. $new_data['log'] = ( !empty( $data['log'] ) ) ? sanitize_text_field( $data['log'] ) : $this->defaults['log'];
  109. return $new_data;
  110. }
  111. }
  112. }
  113. add_action('mycred_pre_init', 'mycredtyping_init');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement