Fix image selection, persistence, security, and slideshow output

This commit is contained in:
ai-bot committed 2026-10-13 08:57:30 +00:00
1 parent d47bf0885d
commit 9bde5fa87d
1 file changed
+152 -87
+152 -87
View File
@@ -1,122 +1,187 @@
<?php <?php
/* /*
Plugin Name: Super Simple Slideshow Plugin Name: Super Simple Slideshow
Description: Super simple slideshow plugin for Wordpress. Description: A simple image slideshow managed from the WordPress admin area.
Version: 1.0 Version: 1.1.0
Author: ai-bot Author: ai-bot
*/ */
if (!defined('ABSPATH')) exit;
add_action('admin_menu', function(){ if (!defined('ABSPATH')) {
add_menu_page('Super Simple Slideshow','Super Simple Slideshow','manage_options','super-simple-slideshow','super_simple_slideshow_admin'); 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){ add_action('admin_enqueue_scripts', function ($hook) {
if($hook !== 'toplevel_page_super-simple-slideshow') return; if ($hook !== 'toplevel_page_super-simple-slideshow') {
return;
}
wp_enqueue_media(); wp_enqueue_media();
wp_enqueue_script('jquery'); wp_enqueue_script('jquery');
}); });
function super_simple_slideshow_admin(){ /**
if(isset($_POST['super_simple_ids'])){ * Return valid image attachment IDs from a comma-separated value.
update_option('super_simple_slideshow_ids', sanitize_text_field($_POST['super_simple_ids'])); *
echo '<div class="notice notice-success"><p>Lagra.</p></div>'; * @param string $value Comma-separated attachment IDs.
* @return int[]
*/
function super_simple_slideshow_image_ids($value)
{
if (!is_string($value) || $value === '') {
return array();
} }
$ids = get_option('super_simple_slideshow_ids',''); $ids = array_map('absint', explode(',', $value));
?> $ids = array_values(array_unique(array_filter($ids)));
<div class="wrap">
<h1>Super Simple Slideshow</h1>
<form method="post">
<input type="hidden" id="super_simple_ids" name="super_simple_ids" value="<?php echo esc_attr($ids); ?>">
<p><button type="button" class="button button-primary" id="super_simple_select">Vel bilete</button></p> return array_values(array_filter($ids, 'wp_attachment_is_image'));
<div id="preview">
<?php
if(!empty($ids)){
foreach(explode(',', $ids) as $id){
echo wp_get_attachment_image((int)$id,'thumbnail',false,array(
'style'=>'margin:5px;width:120px;height:120px;object-fit:cover;'
));
}
}
?>
</div>
<p><input type="submit" class="button button-primary" value="Lagre"></p>
</form>
</div>
<script>
jQuery(function($){
$('#super_simple_select').on('click', function(e){
e.preventDefault();
var frame = wp.media({
title:'Vel bilete',
multiple:true,
library:{type:'image'}
});
frame.on('select', function(){
var ids=[];
var html='';
frame.state().get('selection').each(function(item){
item=item.toJSON();
ids.push(item.id);
if(item.sizes && item.sizes.thumbnail){
html += '<img src="'+item.sizes.thumbnail.url+'" style="margin:5px;width:120px;height:120px;object-fit:cover;">';
}
});
$('#super_simple_ids').val(ids.join(','));
$('#preview').html(html);
});
frame.open();
});
});
</script>
<?php
} }
add_shortcode('super_simple_slideshow', function(){ function super_simple_slideshow_admin()
{
if (!current_user_can('manage_options')) {
return;
}
$ids = get_option('super_simple_slideshow_ids',''); $notice = '';
if(empty($ids)) return ''; if (isset($_POST['super_simple_save'])) {
check_admin_referer('super_simple_slideshow_save', 'super_simple_slideshow_nonce');
$ids = explode(',', $ids); $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>';
}
ob_start(); $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)); ?>">
<div class="super-simple-slideshow"> <p><button type="button" class="button button-primary" id="super_simple_select"><?php echo esc_html__('Add images', 'super-simple-slideshow'); ?></button></p>
<?php foreach($ids as $i => $id): ?>
<?php echo wp_get_attachment_image((int)$id,'full',false,array( <div id="super_simple_preview" class="super-simple-preview">
'class' => $i === 0 ? 'super-simple-slide active' : 'super-simple-slide' <?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; ?> <?php endforeach; ?>
</div> </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> <script>
document.addEventListener('DOMContentLoaded', function(){ jQuery(function ($) {
const slides=document.querySelectorAll('.super-simple-slideshow .super-simple-slide'); var $ids = $('#super_simple_ids');
var $preview = $('#super_simple_preview');
var frame;
if(slides.length < 2) return; function selectedIds() {
return $preview.find('.super-simple-image').map(function () {
return String($(this).data('id'));
}).get();
}
let current=0; function syncIds() {
$ids.val(selectedIds().join(','));
}
setInterval(function(){ function addPreviewImage(attachment) {
slides[current].classList.remove('active'); if ($preview.find('[data-id="' + attachment.id + '"]').length) {
current=(current+1)%slides.length; return;
slides[current].classList.add('active'); }
},5000);
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> </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 <?php
return ob_get_clean(); return ob_get_clean();
}); });