myCRED_Hook

Package: mycred/core Category: Classes

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

Back To Top

myCRED_Lottery

Package: mycred/game Category: Classes

Description

The myCRED_Lottery class handles all lottery related information. This class does not save any information to your database, it only retrieves and processes lottery details in order for you to be able to present or retrieve.

This class requires a lottery post ID to work and only handles one lottery at a time.

 

 

Usage

$lottery = new myCRED_Lottery( $lottery_id );

 

Properties

  • $id (int)
    Lottery post ID.
  • $post (object)
    The lottery post object.
  • $prefs (array)
    This lotteries preferences.
  • $state (string)
    The lotteries current state.
  • $mycred (array)
    The myCRED_Settings Object.

 

Methods

  1. myCRED_Lottery()Constructs the lottery class and populates the lottery’s ID, preferences and loads the myCRED_Settings object.

  2. lottery_exists()Checks if a lottery exists and if the lottery is found, the lottery post object is populated. If this method is not called, the post object is not populated!

  3. default_prefs()This method holds the default settings for lotteries. It is mainly used internally though this class to populate preferences. It’s main job is to make sure there are no missing settings.

  4. get_prefs()Returns the current lotteries preferences.

  5. get_type()Returns the lottery type which is stored as a post meta under ‘mycred_lottery_type’.

  6. get_schedule_prefs()Returns the lottery schedule settings.

  7. get_game_prefs()Returns the lottery game settings.

  8. get_participation_prefs()Returns the lottery participation settings.

  9. get_winnings_prefs()Returns the lottery winnings settings.

  10. get_jackpot_prefs()Returns the lottery jackpot settings.

  11. get_exclude_prefs()Returns the lottery exclude settings.

  12. get_requirements_prefs()Returns the lottery requirements settings.

  13. get_templates_prefs()Returns the lottery templates.

  14. get_state()Returns the lotteries state which is stored as a post meta under ‘mycred_lottery_status’.

  15. get_title()Returns the lottery post title.

  16. get_content()Returns the lottery post content.

  17. can_play( $user_id, $entries, $repeat )Checks if a user can play this lottery. Returns a string value.

    • $user_id (int) required
      The user id to check.
    • $entries (int) required
      The number of entries this user would like to enter. Defaults to 1.
    • $repeat (int) required
      The number of repeats that the user would like play (if allowed). Defaults to 1.

    Returned string values:

    • play – User can play
    • visitor – User is not logged in
    • closed – Lottery is closed
    • excluded – User is excluded
    • req – User does not meet minimum requirements
    • max – User has reached heir maximum number of entries in this lottery
    • insolvent – User can not afford to play
  18. is_excluded( $user_id )Checks if a user is excluded from playing according to the “Exclude” preferences. Returns true or false.

    • $user_id (int)
      The user ID to check.
  19. fail_requirements( $user_id )Checks if a user fails to reach the minimum requirements for this lottery. Returns to true or false.

    • $user_id (int)
      The user ID to check.
  20. max_entry( $user_id )Checks if a user has reached the maximum entries allowed for this lottery according to the “Participation” settings.

    • $user_id (int)
      The user ID to check.
  21. is_insolvent( $user_id, $entries, $repeat )Checks if a user has enough points to play the number of entries and repeats that is requested. Returns true or false.

    • $user_id (int) required
      The user id to check.
    • $entries (int) required
      The number of entries this user would like to enter. Defaults to 1.
    • $repeat (int) required
      The number of repeats that the user would like play (if allowed). Defaults to 1.
  22. get_entries( $users )Returns either all lottery entries or all users with entries in this lottery. Returns an associative array.

    • $users (bool)
      If set to true, an array of user ids are returned else if it is set to false, an associative array of user ids and entries are returned.
  23. get_total_users_playing()Returns the number of users that are playing this lottery.

  24. get_total_entries()Returns the total number of entries in this lottery.

  25. get_users_entries( $user_id )Returns a given users entries.

    • $user_id (int)
      The user ID.
  26. count_users_entries( $user_id )Returns the number of entries a user has in this lottery.

    • $user_id (int)
      The user ID to check.
  27. get_ticket( $entry )Converts an entry string into an assosiative array containing the entries values.

    • $entry (string)
      The entry string.
  28. get_current_ticket_id()Returns the latest lottery ticket ID.

  29. get_ticket_play( $ticket_id, $play )Returns the users play in this lottery. If the lottery is a draw winner type of lottery, the lottery ID is shown while in pick number lotteries, the users played number are returned.

    • $ticket_id (int)
      The ticket ID.
    • $play (string)
      The users play.
  30. get_ticket_run( $repeat )Returns the number of days / weeks a play will run based on the lottery schedule.

    • $repeat (int)
      The number a lottery entry is repeated.
  31. get_current_jackpot()Returns the current jackpot (if used).

  32. increment_current_jackpot()Returns the jackpot amount based on the current jackpot incremented according to the “Jackpot” settings.

 

