Preface
The PHP API is accessed via the borlabsCookieApi()
function.
When called before the WordPress init
action hook, this function returns null
.
If you want to use the PHP API during the init
hook, you may need to explicitly set the value of $priority
to 11
or higher because WordPress executes the initialization of our PHP API with a priority of 10
.
WP-CLI vs PHP API
Use our WP CLI integration to automate tasks in your WordPress environment. The PHP API is designed to provide access to specific functions for use in custom themes or plugins.
Borlabs\Cookie
namespace or writing to our database tables is strictly prohibited and may lead to fatal errors in future updates. We only guarantee compatibility with future updates when using borlabsCookieApi()
.
If you require access to a specific Borlabs Cookie function that is not available via our PHP API, please contact us through our ticket system, and we will work with you to find a solution.
borlabsCookieApi()
The method returns an instance of the class PhpApi
. Documentation of all methods is available via PHPDoc.
contentBlockerApi()
The method returns an instance of the class ContentBlockerPhpApi
.
blockContent()
Blocks the provided content based on the given parameters.
/**
* Blocks the provided content based on the given parameters.
*
* @param string $content the content to be blocked
* @param null|string $url Optional. If provided, determines the type of Content Blocker based on the URL.
* @param null|string $contentBlockerId Optional. If provided, specifies the Content Blocker to use by its ID. If `$contentBlockerId` and `$url` are both provided, `$contentBlockerId` takes precedence.
* @param null|array $attributes Optional. Additional attributes to customize the blocking process.
*
* @return string the blocked content
*/
blockContent(
string $content,
?string $url = null,
?string $contentBlockerId = null,
?array $attributes = null
): string
Example:
add_filter('the_content', function ($content) {
if (function_exists('borlabsCookieApi') !== false) {
// Blocks the entire content
$content = borlabsCookieApi()->contentBlockerApi()->blockContent($content);
}
return $content;
});
detectAndBlockIframes()
Detects and blocks iframes in the provided content.
/**
* Detects and blocks iframes in the provided content.
*
* @param string $htmlContent the content to be blocked
*
* @return string the blocked content
*/
detectAndBlockIframes(string $htmlContent = ''): string
Example:
add_filter('the_content', function ($content) {
if (function_exists('borlabsCookieApi') !== false) {
// Only blocks iframes in $content
$content = borlabsCookieApi()->contentBlockerApi()->detectAndBlockIframes($content);
}
return $content;
});
generalConfigApi()
The method returns an instance of the class GeneralConfigPhpApi
.
get()
Returns the settings from the Settings section, specific to each language. The settings returned correspond to the currently active language.
You can determine the currently loaded language in Borlabs Cookie by using borlabsCookieApi()->languagePhpApi()-
>getCurrentLanguageCode()
.
/**
* Example:
* <code>
* {
* "aggregateConsents": false,
* "automaticCookieDomainAndPath": false,
* "borlabsCookieStatus": false,
* "clearThirdPartyCache": true,
* "cookieDomain": "domain.internal",
* "cookieLifetime": 60,
* "cookieLifetimeEssentialOnly": 60,
* "cookiePath": "/",
* "cookieSameSite": "Lax", // "Lax"|"None",
* "cookieSecure": true,
* "cookiesForBots": true,
* "crossCookieDomains": ["second-domain.internal"],
* "metaBox": ["post" => "1"],
* "pluginUrl": "https://domain.internal/wp-content/plugins/borlabs-cookie",
* "reloadAfterOptIn": false,
* "reloadAfterOptOut": true,
* "respectDoNotTrack": false,
* "setupMode": false,
* }
* </code>.
*
* @return stdClass{
* aggregateConsents: bool,
* automaticCookieDomainAndPath: bool,
* borlabsCookieStatus: bool,
* clearThirdPartyCache: bool,
* cookieDomain: string,
* cookieLifetime: int,
* cookieLifetimeEssentialOnly: int,
* cookiePath: string,
* cookieSameSite: string,
* cookieSecure: bool,
* cookiesForBots: bool,
* crossCookieDomains: array,
* metaBox: array,
* pluginUrl: string,
* reloadAfterOptIn: bool,
* reloadAfterOptOut: bool,
* respectDoNotTrack: bool,
* setupMode: bool
* }
*/
get(): stdClass
Example:
add_action('init', function () {
if (function_exists('borlabsCookieApi') === false) {
return;
}
if (borlabsCookieApi()->generalConfigApi()->get()->borlabsCookieStatus === true) {
// Do something
}
}, 11);
languagePhpApi()
The method returns an instance of the class LanguagePhpApi
.
getCurrentLanguageCode()
Returns the currently active language.
/**
* Returns the language code of the current language.
*/
getCurrentLanguageCode(): string
Example:
add_filter('the_content', function ($content) {
if (function_exists('borlabsCookieApi') === false) {
return $content;
}
if (borlabsCookieApi()->generalConfigApi()->get()->borlabsCookieStatus === true) {
if (borlabsCookieApi()->languagePhpApi()->getCurrentLanguageCode() === 'en') {
$content .= 'This is only visible on an English page.';
} else if (borlabsCookieApi()->languagePhpApi()->getCurrentLanguageCode() === 'de') {
$content .= 'Dies ist nur auf einer deutschen Seite sichtbar.';
}
}
return $content;
});
pluginConfigApi()
The method returns an instance of the class PluginConfigPhpApi
.
get()
Returns the plugin settings that are language-independent. These settings can be configured either in the Dashboard or in the Library.
/**
* Example:
* <code>
* {
* "automaticUpdate": "auto-update-all", // "auto-update-all"|"auto-update-minor"|"auto-update-none"|"auto-update-patch"
* "enableDebugConsole": true,
* "enableDebugLogging": false,
* "enableEmailNotificationsForUpdatablePackagesWithAutoUpdateDisabled": false,
* "enableEmailNotificationsForUpdatablePackagesWithAutoUpdateEnabled": true,
* "packageAutoUpdateEmailAddresses": ["mail@example.internal"],
* "packageAutoUpdateInterval": "after-24-hours", // "after-24-hours"|"monday"|"tuesday"|"wednesday"|"thursday"|"friday"|"saturday"|"sunday"
* "packageAutoUpdateTime": "07:00",
* }
* </code>.
*
* @return stdClass{
* automaticUpdate: string,
* enableDebugConsole: bool,
* enableEmailNotificationsForUpdatablePackagesWithAutoUpdateDisabled: bool,
* enableEmailNotificationsForUpdatablePackagesWithAutoUpdateEnabled: bool,
* packageAutoUpdateEmailAddresses: array,
* packageAutoUpdateInterval: string,
* packageAutoUpdateTime: string
* }
*/
get(): stdClass