plugin_name = $plugin_name; $this->version = $version; } /** * Register the stylesheets for the admin area. * * @since 1.0.0 */ public function albaau_albatross_audio_enqueue_admin_styles($hook) { wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/albatross-audio-admin.css', array(), $this->version, 'all' ); } /** * Register the JavaScript for the admin area. * * @since 1.0.0 */ public function albaau_albatross_audio_enqueue_admin_scripts($hook) { wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/albatross-audio-admin.js', array( 'jquery' ), $this->version, false ); // localize home_url() to use for player placeholder img wp_localize_script( $this->plugin_name, 'wpData', array( 'pluginUrl' => esc_url( plugins_url() ), ) ); } /*================================================================ * albatross_audio post type ==================================================================*/ public function albaau_albatross_audio() { $labels = array( 'name' => __('Albatross Audio', 'albatross-audio'), 'singular_name' => __('Playlist', 'albatross-audio'), 'add_new' => __('New Playlist', 'albatross-audio'), 'add_new_item' => __('New Playlist', 'albatross-audio'), 'edit_item' => __('Edit Playlist', 'albatross-audio'), 'new_item' => __('New Playlist', 'albatross-audio'), 'all_items' => __('Playlists', 'albatross-audio'), 'view_item' => __('View Playlist', 'albatross-audio'), 'search_items' => __('Search Playlists', 'albatross-audio'), 'not_found' => __('Nothing Found', 'albatross-audio'), 'not_found_in_trash' => __('Nothing Found In Trash', 'albatross-audio'), 'remove_featured_image' => __('Remove Image', 'albatross-audio'), 'parent_item_colon' => '', 'menu_name' => 'Albatross' ); $args = array( 'labels' => $labels, 'public' => true, 'hierarchical' => true, 'show_in_menu' => true, 'menu_position' => 11, 'menu_icon' => 'dashicons-playlist-audio', 'supports' => array( 'title', 'thumbnail' ), 'show_in_nav_menus' => true, 'has_archive' => 'albatross-audio', 'show_in_rest' => true, 'with_front' => false, 'rewrite' => array( 'slug' => 'albatross-audio', ) , ); register_post_type('albatross-audio', $args); } /*================================================================ * albatross_audio Taxonomy ==================================================================*/ public function albaau_albatross_audio_taxonomy() { $labels = array( 'name' => __( 'Albatross Audio Categories', 'albatross-audio' ), 'singular_name' => __( 'Category', 'albatross-audio' ), 'search_items' => __( 'Search Categories', 'albatross-audio' ), 'all_items' => __( 'All Categories', 'albatross-audio' ), 'parent_item' => __( 'Parent Category', 'albatross-audio' ), 'parent_item_colon' => __( 'Parent Category:', 'albatross-audio' ), 'edit_item' => __( 'Edit Category', 'albatross-audio' ), 'update_item' => __( 'Update Category', 'albatross-audio' ), 'add_new_item' => __( 'Add New Category', 'albatross-audio' ), 'new_item_name' => __( 'New Category Name', 'albatross-audio' ), 'menu_name' => __( 'Categories', 'albatross-audio' ), ); // register taxonomy register_taxonomy('albatross_audio_categories',array('albatross-audio'), array( 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, 'show_in_rest' => true, 'show_admin_column' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'category' ), )); } /*================================================================ * albatross_audio Admin Columns ==================================================================*/ public function albaau_albatross_audio_admin_columns( $columns ) { $columns = array( 'cb' => $columns['cb'], 'title' => __( 'Title', 'albatross-audio' ), 'songs' => __( 'Songs', 'albatross-audio' ), 'cats' => __( 'Categories', 'albatross-audio' ), 'shortcode' => __( 'Shortcode', 'albatross-audio' ), 'date' => $columns['date'], ); return $columns; } public function albaau_albatross_audio_admin_columns_data( $column, $post_id ) { // songs column if ( 'songs' === $column ) { $songs = get_post_meta($post_id, 'albatross_audio_songs', true ); if($songs) { echo ''.esc_html(count($songs)).''; } else { echo '0'; } } // shortcode column if ( 'shortcode' === $column ) { echo '[albatross-audio id='.esc_html(get_the_ID()).']'; } // cats column if ( 'cats' === $column ) { $terms = get_the_terms( get_the_ID(), 'albatross_audio_categories' ); if( ! empty($terms) ) { echo ''; if( is_array($terms) ) { foreach($terms as $term) { echo ''.esc_html($term->name).''; if (next($terms )) { echo ', '; } } } else { if (!is_wp_error($terms)) { if (!is_wp_error($terms)) { if (!is_wp_error($terms)) { if (!is_wp_error($terms) && isset($terms->name)) { echo esc_html($terms->name); } } } } } echo ''; } } } /*================================================================ * albatross_audio metabox ==================================================================*/ public function albaau_albatross_audio_songs_metabox() { add_meta_box( /** * Metabox ID for the Albatross Audio plugin. * * This ID is used to register the metabox for Albatross songs in the WordPress admin area. * * @var string $blr_albatross_songs_metabox */ 'blr_albatross_songs_metabox', // Metabox ID __('Songs', 'albatross-audio'), // Metabox title array($this, 'albaau_albatross_metabox_callback'), 'albatross-audio', // Post type 'normal', // Context 'high' // Priority ); } public function albaau_albatross_metabox_callback() { // Get the current post ID $post_id = get_the_ID(); // Nonce for security wp_nonce_field('albaau_albatross_audio_metabox_nonce', 'albaau_albatross_audio_metabox_nonce'); $songs = get_post_meta($post_id, 'albatross_audio_songs', true ); // Retrieve stored warnings and display them if ( 'auto-draft' !== get_post_status( $post_id ) ) { $warnings = get_option('albaau_albatross_audio_warnings', []); if (!empty($warnings)) { foreach ($warnings as $warning) { echo '