Lottery Parameters

  • id (int)
    Required Lottery Post ID.

 

Usage Examples

Example 1: Get the lottery post object and echo the publishing date.

$lottery_id = 1;
$lottery = new myCRED_Lottery( $lottery_id );
if ( $lottery->lottery_exists() ) {
	echo 'This lottery was published on: ' . $lottery->post->post_date;
}
else {
	echo 'Lottery not found.';
}

Example 2: Check if the current user can play the lottery and if they can not, show the appropriate template for why not.

$user_id = get_current_user_id();
$lottery_id = 1;
$lottery = new myCRED_Lottery( $lottery_id );
$can_play = $lottery->can_play( $user_id );
// can_play() will return the string 'play' if user can play
if ( $can_play == 'play' ) {
	// user can play
}
else {
	// user can not play so we get all templates
	$templates = $lottery->get_templates_prefs();
	echo $templates[ $can_play ];
}

Last edited June 21, 2016

Back To Top

myCRED_Module

Package: mycred/core Category: Classes

Description

The myCRED_Module is an abstract class, responsible for constructing and supporting each myCRED Module via the Module API. It contains a set of commonly used functions to help you built modules with ease.

As an abstract class, your module must extend this class which will allow you to use any of the provided functions or replace them with your own.

Usage

class My_Custom_Module extends myCRED_Module {
	function __construct( $point_type ) {
		parent::__construct( $module_id, $args, $point_type );
	}
}

Properties

  • $module_id (string)
    The unique module id. Important! Modules will fail and die if no module id is given!
  • $core (object)
    myCRED Settings.
  • $module_name (string)
    Required identifier for settings native to this module.
  • $option_id (string)
    Optional option ID this module uses.
  • $labels (array)
    Menu and Page Title labels. Only used if the module generates a sub page under the “myCRED” menu.
  • $register (boolean)
    Option to register module settings (if used). Defaults to false.
  • $screen_id (string)
    Screen ID for the module page (if used).
  • $menu_pos (int)
    Menu position.

Methods

  1. myCRED_Module( $module_id, $args )Constructs a module. If no module id is set this method will fail and die!
    • $module_id (string) required
      Constructs a module. If no module id is set this method will fail and die!.
    • $args (array)
      Module arguments.
  2. set_settings( $defaults )Populates the modules settings applying defaults if not set.
    • $defaults (array)
      Default settings.
  3. load()Hooks into myCRED. A module can overwrite this with it’s own load() method.
  4. module_pre_init()Runs before ‘init’.
  5. module_init()Runs at ‘init’.
  6. module_admin_init()Runs at ‘admin_init’.
  7. module_widgets_init()Runs at ‘widgets_init’.
  8. call( $call, $callback, $return )Either runs a given function or a class method. If $return is set, the value passed though it is passed on to the function/class method.
    • $call (string)
      Class method name to call. Ignored if function is called.
    • $callback (string)
      Function or Class to call.
    • $return (any)
      Optional value to pass on and return.
  9. add_menu()Adds a new sub menu item to the “myCRED” Admin Menu. Requires $labels and $screen_id to be set!
  10. register_settings()Registers a modules custom settings.
  11. settings_header()Outputs the “click to open” and “click to close” text to the accordion. Only used if the accordion is requested.
  12. admin_page()Outputs the admin page content. This method must be replaced by the custom module or an empty page is returned.
  13. sanitize_settings( $post )Used if the module has custom settings. This method must be replaced when used by the module and proper sanitation of the values must be made!
  14. after_general_settings()Option to insert custom content after the myCRED Core settings.
  15. sanitize_extra_settings( $new_data, $data, $core )If after_general_settings() is used, this method must be replaced by the custom module and sanitize the settings before they are saved into the DB. If not set, no settings are saved!
    • $new_data (array)
      The sanitized and correct data to save in the database.
    • $data (array)
      The initial posted data.
    • $core (object)
      myCRED Settings.

Module Arguments

// default
$args = array(
	'module_name' => '',
	'option_id'   => '',
	'defaults'    => array(),
	'labels'      => array(
		'menu'        => '',
		'page_title'  => ''
	),
	'register'    => true,
	'screen_id'   => '',
	'add_to_core' => false,
	'accordion'   => false,
	'menu_pos'    => 10
);
  • module_name (string)
    Module name used when modules add settings that can be accessed though myCRED_Settings.
  • option_id (string)
    Optional option id used by this module.
  • defaults (array)
    Default module settings.
  • labels (array)
    Array of labels when the module has it’s own admin page.
  • register (boolean)
    Option to register a modules settings though register_setting(). Should be set to true of option_id is set.
  • screen_id (string)
    A screen id if the module generates a custom admin page.
  • add_to_core (boolean)
    Option to add module settings to the myCRED Settings page. Defaults to false.
  • accordion (boolean)
    Option to include the Accordion script on the module page. Defaults to false.
  • menu_pos (int)
    Menu position.Default positions: Log (10), Hooks (20), Add-ons (30), Settings (99).

Examples

Example 1: A module which generates a new menu item between Log and Hooks called “Custom”.

class My_Custom_Module extends myCRED_Module {
	function __construct() {
		parent::__construct( 'My_Custom_Module', array(
			'module_name' => 'custom',
			'labels'      => array(
				'menu'        => __( 'Custom' ),      // Menu Label
				'page_title'  => __( 'Custom Page' )  // Page Title
			),
			'screen_id'   => 'myCRED_page_custom',
			'menu_pos'    => 15
		) );
	}

	function admin_page() {
		echo 'This is my custom page!';
	}
}

Example 2: A module which does not add a new page but instead adds new settings under myCred Settings.

class My_Custom_Module extends myCRED_Module {
	function __construct() {
		parent::__construct( 'My_Custom_Module', array(
			'module_name' => 'custom',
			'defaults'    => array(
				'foo'   => 1,
				'bar'   => ''
			),
			'register'    => false,
			'add_to_core' => true
		) );
	}

	// Output our settings or content
	function after_general_settings() {
		// We can access our custom settings under $this->$module_name
		$custom_settings = $this->custom;
		echo 'Custom settings output.';
	}

	// Sanitize our settings
	function sanitize_extra_settings( $new_data, $data, $core ) {
		// Our custom settings are found under $data[$module_name]
		$new_data['custom']['foo'] = sanitize_text_field( $data['custom']['foo'] );
		$new_data['custom']['bar'] = sanitize_text_field( $data['custom']['bar'] );
	}
}

Last edited March 5, 2016

Back To Top

myCRED_Payment_Gateway

Package: mycred/purchase Category: Classes

Navigation:

  1. Description
  2. Usage
  3. Properties
  4. Methods

Description

The myCRED_Payment_Gateway is a template class used by the Buy Creds Add-on. It contains all the neccessery class methods along with a set of template functions.

Note! You should never initiate this class directly! Instead make sure your custom hook extends it (see usage example bellow).

 

Usage

class My_Custom_Gateway extends myCRED_Payment_Gateway {
	function __construct( $gateway_prefs ) {
		parent::__construct( $args, $gateway_prefs );
	}
}

 

Properties

  • $id (string)
    Gateway ID.
  • $core (object)
    myCRED Settings.
  • $prefs (array)
    This gateways preferences.
  • $current_user_id (int)
    The current users id (if logged in).
  • $response (string)
    Responses from the gateway or via the execution of the gateway. It can for example hold information when an error occurs.
  • $request (array)
    The purchase request being sent to the gateway to process.
  • $status (int|string)
    Payment status.
  • $errors (array)
    Holds (if any) error objects.

 

