Skip to content

Commit

Permalink
PHPCS style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
underdude committed Oct 22, 2018
1 parent d53edea commit fd06889
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 73 deletions.
1 change: 1 addition & 0 deletions flowplayer-ovp.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
/**
* Flowplayer OVP WordPress plugin
*
* @package Flowplayer OVP
* @author Janne Ala-Äijälä <[email protected]>
Expand Down
104 changes: 58 additions & 46 deletions lib/admin-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,85 +4,97 @@
die;
}

/**
* Parse ajax queries
*/
function flowplayer_ovp_query_attachments( $args ) {
if ( isset( $_REQUEST['query']['flowplayer'] ) ) {
$settings = flowplayer_ovp_get_settings();

// Fetch attachments
// Fetch attachments.
$query = array(
'api_key' => $settings['api_key'],
'page_size' => filter_var($_REQUEST['query']['posts_per_page'], FILTER_SANITIZE_NUMBER_INT),
'page' => filter_var($_REQUEST['query']['paged'], FILTER_SANITIZE_NUMBER_INT),
'page_size' => filter_var( $_REQUEST['query']['posts_per_page'], FILTER_SANITIZE_NUMBER_INT ),
'page' => filter_var( $_REQUEST['query']['paged'], FILTER_SANITIZE_NUMBER_INT ),
);

if ( isset($_REQUEST['query']['s'] ) ) {
if ( isset( $_REQUEST['query']['s'] ) ) {
$query['search'] = urlencode( sanitize_text_field( $_REQUEST['query']['s'] ) );
}

$request = wp_remote_get(
sprintf(
'https://api.flowplayer.com/ovp/web/video/v2/site/%s.json?%s',
$settings['site_id'],
build_query($query)
build_query( $query )
)
);

if( is_wp_error( $request ) ) {
return false; // Bail early
}
$body = wp_remote_retrieve_body( $request );
if ( is_wp_error( $request ) ) {
return false; // Bail early.
}
$body = wp_remote_retrieve_body( $request );
$data = json_decode( $body );

// Map attachments to expected format
$videos = array_map('flowplayer_ovp_video_to_media_attachment', $data->videos);
// Map attachments to expected format.
$videos = array_map( 'flowplayer_ovp_video_to_media_attachment', $data->videos );

wp_send_json_success($videos);
wp_send_json_success( $videos );
}

return $args;
}
add_filter( 'ajax_query_attachments_args', 'flowplayer_ovp_query_attachments', 99);
add_filter( 'ajax_query_attachments_args', 'flowplayer_ovp_query_attachments', 99 );

/**
* Convert OVP API response video entity to WordPress attachment
*/
function flowplayer_ovp_video_to_media_attachment( $video ) {
return array(
'type' => 'remote',
'id' => $video->id,
'title' => $video->name,
'filename' => $video->name,
'url' => sprintf(
'https://play.flowplayer.com/api/video/embed.jsp?id=%s',
$video->id
),
'link' => false,
'alt' => $video->name,
'author' => '',
'description' => $video->description,
'caption' => '',
'name' => $video->name,
'status' => 'inherit',
'uploadedTo' => 0,
'date' => strtotime($video->created_at) * 1000,
'modified' => strtotime($video->created_at) * 1000,
'menuOrder' => 0,
'mime' => 'remote/flowplayer',
'subtype' => "flowplayer",
'icon' => $video->images->thumbnail_url,
'dateFormatted' => mysql2date(get_option('date_format'), $video->created_at),
'nonces' => array(
'update' => false,
'delete' => false,
),
'editLink' => false,
'type' => 'remote',
'id' => $video->id,
'title' => $video->name,
'filename' => $video->name,
'url' => sprintf(
'https://play.flowplayer.com/api/video/embed.jsp?id=%s',
$video->id
),
'link' => false,
'alt' => $video->name,
'author' => '',
'description' => $video->description,
'caption' => '',
'name' => $video->name,
'status' => 'inherit',
'uploadedTo' => 0,
'date' => strtotime( $video->created_at ) * 1000,
'modified' => strtotime( $video->created_at ) * 1000,
'menuOrder' => 0,
'mime' => 'remote/flowplayer',
'subtype' => 'flowplayer',
'icon' => $video->images->thumbnail_url,
'dateFormatted' => mysql2date( get_option( 'date_format' ), $video->created_at ),
'editLink' => false,
'nonces' => array(
'update' => false,
'delete' => false,
),
);
}

/**
* Add ajax endpoint for loading player configs
*/
function flowplayer_ovp_ajax_load_players() {
$players = flowplayer_ovp_fetch_players();

wp_send_json_success($players);
wp_send_json_success( $players );
}
add_action( 'wp_ajax_flowplayer_ovp_load_players', 'flowplayer_ovp_ajax_load_players' );

/**
* Fetch players from OVP API
*/
function flowplayer_ovp_fetch_players() {
$settings = flowplayer_ovp_get_settings();

Expand All @@ -94,16 +106,16 @@ function flowplayer_ovp_fetch_players() {
)
);

if( is_wp_error( $request ) ) {
return false; // Bail early
if ( is_wp_error( $request ) ) {
return false; // Bail early.
}
$body = wp_remote_retrieve_body( $request );
$data = json_decode( $body );

$players = [];

foreach ($data->players as $player) {
$players[$player->id] = $player->name;
foreach ( $data->players as $player ) {
$players[ $player->id ] = $player->name;
}

return $players;
Expand Down
12 changes: 8 additions & 4 deletions lib/admin-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
die;
}

// Enqueue admin js
/**
* Enqueue admin js
*/
function flowplayer_ovp_scripts() {
$settings = flowplayer_ovp_get_settings();

if($settings['site_id'] !== '') {
if ( $settings['site_id'] !== '' ) {
wp_enqueue_script(
'flowplayer-ovp-js',
plugin_dir_url( dirname( __FILE__ ) ) . 'js/admin.js',
Expand All @@ -20,7 +22,9 @@ function flowplayer_ovp_scripts() {
}
add_action( 'print_media_templates', 'flowplayer_ovp_scripts' );

// Enqueue extra admin templates
/**
* Enqueue extra admin templates
*/
function flowplayer_ovp_print_media_templates() {
$players = flowplayer_ovp_fetch_players();
?>
Expand All @@ -34,7 +38,7 @@ function flowplayer_ovp_print_media_templates() {
>
<# _.each(data.players, function(label, id) { #>
<option value="{{ id }}">{{ label }}</option>
<# }); #>
<# }); #>
</select>
</label>

Expand Down
29 changes: 18 additions & 11 deletions lib/embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,27 @@
die;
}

/**
* Register the custom oembed handler
*/
function flowplayer_ovp_register_embed_handler() {
wp_embed_register_handler(
'flowplayer_ovp',
'#https?://play\.flowplayer\.com/api/video/embed\.jsp\?id=([a-zA-Z0-9_-]+)&(?:amp;)?pi=([a-zA-Z0-9_-]+)#i',
'flowplayer_ovp_embed_handler'
);
wp_embed_register_handler(
'flowplayer_ovp',
'#https?://play\.flowplayer\.com/api/video/embed\.jsp\?id=([a-zA-Z0-9_-]+)&(?:amp;)?pi=([a-zA-Z0-9_-]+)#i',
'flowplayer_ovp_embed_handler'
);
}
add_action( 'init', 'flowplayer_ovp_register_embed_handler' );

/**
* Convert the recognised embed url to the actual embed code
*/
function flowplayer_ovp_embed_handler( $matches, $attr, $url, $rawattr ) {
$embed = sprintf(
'<div class="flowplayer-embed-container" style="position: relative; padding-bottom: 56.25%%; height: 0; overflow: hidden; max-width:100%%;"> <iframe style="position: absolute; top: 0; left: 0; width: 100%%; height: 100%%;" webkitAllowFullScreen mozallowfullscreen allowfullscreen src="https://ljsp.lwcdn.com/api/video/embed.jsp?id=%1$s&pi=%2$s" title="0" byline="0" portrait="0" width="640" height="360" frameborder="0" allow="autoplay"></iframe> </div>',
esc_attr($matches[1]),
esc_attr($matches[2])
);
return apply_filters( 'embed_flowplayer_ovp', $embed, $matches, $attr, $url, $rawattr );
$embed = sprintf(
'<div class="flowplayer-embed-container" style="position: relative; padding-bottom: 56.25%%; height: 0; overflow: hidden; max-width:100%%;"> <iframe style="position: absolute; top: 0; left: 0; width: 100%%; height: 100%%;" webkitAllowFullScreen mozallowfullscreen allowfullscreen src="https://ljsp.lwcdn.com/api/video/embed.jsp?id=%1$s&pi=%2$s" title="0" byline="0" portrait="0" width="640" height="360" frameborder="0" allow="autoplay"></iframe> </div>',
esc_attr( $matches[1] ),
esc_attr( $matches[2] )
);

return apply_filters( 'embed_flowplayer_ovp', $embed, $matches, $attr, $url, $rawattr );
}
38 changes: 28 additions & 10 deletions lib/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
die;
}

function flowplayer_ovp_add_admin_menu( ) {
/**
* Register settings page
*/
function flowplayer_ovp_add_admin_menu() {
add_options_page(
'Flowplayer OVP Settings',
'Flowplayer OVP',
Expand All @@ -15,7 +18,10 @@ function flowplayer_ovp_add_admin_menu( ) {
}
add_action( 'admin_menu', 'flowplayer_ovp_add_admin_menu' );

function flowplayer_ovp_settings_init( ) {
/**
* Register settings fields
*/
function flowplayer_ovp_settings_init() {
register_setting( 'flowplayer_ovp_settings', 'flowplayer_ovp_settings' );

add_settings_section(
Expand All @@ -31,7 +37,7 @@ function flowplayer_ovp_settings_init( ) {
'flowplayer_ovp_settings_field_render',
'flowplayer_ovp_settings',
'flowplayer_ovp_keys',
['field_id' => 'flowplayer_ovp_site_id']
[ 'field_id' => 'flowplayer_ovp_site_id' ]
);

add_settings_field(
Expand All @@ -40,25 +46,34 @@ function flowplayer_ovp_settings_init( ) {
'flowplayer_ovp_settings_field_render',
'flowplayer_ovp_settings',
'flowplayer_ovp_keys',
['field_id' => 'flowplayer_ovp_api_key']
[ 'field_id' => 'flowplayer_ovp_api_key' ]
);
}
add_action( 'admin_init', 'flowplayer_ovp_settings_init' );

/**
* Renders single setting text field
*/
function flowplayer_ovp_settings_field_render( $args ) {
$options = get_option( 'flowplayer_ovp_settings' );
$options = get_option( 'flowplayer_ovp_settings' );
$field_id = $args['field_id'];
$defval = is_array($options) && array_key_exists($field_id, $options) ? $options[$field_id] : '';
$defval = is_array( $options ) && array_key_exists( $field_id, $options ) ? $options[ $field_id ] : '';
?>
<input type='text' class="regular-text" name='flowplayer_ovp_settings[<?php print $field_id; ?>]' value='<?php echo $defval; ?>'>
<?php
}

function flowplayer_ovp_settings_section_callback( ) {
/**
* Render section
*/
function flowplayer_ovp_settings_section_callback() {
echo __( 'You can find these keys from your <a href="https://flowplayer.com/app/workspace/settings/publishing" target="blank">Flowplayer OVP workspace settings</a>, by clicking <em>API key</em> link from the top right corner.', 'flowplayer_ovp' );
}

function flowplayer_ovp_options_page( ) {
/**
* Settings page template
*/
function flowplayer_ovp_options_page() {
?>
<form action='options.php' method='post'>

Expand All @@ -74,12 +89,15 @@ function flowplayer_ovp_options_page( ) {
<?php
}

/**
* Get all flowplayer_ovp settings
*/
function flowplayer_ovp_get_settings() {
$options = get_option( 'flowplayer_ovp_settings' );

return [
'site_id' => array_key_exists('flowplayer_ovp_site_id', $options) ? $options['flowplayer_ovp_site_id'] : '',
'api_key' => array_key_exists('flowplayer_ovp_api_key', $options) ? $options['flowplayer_ovp_api_key'] : '',
'site_id' => array_key_exists( 'flowplayer_ovp_site_id', $options ) ? $options['flowplayer_ovp_site_id'] : '',
'api_key' => array_key_exists( 'flowplayer_ovp_api_key', $options ) ? $options['flowplayer_ovp_api_key'] : '',
];
}

10 changes: 8 additions & 2 deletions codesniffer.ruleset.xml → phpcs.ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
<description>A custom set of code standard rules to check for Flowplayer OVP.</description>

<!-- Include the WordPress ruleset, with exclusions. -->
<rule ref="WordPress-Core" />
<rule ref="WordPress-Docs" />
<rule ref="WordPress-Core">
<exclude name="WordPress.PHP.YodaConditions" />
</rule>
<rule ref="WordPress-Docs">
<exclude name="Squiz.Commenting.FileComment.Missing" />
<exclude name="Squiz.Commenting.FunctionComment.MissingParamTag" />
</rule>

</ruleset>

0 comments on commit fd06889

Please sign in to comment.