';
if ( $image_arr ) {
$image = ' ';
}
$html = '' . $image . '
';
$html .= '';
$html .= ' ';
$html .= ' ';
$html .= '
';
echo $html;
}
// add instructions
public static function meta_box_instructions( $post ) {
// describe requirements
$message = sprintf( __( '
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 .='
' . __( 'Basic shortcode usage', 'aqua-svg-sprite' ) . ':
[aqua-svg slug="' . $slug . '"' . ( 'general' !== $sprite_slug ? ' sprite="' . $sprite_slug . '"' : '' ) . ']
' . __( 'More complex shortcode example', 'aqua-svg-sprite' ) . ':
[aqua-svg slug="' . $slug . '" sprite="' . $sprite_slug . '" attr="viewbox=0 0 1000 1000,fill=aquamarine"]
' . __( 'PHP usage', 'aqua-svg-sprite' ) . ':
<?php the_aqua_svg( \'' . $slug . '\'' . ( 'general' !== $sprite_slug ? ', \'' . $sprite_slug . '\'' : '' ) . ' ); ?>
' . __( 'More complex PHP example', 'aqua-svg-sprite' ) . ':
<?php
/* Get Sprite String and Echo */
$slug = \''. $slug .'\';
$sprite = \'' . $sprite_slug . '\';
$attr = array(
\'viewbox\' => \'0 0 1000 1000\',
\'fill\' => \'aquamarine\',
);
echo get_aqua_svg( $slug, $sprite, $attr );
?>
';
} else {
$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 ) ) {
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 '';
echo ' name, $name, false ) . '>';
echo '' . esc_html_e( $term->name ) . ' ';
echo ' ';
}
}
/**
* Save the sprite group meta box results.
*
* @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;
}
// get the value input
$group = sanitize_text_field( $_POST['aqua_sprite_group'] );
// if there is a value then update the term
if ( ! empty( $group ) ) {
$term = get_term_by( 'name', $group, 'aqua_svg_sprite_group' );
if ( ! empty( $term ) && ! is_wp_error( $term ) ) {
wp_set_object_terms( $post_id, $term->term_id, 'aqua_svg_sprite_group', false );
}
}
}
/**
* 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 ) );
}
}
}
}
/**
* Rebuild svg sprite on save of svg post type posts.
*/
public static function create_svg_sprite( $post_id ) {
if ( 'aqua_svg_sprite' === get_post_type( $post_id ) ) {
// get the directory where the svg sprite goes (within uploads)
$wp_upload_dir = wp_upload_dir();
$aqua_svg_sprite_dir = $wp_upload_dir['basedir'] . '/aqua-svg-sprite';
// create the directory if it doesn't already exist
if ( ! file_exists( $aqua_svg_sprite_dir ) ) {
mkdir( $aqua_svg_sprite_dir, 0755, true );
}
// get the post term (there will only be one)
$term = 'general';
$terms_arr = wp_get_post_terms( $post_id, 'aqua_svg_sprite_group' );
if ( $terms_arr ) {
$term_obj = $terms_arr[ 0 ];
$term = $term_obj->slug;
}
// start the svg internals (symbols)
$svg_symbols = '';
// 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' => $term,
),
),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
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(),
),
);
// get the id via meta or through the post data if is current post
$svg_id = ( get_the_id() !== $post_id ? get_post_thumbnail_id( get_the_id() ) : (int) $_POST['aqua-svg'] );
// establish the slug (used as id for sprite)
$slug = strtolower( trim( preg_replace( '/[\s-]+/', '-', preg_replace( '/[^A-Za-z0-9-]+/', '-', preg_replace( '/[&]/', 'and', preg_replace( '/[\']/', '', iconv('UTF-8', 'ASCII//TRANSLIT', get_the_title() ) ) ) ) ), '-' ) );
// create svg code and strip out unneeded elements
$svg = file_get_contents( get_attached_file( wp_kses( $svg_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( '//i', '', $svg );
// // get rid of xml namespaces (should be on instead of )
$svg = preg_replace( '/\s*xmlns.*?".*?"/i', '', $svg );
// add this svg to the sprite
$svg_symbols .= $svg;
}
}
// wrap svg internals
$svg_sprite = '';
$svg_sprite .= $svg_symbols;
$svg_sprite .= ' ';
// create the svg file (rebuilds each time)
file_put_contents( $aqua_svg_sprite_dir . '/aqua-svg-' . $term . '-sprite.svg', $svg_sprite );
// update the featured image to the uploaded image
update_post_meta( $post_id, '_thumbnail_id', (int) $_POST['aqua-svg'] );
}
}
}