Methods

  1. myCRED_Payment_Gateway()Class constructor populating $current_user_id, $args, $prefs and $core.

  2. process()This method must be replaced by the child class and should manage requests from either form submissions resulting in contacting a payment gateway or by incoming notifications/verifications from a gateway.

  3. buy()This method must be replaced by the child class and should manage requests made to purchase creds. All purchase requests must nominate the gateway to avoid multiple gateway attempting to process the same purchase.

  4. returning()This method must be replaced by the child class if a purchase request were processed remotely.

  5. preferences()Outputs the gateways preferences. If not set, the default “This Gateway has no settings” is shown.

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

    • $data (array)
      The posted data.
  7. field_name( $data )Outputs this gateways field name.

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

    • $data (string|array)
      Either a string with the field id or an array if hook has multiple settings.
  9. decode_log_entries( $entry, $object )This method decodes the log entry for purchases. When purchases are made, the buyers ID is saved under ref_id allowing us to use this users data as template tags.

    • $entry (type)
      The log entry.
    • $object (type)
      The log object.
  10. callback_url()Returns the callback URL for this gateway. Used by PayPal as the URL for the IPN callback. Can be used by any gateway that requires a callback URL.

  11. purchase_header( $title, $reload )Purchase page header.

  12. purchase_footer()The purchase page’s footer.

  13. form_with_redirect( $hidden_fields, $location, $logo_url, $custom_html, $sales_data )Generates a purchase form with the given hidden fields along with a gateway logo and optional custom html. Used by gateways with remote processors (i.e. PayPal or Skrill).

    • $hidden_fields (array)
      Required hidden form fields needed by the gateway.
    • $location (string)
      Form action URL.
    • $logo_url (string)
      Optional gateway logo URL.
    • $custom_html (string)
      Optional HTML to insert in page.
    • $sales_data (string|array)
      Optional sales data to include in page.
  14. get_to()Returns either the current user id or if gifting is enabled and used the id of the user this is gifted to.

  15. get_thankyou()Returns the “Thank You” page URL.

  16. get_cancelled()Returns the “Cancelled” page URL.

  17. get_entry( $_to, $_from )Returns the appropriate log entry template. If “Gifting” is enabled and the two values do not match the gift template is returned.

    • $_to (int)
      The user id of the recipient.
    • $_from (int)
      The user id of the purchaser.
  18. POST_to_data( $unset )Converts posted variables to local with the option to unset $_POST once done.

    • $unset (bool)
      Option to unser $_POST before returning the variables.
  19. transaction_id_is_unique( $transaction_id )Searches the log for a given transaction id. Returns false if id was found else true. Used to make sure purchases are unique.

    • $transaction_id (string|int) required
      The transaction id to look for.
  20. create_token()Returns a WP Nonce for the purchase. Uses wp_create_nonce().

  21. verify_token( $user_id, $nonce )Based on wp_verify_nonce() with the difference that a user id must be given. Returns true or false.

    • $user_id (type) required
      User id for the nonce in question.
    • $nonce (type) required
      The nonce.
  22. encode_sales_data( $data )Encodes the sales data. Uses the myCRED_Protect() class.

    • $data (string) required
      The data to be encoded.
  23. decode_sales_data( $data )Decodes the sales data. Uses the myCRED_Protect() class.

    • $data (string) required
      The data to be decoded.
  24. currencies_dropdown( $name )Generates a drop-down menu with accepted currencies. Uses the ‘mycred_dropdown_currencies’ filter.

    • $name (string) required
      The drop-down elements name.
  25. item_types_dropdown( $name )Generates a drop-down menu with available item types. Used by PayPal. Uses the ‘mycred_dropdown_item_types’ filter.

    • $name (string) required
      description.
  26. list_option_countries( $selected )Echos a list of html options for nations, highlighting the selected. Uses the ‘mycred_list_option_countries’ filter.

    • $selected (string)
      The selected countries two letter code.
  27. list_option_us_states( $selected, $non_us )Echos a list of html options for US states. If $non_us is set to true the “Outside US” option is appended.

    • $selected (string)
      The selected states two letter abbreviation.
    • $non_us (boolean)
      Option to add “Outside US”.
  28. list_option_months( $selected )Echos a list of html options for each month.

    • $selected (int) required
      The selected month number with leading zero.
  29. list_option_card_years( $selected, $number )Echos a list of html options for credit card expiry years.

    • $selected (type) required
      The selected years last two digits.
    • $number (type) required
      The number of years into the future to generate. Defaults to 16.

Last edited June 21, 2016

Back To Top

myCRED_Query_Log

Package: mycred/log Category: Classes

Navigation:

  1. Description
  2. Usage
  3. Properties
  4. Methods
  5. Query Parameters

Description

The myCRED_Query_Log class handles all log queries and optionally present the results by constructing a HTML table.

This class is used in the following myCRED features:

  • The mycred_history shortcode
  • The myCRED Log page in the admin area
  • BuddyPress Points History page
  • Users personal points history page in the admin area

Usage

Default usage:

// The Query
$query = new myCRED_Query_Log( $args );

// Show results
$query->display();

Without the use of $query->display();:

// The Query
$query = new myCRED_Query_Log( $args );

// The loop
if ( $query->have_entries() ) :
	foreach ( $query->result as $query ) {
		$log->the_entry( $query );
	}
else :
	$query->no_entries();
endif;

Properties

  • $args (array)
    Holds the arguments used in the query.
  • $request (string)
    The SQL query generated from the arguments given.
  • $prep (array)
    The argument values that is passed on to $wpdb->prepare();.
  • $result (object)
    The result of the query.
  • $num_rows (int)
    Number of rows in result.
  • $headers (array)
    The table column headers. Unsetting a column header before displaying the result will exclude that column from the generated table.

Example: Exclude the user column from the generated table.

$log = new myCRED_Query_Log( $args );
unset( $log->headers['column-username'] );
$log->display();

Methods

  1. myCRED_Query_Log( $args )Parses given arguments and makes a DB query using the wpdb class. Arguments can be passed either as a string or an array. This method will populate $args, $request, $prep, $result, $num_rows and $headers.
    • $args (string|array)
      Query arguments.
  2. have_entries()Returns true if the query returns results else false.
  3. table_headers()Returns the column headers.
  4. exporter()Renders the log exporter.
    • $title (string) optional
      Export title.
    • $is_profile (bool)
      Determines what label to show with the export options. Should be set to true if the current user viewing the log is viewing their own entries.
  5. display()Renders the log table.
  6. get_display()Returns the generated log table.
  7. the_entry( $entry, $wrap )Renders a entry row.
    • $entry (object) required
      The log entry object.
    • $wrap (string)
      Optional HTML element to wrap around each column. Defaults to TD. Added in 1.1.1
  8. get_the_entry( $entry, $wrap )Returns each columns value for one table row.
    • $entry (object) required
      The log entry object.
    • $wrap (string)
      Optional HTML element to wrap around each column. Defaults to TD. Added in 1.1.1
  9. no_entries()Renders the no entry row.
  10. get_no_entries()Returns the no entry language.
  11. search()Renders the Log Search field.
  12. filter_dates( $url )Outputs the filter by date links.
    • $url (string)
      The URL the users query is redirected to when clicking “Filter”. Should be the current page’s url.

Query Parameters

  • user_id (int)
    Show log entries for a specific user.

Example: Return the log entries of a specific user.

$log = new myCRED_Query_Log( 'user_id=1' );
  • ctype (string)
    Show log entries of a specific cred type. Defaults to mycred_default.
$log = new myCRED_Query_Log( 'ctype=mycred_default' );
  • number (int)
    Number of entires to return. Defaults to 25.

Example: Return the last 50 entries.

$log = new myCRED_Query_Log( 'number=50' );
  • time (string)
    Return log entries according to one of the Time Filters. Available options are:

    • today – show todays entries
    • yesterday – show yesterdays entries
    • thisweek – show this weeks entries
    • thismonth – show this months entries
    • from,to – Using Unix timestamps, entries logged between the two timestamps are returned. The two timestamps must be separated with a comma sign and nothing else.

Example 1: Return todays entries.

$log = new myCRED_Query_Log( 'time=today' );

Example 2: Return a specific users log entries from yesterday.

$log = new myCRED_Query_Log( 'user_id=1&time=yesterday' );
  • ref (string)
    Return log entries for a specific hook reference.

Example: Return all log entries for imports.

$log = new myCRED_Query_Log( 'ref=import' );
  • ref_id (int|string)
    Return entires with a specific reference ID.

Example 1: Return all Cred Transfers made to a specific user.

$log = new myCRED_Query_Log( 'ref=transfer&ref_id=1' );

Example 2: Return all Cred Transfers made from one user to another.

$log = new myCRED_Query_Log( 'user_id=1&ref=transfer&ref_id=2' );
  • amount (array|int|float)
    Return entries with a specific amount passed as a integer or float, or a range using an associative array. Required keys are: start (range start), end (range end) or if you want to make a comparison: num (the amount to compare to) and compare (=, LIKE, NOT LIKE etc.).

Example 1: Return all entries where 10 points were given.

$log = new myCRED_Query_Log( 'amount=10' );

Example 2: Return all entries where points were between 10 and 20.

$log = new myCRED_Query_Log( array( 'amount' => array( 'start' => 10, 'end' => 20 ) ) );

Example 3: Return all entires where anything but 20 points were given.

