MainWP: Pro report additional custom tokens

Tech Articles | January 2, 2025 | Blog, Coding, Hosting, MainWP, Wordpress

I released an Extension for MainWP sometime ago for allowing Cloudflare data in your client reports using the pro report, and in a version of the report I sent myself, I wanted to combine some totals for security, specifically Cloudflare & Solid Security Pro.

This is a reasonably easy thing in MainWP. Now, they have introduced the hook “mainwp_pro_reports_addition_custom_tokens.” An example of that code is below.

add_filter( 'mainwp_pro_reports_addition_custom_tokens', 'mycustom_mainwp_pro_reports_addition_custom_tokens', 10, 3 );
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['[custom.update.total]'] = $total;
    }
    
    return $tokens;
}
PHP

The issue I had was getting the data from my custom extension to use with the above filter. I asked in the community forum, but it was unsuccessful as there was a misunderstanding that there is no child data here. It’s all done within the dashboard, and once confirmed, I could do what I wanted using PHP Global Variables.

Then I realised I needed to think more about WordPress and Less MainWP. They are both on the same site, and WordPress provides an answer for this humble filter.

So, in no time at all, I adjusted the code to add a new filter that would make these variables available outside of the extension, i.e. during pro-report generation; in testing on several sites, it seems to work well.

// Set token values
        $tokensValues['[cfmwp-requests]'] = $analytics->requests;
        $tokensValues['[cfmwp-uniques]'] = $uniq->uniques;
        $tokensValues['[cfmwp-cached]'] = $analytics->cachedRequests;
        $tokensValues['[cfmwp-bandwidth]'] = cfmwp_format_bandwidth($analytics->bytes); // Use the new formatting function here
        $tokensValues['[cfmwp-attacks]'] = $analytics->threats;

        
        // Pass all analytics data via a 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;
        });
PHP


and with that in my mainwp_settings plugin, a quick update, and I now have some working custom tags πŸ™‚

add_filter( 'mainwp_pro_reports_addition_custom_tokens', 'rup_ithemes_security_tokens', 10, 3 );
function rup_ithemes_security_tokens( $tokens, $site_id, $data ) {
    $all_analytics = apply_filters('cfmwp_all_analytics_data', array());
    // Log the $all_analytics array to the error log
    if (!empty($all_analytics)) {
        //error_log('CFMWP Analytics Data: ' . print_r($all_analytics, true));
    } else {
        //error_log('CFMWP Analytics Data: No data returned');
    }
    if(is_array($data) && isset($data[$site_id])){
         $total = 0;
         $total += isset($data[$site_id]['other_tokens_data']['body']['[ithemes.lockout.count]']) ? intval( $data[$site_id]['other_tokens_data']['body']['[ithemes.lockout.count]'] ) : 0;
         $total += isset($data[$site_id]['other_tokens_data']['body']['[ithemes.blocked.count]']) ? intval( $data[$site_id]['other_tokens_data']['body']['[ithemes.blocked.count]'] ) : 0; 
         $total += isset($all_analytics['attacks']) ? $all_analytics['attacks'] : 0;
         $tokens['[ithemes.security.total]'] = $total;

    }
    
    return $tokens;

}
 
PHP


With this, I will now be pushing an update toΒ MainWP-Bridge-to-Cloudflare-BridgeΒ that will add this filter for everyone and also will better deal with TLDs, something I’ve been testing for several months now without fail, so I think it is time to distribute wider and close down the issue report.

Support the Author

buy me a coffee
Really Useful Plugin Logo
Appoligies for any spelling and grammer issue. As a dyslexic i need to rely on tools for this they like me are not perfect but I do try my best