admin管理员组

文章数量:1125019

I am working on a custom CRM which includes keeping an eye on my clients WordPress sites so I have created some custom REST API endpoints to collect different types of data like the installed plugins & themes etc, which of those need to be updated, and a bunch of other stuff to help maintain their websites.

So, I made a start on adding some features to update plugins, themes and core using the REST API, but I seem to have run into an issue I can't pinpoint when it comes to updating plugins. As far as I can tell the callback function runs as it should, but no plugin actually gets updated.

This is from the error_log

[10-Feb-2024 10:54:16 UTC] Received request to update a plugin
[10-Feb-2024 10:54:17 UTC] We are updating elementor
[10-Feb-2024 10:54:17 UTC] Plugin updated successfully.

'elementor' is the plugin slug sent to the callback via REST API.

Here is the callback on the child WordPress site.

// Callback function for updating a plugin
function wptm_update_plugins($request) {
    error_log("Received request to update a plugin");
    $parameters = $request->get_params();

    // Check if plugin slug is provided
    if (isset($parameters['plugin_slug']) && !empty($parameters['plugin_slug'])) {
        $plugin_slug = $parameters['plugin_slug'];

        // Perform plugin update
        require_once ABSPATH . 'wp-admin/includes/plugin.php'; // Include plugin.php for wp_update_plugins()
        $result = wp_update_plugins(array($plugin_slug));
        error_log('We are updating '.$plugin_slug);

        if (is_wp_error($result)) {
            // Plugin update failed
            $response = array(
                'error' => $result->get_error_message(),
            );
            error_log('Plugin failed to update.');
        } else {
            // Plugin update successful
            $response = array(
                'success' => 'Plugin updated successfully.',
            );
            error_log("Plugin updated successfully.");
        }
    } else {
        // Plugin slug not provided
        $response = array(
            'error' => 'Plugin slug is required.',
        );
    }

    return rest_ensure_response($response);
}

Code on the remote server

if(isset($_POST['plugin_id'])) {
    $plugin_id = $_POST['plugin_id'];
    error_log("Plugin Update Reqiest Started.");
}

// Retrieve list of website URLs with updates enabled
$sql = "SELECT website_url, plugin_slug FROM NFRi2I_jet_cct_plugin_list WHERE _ID = $plugin_id";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();
    $website_url = $row['website_url'];
    $plugin_slug = $row['plugin_slug'];

    error_log('We found the plugin: '.$plugin_slug.'');

    // Construct API request URL
    $api_url = $website_url . '/wp-json/custom-endpoint-here/v1/plugins/update';

    // Prepare request data
    $data = array(
        'plugin_slug' => $plugin_slug,
    );

    // Create request context
    $context = stream_context_create(array(
        'http' => array(
            'method' => 'POST',
            'header' => 'Content-Type: application/json',
            'content' => json_encode($data),
        ),
    ));

    // Send API request
    $response = file_get_contents($api_url, false, $context);

    if ($response === false) {
        // Handle API request failure
        echo "Failed to send API request.";
        error_log("Failed to send API request.");
    } else {
        // Handle API response
        echo "API response: " . $response;
        error_log('API Response: '.$response.'');
    }
} else {
    // Handle no matching record found
    echo "No matching record found for plugin ID $plugin_id.";
}

The REST Route

register_rest_route('custom-endpoint-here/v1', '/plugins/update', array(
        'methods' => 'POST',
        'callback' => 'wptm_update_plugins',
    ));

本文标签: phpREST API Plugin Update call back not updating the plugin