188 lines
7.4 KiB
PHP
188 lines
7.4 KiB
PHP
<?php
|
|
/*
|
|
Plugin Name: Super Simple Slideshow
|
|
Description: A simple image slideshow managed from the WordPress admin area.
|
|
Version: 1.1.0
|
|
Author: ai-bot
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
add_action('admin_menu', function () {
|
|
add_menu_page(
|
|
'Super Simple Slideshow',
|
|
'Super Simple Slideshow',
|
|
'manage_options',
|
|
'super-simple-slideshow',
|
|
'super_simple_slideshow_admin'
|
|
);
|
|
});
|
|
|
|
add_action('admin_enqueue_scripts', function ($hook) {
|
|
if ($hook !== 'toplevel_page_super-simple-slideshow') {
|
|
return;
|
|
}
|
|
|
|
wp_enqueue_media();
|
|
wp_enqueue_script('jquery');
|
|
});
|
|
|
|
/**
|
|
* Return valid image attachment IDs from a comma-separated value.
|
|
*
|
|
* @param string $value Comma-separated attachment IDs.
|
|
* @return int[]
|
|
*/
|
|
function super_simple_slideshow_image_ids($value)
|
|
{
|
|
if (!is_string($value) || $value === '') {
|
|
return array();
|
|
}
|
|
|
|
$ids = array_map('absint', explode(',', $value));
|
|
$ids = array_values(array_unique(array_filter($ids)));
|
|
|
|
return array_values(array_filter($ids, 'wp_attachment_is_image'));
|
|
}
|
|
|
|
function super_simple_slideshow_admin()
|
|
{
|
|
if (!current_user_can('manage_options')) {
|
|
return;
|
|
}
|
|
|
|
$notice = '';
|
|
if (isset($_POST['super_simple_save'])) {
|
|
check_admin_referer('super_simple_slideshow_save', 'super_simple_slideshow_nonce');
|
|
|
|
$submitted_ids = isset($_POST['super_simple_ids'])
|
|
? sanitize_text_field(wp_unslash($_POST['super_simple_ids']))
|
|
: '';
|
|
$ids = super_simple_slideshow_image_ids($submitted_ids);
|
|
update_option('super_simple_slideshow_ids', implode(',', $ids));
|
|
$notice = '<div class="notice notice-success"><p>' . esc_html__('Settings saved.', 'super-simple-slideshow') . '</p></div>';
|
|
}
|
|
|
|
$ids = super_simple_slideshow_image_ids((string) get_option('super_simple_slideshow_ids', ''));
|
|
?>
|
|
<div class="wrap">
|
|
<h1><?php echo esc_html__('Super Simple Slideshow', 'super-simple-slideshow'); ?></h1>
|
|
<?php echo $notice; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
|
|
<form method="post">
|
|
<?php wp_nonce_field('super_simple_slideshow_save', 'super_simple_slideshow_nonce'); ?>
|
|
<input type="hidden" id="super_simple_ids" name="super_simple_ids" value="<?php echo esc_attr(implode(',', $ids)); ?>">
|
|
|
|
<p><button type="button" class="button button-primary" id="super_simple_select"><?php echo esc_html__('Add images', 'super-simple-slideshow'); ?></button></p>
|
|
|
|
<div id="super_simple_preview" class="super-simple-preview">
|
|
<?php foreach ($ids as $id) : ?>
|
|
<div class="super-simple-image" data-id="<?php echo esc_attr($id); ?>" style="display:inline-block;position:relative;margin:5px;">
|
|
<?php echo wp_get_attachment_image($id, 'thumbnail', false, array('style' => 'width:120px;height:120px;object-fit:cover;display:block;')); ?>
|
|
<button type="button" class="button-link-delete super-simple-remove" aria-label="<?php echo esc_attr__('Remove image', 'super-simple-slideshow'); ?>" style="display:block;"><?php echo esc_html__('Remove', 'super-simple-slideshow'); ?></button>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
|
|
<p><button type="submit" name="super_simple_save" value="1" class="button button-primary"><?php echo esc_html__('Save', 'super-simple-slideshow'); ?></button></p>
|
|
</form>
|
|
</div>
|
|
|
|
<script>
|
|
jQuery(function ($) {
|
|
var $ids = $('#super_simple_ids');
|
|
var $preview = $('#super_simple_preview');
|
|
var frame;
|
|
|
|
function selectedIds() {
|
|
return $preview.find('.super-simple-image').map(function () {
|
|
return String($(this).data('id'));
|
|
}).get();
|
|
}
|
|
|
|
function syncIds() {
|
|
$ids.val(selectedIds().join(','));
|
|
}
|
|
|
|
function addPreviewImage(attachment) {
|
|
if ($preview.find('[data-id="' + attachment.id + '"]').length) {
|
|
return;
|
|
}
|
|
|
|
var imageUrl = attachment.sizes && attachment.sizes.thumbnail
|
|
? attachment.sizes.thumbnail.url
|
|
: attachment.url;
|
|
var $item = $('<div>', { class: 'super-simple-image', 'data-id': attachment.id })
|
|
.css({ display: 'inline-block', position: 'relative', margin: '5px' });
|
|
$('<img>', { src: imageUrl, alt: '', css: { width: '120px', height: '120px', objectFit: 'cover', display: 'block' } }).appendTo($item);
|
|
$('<button>', { type: 'button', class: 'button-link-delete super-simple-remove', text: 'Remove', 'aria-label': 'Remove image' }).css('display', 'block').appendTo($item);
|
|
$preview.append($item);
|
|
}
|
|
|
|
$preview.on('click', '.super-simple-remove', function () {
|
|
$(this).closest('.super-simple-image').remove();
|
|
syncIds();
|
|
});
|
|
|
|
$('#super_simple_select').on('click', function (event) {
|
|
event.preventDefault();
|
|
|
|
if (!frame) {
|
|
frame = wp.media({
|
|
title: 'Add images',
|
|
button: { text: 'Use selected images' },
|
|
multiple: true,
|
|
library: { type: 'image' }
|
|
});
|
|
|
|
frame.on('open', function () {
|
|
var selection = frame.state().get('selection');
|
|
selection.reset();
|
|
selectedIds().forEach(function (id) {
|
|
var attachment = wp.media.attachment(id);
|
|
attachment.fetch();
|
|
selection.add(attachment);
|
|
});
|
|
});
|
|
|
|
frame.on('select', function () {
|
|
frame.state().get('selection').each(function (attachment) {
|
|
addPreviewImage(attachment.toJSON());
|
|
});
|
|
syncIds();
|
|
});
|
|
}
|
|
|
|
frame.open();
|
|
});
|
|
});
|
|
</script>
|
|
<?php
|
|
}
|
|
|
|
add_shortcode('super_simple_slideshow', function () {
|
|
$ids = super_simple_slideshow_image_ids((string) get_option('super_simple_slideshow_ids', ''));
|
|
if (!$ids) {
|
|
return '';
|
|
}
|
|
|
|
wp_register_style('super-simple-slideshow', false, array(), '1.1.0');
|
|
wp_enqueue_style('super-simple-slideshow');
|
|
wp_add_inline_style('super-simple-slideshow', '.super-simple-slideshow .super-simple-slide{display:none;width:100%;height:auto}.super-simple-slideshow .super-simple-slide.active{display:block}');
|
|
|
|
wp_register_script('super-simple-slideshow', false, array(), '1.1.0', true);
|
|
wp_enqueue_script('super-simple-slideshow');
|
|
wp_add_inline_script('super-simple-slideshow', '(function(){document.querySelectorAll(".super-simple-slideshow").forEach(function(slideshow){var slides=slideshow.querySelectorAll(".super-simple-slide");if(slides.length<2){return;}var current=0;window.setInterval(function(){slides[current].classList.remove("active");current=(current+1)%slides.length;slides[current].classList.add("active");},5000);});})();');
|
|
|
|
ob_start();
|
|
?>
|
|
<div class="super-simple-slideshow">
|
|
<?php foreach ($ids as $index => $id) : ?>
|
|
<?php echo wp_get_attachment_image($id, 'full', false, array('class' => $index === 0 ? 'super-simple-slide active' : 'super-simple-slide')); ?>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php
|
|
return ob_get_clean();
|
|
});
|