first commit
This commit is contained in:
563
wp-content/plugins/svg-support/functions/attachment.php
Normal file
563
wp-content/plugins/svg-support/functions/attachment.php
Normal file
@@ -0,0 +1,563 @@
|
||||
<?php
|
||||
/**
|
||||
* SVG Support in attachment modal
|
||||
*
|
||||
* This file contains functions to manage and manipulate SVG attachments in WordPress.
|
||||
* It includes functionality for displaying SVGs in the attachment modal, generating metadata,
|
||||
* sanitizing SVG files, and handling specific SVG-related scenarios.
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
/**
|
||||
* Add SVG dimensions and other details to the response for displaying in the attachment modal.
|
||||
*
|
||||
* @param array $response The prepared attachment response data.
|
||||
* @param object $attachment The attachment object.
|
||||
* @param array $meta The attachment meta data.
|
||||
*
|
||||
* @return array Modified response with SVG dimensions and URL.
|
||||
*/
|
||||
function bodhi_svgs_response_for_svg( $response, $attachment, $meta ) {
|
||||
|
||||
if ( $response['mime'] == 'image/svg+xml' && empty( $response['sizes'] ) ) {
|
||||
|
||||
$svg_path = get_attached_file( $attachment->ID );
|
||||
|
||||
if ( ! file_exists( $svg_path ) ) {
|
||||
// If SVG is external, use the URL instead of the path
|
||||
$svg_path = $response['url'];
|
||||
}
|
||||
|
||||
$dimensions = bodhi_svgs_get_dimensions( $svg_path );
|
||||
|
||||
$response['sizes'] = array(
|
||||
'full' => array(
|
||||
'url' => $response['url'],
|
||||
'width' => $dimensions->width,
|
||||
'height' => $dimensions->height,
|
||||
'orientation' => $dimensions->width > $dimensions->height ? 'landscape' : 'portrait'
|
||||
)
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
return $response;
|
||||
|
||||
}
|
||||
add_filter( 'wp_prepare_attachment_for_js', 'bodhi_svgs_response_for_svg', 10, 3 );
|
||||
|
||||
/**
|
||||
* Get the dimensions of an SVG file.
|
||||
*
|
||||
* This function checks if the SVG is a local file or a remote URL, retrieves its content, and
|
||||
* parses the width and height from the SVG attributes.
|
||||
*
|
||||
* @param string $svg The file path or URL of the SVG.
|
||||
*
|
||||
* @return object An object containing the width and height of the SVG.
|
||||
*/
|
||||
function bodhi_svgs_get_dimensions( $svg ) {
|
||||
|
||||
$svg_content = '';
|
||||
|
||||
// Check if $svg is a URL or a local file path
|
||||
if ( filter_var( $svg, FILTER_VALIDATE_URL ) ) {
|
||||
// For remote SVGs, use wp_remote_get()
|
||||
$response = wp_remote_get( $svg );
|
||||
if ( is_wp_error( $response ) ) {
|
||||
return (object) array( 'width' => 0, 'height' => 0 );
|
||||
}
|
||||
$svg_content = wp_remote_retrieve_body( $response );
|
||||
} else {
|
||||
// For local files, use WP_Filesystem to read the file content
|
||||
if ( ! function_exists( 'WP_Filesystem' ) ) {
|
||||
require_once ABSPATH . 'wp-admin/includes/file.php';
|
||||
}
|
||||
global $wp_filesystem;
|
||||
WP_Filesystem();
|
||||
$svg_content = $wp_filesystem->get_contents( $svg );
|
||||
}
|
||||
|
||||
if ( empty( $svg_content ) ) {
|
||||
return (object) array( 'width' => 0, 'height' => 0 );
|
||||
}
|
||||
|
||||
$svg = simplexml_load_string( $svg_content );
|
||||
|
||||
if ( $svg === FALSE ) {
|
||||
$width = '0';
|
||||
$height = '0';
|
||||
} else {
|
||||
$attributes = $svg->attributes();
|
||||
$width = (string) $attributes->width;
|
||||
$height = (string) $attributes->height;
|
||||
}
|
||||
|
||||
return (object) array( 'width' => $width, 'height' => $height );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate attachment metadata for SVGs. (Thanks @surml)
|
||||
*
|
||||
* This function generates metadata for SVG attachments, including dimensions and file path.
|
||||
* It is used to fix warnings related to missing width and height metadata for SVGs.
|
||||
*
|
||||
* @param array $metadata The attachment metadata.
|
||||
* @param int $attachment_id The attachment ID.
|
||||
*
|
||||
* @return array Modified metadata including SVG dimensions.
|
||||
*/
|
||||
function bodhi_svgs_generate_svg_attachment_metadata( $metadata, $attachment_id ) {
|
||||
|
||||
$mime = get_post_mime_type( $attachment_id );
|
||||
|
||||
if ( $mime == 'image/svg+xml' ) {
|
||||
|
||||
$svg_path = get_attached_file( $attachment_id );
|
||||
$upload_dir = wp_upload_dir();
|
||||
// Get the path relative to /uploads/
|
||||
$relative_path = $svg_path ? str_replace($upload_dir['basedir'], '', $svg_path) : '';
|
||||
$filename = basename( $svg_path );
|
||||
|
||||
$dimensions = bodhi_svgs_get_dimensions( $svg_path );
|
||||
|
||||
$metadata = array(
|
||||
'width' => intval($dimensions->width),
|
||||
'height' => intval($dimensions->height),
|
||||
'file' => $relative_path
|
||||
);
|
||||
|
||||
$height = intval($dimensions->height);
|
||||
$width = intval($dimensions->width);
|
||||
|
||||
// Generate sizes array for future implementations, if needed
|
||||
$sizes = array();
|
||||
foreach ( get_intermediate_image_sizes() as $s ) {
|
||||
|
||||
$sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => false );
|
||||
|
||||
if ( $width !== 0 && $height !== 0 ) {
|
||||
|
||||
if ( isset( $_wp_additional_image_sizes[$s]['width'] ) ) {
|
||||
$width_current_size = intval( $_wp_additional_image_sizes[$s]['width'] );
|
||||
} else {
|
||||
$width_current_size = get_option( "{$s}_size_w" );
|
||||
}
|
||||
|
||||
if ( $width > $height ) {
|
||||
$ratio = round($width / $height, 2);
|
||||
$new_height = round($width_current_size / $ratio);
|
||||
} else {
|
||||
$ratio = round($height / $width, 2);
|
||||
$new_height = round($width_current_size * $ratio);
|
||||
}
|
||||
|
||||
$sizes[$s]['width'] = $width_current_size;
|
||||
$sizes[$s]['height'] = $new_height;
|
||||
|
||||
$sizes[$s]['crop'] = false;
|
||||
|
||||
} else {
|
||||
|
||||
if ( isset( $_wp_additional_image_sizes[$s]['width'] ) ) {
|
||||
$sizes[$s]['width'] = intval( $_wp_additional_image_sizes[$s]['width'] );
|
||||
} else {
|
||||
$sizes[$s]['width'] = get_option( "{$s}_size_w" );
|
||||
}
|
||||
|
||||
if ( isset( $_wp_additional_image_sizes[$s]['height'] ) ) {
|
||||
$sizes[$s]['height'] = intval( $_wp_additional_image_sizes[$s]['height'] );
|
||||
} else {
|
||||
$sizes[$s]['height'] = get_option( "{$s}_size_h" );
|
||||
}
|
||||
|
||||
if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) ) {
|
||||
$sizes[$s]['crop'] = intval( $_wp_additional_image_sizes[$s]['crop'] );
|
||||
} else {
|
||||
$sizes[$s]['crop'] = get_option( "{$s}_crop" );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$sizes[$s]['file'] = $filename;
|
||||
$sizes[$s]['mime-type'] = 'image/svg+xml';
|
||||
|
||||
}
|
||||
|
||||
$metadata['sizes'] = $sizes;
|
||||
|
||||
}
|
||||
|
||||
return $metadata;
|
||||
|
||||
}
|
||||
add_filter( 'wp_generate_attachment_metadata', 'bodhi_svgs_generate_svg_attachment_metadata', 10, 3 );
|
||||
|
||||
/**
|
||||
* Sanitize SVG files.
|
||||
*
|
||||
* This function sanitizes SVG files by removing potentially harmful elements and attributes.
|
||||
* It also optionally minifies the SVG content and re-encodes it if it was originally gzipped.
|
||||
*
|
||||
* @param string $file The file path of the SVG to sanitize.
|
||||
*
|
||||
* @return bool True if the file was successfully sanitized, false otherwise.
|
||||
*/
|
||||
function bodhi_svgs_sanitize( $file ){
|
||||
|
||||
global $sanitizer;
|
||||
|
||||
$sanitizer->setAllowedTags( new bodhi_svg_tags() );
|
||||
$sanitizer->setAllowedAttrs( new bodhi_svg_attributes() );
|
||||
|
||||
$dirty = '';
|
||||
|
||||
// Using WP_Filesystem to read the SVG file content
|
||||
if ( ! function_exists( 'WP_Filesystem' ) ) {
|
||||
require_once ABSPATH . 'wp-admin/includes/file.php';
|
||||
}
|
||||
global $wp_filesystem;
|
||||
WP_Filesystem();
|
||||
$dirty = $wp_filesystem->get_contents( $file );
|
||||
|
||||
// Try to decode if gzipped is enabled
|
||||
if ( $is_zipped = bodhi_svgs_is_gzipped( $dirty ) ) {
|
||||
|
||||
$dirty = gzdecode( $dirty );
|
||||
|
||||
// Return on failure, since we can't read file
|
||||
if ( $dirty === false ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Remove remote references since they are dangerous and lead to injection
|
||||
$sanitizer->removeRemoteReferences(true);
|
||||
|
||||
// Enable minify in library if its enabled in admin panel
|
||||
bodhi_svgs_minify();
|
||||
|
||||
$clean = $sanitizer->sanitize( $dirty );
|
||||
|
||||
if ( $clean === false ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// If the file was gzipped, we need to re-zip it
|
||||
if ( $is_zipped ) {
|
||||
$clean = gzencode( $clean );
|
||||
}
|
||||
|
||||
// Use WP_Filesystem to write the sanitized content back
|
||||
if ( ! $wp_filesystem->put_contents( $file, $clean, FS_CHMOD_FILE ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable minification for SVG files.
|
||||
*
|
||||
* This function enables minification for SVG files if the option is set in the plugin settings.
|
||||
*/
|
||||
function bodhi_svgs_minify() {
|
||||
|
||||
global $bodhi_svgs_options;
|
||||
global $sanitizer;
|
||||
|
||||
if ( !empty($bodhi_svgs_options['minify_svg']) && $bodhi_svgs_options['minify_svg'] === 'on' ) {
|
||||
$sanitizer->minify(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a string is gzipped.
|
||||
*
|
||||
* @param string $contents The contents to check.
|
||||
*
|
||||
* @return bool True if the contents are gzipped, false otherwise.
|
||||
*/
|
||||
function bodhi_svgs_is_gzipped( $contents ) {
|
||||
if ($contents === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( function_exists( 'mb_strpos' ) ) {
|
||||
return 0 === mb_strpos( $contents, "\x1f" . "\x8b" . "\x08" );
|
||||
} else {
|
||||
return 0 === strpos( $contents, "\x1f" . "\x8b" . "\x08" );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pre-filter for handling SVG uploads.
|
||||
*
|
||||
* This function checks if the uploaded file is an SVG and applies sanitization if required.
|
||||
*
|
||||
* @param array $file The uploaded file data.
|
||||
*
|
||||
* @return array The modified file data.
|
||||
*/
|
||||
function bodhi_svgs_sanitize_svg($file) {
|
||||
global $bodhi_svgs_options;
|
||||
|
||||
$file_path = $file['tmp_name'];
|
||||
$file_name = $file['name'];
|
||||
|
||||
// First quick check - if it's clearly not an SVG, return early
|
||||
if (!empty($file_name) && strtolower(pathinfo($file_name, PATHINFO_EXTENSION)) !== 'svg') {
|
||||
return $file;
|
||||
}
|
||||
|
||||
// Multiple validation checks for SVG
|
||||
if ( $file_path && file_exists( $file_path ) ) {
|
||||
// 1. Check MIME type using fileinfo
|
||||
$finfo = finfo_open( FILEINFO_MIME_TYPE );
|
||||
$real_mime = finfo_file( $finfo, $file_path );
|
||||
finfo_close( $finfo );
|
||||
|
||||
// 2. Read first bytes of the file to check for SVG header
|
||||
$file_content = file_get_contents( $file_path );
|
||||
|
||||
// Check for XML declaration and SVG tag
|
||||
$pattern1 = '/^[\s\n]*(?:<\?xml[^>]*>[\s\n]*)?(?:<!--.*?-->[\s\n]*)*(?:<!DOCTYPE[^>]*>[\s\n]*)?(?:<!--.*?-->[\s\n]*)*<svg[^>]*>/is';
|
||||
$pattern2 = '/^[\s\n]*(?:<!--.*?-->[\s\n]*)*<svg[^>]*>/is';
|
||||
|
||||
$match1 = preg_match( $pattern1, $file_content );
|
||||
$match2 = preg_match( $pattern2, $file_content );
|
||||
$has_closing = strpos( $file_content, '</svg>' ) !== false;
|
||||
|
||||
$is_svg_content = ( $match1 || $match2 ) && $has_closing;
|
||||
|
||||
// If content validation fails OR (mime type isn't SVG AND isn't a plain text file containing SVG)
|
||||
if ( !$is_svg_content ||
|
||||
( $real_mime !== 'image/svg+xml' &&
|
||||
$real_mime !== 'image/svg' &&
|
||||
!( $real_mime === 'text/plain' && $is_svg_content ) ) ) {
|
||||
$file['error'] = __( 'File is not a valid SVG.', 'svg-support' );
|
||||
return $file;
|
||||
}
|
||||
}
|
||||
|
||||
// Now we know it's an SVG, continue with security checks
|
||||
if (!defined('REST_REQUEST') && !wp_verify_nonce(
|
||||
sanitize_text_field(wp_unslash($_REQUEST['_wpnonce'] ?? '')),
|
||||
'media-form'
|
||||
)) {
|
||||
$file['error'] = __('Security check failed.', 'svg-support');
|
||||
return $file;
|
||||
}
|
||||
|
||||
// Get the roles that do not require SVG sanitization
|
||||
$sanitize_on_upload_roles_array = (array) $bodhi_svgs_options['sanitize_on_upload_roles'];
|
||||
$user = wp_get_current_user();
|
||||
$current_user_roles = (array) $user->roles;
|
||||
|
||||
// Check if the current user's roles intersect with the roles that do not need sanitization
|
||||
$no_sanitize_needed = array_intersect($sanitize_on_upload_roles_array, $current_user_roles);
|
||||
|
||||
// Check if the user has the capability to upload SVGs
|
||||
$can_upload_files = current_user_can('upload_files');
|
||||
|
||||
// Force sanitize unless user is in roles that bypass sanitization
|
||||
if ($can_upload_files && empty($no_sanitize_needed)) {
|
||||
global $sanitizer;
|
||||
|
||||
// Read file contents
|
||||
$file_content = file_get_contents($file_path);
|
||||
if ($file_content === false) {
|
||||
$file['error'] = __("Unable to read SVG file for sanitization.", 'svg-support');
|
||||
return $file;
|
||||
}
|
||||
|
||||
// Sanitize the content
|
||||
$clean_svg = $sanitizer->sanitize($file_content);
|
||||
|
||||
if ($clean_svg === false) {
|
||||
$file['error'] = __("Sorry, this file couldn't be sanitized for security reasons and wasn't uploaded.", 'svg-support');
|
||||
return $file;
|
||||
}
|
||||
|
||||
// Write sanitized content back
|
||||
$write_result = file_put_contents($file_path, $clean_svg);
|
||||
if ($write_result === false) {
|
||||
$file['error'] = __("Unable to save sanitized SVG file.", 'svg-support');
|
||||
return $file;
|
||||
}
|
||||
}
|
||||
|
||||
return $file;
|
||||
}
|
||||
// Add filter to handle upload pre-filtering for sanitization
|
||||
add_filter('wp_handle_upload_prefilter', 'bodhi_svgs_sanitize_svg');
|
||||
|
||||
/**
|
||||
* Fix for image widget PHP warnings related to metadata.
|
||||
*
|
||||
* @param array $data The attachment metadata.
|
||||
*
|
||||
* @return array|false The modified metadata or false if invalid.
|
||||
*/
|
||||
function bodhi_svgs_get_attachment_metadata( $data ) {
|
||||
|
||||
$res = $data;
|
||||
|
||||
if ( !isset( $data['width'] ) || !isset( $data['height'] ) ) {
|
||||
$res = false;
|
||||
}
|
||||
|
||||
return $res;
|
||||
|
||||
}
|
||||
// add_filter( 'wp_get_attachment_metadata' , 'bodhi_svgs_get_attachment_metadata' );
|
||||
// Commented this out 20200307 because it was stripping metadata from other attachments as well. Need to make this target only SVG attachments.
|
||||
|
||||
/**
|
||||
* Remove srcset attribute for SVG images.
|
||||
*
|
||||
* @param array $sources The sources array for the image.
|
||||
*
|
||||
* @return array The modified sources array.
|
||||
*/
|
||||
function bodhi_svgs_disable_srcset( $sources ) {
|
||||
|
||||
$first_element = reset($sources);
|
||||
if ( isset($first_element) && !empty($first_element['url']) ) {
|
||||
|
||||
$ext = pathinfo(reset($sources)['url'], PATHINFO_EXTENSION);
|
||||
|
||||
if ( $ext == 'svg' ) {
|
||||
|
||||
// return empty array
|
||||
$sources = array();
|
||||
|
||||
return $sources;
|
||||
|
||||
} else {
|
||||
|
||||
return $sources;
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
return $sources;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
add_filter( 'wp_calculate_image_srcset', 'bodhi_svgs_disable_srcset' );
|
||||
|
||||
/**
|
||||
* Fix for division by zero error for SVGs. (Proposed by @starsis)
|
||||
*
|
||||
* This function ensures that SVGs do not cause division by zero errors by providing default
|
||||
* dimensions if they are missing.
|
||||
*
|
||||
* @param array $image The image data.
|
||||
* @param int $attachment_id The attachment ID.
|
||||
* @param string $size The requested image size.
|
||||
* @param bool $icon Whether the image is an icon.
|
||||
*
|
||||
* @return array The modified image data.
|
||||
*/
|
||||
function bodhi_svgs_dimension_fallback( $image, $attachment_id, $size, $icon ) {
|
||||
|
||||
// only manipulate for svgs
|
||||
if ( get_post_mime_type($attachment_id) == 'image/svg+xml' ) {
|
||||
|
||||
if ( isset($image[1]) && $image[1] === 0 ) {
|
||||
$image[1] = 1;
|
||||
}
|
||||
if ( isset($image[2]) && $image[2] === 0 ) {
|
||||
$image[2] = 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $image;
|
||||
|
||||
}
|
||||
add_filter( 'wp_get_attachment_image_src', 'bodhi_svgs_dimension_fallback', 10, 4 );
|
||||
|
||||
/**
|
||||
* Pre-process SVG files uploaded via REST API
|
||||
*
|
||||
* @param array $file File data before processing
|
||||
* @param array $request The full request payload
|
||||
* @return array|WP_Error Modified file data or error
|
||||
*/
|
||||
function bodhi_svgs_rest_pre_upload($file, $request) {
|
||||
if ($file['type'] === 'image/svg+xml') {
|
||||
// Randomize filename for REST API uploads
|
||||
$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
|
||||
$random_name = wp_generate_password(12, false) . '.' . $ext;
|
||||
$file['name'] = sanitize_file_name($random_name);
|
||||
|
||||
// Force sanitization
|
||||
if (!bodhi_svgs_sanitize($file['tmp_name'])) {
|
||||
return new WP_Error(
|
||||
'svg_sanitization_failed',
|
||||
__('SVG sanitization failed for security reasons.', 'svg-support'),
|
||||
array('status' => 400)
|
||||
);
|
||||
}
|
||||
}
|
||||
return $file;
|
||||
}
|
||||
add_filter('rest_pre_upload_file', 'bodhi_svgs_rest_pre_upload', 10, 2);
|
||||
|
||||
function bodhi_svgs_handle_upload_check($fileinfo) {
|
||||
if ($fileinfo['type'] === 'image/svg+xml') {
|
||||
global $bodhi_last_upload_info;
|
||||
$bodhi_last_upload_info = $fileinfo;
|
||||
}
|
||||
return $fileinfo;
|
||||
}
|
||||
add_filter('wp_handle_upload', 'bodhi_svgs_handle_upload_check');
|
||||
|
||||
function bodhi_svgs_rest_insert_attachment($prepared_attachment, $request) {
|
||||
if ($request->get_header('content-type') !== 'image/svg+xml') {
|
||||
return $prepared_attachment;
|
||||
}
|
||||
|
||||
global $bodhi_svgs_options;
|
||||
$user = wp_get_current_user();
|
||||
$current_user_roles = (array) $user->roles;
|
||||
$sanitize_on_upload_roles_array = (array) $bodhi_svgs_options['sanitize_on_upload_roles'];
|
||||
|
||||
$should_sanitize = empty(array_intersect($sanitize_on_upload_roles_array, $current_user_roles));
|
||||
|
||||
if ($should_sanitize) {
|
||||
$file_path = get_attached_file($prepared_attachment->ID);
|
||||
if (!$file_path) {
|
||||
global $bodhi_last_upload_info;
|
||||
if (isset($bodhi_last_upload_info['file'])) {
|
||||
$file_path = $bodhi_last_upload_info['file'];
|
||||
}
|
||||
}
|
||||
|
||||
if ($file_path && file_exists($file_path)) {
|
||||
global $sanitizer;
|
||||
$file_content = file_get_contents($file_path);
|
||||
|
||||
if ($file_content !== false) {
|
||||
$clean_svg = $sanitizer->sanitize($file_content);
|
||||
if ($clean_svg !== false) {
|
||||
file_put_contents($file_path, $clean_svg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $prepared_attachment;
|
||||
}
|
||||
add_filter('rest_insert_attachment', 'bodhi_svgs_rest_insert_attachment', 10, 2);
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
/**
|
||||
* Attribute Control
|
||||
*/
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
/**
|
||||
* If in Advanced Mode
|
||||
*/
|
||||
if ( bodhi_svgs_advanced_mode() ) {
|
||||
|
||||
/**
|
||||
* Strip HTML of all attributes and add custom class if the file is .svg
|
||||
*/
|
||||
function bodhi_svgs_auto_insert_class( $html, $alt='' ) {
|
||||
|
||||
global $bodhi_svgs_options;
|
||||
|
||||
if ( ! empty( $bodhi_svgs_options['css_target'] ) ) {
|
||||
|
||||
// if custom class is set, use it
|
||||
$class = $bodhi_svgs_options['css_target'];
|
||||
|
||||
} else {
|
||||
|
||||
// if no custom class set, use default
|
||||
$class = 'style-svg';
|
||||
|
||||
}
|
||||
|
||||
// check if the src file has .svg extension
|
||||
if ( strpos( $html, '.svg' ) !== FALSE ) {
|
||||
|
||||
// strip html for svg files
|
||||
$html = preg_replace( '/(width|height|title|alt|class)=".*"\s/', 'class="' . esc_attr($class) . '"', $html );
|
||||
|
||||
} else {
|
||||
|
||||
// leave html intact for non-svg
|
||||
$html = $html;
|
||||
|
||||
}
|
||||
|
||||
return $html;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Fire auto insert class
|
||||
*/
|
||||
if ( ! empty( $bodhi_svgs_options['auto_insert_class'] ) ) {
|
||||
add_filter( 'image_send_to_editor', 'bodhi_svgs_auto_insert_class', 10 );
|
||||
// add_filter( 'post_thumbnail_html', 'bodhi_svgs_auto_insert_class', 10 );
|
||||
}
|
||||
|
||||
}
|
||||
145
wp-content/plugins/svg-support/functions/enqueue.php
Normal file
145
wp-content/plugins/svg-support/functions/enqueue.php
Normal file
@@ -0,0 +1,145 @@
|
||||
<?php
|
||||
/**
|
||||
* Enqueue scripts and styles
|
||||
* This file is to enqueue the scripts and styles both admin and front end
|
||||
*/
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue the admin CSS using screen check functions
|
||||
*/
|
||||
function bodhi_svgs_admin_css() {
|
||||
global $svgs_plugin_version;
|
||||
|
||||
// Check if user is on SVG Support settings page or media library page
|
||||
if ( bodhi_svgs_specific_pages_settings() || bodhi_svgs_specific_pages_media_library() ) {
|
||||
wp_enqueue_style( 'bodhi-svgs-admin', BODHI_SVGS_PLUGIN_URL . 'css/svgs-admin.css', array(), $svgs_plugin_version );
|
||||
}
|
||||
|
||||
// Check if user is on SVG Support settings page and not in "Advanced Mode"
|
||||
if ( bodhi_svgs_specific_pages_settings() && ! bodhi_svgs_advanced_mode() ) {
|
||||
wp_enqueue_style( 'bodhi-svgs-admin-simple-mode', BODHI_SVGS_PLUGIN_URL . 'css/svgs-admin-simple-mode.css', array(), $svgs_plugin_version );
|
||||
}
|
||||
|
||||
// Check if user is on an edit post page
|
||||
if ( bodhi_svgs_is_edit_page() ) {
|
||||
wp_enqueue_style( 'bodhi-svgs-admin-edit-post', BODHI_SVGS_PLUGIN_URL . 'css/svgs-admin-edit-post.css', array(), $svgs_plugin_version );
|
||||
}
|
||||
}
|
||||
add_action( 'admin_enqueue_scripts', 'bodhi_svgs_admin_css' );
|
||||
|
||||
/*
|
||||
* Enqueue multiselect for settings page only
|
||||
*/
|
||||
function bodhi_svgs_admin_multiselect() {
|
||||
global $svgs_plugin_version;
|
||||
|
||||
// Ensure multiselect is only loaded on the settings page
|
||||
if ( bodhi_svgs_specific_pages_settings() ) {
|
||||
wp_enqueue_style( 'CSS-for-multiselect', BODHI_SVGS_PLUGIN_URL . 'css/jquery.dropdown-min.css', array(), $svgs_plugin_version );
|
||||
wp_enqueue_script( 'js-for-multiselect', BODHI_SVGS_PLUGIN_URL . 'js/min/jquery.dropdown-min.js', array( 'jquery' ), $svgs_plugin_version, true );
|
||||
|
||||
wp_add_inline_script( 'js-for-multiselect', 'jQuery(document).ready(function(){jQuery(".upload_allowed_roles").dropdown({multipleMode: "label",input: \'<input type="text" maxLength="20" placeholder="Search">\',searchNoData: \'<li style="color:#ddd">No Results</li>\'});});', 'after' );
|
||||
wp_add_inline_script( 'js-for-multiselect', 'jQuery(document).ready(function(){jQuery(".sanitize_on_upload_roles").dropdown({multipleMode: "label",input: \'<input type="text" maxLength="20" placeholder="Search">\',searchNoData: \'<li style="color:#ddd">No Results</li>\'});});', 'after' );
|
||||
}
|
||||
}
|
||||
add_action( 'admin_enqueue_scripts', 'bodhi_svgs_admin_multiselect' );
|
||||
|
||||
/**
|
||||
* Enqueue Block editor JS
|
||||
*/
|
||||
function bodhi_svgs_block_editor() {
|
||||
global $svgs_plugin_version;
|
||||
|
||||
if ( bodhi_svgs_advanced_mode() ) {
|
||||
wp_enqueue_script( 'bodhi-svgs-gutenberg-filters', BODHI_SVGS_PLUGIN_URL . '/js/min/gutenberg-filters-min.js', ['wp-edit-post'], $svgs_plugin_version, true );
|
||||
}
|
||||
}
|
||||
add_action( 'enqueue_block_editor_assets', 'bodhi_svgs_block_editor' );
|
||||
|
||||
/**
|
||||
* Enqueue frontend CSS
|
||||
*/
|
||||
function bodhi_svgs_frontend_css() {
|
||||
global $bodhi_svgs_options;
|
||||
global $svgs_plugin_version;
|
||||
|
||||
if ( ! empty( $bodhi_svgs_options['frontend_css'] ) ) {
|
||||
wp_enqueue_style( 'bodhi-svgs-attachment', BODHI_SVGS_PLUGIN_URL . 'css/svgs-attachment.css', array(), $svgs_plugin_version );
|
||||
}
|
||||
}
|
||||
add_action( 'wp_enqueue_scripts', 'bodhi_svgs_frontend_css' );
|
||||
|
||||
/**
|
||||
* Enqueue frontend JS
|
||||
*/
|
||||
function bodhi_svgs_frontend_js() {
|
||||
global $bodhi_svgs_options;
|
||||
global $svgs_plugin_version;
|
||||
|
||||
if ( ! empty( $bodhi_svgs_options['sanitize_svg_front_end'] ) && $bodhi_svgs_options['sanitize_svg_front_end'] === 'on' && bodhi_svgs_advanced_mode() === true ) {
|
||||
$bodhi_svgs_js_footer = ! empty( $bodhi_svgs_options['js_foot_choice'] );
|
||||
wp_enqueue_script( 'bodhi-dompurify-library', BODHI_SVGS_PLUGIN_URL . 'vendor/DOMPurify/DOMPurify.min.js', array(), '2.5.8', $bodhi_svgs_js_footer );
|
||||
}
|
||||
}
|
||||
add_action( 'wp_enqueue_scripts', 'bodhi_svgs_frontend_js', 9 );
|
||||
|
||||
/**
|
||||
* Enqueue and localize JS for IMG tag replacement
|
||||
*/
|
||||
function bodhi_svgs_inline() {
|
||||
global $bodhi_svgs_options;
|
||||
global $svgs_plugin_version;
|
||||
|
||||
if ( bodhi_svgs_advanced_mode() ) {
|
||||
$force_inline_svg_active = ! empty( $bodhi_svgs_options['force_inline_svg'] ) ? 'true' : 'false';
|
||||
|
||||
if ( ! empty( $bodhi_svgs_options['css_target'] ) ) {
|
||||
$css_target_array = array(
|
||||
'Bodhi' => 'img.' . esc_attr( $bodhi_svgs_options['css_target'] ),
|
||||
'ForceInlineSVG' => esc_attr( $bodhi_svgs_options['css_target'] )
|
||||
);
|
||||
} else {
|
||||
$css_target_array = array(
|
||||
'Bodhi' => 'img.style-svg',
|
||||
'ForceInlineSVG' => 'style-svg'
|
||||
);
|
||||
}
|
||||
|
||||
if ( ! empty( $bodhi_svgs_options['use_expanded_js'] ) ) {
|
||||
$bodhi_svgs_js_folder = '';
|
||||
$bodhi_svgs_js_file = '';
|
||||
} else {
|
||||
$bodhi_svgs_js_folder = 'min/';
|
||||
$bodhi_svgs_js_file = '-min';
|
||||
}
|
||||
|
||||
$bodhi_svgs_js_footer = ! empty( $bodhi_svgs_options['js_foot_choice'] );
|
||||
$bodhi_svgs_js_vanilla = ! empty( $bodhi_svgs_options['use_vanilla_js'] ) ? '-vanilla' : '';
|
||||
|
||||
$bodhi_svgs_js_path = 'js/' . $bodhi_svgs_js_folder . 'svgs-inline' . $bodhi_svgs_js_vanilla . $bodhi_svgs_js_file . '.js';
|
||||
|
||||
// Only change: Make jQuery dependency conditional on vanilla JS setting
|
||||
$bodhi_svgs_dependencies = ! empty( $bodhi_svgs_options['use_vanilla_js'] ) ? array() : array( 'jquery' );
|
||||
|
||||
wp_register_script( 'bodhi_svg_inline', BODHI_SVGS_PLUGIN_URL . $bodhi_svgs_js_path, $bodhi_svgs_dependencies, $svgs_plugin_version, $bodhi_svgs_js_footer );
|
||||
wp_enqueue_script( 'bodhi_svg_inline' );
|
||||
|
||||
wp_localize_script('bodhi_svg_inline', 'svgSettings', array(
|
||||
'skipNested' => !empty($bodhi_svgs_options['skip_nested_svg'])
|
||||
));
|
||||
|
||||
wp_add_inline_script(
|
||||
'bodhi_svg_inline',
|
||||
sprintf(
|
||||
'cssTarget=%s;ForceInlineSVGActive=%s;frontSanitizationEnabled=%s;',
|
||||
wp_json_encode( $css_target_array ),
|
||||
wp_json_encode( $force_inline_svg_active ),
|
||||
wp_json_encode( $bodhi_svgs_options['sanitize_svg_front_end'] )
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
add_action( 'wp_enqueue_scripts', 'bodhi_svgs_inline' );
|
||||
160
wp-content/plugins/svg-support/functions/featured-image.php
Normal file
160
wp-content/plugins/svg-support/functions/featured-image.php
Normal file
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
/**
|
||||
* Featured image meta checkbox to inline SVG
|
||||
*
|
||||
* Allow users to select whether featured images should contain the SVG Support class.
|
||||
* Check if the featured image is SVG first, then display meta box for SVG only.
|
||||
*/
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
/**
|
||||
* Add checkbox to the featured image metabox.
|
||||
*/
|
||||
function bodhi_svgs_featured_image_meta( $content ) {
|
||||
|
||||
global $post;
|
||||
|
||||
$thumbnail = get_the_post_thumbnail();
|
||||
// Check if featured image is set and has extension of .svg or .svgz.
|
||||
if ( $thumbnail && strpos( $thumbnail, '.svg' ) !== false ) {
|
||||
|
||||
$text = __( 'Render this SVG inline (advanced)', 'svg-support' );
|
||||
$id = 'inline_featured_image';
|
||||
$value = esc_attr( get_post_meta( $post->ID, $id, true ) );
|
||||
$label = '<label for="' . $id . '" class="selectit"><input name="' . $id . '" type="checkbox" id="' . $id . '" value="1" '. checked( $value, 1, false ) .'> ' . $text .'</label>';
|
||||
$nonce = wp_nonce_field( 'bodhi_svgs_save_featured_image_meta', 'bodhi_svgs_featured_image_nonce', true, false );
|
||||
|
||||
return $content .= $label . $nonce;
|
||||
|
||||
} else {
|
||||
|
||||
return $content;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
if ( bodhi_svgs_advanced_mode() ) {
|
||||
add_filter( 'admin_post_thumbnail_html', 'bodhi_svgs_featured_image_meta' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Save featured image meta data when saved.
|
||||
*/
|
||||
function bodhi_svgs_save_featured_image_meta( $post_id, $post, $update ) {
|
||||
|
||||
// Verify nonce
|
||||
if ( ! isset( $_POST['bodhi_svgs_featured_image_nonce'] ) ||
|
||||
! wp_verify_nonce(
|
||||
sanitize_text_field( wp_unslash( $_POST['bodhi_svgs_featured_image_nonce'] ) ),
|
||||
'bodhi_svgs_save_featured_image_meta'
|
||||
)
|
||||
) {
|
||||
return $post_id;
|
||||
}
|
||||
|
||||
// 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 $post_id;
|
||||
}
|
||||
|
||||
// If the user does not have permission to edit posts, do nothing.
|
||||
if ( ! current_user_can( 'edit_post', $post_id ) ) {
|
||||
return $post_id;
|
||||
}
|
||||
|
||||
// If checkbox is checked, add/update meta
|
||||
if ( isset( $_POST['inline_featured_image'] ) ) {
|
||||
update_post_meta( $post_id, 'inline_featured_image', 1 );
|
||||
} else {
|
||||
// If unchecked, delete the meta entirely
|
||||
delete_post_meta( $post_id, 'inline_featured_image' );
|
||||
}
|
||||
}
|
||||
add_action( 'save_post', 'bodhi_svgs_save_featured_image_meta', 10, 3 );
|
||||
|
||||
/*
|
||||
* Save featured image meta for Gutenberg Editor
|
||||
*/
|
||||
function bodhi_svgs_register_meta() {
|
||||
|
||||
register_meta( 'post', 'inline_featured_image', array(
|
||||
'show_in_rest' => true,
|
||||
'single' => true,
|
||||
'type' => 'boolean',
|
||||
'auth_callback' => '__return_true'
|
||||
) );
|
||||
|
||||
}
|
||||
add_action( 'init', 'bodhi_svgs_register_meta' );
|
||||
|
||||
/**
|
||||
* Add class to the featured image output on front end.
|
||||
*/
|
||||
function bodhi_svgs_add_class_to_thumbnail( $thumb ) {
|
||||
|
||||
$inline_featured_image = get_post_meta( get_the_ID(), 'inline_featured_image' );
|
||||
|
||||
if ( is_array( $inline_featured_image ) && in_array( 1, $inline_featured_image ) ) {
|
||||
|
||||
global $bodhi_svgs_options;
|
||||
|
||||
$target_class = ! empty( $bodhi_svgs_options['css_target'] ) ? $bodhi_svgs_options['css_target'] : 'style-svg';
|
||||
|
||||
if ( is_singular() ) {
|
||||
|
||||
$thumb = str_replace( 'attachment-', $target_class . ' attachment-', $thumb );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $thumb;
|
||||
|
||||
}
|
||||
if ( bodhi_svgs_advanced_mode() ) {
|
||||
add_filter( 'post_thumbnail_html', 'bodhi_svgs_add_class_to_thumbnail' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Safe update of inline featured image meta
|
||||
*/
|
||||
function bodhi_svgs_update_featured_image_meta($post_id, $value) {
|
||||
// Delete any existing meta first
|
||||
delete_post_meta($post_id, 'inline_featured_image');
|
||||
|
||||
// Add the new value
|
||||
add_post_meta($post_id, 'inline_featured_image', $value, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the AJAX request for updating featured image inline status
|
||||
*/
|
||||
function bodhi_svgs_featured_image_inline_toggle() {
|
||||
// Verify nonce and permissions
|
||||
if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'svg-support-featured')) {
|
||||
wp_send_json_error('Invalid nonce');
|
||||
}
|
||||
|
||||
if (!current_user_can('edit_posts')) {
|
||||
wp_send_json_error('Insufficient permissions');
|
||||
}
|
||||
|
||||
// Validate and sanitize input
|
||||
if (!isset($_POST['post_id']) || !isset($_POST['checked'])) {
|
||||
wp_send_json_error('Missing parameters');
|
||||
}
|
||||
|
||||
$post_id = intval($_POST['post_id']);
|
||||
$checked = ($_POST['checked'] === 'true');
|
||||
|
||||
// Update the meta safely
|
||||
bodhi_svgs_update_featured_image_meta($post_id, $checked);
|
||||
|
||||
wp_send_json_success();
|
||||
}
|
||||
|
||||
// Hook the AJAX actions for both logged-in and non-logged-in users
|
||||
add_action('wp_ajax_bodhi_svgs_featured_image_inline_toggle', 'bodhi_svgs_featured_image_inline_toggle');
|
||||
add_action('wp_ajax_nopriv_bodhi_svgs_featured_image_inline_toggle', 'bodhi_svgs_featured_image_inline_toggle');
|
||||
14
wp-content/plugins/svg-support/functions/localization.php
Normal file
14
wp-content/plugins/svg-support/functions/localization.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
/**
|
||||
* INTERNATIONALIZATION / LOCALIZATION
|
||||
*/
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
add_action( 'init', 'bodhi_svgs_localization' );
|
||||
|
||||
function bodhi_svgs_localization() {
|
||||
load_plugin_textdomain( 'svg-support', false, basename( dirname( __FILE__ ) ) . '/languages' );
|
||||
|
||||
}
|
||||
29
wp-content/plugins/svg-support/functions/meta-cleanup.php
Normal file
29
wp-content/plugins/svg-support/functions/meta-cleanup.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
function bodhi_svgs_cleanup_duplicate_meta() {
|
||||
global $wpdb;
|
||||
|
||||
// Delete all but the latest inline_featured_image meta per post
|
||||
$wpdb->query("
|
||||
DELETE pm FROM {$wpdb->postmeta} pm
|
||||
JOIN (
|
||||
SELECT post_id, meta_id FROM (
|
||||
SELECT post_id, meta_id,
|
||||
ROW_NUMBER() OVER (PARTITION BY post_id ORDER BY meta_id DESC) AS row_num
|
||||
FROM {$wpdb->postmeta}
|
||||
WHERE meta_key = 'inline_featured_image'
|
||||
) as duplicates
|
||||
WHERE row_num > 1
|
||||
) to_delete ON pm.meta_id = to_delete.meta_id
|
||||
");
|
||||
|
||||
// Delete all inline_featured_image meta entries that aren't explicitly set to 1
|
||||
$wpdb->query("
|
||||
DELETE FROM {$wpdb->postmeta}
|
||||
WHERE meta_key = 'inline_featured_image'
|
||||
AND (meta_value = '' OR meta_value = '0' OR meta_value IS NULL)
|
||||
");
|
||||
}
|
||||
117
wp-content/plugins/svg-support/functions/mime-types.php
Normal file
117
wp-content/plugins/svg-support/functions/mime-types.php
Normal file
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
/**
|
||||
* Add SVG mime types to WordPress
|
||||
*
|
||||
* Allows you to upload SVG files to the media library like any other image.
|
||||
* Additionally provides a fix for WP 4.7.1 - 4.7.2 upload issues and for Avada theme.
|
||||
*/
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
/**
|
||||
* Add Mime Types
|
||||
*/
|
||||
function bodhi_svgs_upload_mimes( $mimes = array() ) {
|
||||
global $bodhi_svgs_options;
|
||||
|
||||
// Ensure the option is set
|
||||
if ( !isset($bodhi_svgs_options['restrict']) ) {
|
||||
return $mimes;
|
||||
}
|
||||
|
||||
// Get the allowed roles from settings
|
||||
$allowed_roles_array = (array) $bodhi_svgs_options['restrict'];
|
||||
|
||||
// Get the current user and their roles
|
||||
$user = wp_get_current_user();
|
||||
$current_user_roles = (array) $user->roles;
|
||||
|
||||
// For multisite, add network admin to allowed roles
|
||||
if (is_multisite() && is_super_admin()) {
|
||||
$current_user_roles[] = 'administrator';
|
||||
}
|
||||
|
||||
// Check if the user has the capability or the role
|
||||
$is_role_allowed = array_intersect($allowed_roles_array, $current_user_roles);
|
||||
if ( empty($is_role_allowed) || !current_user_can('upload_files') ) {
|
||||
return $mimes;
|
||||
}
|
||||
|
||||
// Allow SVG file upload
|
||||
$mimes['svg'] = 'image/svg+xml';
|
||||
$mimes['svgz'] = 'image/svg+xml';
|
||||
|
||||
return $mimes;
|
||||
}
|
||||
add_filter( 'upload_mimes', 'bodhi_svgs_upload_mimes', 99 );
|
||||
|
||||
/**
|
||||
* Check Mime Types
|
||||
*/
|
||||
function bodhi_svgs_upload_check( $checked, $file, $filename, $mimes ) {
|
||||
|
||||
if ( ! $checked['type'] ) {
|
||||
|
||||
$check_filetype = wp_check_filetype( $filename, $mimes );
|
||||
$ext = $check_filetype['ext'];
|
||||
$type = $check_filetype['type'];
|
||||
$proper_filename = $filename;
|
||||
|
||||
// Check if the file is an SVG or SVGZ
|
||||
if ( ( $ext === 'svg' || $ext === 'svgz' ) && $type === 'image/svg+xml' ) {
|
||||
$checked = compact( 'ext', 'type', 'proper_filename' );
|
||||
}
|
||||
}
|
||||
|
||||
return $checked;
|
||||
}
|
||||
add_filter( 'wp_check_filetype_and_ext', 'bodhi_svgs_upload_check', 10, 4 );
|
||||
|
||||
|
||||
/**
|
||||
* Mime Check fix for WP 4.7.1 / 4.7.2
|
||||
*
|
||||
* Fixes uploads for these 2 versions of WordPress.
|
||||
* Issue was fixed in 4.7.3 core.
|
||||
*/
|
||||
function bodhi_svgs_allow_svg_upload( $data, $file, $filename, $mimes ) {
|
||||
|
||||
global $wp_version;
|
||||
// Corrected the version check condition
|
||||
if ( version_compare($wp_version, '4.7.1', '>=') && version_compare($wp_version, '4.7.3', '<') ) {
|
||||
$filetype = wp_check_filetype( $filename, $mimes );
|
||||
|
||||
// Check if the file is an SVG or SVGZ
|
||||
if ( ( $filetype['ext'] === 'svg' || $filetype['ext'] === 'svgz' ) && $filetype['type'] === 'image/svg+xml' ) {
|
||||
return [
|
||||
'ext' => $filetype['ext'],
|
||||
'type' => $filetype['type'],
|
||||
'proper_filename' => $data['proper_filename']
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
add_filter( 'wp_check_filetype_and_ext', 'bodhi_svgs_allow_svg_upload', 10, 4 );
|
||||
|
||||
function bodhi_svgs_multisite_settings($mimes) {
|
||||
// Check if this is a multisite installation
|
||||
if (is_multisite()) {
|
||||
// Get the site ID
|
||||
$blog_id = get_current_blog_id();
|
||||
|
||||
// Allow SVG on main site
|
||||
if (is_main_site()) {
|
||||
return $mimes;
|
||||
}
|
||||
|
||||
// For subsites, check if upload_files capability is allowed
|
||||
if (get_site_option('upload_space_check_disabled')) {
|
||||
return $mimes;
|
||||
}
|
||||
}
|
||||
return $mimes;
|
||||
}
|
||||
add_filter('upload_mimes', 'bodhi_svgs_multisite_settings', 98);
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
/**
|
||||
* ADD ABILITY TO VIEW THUMBNAILS IN WP 4.0+
|
||||
*/
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
add_action( 'admin_init', 'bodhi_svgs_display_thumbs' );
|
||||
function bodhi_svgs_display_thumbs() {
|
||||
|
||||
if ( bodhi_svgs_specific_pages_media_library() ) {
|
||||
|
||||
function bodhi_svgs_thumbs_filter( $content ) {
|
||||
|
||||
return apply_filters( 'final_output', $content );
|
||||
|
||||
}
|
||||
|
||||
ob_start( 'bodhi_svgs_thumbs_filter' );
|
||||
|
||||
add_filter( 'final_output', 'bodhi_svgs_final_output' );
|
||||
function bodhi_svgs_final_output( $content ) {
|
||||
if ($content === null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$content = str_replace(
|
||||
'<# } else if ( \'image\' === data.type && data.sizes && data.sizes.full ) { #>',
|
||||
'<# } else if ( \'svg+xml\' === data.subtype ) { #>
|
||||
<img class="details-image" src="{{ data.url }}" draggable="false" />
|
||||
<# } else if ( \'image\' === data.type && data.sizes && data.sizes.full ) { #>',
|
||||
$content
|
||||
);
|
||||
|
||||
$content = str_replace(
|
||||
'<# } else if ( \'image\' === data.type && data.sizes ) { #>',
|
||||
'<# } else if ( \'svg+xml\' === data.subtype ) { #>
|
||||
<div class="centered">
|
||||
<img src="{{ data.url }}" class="thumbnail" draggable="false" />
|
||||
</div>
|
||||
<# } else if ( \'image\' === data.type && data.sizes ) { #>',
|
||||
$content
|
||||
);
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user