PHP API v3

|

Table of Contents

Introduction

Access to the PHP API is provided through the borlabsCookieApi() function.

If the function is called before the WordPress init action hook, it returns null.

If you want to use the PHP API during the init hook, you may need to explicitly set the $priority value to 11 or higher, as WordPress initializes our PHP API with a priority of 10.

WP-CLI vs PHP API

Use our WP-CLI integration to automate tasks within your WordPress environment. The PHP API is designed to provide access to specific functionality for use in custom themes or plugins.

Direct access to classes within the Borlabs\Cookie namespace or writing directly to our database tables is strictly prohibited and may result in fatal errors in future updates.

We only guarantee compatibility with future releases when using borlabsCookieApi().

If you require access to a specific Borlabs Cookie feature that is not currently available through the PHP API, please contact us through our ticket system and we will work with you to find a suitable solution.

borlabsCookieApi()

This method returns an instance of the PhpApi class. Documentation for all available methods is provided through PHPDoc.

contentBlockerApi()

This method returns an instance of the ContentBlockerPhpApi class.

blockContent()

Blocks content based on the specified 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 contained within the 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()

This method returns an instance of the GeneralConfigPhpApi class.

get()

Returns the settings from the "Settings" section that are specific to each language. The returned settings correspond to the currently active language.

You can determine the currently active language in Borlabs Cookie 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()

This method returns an instance of the LanguagePhpApi class.

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()

This method returns an instance of the PluginConfigPhpApi class.

get()

Returns the plugin settings that are not language-specific.

These settings can be configured either in the dashboard or through 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

Was this article helpful?

Helpful
Not helpful

Couldn't find the answer you were looking for? We're happy to help.