init(); } public static function onboarding_is_done() { return ! self::DEV_ONBOARDING_IS_UNRESTRICTED && (bool) get_option( self::ONBOARDING_DONE_OPTION, false ); } public static function onboarding_can_prompt_gpt() { if ( self::DEV_ONBOARDING_IS_UNRESTRICTED ) { return true; } $gpt_prompt_count = (int) get_option( self::ONBOARDING_GPT_PROMPT_COUNT_OPTION, 0 ); return $gpt_prompt_count < self::ONBOARDING_GPT_PROMPT_MAX_COUNT; } public static function clear_onboarding_settings() { update_option( 'blogname', 'VideoPress.tv' ); update_option( 'blogdescription', '' ); delete_option( VideoPressHQAdmin::CATEGORIES_SUGGESTION_OPTION ); delete_option( 'vphq-custom-colors' ); update_option( VideoPressHQAdmin::ONBOARDING_DONE_OPTION, true ); } public static function onboarding_get_last_gpt_prompt() { $last_gpt_prompt = get_option( self::ONBOARDING_GPT_LAST_PROMPT_OPTION, '' ); if ( ! is_string( $last_gpt_prompt ) ) { $last_gpt_prompt = ''; } return trim( wp_unslash( $last_gpt_prompt ) ); } private function onboarding_increment_gpt_prompt() { $prompt_count = get_option( self::ONBOARDING_GPT_PROMPT_COUNT_OPTION, 0 ); if ( ! is_numeric( $prompt_count ) ) { $prompt_count = 0; } $prompt_count++; update_option( self::ONBOARDING_GPT_PROMPT_COUNT_OPTION, $prompt_count ); } private function init() { if ( ! $this->has_videopress_library() ) { return; } $this->init_admin_ajax(); $this->init_admin_menus(); } private function has_videopress_library() { return function_exists( 'videopress_is_valid_guid' ) && function_exists( 'videopress_get_video_details' ); } private function init_admin_ajax() { add_action( 'wp_ajax_videopress_hq_new_video', array( $this, 'videopress_hq_new_video' ) ); add_action( 'wp_ajax_videopress_hq_add_guid_to_post', array( $this, 'videopress_hq_add_guid_to_post' ) ); add_action( 'wp_ajax_videopress_hq_edit_video', array( $this, 'videopress_hq_edit_video' ) ); add_action( 'wp_ajax_videopress_hq_create_category', array( $this, 'videopress_hq_create_category' ) ); add_action( 'wp_ajax_videopress_hq_delete_video', array( $this, 'videopress_hq_delete_video' ) ); add_action( 'wp_ajax_videopress_hq_toggle_featured_video', array( $this, 'videopress_hq_toggle_featured_video' ) ); add_action( 'wp_ajax_videopress_hq_edit_playlist', array( $this, 'videopress_hq_edit_playlist' ) ); add_action( 'wp_ajax_videopress_hq_delete_playlist', array( $this, 'videopress_hq_delete_playlist' ) ); add_action( 'wp_ajax_videopress_hq_search_videos', array( $this, 'videopress_hq_search_videos' ) ); add_action( 'wp_ajax_videopress_hq_save_appearance', array( $this, 'videopress_hq_save_appearance' ) ); add_action( 'wp_ajax_videopress_hq_onboarding_customize', array( $this, 'videopress_hq_onboarding_customize' ) ); add_action( 'wp_ajax_videopress_hq_onboarding_gpt_prompt', array( $this, 'videopress_hq_onboarding_gpt_prompt' ) ); add_action( 'wp_ajax_videopress_hq_add_category', array( $this, 'videopress_hq_add_category' ) ); add_action( 'wp_ajax_videopress_hq_delete_category', array( $this, 'videopress_hq_delete_category' ) ); add_action( 'wp_ajax_videopress_hq_update_comment_status', array( $this, 'videopress_hq_update_comment_status' ) ); } private function init_admin_menus() { add_filter( self::FILTER_ADMIN_TOP_MENUS, array( $this, 'add_top_menus' ) ); add_filter( self::FILTER_ADMIN_BOTTOM_MENUS, array( $this, 'add_bottom_menus' ) ); } public function add_top_menus( array $menus ) { if ( current_user_can( VideoPressHQCaps::CAP_READ_DASHBOARD ) ) { $menus[] = array( 'name' => __( 'Dashboard', 'videopress-hq' ), 'icon' => 'grid', 'pages' => array( 'dashboard' ), 'url' => vphq_admin_page_link( 'dashboard' ), ); } if ( current_user_can( VideoPressHQCaps::CAP_EDIT_VIDEOS ) ) { $menus[] = array( 'name' => __( 'Videos', 'videopress-hq' ), 'icon' => 'capture-video', 'pages' => array( 'manage-videos', 'edit' ), 'url' => vphq_admin_page_link( 'videos' ), 'cap' => VideoPressHQCaps::CAP_EDIT_VIDEOS, ); } if ( current_user_can( VideoPressHQCaps::CAP_EDIT_PLAYLISTS ) ) { $menus[] = array( 'name' => __( 'Playlists', 'videopress-hq' ), 'icon' => 'playlist', 'pages' => array( 'manage-playlists', 'edit-playlist' ), 'url' => vphq_admin_page_link( 'playlists' ), ); } if ( current_user_can( 'manage_categories' ) ) { $menus[] = array( 'name' => __( 'Categories', 'videopress-hq' ), 'icon' => 'file', 'pages' => array( 'manage-categories' ), 'url' => vphq_admin_page_link( 'categories' ), ); } if ( current_user_can( 'moderate_comments' ) ) { $menus[] = array( 'name' => __( 'Comments', 'videopress-hq' ), 'icon' => 'comment', 'pages' => array( 'manage-comments' ), 'url' => vphq_admin_page_link( 'comments' ), ); } return $menus; } public function add_bottom_menus( array $menus ) { if ( current_user_can( 'list_users' ) ) { $menus[] = array( 'name' => __( 'Users', 'videopress-hq' ), 'icon' => 'people', 'url' => admin_url( 'users.php' ), 'ext' => true, ); } if ( current_user_can( 'edit_theme_options' ) ) { $menus[] = array( 'name' => __( 'Appearance', 'videopress-hq' ), 'icon' => 'styles', 'pages' => array( 'appearance' ), 'url' => vphq_admin_page_link( 'appearance' ), ); } $is_wpcom = defined( 'IS_WPCOM' ) && IS_WPCOM; $wpcom_blog_url = ''; if ( $is_wpcom ) { $wpcom_blog_url = wpcom_get_blog_url( get_blog_details() ); } if ( ! empty( $wpcom_blog_url ) && current_user_can( 'manage_options' ) ) { $menus[] = array( 'name' => __( 'Billing', 'videopress-hq' ), 'icon' => 'payment', 'url' => 'https://wordpress.com/purchases/subscriptions/' . urlencode( $wpcom_blog_url ), 'ext' => true, ); } if ( current_user_can( 'manage_options' ) ) { $site_settings_link = admin_url( 'options-general.php' ); if ( ! empty( $wpcom_blog_url ) ) { $site_settings_link = 'https://wordpress.com/settings/general/' . urlencode( $wpcom_blog_url ); } $menus[] = array( 'name' => __( 'Site settings', 'videopress-hq' ), 'icon' => 'cog', 'url' => $site_settings_link, 'ext' => true, ); } $menus[] = array( 'name' => __( 'Log out', 'videopress-hq' ), 'icon' => '', 'url' => wp_logout_url(), ); return $menus; } public function videopress_hq_new_video() { if ( ! current_user_can( VideoPressHQCaps::CAP_EDIT_VIDEOS ) ) { return $this->ajax_response( new WP_Error( 'invalid_permissions', 'You do not have permission to add videos.' ) ); } if ( ! $this->check_posted_nonce( 'add_video_nonce', 'videopress-hq-add-video' ) ) { return $this->ajax_response( new WP_Error( 'invalid_nonce', 'Nonce is invalid.' ) ); } $new_video_details = array( 'post_status' => VideoPressHQPostStatus::STATUS_DRAFT, 'post_type' => 'vp_video', ); $new_video_id = wp_insert_post( $new_video_details ); if ( is_wp_error( $new_video_id ) ) { return $this->ajax_response( $new_video_id ); } $video_link = get_edit_video_link( $new_video_id ); return $this->ajax_response( $video_link ); } public function videopress_hq_add_guid_to_post() { if ( ! current_user_can( VideoPressHQCaps::CAP_EDIT_VIDEOS ) ) { return $this->ajax_response( new WP_Error( 'invalid_permissions', 'You do not have permission to edit videos.' ) ); } $create_post = ! empty( $_POST['create_post'] ); $post_id = null; if ( $create_post ) { if ( ! $this->check_posted_nonce( 'add_video_with_guid_nonce', 'videopress-hq-video-add-guid-new' ) ) { return $this->ajax_response( new WP_Error( 'invalid_nonce', 'Nonce is invalid.' ) ); } $new_video_details = array( 'post_status' => VideoPressHQPostStatus::STATUS_PUBLISHED, 'post_type' => 'vp_video', ); $post_id = wp_insert_post( $new_video_details ); if ( is_wp_error( $post_id ) ) { return $this->ajax_response( $post_id ); } } else { if ( ! isset( $_POST['post_id'] ) ) { return $this->ajax_response( new WP_Error( 'invalid_post_id', 'Post ID is missing' ) ); } $post_id = sanitize_text_field( $_POST['post_id'] ); if ( ! $this->check_posted_nonce( 'add_guid_nonce', 'videopress-hq-video-add-guid-' . $post_id ) ) { return $this->ajax_response( new WP_Error( 'invalid_nonce', 'Nonce is invalid.' ) ); } $post = get_post( $post_id ); if ( ! $post ) { return $this->ajax_response( new WP_Error( 'invalid_post_id', 'Post does not exist' ) ); } } if ( ! isset( $_POST['guid'] ) ) { return $this->ajax_response( new WP_Error( 'invalid_guid', 'Invalid GUID' ) ); } $guid = sanitize_text_field( $_POST['guid'] ); if ( ! videopress_is_valid_guid( $guid ) ) { return $this->ajax_response( new WP_Error( 'invalid_guid', 'Invalid GUID' ) ); } $video_details = videopress_hq_get_video_details( $guid ); if ( is_wp_error( $video_details ) ) { return $this->ajax_response( new WP_Error( 'invalid_guid', 'Invalid GUID' ) ); } $result = update_post_meta( $post_id, 'guid', $guid ); if ( is_wp_error( $result ) ) { return $this->ajax_response( $result ); } if ( $create_post ) { $title = $video_details->title ?? ''; if ( ! empty( $title ) ) { wp_update_post( array( 'ID' => $post_id, 'post_title' => $title, ) ); } } $options = array(); $token = vphq_get_metadata_token_if_required( $guid ); if ( false !== $token ) { $options['metadata_token'] = $token; } $embed_url = generate_embed_url( $guid, $options ); $video_embed = "