get_var( $wpdb->prepare( 'SELECT SUM( plays ) as plays FROM video_stats WHERE blog_id=%d AND post_id=%d', $video_details->blog_id, $video_details->post_id, ), ); if ( ! is_numeric( $plays ) ) { $plays = 0; } $count = intval( $plays ); wp_cache_set( $video_plays_cache_key, $count, '', 60 ); // 1 minute expiry return $autoformat ? format_view_count( $count ) : $count; } function render_playlist_stat( $playlist, $autoformat = true ) { global $wpdb; if ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM ) { return $autoformat ? format_view_count( 0 ) : 0; } $blog_id = get_current_blog_id(); $video_plays_cache_key = sprintf( 'vphq-plays-playlist-%d-%d', $blog_id, $playlist->term->term_id ); $count = wp_cache_get( $video_plays_cache_key ); if ( is_numeric( $count ) ) { return $autoformat ? format_view_count( $count ) : $count; } $query = $playlist->get_posts_query( array( 'fields' => 'ids' ) ); $post_ids = $query->get_posts(); array_filter( $post_ids, 'is_numeric' ); if ( empty( $post_ids ) ) { wp_cache_set( $video_plays_cache_key, 0, '', 60 ); return $autoformat ? format_view_count( 0 ) : 0; } $count = 0; foreach ( $post_ids as $post_id ) { $count += render_stat( $post_id, false ); } wp_cache_set( $video_plays_cache_key, $count, '', 600 ); // 10 minute expiry return $autoformat ? format_view_count( $count ) : $count; } function generate_embed_url( $guid, array $options = array() ) { $options = array_merge( array( 'resizeToParent' => true, 'cover' => false, 'preloadContent' => 'metadata', 'useAverageColor' => true, ), $options ); $base_url = 'https://video.wordpress.com/v/' . esc_attr( $guid ); foreach ( $options as $key => $value ) { if ( true === $value ) { $value = 'true'; } elseif ( false === $value ) { $value = 'false'; } $base_url = add_query_arg( $key, $value, $base_url ); } return $base_url; } function vphq_get_custom_admin_pages() { return array( 'dashboard' => 'index.php?vp-custom-page=dashboard', 'videos' => 'index.php?vp-custom-page=manage-videos', 'playlist/new' => 'index.php?vp-custom-page=edit-playlist', 'categories' => 'index.php?vp-custom-page=manage-categories', 'appearance' => 'index.php?vp-custom-page=appearance', 'comments' => 'index.php?vp-custom-page=manage-comments', 'onboarding/upload' => array( 'rule' => 'index.php?vp-custom-page=onboarding-upload', 'no_prefix' => true, ), 'onboarding' => array( 'rule' => 'index.php?vp-custom-page=onboarding', 'no_prefix' => true, ), 'onboarding/customize' => array( 'rule' => 'index.php?vp-custom-page=onboarding-customize', 'no_prefix' => true, ), 'onboarding/clear' => array( 'rule' => 'index.php?vp-custom-page=onboarding-clear', 'no_prefix' => true, ), 'video/edit' => array( 'rule' => array( 'pattern' => 'video/(\\d+)', 'rewrite' => 'index.php?post_type=vp_video&p=$matches[1]&vp-custom-page=edit', ), 'rewrite_url' => 'video/%d', 'raw_url' => 'index.php?post_type=vp_video&p=%d&vp-custom-page=edit', ), 'playlists' => 'index.php?vp-custom-page=manage-playlists', 'playlist/edit' => array( 'rule' => array( 'pattern' => 'playlist/(\\d+)', 'rewrite' => 'index.php?vp-custom-page=edit-playlist&playlist_id=$matches[1]', ), 'rewrite_url' => 'playlist/%d', 'raw_url' => 'index.php?vp-custom-page=edit-playlist&playlist_id=%d', ), // This is matching (and redirecting to) the "comments" rule, so no rewrite rule is needed for this one. 'comments-by-post' => array( 'rewrite_url' => 'comments/?post_id=%d', 'raw_url' => 'index.php?vp-custom-page=manage-comments&post_id=%d', ), ); } function vphq_relative_admin_page_link( $page, ...$args ) { $pages = vphq_get_custom_admin_pages(); if ( ! isset( $pages[ $page ] ) ) { return ''; } $admin_page = $pages[ $page ]; if ( is_string( $admin_page ) ) { return get_option( 'permalink_structure' ) ? ( '/manage/' . $page ) : ( '/' . $admin_page ); } if ( ! get_option( 'permalink_structure' ) ) { $raw_url = $admin_page['raw_url'] ?? $admin_page['rule']; return is_string( $raw_url ) ? ( '/' . sprintf( $raw_url, ...$args ) ) : ''; } $prefix = isset( $admin_page['no_prefix'] ) && $admin_page['no_prefix'] ? '/' : '/manage/'; $rewrite_url = $admin_page['rewrite_url'] ?? $page; return $prefix . sprintf( $rewrite_url, ...$args ); } function vphq_admin_page_link( $page, ...$args ) { $relative_link = vphq_relative_admin_page_link( $page, ...$args ); if ( empty( $relative_link ) ) { return ''; } return home_url( $relative_link ); } function get_edit_video_link( $post_id ) { return vphq_admin_page_link( 'video/edit', $post_id ); } function get_edit_playlist_link( $playlist_id ) { return vphq_admin_page_link( 'playlist/edit', $playlist_id ); } function get_video_post_thumbnail( $post_id, $format ) { $video_guid = get_post_meta( $post_id, 'guid', true ); $post_thumbnail = get_the_post_thumbnail_url( $post_id, $format ); if ( empty( $post_thumbnail ) && ! empty( $video_guid ) ) { $video_details = videopress_hq_get_video_details( $video_guid ); if ( $video_details ) { $post_thumbnail = $video_details->poster ?? ''; } } return $post_thumbnail; } function videopress_hq_get_video_details( $video_guid ) { if ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM ) { $video_details = videopress_get_video_details( $video_guid ); if ( ! $video_details ) { return $video_details; } // JETPACK override - transient is cached, and if its missing poster it won't have it for an hour, so clear it and try again. if ( empty( $video_details->poster ) ) { delete_transient( 'jetpack_videopress_' . $video_guid, false ); $video_details = videopress_get_video_details( $video_guid ); } return $video_details; } else { // The following wpcom checks, fields and their values are taken from `video_get_single_response` in `wp-content/mu-plugins/videopress/get.php` if ( ! videopress_is_valid_guid( $video_guid ) ) { return new WP_Error( 'bad-guid-format', __( 'Invalid Video GUID!', 'jetpack' ) ); } $video_info = video_get_info_by_guid( $video_guid ); if ( false === $video_info ) { return $video_info; } $thumbnail = video_highest_resolution_image( $video_info ); $poster = ( $thumbnail ) ? jetpack_photon_url( video_cdn_file_url( $video_info->guid, video_highest_resolution_image( $video_info ) ) ) : null; $video_title = $video_info->title ?? video_get_title( $video_info->blog_id, $video_info->post_id ); if ( $video_info->duration_milliseconds ) { $duration = $video_info->duration_milliseconds; } else { $duration = floatval( $video_info->duration ) * 1000; } // This list of params is based on what we are currently using in videopress-hq, in an attempt to cut out the inefficiency of all // the extra processing that occurs when calling `videopress_get_video_details` on wpcom which is needed for the player, but not for // rendering video previews in HQ. $response = array( 'blog_id' => intval( $video_info->blog_id ), 'post_id' => intval( $video_info->post_id ), 'title' => $video_title, 'poster' => $poster, 'duration' => $duration, ); return (object) $response; } } function videopress_hq_is_video_upload_allowed() { if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { require_lib( 'videopress' ); return VideoPress\videopress_hq_site_can_upload_video( get_current_blog_id() ); } return true; } function videopress_hq_render_upgrade_prompt() { // Todo: change link to actual VPHQ plan ?>
blog_id, wp_get_current_user() ) ) { require_lib( 'videopress' ); if ( VideoPress\videopress_hq_site_can_serve_video( $video_info->blog_id, get_current_user_id() ) ) { return \video_generate_auth_token( $video_info ); } } } return false; } /** * Retrieves admin url for front-end requests. * * @param mixed $original_path The path you want the admin url for * @return string The admin url */ function vphq_get_admin_url( $original_path ) { global $blog_id; if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { $path = ltrim( $original_path, '/' ); $site_url = get_site_url( $blog_id ); $site_domain_only = parse_url( $site_url, PHP_URL_HOST ); $domain_mapping = get_primary_redirect( $blog_id ); if ( ! empty( $domain_mapping ) && $domain_mapping !== $site_domain_only ) { return "https://{$domain_mapping}/wp-admin/$path"; } } return admin_url( $original_path ); }