Skip to content

Instantly share code, notes, and snippets.

@matgargano
Created January 9, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matgargano/36e76cf4bb2b55e88981 to your computer and use it in GitHub Desktop.
Save matgargano/36e76cf4bb2b55e88981 to your computer and use it in GitHub Desktop.
Issue updating backbone pane in upload script
<?php
add_action( 'save_post', 'myplugin_save_meta_box_data' );
add_action( 'admin_enqueue_scripts', function () {
wp_enqueue_media();
} );
add_action( 'add_meta_boxes', 'myplugin_add_meta_box' );
add_action( 'admin_footer', function () {
?>
<style>.pdf-media-field img {
max-width: 100%;
height: auto;
padding: 3px;
}
.pdf-media-field a {
display: inline-block;
}</style>
<script>
(function ($, window, document, undefined) {
var defaults = {
addSelector : '.pdf-add',
removeSelector: '.pdf-remove',
inputSelector : '.pdf-id',
thumbSize : 'medium',
modalOptions : {}
};
function metaPdf(element, options) {
this.$element = $(element);
this.settings = $.extend({}, defaults, options);
this._defaults = defaults;
this.init();
}
metaPdf.prototype = {
init: function () {
this.$addLink = this.$element.find(this.settings.addSelector);
this.$removeLink = this.$element.find(this.settings.removeSelector);
this.$inputField = this.$element.find(this.settings.inputSelector);
this.listen();
},
listen: function () {
var _this = this;
this.$addLink.on('click', function (e) {
e.preventDefault();
_this.openModal();
});
this.$removeLink.on('click', function (e) {
e.preventDefault();
_this.removeImage();
$(this).hide();
});
},
openModal: function () {
var defaultOptions = {
title: 'Select Media'
};
var options = $.extend(true, {}, defaultOptions, this.settings.modalOptions);
this.modal = wp.media.frames.file_frame = wp.media(options);
this.modalListen();
this.modal.open();
},
modalListen: function () {
var _this = this;
this.modal.on('toolbar:create:select', function () {
_this.modal.state().set('filterable', 'uploaded');
});
this.modal.on('select', function () {
_this.getModalAttachment();
});
this.modal.on('open activate', function () {
_this.attachmentToModal();
});
},
getModalAttachment: function () {
var attachments = [];
var selections = this.modal.state().get('selection').models;
$.each(selections, function () {
attachments.push(this.toJSON());
});
this.attachImage(attachments);
},
attachmentToModal: function () {
var attachments = this.$inputField.val().split(',');
if (attachments) {
var Attachment = wp.media.model.Attachment;
var selection = this.modal.state().get('selection');
if (typeof attachments == 'object') {
$.each(attachments, function () {
selection.add(Attachment.get(this));
});
}
else {
selection.add(Attachment.get(attachments));
}
}
},
attachImage: function (attachments) {
var ids = [];
var urls = [];
var _this = this;
$.each(attachments, function (i, attachment) {
var url = _this.getThumbUrl(attachment);
ids.push(attachment.id);
urls.push(url);
});
this.$inputField.val(ids);
this.setThumbHTML(urls);
this.hasImage = true;
this.$removeLink.show();
},
getThumbUrl: function (attachment) {
var img_url = "";
if (typeof attachment.sizes != "object") {
img_url = attachment.icon;
}
else if (typeof attachment.sizes[this.settings.thumbSize] != "undefined") {
img_url = attachment.sizes[this.settings.thumbSize].url;
}
else if (typeof attachment.sizes.medium != "undefined") {
img_url = attachment.sizes.medium.url;
}
else {
img_url = attachment.sizes.full.url;
}
return img_url;
},
removeImage: function () {
this.$inputField.val('');
this.$addLink.html(this.$addLink.attr('title')).addClass('button');
this.hasImage = false;
},
setThumbHTML: function (urls) {
_this = this;
this.$addLink.html('').removeClass('button');
$.each(urls, function (i, url) {
var $img = $('<img>');
$img.attr('src', url);
_this.$addLink.append($img);
});
}
};
$(document).ready(function () {
$('.pdf-media-field').each(function (i, e) {
new metaPdf(this, $(this).data('pdf-field-settings'));
});
});
})(jQuery, window, document);
</script>
<?php
} );
function myplugin_add_meta_box() {
$screens = array( 'post', 'page' );
foreach ( $screens as $screen ) {
add_meta_box(
'myplugin_sectionid',
__( 'My Post Section Title', 'myplugin_textdomain' ),
'myplugin_meta_box_callback',
$screen
);
}
}
/**
* Prints the box content.
*
* @param WP_Post $post The object for the current post/page.
*/
function myplugin_meta_box_callback( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field( 'myplugin_meta_box', 'myplugin_meta_box_nonce' );
$mime_types = array( 'application/pdf' );
$thumb_size = 'medium';
$label_add = 'Set pdf';
$label_remove = 'Remove pdf';
$link_content = 'Link';
$value_string = 'Link';
$field_name = $field_id = 'thepdf';
$hide_remove = true;
$value = get_post_meta( $post->ID, 'thepdf_meta_key' );
if ( $value ) {
if ( ! is_array( $value ) ) {
$value = array( $value );
}
$value_string = implode( ',', $value );
foreach ( $value as $attachment ) {
$value_post = get_post( $attachment );
if ( $value_post ) {
$mime_type = $value_post->post_mime_type;
$icon = ( strpos( $mime_type, 'image' ) ) ? false : true;
$thumbnail_html = wp_get_attachment_image( $attachment, $thumb_size, $icon );
if ( ! empty( $thumbnail_html ) ) {
$link_content .= $thumbnail_html;
$hide_remove = false;
}
$link_content .= '<br>' . get_the_title( $value_post->ID );
}
}
}
// If no thumbnails then use link text and hide remove
if ( empty( $link_content ) ) {
$link_content = esc_html( $label_add );
$hide_remove = true;
}
// Settings for the the js object
$field_settings = array(
'thumbSize' => $thumb_size,
'modalOptions' => array(
'multiple' => false,
'title' => $label_add,
'button' => array(
'text' => $label_add
),
'library' => array(
'type' => $mime_types
)
)
);
?>
<div class="pdf-media-field hide-if-no-js" data-pdf-field-settings="<?php echo esc_attr( json_encode( $field_settings ) ); ?>">
<p>select a file</p>
<p>
<input class="hidden pdf-id" type="hidden" id="<?php echo $field_id; ?>" name="<?php echo $field_name; ?>" value="<?php echo esc_attr( $value_string ); ?>" />
<a title="<?php echo esc_attr( $label_add ); ?>" href="#" class="pdf-add <?php echo ( $hide_remove ) ? 'button' : ''; ?>">
<?php echo $link_content; ?>
</a>
</p>
<p>
<a href="#" class="pdf-remove button" <?php echo ( $hide_remove ) ? 'style="display:none;"' : ''; ?>>
<?php echo esc_html( $label_remove ); ?>
</a>
</p>
</div>
<?php
}
/**
* When the post is saved, saves our custom data.
*
* @param int $post_id The ID of the post being saved.
*/
function myplugin_save_meta_box_data( $post_id ) {
/*
* We need to verify this came from our screen and with proper authorization,
* because the save_post action can be triggered at other times.
*/
// Check if our nonce is set.
if ( ! isset( $_POST['myplugin_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['myplugin_meta_box_nonce'], 'myplugin_meta_box' ) ) {
return;
}
// 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;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
/* OK, it's safe for us to save the data now. */
// Make sure that it is set.
if ( ! isset( $_POST['thepdf'] ) ) {
return;
}
// Sanitize user input.
$my_data = sanitize_text_field( $_POST['thepdf'] );
// Update the meta field in the database.
update_post_meta( $post_id, 'thepdf_meta_key', $my_data );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment