0
X
Add Snippet To Project
New Project
Add To Existing Project
<?php
/**
* Function to find and update API status
*/
function maadpro_update_api_status() {
// Include the Google Sheets API library
require_once 'path/to/google-api-php-client/vendor/autoload.php';
// Load the API credentials and create a client
$client = new Google_Client();
$client->setAuthConfig('path/to/client_secret.json');
$client->addScope(Google_Service_Sheets::SPREADSHEETS);
// Create a Sheets service object
$service = new Google_Service_Sheets($client);
// Spreadsheet ID and range
$spreadsheetId = 'YOUR_SPREADSHEET_ID';
$range = 'Sheet1!A2:C';
// Get the API keys and statuses from the spreadsheet
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
$values = $response->getValues();
// Find the first API key with status "Unused"
foreach ($values as $row) {
$apiKey = $row[1];
$status = $row[2];
if ($status === "Unused") {
// Update the status to "Used"
$row[2] = "Used";
$values = [$row];
$body = new Google_Service_Sheets_ValueRange([
'values' => $values
]);
// Update the cell value
$service->spreadsheets_values->update($spreadsheetId, $range, $body, [
'valueInputOption' => 'RAW'
]);
// Break out of the loop after updating the status
break;
}
}
}
// Hook the function to a WordPress action
add_action('wp_loaded', 'maadpro_update_api_status');
Explanation:
In this modified code snippet, the original plugin code is transformed into a standalone WordPress function. Here's how the code has been adjusted:
By converting the code into a WordPress function, you can now include and use it directly within your theme's functions.php file or any other custom code file within your theme. Make sure to replace 'YOUR_SPREADSHEET_ID' and the paths to the Google API client library and client secret JSON file with your own values.