plugin_name = $plugin_name;
$this->version = $version;
}
public function enqueue_styles() {
wp_enqueue_style(
$this->plugin_name,
plugin_dir_url(__FILE__) . 'css/gallery-admin.css',
array(),
$this->version,
'all'
);
// Add Dashicons for feature icons
wp_enqueue_style('dashicons');
}
public function enqueue_scripts() {
wp_enqueue_media();
// Enqueue jQuery UI for sortable functionality
wp_enqueue_script('jquery-ui-core');
wp_enqueue_script('jquery-ui-sortable');
wp_enqueue_script(
$this->plugin_name,
plugin_dir_url(__FILE__) . 'js/gallery-admin.js',
array('jquery', 'jquery-ui-sortable'),
$this->version,
false
);
}
public function add_admin_menu() {
add_management_page(
'Advanced Gallery Fields Settings',
'Advanced Gallery Fields',
'manage_options',
'advanced-gallery-fields',
array($this, 'display_settings_page')
);
}
public function register_settings() {
register_setting(
'advanced_gallery_settings',
'advanced_gallery_post_types',
array(
'type' => 'array',
'sanitize_callback' => array($this, 'sanitize_post_types'),
)
);
}
/**
* Sanitize the post types array
*
* @param array $input Array of post types to sanitize
* @return array Sanitized array of post types
*/
public function sanitize_post_types($input) {
if (!is_array($input)) {
return array('post');
}
return array_map('sanitize_key', $input);
}
public function display_settings_page() {
$post_types = get_post_types(['public' => true], 'objects');
$saved_post_types = get_option('advanced_gallery_post_types', ['post']);
?>
ID, '_advanced_gallery', true);
$gallery_ids = $this->sanitize_gallery_ids($gallery_ids);
?>
is_valid_attachment($image_id)) {
continue;
}
$image_html = wp_get_attachment_image($image_id, 'thumbnail');
if ($image_html) {
echo '
';
echo wp_kses_post($image_html);
echo '
';
echo '';
echo '
';
}
}
}
?>
sanitize_gallery_ids(sanitize_text_field(wp_unslash($_POST['gallery_data'])));
if ($gallery_data !== false) {
update_post_meta($post_id, '_advanced_gallery', $gallery_data);
}
}
}
/**
* Sanitize and validate gallery IDs
*
* @param string $gallery_ids Comma-separated list of gallery image IDs
* @return string|false Sanitized comma-separated list of gallery image IDs, or false on error
*/
private function sanitize_gallery_ids($gallery_ids) {
if (empty($gallery_ids)) {
return '';
}
// Basic input validation
if (!is_string($gallery_ids)) {
return false;
}
// Convert to array and ensure all values are numeric
$ids_array = array_filter(
explode(',', $gallery_ids),
function($value) {
return is_numeric(trim($value));
}
);
if (empty($ids_array)) {
return false;
}
// Convert all IDs to positive integers and remove duplicates
$ids_array = array_unique(array_map('absint', $ids_array));
// Filter out any invalid attachment IDs
$valid_ids = array_filter($ids_array, array($this, 'is_valid_attachment'));
if (empty($valid_ids)) {
return false;
}
return implode(',', $valid_ids);
}
/**
* Validate if an ID represents a valid attachment
*
* @param int $attachment_id The attachment ID to validate
* @return bool Whether the attachment ID is valid
*/
private function is_valid_attachment($attachment_id) {
$attachment = get_post($attachment_id);
return !empty($attachment) && $attachment->post_type === 'attachment';
}
}