Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Plugin Name: TypeCRED
- * Plugin URI: https://www.edtechify.com/
- * Description: This addon enables you to set myCRED points for Typing Activities
- * Version: 1.0
- * Author: Robert Petitto
- * Author URI: https://www.edtechify.com/
- * Text Domain: wmca
- */
- // If this file is called directly, abort.
- if (!defined('WPINC')) {
- die;
- }
- add_filter('mycred_setup_hooks', 'mycredtyping_register');
- function mycredtyping_register($installed) {
- $installed['mycredtyping'] = array(
- 'title' => __('myCRED Typing', 'mycred'),
- 'description' => __('Adds a myCRED hook for tracking points scored in Typing Activities.', 'mycred'),
- 'callback' => array('myCRED_Hook_Typing')
- );
- return $installed;
- }
- /**
- *
- */
- function mycredtyping_badge($references) {
- $references['completing_typing'] = __('Completing Typing', 'mycred');
- return $references;
- }
- add_filter('mycred_all_references', 'mycredtyping_badge');
- /**
- *
- */
- function mycredtyping_init() {
- /**
- * Class
- */
- class myCRED_Hook_Typing extends myCRED_Hook {
- /**
- * Construct
- * USING MYCRED_CURRENCY WHICH IS A SECOND POINT TYPE
- */
- function __construct($hook_prefs, $type = 'mycred_currency' ) {
- parent::__construct(array(
- 'id' => 'mycredtyping',
- 'defaults' => array(
- 'completing_typing' => array(
- 'creds' => 0,
- 'log' => '%plural% for Completing a Typing Activity',
- )
- )
- ), $hook_prefs, $type);
- }
- /**
- * Hook into Typing
- */
- public function run() {
- // Type Completed
- if ( $this->prefs['completing_typing']['creds'] != 0 )
- add_action('finished_typing', array($this, 'typing_result'), 10, 4);
- }
- /**
- * Give points for Typing result
- */
- public function typing_result($data, $user_id) {
- $user_id = get_current_user_id();
- $average = ($data['length'] - $data['errors'])/$data['length'];
- $this->prefs['completing_typing']['creds'] = ceil( $data['length']/100 * $average ) ;
- if ($average < .8 ) $this->prefs['completing_typing']['creds'] = 0;
- // Execute
- $this->core->add_creds(
- 'completing_typing',
- $user_id,
- $this->prefs['completing_typing']['creds'],
- $this->prefs['completing_typing']['log'],
- array( 'ref_type' => 'post' ),
- $this->mycred_type
- );
- }
- public function preferences() {
- $prefs = $this->prefs;
- ?>
- <label class="subheader" for="<?php echo $this->field_id( array( 'completing_typing' => 'creds' ) ); ?>"><?php _e( 'Completing a Typing Activity', 'mycred' ); ?></label>
- <ol>
- <li>
- <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>
- </li>
- </ol>
- <label class="subheader" for="<?php echo $this->field_id( array( 'completing_typing' => 'log' ) ); ?>"><?php _e( 'Log Template', 'mycred' ); ?></label>
- <ol>
- <li>
- <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>
- <span class="description"><?php echo $this->available_template_tags( array( 'general', 'post' ) ); ?></span>
- </li>
- </ol>
- <?php
- }
- /**
- * Sanitize Preferences
- */
- public function sanitise_preferences( $data ) {
- $new_data = $data;
- // Apply defaults if any field is left empty
- $new_data['creds'] = ( !empty( $data['creds'] ) ) ? $data['creds'] : $this->defaults['creds'];
- $new_data['log'] = ( !empty( $data['log'] ) ) ? sanitize_text_field( $data['log'] ) : $this->defaults['log'];
- return $new_data;
- }
- }
- }
- add_action('mycred_pre_init', 'mycredtyping_init');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement