Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Register myCRED Hook
- * First we need to tell myCRED that there is this custom hook.
- * We need to give a unique ID for our hook and define the Hook class's
- * name. The hook class is loaded seperatly.
- * @requires myCRED 1.6 or higher
- * @version 1.0
- */
- add_filter( 'mycred_setup_hooks', 'register_my_custom_mycred_hook' );
- function register_my_custom_mycred_hook( $installed ) {
- $installed['uniqueid'] = array(
- 'title' => __( 'My Hook Name', 'textdomain' ),
- 'description' => __( 'Awards %_plural% for completing quizes.', 'textdomain' ),
- 'callback' => array( 'myCRED_My_Custom_Hook_Class' )
- );
- return $installed;
- }
- /**
- * Load Custom Hook
- * @version 1.0
- */
- if ( ! class_exists( 'myCRED_My_Custom_Hook_Class' ) && class_exists( 'myCRED_Hook' ) ) :
- /**
- * The Custom Hook Class
- * Built of the abstract myCRED_Hook class, we only need to define
- * a contruct, a run method, hook execution methods and if needed settings
- * and setting sanitation.
- * @version 1.0
- */
- class myCRED_My_Custom_Hook_Class extends myCRED_Hook {
- /**
- * Construct
- * $hook_prefs - an associative array of all hook settings.
- * $type - the point type using this class.
- */
- function __construct( $hook_prefs, $type = 'mycred_default' ) {
- parent::__construct( array(
- 'id' => 'uniqueid',
- 'defaults' => array(
- 'creds' => 0,
- 'log' => '%plural% for doing something',
- 'limit' => '0/x'
- )
- ), $hook_prefs, $type );
- }
- /**
- * Run
- * This method runs during "init" if the hook has been enabled.
- * If the hook is not enabled, this class will never be constructed.
- * This methos should be used to hook into a third party plugin or load
- * core features.
- * @version 1.0
- */
- public function run() {
- // Add javascript to the website footer
- add_action( 'wp_footer', array( $this, 'wp_footer' ) );
- // Add the AJAX call handler
- add_action( 'wp_ajax_my-custom-ajax-handle', array( $this, 'ajax_handler' ) );
- }
- /**
- * WordPress Footer
- * @version 1.0
- */
- public function wp_footer() {
- // Users must be logged in
- if ( ! is_user_logged_in() ) return;
- // If the user is set to be excluded from using
- // mycCRED, there is no need to load the script
- if ( $this->core->exclude_user( get_current_user_id() ) ) return;
- ?>
- <script type="text/javascript">
- jQuery(function($) {
- // Listen for changes in the element.
- $( '#resultselement' ).on( 'change', function(){
- // AJAX
- $.ajax({
- type : "POST",
- data : {
- action : 'my-custom-ajax-handle',
- content : $(this).text()
- },
- dataType : "JSON",
- url : ajaxurl,
- success : function( data ) {
- // If you need to do something once the ajax script has run
- }
- });
- });
- });
- </script>
- <?php
- }
- /**
- * The AJAX Handler
- * @version 1.0
- */
- public function ajax_handler() {
- // Act on the content in the element
- }
- /**
- * Preferences
- * Our hooks settings. This is optional and if this method is not defined
- * the hook will show "This hook has no settings" in the admin area.
- * In this case the only option available is to enable / disable the hook.
- * @version 1.0
- */
- public function preferences() {
- $prefs = $this->prefs;
- ?>
- <label for="<?php echo $this->field_id( 'creds' ); ?>" class="subheader"><?php _e( 'Completing Quiz', 'mycred' ); ?></label>
- <ol>
- <li>
- <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>
- </li>
- <li>
- <label for="<?php echo $this->field_id( 'limit' ); ?>"><?php _e( 'Limit', 'mycred' ); ?></label>
- <?php echo $this->hook_limit_setting( $this->field_name( 'limit' ), $this->field_id( 'limit' ), $prefs['limit'] ); ?>
- </li>
- <li class="empty"> </li>
- <li>
- <label for="<?php echo $this->field_id( 'log' ); ?>"><?php _e( 'Log template', 'mycred' ); ?></label>
- <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>
- </li>
- </ol>
- <?php
- }
- /**
- * Sanitise Preferences
- * If we are using the hook limit feature we must sanitize our settings.
- * This is because limits are stored as a string but submitted in the form
- * as two seperate items. These two values "limit" and "limit_by" must be combined
- * into limit/limit_by. Example (no limit) 0/x.
- * @version 1.0
- */
- function sanitise_preferences( $data ) {
- if ( isset( $data['limit'] ) && isset( $data['limit_by'] ) ) {
- $limit = sanitize_text_field( $data['limit'] );
- if ( $limit == '' ) $limit = 0;
- $data['limit'] = $limit . '/' . $data['limit_by'];
- unset( $data['limit_by'] );
- }
- return $data;
- }
- }
- endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement