Helping functions and methods

Where to find the tools

You can find a complete list of where things are place at the page Module file and directory structure.

Global Methods

To make external integrations simpler to use without too many imports, there are some features that are callable without the need of the below namespaces and statics. They works similarly to how WooCommerce and WordPress own global methods. They was implemented September 2022 and will be available from 0.0.1.8.

For example, instead of using imports and calling Data::getResursOption, one can simply use rbwc_get_option from wherever it is needed as long as the primary plugin is loaded before your own integration.

MethodHelper for...ReturnDescription
rbwc_get_option
Data::getResursOption($key, $namespace, $getDefaults)bool|string|null (usually)Configuration values from the configuration array that WooCommerce handles.
rbwc_get_prefix
Data::getPrefix($extra, $ignoreCodeBase)

string

Getting the proper prefix that the plugin is using. For example "trbwc" or "resursbank" depending on the plugin slug.
rbwc_get_truth
Data::getTruth($paymentMethod)bool|nullUsed internally by get_option-features, to make sure booleans are really returned as booleans.
In the internal platform, default boolean values are defined as "yes", "no" and not true/false.
This feature makes sure such values are properly translated.
rbwc_can_handle_order
Data::canHandleOrder($paymentMethod)boolUsing the payment method name from WC_Order to decide whether the plugin can or can not handle a specific order.
rbwc_get_escaped_html
Data::getEscapedHtml($html)stringReturns escaped html, WordPress-style. You can use the wordpress escaping tools directly instead of this feature, however this has been made generic for where we know that we always need the same kind of parsing.
rbwc_get_order_meta
Data::getOrderMeta($key, $order)mixedReturns metadata from an order properly, including ecom-data.
rbwc_log_info
Data::setLogInfo($message)voidLog data to WooCommerce log repository, properly formatted with trace data.
All WooCommerce loggers are using different severities and the Data container handles them WooCommerce-properly.
rbwc_log_error
Data::setLogError($message)voidLog a simple custom error message string.
All WooCommerce loggers are using different severities and the Data container handles them WooCommerce-properly.
rbwc_log_exception
Data::setLogException($exception, $fromFunction)voidSend an entire exception to the WooCommerce-logger.
All WooCommerce loggers are using different severities and the Data container handles them WooCommerce-properly.
rbwc_apply_mock
WooCommerce::applyMock($mockName)mixedApply a mocking fitler, that can be used to mock unexpected events.

Statics

MethodReturnDescription
ResursBank\Module\Data::getPrefix($extra)A prefixReturns the default prefix that is used by the plugin internally. Adding an extra variable, will return "the_prefix_$extra". For example, all configuration data are using "admin" as an extra parameter, so currently the returned data is trbwc_admin. Getting configuration data in this way, for example if you want to determine if the plugin is enabled the complete option key in the database looks like trbwc_admin_enabled.
ResursBank\Module\Data::getImage($imageName)URL

$imageName is the short name (i.e. "logotype") of an image that usually resides in /images in the plugin structure.
Loading will occur with an autodetected file extension which limits the risks of loading something not allowed.

Example: getImage('author-photo')
Returns: https://url.com/plugin/url/author-photo.[jpg|png|gif]

ResursBank\Module\Data::getGatewayPath($subDirectory)Absolute path to plugin structure.By entering a subdirectory name it als returns /full/path/to/subDirectory.
Example: getGatewayPath('images')
Returns /full/path/to/plugin/images
ResursBank\Module\Data::applyFilters($filterName, $value[, $args])Whatever that is applied.

This is actually a standard apply_filters, but with a helper that always adds a proper prefix to every filters applied.

Example: applyFilters('checkout')
Will apply: rbwc_checkout

Note: Compare to applyFiltersDeprecated that instead generates "resurs_bank_checkout", to comply with prior plugin releases.

ResursBank\Module\Data::getVersionByComposer
Always returns version number that lies within composer.json.
ResursBank\Module\Data::getCurrentVersion
Always returns version number that lies within the initializer.
ResursBank\Module\Data::getValidatedVersionbooleanReturns true if both composer-version and internal version is the same. If not, the plugin should warn inside wp-admin that someone forgot to update this data.
ResursBank\Module\Data::getGenericClassGeneric::class

Returns a Generic::class with properly configured template path pointing too plugin-directory/templates - adding templates to that directory in the extension .html makes it possible to call the class like this:

$content .= self::getGenericClass()->getTemplate(
            'plugin_information',
            [
                'required_drivers' => self::getSpecialString('required_drivers'),
                'render' => $renderData,
            ]
        );

The variables added to the second argument in the getTemplate-method could then be used like this:

Required drivers: <?php echo $required_drivers ?>
ResursBank\Module\Data::getResursOptionA value

Using getResursOption directly will give you the opportunity to just send in a saved option key in the method. The default parameter keys are prefixed trbwc_admin and is added automatically. It also checks values against "boolean-like" valued strings like yes/true/no/false (via getTruth below).

As the prefix COULD possibly change, it is recommended to always use those internals.

ResursBank\Module\Data::getTruth($value)boolean or nullIf you are using get_options directly, you can use this function to determine if the returned option should be considered a boolean or string (on the values yes/true and false/no that is the default boolean value returned from a woocommerce setup). If the returned value is null, then you can use it as a string. This is entirely handled by Data::getResursOption if you decide to use that instead.
ResursBank\Module\Data::getFormFields($section)$formFieldArrayForm fields for admin based on section.
ResursBank\Module\Data::setLogInternal($type, $message)-

To send logs to Woocommerce natural logger, setLogInternal can be used. The datatypes in the first argument can be found in the Data class and are defined as below. This first introduced an action, but was later converted to a static method.

In this plugin, the types are just used to mark severity, but uses respecitvely method in WC_Logger anyway:

Constant Type/NameDescriptionWC_Logger()Suggested when
Data::LOG_DEBUGDebug information.debug()Developer notes.
Data::LOG_NOTICENotice only (default).notice()Notices to merchant and less important data.
Data::LOG_CRITICALCritical errors.critical()Critical errors, I.E. when API is down.
Data::LOG_ERRORNot critical but an error.debug()
Data::LOG_WARNINGNot an error. Not critical.
A warning.
debug()A headsup to merchants/developers.
I.E. when the plugin mismatches the required woocommerce version.

Example on how to log data:

Data::setLogInternal(
    Data::LOG_NOTICE,
    sprintf('Customer %s has an address located outside country.', $customer)
);