$log = new myCRED_Query_Log( array( 'amount' => array( 'num' => 20, 'compare' => '!=' ) );
  • s (string)
    Return results with this search string. If the user_id is not used here, only the ref, ref_id and entry columns are searched.

Example: Return all entires where the string foo is found.

$log = new myCRED_Query_Log( 's=foo' );
  • orderby (string)
    Oder the results by a given column. Available columns are: id, blog_id, ref, ref_id, user_id, creds, ctype, entry, data and time. Defaults to time.

Example: Return results and order by id.

$log = new myCRED_Query_Log( 'orderby=id' );
  • order (string)
    Choice of ordering the result either Ascending (ASC) or Descending (DESC). Defaults to DESC.

Example: Return results for a given user in ascending onder.

$log = new myCRED_Query_Log( 'user_id=1&order=ASC' );
  • ids (boolean)
    Choice of only returning the entry ids only (true) or all columns (false). Defaults to all columns (false).

Example: Return only the entry ids for a specific user.

$log = new myCRED_Query_Log( 'user_id=1&ids=true' );

Last edited March 5, 2016

Back To Top

myCRED_Remote

Package: mycred/api Category: Classes

Description

This is the main myCRED API class allowing your site to award / deduct points based on requests from other websites. Just like everything else in myCRED, this class is pluggable and can be replaced / altered. There are also several actions and filters associated with this class allowing further customizations / expansion.

Requires myCRED 1.3 or higher.

 

Supported Actions

  • CREDIT
    Add points to a specific user.
  • DEBIT
    Deduct points from a specific user.
  • GET
    Get the current balance of a specific user.
  • PAY
    Deduct points from one user and credit the amount deducted to another user.

 

Supported Methods

  • JSON
  • _GET
  • _POST

If you are looking to create call scripts to interact with this class, please consider using the wp_remote_get or wp_remote_post WordPress functions.

 

Properties

  • $method (string)
    The method used for the incoming API call.
  • $uri (array)
    The $_SERVER[‘REQUEST_URI’] value in an array format.
  • $request (array)
    The API call request array.
  • $host (string)
    The remote callers IP address.
  • $format (string)
    The format of the API call.
  • $user (object)
    The requested user object.
  • $recipient (object)
    The requested recipient user object. Only used for transfers.
  • $reply (string)
    The reply sent back once the call has been processed. Defaults to ‘PROCESSING’. See the error list for a complete list of possible replies.
  • $core (object)
    The myCRED_Settings object.
  • $key (string)
    The API Key.

 

Methods

  1. myCRED_Remote( $key )Handles magic quotes, populates $method, $uri, $format, $host and $core. Requires the API key.

    Action: mycred_remote (ref array)

  2. handle_magic()Handles magic quotes for $_GET, $_POST, $_COOKIE and $_REQUEST.

    Action: mycred_remote_magic(ref array)

  3. set_headers()Sets the API reply headers.

    Action: mycred_remote_headers (ref array)

  4. parse_call()Parses the API call to determine how the API call is made. Supports JSON or HTML. Populates $request and $format.

    Action: mycred_remote_parse (ref array)

  5. get_host_IP()Retreaves the IP address of the caller.

    Action: mycred_remote_host_IP (ref array)

  6. validate_call()Validates the API call. This method either returns an error message or true if the call passes validation.

    Action: mycred_remote_validate (ref array)
    Returns: True (bool) or an error code (string).

  7. process()The main API processor. Processes the API request and returns a string reply. Possible values:

    Filter: mycred_transfer_acc_limit
    Action: mycred_remote_process (ref array)
    Action: mycred_remote_process_{$action} (ref array)
    Returns: The process result (string).

    • COMPLETEDThe request has been successfully completed.
    • FAILEDThe request failed.
    • DUPLICATEThe request is deemed to be a duplicate and was rejected.
    • DECLINEDThe request was declined due to insufficient funds.
    • UNKNOWNUnknown / unsupported request.

 

Error Messages

Important! The following error messages is only available if you have enabled “Debug Mode”. It is very important to disable “Debug Mode” once you are done testing / developing to prevent mischievous calls from learning about your setup!

  1. 0 – 100 Plugin related issues.

    • ERROR: 99 The API Key has not been set or is empty.
  2. 100 – 199 API Call related issues.

    • ERROR: 101 The request is empty.
    • ERROR: 102 The required “action” key is missing or empty in the request.
    • ERROR: 103 The required “account” key is missing or empty in the request.
    • ERROR: 104 The required “token” key is missing or empty in the request.
    • ERROR: 105 The required “host” key is missing or empty in the request.
  3. 200 – 299 User related issues.

    • ERROR: 201 The user count not be found. Remember that the API only supports users identified via an email address.
    • ERROR: 202 The user is excluded from using myCRED.
  4. 300 – 399 Request specific issues regardless of what action is requested.

    • ERROR: 301 Invalid security token.
    • ERROR: 302 Unknown / Unsupported Action request.
  5. 400 – 499 CREDIT & DEBIT related issues.

    • ERROR: 401 The required “ref” key is missing or empty in the request.
    • ERROR: 402 The required “entry” key is missing or empty in the request.
    • ERROR: 403 The required “amount” key is missing or empty in the request.
  6. 500 – 599 GET related issues.

    • none
  7. 600 – 699 PAY related issues.

    • ERROR: 501 The required “ref” key is missing or empty in the request.
    • ERROR: 502 The required “entry” key is missing or empty in the request.
    • ERROR: 503 The required “amount” key is missing or empty in the request.
    • ERROR: 504 The required “to” key is missing or empty in the request.
    • ERROR: 505 The required “data” key is missing or empty in the request.
    • ERROR: 506 The recipient user could not be found.
    • ERROR: 507 The recipient user ID is the same as the sender.
    • ERROR: 508 The recipient user has been excluded from using myCRED.

Last edited June 21, 2016

Back To Top

myCRED_Settings

Package: mycred/core Category: Classes

Navigation:

  1. Description
  2. Usage
  3. Properties
  4. Methods
  5. Related Functions

Description

The myCRED_Settings class is the core myCRED class used by all features.

Besides containing your point type settings, it also contains a large set of commonly used functions such as get a users balance, check if a user is excluded from using a particular point type or awarding / deducting points.

While you can initialize the class directly, you should use the mycred function to access this class.

 

Note that the mycred function was introduced in version 1.4. Older versions must use the mycred_get_settings function which works the same with but without support for multiple point types.

Usage

$mycred = mycred();
$user_id = 1;

// Echo the singular name
echo $mycred->name['singular'];

// Check if a given user can edit this plugin
if ( $mycred->can_edit_plugin( $user_id ) ) {
	// do something if true
}

// Check if user is "excluded".
if ( $mycred->exclude_user( $user_id ) ) {
	// do something if true
}

// Get a users balance
$balance = $mycred->get_users_balance( $user_id );

// Get a users balance for a different point type then what was used when initializing this class
$balance = $mycred->get_users_balance( $user_id, 'mytype' );

Properties

  • $core (array)
    Associative array of “General Settings” saved under mycred_pref_core.
    Note! On Multisite installations, if the “Master Template” feature is enabled, the main site’s (blog id 1) settings are retrieved.

Note! When this class is initiated, the constructor will automatically add the arrays first level keys and it’s values as a property allowing you to access these keys directly.

Example: Two ways to access the singular name, both returning the same value.

$mycred = mycred();
echo $mycred->core->name['singular'];
echo $mycred->name['singular'];

Methods

  1. myCRED_Settings()Grabs the plugin settings and populates the $core property.
  2. singular()Returns the singular name.
  3. plural()Returns the plural name.
  4. zero()Returns zero formatted according to your setup with or without decimals.
  5. number( $number )Returns the given value either as an integer or as a float.
    • $number (int|float) required
      The initial value to format.
  6. format_number( $number )Returns a given value formated according to setup. Mainly checks to see if decimals are used. Uses number_format().
    • $number (int|float) required
      The initial value to format.

    Filter: mycred_format_number

  7. format_creds( $number, $before, $after, $force_in )Returns a given value formatted with prefix and/or suffix. You can also insert a custom string before or after the formated value.
    • $number (int|float) required
      The value to format.
    • $before (string)
      Optional string to insert before the value.
    • $after (string)
      Optional string to isnert after the value.
    • $force_in (boolean)
      Option to force $before after prefix and $after before suffix.

    Filter: mycred_format_creds

  8. round_value( $amount, $up_down, $precision )Will round a given value either up or down with the option to use precision.
    • $amount (int|float) required
      The value to be rounded.
    • $up_down (string|boolean)
      Choice of rounding up or down. Using false bypasses this function.
    • $precision (int)
      The optional number of decimal digits to round to. Defaults to 0.
  9. apply_exchange_rate( $amount, $rate, $round )Devides the $amount value with $rate with the option to round the value using round().
    • $amount (int|float) required
      The initial value.
    • $rate (int|float) required
      Value to divide $amount with.
    • $round (bool)
      Option to round the value. Defaults to true.
  10. parse_template_tags( $content, $log_entry )Replaces template tags in a given string while using the $log_entry as reference. Used for each log entry row before displaying the result. The log entry object must be supplied for this method to work.
    • $content (string) required
      The string containing the template tags.
    • $log_entry (object) required
      The Log entry object

    Filter: mycred_parse_log_entry
    Filter: mycred_parse_log_entry_{$reference}
    Filter: mycred_parse_tags_{$type}

  11. template_tags_general( $text )Replaces General Template Tags: %singular%, %plural%, %login_url%, %logout_url% etc.
    • $text (string) required
      The string containing the template tags.

    Filter: mycred_parse_tags_general

  12. template_tags_amount( $text, $amount )Replaces Amount Template Tags: %cred% and %cred_f%.
    • $text (string) required
      The string containing the template tags.
    • $amount (int) required
      The amount to be formated.

    Filter: mycred_parse_tags_amount

  13. template_tags_post( $text, $ref_id, $data )Replaces Post Template Tags: %link_with_title%, %post_title%, %post_type% and %post_url%. Requires that the reference ID is a post id!
    • $text (string) required
      The string containing the template tags.
    • $ref_id (int) required
      The post id used as reference id.
    • $data (object)
      Optional extra data. When used to parse the log, the data columns content is passed as $data.

    Filter: mycred_parse_tags_post

  14. template_tags_user( $text, $ref_id, $data )Replaces User Template Tags: %display_name%, %user_id%, %user_name%, %user_name_ec%, %user_profile_link% and %user_profile_url%. Requires that the reference ID is a user id!
    • $text (string) required
      The string containing the template tags.
    • $ref_id (int|bool) required
      Either false, if the user object is passed along though $data else a user id.
    • $data (array|object)
      Optional extra data. Either the user object or an associative array of user details. Used when the user object is available and no new query is required. Changed in 1.2

    Filter: mycred_parse_tags_user

  15. template_tags_comment( $text, $ref_id, $data )Replaces Comment Template Tags: %comment_id%, %c_link_with_title%, %c_post_id%, %c_post_title% and %c_post_url%. Requires that the reference ID is a comment id!
    • $text (string) required
      The string containing the template tags.
    • $ref_id (int) required
      The comment id used as reference id.
    • $data (object)
      Optional extra data. When used to parse the log, the data columns content is passed as $data.

    Filter: mycred_parse_tags_comment

  16. has_tags()Checks if a string has any of the defined template tags. Used by template tag parsers.
  17. allowed_tags( $data, $allow )Strips HTML tags from a given string.
    • $data (string)
      String to strip.
    • $allow (string)
      Optional overwrite of the allowed HTML tags.

    Filter: mycred_allowed_tags

  18. edit_creds_cap()Returns the set edit creds capability.
  19. can_edit_creds( $user_id )Check if a given user or the current user can edit users creds. Uses edit_creds_cap().
    • $user_id (int)
      User id to check for. Defaults to the current user.
  20. edit_plugin_cap()Returns the set edit plugin capability.
  21. can_edit_plugin( $user_id )Check if a given user or the current user can edit the myCRED plugin. Uses edit_plugin_cap().
    • $user_id (int)
      User id to check for. Defaults to the current user.
  22. in_exclude_list( $user_id )Check if user id is in exclude list. Returns true or false.
    • $user_id (int)
      User id to check for. Defaults to the current user.
  23. exclude_plugin_editors()Check if plugin editors should be excluded. Returns true or false.
  24. exclude_creds_editors()Check if cred editors should be excluded. Returns true or false.
  25. exclude_user( $user_id )Checks is the given user id should be excluded.
    • $user_id (int)
      User id to check.
  26. get_cred_id()Returns the set cred id.
  27. get_users_balance( $user_id, $type )Returns the given users cred balance. Note that no formatting is done to the returned value. Returns either an integer or float.
    • $user_id (int) required
      User id.

    Filter: mycred_get_users_cred

  28. update_users_balance( $user_id, $amount )Returns the updated balance of the given user.
    • $user_id (int) required
      User id.
    • $amount (int|float)
      Amount to add/deduct from users balance. This value must be pre-formated.

    This method was renamed from update_users_cred in version 1.0.9.2

    Action: mycred_update_user_balance

  29. add_creds( $ref, $user_id, $cred, $entry, $ref_id, $data, $type )Adds creds to a given user. A refernece ID, user id and number of creds must be given. Important! This function will not check if the user should be excluded from gaining points, this must be done before calling this function!
    • $ref (string)
      Required reference id.
    • $user_id (int)
      Required User id.
    • $cred (int|float)
      Amount to be added/deducted.
    • $entry (string)
      Log entry.
    • $ref_id (int)
      Optional reference id.
    • $data (int|string|array|object)
      Optional extra data to be saved with the entry. Note that arrays get serialized before saved.
    • $type (string)
      Optional cred type.

    Filter: mycred_add

  30. update_rankings( $force, $type )Updates Rankings for a given post type.
    • $foce (bool)
      If rankings are updated on a set interval, this option can override and force a new setting to be saved. Defaults to false
    • $user_id (int) required
      User id.
  31. add_to_log( $ref, $user_id, $cred, $entry, $ref_id, $data, $type )Adds a new entry into the log. A reference id, user id and number of credits must be set.
    • $ref (string)
      Required reference id.
    • $user_id (int)
      Required User id.
    • $cred (int|float)
      Amount to be added/deducted.
    • $entry (string)
      Log entry.
    • $ref_id (int)
      Optional reference id.
    • $data (int|string|array|object)
      Optional extra data to be saved with the entry. Note that arrays get serialized before saved.
    • $type (string)
      Optional cred type.
  32. has_entry( $reference, $ref_id, $user_id, $data )Moved from the myCRED_Hook abstract class in 1.2. Checks for a log entry with the given variables. Returns true or false.
    • $reference (string)
      Reference to check.
    • $ref_id (int)
      Reference ID to check. Reference IDs are always integers.
    • $user_id (int)
      User ID check.
    • $data (string)
      Data to check.

Related Functions

Last edited March 5, 2016

Back To Top

Last edited June 5, 2016