Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add standalone jw player adapter and api logic #10

Merged
merged 23 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
3dde99b
add standalone jw player adapter and api logic
manewc Sep 20, 2024
8c57df8
correct and update plugin point version
manewc Sep 20, 2024
31c3aeb
remove unnecessary origin modified date
manewc Sep 23, 2024
1bc6198
assign allowed null value to type declaration
manewc Sep 23, 2024
dcd3f22
refactor request arg retrieval for api requests
manewc Sep 23, 2024
6e57ffb
refactor logic to make the api calls more abstract
manewc Sep 23, 2024
5b66892
consolidate repeated logic and moved to a parent class
manewc Sep 24, 2024
8d24cc1
address phpcs sniffs
manewc Sep 26, 2024
1cfbed8
address phpstan errors
manewc Sep 26, 2024
4a728e9
check for function existence. PHPStan not checking the parent class m…
manewc Sep 26, 2024
400d6c6
strict parameter definition to conform to the wp_remote_get parameter…
manewc Sep 26, 2024
91d61c9
correct formatting
manewc Sep 26, 2024
55c36fc
Testing CI
srtfisher Sep 26, 2024
97b5f7b
Enable debug mode
srtfisher Sep 26, 2024
910760d
Fix action
srtfisher Sep 26, 2024
b15d7a2
Remove debug mode
srtfisher Sep 26, 2024
0bb364a
add unit test for the JW Player Adapter integration
manewc Sep 27, 2024
020ded3
provide base README documentation outlining class methods
manewc Oct 1, 2024
f54af24
implement php constructor property promotion
manewc Oct 7, 2024
e7e8ce0
ensure get_last_modified_date method is applied within adapter
manewc Oct 7, 2024
3ad4dd5
restrict setting last modified date to a DateTimeImmutable object
manewc Oct 7, 2024
7397082
ensure last modified date is a DateTimeImmutable instance, encapsulat…
manewc Oct 7, 2024
31a0135
update unit test to remove dependency class param
manewc Oct 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/all-pr-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ jobs:
uses: alleyinteractive/action-test-php@develop
with:
php-version: ${{ matrix.php-version }}
wordpress-host: 'false'
wordpress-version: 'false'
wordpress-version: 'latest'
skip-wordpress-install: 'true'
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use DateInterval;
use DateTimeImmutable;
use WP_Query;

