Pro Tip: It\'s a great idea to run the file through an SVG compressor like
%1$s
before uploading it here so you can apply manual compression.
', 'aqua-svg-sprite' ), 'SVG OMG' );
// get valid post id
$post_id = ( int ) $_GET['post'];
// provide API helpers
if ( get_post_field( 'post_name', $post_id ) ) {
// get this post's slug
$slug = get_post_field( 'post_name', $post_id );
// get the sprite this is part of (can only be one)
$term_arr = wp_get_post_terms( $post_id, 'aqua_svg_sprite_group' );
$first_term_obj = $term_arr[0];
$term_id = $first_term_obj->term_taxonomy_id;
$term_obj = get_term_by( 'id', $term_id, 'aqua_svg_sprite_group' );
$sprite_slug = $term_obj->slug;
// set up the message text
$message .='
(' . __( 'helpful API docs will appear here once you save the post', 'aqua-svg-sprite' ) . ')
';
}
// output it
echo $message;
}
// save meta box results
public static function save_aqua_svg_sprite_meta_box( $post_id ) {
// check to make sure this should be happening
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'aqua_svg_sprite_nonce' ] ) && wp_verify_nonce( $_POST[ 'aqua_svg_sprite_nonce' ], 'aqua_svg_sprite_submit' ) ) ? 'true' : 'false';
// exit if not
if ( $is_autosave || $is_revision || ! $is_valid_nonce ) {
return;
}
// save the new attachment id as the thumb for this post
$att_id = ( int ) $_POST['aqua-svg'];
if( isset( $att_id ) && 0 < $att_id ) {
update_post_meta( $post_id, 'aqua-svg', $att_id );
}
}
/**
* Add meta box for sprite group.
*
* @link http://sudarmuthu.com/blog/creating-single-select-wordpress-taxonomies/
* @param obj post object
*/
function group_meta_box( $post ) {
$terms = get_terms( 'aqua_svg_sprite_group', array( 'hide_empty' => false ) );
$post = get_post();
$group = wp_get_object_terms( $post->ID, 'aqua_svg_sprite_group', array( 'orderby' => 'term_id', 'order' => 'ASC' ) );
$name = '';
if ( ! is_wp_error( $group ) ) {
if ( isset( $group[0] ) && isset( $group[0]->name ) ) {
$name = $group[0]->name;
}
}
foreach ( $terms as $term ) {
echo ' ';
}
}
/**
* Save the sprite group meta box values.
*
* @link http://sudarmuthu.com/blog/creating-single-select-wordpress-taxonomies/
* @param int $post_id The ID of the post that's being saved.
*/
function save_group_meta_box( $post_id ) {
// handle autosaves
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// don't do stuff if this doesn't even have the meta box
if ( ! isset( $_POST['aqua_sprite_group'] ) ) {
return;
}
// validate nonce
$is_valid_nonce = ( isset( $_POST[ 'aqua_svg_sprite_nonce' ] ) && wp_verify_nonce( $_POST[ 'aqua_svg_sprite_nonce' ], 'aqua_svg_sprite_submit' ) ) ? 'true' : 'false';
if ( ! $is_valid_nonce ) {
return;
}
// get the value input
$group = sanitize_text_field( $_POST['aqua_sprite_group'] );
// if there is a value
if ( ! empty( $group ) ) {
$term = get_term_by( 'name', $group, 'aqua_svg_sprite_group' );
if ( ! empty( $term ) && ! is_wp_error( $term ) ) {
// get the new term slug
$new_term = $term->slug;
// get the current term id (general by default)
$old_term_arr = get_the_terms( $post_id, 'aqua_svg_sprite_group' );
$old_term_obj = $old_term_arr[0];
$old_term = $old_term_obj->slug;
// update the term
wp_set_object_terms( $post_id, $term->term_id, 'aqua_svg_sprite_group', false );
// if the term has now changed
if ( $new_term !== $old_term ) {
// create code for the old sprite to remove this svg from it
self::create_svg_sprite( 0, $old_term );
}
}
}
}
/**
* Add an automatic default custom taxonomy for custom post type.
* If no story (taxonomy) is set, the comic post will be sorted as “draft” and won’t return an offset error.
* @link https://gist.github.com/mayeenulislam/f208b4fd408fd4742c06
*/
function set_default_object_terms( $post_id, $post ) {
// only for the aqua sprites
if ( 'publish' === $post->post_status && 'aqua_svg_sprite' === $post->post_type ) {
// set default to "general" nothing is selected
$defaults = array( 'aqua_svg_sprite_group' => array( 'general' ) );
$taxonomies = get_object_taxonomies( $post->post_type );
foreach ( (array) $taxonomies as $taxonomy ) {
$terms = wp_get_post_terms( $post_id, $taxonomy );
if ( empty( $terms ) && array_key_exists( $taxonomy, $defaults ) ) {
wp_set_object_terms( $post_id, $defaults[$taxonomy], $taxonomy );
}
}
}
}
/**
* Validate before saving posts.
*/
public static function validate_values( $post_id ) {
if ( 'aqua_svg_sprite' === get_post_type( $post_id ) ) {
// if there's post value (empty if moving to trash)
if ( ! empty( $_POST ) ) {
// get the attachment id
$att_id = ( int ) $_POST['aqua-svg'];
// if there is no SVG attached
if ( ! $att_id ) {
wp_die( 'You must add an SVG before saving.'.$att_id, 'Error - Missing SVG', array( 'back_link' => true ) );
}
$attachment_src_arr = wp_get_attachment_image_src( $att_id );
$ext = pathinfo( $attachment_src_arr[0], PATHINFO_EXTENSION );
if ( 'svg' !== $ext ) {
wp_die( 'You must choose an SVG file (file extension of chosen file was ".' . $ext . '").', 'Error - Not SVG', array( 'back_link' => true ) );
}
}
}
}
/**
* Set up request for SVG sprite creation on hooks (given only post ID).
*/
public static function request_svg_sprite_creation( $post_id ) {
// called on saves: make sure this is of the right custom post type before executing
if ( 'aqua_svg_sprite' === get_post_type( $post_id ) ) {
// get the post term (there will only be one) or default to general
$group = 'general';
$terms_arr = wp_get_post_terms( $post_id, 'aqua_svg_sprite_group' );
if ( $terms_arr ) {
$term_obj = $terms_arr[ 0 ];
$group = $term_obj->slug;
}
// create code for this sprite
self::create_svg_sprite( $post_id, $group );
}
}
/**
* Create svg code.
*/
public static function create_svg_sprite( $post_id, $group ) {
// store the svg sprite code
$svg_sprite = '';
// loop through all svgs in this group
$args = array(
'post_type' => 'aqua_svg_sprite',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'aqua_svg_sprite_group',
'field' => 'slug',
'terms' => $group,
),
),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
// store the svg internals (symbols)
$svg_symbols = '';
while( $query->have_posts() ) { $query->the_post();
// allow just svg-related info (used for wp_kses)
$allowed = array(
'svg' => array(
'width' => array(),
'height' => array(),
'viewbox' => array(),
'version' => array(),
'xmlns' => array(),
'xmlns:xlink' => array(),
),
'g' => array(
'stroke' => array(),
'stroke-width' => array(),
'fill' => array(),
'fill-rule' => array(),
),
'path' => array(
'd' => array(),
'id' => array(),
),
);
// store the svg's id
$attachment_id = 0;
// if loop has reached this current post then use the value being posted
$attachment_id = ( get_the_id() === $post_id ? ( int ) $_POST['aqua-svg'] : 0 );
// if on a different post or if the value is still 0 (e.g. untrash_post hook) then query db
$attachment_id = ( 0 === $attachment_id ? get_post_meta( get_the_id(), 'aqua-svg', true ) : $attachment_id );
// get the slug (used as id for sprite)
$slug = basename( get_permalink() );
// create svg code and strip out unneeded elements
$svg = file_get_contents( get_attached_file( wp_kses( $attachment_id, $allowed ) ) );
// get rid of classes and ids
$svg = preg_replace( '#\s(id|class)="[^"]+"#', '', $svg );
// get rid of /i', '', $svg );
// get rid of comments
$svg = preg_replace( '/\s*<\!--.*?-->/i', '', $svg );
// change svg to symbol
$svg = preg_replace( '/