Skip to content

Instantly share code, notes, and snippets.

@thomasgriffin
Created January 10, 2013 15:08
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 thomasgriffin/f3d93e78ed118b283938 to your computer and use it in GitHub Desktop.
Save thomasgriffin/f3d93e78ed118b283938 to your computer and use it in GitHub Desktop.
Proof of concept for patch.
<?php
/**
* Media class for Soliloquy.
*
* @since 1.0.0
*
* @package Soliloquy
* @author Thomas Griffin
*/
class Tgmsp_Media {
/**
* Holds a copy of the object for easy reference.
*
* @since 1.0.0
*
* @var object
*/
private static $instance;
/**
* Constructor. Hooks all interactions to initialize the class.
*
* @since 1.0.0
*/
public function __construct() {
self::$instance = $this;
add_filter( 'media_view_strings', array( $this, 'media_strings' ) );
add_action( 'print_media_templates', array( $this, 'print_media_templates' ) );
add_filter( 'wp_prepare_attachment_for_js', array( $this, 'prepare_slides' ), 10, 3 );
add_action( 'wp_ajax_save_attachment', array( $this, 'save_attachment_fields' ), 10, 2 );
}
/**
* Modify WordPress media workflow strings to fit Soliloquy.
*
* @since 1.5.0
*
* @param array $strings Default strings
* @return array $strings Amended media strings
*/
public function media_strings( $strings ) {
if ( ! Tgmsp::is_soliloquy_add_edit_screen() )
return $strings;
// Modify existing strings.
$strings['insertMediaTitle'] = Tgmsp_Strings::get_instance()->strings['media_title'];
$strings['insertIntoPost'] = Tgmsp_Strings::get_instance()->strings['media_insert'];
$strings['uploadedToThisPost'] = Tgmsp_Strings::get_instance()->strings['media_uploaded'];
$strings['uploadFilesTitle'] = Tgmsp_Strings::get_instance()->strings['media_upload'];
// Remove unnecessary existing strings.
unset( $strings['createGalleryTitle'] );
unset( $strings['insertFromUrlTitle'] );
// Return the modified strings.
return $strings;
}
/**
* Output the custom media workflow templates for our custom slide types.
*
* @since 1.5.0
*/
public function print_media_templates() {
if ( ! Tgmsp::is_soliloquy_add_edit_screen() )
return;
// Custom template for attachment fields. ?>
<script type="text/html" id="tmpl-soliloquy-attachment-fields">
<label class="setting" data-setting="soliloquy_link">
<span><?php echo Tgmsp_Strings::get_instance()->strings['image_link']; ?></span>
<input type="text" value="{{ data.soliloquy_link }}" />
</label>
</script>
<?php
}
/**
* Filter the attachment response to include custom attachment metadata fields
* for Soliloquy slides.
*
* @since 1.5.0
*
* @param array $response Array of response data about the attachment
* @param object $$attachment The attachment object
* @param array $meta Array of attachment metadata
* @return array $response Amended $response array with our custom data
*/
public function prepare_slides( $response, $attachment, $meta ) {
// Return early if not on a Soliloquy post type.
$parent = get_post( $attachment->post_parent );
if ( $parent )
if ( 'soliloquy' !== $parent->post_type )
return $response;
// Add our custom metadata fields to the response array.
$response['soliloquy_link'] = get_post_meta( $attachment->ID, '_soliloquy_image_link', true );
// Return our amended response array.
return $response;
}
/**
* Saves custom attachment metadata when WordPress runs the update via Backbone.
*
* @since 1.5.0
*
* @param array $attachment Array of attachment data
* @param array $changes Changes that have been sent from the server
*/
public function save_attachment_fields( $attachment, $changes ) {
if ( ! empty( $changes['soliloquy_link'] ) )
update_post_meta( $attachment['ID'], '_soliloquy_image_link', esc_url( $changes['soliloquy_link'] ) );
}
/**
* Getter method for retrieving the object instance.
*
* @since 1.0.0
*/
public static function get_instance() {
return self::$instance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment