Classes
Description
This is an abstract class used to construct myCRED Hooks. It contains a set of predefined methods to help hook constructions.
Class Properties
Property | Declared | Type | Description |
---|---|---|---|
$id | Public | string | The hook ID. Must be unique. |
$core | Public | OBJECT | The main point type object. |
$point_types | Public | ARRAY | All registered point type IDs. |
$is_main_type | Public | BOOL | True if the current instance is the main point type (first instance) or false if not. |
$mycred_type | Public | BOOL | The current point type instance. |
$prefs | Public | ARRAY | Associative array of the hooks saved settings. |
Interacting with the Class
As an abstract class, your use this by extending your own hook class.
class myCRED_Custom_Hook extends myCRED_Hook {
function __construct( $hook_prefs, $type = MYCRED_DEFAULT_TYPE_KEY ) {
parent::__construct( array(
'id' => 'hookid',
'defaults' => array(
'creds' => 10,
'log' => '%plural% for a specific event'
)
), $hook_prefs, $type );
}
}
Parameter | Type | Required | Description |
---|---|---|---|
$args | ARRAY | Yes | The hook arguments containing the hooks ID and preferences. |
$hook_prefs | ARRAY | Yes | The module settings array. |
$type | string | Yes | The point type key instance. |
Class Methods
When using this class, the only requirement is that your class must define and setup it’s own run() method, all other methods are optional and not required to use.
public function run() { | ||||
Description: | If a hook is enabled, this method is run for each point type instance. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
No variables | ||||
} |
public function preferences() { | ||||
Description: | Method that displays the hooks settings in the admin area. If this method is not defined in your class, the hook will appear to have no settings when viewed in the admin area. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
No variables | ||||
} |
public function sanitise_preferences( $data ) { | ||||
Description: | Method used to sanitize or setup hook settings after they have been saved by the user in the admin area. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
data | ARRAY | Yes | The hooks newly submitted settings. | |
} |
public function field_name( $field = '' ) { | ||||
Description: | Returns the hook settings field name. Should be used when displaying settings in the admin area. Ensures that field names are always correct. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
field | STRING or ARRAY | Yes | The field name. | |
} |
public function field_id( $field = '' ) { | ||||
Description: | Returns the hook settings field id. Should be used when displaying settings in the admin area. Ensures that field ids are always correct. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
field | STRING or ARRAY | Yes | The field id. | |
} |
public function over_hook_limit( $instance = '', $reference = '', $user_id = NULL, $ref_id = NULL ) { | ||||
Description: | Returns true if the hook limit has been reached (assuming one has been set), else it returns false. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
instance | STRING | Yes | The hook instance to check. | |
reference | STRING | Yes | The reference to check. | |
user_id | INT | Yes | The user ID to check. | |
ref_id | INT | No | Optional reference ID to check. | |
} |
public function get_limit_types() { | ||||
Description: | Returns an array of options that can be selected in the hook limit settings dropdown. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
No variables | ||||
} |
public function hook_limit_setting( $name = '', $id = '', $selected = '' ) { | ||||
Description: | Returns the hook limit settings. This method can only be used in the preferences() method for instances that needs to support limits. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
name | STRING | Yes | The settings name to use. | |
id | STRING | Yes | The settings id to use. | |
selected | STRING | Yes | The saved settings. | |
} |
public function has_entry( $reference = NULL, $ref_id = NULL, $user_id = NULL, $data = NULL, $type = NULL ) { | ||||
Description: | Checks if a specific log entry exists in the database. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
reference | STRING | No | The reference to check. | |
ref_id | INT | No | The reference id to check. | |
user_id | INT | No | The user id to check. | |
data | STRING | No | The data to check for. If you know the data is an array, make sure this value is a serialized array. | |
type | STRING | No | The point type to check. | |
} |
Navigation:
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
myCRED_Lottery()Constructs the lottery class and populates the lottery’s ID, preferences and loads the myCRED_Settings object.
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!
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.
get_prefs()Returns the current lotteries preferences.
get_type()Returns the lottery type which is stored as a post meta under ‘mycred_lottery_type’.
get_schedule_prefs()Returns the lottery schedule settings.
get_game_prefs()Returns the lottery game settings.
get_participation_prefs()Returns the lottery participation settings.
get_winnings_prefs()Returns the lottery winnings settings.
get_jackpot_prefs()Returns the lottery jackpot settings.
get_exclude_prefs()Returns the lottery exclude settings.
get_requirements_prefs()Returns the lottery requirements settings.
get_templates_prefs()Returns the lottery templates.
get_state()Returns the lotteries state which is stored as a post meta under ‘mycred_lottery_status’.
get_title()Returns the lottery post title.
get_content()Returns the lottery post content.
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
- $user_id (int) required
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.
- $user_id (int)
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.
- $user_id (int)
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.
- $user_id (int)
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.
- $user_id (int) required
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.
- $users (bool)
get_total_users_playing()Returns the number of users that are playing this lottery.
get_total_entries()Returns the total number of entries in this lottery.
get_users_entries( $user_id )Returns a given users entries.
- $user_id (int)
The user ID.
- $user_id (int)
count_users_entries( $user_id )Returns the number of entries a user has in this lottery.
- $user_id (int)
The user ID to check.
- $user_id (int)
get_ticket( $entry )Converts an entry string into an assosiative array containing the entries values.
- $entry (string)
The entry string.
- $entry (string)
get_current_ticket_id()Returns the latest lottery ticket ID.
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.
- $ticket_id (int)
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.
- $repeat (int)
get_current_jackpot()Returns the current jackpot (if used).
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 ];
}
myCRED_Module
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.
Navigation
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
- 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.
- $module_id (string) required
- set_settings( $defaults )Populates the modules settings applying defaults if not set.
- $defaults (array)
Default settings.
- $defaults (array)
- load()Hooks into myCRED. A module can overwrite this with it’s own load() method.
- module_pre_init()Runs before ‘init’.
- module_init()Runs at ‘init’.
- module_admin_init()Runs at ‘admin_init’.
- module_widgets_init()Runs at ‘widgets_init’.
- 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.
- $call (string)
- add_menu()Adds a new sub menu item to the “myCRED” Admin Menu. Requires $labels and $screen_id to be set!
- register_settings()Registers a modules custom settings.
- settings_header()Outputs the “click to open” and “click to close” text to the accordion. Only used if the accordion is requested.
- admin_page()Outputs the admin page content. This method must be replaced by the custom module or an empty page is returned.
- 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!
- after_general_settings()Option to insert custom content after the myCRED Core settings.
- 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.
- $new_data (array)
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'] );
}
}
Navigation:
- Description
- Usage
- Properties
- Methods
Description
The myCRED_Payment_Gateway is a template class used by the BuyCred 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
- myCRED_Payment_Gateway()Class constructor populating $current_user_id, $args, $prefs and $core.
- 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.
- 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.
- returning()This method must be replaced by the child class if a purchase request were processed remotely.
- preferences()Outputs the gateways preferences. If not set, the default “This Gateway has no settings” is shown.
- sanitise_preferences( $data )Option to sanitize the gateways settings before they are saved in the database.
- $data (array)
The posted data.
- $data (array)
- 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.
- $data (string|array)
- 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.
- $data (string|array)
- 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.
- $entry (type)
- 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.
- purchase_header( $title, $reload )Purchase page header.
- purchase_footer()The purchase page’s footer.
- 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.
- $hidden_fields (array)
- get_to()Returns either the current user id or if gifting is enabled and used the id of the user this is gifted to.
- get_thankyou()Returns the “Thank You” page URL.
- get_cancelled()Returns the “Cancelled” page URL.
- 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.
- $_to (int)
- 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.
- $unset (bool)
- 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.
- $transaction_id (string|int) required
- create_token()Returns a WP Nonce for the purchase. Uses wp_create_nonce().
- 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.
- $user_id (type) required
- encode_sales_data( $data )Encodes the sales data. Uses the myCRED_Protect() class.
- $data (string) required
The data to be encoded.
- $data (string) required
- decode_sales_data( $data )Decodes the sales data. Uses the myCRED_Protect() class.
- $data (string) required
The data to be decoded.
- $data (string) required
- 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.
- $name (string) required
- 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.
- $name (string) required
- 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.
- $selected (string)
- 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”.
- $selected (string)
- list_option_months( $selected )Echos a list of html options for each month.
- $selected (int) required
The selected month number with leading zero.
- $selected (int) required
- 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
- $selected (type) required
Description
This class allows you to search the myCRED Log for entries based on a large variety of options. Similar to the WP_Query class, you can use this class to query and then loop through the results in order to show the results in any way you like. Currently the class also has a large set of functions to help you render a table where these results are presented. This feature is currently used by the Log module in your admin area and by the mycred_history shortcode / widget.
Interacting with the Class
While the class offers a large number of functions for you to use, your most common interaction will be with constructing and providing arguments for your search.
// Example 1: Show the last 10 entries
$args = array(
'number' => 10
);
// The Query
$log = new myCRED_Query_Log( $args );
// The Loop
if ( $log->have_entries() ) {
// Display using the built-in table
$log->display();
}
// Example 2: Get log entries for user 1
$args = array(
'user_id' => 1
);
// The Query
$log = new myCRED_Query_Log( $args );
// The Loop
if ( $log->have_entries() ) {
// Build your custom loop
foreach ( $log->results as $entry ) {
// Present the results anyway you like
}
}
Class Properties
Property | Declared | Type | Description |
---|---|---|---|
$now | Public | INT | The current unix timestamp. Used for date / time rendering and parsing. |
$render_mode | Public | BOOL | Indicates if the class allows results to be rendered using the built-in methods or if only queries can be made. Added in 1.7.5 |
$args | Public | ARRAY | Holds the arguments that the class was constructed around. |
$request | Public | STRING | The SQL query that was run to get the results you are seeing. |
$num_rows | Public | INT | The number of rows the query returned. |
$max_num_pages | Public | STRING | The maximum number of pages the results span over. Defaults to 1. |
$total_rows | Public | INT | The total number of rows in the database. |
$results | Public | ARRAY | The results of your query. |
$headers | Public | ARRAY | Holds an associative array of headers that the table will render. |
$hidden_headers | Public | ARRAY | Holds an array of header ids that you have selected to hide. |
$core | Public | OBJECT | The myCRED_Settings class object. |
$is_admin | Public | BOOL | Indicates if the class is used in the front end or in the wp-admin area. |
$references | Public | ARRAY | An array of reference IDs and their labels. This is used to translate each log entries reference when displayed in the table. |
$refs | Public | ARRAY | An array containing all references that exists in the database, based on the current query. Added in 1.7.5 |
$types | Public | ARRAY | Populated with the myCRED_Settings object for each point type we are searching for. This is used when you are rendering a table for the query results with multiple point types. Added in 1.7.5 |
Class Parameters
The class has only two parameters that you can use.
Parameter | Type | Required | Description |
---|---|---|---|
$args | ARRAY | Yes | The query arguments to return results for. |
$array | BOOL | No | Option to return entries in an array format instead of object format. Can not be used when the fields argument is set to return a specific column. |
Query Arguments
Arguments can be passed in string or array format. When using a string format, you separate each argument with an & character. String format is well suited in simple straightforward queries, however for more advanced queries, an array format is required.
Entry IDs
Argument | Type | Description |
---|---|---|
entry_id | ARRAY or INT | Retrieve log entries based on their unique ids. Supports queries of a single id or multiple ids. Supports = , != , IN and NOT IN comparisons. |
Example 1: Get a single entry that has the id 1
$log = new myCRED_Query_Log( 'entry_id=1' );
Example 2: Get multiple entries
$log = new myCRED_Query_Log( array( 'entry_id' => array( 1, 2, 3 ) );
Example 3: Get entries that does not have the ids 1 or 2
$args = array(
'entry_id' => array(
'ids' => array( 1, 2 ),
'compare' => 'NOT IN'
)
);
$log = new myCRED_Query_Log( $args );
Example 4: Get entries that does not have the id 5
$args = array(
'entry_id' => array(
'ids' => 5,
'compare' => '!='
)
);
$log = new myCRED_Query_Log( $args );
Point Types
Argument | Type | Description |
---|---|---|
ctype | ARRAY or STRING | Retrieve log entries of a particular point type or multiple types based on the point type key. Supports = , != , IN and NOT IN comparisons. |
Example 1: Get entries of the default point type
$log = new myCRED_Query_Log( 'ctype=mycred_default' );
Example 2: Get entries of a custom point type
$log = new myCRED_Query_Log( array( 'ctype' => 'mycustom_type' ) );
Example 3: Get entries that are not of a custom point type
$args = array(
'ctype' => array(
'ids' => array( 'mycustom_type', 'anothertype' ),
'compare' => 'NOT IN'
)
);
$log = new myCRED_Query_Log( $args );
Example 4: Get entries of all point types
$log = new myCRED_Query_Log( array( 'ctype' => '' ) );
User IDs
Argument | Type | Description |
---|---|---|
user_id | ARRAY or INT | Retrieve log entries one or multiple users based on their numeric IDs. Supports = , != , IN and NOT IN comparisons. |
Example 1: Get entries for the user with the ID 1
$log = new myCRED_Query_Log( 'user_id=1' );
Example 2: Get entries for a set of users
$log = new myCRED_Query_Log( array( 'user_id' => array( 1, 2, 4 ) ) );
Example 3: Get entries that do not belong to users 1, 2 and 6
$args = array(
'user_id' => array(
'ids' => array( 1, 2, 6 ),
'compare' => 'NOT IN'
)
);
$log = new myCRED_Query_Log( $args );
References
Argument | Type | Description |
---|---|---|
ref | ARRAY or STRING | Retrieve log entries based on one or multiple references. Supports = , != , IN and NOT IN comparisons. |
Example 1: Get all manual entries
$log = new myCRED_Query_Log( 'ref=manual' );
Example 2: Get entries for approved comments and published content
$log = new myCRED_Query_Log( array( 'ref' => array( 'approved_comment', 'published_content' ) ) );
Example 3: Get all entries that are not manual
$args = array(
'ref' => array(
'ids' => 'manual',
'compare' => '!='
)
);
$log = new myCRED_Query_Log( $args );
Example 4: Get all entries of a custom reference
$args = array(
'ref' => array(
'ids' => 'mycustomreference',
'compare' => '='
)
);
$log = new myCRED_Query_Log( $args );
Reference IDs
Argument | Type | Description |
---|---|---|
ref_id | ARRAY or INT | Retrieve log entries based on one or multiple reference IDs. Supports = , != , < , <= , >= , IN and NOT IN comparisons. |
Example 1: Get all entries with the reference id 1
$log = new myCRED_Query_Log( 'ref_id=1' );
Example 2: Get entries with a reference id of 1, 2 or 3
$log = new myCRED_Query_Log( array( 'ref_id' => array( 1, 2, 3 ) ) );
Example 3: Get all entries with a reference id lower than 100
$args = array(
'ref_id' => array(
'ids' => 100,
'compare' => '<'
)
);
$log = new myCRED_Query_Log( $args );
Example 4: Get all entries that have a reference id higher or equal to 10, no matter which point type the entry belongs to
$args = array(
'ref_id' => array(
'ids' => 10,
'compare' => '>='
),
'ctype' => ''
);
$log = new myCRED_Query_Log( $args );
Point Amounts
Argument | Type | Description |
---|---|---|
amount | ARRAY or INT or FLOAT | Retrieve log entries based on one or multiple amounts. Supports = , != , < , <= , >= , BETWEEN , NOT BETWEEN , IN and NOT IN comparisons. |
Example 1: Get all entries were users gained 1000 points
$log = new myCRED_Query_Log( 'amount=1000' );
Example 2: Get entries where a user gained 10, 20 or 50 points
$log = new myCRED_Query_Log( array( 'amount' => array( 10, 20, 50 ) ) );
Example 3: Get all entries where a user has gained between 0 and 100 points
$args = array(
'amount' => array(
'num' => array( 0, 100 ),
'compare' => 'BETWEEN'
)
);
$log = new myCRED_Query_Log( $args );
Example 4: Get all entries where a user lost points, no matter which point type
$args = array(
'amount' => array(
'num' => 0,
'compare' => '<'
),
'ctype' => ''
);
$log = new myCRED_Query_Log( $args );
Time
Argument | Type | Description |
---|---|---|
time | ARRAY or INT or STRING | Retrieve log entries for a given time period or date/time. You can supply a well formatted date/time string, a unix timestamp or a keyword. Supported keywords: today , yesterday , thisweek and thismonth . Supports = , != , < , <= , >= , BETWEEN , NOT BETWEEN , IN and NOT IN comparisons. |
Example 1: Get todays entries
$log = new myCRED_Query_Log( 'time=today' );
Example 2: Get all entries for a specific date
$log = new myCRED_Query_Log( array( 'time' => '2016-10-01' ) );
Example 3: Get all entries between two dates
$args = array(
'time' => array(
'dates' => array( '2016-01-01 00:00:01', '2016-12-31 23:59:59' ),
'compare' => 'BETWEEN'
)
);
$log = new myCRED_Query_Log( $args );
Example 4: Get all entries before the start of today
$args = array(
'time' => array(
'dates' => 'today midnight',
'compare' => '<'
)
);
$log = new myCRED_Query_Log( $args );
Searching
Argument | Type | Description |
---|---|---|
s | INT or STRING | Search the “entry” columns and retrieve entries that match. You can use wildcards % for LIKE comparisons. |
data | INT or STRING | Search the “data” columns and retrieve entries that match. You can use wildcards % for LIKE comparisons. |
Example 1: Get entries that has the string “boo“ in their description
$log = new myCRED_Query_Log( 's=boo' );
Example 2: Get entries where the “entry“ columns content starts with “Tokens“
$log = new myCRED_Query_Log( array( 's' => 'Tokens%' ) );
Example 3: Get entries that has the string “boo“ in their data column
$log = new myCRED_Query_Log( 'data=boo' );
Example 4: Get entries where the “data“ columns content a serialized array
$log = new myCRED_Query_Log( array( 'data' => 'a:1:{s:8:"ref_type";s:4:"user";}' ) );
Sorting of Results
Argument | Type | Description |
---|---|---|
orderby | ARRAY or STRING | Sort the retrieved log entries. As of version 1.7.5 you can sort by multiple columns by providing an array of columns and their order. |
order | STRING | Either ASC for ascending or DESC for descending. Ignored if orderby is an array. |
Example 1: Get entries for the user 1 ordered by time in an ascending order (latest entry is shown last)
$args = array(
'user_id' => 1,
'orderby' => 'time',
'order' => 'ASC'
);
$log = new myCRED_Query_Log( $args );
Example 2: Get “Manual“ entries sorted by the point amount (largest first), then by time
$args = array(
'ref' => 'manual',
'orderby' => array( 'creds' => 'DESC', 'time' => 'DESC' )
);
$log = new myCRED_Query_Log( $args );
Return Fields
Argument | Type | Description |
---|---|---|
fields | ARRAY or STRING | Either the name of a particular column or an array of column names you want returned in your results. See the log structure documentation for accepted values. |
ids | BOOL | Option to only return the log entries unique ID. Depreciated since 1.7.5 and will be removed in future versions in favour for fields . |
Example 1: Get the log entry ids only for a specific user
$args = array(
'user_id' => 1,
'fields' => 'id'
);
$log = new myCRED_Query_Log( $args );
Example 2: Get the log entry ids and the point amounts only for a specific user
$args = array(
'user_id' => 5,
'fields' => array( 'id', 'creds' )
);
$log = new myCRED_Query_Log( $args );
Pagination
Argument | Type | Description |
---|---|---|
number | INT | The number of entries to return. By default this is 25. Use -1 to return all entries. If you are rendering the results in the class table, this will be the number of entries shown per page. |
paged | INT | The page number. Retrieve entries that would show on page x when using the navigation. Defaults to 1 as in the first page. |
offset | INT | Number of entries to displace or pass over. Should be used in combination with number . Defaults to zero. |
Example 1: Show 15 entries per page
$args = array(
'user_id' => 1,
'number' => 15
);
$log = new myCRED_Query_Log( $args );
Example 2: Get all manual log entries
$args = array(
'ref' => 'manual',
'number' => -1
);
$log = new myCRED_Query_Log( $args );
Query Filters
Your query will trigger the following filters whenever the class is constructed:
Filter | Description |
---|---|
mycred_query_log_args | Allows you to manipulate the query arguments before it is processed by the class. |
mycred_allowed_sortby | Controls what values the query can be sorted by. By default this is the log column headers. |
myCRED_Remote
Navigation:
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
myCRED_Remote( $key )Handles magic quotes, populates $method, $uri, $format, $host and $core. Requires the API key.
Action: mycred_remote (ref array)
handle_magic()Handles magic quotes for $_GET, $_POST, $_COOKIE and $_REQUEST.
Action: mycred_remote_magic(ref array)
set_headers()Sets the API reply headers.
Action: mycred_remote_headers (ref array)
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)
get_host_IP()Retreaves the IP address of the caller.
Action: mycred_remote_host_IP (ref array)
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).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!
0 – 100 Plugin related issues.
- ERROR: 99 The API Key has not been set or is empty.
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.
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.
300 – 399 Request specific issues regardless of what action is requested.
- ERROR: 301 Invalid security token.
- ERROR: 302 Unknown / Unsupported Action request.
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.
500 – 599 GET related issues.
- none
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.
Description
This class constructs and enables a set of core tools that allows you to execute point related actions or retrieve points related details.
Interacting with the Class
While the class can be called directly, it is highly recommended that you instead use the mycred function.
// Default usage
$mycred = mycred();
// Setup for a custom point type
$mycred = mycred( 'customtypekey' );
Class Properties
Property | Declared | Type | Description |
---|---|---|---|
$core | Public | array | The settings array for the current point type. |
$log_table | Public | string | The database table name based on your Network settings (if you use Multisite). |
$cred_id | Public | string | The meta key ID of the current point type. |
$is_multisite | Public | BOOL | The value of the is_multisite function. |
$use_master_template | Public | BOOL | True if the master template feature is enabled. False if not using multisite. |
$use_central_logging | Public | BOOL | True if the central logging is enabled. False if not using multisite. |
Class Parameters
The class has only one parameter that can be used.
Parameter | Type | Required | Description |
---|---|---|---|
$typekey | STRING | No | The point type key to construct. |
Class Methods
While you can interact with balances without using this class, the methods offered here will make sure your interaction happens faster and safer. Using these methods also ensures that you always have access to all point related features in myCRED.
public function defaults() { | ||||
Description: | Returns the default settings array which is used when compiling the point type’s settings. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
No variables | ||||
} |
public function singular() { | ||||
Description: | Returns the current point types name in singular form. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
No variables | ||||
} |
Example 1: Display the point types given name in singular form
$mycred = mycred();
echo $mycred->singular();
public function plural() { | ||||
Description: | Returns the current point types name in plural form. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
No variables | ||||
} |
Example 1: Display the point types given name in plural form
$mycred = mycred();
echo $mycred->plural();
public function zero() { | ||||
Description: | Returns zero formatted according to the point type setup. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
No variables | ||||
} |
public function number( $number = '' ) { | ||||
Description: | Returns the given value formatted according to the point types setup. Should be used when points are used in calculations. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
number | INT or FLOAT | Yes | The value to format. | |
} |
public function format_number( $number = '' ) { | ||||
Description: | Returns a given value formatted based on the point types settings as a string. Should be used when points are intended to be displayed and not used in calculations. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
number | INT or FLOAT | Yes | The value to format. | |
} |
public function format_creds( $creds = 0, $before = '', $after = '', $force_in = false ) { | ||||
Description: | Returns a given value formatted based on the point types settings with prefix / suffix added (if used). Can not be used in calculations! | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
creds | INT or FLOAT | Yes | The value to format. | |
before | STRING | No | Optional string to attach before the formatted point value. If you use a prefix, this string is appended before the prefix. | |
after | STRING | No | Optional string to attach after the formatted point value. If you use a suffix, this string is appended after the suffix. | |
force_in | BOOL | No | Option to force the $before or $after variables before / after the prefix / suffix. | |
} |
Example 1: Display a users balance formatted with a prefix / suffix (if used)
$mycred = mycred();
$balance = $mycred->get_users_balance( get_current_user_id() );
echo $mycred->format_creds( $balance );
public function round_value( $amount = 0, $up_down = false, $precision = 0 ) { | ||||
Description: | Rounds a given value either up or down with an option to set the precision. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
amount | INT or FLOAT | Yes | The value to round. | |
up_down | STRING | Yes | Either “up” to round up or “down” to round down. | |
precision | INT | No | Rounding precision. Defaults to zero. | |
} |
public function get_lowest_value() { | ||||
Description: | Returns the lowest possible points value above zero based on the point type setup. A point type setup that does not uses any decimals would return 1 while a point type using for example 2 decimal places would return 0.01. Mainly used to prevent users from sending partial points. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
No variables | ||||
} |
public function apply_exchange_rate( $amount = 0, $rate = 1, $round = true ) { | ||||
Description: | Divides the amount by the given rate. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
amount | INT or FLOAT | Yes | The initial point amount. | |
rate | INT or FLOAT | Yes | The exchange rate that are applied to the amount. | |
round | BOOL | No | Option to round the value. Uses PHP round. | |
} |
public function parse_template_tags( $content = '', $log_entry ) { | ||||
Description: | Parses template tags in a given string. Can only be used when a log entry object is available. Based on the data column details, this method loads the appropriate template tag processor (see below). | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
content | STRING | Yes | The string that potentially contain template tags. | |
log_entry | OBJECT | Yes | The log entry object. | |
} |
public function template_tags_general( $content = '' ) { | ||||
Description: | Converts general template tags into their appropriate values. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
content | STRING | Yes | The string that potentially contain template tags. | |
} |
public function template_tags_amount( $content = '', $amount = 0 ) { | ||||
Description: | Converts amount related template tags into their appropriate values. Uses template_tags_general(). | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
content | STRING | Yes | The string that potentially contain template tags. | |
amount | INT or FLOAT | Yes | The amount to use when replacing amount related template tags. | |
} |
public function template_tags_post( $content = '', $ref_id = NULL, $data = '' ) { | ||||
Description: | Converts post related template tags into their appropriate values. Uses template_tags_general(). | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
content | STRING | Yes | The string that potentially contain template tags. | |
ref_id | INT | Yes | The reference ID stored with the log entry, usually the post object ID. | |
data | STRING or ARRAY | No | The data stored with the log entry. Mainly used when a post has been deleted as the old past data is stored in the data column. | |
} |
public function template_tags_user( $content = '', $ref_id = NULL, $data = '' ) { | ||||
Description: | Converts user related template tags into their appropriate values. Uses template_tags_general(). | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
content | STRING | Yes | The string that potentially contain template tags. | |
ref_id | INT | Yes | The reference ID stored with the log entry, usually the user object ID. | |
data | STRING or ARRAY | No | The data stored with the log entry. | |
} |
public function template_tags_comment( $content = '', $ref_id = NULL, $data = '' ) { | ||||
Description: | Converts comment related template tags into their appropriate values. Uses template_tags_general(). | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
content | STRING | Yes | The string that potentially contain template tags. | |
ref_id | INT | Yes | The reference ID stored with the log entry, usually the comment object ID. | |
data | STRING or ARRAY | No | The data stored with the log entry. | |
} |
public function has_tags( $type = '', $tags = '', $content = '' ) { | ||||
Description: | Checks if a given string contains any template tags based on a set type e.g. comment, post or user. Returns true or false. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
type | STRING | Yes | The template tag type. | |
tags | STRING | Yes | List of template tags to check for. This list is passed on to preg_match so if more then one tag needs to be checked for, this must be separated with a horizontal line |. | |
content | STRING | Yes | The content string that needs to be checked. | |
} |
public function available_template_tags( $available = array(), $custom = '' ) { | ||||
Description: | Returns a string indicating the available template tags that can be used. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
available | ARRAY | Yes | An array or template tag types. These will be listed after the sentence: Available Template Tags: . | |
custom | STRING | No | Optional list of additional / special template tags that are appended at the end of the string. | |
} |
public function allowed_tags( $data = '', $allow = '' ) { | ||||
Description: | Strips HTML tags of a given string based on the allowed html tags list provided by the allowed_html_tags method. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
data | STRING | Yes | The string that contains HTML tags that needs to be stripped. | |
allow | ARRAY or BOOL | No | Either false to strip all tags, an array or allowed HTML tags not to strip of or if left empty, a default set of tags are removed. | |
} |
public function edit_creds_cap() { | ||||
Description: | Returns the capability set for point editors. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
No variables | ||||
} |
Example 1: Make sure the current user is a point editor.
$mycred = mycred();
// If the current user is a "point editor"
if ( current_user_can( $mycred->edit_creds_cap() ) ) {
}
public function can_edit_creds( $user_id = '' ) { | ||||
Description: | Checks if a given user is a “point editor” based on the current point type. Uses user_can. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
user_id | INT | Yes | The user ID to check. | |
} |
public function edit_plugin_cap() { | ||||
Description: | Returns the capability set for point administrators. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
No variables | ||||
} |
Example 1: Make sure the current user is a point editor.
$mycred = mycred();
// If the current user is a "point administrator"
if ( current_user_can( $mycred->edit_plugin_cap() ) ) {
}
public function can_edit_plugin( $user_id = '' ) { | ||||
Description: | Checks if a given user is a “point administrator” based on the current point type. Uses user_can. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
user_id | INT | Yes | The user ID to check. | |
} |
public function in_exclude_list( $user_id = '' ) { | ||||
Description: | Checks if a given user is in the list of excluded users. The ID is checked against the comma separated list of IDs you might have set to the current point type. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
user_id | INT | Yes | The user ID to check. | |
} |
public function exclude_plugin_editors() { | ||||
Description: | Checks if the current point type has been set to exclude “point administrators”. Returns true or false. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
No variables | ||||
} |
public function exclude_creds_editors() { | ||||
Description: | Checks if the current point type has been set to exclude “point editors”. Returns true or false. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
No variables | ||||
} |
public function exclude_user( $user_id = NULL ) { | ||||
Description: | Checks if the given user is excluded from the current point type. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
user_id | INT | Yes | The user object ID. | |
} |
Example 1: Check if the current user is excluded from using the current point type.
$user_id = get_current_user_id();
$mycred = mycred();
if ( ! $mycred->exclude_user( $user_id ) ) {
// User is not excluded
}
public function count_members() { | ||||
Description: | Returns the total number of users with a balance of the current point type. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
No variables | ||||
} |
public function get_cred_id() { | ||||
Description: | Returns ID of the current point type. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
No variables | ||||
} |
public function max() { | ||||
Description: | Returns the highest value allowed for the current point type. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
No variables | ||||
} |
public function get_users_balance( $user_id = NULL, $type = NULL ) { | ||||
Description: | Returns the given users current balance for either a given point type or the current point type. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
user_id | INT | Yes | The users numeric ID. | |
type | STRING | No | Option to retrieve a users balance for a different point type then the current one. If you are want the current point types balance then this should not be used. | |
} |
Example 1: Get the current users balance for the current point type
$user_id = get_current_user_id();
$mycred = mycred();
$balance = 0;
// Get the balance if the user is not excluded
if ( ! $mycred->exclude_user( $user_id ) )
$balance = $mycred->get_users_balance( $user_id );
public function update_users_balance( $user_id = NULL, $amount = NULL, $type = NULL ) { | ||||
Description: | Updates a given users points balance by a given value. This value can be positive to increase a users balance or negative to reduce a balance. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
user_id | INT | Yes | The users numeric ID. | |
amount | INT or FLOAT | Yes | The point amount to adjust the balance with. This value must be properly formatted. If the point type does not use decimals, then the amount can not use decimals either. | |
type | STRING | No | Option to update a different point balance then the current point type. | |
} |
Example 1: Add 10 points to the current users balance.
$user_id = get_current_user_id();
$mycred = mycred();
$amount = 10;
// Update the balance if the user is not excluded
if ( ! $mycred->exclude_user( $user_id ) )
$mycred->update_users_balance( $user_id, $amount );
Example 2: Deduct 50 points to the current users balance.
$user_id = get_current_user_id();
$mycred = mycred();
$amount = -50;
// Update the balance if the user is not excluded
if ( ! $mycred->exclude_user( $user_id ) )
$mycred->update_users_balance( $user_id, $amount );
public function set_users_balance( $user_id = NULL, $new_balance = NULL ) { | ||||
Description: | Changes a users balance to the given value. Added in 1.7.3. Can only be used for the current point type. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
user_id | INT | Yes | The users numeric ID. | |
new_balance | INT or FLOAT | Yes | The point amount to adjust the balance with. This value must be properly formatted. If the point type does not use decimals, then the amount can not use decimals either. | |
} |
Example 1: Change the current users balance to 100 points.
$user_id = get_current_user_id();
$mycred = mycred();
$new_balance = 100;
// Update the balance if the user is not excluded
if ( ! $mycred->exclude_user( $user_id ) )
$mycred->set_users_balance( $user_id, $new_balance );
public function add_creds( $ref = '', $user_id = '', $amount = '', $entry = '', $ref_id = '', $data = '', $type = MYCRED_DEFAULT_TYPE_KEY ) { | ||||
Description: | Adds or deducts points from a given users account and saves a log entry of the transaction. Contrarily to what the name suggests, this method can be used to adding points as well as deducting points. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
ref | STRING | Yes | The reference to log the transaction under. | |
user_id | INT | Yes | The users numeric ID. | |
amount | INT or FLOAT | Yes | The point amount to adjust the balance with. This value must be properly formatted. If the point type does not use decimals, then the amount can not use decimals either. | |
entry | STRING | Yes | The log entry of the transaction. This is what is shown to users in the logs. | |
ref_id | INT | No | Optional reference ID to save with the log entry. | |
data | STRING or ARRAY | No | Optional data to save with the log entry. This can be a string or an array. Note that arrays will be serialized before saved to the log. | |
type | STRING | No | The point type to adjust and log the transaction under. Should only be used if the point type is not the current point type set in this class. | |
} |
Example 1: Give a user 10 points and log it as a “Donation“.
$user_id = 1;
$amount = 10;
$mycred = mycred();
if ( ! $mycred->exclude_user( $user_id ) )
$mycred->add_creds(
'donation',
$user_id,
$amount,
'%plural% donation'
);
public function add_to_log( $ref = '', $user_id = '', $amount = '', $entry = '', $ref_id = '', $data = '', $type = MYCRED_DEFAULT_TYPE_KEY ) { | ||||
Description: | Adds a log entry to the myCRED Log database table. Used by add_creds(). | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
ref | STRING | Yes | The reference to log the transaction under. | |
user_id | INT | Yes | The users numeric ID. | |
amount | INT or FLOAT | Yes | The point amount. If the point type does not use decimals, then the amount can not use decimals either. | |
entry | STRING | Yes | The log entry of the transaction. | |
ref_id | INT | No | Optional reference ID to save with the log entry. | |
data | STRING or ARRAY | No | Optional data to save with the log entry. This can be a string or an array. Note that arrays will be serialized before saved to the log. | |
type | STRING | No | The point type to adjust and log the transaction under. Should only be used if the point type is not the current point type set in this class. | |
} |
public function update_log_entry( $entry_id = NULL, $data = array(), $prep = array() ) { | ||||
Description: | Updates a given log entry. Requires that you have the log entries ID. Added in 1.6.7 | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
entry_id | INT | Yes | The unique log entry ID. | |
data | ARRAY | Yes | An array of new data. Uses the $wpdb->update(). | |
prep | ARRAY | Yes | An array of formats to be mapped to each of the values in $data.. | |
} |
public function has_entry( $reference = NULL, $ref_id = NULL, $user_id = NULL, $data = NULL, $type = NULL ) { | ||||
Description: | Checks if a specific log entry exists in the database. | |||
---|---|---|---|---|
Variable | Type | Required | Description | |
reference | STRING | No | The reference to check. | |
ref_id | INT | No | The reference id to check. | |
user_id | INT | No | The user id to check. | |
data | STRING | No | The data to check for. If you know the data is an array, make sure this value is a serialized array. | |
type | STRING | No | The point type to check. | |
} |