Skip to content

Commit

Permalink
Make the adapter responsible for keeping track of the last modified date
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinfodness committed Jul 23, 2024
1 parent 8243688 commit fe1f9bc
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 8 deletions.
41 changes: 39 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Contributors: alleyinteractive

Tags: alleyinteractive, wp-video-sync

Stable tag: 0.0.0
Stable tag: 0.1.0

Requires at least: 6.6

Expand All @@ -18,6 +18,10 @@ License: GPL v2 or later

Sync videos from a hosting provider to WordPress.

Runs a scheduled task to sync videos from a supported video hosting provider to WordPress in batches based on the last modified date of the video. Implementers are responsible for installing and configuring a compatible plugin, choosing it as an adapter, and defining the callback that will be run for each video, which will be responsible for performing any post creations or updates in WordPress.

This plugin is a great way to sync videos uploaded to a hosting provider (such as JW Player) to WordPress, such that the video itself remains on the hosting provider, but the video can be displayed in WordPress using a player block or shortcode, appears at its own unique URL, and can be included in search results.

## Installation

You can install the package via Composer:
Expand All @@ -31,11 +35,44 @@ composer require alleyinteractive/wp-video-sync
Activate the plugin in WordPress and use it like so:

```php
$video_sync = new Alley\WP\WP_Video_Sync\Sync_Manager()->with_jw_player_7_for_wp();
use Alley\WP\WP_Video_Sync\Adapters\JW_Player_7_For_WP;
use Alley\WP\WP_Video_Sync\Sync_Manager;
use DateTimeImmutable;
use WP_Query;

$sync_manager = Sync_Manager::init()
->with_adapter( new JW_Player_7_For_WP() )
->with_frequency( 'hourly' )
->with_callback(
function ( $video ) {
$existing_video = new WP_Query( [ 'meta_key' => 'jwplayer_id', 'meta_value' => $video->id ] );
$existing_id = $existing_video->posts[0]->ID ?? 0;
wp_insert_post(
[
'ID' => $existing_id,
'post_title' => $video->metadata->title,
'post_date' => DateTimeImmutable::createFromFormat( DATE_W3C, $video->created )->format( 'Y-m-d H:i:s' ),
'post_modified' => DateTimeImmutable::createFromFormat( DATE_W3C, $video->last_modified )->format( 'Y-m-d H:i:s' ),
'meta_input' => [
'jwplayer_id' => $video->id,
],
]
);
}
);
```

This will configure the plugin to import a batch of 100 videos every hour from JW Player, sorted by least to most recently updated, starting with the date and time of the last video that was updated. If videos have already been imported (as identified by the postmeta value saved for the unique video ID) they will be updated rather than created. New videos will be created. The example code above uses the `post` post type for this purpose, but the code could easily be adapted to use a custom post type. Additionally, the post content could be set to include a Gutenberg block or a shortcode for a player.

### Supported Adapters

As of now, the plugin only supports JW Player 7 for WordPress (both the free and premium versions). Other adapters may be added in the future.

#### JW Player 7 for WordPress

- Requires the [JW Player 7 for WordPress](https://wordpress.org/plugins/jw-player-7-for-wp/) plugin to be installed, activated, and properly configured with access credentials. Also supports the premium version.
- The video object in the callback is a `stdClass` with the properties described in the `media` object under response code `200` in [the JW Player API documentation for the media list endpoint](https://docs.jwplayer.com/platform/reference/get_v2-sites-site-id-media).

## Releasing the Plugin

New versions of this plugin will be created as releases in GitHub once ready.
Expand Down
28 changes: 27 additions & 1 deletion src/adapters/class-jw-player-7-for-wp.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@
* 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;
}

/**
* Fetches videos from JW Player that were modified after the provided DateTime.
*
Expand All @@ -32,7 +48,17 @@ public function get_videos( DateTimeImmutable $updated_after ): array {
$updated_after->format( 'Y-m-d' )
)
);
return $result->media ?? [];
$videos = $result->media ?? [];

// Attempt to set the last modified date.
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;
}
}

return $videos;
}

return [];
Expand Down
8 changes: 3 additions & 5 deletions src/class-sync-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,9 @@ public function sync_videos(): void {
}

// Try to update the last sync time to the last modified time of the last video that was processed.
if ( isset( $video->last_modified ) && is_string( $video->last_modified ) ) {
$check = DateTimeImmutable::createFromFormat( DATE_W3C, $video->last_modified );
if ( $check instanceof DateTimeImmutable ) {
update_option( self::LAST_SYNC_OPTION, $video->last_modified );
}
$next_last_modified = $this->adapter->get_last_modified_date();
if ( $next_last_modified instanceof DateTimeImmutable ) {
update_option( self::LAST_SYNC_OPTION, $next_last_modified->format( DATE_W3C ) );
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/interfaces/adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
* Defines an interface that all adapters must implement.
*/
interface Adapter {
/**
* Fetches the date of the last modification to the last batch of videos.
*
* @return ?DateTimeImmutable
*/
public function get_last_modified_date(): ?DateTimeImmutable;

/**
* Fetches videos from the provider that were modified after the provided DateTime.
*
Expand Down

0 comments on commit fe1f9bc

Please sign in to comment.