Advertisement
designbymerovingi

Custom myCRED Hook Example

May 14th, 2015
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.96 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
  8.  */
  9. add_filter( 'mycred_setup_hooks', 'register_my_custom_mycred_hook' );
  10. function register_my_custom_mycred_hook( $installed ) {
  11.  
  12.     $installed['uniqueid'] = array(
  13.         'title'       => __( 'My Hook Name', 'textdomain' ),
  14.         'description' => __( 'Awards %_plural% for completing quizes.', 'textdomain' ),
  15.         'callback'    => array( 'myCRED_My_Custom_Hook_Class' )
  16.     );
  17.  
  18.     return $installed;
  19.  
  20. }
  21.  
  22. /**
  23.  * Load Custom Hook
  24.  * @version 1.0
  25.  */
  26. if ( ! class_exists( 'myCRED_My_Custom_Hook_Class' ) && 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_My_Custom_Hook_Class extends myCRED_Hook {
  36.  
  37.         /**
  38.          * Construct
  39.          * $hook_prefs - an associative array of all hook settings.
  40.          * $type - the point type using this class.
  41.          */
  42.         function __construct( $hook_prefs, $type = 'mycred_default' ) {
  43.  
  44.             parent::__construct( array(
  45.                 'id'       => 'uniqueid',
  46.                 'defaults' => array(
  47.                     'creds'  => 0,
  48.                     'log'    => '%plural% for doing something',
  49.                     'limit'  => '0/x'
  50.                 )
  51.             ), $hook_prefs, $type );
  52.  
  53.         }
  54.  
  55.         /**
  56.          * Run
  57.          * This method runs during "init" if the hook has been enabled.
  58.          * If the hook is not enabled, this class will never be constructed.
  59.          * This methos should be used to hook into a third party plugin or load
  60.          * core features.
  61.          * @version 1.0
  62.          */
  63.         public function run() {
  64.  
  65.             // Add javascript to the website footer
  66.             add_action( 'wp_footer', array( $this, 'wp_footer' ) );
  67.  
  68.             // Add the AJAX call handler
  69.             add_action( 'wp_ajax_my-custom-ajax-handle', array( $this, 'ajax_handler' ) );
  70.  
  71.         }
  72.  
  73.         /**
  74.          * WordPress Footer
  75.          * @version 1.0
  76.          */
  77.         public function wp_footer() {
  78.  
  79.             // Users must be logged in
  80.             if ( ! is_user_logged_in() ) return;
  81.  
  82.             // If the user is set to be excluded from using
  83.             // mycCRED, there is no need to load the script
  84.             if ( $this->core->exclude_user( get_current_user_id() ) ) return;
  85.  
  86. ?>
  87. <script type="text/javascript">
  88. jQuery(function($) {
  89.  
  90.     // Listen for changes in the element.
  91.     $( '#resultselement' ).on( 'change', function(){
  92.  
  93.         // AJAX
  94.         $.ajax({
  95.             type     : "POST",
  96.             data     : {
  97.                 action  : 'my-custom-ajax-handle',
  98.                 content : $(this).text()
  99.             },
  100.             dataType : "JSON",
  101.             url      : ajaxurl,
  102.             success  : function( data ) {
  103.  
  104.                 // If you need to do something once the ajax script has run
  105.  
  106.             }
  107.         });
  108.  
  109.     });
  110.  
  111. });
  112. </script>
  113. <?php
  114.  
  115.         }
  116.  
  117.         /**
  118.          * The AJAX Handler
  119.          * @version 1.0
  120.          */
  121.         public function ajax_handler() {
  122.  
  123.             // Act on the content in the element
  124.  
  125.         }
  126.  
  127.         /**
  128.          * Preferences
  129.          * Our hooks settings. This is optional and if this method is not defined
  130.          * the hook will show "This hook has no settings" in the admin area.
  131.          * In this case the only option available is to enable / disable the hook.
  132.          * @version 1.0
  133.          */
  134.         public function preferences() {
  135.  
  136.             $prefs = $this->prefs;
  137.  
  138. ?>
  139. <label for="<?php echo $this->field_id( 'creds' ); ?>" class="subheader"><?php _e( 'Completing Quiz', 'mycred' ); ?></label>
  140. <ol>
  141.     <li>
  142.         <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>
  143.     </li>
  144.     <li>
  145.         <label for="<?php echo $this->field_id( 'limit' ); ?>"><?php _e( 'Limit', 'mycred' ); ?></label>
  146.         <?php echo $this->hook_limit_setting( $this->field_name( 'limit' ), $this->field_id( 'limit' ), $prefs['limit'] ); ?>
  147.     </li>
  148.     <li class="empty">&nbsp;</li>
  149.     <li>
  150.         <label for="<?php echo $this->field_id( 'log' ); ?>"><?php _e( 'Log template', 'mycred' ); ?></label>
  151.         <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>
  152.     </li>
  153. </ol>
  154. <?php
  155.  
  156.         }
  157.            
  158.         /**
  159.          * Sanitise Preferences
  160.          * If we are using the hook limit feature we must sanitize our settings.
  161.          * This is because limits are stored as a string but submitted in the form
  162.          * as two seperate items. These two values "limit" and "limit_by" must be combined
  163.          * into limit/limit_by. Example (no limit) 0/x.
  164.          * @version 1.0
  165.          */
  166.         function sanitise_preferences( $data ) {
  167.  
  168.             if ( isset( $data['limit'] ) && isset( $data['limit_by'] ) ) {
  169.                 $limit = sanitize_text_field( $data['limit'] );
  170.                 if ( $limit == '' ) $limit = 0;
  171.                 $data['limit'] = $limit . '/' . $data['limit_by'];
  172.                 unset( $data['limit_by'] );
  173.             }
  174.  
  175.             return $data;
  176.  
  177.         }
  178.  
  179.     }
  180.  
  181. endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement