-
Notifications
You must be signed in to change notification settings - Fork 24
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
major refactoring to use video provider #54
Changes from 4 commits
5723317
455140d
1356820
390f454
884bb32
3318d3e
3fddea8
8579e4d
80ca179
437c8f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
class Provider_Bootstrap { | ||
|
||
protected $providers = []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely willing to drop 5.2, but this array shorthand would necessitate dropping 5.3 as well. Also tempting, especially with this plugin not being one of our more popular ones, even with useful utility. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a habit...let me know where you want to go with versioning and I'll adjust accordingly. |
||
|
||
public function add_provider( $provider ) { | ||
if ( ! is_a( $provider, Video_Provider::class ) ) { | ||
return new WP_Error( __( 'The passed provider is not a Provider Object', 'automatic-featured-images-from-videos' ) ); | ||
} | ||
|
||
$this->providers[] = $provider; | ||
} | ||
|
||
public function video_providers() { | ||
/** | ||
* Allow developers to pass in custom video providers. | ||
* Video providers should extend the Video_Provider class. | ||
* | ||
* @since 1.1.1 | ||
* | ||
* @param Video_Provider $value An object that is of a class that extends Video_Provider. | ||
*/ | ||
return apply_filters( 'wds_video_providers', $this->providers ); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Feels like we have a slight disconnect with how to add providers. We get to do so with the add_provider method, everyone else would need to use the filter. Is there a way we could work things to be the same routine for everyone? Perhaps everyone uses the filter version. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Totally fair point. I've refactored based on this note. |
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
abstract class Video_Provider { | ||
|
||
protected $id; | ||
|
||
abstract public function match_content( $content ); | ||
|
||
abstract public function get_video_id(); | ||
|
||
abstract public function get_video_thumbnail_url(); | ||
|
||
abstract public function get_video_url(); | ||
|
||
abstract public function get_video_embed_url(); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
class Vimeo extends Video_Provider { | ||
|
||
private $thumbnail_url; | ||
private $url; | ||
private $embed_url; | ||
|
||
|
||
public function match_content( $content ) { | ||
if ( preg_match( '#\/\/(.+\.)?(vimeo\.com)\/(\d*)#', $content, $vimeo_matches ) ) { | ||
$this->id = $vimeo_matches[3]; | ||
$this->get_vimeo(); | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private function get_vimeo() { | ||
$vimeo_data = wp_remote_get( 'http://www.vimeo.com/api/v2/video/' . (int) $this->id . '.php' ); | ||
if ( 200 === wp_remote_retrieve_response_code( $vimeo_data ) ) { | ||
$response = unserialize( $vimeo_data['body'] ); | ||
$this->thumbnail_url = isset( $response[0]['thumbnail_large'] ) ? $response[0]['thumbnail_large'] : false; | ||
$this->url = $response[0]['url']; | ||
$this->embed_url = 'https://player.vimeo.com/video/' . $this->id; | ||
} | ||
} | ||
|
||
public function get_video_thumbnail_url() { | ||
return $this->thumbnail_url; | ||
} | ||
|
||
public function get_video_url() { | ||
return $this->url; | ||
} | ||
|
||
public function get_video_embed_url() { | ||
return $this->embed_url; | ||
} | ||
|
||
public function get_video_id() { | ||
return $this->id; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
class Youtube extends Video_Provider { | ||
|
||
public function match_content( $content ) { | ||
if ( preg_match( '#\/\/(www\.)?(youtu|youtube|youtube-nocookie)\.(com|be)\/(watch|embed)?\/?(\?v=)?([a-zA-Z0-9\-\_]+)#', $content, $youtube_matches ) ) { | ||
$this->id = $youtube_matches[6]; | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function get_video_thumbnail_url() { | ||
$video_thumbnail_url_string = 'http://img.youtube.com/vi/%s/%s'; | ||
|
||
$video_check = wp_remote_head( 'https://www.youtube.com/oembed?format=json&url=http://www.youtube.com/watch?v=' . $this->id ); | ||
if ( 200 === wp_remote_retrieve_response_code( $video_check ) ) { | ||
$remote_headers = wp_remote_head( | ||
sprintf( | ||
$video_thumbnail_url_string, | ||
$this->id, | ||
'maxresdefault.jpg' | ||
) | ||
); | ||
$video_thumbnail_url = ( 404 === wp_remote_retrieve_response_code( $remote_headers ) ) ? | ||
sprintf( | ||
$video_thumbnail_url_string, | ||
$this->id, | ||
'hqdefault.jpg' | ||
) : | ||
sprintf( | ||
$video_thumbnail_url_string, | ||
$this->id, | ||
'maxresdefault.jpg' | ||
); | ||
return $video_thumbnail_url; | ||
} | ||
|
||
return ''; | ||
} | ||
|
||
public function get_video_url() { | ||
return 'https://www.youtube.com/watch?v=' . $this->id; | ||
} | ||
|
||
public function get_video_embed_url() { | ||
return 'https://www.youtube.com/embed/' . $this->id; | ||
} | ||
|
||
public function get_video_id() { | ||
return $this->id; | ||
} | ||
|
||
} |
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.
You have a WP_Error return possible for this method, but don't do anything with that potential return value.
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.
Yeah...not sure what to do with that. We don't usually raise exceptions in WP? Do you have thoughts, preferences?