<?php

/*
|--------------------------------------------------------------------------
| WordPress version utility
|--------------------------------------------------------------------------
|
| Here we're going to add our own action when the WordPress version is updated.
|
|
*/

	/**
	 * Fires after WordPress core has been successfully updated.
	 *
	 * @since 3.3.0
	 *
	 * @param string $wp_version The current WordPress version.
	 */
add_action('_core_updated_successfully', 
  function (string $wp_version)
  {
    $curl = curl_init();

    $data =["wpVersion" => "$wp_version"];

    $data_string = json_encode($data);

    $jwt_token = getenv("JWT_TOKEN");

    if (!$jwt_token) {
      error_log("JWT_TOKEN not set");
      return;
    }

    $website_webhook_url = getenv("WEBSITE_WEBHOOK_URL");

    if (!$website_webhook_url) {
      error_log("WEBSITE_WEBHOOK_URL not set");
      return;
    }

    curl_setopt_array($curl, array(
      CURLOPT_URL            => "{$website_webhook_url}/v1/wpversion",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING       => '',
      CURLOPT_MAXREDIRS      => 10,
      CURLOPT_TIMEOUT        => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST  => 'PUT',
      CURLOPT_HTTPHEADER     => [
        'Content-Type: application/json',
        'authorization: '. $jwt_token,
      ],
      CURLOPT_POSTFIELDS     => $data_string
    ));

    curl_exec($curl);
    curl_close($curl);
  }
, 10);