WP-Stateless

repository·latest·Indexed 43 days ago

https://github.com/udx/wp-stateless

A WordPress plugin for handling media files via Google Cloud Storage. It offers four operating modes: Backup (uploads to GCS but serves local URLs), CDN (copies to GCS and serves from GCS URLs), Ephemeral (stores and serves via GCS with temporary local processing), and Stateless (stores and serves via GCS without local storage). Supports manual Service Account JSON configuration with Storage Admin roles.

Tokens
15.1K
Snippets
58
Records
77
Agent score
54%

What's inside WP-Stateless

  1. Locate Google API Client Service classes

    latest
    The Google API Client Service classes are no longer maintained within this repository. They have been moved to a dedicated repository: google-api-php-client-services. If you need to access specific service classes (e.g., Drive, Sheets, etc.), you must use the external services repository.
  2. Handle Access Tokens and ID Token data as arrays

    latest

    In version 2.0 and later, authentication data structures have changed from JSON strings to associative arrays.

    • Access Tokens: getAccessToken() now returns an array instead of a JSON string. When saving tokens, you must use json_encode().
    • ID Tokens: verifyIdToken() now returns an array directly, rather than a ticket object with a getAttributes() method.
    // Access Tokens
    $accessToken = $client->getAccessToken();
    file_put_contents($credentialsPath, json_encode($accessToken));
    
    // ID Tokens
    $userData = $client->verifyIdToken($idToken);
    $userId = $userData['sub'];
  3. Migrate caching to PSR-6

    latest
    The library now uses PSR-6 for all caching operations. Google_Cache_Abstract has been removed. Existing cache classes like Google_Cache_Apc, Google_Cache_File, Google_Cache_Memcache, and Google_Cache_Null have been updated to implement Google\Auth\CacheInterface.
  4. Migrate authentication from Google_Auth_Abstract to Google_Client::authorize

    latest

    The Google_Auth_Abstract class and its methods sign and authenticatedRequest have been removed. They are replaced by Google_Client::authorize. This method accepts a GuzzleHttp\ClientInterface object and follows this authentication priority:

    1. Application Default Credentials (ADC): Triggered by calling Google_Client::useApplicationDefaultCredentials. It looks for the GOOGLE_APPLICATION_CREDENTIALS environment variable, then ~/.config/gcloud/application_default_credentials.json, and finally falls back to GCECredentials.
    2. API Key: If set via Google_Client::setDeveloperKey.
    3. Access Token: If set via Google_Client::setAccessToken.

    Note: The client will automatically refresh access tokens if an expired one is provided.

    // Instead of Google_Auth_Abstract::sign, use:
    $client->authorize($guzzleClient);
  5. Understand WP-Stateless operating modes

    latest

    WP-Stateless offers four distinct modes for handling media files, depending on your storage and delivery requirements:

    • Backup: Uploads media files to Google Storage but continues to serve local file URLs.
    • CDN: Copies media files to Google Storage and serves them directly from the GCS URLs.
    • Ephemeral: Stores and serves media files via Google Cloud Storage only. Local storage is used temporarily for processing and compatibility, but files are not permanently stored locally.
    • Stateless: Stores and serves media files via Google Cloud Storage only. Media files are not stored locally at all.
  6. Make API requests using generated service classes

    latest

    The library uses autogenerated classes that map to the APIs Explorer JSON structure. You can build requests in two ways:

    1. Array-based: Pass associative arrays to the service methods that mirror the JSON request body.
    2. Object-based: Use the generated request and query classes (e.g., Google\Service\Datastore\Query) and their setter methods.

    It is highly recommended to understand the JSON syntax of the target API before using these services.

    // Object-based approach example for Datastore
    $datastore = new Google\Service\Datastore($client);
    
    $request = new Google\Service\Datastore_RunQueryRequest();
    $query = new Google\Service\Datastore\Query();
    
    $order = new Google\Service\Datastore_PropertyOrder();
    $order->setDirection('descending');
    $property = new Google\Service\Datastore\PropertyReference();
    $property->setName('title');
    $order->setProperty($property);
    
    $query->setOrder([$order]);
    $kind = new Google\Service\Datastore\KindExpression();
    $kind->setName('Book');
    $query->setKinds([$kind]);
    $query->setLimit(10);
    
    $request->setQuery($query);
    $response = $datastore->projects->runQuery('YOUR_DATASET_ID', $request);
  7. Check and fix PHP coding style violations

    latest

    Use phpcs to identify coding style violations based on the project's ruleset, and phpcbf to automatically fix fixable violations.

    To check violations:

    vendor/bin/phpcs src --standard=style/ruleset.xml -np

    To automatically fix violations:

    vendor/bin/phpcbf src --standard=style/ruleset.xml
  8. Install the Google API Client via Composer

    latest

    The library now requires Composer for installation and autoloading. Instead of manual file inclusion, use Composer to manage dependencies and generate the autoloader.

    $ composer require google/apiclient:~2.0
    // Include the generated autoloader
    require_once 'vendor/autoload.php';
  9. Authenticate using Service Accounts

    latest

    Service accounts are used for server-to-server authentication. Note that some APIs (like YouTube Data API) do not support them.

    Option 1: Using Environment Variables Set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the path of your JSON key, then call $client->useApplicationDefaultCredentials().

    Option 2: Using a specific JSON key array Pass the credentials array directly to $client->setAuthConfig($jsonKey).

    Impersonation If you have domain-wide delegation, you can impersonate a user by calling $client->setSubject($user_email).

    // Option 1: Environment Variable
    putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
    $client = new Google\Client();
    $client->useApplicationDefaultCredentials();
    $client->addScope(Google\Service\Drive::DRIVE);
    
    // Option 2: Specific JSON key array
    $jsonKey = [
       'type' => 'service_account',
       // ...
    ];
    $client = new Google\Client();
    $client->setAuthConfig($jsonKey);
    
    // Impersonation
    $client->setSubject($user_to_impersonate);
  10. Request JSON responses for services that default to XML

    latest

    Some Google services return XML or other formats by default. To ensure the library receives a JSON response, add an alt key with the value json to the optional parameters array. This array is typically passed as the last argument to a method call.

    $opt_params = array(
      'alt' => "json"
    );
  11. Install the Google API Client Library via manual download

    latest

    If you do not use Composer, you can download the package directly from the Releases page.

    1. Download the file named google-api-php-client-[RELEASE_NAME].zip.
    2. Uncompress the zip file.
    3. Include the autoloader in your project:
    require_once '/path/to/google-api-php-client/vendor/autoload.php';