Filter: mainwp_pro_reports_addition_custom_tokens

I want to do some testing using the above filter. Specifically, I am currently trying to total up Itheme and attacks from my Cloudflare bridge plugin

I have got it to pull the i-theme stuff fine using a model if seem before but I’ve tried every variation I can think of to pull cfmwp-attacks, is there a way to pull custom tokens (there in the array and working in the report but I am trying to display a total security count and can’t because I can’t get cfmwp-attacks or any custom added token to “add” it just doesn’t seem to pull through do I need to add something to the plugins or is there another way to pull this data i.e directly out of the generated tokens?

add_filter( 'mainwp_pro_reports_addition_custom_tokens', 'ithemes_and_cloudflare_total_tokens', 10, 3 );
    function ithemes_and_cloudflare_total_tokens( $tokens, $site_id, $data ) {
        // Log all tokens for debugging
        error_log( 'Available Tokens: ' . print_r( $tokens, true ) );

        if ( is_array( $data ) && isset( $data[$site_id] ) ) {
            // Access existing tokens directly
            $ithemes_blocked_count = isset( $data[$site_id]['other_tokens_data']['body']['[ithemes.blocked.count]'] ) 
                ? intval( $data[$site_id]['other_tokens_data']['body']['[ithemes.blocked.count]'] ) 
                : 0;

            $ithemes_lockout_count = isset( $data[$site_id]['other_tokens_data']['body']['[ithemes.lockout.count]'] ) 
                ? intval( $data[$site_id]['other_tokens_data']['body']['[ithemes.lockout.count]'] ) 
                : 0;

            $cfmwp_attacks_count = isset( $tokens['[cfmwp-attacks]'] ) 
                ? intval( $tokens['[cfmwp-attacks]'] ) 
                : 0;

            // Debugging individual token values
            error_log( "Blocked: {$ithemes_blocked_count}, Lockouts: {$ithemes_lockout_count}, Attacks: {$cfmwp_attacks_count}" );

            // Calculate total
            $total_security_issues = $ithemes_blocked_count + $ithemes_lockout_count + $cfmwp_attacks_count;

            // Add the total token
            $tokens['[security.issues.total]'] = $total_security_issues;
        }

        return $tokens;
    }

Thanks in Advance

1 Like

Hey @stingray82

The development team has reviewed your post.

To them, it looks like you need to get the cfmwp-attacks info somewhere from the child site.
Then you need to use filter mainwp_site_sync_others_data in child plugin, and save the data into the MainWP Dashboard.

Then get the value in your current filter: mainwp_pro_reports_addition_custom_tokens

Thanks, but that’s not going to work the way this works; there is nothing to grab from the child site. It’s all done in the dashboard at the generation of the pro-reports

it gets the generating pro-report site domain and pulls the record from Cloudflare and then gets the data from the API for it, so there is nothing to “generate on the child site”. It doesn’t touch a child site.

I need a way to get the data from the the below function, specicially $tokensValues[‘[cfmwp-attacks]’] = $analytics->threats; and add it to ithemes.blocked.count + ithemes.lockout.count or add to the below filter?

add_filter( 'mainwp_pro_reports_addition_custom_tokens', 'rup_ithemes_security_tokens', 10, 3 );
function rup_ithemes_security_tokens( $tokens, $site_id, $data ) {
    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;   
         $tokens['[ithemes.security.total]'] = $total;
    }
    
    return $tokens;

}

The applicable bit of the extension is below what I want to do is add ithemes.blocked.count + ithemes.lockout.count with cfmwp-attacks

Is there a filter to add data to be avaliable to MainWP from within the dashboard site? i.e from external plugins etc?

function cfmwp_generate_custom_analytics_tokens($tokensValues, $report, $site, $templ_email) {
    $api_token = get_option('cfmwp_api_token');
    if (!$api_token) {
        return $tokensValues;
    }

    $site_url = isset($site['url']) ? $site['url'] : '';
    if (!$site_url) {
        return $tokensValues;
    }

    $parsed_url = parse_url($site_url);
    $domain = isset($parsed_url['host']) ? $parsed_url['host'] : '';

    if (!$domain) {
        return $tokensValues;
    }

    $root_domain = get_root_domain($domain);

    // Get the zone ID based on the root domain
    $api_url = "https://api.cloudflare.com/client/v4/zones?name={$root_domain}";

    $response = wp_remote_get($api_url, array(
        'headers' => array(
            'Authorization' => 'Bearer ' . $api_token,
            'Content-Type'  => 'application/json',
        ),
    ));

    if (is_wp_error($response)) {
        return $tokensValues;
    }

    $body = wp_remote_retrieve_body($response);
    $data = json_decode($body);

    if (!isset($data->result[0]->id)) {
        return $tokensValues;
    }

    $zone_id = $data->result[0]->id;

    $from_date = isset($report->date_from) ? date('Y-m-d', $report->date_from) : '';
    $to_date = isset($report->date_to) ? date('Y-m-d', $report->date_to) : '';

    $query = "{\"query\":\"{viewer {zones(filter: {zoneTag: \\\"$zone_id\\\"}) {httpRequests1dGroups(limit: 10, filter: {date_gt: \\\"$from_date\\\", date_lt: \\\"$to_date\\\"}) {dimensions {date} uniq { uniques } sum {requests cachedRequests cachedBytes threats bytes}}}}}\"}";

    $response = wp_remote_post('https://api.cloudflare.com/client/v4/graphql/', array(
        'body'    => $query,
        'headers' => array(
            'Authorization' => 'Bearer ' . $api_token,
            'Content-Type'  => 'application/json',
        ),
    ));

    if (is_wp_error($response)) {
        return $tokensValues;
    }

    $body = wp_remote_retrieve_body($response);
    $data = json_decode($body);

    if (isset($data->data->viewer->zones[0]->httpRequests1dGroups[0]->sum)) {
        $analytics = $data->data->viewer->zones[0]->httpRequests1dGroups[0]->sum;
        $uniq = $data->data->viewer->zones[0]->httpRequests1dGroups[0]->uniq;

        // 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;
    }

    return $tokensValues; mainwp_site_sync_others_data 
}

I can get it to work with global variables but that means I am effectively making MainWP Bridge to Cloudflare my personal plugin not public as I will be modifying the code to suit my purpose rather than a more general allow tokens in pro-reports?

What I need to to is be able to get that data back out and available to use within the filter

I think I have a solution, I needed to think more WordPress and Less MainWP I will come back once I’ve done some more testing

1 Like

Ok this is fixed; I kept thinking this was a MainWP issue but WordPress already provides the answer, so the short answer is I added a filter to the Cloudflare bridge and used the apply filter within the code.

So, I added this code to the bridge.

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;
        });

Then, called it where needed

 $all_analytics = apply_filters('cfmwp_all_analytics_data', array());
$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;

I did a quick write-up about it here: https://techarticles.co.uk/mainwp-pro-report-additional-custom-tokens/ mainly to remind me in future why I’ve done what I’ve done;

I will be pushing an update to the bridge in the next half hour to take it to 1.1 add this filter and hopefully a great fix for the 2nd level TLD issue

1 Like

Glad to hear you’ve managed to find the solution, and thank you for sharing your findings with the community, both here and in your write-up.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.