myCRED_Hook

Codex / mycred/core / Classes / myCRED_Hook

Navigation:

  1. Description
  2. Usage
  3. Properties
  4. Methods
  5. Hook Parameters
  6. Examples

Description

myCRED_Hook is an abstract class used by all myCRED Hooks. It contains commonly used methods to help you build hooks without to much programming.

 

 

Usage

class My_Custom_Hook extends myCRED_Hook {
	function __construct( $hook_prefs ) {
		parent::__construct( $args, $hook_prefs );
	}
}

 

Properties

  • $id (string)
    Hook ID.
  • $core (object)
    myCRED Settings.
  • $prefs (array)
    This hooks preferences.

 

Methods

  1. myCRED_Hook()Constructs the hook class populating $id, $core and $prefs.

  2. run()Method containing your WordPress or Plugin hooks. Your Hook class must replace this method or myCRED_Hook will fail and die.

  3. preferences()Method to output this hooks preferences on the “Hooks” page. If not defined in your class, the default “This Hook has no settings” text is returned.

  4. sanitise_preferences( $data )Option to sanitize hook settings before they are saved in the database.

    • $data (array)
      The posted data.
  5. field_name( $data )Outputs this hooks field name.

    • $data (string|array)
      Either a string with the field id or an array if hook has multiple settings.
  6. field_id( $data )Outputs this hooks field id.

    • $data (string|array)
      Either a string with the field id or an array if hook has multiple settings.
  7. impose_limits_dropdown( $pref_id )Outputs a select element with the available limits. Used by the “Points for Loggin” hook but can be used by any hook that needs the option of imposing time specific limits.

    • $pref_id (string|array)
      The preference id holding the value.
  8. has_entry( $action, $ref_id, $user_id )Queries the database to see if the given variable combination already exists in the log, to avoid duplicate entries. Returns true if a record was found or false.

    • $action (string) required
      An action reference to search for.
    • $ref_id (int)
      Optional reference id to add in search.
    • $user_id (int)
      Optional user id to add in search.

 

Hook Parameters

// default
$args = array(
	'id'        => '',
	'defaults'  => array()
);
  • id (string)
    Required Hook ID.
  • defaults (array)
    Array of default settings for this hook.

 

Examples

Example 1: A hook with one instance.

class My_Custom_Hook extends myCRED_Hook {
	function __construct( $hook_prefs ) {
		// Construct
		parent::__construct( array(
			'id'       => 'custom',
			'defaults' => array(
				'creds'    => 10,
				'log'      => '%plural% for custom instance'
			)
		), $hook_prefs );
	}

	public function run() {
		// Since we are running a single instance, we do not need to check
		// if points are set to zero (disable). myCRED will check if this
		// hook has been enabled before calling this method so no need to check
		// that either.
		add_action( 'a_hook_id', array( $this, 'hook_method' ) );
	}

	public function hook_method() {
		// Do what you need to do to add/deduct points when this hook fired.
		// You can name this method anything you want but it should be something
		// that reflects the instance. i.e. new_post or logging_in.
	}

	public function preferences() {
		// Our preferences are available under $this->prefs
		$prefs = $this->prefs; ?>

<!-- First we set the amount -->
<label class="subheader"><?php echo $this->core->plural(); ?></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->format_number( $prefs['creds'] ); ?>" size="8" /></div>
	</li>
</ol>
<!-- Then the log template -->
<label class="subheader"><?php _e( 'Log template', 'mycred' ); ?></label>
<ol>
	<li>
		<div class="h2"><input type="text" name="<?php echo $this->field_name( 'log' ); ?>" id="<?php echo $this->field_id( 'log' ); ?>" value="<?php echo $prefs['log']; ?>" class="long" /></div>
	</li>
</ol>
<?php
	}
}

Example 2: A hook with multiple instances.

class My_Custom_Hook extends myCRED_Hook {
	function __construct( $hook_prefs ) {
		// Construct
		parent::__construct( array(
			'id'       => 'custom',
			'defaults' => array(
				'instance_one' => array(
					'creds'    => 10,
					'log'      => '%plural% for first instance'
				),
				'instance_two' => array(
					'creds'    => 5,
					'log'      => '%plural% for second instance'
				)
			)
		), $hook_prefs );
	}

	public function run() {
		// Multiple instances needs to be checked as one of them might be disabled
		// leaving us with just one active instance.
		if ( $this->prefs['instance_one']['creds'] != 0 )
			add_action( 'one_hook_id', array( $this, 'hook_method_first' ) );
		if ( $this->prefs['instance_two']['creds'] != 0 )
			add_action( 'another_hook_id', array( $this, 'hook_method_second' ) );
	}

	public function hook_method_first() {
		// Do what you need to do to add/deduct points when this hook fired.
		// You can name this method anything you want but it should be something
		// that reflects the instance. i.e. new_post or logging_in.
	}

	public function hook_method_second() {
		// Do what you need to do to add/deduct points when this hook fired.
		// You can name this method anything you want but it should be something
		// that reflects the instance. i.e. new_post or logging_in.
	}

	public function preferences() {
		// Our preferences are available under $this->prefs
		$prefs = $this->prefs; ?>

<!-- First we set the amount -->
<label class="subheader"><?php echo $this->core->template_tags_general( '%plural% for Instance One' ); ?></label>
<ol>
	<li>
		<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'instance_one', 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'instance_one', 'creds' ) ); ?>" value="<?php echo $this->core->format_number( $prefs['instance_one']['creds'] ); ?>" size="8" /></div>
	</li>
</ol>
<!-- Then the log template -->
<label class="subheader"><?php _e( 'Log template', 'mycred' ); ?></label>
<ol>
	<li>
		<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'instance_one', 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'instance_one', 'log' ) ); ?>" value="<?php echo $prefs['instance_one']['log']; ?>" class="long" /></div>
	</li>
</ol>
<!-- First we set the amount -->
<label class="subheader"><?php echo $this->core->template_tags_general( '%plural% for Instance Two' ); ?></label>
<ol>
	<li>
		<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'instance_two', 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'instance_two', 'creds' ) ); ?>" value="<?php echo $this->core->format_number( $prefs['instance_two']['creds'] ); ?>" size="8" /></div>
	</li>
</ol>
<!-- Then the log template -->
<label class="subheader"><?php _e( 'Log template', 'mycred' ); ?></label>
<ol>
	<li>
		<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'instance_two', 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'instance_two', 'log' ) ); ?>" value="<?php echo $prefs['instance_two']['log']; ?>" class="long" /></div>
	</li>
</ol>
<?php
	}
}

Notice how we used an array as a value in field_name() and field_id() instead of a string.

Last edited March 5, 2016