generated from alleyinteractive/create-wordpress-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from 18 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 8c57df8
correct and update plugin point version
manewc 31c3aeb
remove unnecessary origin modified date
manewc 1bc6198
assign allowed null value to type declaration
manewc dcd3f22
refactor request arg retrieval for api requests
manewc 6e57ffb
refactor logic to make the api calls more abstract
manewc 5b66892
consolidate repeated logic and moved to a parent class
manewc 8d24cc1
address phpcs sniffs
manewc 1cfbed8
address phpstan errors
manewc 4a728e9
check for function existence. PHPStan not checking the parent class m…
manewc 400d6c6
strict parameter definition to conform to the wp_remote_get parameter…
manewc 91d61c9
correct formatting
manewc 55c36fc
Testing CI
srtfisher 97b5f7b
Enable debug mode
srtfisher 910760d
Fix action
srtfisher b15d7a2
Remove debug mode
srtfisher 0bb364a
add unit test for the JW Player Adapter integration
manewc 020ded3
provide base README documentation outlining class methods
manewc f54af24
implement php constructor property promotion
manewc e7e8ce0
ensure get_last_modified_date method is applied within adapter
manewc 3ad4dd5
restrict setting last modified date to a DateTimeImmutable object
manewc 7397082
ensure last modified date is a DateTimeImmutable instance, encapsulat…
manewc 31a0135
update unit test to remove dependency class param
manewc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?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\API\Request; | ||
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 { | ||
|
||
/** | ||
* The JW Player API. | ||
* | ||
* @var JW_Player_API | ||
*/ | ||
public JW_Player_API $jw_player_api; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param JW_Player_API $api Instance of the JW Player API object. | ||
*/ | ||
public function __construct( JW_Player_API $api ) { | ||
$this->jw_player_api = $api; | ||
} | ||
|
||
/** | ||
* 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 { | ||
// Set the request URL based on the arguments. | ||
$this->jw_player_api->set_request_url( | ||
$updated_after->format( 'Y-m-d' ), | ||
$batch_size | ||
); | ||
|
||
// Perform the request. | ||
$videos = ( new Request( $this->jw_player_api ) )->get(); | ||
|
||
// 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 ) | ||
) { | ||
$this->set_last_modified_date( $videos['media'][ count( $videos ) - 1 ]->last_modified ); | ||
} | ||
|
||
// Return the videos. | ||
return ! empty( $videos['media'] ) ? $videos['media'] : []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?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; | ||
|
||
/** | ||
* JW Player API. | ||
*/ | ||
class JW_Player_API implements API_Requester { | ||
|
||
/** | ||
* The API URL. | ||
* | ||
* @var string | ||
*/ | ||
public string $api_url = 'https://api.jwplayer.com/v2/sites'; | ||
|
||
/** | ||
* The API public key. | ||
* | ||
* @var string | ||
*/ | ||
public string $api_key; | ||
|
||
/** | ||
* The API v2 secret key. | ||
* | ||
* @var string | ||
*/ | ||
public string $api_secret; | ||
|
||
/** | ||
* The request URL. | ||
* | ||
* @var string | ||
*/ | ||
public string $request_url; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param string $api_key The API key. | ||
* @param string $api_secret The API secret. | ||
*/ | ||
public function __construct( string $api_key, string $api_secret ) { | ||
$this->api_key = $api_key; | ||
$this->api_secret = $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', | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* 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'] ] | ||
: []; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make this a modern PHP constructor: