Cloudflare to MainWP Bridge
This is installed on the MainWP Dashboard, this is now fully hooked in to report generation and the only modification needed is to add your API Key to the dashboard and it will add your new tokens for you; ## new-template.php This is a custom template based on the example uploaded to the Admin Bar Facebook group that set this in video and code solution https://www.youtube.com/watch?v=ytUNgJMu0vg
I recreated it just because I could and to begin to show what is possible, this uses new functionality which will be in a future update of the pro reports that was passed to me by MainWP support.
Which allows a custom function to create a new tag manipulating data and created the [website.updated.total] template token
Which allows a custom function to create a new tag manipulating data and created the [website.updated.total] template token this function lives in my mainwp settings plugin but I have added it below for you;
function mycustom_mainwp_pro_reports_addition_custom_tokens( $tokens, $site_id, $data ) {
if(is_array($data) && isset($data[$site_id])){
$total = 0;
$total += isset($data[$site_id]['other_tokens_data']['body']['[plugin.updated.count]']) ? intval( $data[$site_id]['other_tokens_data']['body']['[plugin.updated.count]'] ) : 0;
$total += isset($data[$site_id]['other_tokens_data']['body']['[theme.updated.count]']) ? intval( $data[$site_id]['other_tokens_data']['body']['[theme.updated.count]'] ) : 0;
$total += isset($data[$site_id]['other_tokens_data']['body']['[wordpress.updated.count]']) ? intval( $data[$site_id]['other_tokens_data']['body']['[wordpress.updated.count]'] ) : 0;
$tokens['[website.updated.total]'] = $total;
}
return $tokens;
}
-
Add the main extension to your dashboard
-
Navigate to the extension within MainWP and click Cloudflare bridge so you can add your Cloudflare api key
![](https://github.com/stingray82/repo-images/raw/main/mainwp-cloudflare-bridge/Extentions Installed.png)
- head over to Cloudflare and your API settings should look like this to work with this extension
![](https://github.com/stingray82/repo-images/raw/main/mainwp-cloudflare-bridge/Cloudflare API Token.png)
-
Now head back to your screen within the extension to add your API key which you'll need to get from Cloudflare in the step above and save
-
That's it you now have the new custom tokens avaliable to you within pro-reports
It adds three new custom tokens you can access which are displayed below; [cfmwp-requests] [cfmwp-uniques] [cfmwp-cached] [cfmwp-bandwidth] [cfmwp-attacks]
#### bandwidth is reported from cloudflares api in bytes which isn't useful when you get to GB or TB so a new function was added to convert this to the "best unit" to report in code below
function cfmwp_format_bandwidth($bytes) {
$units = array('bytes', 'KB', 'MB', 'GB', 'TB');
$power = $bytes > 0 ? floor(log($bytes, 1024)) : 0;
$value = $bytes / pow(1024, $power);
return round($value, 2) . ' ' . $units[$power];
}
Please Note: I have tested this and written this and it works for my custom dashboard and requirements you will need to test in your setup
As of Version 1.1 -
Improvement:
It now updates suffixes information from the public database:
https://publicsuffix.org/list/public_suffix_list.dat this download is now cached for 24 hours and should mean its always update with new domain suffix’s
This should permanently fix issue number 1 - Not working for Country TLDs, I have been testing this for a few months now with no issues but obviously do your won testing!
New:
New filter added to get the data out for use in calculations with the mainwp_pro_reports_addition_custom_tokens filter,
$all_analytics = array(
'requests' => $analytics->requests,
'uniques' => $uniq->uniques,
'cached' => $analytics->cachedRequests,
'bandwidth' => $analytics->bytes,
'attacks' => $analytics->threats,
);
//Use add_filter to register the data for custom hook
add_filter('cfmwp_all_analytics_data', function () use ($all_analytics) {
return $all_analytics;
});
Example Usage:
$all_analytics = apply_filters('cfmwp_all_analytics_data', array());
$all_analytics['attacks']