533 lines
18 KiB
PHP
533 lines
18 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Plugin Name: PeerTube Video Gallery
|
|
* Description: A plugin to display a video gallery with categories visible to certain user groups.
|
|
* Version: 0.1
|
|
* Author: Hannah L (@hannah:private.coffee)
|
|
* License: MIT
|
|
*/
|
|
|
|
// Make sure we don't expose any info if called directly
|
|
if (!function_exists('add_action')) {
|
|
echo 'Hi there! I\'m just a plugin, not much I can do when called directly.';
|
|
exit;
|
|
}
|
|
|
|
// Register Custom Post Type for our Video Gallery
|
|
function register_video_gallery_cpt()
|
|
{
|
|
$args = array(
|
|
'public' => true,
|
|
'label' => 'Video',
|
|
'supports' => array('title', 'editor', 'thumbnail'),
|
|
'menu_icon' => 'dashicons-video-alt3',
|
|
);
|
|
|
|
register_post_type('video_gallery', $args);
|
|
}
|
|
|
|
add_action('init', 'register_video_gallery_cpt');
|
|
|
|
// Register Custom Taxonomies for Video Categories and User Groups
|
|
function register_video_gallery_taxonomies()
|
|
{
|
|
// Video Categories
|
|
$labels = array(
|
|
'name' => 'Video Categories',
|
|
'singular_name' => 'Video Category',
|
|
);
|
|
|
|
$args = array(
|
|
'hierarchical' => true,
|
|
'labels' => $labels,
|
|
);
|
|
|
|
register_taxonomy('video_category', array('video_gallery'), $args);
|
|
|
|
// User Groups
|
|
$labels = array(
|
|
'name' => 'User Groups',
|
|
'singular_name' => 'User Group',
|
|
);
|
|
|
|
$args = array(
|
|
'labels' => $labels,
|
|
// 'public' and 'show_ui' are false to hide from admin menu as we'll create custom management
|
|
'public' => false,
|
|
'show_ui' => false,
|
|
'rewrite' => false,
|
|
'hierarchical' => true,
|
|
);
|
|
|
|
register_taxonomy('user_group', 'user', $args);
|
|
}
|
|
|
|
add_action('init', 'register_video_gallery_taxonomies', 0);
|
|
|
|
// Function to embed PeerTube video
|
|
function output_video_html($video_post_id)
|
|
{
|
|
$video_html = get_post_meta($video_post_id, 'video_embed_html', true);
|
|
// Safety filter for executing HTML content on the page.
|
|
$video_html = wp_kses_post($video_html);
|
|
|
|
// Warn if the video embed code is empty
|
|
if (empty($video_html)) {
|
|
$video_html = '<p class="warning">No video embed code found.</p>';
|
|
}
|
|
|
|
return $video_html;
|
|
}
|
|
|
|
// Function to display user groups admin page in the admin menu
|
|
function user_groups_admin_submenu()
|
|
{
|
|
add_submenu_page(
|
|
'edit.php?post_type=video_gallery', // Parent slug
|
|
__('Manage User Groups', 'text-domain'), // Page title
|
|
__('User Groups', 'text-domain'), // Menu title
|
|
'manage_options', // Capability required
|
|
'manage-user-groups', // Menu slug
|
|
'user_groups_admin_page' // Callback function
|
|
);
|
|
}
|
|
|
|
add_action('admin_menu', 'user_groups_admin_submenu');
|
|
|
|
|
|
// Function to display user groups admin page
|
|
function user_groups_admin_page()
|
|
{
|
|
// Ensure user has the capability to manage options
|
|
if (!current_user_can('manage_options')) return;
|
|
|
|
if (isset($_POST['action']) && $_POST['action'] == 'add_user_group') {
|
|
// Check for nonces and user capabilities if necessary
|
|
$new_group_name = sanitize_text_field($_POST['user_group_name']);
|
|
wp_insert_term($new_group_name, 'user_group');
|
|
}
|
|
|
|
if (isset($_GET['delete_user_group'])) {
|
|
$term_id = intval($_GET['delete_user_group']);
|
|
wp_delete_term($term_id, 'user_group');
|
|
}
|
|
|
|
// List user groups
|
|
$user_groups = get_terms(array('taxonomy' => 'user_group', 'hide_empty' => false));
|
|
?>
|
|
<div class="wrap">
|
|
<h1><?php _e('Manage User Groups', 'text-domain'); ?></h1>
|
|
<form method="post">
|
|
<?php wp_nonce_field('add_user_group_nonce', 'add_user_group_nonce_field'); ?>
|
|
<input type="text" name="user_group_name" />
|
|
<input type="submit" value="<?php _e('Add New User Group', 'text-domain'); ?>">
|
|
<input type="hidden" name="action" value="add_user_group">
|
|
</form>
|
|
<ul>
|
|
<?php foreach ($user_groups as $group) : ?>
|
|
<li>
|
|
<?php echo $group->name; ?>
|
|
<a href="<?php echo esc_url(add_query_arg('delete_user_group', $group->term_id)); ?>" class="delete-user-group"><?php _e('Delete', 'text-domain'); ?></a>
|
|
</li>
|
|
<?php endforeach; ?>
|
|
</ul>
|
|
</div>
|
|
<?php
|
|
wp_enqueue_script('my-custom-script', plugin_dir_url(__FILE__) . 'js/my-custom-script.js', array('jquery'), null, true);
|
|
}
|
|
|
|
// Add user groups to user profile
|
|
function custom_user_group_profile_fields($user)
|
|
{
|
|
// Check for user capabilities
|
|
if (!current_user_can('assign_user_groups')) return;
|
|
|
|
// Get all the available user groups
|
|
$groups = get_terms('user_group', array('hide_empty' => false));
|
|
|
|
// If there is a WP_Error, let's log it and return early
|
|
if (is_wp_error($groups)) {
|
|
error_log('Failed to get user groups: ' . $groups->get_error_message());
|
|
return;
|
|
}
|
|
|
|
// Get the user's saved group
|
|
$user_groups = get_user_meta($user->ID, 'user_groups', true);
|
|
|
|
if (!is_array($user_groups)) {
|
|
$user_groups = array($user_groups); // Ensure $user_groups is always an array
|
|
}
|
|
|
|
// Debugging
|
|
error_log('User Groups: ' . print_r($user_groups, true));
|
|
|
|
?>
|
|
<h3><?php esc_html_e('User Groups', 'text-domain'); ?></h3>
|
|
<table class="form-table">
|
|
<tr>
|
|
<th><label for="user_groups"><?php esc_html_e('Groups', 'text-domain'); ?></label></th>
|
|
<td>
|
|
<?php
|
|
// Get all the available user groups
|
|
$groups = get_terms('user_group', array('hide_empty' => false));
|
|
?>
|
|
<select multiple name="user_groups[]" id="user_groups" style="width:100%;max-width:25em;">
|
|
<?php foreach ($groups as $group) : ?>
|
|
<option value="<?php echo esc_attr($group->term_id); ?>" <?php if (in_array($group->term_id, $user_groups)) echo 'selected="selected"'; ?>>
|
|
<?php echo esc_html($group->name); ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
<span class="description"><?php esc_html_e('Select the user group.', 'text-domain'); ?></span>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
<?php
|
|
}
|
|
|
|
add_action('show_user_profile', 'custom_user_group_profile_fields');
|
|
add_action('edit_user_profile', 'custom_user_group_profile_fields');
|
|
|
|
|
|
// Function to add user groups to user profile
|
|
function save_user_groups($user_id)
|
|
{
|
|
// Assume that the form will send an array of group IDs named 'user_groups'
|
|
if (isset($_POST['user_groups'])) {
|
|
$user_groups = array_map('sanitize_text_field', $_POST['user_groups']);
|
|
update_user_meta($user_id, 'user_groups', $user_groups);
|
|
}
|
|
}
|
|
|
|
add_action('personal_options_update', 'save_user_groups');
|
|
add_action('edit_user_profile_update', 'save_user_groups');
|
|
|
|
function video_category_add_group_field()
|
|
{
|
|
// This function generates the form fields for adding a new video category
|
|
?>
|
|
<div class="form-field">
|
|
<label for="video-category-group"><?php _e('User Groups Access', 'text-domain'); ?></label>
|
|
<select name="video_category_group[]" id="video-category-group" multiple>
|
|
<?php
|
|
$groups = get_terms('user_group', array('hide_empty' => false));
|
|
foreach ($groups as $group) :
|
|
?>
|
|
<option value="<?php echo esc_attr($group->term_id); ?>">
|
|
<?php echo esc_html($group->name); ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
<p class="description"><?php _e('Select the user groups that should have access to this category.', 'text-domain'); ?></p>
|
|
</div>
|
|
<?php
|
|
}
|
|
add_action('video_category_add_form_fields', 'video_category_add_group_field');
|
|
|
|
// Function to display user groups field for video category
|
|
function video_category_edit_group_field($term)
|
|
{
|
|
$term_id = $term->term_id;
|
|
$assigned_groups = get_term_meta($term_id, 'assigned_user_groups', true);
|
|
if (!is_array($assigned_groups)) {
|
|
$assigned_groups = array();
|
|
}
|
|
?>
|
|
<tr class="form-field">
|
|
<th scope="row" valign="top"><label for="video-category-group"><?php _e('User Groups Access', 'text-domain'); ?></label></th>
|
|
<td>
|
|
<select name="video_category_group[]" id="video-category-group" multiple>
|
|
<?php
|
|
$groups = get_terms('user_group', array('hide_empty' => false));
|
|
foreach ($groups as $group) :
|
|
?>
|
|
<option value="<?php echo esc_attr($group->term_id); ?>" <?php echo in_array($group->term_id, $assigned_groups) ? 'selected' : ''; ?>>
|
|
<?php echo esc_html($group->name); ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
<p class="description"><?php _e('Select the user groups that should have access to this category.', 'text-domain'); ?></p>
|
|
</td>
|
|
</tr>
|
|
<?php
|
|
}
|
|
|
|
add_action('video_category_edit_form_fields', 'video_category_edit_group_field');
|
|
|
|
// Function to save user groups for video category
|
|
function save_video_category_group($term_id)
|
|
{
|
|
if (isset($_POST['video_category_group'])) {
|
|
$group_ids = array_map('intval', $_POST['video_category_group']);
|
|
update_term_meta($term_id, 'assigned_user_groups', $group_ids);
|
|
}
|
|
}
|
|
|
|
add_action('created_video_category', 'save_video_category_group');
|
|
add_action('edited_video_category', 'save_video_category_group');
|
|
|
|
|
|
// Shortcode to display videos based on user group
|
|
function display_videos_shortcode($atts) {
|
|
// Ensure a user is logged in
|
|
$user_id = get_current_user_id();
|
|
if (!$user_id) {
|
|
return 'You must be logged in to view videos.';
|
|
}
|
|
|
|
// Retrieves the user's groups
|
|
$user_groups = get_user_meta($user_id, 'user_groups', true);
|
|
if (empty($user_groups)) {
|
|
return 'You are not assigned to any user groups.';
|
|
}
|
|
|
|
// Fetches the categories that match the assigned user groups
|
|
$category_query_args = array(
|
|
'taxonomy' => 'video_category',
|
|
'hide_empty' => false
|
|
);
|
|
$categories = get_terms($category_query_args);
|
|
|
|
// Checks if no categories were found, returns a message
|
|
if (empty($categories) || is_wp_error($categories)) {
|
|
return 'No video categories found.';
|
|
}
|
|
|
|
// Initialize the output string
|
|
$output = '<div class="video-categories">';
|
|
|
|
// Pagination variables
|
|
$videos_per_page = 10; // Number of videos to display per page
|
|
$paged = get_query_var('paged') ? get_query_var('paged') : 1; // Current page number
|
|
|
|
// Loop through each category and get the associated user groups
|
|
foreach ($categories as $category) {
|
|
// Get the associated user groups for this category
|
|
$assigned_groups = get_term_meta($category->term_id, 'assigned_user_groups', true);
|
|
$matching_groups = array_intersect($assigned_groups, $user_groups);
|
|
|
|
if (!empty($matching_groups)) {
|
|
$output .= '<div class="video-category">';
|
|
$output .= '<h3>' . esc_html($category->name) . '</h3>'; // Category name
|
|
|
|
// Set up the query for the videos within the category
|
|
$video_query_args = array(
|
|
'post_type' => 'video_gallery',
|
|
'posts_per_page' => $videos_per_page,
|
|
'paged' => $paged,
|
|
'tax_query' => array(
|
|
array(
|
|
'taxonomy' => 'video_category',
|
|
'field' => 'term_id',
|
|
'terms' => $category->term_id,
|
|
),
|
|
),
|
|
);
|
|
|
|
// Execute the query
|
|
$video_query = new WP_Query($video_query_args);
|
|
|
|
// Check if the category has any videos
|
|
if ($video_query->have_posts()) {
|
|
// Loop through the videos and display them
|
|
while ($video_query->have_posts()) {
|
|
$video_query->the_post();
|
|
// Your markup for displaying the video details
|
|
$output .= '<div class="video">';
|
|
$output .= '<a href="' . get_post_permalink() . '"><h4>' . get_the_title() . '</h4></a>';
|
|
$output .= output_video_html(get_the_ID());
|
|
$output .= '</div>';
|
|
}
|
|
|
|
// Pagination
|
|
$pagination_args = array(
|
|
'base' => get_pagenum_link(1) . '%_%',
|
|
'format' => 'page/%#%',
|
|
'current' => $paged,
|
|
'total' => $video_query->max_num_pages,
|
|
);
|
|
$pagination = paginate_links($pagination_args);
|
|
|
|
if ($pagination) {
|
|
$output .= '<div class="pagination">' . $pagination . '</div>';
|
|
}
|
|
|
|
// Reset post data to the default query
|
|
wp_reset_postdata();
|
|
|
|
} else {
|
|
$output .= '<p>No videos available in this category.</p>';
|
|
}
|
|
$output .= '</div>'; // Close the video-category div
|
|
}
|
|
}
|
|
$output .= '</div>'; // Close the wrap div
|
|
return $output;
|
|
}
|
|
|
|
add_shortcode('display_videos', 'display_videos_shortcode');
|
|
|
|
// Function to display video embed code above the text content
|
|
function add_video_above_content() {
|
|
// Get the text content
|
|
$content = get_the_content();
|
|
|
|
// Check if we are on the single post page for the 'video_gallery' post type
|
|
if (is_singular('video_gallery')) {
|
|
// Fetch the video embed code from a custom field
|
|
$video_embed_html = get_post_meta(get_the_ID(), 'video_embed_html', true);
|
|
|
|
// Ensure $video_embed_html is not null
|
|
if (!empty($video_embed_html)) {
|
|
// Prepend the video embed HTML to the content being filtered
|
|
$content = '<div class="video-embed">' . wp_kses_post($video_embed_html) . '</div>' . $content;
|
|
}
|
|
}
|
|
|
|
// Return the possibly modified content
|
|
return $content;
|
|
}
|
|
|
|
// Assuming your theme has a 'the_content' hook, replace with actual hook name if different
|
|
add_action('the_content', 'add_video_above_content', 10);
|
|
|
|
|
|
// Function to add video embed code meta box to video gallery post type
|
|
function add_video_embed_meta_box()
|
|
{
|
|
add_meta_box(
|
|
'video_embed_code', // ID of the meta box
|
|
'Video Embed Code', // Title of the meta box
|
|
'video_embed_html_meta_box_callback', // Callback function
|
|
'video_gallery', // Post type
|
|
'normal', // Context where the box should show
|
|
'high' // Priority within the context
|
|
);
|
|
}
|
|
|
|
add_action('add_meta_boxes', 'add_video_embed_meta_box');
|
|
|
|
// Function to output video embed code meta box
|
|
function video_embed_html_meta_box_callback($post)
|
|
{
|
|
// Add a nonce field so we can check for it later.
|
|
wp_nonce_field('video_embed_code_nonce', 'video_embed_code_nonce_field');
|
|
|
|
// Get the current value if any
|
|
$embed_code = get_post_meta($post->ID, 'video_embed_html', true);
|
|
|
|
// Echo out the field
|
|
echo '<textarea id="video_embed_html" name="video_embed_html" rows="4" cols="50" class="widefat">' . esc_textarea($embed_code) . '</textarea>';
|
|
}
|
|
|
|
// Function to save video embed code meta box
|
|
function save_video_embed_code($post_id)
|
|
{
|
|
// Check if our nonce is set.
|
|
if (!isset($_POST['video_embed_code_nonce_field'])) {
|
|
return;
|
|
}
|
|
|
|
// Verify that the nonce is valid.
|
|
if (!wp_verify_nonce($_POST['video_embed_code_nonce_field'], 'video_embed_code_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 (isset($_POST['post_type']) && 'video_gallery' == $_POST['post_type']) {
|
|
if (!current_user_can('edit_post', $post_id)) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
// Make sure that it is set.
|
|
if (!isset($_POST['video_embed_html'])) {
|
|
return;
|
|
}
|
|
|
|
// Sanitize and save the input
|
|
$embed_code = wp_kses_post($_POST['video_embed_html']);
|
|
update_post_meta($post_id, 'video_embed_html', $embed_code);
|
|
}
|
|
|
|
add_action('save_post', 'save_video_embed_code');
|
|
|
|
|
|
// Enqueue necessary scripts and styles (if any)
|
|
function enqueue_plugin_scripts_and_styles()
|
|
{
|
|
// Example of including a CSS file
|
|
// wp_enqueue_style('video-gallery-style', plugins_url('/css/style.css', __FILE__));
|
|
}
|
|
|
|
add_action('wp_enqueue_scripts', 'enqueue_plugin_scripts_and_styles');
|
|
|
|
// Activation and Deactivation hooks
|
|
function plugin_activation()
|
|
{
|
|
register_video_gallery_cpt();
|
|
register_video_gallery_taxonomies();
|
|
|
|
flush_rewrite_rules();
|
|
}
|
|
|
|
register_activation_hook(__FILE__, 'plugin_activation');
|
|
|
|
function add_group_assignment_capability() {
|
|
// Get the administrator role
|
|
$admin_role = get_role('administrator');
|
|
|
|
// Add a new capability for group assignment
|
|
$admin_role->add_cap('assign_user_groups', true);
|
|
}
|
|
|
|
register_activation_hook(__FILE__, 'add_group_assignment_capability');
|
|
|
|
function plugin_deactivation()
|
|
{
|
|
flush_rewrite_rules();
|
|
}
|
|
|
|
register_deactivation_hook(__FILE__, 'plugin_deactivation');
|
|
|
|
function remove_group_assignment_capability() {
|
|
// Get the administrator role
|
|
$admin_role = get_role('administrator');
|
|
|
|
// Remove the capability for group assignment
|
|
$admin_role->remove_cap('assign_user_groups');
|
|
}
|
|
|
|
register_deactivation_hook(__FILE__, 'remove_group_assignment_capability');
|
|
|
|
// Function to allow iframe tags in posts
|
|
function custom_allow_iframe_tags_in_posts($allowedposttags)
|
|
{
|
|
$allowedposttags['iframe'] = array(
|
|
'align' => true,
|
|
'width' => true,
|
|
'height' => true,
|
|
'frameborder' => true,
|
|
'name' => true,
|
|
'src' => true,
|
|
'id' => true,
|
|
'class' => true,
|
|
'style' => true,
|
|
'scrolling' => true,
|
|
'marginwidth' => true,
|
|
'marginheight' => true,
|
|
'allowfullscreen' => true,
|
|
'sandbox' => true,
|
|
'title' => true
|
|
);
|
|
|
|
return $allowedposttags;
|
|
}
|
|
|
|
add_filter('wp_kses_allowed_html', 'custom_allow_iframe_tags_in_posts', 1);
|