public

Update Infographic Embedder plugin to support pages (entire file)

  • Download Gist
infographic-embedder.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
<?php
/*
Plugin Name: Infographic Embedder
Plugin URI: http://pardot.com
Description: Takes the infographic URL and adds content to the end of posts.
Version: 1.0
Author: Cliff Seal (Pardot)
Author URI: http://pardot.com
Author Email: cliff.seal@pardot.com
License:
 
Copyright 2012 Pardot (cliff.seal@pardot.com)
 
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
 
/* Add Meta Boxes */
 
add_action( 'load-post.php', 'infographic_embedder_post_meta_boxes_setup' );
add_action( 'load-post-new.php', 'infographic_embedder_post_meta_boxes_setup' );
 
function infographic_embedder_post_meta_boxes_setup() {
add_action( 'add_meta_boxes', 'infographic_embedder_add_post_meta_boxes' );
add_action( 'save_post', 'infographic_embedder_save_post_class_meta', 10, 2 );
}
 
function infographic_embedder_add_post_meta_boxes() {
add_meta_box(
'infographic_embedder-post-class',
__( 'Infographic Embed', 'example' ),
'infographic_embedder_post_class_meta_box',
'post',
'side',
'default'
);
add_meta_box(
'infographic_embedder-post-class',
__( 'Infographic Embed', 'example' ),
'infographic_embedder_post_class_meta_box',
'page',
'side',
'default'
);
}
 
function infographic_embedder_post_class_meta_box( $object, $box ) { ?>
<?php wp_nonce_field( basename( __FILE__ ), 'infographic_embedder_post_class_nonce' ); ?>
<p>
<label for="infographic_embedder-post-class"><?php _e( "Infographic URL (.jpg or .png)", 'example' ); ?></label>
<br />
<input class="widefat" type="text" name="infographic_embedder-post-class" id="infographic_embedder-post-class" value="<?php echo get_post_meta( $object->ID, 'infographic_embedder_post_class', true ); ?>" size="30" />
</p>
<?php }
 
/* Let's Do This: Save the Info */
 
add_action( 'save_post', 'infographic_embedder_save_post_class_meta', 10, 2 );
 
function infographic_embedder_save_post_class_meta( $post_id, $post ) {
if ( !isset( $_POST['infographic_embedder_post_class_nonce'] ) || !wp_verify_nonce( $_POST['infographic_embedder_post_class_nonce'], basename( __FILE__ ) ) ) {
return $post_id;
}
$post_type = get_post_type_object( $post->post_type );
if ( !current_user_can( $post_type->cap->edit_post, $post_id ) ) {
return $post_id;
}
$new_meta_value = ( isset( $_POST['infographic_embedder-post-class'] ) ? $_POST['infographic_embedder-post-class'] : '' );
$meta_key = 'infographic_embedder_post_class';
$meta_value = get_post_meta( $post_id, $meta_key, true );
if ( $new_meta_value && '' == $meta_value ) {
add_post_meta( $post_id, $meta_key, $new_meta_value, true );
} elseif ( $new_meta_value && $new_meta_value != $meta_value ) {
update_post_meta( $post_id, $meta_key, $new_meta_value );
} elseif ( '' == $new_meta_value && $meta_value ) {
delete_post_meta( $post_id, $meta_key, $meta_value );
}
}
 
/* Hook Helpful Stuff to the End of the_content() */
 
add_filter('the_content', 'add_infographic_embed');
 
function add_infographic_embed($content) {
global $post;
$infourl = get_post_meta( $post->ID, 'infographic_embedder_post_class', true );
if ( $infourl ) {
$embedval = htmlentities('<img src="' . $infourl . '" alt="' . get_the_title() . ' - An Infographic from ' . get_bloginfo('name') . '" width="100%" class="infographic_embedder" />
<div class="infographic_attr"><p>Embedded from <a href="' . get_permalink() . '" target="_blank">' . get_bloginfo('name') . '</a></p></div>');
$content .= '<p><a href="' . $infourl . '" target="_blank">Download</a> this infographic.</p>';
$content .= '<div id="infographic_embed"><h3>Embed Our Infographic On Your Site!</h3><label for="embed_width">Image Width (must be in px or %)</label><input id="embed_width" value="100%" type="text" size="50" /><input type="button" value="Update" id="embedupdate" /><textarea id="embedcode">' . $embedval . '</textarea><div id="embed_width_hidden_div" style="display: none"><textarea id="embed_width_hidden">' . $embedval . '</textarea></div></div>';
}
remove_filter('the_content', 'add_infographic_embed');
return $content;
}
 
/* Note: We're not enqueuing, because we want these to load only when the Custom Field is there, and conditional enqueuing is HARD: http://scribu.net/wordpress/optimal-script-loading.html */
 
/* Print the JavaScript Stuffs */
 
function infographic_embed_width_script() {
global $post;
$infourl = get_post_meta( $post->ID, 'infographic_embedder_post_class', true );
if ( $infourl ) {
?>
<script type="text/javascript">
var prev = jQuery("#embed_width").val();
jQuery("#embedupdate").bind("click", function() {
prev = jQuery('#embed_width').val();
var _code = jQuery("#embedcode").val();
var _test = new RegExp(/width=['"][0-9]{1,3}(%|px)['"]/);
var valid = /[0-9]{1,3}(%|px)/g;
if ( !valid.test(prev) ) {
alert('Image width value must include a % sign or "px". For instance, 80% or 500px.');
return false;
} else {
jQuery("#embedcode").val(_code.replace(_test, "width=\"" + prev + "\""));
jQuery("#embed_width_hidden").text(_code.replace(_test, "width=\"" + prev + "\""));
jQuery("#embedcode").val(jQuery('#embed_width_hidden').text());
return true;
}
});
jQuery("#embedcode").focus(function() {
var $this = jQuery(this);
$this.select();
$this.mouseup(function() {
$this.unbind("mouseup");
return false;
});
});
</script>
<?php }
}
 
add_filter('wp_footer', 'infographic_embed_width_script');
 
/* Print the Styles */
 
function infographic_embed_style() {
global $post;
$infourl = get_post_meta( $post->ID, 'infographic_embedder_post_class', true );
if ( $infourl ) {
?>
<link rel="stylesheet" type="text/css" media="all" href="<?php echo plugins_url( 'infographicembedder.css' , __FILE__ ); ?>" />
<?php }
}
 
add_filter('wp_head', 'infographic_embed_style');

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.