' . esc_html($warning) . '

'; } // Clear warnings after displaying delete_option('blr_albatross_audio_warnings'); } } ?>
render_song_fields($songs[$i], $i); } } else { $this->render_song_fields(null, 0); } ?>
'', 'song_artist' => '', 'song_thumb' => '', 'song_file' => '', 'song_links' => '', ]; $song = wp_parse_args($song, $defaults); ?>

$value ) { ?>
albaau_albatross_AddPlayTime($play_times); } /******************************************************************************* Get playlist total play time *******************************************************************************/ public function albaau_albatross_AddPlayTime($play_times) { $minutes = 0; if (!empty($play_times)) { foreach ($play_times as $time) { if (preg_match('/^\d{2}:\d{2}$/', $time)) { list($hour, $minute) = explode(':', $time); } else { $hour = 0; $minute = 0; } $minutes += $hour * 60 + $minute; } } $hours = floor($minutes / 60); $minutes -= $hours * 60; return sprintf('%02d:%02d', $hours, $minutes); } /******************************************************************************* Set first thumb as featured image if not set *******************************************************************************/ public function albaau_modify_featured_image_text($content, $post_id) { $post_type = get_post_type($post_id); if ($post_type === 'albatross-audio') { $content = $content.'

If empty, the featured image will be set with the first available song thumbnail when post is saved.

'; } return $content; } public function albaau_update_featured_image_from_song_thumbnail($post_id) { // Check if a featured image is already set if (has_post_thumbnail($post_id)) { return; } // Get the songs metadata $songs = get_post_meta($post_id, 'albatross_audio_songs', true); if (!empty($songs)) { foreach ($songs as $song) { if (!empty($song['song_thumb'])) { $thumbnail_url = esc_url_raw($song['song_thumb']); // Get the attachment ID from the URL $attachment_id = attachment_url_to_postid($thumbnail_url); // If a valid attachment ID is found, set it as the featured image if ($attachment_id) { set_post_thumbnail($post_id, $attachment_id); return; // Stop after setting the first valid image } } } } } /******************************************************************************* Save Functions *******************************************************************************/ public function albaau_albatross_audio_save_metabox($post_id) { // Check if our nonce is set. if ( ! isset($_POST['albaau_albatross_audio_metabox_nonce']) ) { return; } // Verify that the nonce is valid. if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['albaau_albatross_audio_metabox_nonce'] ) ), 'albaau_albatross_audio_metabox_nonce' ) ) { return; } // If this is an autosave, our form has not been submitted, so we don't want to do anything. if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return; } // Check the user's permissions. if ( ! current_user_can('edit_post', $post_id) ) { return; } // Audio $songs = []; $warnings = []; if (!empty($_POST['albatross_song_title'])) { $count = count($_POST['albatross_song_title']); for ($i = 0; $i < $count; $i++) { $song_title = isset( $_POST['albatross_song_title'][$i] ) ? sanitize_text_field( wp_unslash( $_POST['albatross_song_title'][$i] ) ) : ''; $song_artist = isset( $_POST['albatross_song_artist'][$i] ) ? sanitize_text_field( wp_unslash( $_POST['albatross_song_artist'][$i] ) ) : ''; $song_file = isset( $_POST['albatross_song_file'][$i] ) ? esc_url_raw( wp_unslash( $_POST['albatross_song_file'][$i] ) ) : ''; $song_thumb = isset( $_POST['albatross_song_thumb'][$i] ) ? esc_url_raw( wp_unslash( $_POST['albatross_song_thumb'][$i] ) ) : ''; $songs[$i] = [ 'song_title' => $song_title, 'song_artist' => $song_artist, 'song_file' => $song_file, 'song_thumb' => $song_thumb, ]; // Validate URL if (!$this->albaau_is_valid_url($song_file)) { $warnings[] = "Warning: Invalid URL on song \"$song_title\". This song will not be included in the playlist."; } elseif (!$this->albaau_is_playable_url($song_file)) { $warnings[] = "Warning: The song \"$song_title\" is not playable. This song will not be included in the playlist."; } // Allow plugins to modify or add extra fields before saving $songs[$i] = apply_filters('albatross_audio_save_song_data', $songs[$i], $i, $post_id); } // Save the 'albatross_audio_songs' array as a serialized string update_post_meta($post_id, 'albatross_audio_songs', $songs); if (!empty($warnings)) { update_option('albaau_albatross_audio_warnings', $warnings); } } } // end blr_save_songs_metabox // Validate URL /** * Validate URL. * * @param string $url The URL to validate. * @return bool True if valid, false otherwise. */ public function albaau_is_valid_url($url) { return filter_var($url, FILTER_VALIDATE_URL) !== false; } // Check if URL is playable public function albaau_is_playable_url($url) { $headers = get_headers($url, 1); if ($headers === false || strpos($headers[0], '200') === false) { return false; } return isset($headers['Content-Type']) && (strpos($headers['Content-Type'], 'audio/') !== false); } // Display admin notices public function albaau_blr_display_song_file_warnings() { $warnings = get_option('albaau_albatross_audio_warnings', []); if (!empty($warnings)) { foreach ($warnings as $warning) { echo "

" . esc_html($warning) . "

"; } // Clear the warnings after displaying delete_option('albaau_albatross_audio_warnings'); } } } // end class ALBAAU_Albatross_Audio_Admin