true,
'width' => true,
'height' => true,
'frameborder' => true,
'name' => true,
'src' => true,
'id' => true,
'class' => true,
'style' => true,
'scrolling' => true,
'marginwidth' => true,
'marginheight' => true,
);
return $allowed_tags;
}
/*
* Whether the current user has a specific capability.
*
* @since 1.0.0
* @param string $capability Capability name.
* @param int $post_id Optional. ID of the specific object to check against if
`$capability` is a "meta" cap.
* @return bool True if the current user has the capability, false if not.
*/
function aiovg_current_user_can( $capability, $post_id = 0 ) {
$user_id = get_current_user_id();
// If editing, deleting, or reading a video, get the post and post type object
if ( 'edit_aiovg_video' == $capability || 'delete_aiovg_video' == $capability || 'read_aiovg_video' == $capability ) {
$post = get_post( $post_id );
$post_type = get_post_type_object( $post->post_type );
// If editing a video, assign the required capability
if ( 'edit_aiovg_video' == $capability ) {
if ( $user_id == $post->post_author ) {
$capability = 'edit_aiovg_videos';
} else {
$capability = 'edit_others_aiovg_videos';
}
}
// If deleting a video, assign the required capability
elseif ( 'delete_aiovg_video' == $capability ) {
if ( $user_id == $post->post_author ) {
$capability = 'delete_aiovg_videos';
} else {
$capability = 'delete_others_aiovg_videos';
}
}
// If reading a private video, assign the required capability
elseif ( 'read_aiovg_video' == $capability ) {
if ( 'private' != $post->post_status ) {
$capability = 'read';
} elseif ( $user_id == $post->post_author ) {
$capability = 'read';
} else {
$capability = 'read_private_aiovg_videos';
}
}
}
return current_user_can( $capability );
}
/**
* Delete category attachments.
*
* @since 1.0.0
* @param int $term_id Term ID.
*/
function aiovg_delete_category_attachments( $term_id ) {
$image_id = get_term_meta( $term_id, 'image_id', true );
if ( ! empty( $image_id ) ) wp_delete_attachment( $image_id, true );
}
/**
* Delete video attachments.
*
* @since 1.0.0
* @param int $post_id Post ID.
*/
function aiovg_delete_video_attachments( $post_id ) {
$mp4_id = get_post_meta( $post_id, 'mp4_id', true );
if ( ! empty( $mp4_id ) ) wp_delete_attachment( $mp4_id, true );
$webm_id = get_post_meta( $post_id, 'webm_id', true );
if ( ! empty( $webm_id ) ) wp_delete_attachment( $webm_id, true );
$ogv_id = get_post_meta( $post_id, 'ogv_id', true );
if ( ! empty( $ogv_id ) ) wp_delete_attachment( $ogv_id, true );
$image_id = get_post_meta( $post_id, 'image_id', true );
if ( ! empty( $image_id ) ) wp_delete_attachment( $image_id, true );
$tracks = get_post_meta( $post_id, 'track' );
if ( count( $tracks ) ) {
foreach ( $tracks as $key => $track ) {
if ( 'src_id' == $key ) wp_delete_attachment( (int) $track['src_id'], true );
}
}
}
/**
* Get attachment ID of the given URL.
*
* @since 1.0.0
* @param string $url Media file URL.
* @param string $media "image" or "video". Type of the media.
* @return int Attachment ID on success, 0 on failure.
*/
function aiovg_get_attachment_id( $url, $media = 'image' ) {
$attachment_id = 0;
if ( empty( $url ) ) {
return $attachment_id;
}
if ( 'image' == $media ) {
$dir = wp_upload_dir();
if ( false !== strpos( $url, $dir['baseurl'] . '/' ) ) { // Is URL in uploads directory?
$file = basename( $url );
$query_args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'fields' => 'ids',
'meta_query' => array(
array(
'value' => $file,
'compare' => 'LIKE',
'key' => '_wp_attachment_metadata',
),
)
);
$query = new WP_Query( $query_args );
if ( $query->have_posts() ) {
foreach ( $query->posts as $post_id ) {
$meta = wp_get_attachment_metadata( $post_id );
$original_file = basename( $meta['file'] );
$cropped_image_files = wp_list_pluck( $meta['sizes'], 'file' );
if ( $original_file === $file || in_array( $file, $cropped_image_files ) ) {
$attachment_id = $post_id;
break;
}
}
}
}
} else {
$url = wp_make_link_relative( $url );
if ( ! empty( $url ) ) {
global $wpdb;
$query = $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE guid RLIKE %s", $url );
$attachment_id = $wpdb->get_var( $query );
}
}
return $attachment_id;
}
/**
* Get the category page URL.
*
* @since 1.0.0
* @param object $term The term object.
* @return string Category page URL.
*/
function aiovg_get_category_page_url( $term ) {
$page_settings = get_option( 'aiovg_page_settings' );
$url = '/';
if ( $page_settings['category'] > 0 ) {
$url = get_permalink( $page_settings['category'] );
if ( '' != get_option( 'permalink_structure' ) ) {
$url = user_trailingslashit( trailingslashit( $url ) . $term->slug );
} else {
$url = add_query_arg( 'aiovg_category', $term->slug, $url );
}
}
return $url;
}
/*
* Get image from the Iframe Embed Code.
*
* @since 1.0.0
* @param string $embedcode Iframe Embed Code.
* @return string $url Image URL.
*/
function aiovg_get_embedcode_image_url( $embedcode ) {
$document = new DOMDocument();
$document->loadHTML( $embedcode );
$iframes = $document->getElementsByTagName( 'iframe' );
$src = $iframes->item(0)->getAttribute( 'src' );
$url = aiovg_get_youtube_image_url( $src );
if ( empty( $url ) ) {
$url = aiovg_get_vimeo_image_url( $src );
}
return $url;
}
/**
* Get the video excerpt.
*
* @since 1.0.0
* @param int $char_length Excerpt length.
* @return string $content Excerpt content.
*/
function aiovg_get_excerpt( $char_length = 55 ) {
global $post;
$excerpt = wp_strip_all_tags( $post->post_content, true );
$char_length++;
$content = '';
if ( mb_strlen( $excerpt ) > $char_length ) {
$subex = mb_substr( $excerpt, 0, $char_length - 5 );
$exwords = explode( ' ', $subex );
$excut = - ( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) );
if ( $excut < 0 ) {
$content = mb_substr( $subex, 0, $excut );
} else {
$content = $subex;
}
$content .= '[...]';
} else {
$content = $excerpt;
}
return $content;
}
/*
* Get image URL using the attachment ID.
*
* @since 1.0.0
* @param integer $id Attachment ID.
* @param string $size Size of the image.
* @param string $default Default image URL.
* @param string $type "gallery" or "player".
* @return string $url Image URL.
*/
function aiovg_get_image_url( $id, $size = "large", $default = '', $type = 'gallery' ) {
$url = '';
// Get image from attachment
if ( $id ) {
$attributes = wp_get_attachment_image_src( (int) $id, $size );
if ( ! empty( $attributes ) ) {
$url = $attributes[0];
}
}
// Set default image if empty
if ( 'gallery' == $type && empty( $default ) ) {
$default = AIOVG_PLUGIN_URL . 'public/assets/images/placeholder-image.png';
}
if ( empty( $url ) ) {
$url = $default;
}
// Return image url
return $url;
}
/*
* Get message to display based on the $type input.
*
* @since 1.0.0
* @param string $type Type of the message.
* @return string $message Message to display.
*/
function aiovg_get_message( $type ) {
$message = '';
switch ( $type ) {
case 'empty':
$message = __( 'No Items found.', 'all-in-one-video-gallery' );
break;
}
return $message;
}
/**
* Get current page number.
*
* @since 1.0.0
* @return int $paged The current page number.
*/
function aiovg_get_page_number() {
global $paged;
if ( get_query_var( 'paged' ) ) {
$paged = get_query_var( 'paged' );
} elseif ( get_query_var( 'page' ) ) {
$paged = get_query_var( 'page' );
} else {
$paged = 1;
}
return absint( $paged );
}
/**
* Get player HTML.
*
* @since 1.0.0
* @param int $post_id Post ID.
* @param array $atts Player configuration data.
* @return string $html Player HTML.
*/
function aiovg_get_player_html( $post_id = 0, $atts = array() ) {
// Vars
$player_settings = get_option( 'aiovg_player_settings' );
$attributes = array_merge( array(
'width' => $player_settings['width'],
'ratio' => $player_settings['ratio'],
'mp4' => '',
'webm' => '',
'ogv' => '',
'youtube' => '',
'vimeo' => '',
'poster' => '',
'autoplay' => '',
'loop' => '',
'playpause' => '',
'current' => '',
'progress' => '',
'duration' => '',
'tracks' => '',
'volume' => '',
'fullscreen' => ''
), $atts );
$url = aiovg_get_player_page_url( $post_id, $attributes );
// Process output
$html = '';
if ( ! empty( $url ) ) {
$iframe = sprintf( '', esc_url( $url ) );
$html = sprintf(
'
',
! empty( $attributes['width'] ) ? (int) $attributes['width'] . 'px' : '100%',
! empty( $attributes['ratio'] ) ? (float) $attributes['ratio'] . '%' : '56.25%',
$iframe
);
}
return $html;
}
/**
* Get player page URL.
*
* @since 1.0.0
* @param int $post_id Post ID.
* @param array $atts Player configuration data.
* @return string $url Player page URL.
*/
function aiovg_get_player_page_url( $post_id = 0, $atts = array() ) {
$page_settings = get_option( 'aiovg_page_settings' );
$url = '';
if ( $page_settings['player'] > 0 ) {
$url = get_permalink( $page_settings['player'] );
if ( $post_id > 0 ) {
if ( '' != get_option( 'permalink_structure' ) ) {
$url = user_trailingslashit( trailingslashit( $url ) . 'id/' . $post_id );
} else {
$url = add_query_arg( array( 'aiovg_type' => 'id', 'aiovg_video' => $post_id ), $url );
}
}
}
$query_args = array();
foreach ( $atts as $key => $value ) {
if ( '' !== $value ) {
switch ( $key ) {
case 'mp4':
case 'webm':
case 'ogv':
case 'youtube':
case 'vimeo':
case 'facebook':
case 'poster':
$query_args[ $key ] = urlencode( $atts[ $key ] );
break;
case 'autoplay':
case 'loop':
case 'playpause':
case 'current':
case 'progress':
case 'duration':
case 'tracks':
case 'volume':
case 'fullscreen':
$query_args[ $key ] = (int) $atts[ $key ];
break;
}
}
}
if ( ! empty( $query_args ) ) {
$url = add_query_arg( $query_args, $url );
}
return apply_filters( 'aiovg_player_page_url', $url, $post_id, $atts );
}
/**
* Generate the search results page URL.
*
* @since 1.0.0
* @return string Search results page URL.
*/
function aiovg_get_search_page_url() {
$page_settings = get_option( 'aiovg_page_settings' );
$url = '/';
if ( $page_settings['search'] > 0 ) {
$url = get_permalink( $page_settings['search'] );
}
return $url;
}
/**
* Get the user videos page URL.
*
* @since 1.0.0
* @param int $user_id User ID.
* @return string User videos page URL.
*/
function aiovg_get_user_videos_page_url( $user_id ) {
$page_settings = get_option( 'aiovg_page_settings' );
$url = '/';
if ( $page_settings['user_videos'] > 0 ) {
$url = get_permalink( $page_settings['user_videos'] );
$user_slug = get_the_author_meta( 'user_nicename', $user_id );
if ( '' != get_option( 'permalink_structure' ) ) {
$url = user_trailingslashit( trailingslashit( $url ) . $user_slug );
} else {
$url = add_query_arg( 'aiovg_user', $user_slug, $url );
}
}
return $url;
}
/**
* Get video source types.
*
* @since 1.0.0
* @return array Array of source types.
*/
function aiovg_get_video_source_types() {
$types = array(
'default' => __( 'Self Hosted', 'all-in-one-video-gallery' ) . ' / ' . __( 'External URL', 'all-in-one-video-gallery' ),
'youtube' => __( 'YouTube', 'all-in-one-video-gallery' ),
'vimeo' => __( 'Vimeo', 'all-in-one-video-gallery' ),
'facebook' => __( 'Facebook', 'all-in-one-video-gallery' ),
'embedcode' => __( 'Iframe Embed Code', 'all-in-one-video-gallery' )
);
return apply_filters( 'aiovg_video_source_types', $types );
}
/*
* Get Vimeo ID from URL.
*
* @since 1.0.0
* @param string $url Vimeo page URL.
* @return string $id Vimeo video ID.
*/
function aiovg_get_vimeo_id_from_url( $url ) {
$id = '';
$is_vimeo = preg_match( '/vimeo\.com/i', $url );
if ( $is_vimeo ) {
$id = preg_replace( '/[^\/]+[^0-9]|(\/)/', '', rtrim( $url, '/' ) );
}
return $id;
}
/*
* Get Vimeo image from URL.
*
* @since 1.0.0
* @param string $url Vimeo page URL.
* @return string $url Vimeo image URL.
*/
function aiovg_get_vimeo_image_url( $url ) {
$id = aiovg_get_vimeo_id_from_url( $url );
$url = '';
if ( ! empty( $id ) ) {
$vimeo = unserialize( file_get_contents( "https://vimeo.com/api/v2/video/$id.php" ) );
$url = $vimeo[0]['thumbnail_large'];
}
return $url;
}
/*
* Get YouTube ID from URL.
*
* @since 1.0.0
* @param string $url YouTube page URL.
* @return string $id YouTube video ID.
*/
function aiovg_get_youtube_id_from_url( $url ) {
$id = '';
$url = parse_url( $url );
if ( 0 === strcasecmp( $url['host'], 'youtu.be' ) ) {
$id = substr( $url['path'], 1 );
} elseif ( 0 === strcasecmp( $url['host'], 'www.youtube.com' ) ) {
if ( isset( $url['query'] ) ) {
parse_str( $url['query'], $url['query'] );
if ( isset( $url['query']['v'] ) ) {
$id = $url['query']['v'];
}
}
if ( empty( $id ) ) {
$url['path'] = explode( '/', substr( $url['path'], 1 ) );
if ( in_array( $url['path'][0], array( 'e', 'embed', 'v' ) ) ) {
$id = $url['path'][1];
}
}
}
return $id;
}
/*
* Get YouTube image from URL.
*
* @since 1.0.0
* @param string $url YouTube page URL.
* @return string $url YouTube image URL.
*/
function aiovg_get_youtube_image_url( $url ) {
$id = aiovg_get_youtube_id_from_url( $url );
$url = '';
if ( ! empty( $id ) ) {
$url = "https://img.youtube.com/vi/$id/0.jpg";
}
return $url;
}
/*
* Inserts a new key/value after the key in the array.
*
* @since 1.0.0
* @param string $key The key to insert after.
* @param array $array An array to insert in to.
* @param array $new_array An array to insert.
* @return The new array if the key exists, FALSE otherwise.
*/
function aiovg_insert_array_after( $key, $array, $new_array ) {
if ( array_key_exists( $key, $array ) ) {
$new = array();
foreach ( $array as $k => $value ) {
$new[ $k ] = $value;
if ( $k === $key ) {
foreach ( $new_array as $new_key => $new_value ) {
$new[ $new_key ] = $new_value;
}
}
}
return $new;
}
return $array;
}
/**
* Insert required custom pages and return their IDs as array.
*
* @since 1.0.0
* @return array Array of created page IDs.
*/
function aiovg_insert_custom_pages() {
// Vars
$page_settings = get_option( 'aiovg_page_settings', array() );
$page_definitions = array(
'category' => array(
'title' => __( 'Video Category', 'all-in-one-video-gallery' ),
'content' => '[aiovg_category]'
),
'search' => array(
'title' => __( 'Search Videos', 'all-in-one-video-gallery' ),
'content' => '[aiovg_search]'
),
'user_videos' => array(
'title' => __( 'User Videos', 'all-in-one-video-gallery' ),
'content' => '[aiovg_user_videos]'
),
'player' => array(
'title' => __( 'Player Embed', 'all-in-one-video-gallery' ),
'content' => ''
)
);
// ...
$pages = array();
foreach ( $page_definitions as $slug => $page ) {
$id = 0;
if ( array_key_exists( $slug, $page_settings ) ) {
$id = (int) $page_settings[ $slug ];
}
if ( ! $id ) {
$id = wp_insert_post(
array(
'post_title' => $page['title'],
'post_content' => $page['content'],
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page',
'comment_status' => 'closed'
)
);
}
$pages[ $slug ] = $id;
}
return $pages;
}
/**
* Removes an item or list from the query string.
*
* @since 1.0.0
* @param string|array $key Query key or keys to remove.
* @param bool|string $query When false uses the $_SERVER value. Default false.
* @return string New URL query string.
*/
function aiovg_remove_query_arg( $key, $query = false ) {
if ( is_array( $key ) ) { // removing multiple keys
foreach ( $key as $k ) {
$query = str_replace( '#038;', '&', $query );
$query = add_query_arg( $k, false, $query );
}
return $query;
}
return add_query_arg( $key, false, $query );
}
/**
* Sanitize the array inputs.
*
* @since 1.0.0
* @param array $value Input array.
* @return array Sanitized array.
*/
function aiovg_sanitize_array( $value ) {
return ! empty( $value ) ? array_map( 'esc_attr', $value ) : array();
}
/**
* Sanitize the integer inputs, accepts empty values.
*
* @since 1.0.0
* @param string|int $value Input value.
* @return string|int Sanitized value.
*/
function aiovg_sanitize_int( $value ) {
$value = intval( $value );
return ( 0 == $value ) ? '' : $value;
}
/**
* Update video views count.
*
* @since 1.0.0
* @param int $post_id Post ID
*/
function aiovg_update_views_count( $post_id ) {
$user_ip = $_SERVER['REMOTE_ADDR']; // Retrieve the current IP address of the visitor
$key = $user_ip . '_aiovg_' . $post_id; // Combine post ID & IP to form unique key
$value = array( $user_ip, $post_id ); // Store post ID & IP as separate values
$visited = get_transient( $key ); // Get transient and store in variable
// Check to see if the post ID/IP ($key) address is currently stored as a transient
if ( false === $visited ) {
// Store the unique key, Post ID & IP address for 12 hours if it does not exist
set_transient( $key, $value, 60 * 60 * 12 );
// Now run post views function
$count = (int) get_post_meta( $post_id, 'views', true );
update_post_meta( $post_id, 'views', ++$count );
}
}
/**
* Display the video excerpt.
*
* @since 1.0.0
* @param int $char_length Excerpt length.
*/
function the_aiovg_excerpt( $char_length ) {
echo aiovg_get_excerpt( $char_length );
}
/**
* Display paginated links for video pages.
*
* @since 1.0.0
* @param int $numpages The total amount of pages.
* @param int $pagerange How many numbers to either side of current page.
* @param int $paged The current page number.
*/
function the_aiovg_pagination( $numpages = '', $pagerange = '', $paged = '' ) {
if ( $numpages == '' ) {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if ( ! $numpages ) {
$numpages = 1;
}
}
if ( empty( $pagerange ) ) {
$pagerange = 2;
}
if ( empty( $paged ) ) {
$paged = aiovg_get_page_number();
}
// Construct the pagination arguments to enter into our paginate_links function
$arr_params = array( 'vi', 'ca', 'lang' );
$base = aiovg_remove_query_arg( $arr_params, get_pagenum_link( 1 ) );
if ( ! get_option('permalink_structure') || isset( $_GET['aiovg'] ) ) {
$prefix = strpos( $base, '?' ) ? '&' : '?';
$format = $prefix.'paged=%#%';
} else {
$prefix = ( '/' == substr( $base, -1 ) ) ? '' : '/';
$format = $prefix.'page/%#%';
}
$pagination_args = array(
'base' => $base . '%_%',
'format' => $format,
'total' => $numpages,
'current' => $paged,
'show_all' => false,
'end_size' => 1,
'mid_size' => $pagerange,
'prev_next' => true,
'prev_text' => __( '«' ),
'next_text' => __( '»' ),
'type' => 'array',
'add_args' => false,
'add_fragment' => ''
);
$paginate_links = paginate_links( $pagination_args );
if ( $paginate_links ) {
echo '';
}
}
/**
* Display a video player.
*
* @since 1.0.0
* @param int $post_id Post ID.
* @param array $atts Player configuration data.
*/
function the_aiovg_player( $post_id = 0, $atts = array() ) {
echo aiovg_get_player_html( $post_id, $atts );
// Update views count for Iframe Embed Codes
if ( $post_id > 0 ) {
$type = get_post_meta( $post_id, 'type', true );
if ( 'embedcode' == $type ) {
aiovg_update_views_count( $post_id );
}
}
}
/**
* Display social sharing buttons.
*
* @since 1.0.0
*/
function the_aiovg_socialshare_buttons() {
global $post;
$socialshare_settings = get_option( 'aiovg_socialshare_settings' );
if ( is_singular( 'aiovg_videos' ) ) {
// Get current page url
$url = get_permalink();
// Get current page title
$title = get_the_title();
$title = str_replace( ' ', '%20', $title );
// Get image
$image_url = get_post_meta( $post->ID, 'image', true );
$image_id = get_post_meta( $post->ID, 'image_id', true );
$image = aiovg_get_image_url( $image_id, 'large', $image_url, 'player' );
// Build sharing buttons
$buttons = array();
if ( isset( $socialshare_settings['services']['facebook'] ) ) {
$facebook = "https://www.facebook.com/sharer/sharer.php?u={$url}";
$buttons[] = sprintf( '%s', $facebook, __( 'Facebook', 'all-in-one-video-gallery' ) );
}
if ( isset( $socialshare_settings['services']['twitter'] ) ) {
$twitter = "https://twitter.com/intent/tweet?text={$title}&url={$url}";
$buttons[] = sprintf( '', $twitter, __( 'Twitter', 'all-in-one-video-gallery' ) );
}
if ( isset( $socialshare_settings['services']['gplus'] ) ) {
$google = "https://plus.google.com/share?url={$url}";
$buttons[] = sprintf( '%s', $google, __( 'Google+', 'all-in-one-video-gallery' ) );
}
if ( isset( $socialshare_settings['services']['linkedin'] ) ) {
$linkedin = "https://www.linkedin.com/shareArticle?url={$url}&title={$title}";
$buttons[] = sprintf( '%s', $linkedin, __( 'Linkedin', 'all-in-one-video-gallery' ) );
}
if ( isset( $socialshare_settings['services']['pinterest'] ) ) {
$pinterest = "https://pinterest.com/pin/create/button/?url={$url}&media={$image}&description={$title}";
$buttons[] = sprintf( '%s', $pinterest, __( 'Pin It', 'all-in-one-video-gallery' ) );
}
if ( count( $buttons ) ) {
printf( '%s
', implode( ' ', $buttons ) );
}
}
}
/**
* Build & display attributes using the $atts array.
*
* @since 1.0.0
* @param array $atts Array of attributes.
*/
function the_aiovg_video_attributes( $atts ) {
$attributes = array();
foreach ( $atts as $key => $value ) {
$attributes[] = sprintf( '%s="%s"', $key, $value );
}
echo implode( ' ', $attributes );
}