Fix image selection, persistence, security, and slideshow output
This commit is contained in:
1 parent
d47bf0885d
commit
9bde5fa87d
1 file changed
+143
-78
+143
-78
@@ -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)));
|
||||||
|
|
||||||
|
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">
|
<div class="wrap">
|
||||||
<h1>Super Simple Slideshow</h1>
|
<h1><?php echo esc_html__('Super Simple Slideshow', 'super-simple-slideshow'); ?></h1>
|
||||||
|
<?php echo $notice; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
|
||||||
<form method="post">
|
<form method="post">
|
||||||
<input type="hidden" id="super_simple_ids" name="super_simple_ids" value="<?php echo esc_attr($ids); ?>">
|
<?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">Vel bilete</button></p>
|
<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="preview">
|
<div id="super_simple_preview" class="super-simple-preview">
|
||||||
<?php
|
<?php foreach ($ids as $id) : ?>
|
||||||
if(!empty($ids)){
|
<div class="super-simple-image" data-id="<?php echo esc_attr($id); ?>" style="display:inline-block;position:relative;margin:5px;">
|
||||||
foreach(explode(',', $ids) as $id){
|
<?php echo wp_get_attachment_image($id, 'thumbnail', false, array('style' => 'width:120px;height:120px;object-fit:cover;display:block;')); ?>
|
||||||
echo wp_get_attachment_image((int)$id,'thumbnail',false,array(
|
<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>
|
||||||
'style'=>'margin:5px;width:120px;height:120px;object-fit:cover;'
|
</div>
|
||||||
));
|
<?php endforeach; ?>
|
||||||
}
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p><input type="submit" class="button button-primary" value="Lagre"></p>
|
<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>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
jQuery(function($){
|
jQuery(function ($) {
|
||||||
$('#super_simple_select').on('click', function(e){
|
var $ids = $('#super_simple_ids');
|
||||||
e.preventDefault();
|
var $preview = $('#super_simple_preview');
|
||||||
|
var frame;
|
||||||
|
|
||||||
var frame = wp.media({
|
function selectedIds() {
|
||||||
title:'Vel bilete',
|
return $preview.find('.super-simple-image').map(function () {
|
||||||
multiple:true,
|
return String($(this).data('id'));
|
||||||
library:{type:'image'}
|
}).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();
|
||||||
});
|
});
|
||||||
|
|
||||||
frame.on('select', function(){
|
$('#super_simple_select').on('click', function (event) {
|
||||||
var ids=[];
|
event.preventDefault();
|
||||||
var html='';
|
|
||||||
|
|
||||||
frame.state().get('selection').each(function(item){
|
if (!frame) {
|
||||||
item=item.toJSON();
|
frame = wp.media({
|
||||||
ids.push(item.id);
|
title: 'Add images',
|
||||||
|
button: { text: 'Use selected images' },
|
||||||
|
multiple: true,
|
||||||
|
library: { type: 'image' }
|
||||||
|
});
|
||||||
|
|
||||||
if(item.sizes && item.sizes.thumbnail){
|
frame.on('open', function () {
|
||||||
html += '<img src="'+item.sizes.thumbnail.url+'" style="margin:5px;width:120px;height:120px;object-fit:cover;">';
|
var selection = frame.state().get('selection');
|
||||||
}
|
selection.reset();
|
||||||
});
|
selectedIds().forEach(function (id) {
|
||||||
|
var attachment = wp.media.attachment(id);
|
||||||
|
attachment.fetch();
|
||||||
|
selection.add(attachment);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
$('#super_simple_ids').val(ids.join(','));
|
frame.on('select', function () {
|
||||||
$('#preview').html(html);
|
frame.state().get('selection').each(function (attachment) {
|
||||||
|
addPreviewImage(attachment.toJSON());
|
||||||
|
});
|
||||||
|
syncIds();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
frame.open();
|
||||||
});
|
});
|
||||||
|
|
||||||
frame.open();
|
|
||||||
});
|
});
|
||||||
});
|
</script>
|
||||||
</script>
|
<?php
|
||||||
<?php
|
|
||||||
}
|
}
|
||||||
|
|
||||||
add_shortcode('super_simple_slideshow', function(){
|
add_shortcode('super_simple_slideshow', function () {
|
||||||
|
$ids = super_simple_slideshow_image_ids((string) get_option('super_simple_slideshow_ids', ''));
|
||||||
|
if (!$ids) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
$ids = get_option('super_simple_slideshow_ids','');
|
wp_register_style('super-simple-slideshow', false, array(), '1.1.0');
|
||||||
if(empty($ids)) return '';
|
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}');
|
||||||
|
|
||||||
$ids = explode(',', $ids);
|
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();
|
ob_start();
|
||||||
?>
|
?>
|
||||||
|
|
||||||
<div class="super-simple-slideshow">
|
<div class="super-simple-slideshow">
|
||||||
<?php foreach($ids as $i => $id): ?>
|
<?php foreach ($ids as $index => $id) : ?>
|
||||||
<?php echo wp_get_attachment_image((int)$id,'full',false,array(
|
<?php echo wp_get_attachment_image($id, 'full', false, array('class' => $index === 0 ? 'super-simple-slide active' : 'super-simple-slide')); ?>
|
||||||
'class' => $i === 0 ? 'super-simple-slide active' : 'super-simple-slide'
|
<?php endforeach; ?>
|
||||||
)); ?>
|
|
||||||
<?php endforeach; ?>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
|
||||||
document.addEventListener('DOMContentLoaded', function(){
|
|
||||||
const slides=document.querySelectorAll('.super-simple-slideshow .super-simple-slide');
|
|
||||||
|
|
||||||
if(slides.length < 2) return;
|
|
||||||
|
|
||||||
let current=0;
|
|
||||||
|
|
||||||
setInterval(function(){
|
|
||||||
slides[current].classList.remove('active');
|
|
||||||
current=(current+1)%slides.length;
|
|
||||||
slides[current].classList.add('active');
|
|
||||||
},5000);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
return ob_get_clean();
|
return ob_get_clean();
|
||||||
});
|
});
|
||||||
Reference in new issue
Block a user