post_type ) { $theme_template = locate_template( 'single-plugetune_video.php' ); if ( $theme_template ) { return $theme_template; } $plugin_template = PLUGETUNE_PATH . 'public/single-plugetune_video.php'; if ( file_exists( $plugin_template ) ) { return $plugin_template; } return $template; } return $template; } add_filter( 'single_template', 'plugetune_single_template', 10, 1 ); function plugetune_register_cpt() { $labels = array( 'name' => _x( 'PlugeTune Videos', 'Post Type General Name', 'plugetune' ), 'singular_name' => _x( 'PlugeTune Video', 'Post Type Singular Name', 'plugetune' ), 'menu_name' => __( 'PlugeTune Videos', 'plugetune' ), 'name_admin_bar' => __( 'PlugeTune Video', 'plugetune' ), 'all_items' => __( 'All Videos', 'plugetune' ), 'add_new_item' => __( 'Add New Video', 'plugetune' ), 'add_new' => __( 'Add New', 'plugetune' ), 'edit_item' => __( 'Edit Video', 'plugetune' ), 'view_item' => __( 'View Video', 'plugetune' ), 'search_items' => __( 'Search Videos', 'plugetune' ), 'not_found' => __( 'No videos found.', 'plugetune' ), 'not_found_in_trash' => __( 'No videos found in Trash.', 'plugetune' ), ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'show_in_nav_menus' => true, 'show_in_admin_bar' => true, 'show_in_rest' => true, 'has_archive' => true, 'hierarchical' => false, 'menu_position' => 6, 'menu_icon' => 'dashicons-video-alt3', 'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'categories', 'tags' ), 'taxonomies' => array('category', 'post_tag'), 'rewrite' => array( 'slug' => 'videos', 'with_front' => true, 'pages' => true, 'feeds' => true, ), 'query_var' => true, 'capability_type' => 'post', 'exclude_from_search' => false, ); register_post_type( 'plugetune_video', $args ); } add_action( 'init', 'plugetune_register_cpt', 0 ); function plugetune_register_all_admin_menus() { $menus = array( array( 'parent' => 'edit.php?post_type=plugetune_video', 'page_title' => __( 'Import Videos', 'plugetune' ), 'menu_title' => __( 'Import Videos', 'plugetune' ), 'capability' => 'manage_options', 'menu_slug' => 'plugetune-video-import', 'function' => 'plugetune_render_video_import_page', ), array( 'parent' => 'edit.php?post_type=plugetune_video', 'page_title' => __( 'PlugeTune Settings', 'plugetune' ), 'menu_title' => __( 'Settings', 'plugetune' ), 'capability' => 'manage_options', 'menu_slug' => 'plugetune-settings', 'function' => 'plugetune_render_settings_page', ), array( 'parent' => 'edit.php?post_type=plugetune_video', 'page_title' => __( 'Contact Us', 'plugetune' ), 'menu_title' => __( 'Contact Us', 'plugetune' ), 'capability' => 'manage_options', 'menu_slug' => 'plugetune-fs-contact', 'function' => 'plugetune_render_fs_contact_page', 'load' => array('Freemius', '_clean_admin_content_section'), ), array( 'parent' => 'edit.php?post_type=plugetune_video', 'page_title' => __( 'Upgrade', 'plugetune' ), 'menu_title' => __( 'Upgrade', 'plugetune' ) . ' ', 'capability' => 'manage_options', 'menu_slug' => 'plugetune-upgrade', 'function' => 'plugetune_render_fs_pricing_page', 'load' => array('Freemius', '_clean_admin_content_section'), ) ); if ( plugetune_is_premium_build() ) { array_splice( $menus, 2, 0, array(array( 'parent' => 'edit.php?post_type=plugetune_video', 'page_title' => __( 'Account', 'plugetune' ), 'menu_title' => __( 'Account', 'plugetune' ), 'capability' => 'manage_options', 'menu_slug' => 'plugetune-fs-account', 'function' => 'plugetune_render_fs_account_page', 'load' => array(plugetune_fs(), '_account_page_load'), )) ); } foreach ( $menus as $menu ) { $hook = add_submenu_page( $menu['parent'], $menu['page_title'], $menu['menu_title'], $menu['capability'], $menu['menu_slug'], $menu['function'] ); if ( $hook && !empty( $menu['load'] ) && is_callable( $menu['load'] ) ) { add_action( "load-{$hook}", $menu['load'] ); } } } add_action( 'admin_menu', 'plugetune_register_all_admin_menus' ); function plugetune_render_settings_page() { if ( class_exists( 'PlugeTune_Settings_Page' ) && method_exists( 'PlugeTune_Settings_Page', 'render_settings_page' ) ) { $settings = new PlugeTune_Settings_Page(); $settings->render_settings_page(); } else { plugetune_fallback_page( 'Settings' ); } } function plugetune_render_fs_pricing_page() { if ( !current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'plugetune' ) ); } plugetune_fs()->_pricing_page_render(); } function plugetune_render_fs_account_page() { if ( !current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'plugetune' ) ); } $plugetune_fs = plugetune_fs(); // The Freemius account page needs a connected account. Until the site has opted in, send the user to the opt-in / licence activation screen instead. if ( !$plugetune_fs->is_registered() ) { $plugetune_connect_url = ( $plugetune_fs->is_anonymous() ? $plugetune_fs->get_reconnect_url() : $plugetune_fs->get_activation_url() ); ?>

_account_page_render(); } function plugetune_render_fs_contact_page() { if ( !current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'plugetune' ) ); } plugetune_fs()->_contact_page_render(); } /** * Upgrade menu item: light-blue text with the crown icon on every admin screen. */ function plugetune_upgrade_menu_assets() { wp_enqueue_style( 'font-awesome', PLUGETUNE_URL . 'assets/vendor/font-awesome/css/all.min.css', array(), '6.4.0' ); } add_action( 'admin_enqueue_scripts', 'plugetune_upgrade_menu_assets' ); function plugetune_upgrade_menu_style() { echo ''; } add_action( 'admin_head', 'plugetune_upgrade_menu_style' ); function plugetune_render_video_import_page() { if ( class_exists( 'PlugeTune_Video_Import_Layout' ) ) { ?>
'; echo '

' . esc_html( $title ) . '

'; echo '

' . esc_html( $message ) . '

'; echo ''; } function plugetune_ajax_validate_import() { if ( !isset( $_POST['nonce'] ) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'plugetune_ajax_nonce' ) ) { wp_send_json_error( array( 'message' => __( 'Security check failed.', 'plugetune' ), ) ); } if ( !current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'Unauthorized access.', 'plugetune' ), ) ); } $api_key = PlugeTune_YouTube_API::get_api_key(); if ( empty( $api_key ) ) { wp_send_json_error( array( 'message' => __( 'YouTube API Key is missing. Please configure it in the Settings page.', 'plugetune' ), 'field' => 'api_key', ) ); } $channel_ids = PlugeTune_YouTube_API::get_channel_ids(); if ( empty( $channel_ids ) ) { wp_send_json_error( array( 'message' => __( 'Channel ID(s) are missing. Please configure them in the Settings page.', 'plugetune' ), 'field' => 'channel_ids', ) ); } $playlist_links = ( isset( $_POST['playlist_links'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash( (array) $_POST['playlist_links'] ) ) ) : array() ); $single_video_links = ( isset( $_POST['single_video_links'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash( (array) $_POST['single_video_links'] ) ) ) : array() ); if ( empty( $playlist_links ) && empty( $single_video_links ) ) { wp_send_json_error( array( 'message' => __( 'Please add at least one playlist link or single video link to import.', 'plugetune' ), 'field' => 'links', ) ); } $test_url = "https://www.googleapis.com/youtube/v3/videos?part=snippet&id=dQw4w9WgXcQ&key={$api_key}"; $test_response = wp_remote_get( $test_url, array( 'timeout' => 45, 'headers' => array( 'Accept' => 'application/json', 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36', ), 'sslverify' => true, ) ); if ( is_wp_error( $test_response ) ) { wp_send_json_error( array( 'message' => sprintf( /* translators: %s: error message returned by the HTTP request. */ __( 'Failed to connect to YouTube API: %s', 'plugetune' ), $test_response->get_error_message() ), 'field' => 'api_key', ) ); } $test_data = json_decode( wp_remote_retrieve_body( $test_response ) ); if ( isset( $test_data->error ) ) { $error_msg = __( 'Invalid YouTube API Key. ', 'plugetune' ); if ( isset( $test_data->error->errors[0]->reason ) ) { switch ( $test_data->error->errors[0]->reason ) { case 'keyInvalid': $error_msg .= __( 'Your API key is invalid.', 'plugetune' ); break; case 'keyExpired': $error_msg .= __( 'Your API key has expired.', 'plugetune' ); break; case 'accessNotConfigured': $error_msg .= __( 'YouTube Data API v3 is not enabled for this project.', 'plugetune' ); break; case 'quotaExceeded': $error_msg .= __( 'API quota exceeded. Please try again later.', 'plugetune' ); break; default: $error_msg .= ( isset( $test_data->error->errors[0]->message ) ? $test_data->error->errors[0]->message : '' ); } } wp_send_json_error( array( 'message' => $error_msg, 'field' => 'api_key', ) ); } wp_send_json_success( array( 'message' => __( 'Validation passed. Starting import...', 'plugetune' ), 'playlist_count' => count( $playlist_links ), 'video_count' => count( $single_video_links ), ) ); } add_action( 'wp_ajax_plugetune_validate_import', 'plugetune_ajax_validate_import' ); function plugetune_init_feature( $class_name, $method_name = 'init' ) { if ( !class_exists( $class_name ) ) { return false; } $instance = null; if ( method_exists( $class_name, 'get_instance' ) ) { $instance = call_user_func( array($class_name, 'get_instance') ); } else { try { $instance = new $class_name(); } catch ( Exception $e ) { return false; } } if ( $instance && method_exists( $instance, $method_name ) ) { $instance->{$method_name}(); return true; } return false; } function plugetune_init_free_feature() { plugetune_init_feature( 'PlugeTune_Free_Feature' ); } add_action( 'init', 'plugetune_init_free_feature' ); function plugetune_init_schedule_import() { plugetune_init_feature( 'PlugeTune_Schedule_Import' ); } add_action( 'init', 'plugetune_init_schedule_import' ); function plugetune_init_settings_page() { plugetune_init_feature( 'PlugeTune_Settings_Page' ); } add_action( 'init', 'plugetune_init_settings_page' ); function plugetune_enqueue_admin_assets( $hook ) { if ( !in_array( $hook, array('post.php', 'post-new.php'), true ) ) { return; } wp_enqueue_media(); $js_path = PLUGETUNE_PATH . 'assets/js/plugetune_admin.js'; $js_ver = ( file_exists( $js_path ) ? filemtime( $js_path ) : PLUGETUNE_VERSION ); wp_enqueue_script( 'plugetune-admin', PLUGETUNE_URL . 'assets/js/plugetune_admin.js', array('jquery'), $js_ver, true ); wp_localize_script( 'plugetune-admin', 'PLUGETUNE_ADMIN', array( 'videoMetaKey' => '_plugetune_video_id', 'ytMetaKey' => '_plugetune_youtube_url', 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'plugetune_single_video_nonce' ), ) ); } add_action( 'admin_enqueue_scripts', 'plugetune_enqueue_admin_assets' ); function plugetune_enqueue_editor_assets() { $js_path = PLUGETUNE_PATH . 'assets/js/plugetune-editor.js'; $js_ver = ( file_exists( $js_path ) ? filemtime( $js_path ) : PLUGETUNE_VERSION ); wp_enqueue_script( 'plugetune-editor', PLUGETUNE_URL . 'assets/js/plugetune-editor.js', array( 'wp-plugins', 'wp-edit-post', 'wp-element', 'wp-components', 'wp-data' ), $js_ver, true ); wp_enqueue_script( 'plugetune-elementor-editor', PLUGETUNE_URL . 'assets/js/plugetune_elementor-editor.js', array('jquery', 'elementor-frontend'), PLUGETUNE_VERSION, true ); wp_localize_script( 'plugetune-editor', 'PLUGETUNE', array( 'metaYt' => '_plugetune_youtube_url', 'metaVideo' => '_plugetune_video_id', 'metaFmt' => '_plugetune_img_format', 'i18n' => array( 'panelTitle' => __( 'YouTube & Media Library Video', 'plugetune' ), 'ytLabel' => __( 'YouTube Video URL', 'plugetune' ), 'videoLabel' => __( 'Upload/Select Video', 'plugetune' ), ), ) ); } add_action( 'enqueue_block_editor_assets', 'plugetune_enqueue_editor_assets' ); function plugetune_enqueue_frontend_assets() { if ( !is_singular() ) { return; } wp_enqueue_style( 'plugetune-frontend', PLUGETUNE_URL . 'assets/css/plugetune_frontend.css', array(), PLUGETUNE_VERSION ); wp_enqueue_script( 'jquery' ); } add_action( 'wp_enqueue_scripts', 'plugetune_enqueue_frontend_assets' ); function plugetune_enqueue_settings_assets( $hook ) { if ( strpos( $hook, 'plugetune-settings' ) === false && strpos( $hook, 'plugetune-video-import' ) === false ) { return; } wp_enqueue_style( 'font-awesome', PLUGETUNE_URL . 'assets/vendor/font-awesome/css/all.min.css', array(), '6.4.0' ); wp_enqueue_media(); wp_enqueue_style( 'plugetune-admin', PLUGETUNE_URL . 'assets/css/plugetune_admin.css', array(), PLUGETUNE_VERSION ); wp_enqueue_script( 'plugetune-admin-js', PLUGETUNE_URL . 'assets/js/plugetune_admin.js', array('jquery'), PLUGETUNE_VERSION, true ); $api_key = get_option( 'plugetune_api_key', '' ); $channel_ids = get_option( 'plugetune_allowed_channel_ids', '' ); $is_configured = !empty( $api_key ) && !empty( $channel_ids ); $settings_url = admin_url( 'edit.php?post_type=plugetune_video&page=plugetune-settings' ); wp_localize_script( 'plugetune-admin-js', 'plugetune_ajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'plugetune_ajax_nonce' ), 'settings_url' => $settings_url, 'api_configured' => ( $is_configured ? 'true' : 'false' ), 'is_pro' => ( plugetune_is_pro() ? 'true' : 'false' ), 'upgrade_url' => esc_url_raw( plugetune_upgrade_page_url() ), 'pro_message' => __( 'Browse is a Pro feature. You can still paste a YouTube link manually, or upgrade to Pro to browse your channel.', 'plugetune' ), 'pro_message_html' => plugetune_browse_pro_message_html(), ) ); } add_action( 'admin_enqueue_scripts', 'plugetune_enqueue_settings_assets' ); function plugetune_activate() { plugetune_register_cpt(); flush_rewrite_rules(); update_option( 'plugetune_wizard_completed', false ); update_option( 'plugetune_wizard_skipped', false ); add_option( 'plugetune_embed_options', '1' ); add_option( 'plugetune_embed_position', 'before' ); add_option( 'plugetune_video_width', '560' ); add_option( 'plugetune_video_height', '315' ); add_option( 'plugetune_thumbnail_quality', '90' ); add_option( 'plugetune_lazy_load', '0' ); add_option( 'plugetune_protection', '0' ); add_option( 'plugetune_default_img_format', 'webp' ); add_option( 'plugetune_auto_excerpt', '1' ); add_option( 'plugetune_import_post_status', 'publish' ); update_option( 'plugetune_activation_redirect', 'yes' ); } register_activation_hook( PLUGETUNE_FILE, 'plugetune_activate' ); function plugetune_deactivate() { flush_rewrite_rules(); delete_option( 'plugetune_activation_redirect' ); } register_deactivation_hook( PLUGETUNE_FILE, 'plugetune_deactivate' ); function plugetune_redirect_to_wizard() { if ( !current_user_can( 'manage_options' ) ) { return; } $is_activation = 'yes' === get_option( 'plugetune_activation_redirect', 'no' ); if ( !$is_activation && function_exists( 'plugetune_fs' ) && is_object( plugetune_fs() ) && method_exists( plugetune_fs(), 'is_plugin_activation' ) ) { $is_activation = (bool) plugetune_fs()->is_plugin_activation(); if ( $is_activation ) { delete_transient( 'fs_plugin_' . plugetune_fs()->get_slug() . '_activated' ); } } // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( isset( $_GET['page'] ) && 'plugetune-setup-wizard' === sanitize_key( wp_unslash( $_GET['page'] ) ) ) { return; } if ( !$is_activation ) { return; } delete_option( 'plugetune_activation_redirect' ); $api_key = get_option( 'plugetune_api_key', '' ); $channel_ids = get_option( 'plugetune_allowed_channel_ids', '' ); if ( !empty( $api_key ) && !empty( $channel_ids ) ) { update_option( 'plugetune_wizard_completed', true ); return; } if ( get_option( 'plugetune_wizard_skipped', false ) || get_option( 'plugetune_wizard_completed', false ) ) { return; } wp_safe_redirect( admin_url( 'admin.php?page=plugetune-setup-wizard' ) ); exit; } add_action( 'admin_init', 'plugetune_redirect_to_wizard', 1 ); function plugetune_plugin_action_links( $links ) { if ( !is_array( $links ) ) { $links = array(); } $settings_link = '' . esc_html__( 'Settings', 'plugetune' ) . ''; $wizard_link = '' . esc_html__( 'Quick Setup', 'plugetune' ) . ''; array_unshift( $links, $wizard_link, $settings_link ); return $links; } add_filter( 'plugin_action_links_' . plugin_basename( PLUGETUNE_FILE ), 'plugetune_plugin_action_links' ); function plugetune_fix_admin_title( $admin_title, $title ) { if ( empty( $admin_title ) ) { $admin_title = ( !empty( $title ) ? $title : get_bloginfo( 'name' ) ); } return $admin_title; } add_filter( 'admin_title', 'plugetune_fix_admin_title', 10, 2 );