add_action( 'plugins_loaded', function () => {
add_action( 'plugins_loaded', function () {
$sync_manager = Sync_Manager::init()
->with_adapter( new JW_Player_7_For_WP() )
->with_frequency( 'hourly' )
Expand Down
28 changes: 28 additions & 0 deletions src/adapters/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
## Class JW_Player

This class will implement the base `Adapter` interface. Instantiating this class with a `JW_Player_API` object will allow the request to be made to get the latest videos to be synced. This class may be used to work with a theme's custom integration with JW Player.

Example Integration:

```shell
\Alley\WP\WP_Video_Sync\Sync_Manager::init()
->with_adapter(
new \Alley\WP\WP_Video_Sync\Adapters\JW_Player(
new \Alley\WP\WP_Video_Sync\API\JW_Player_API(
[PROPERTY_ID],
[API_KEY]
)
)
)
->with_batch_size()
->with_frequency()
->with_callback(
function ( $video ) {
// Do something with the video.
}
);
```

## Class JW_Player_7_For_WP

This class is to work with the JW Player 7 for WordPress plugin.
20 changes: 3 additions & 17 deletions src/adapters/class-jw-player-7-for-wp.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,14 @@
namespace Alley\WP\WP_Video_Sync\Adapters;

use Alley\WP\WP_Video_Sync\Interfaces\Adapter;
use Alley\WP\WP_Video_Sync\Last_Modified_Date;
use DateTimeImmutable;
use stdClass;

/**
* JW Player 7 for WP Adapter. Supports both the free and premium versions of the plugin.
*/
class JW_Player_7_For_WP implements Adapter {
/**
* The date of the last modification to the last batch of videos.
*
* @var ?DateTimeImmutable
*/
private ?DateTimeImmutable $last_modified_date;

/**
* Fetches the date of the last modification to the last batch of videos.
*
* @return ?DateTimeImmutable
*/
public function get_last_modified_date(): ?DateTimeImmutable {
return $this->last_modified_date;
}
class JW_Player_7_For_WP extends Last_Modified_Date implements Adapter {

/**
* Fetches videos from JW Player that were modified after the provided DateTime.
Expand All @@ -56,7 +42,7 @@ public function get_videos( DateTimeImmutable $updated_after, int $batch_size ):
if ( isset( $videos[ count( $videos ) - 1 ]->last_modified ) ) {
$last_modified_date = DateTimeImmutable::createFromFormat( DATE_W3C, $videos[ count( $videos ) - 1 ]->last_modified );
if ( $last_modified_date instanceof DateTimeImmutable ) {
$this->last_modified_date = $last_modified_date;
$this->set_last_modified_date( $last_modified_date );
}
}

Expand Down
74 changes: 74 additions & 0 deletions src/adapters/class-jw-player.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* WP Video Sync: JW Player Adapter.
*
* @package wp-video-sync
*/

namespace Alley\WP\WP_Video_Sync\Adapters;

use Alley\WP\WP_Video_Sync\API\JW_Player_API;
use Alley\WP\WP_Video_Sync\Interfaces\Adapter;
use Alley\WP\WP_Video_Sync\Last_Modified_Date;
use DateTimeImmutable;

/**
* JW Player Adapter.
*/
class JW_Player extends Last_Modified_Date implements Adapter {

/**
* Constructor.
*
* @param string $api_key The API key.
* @param string $api_secret The API v2 secret key.
*/
public function __construct(
public readonly string $api_key,
public readonly string $api_secret
) {}

/**
* Fetches videos from JW Player that were modified after the provided DateTime.
*
* @param DateTimeImmutable $updated_after Return videos modified after this date.
* @param int $batch_size The number of videos to fetch in each batch.
*
* @return array<mixed> An array of video data objects.
*/
public function get_videos( DateTimeImmutable $updated_after, int $batch_size ): array {
$videos = (
new JW_Player_API(
$this->api_key,
$this->api_secret
)
)->get_videos_after(
$updated_after,
$batch_size
);

// Check for an API error.
if ( ! empty( $videos['error'] ) ) {
return [];
}

// Validate the media property.
if ( ! is_array( $videos['media'] ) ) {
return [];
}

// Attempt to set the last modified date.
if (
! empty( $videos['media'][ count( $videos['media'] ) - 1 ] )
&& isset( $videos['media'][ count( $videos['media'] ) - 1 ]->last_modified )
) {
$last_modified_date = DateTimeImmutable::createFromFormat( DATE_W3C, $videos['media'][ count( $videos['media'] ) - 1 ]->last_modified );
if ( $last_modified_date instanceof DateTimeImmutable ) {
$this->set_last_modified_date( $last_modified_date );
}
}

// Return the videos.
return ! empty( $videos['media'] ) ? $videos['media'] : [];
}
}
7 changes: 7 additions & 0 deletions src/api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Class JW_Player_API

This class is used for constructing an object for making an API call to the JW Player API. Simply pass your API key and API Secret on instantiation. Please reference the interface `API_Requester` for the required methods. This object may be used to pass to the `JW_Player` adapter.

## Class Request

This class will handle the generation of the URL for performing an API request. The API response will call the `parse_response` method which will subsequently call the `parse_success()` or `parse_error()` method of the `API_Requester` object that is used to construct this object.
134 changes: 134 additions & 0 deletions src/api/class-jw-player-api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php
/**
* WP Video Sync: JW Player API integration.
*
* @package wp-video-sync
*/

namespace Alley\WP\WP_Video_Sync\API;

use Alley\WP\WP_Video_Sync\Interfaces\API_Requester;
use Alley\WP\WP_Video_Sync\Last_Modified_Date;
use DateTimeImmutable;

/**
* JW Player API.
*/
class JW_Player_API extends Last_Modified_Date implements API_Requester {

/**
* The API URL.
*
* @var string
*/
public string $api_url = 'https://api.jwplayer.com/v2/sites';

/**
* The request URL.
*
* @var string
*/
public string $request_url;

/**
* Constructor.
*
* @param string $api_key The API key.
* @param string $api_secret The API v2 secret key.
*/
public function __construct(
public readonly string $api_key,
public readonly string $api_secret
) {}

/**
* Generate the request URL.
*
* @param string $last_modified_date The date of the last modification to the last batch of videos.
* @param int $batch_size The number of videos to fetch in each batch.
*/
public function set_request_url( string $last_modified_date, int $batch_size ): void {
$request_url = $this->api_url . '/' . $this->api_key . '/media/';

$this->request_url = add_query_arg(
[
'q' => 'last_modified:[' . $last_modified_date . ' TO *]',
'page' => 1,
'sort' => 'last_modified:asc',
'page_length' => $batch_size,
],
$request_url
);
}

/**
* Get the request URL.
*
* @return string
*/
public function get_request_url(): string {
return $this->request_url;
}

/**
* Get the request arguments.
*
* @return array<string, array<string, string>>
*/
public function get_request_args(): array {
return [
'headers' => [
'Authorization' => 'Bearer ' . $this->api_secret,
'Content-Type' => 'application/json',
],
];
}

/**
* Fetches videos from JW Player that were modified after the provided DateTime.
*
* @param DateTimeImmutable $updated_after Return videos modified after this date.
* @param int $batch_size The number of videos to fetch in each batch.
*
* @return array<mixed> An array of video data objects.
*/
public function get_videos_after( DateTimeImmutable $updated_after, int $batch_size ): array {
// Set the request URL based on the arguments.
$this->set_request_url(
$updated_after->format( 'Y-m-d' ),
$batch_size
);

// Perform the request.
return ( new Request( $this ) )->get();
}

/**
* Parse the API error response.
*
* @param array<mixed> $response_object The API response object.
*
* @return array<string, string>
*/
public function parse_error( array $response_object ): array {
return ! empty( $response_object['errors'] )
&& is_array( $response_object['errors'] )
&& isset( $response_object['errors'][0]->description )
? [ 'error' => $response_object['errors'][0]->description ]
: [];
}

/**
* Parse the API successful response.
*
* @param array<mixed> $response_object The API response object.
*
* @return array<string, mixed>
*/
public function parse_success( array $response_object ): array {
return ! empty( $response_object['media'] )
&& is_array( $response_object['media'] )
? [ 'media' => $response_object['media'] ]
: [];
}
}
Loading
Loading