Skip to content

Instantly share code, notes, and snippets.

@staylor
Created April 6, 2012 22:01
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 staylor/2323382 to your computer and use it in GitHub Desktop.
Save staylor/2323382 to your computer and use it in GitHub Desktop.
This is the kind of annoying as fuck structured data I have to support for legacy Registration and Pitch configs
<?php
class LegacyOfferConfig extends eMusicPostTypes implements PostType {
var $post_type;
var $boxes;
var $lists;
var $qas;
function init() {
$this->post_type = 'legacy_offer_config';
add_action( 'init', array( $this, 'type' ) );
add_action( 'save_post', array( $this, 'save' ), 10, 2 );
add_action( 'add_meta_boxes_legacy_offer_config', array( $this, 'post_type_actions' ) );
add_action( 'wp_ajax_catalog-list-select', array( $this, 'catalog_list_select' ) );
add_action( 'wp_ajax_reg-box-select', array( $this, 'reg_box_select' ) );
add_filter( 'image_send_to_editor', array( $this, 'send_to_editor' ), 10, 2 );
}
function post_type_actions() {
add_action( 'admin_head', array( $this, 'inline_admin_css_js' ) );
add_filter( 'generic_meta_title', function ( $default ) {
return __( 'Configs' );
} );
}
function inline_admin_css_js() {
?>
<style type="text/css">
#poststuff .emusic-admin {padding: 0 10px}
#poststuff .emusic-admin .emusic-admin {padding: 0 20px}
#poststuff .emusic-admin h4 {margin-bottom: 5px; text-transform: uppercase; font-size: 13px}
#poststuff .emusic-admin .emusic-admin h4 {text-transform: none}
#poststuff .emusic-admin p {margin: 5px 0}
#poststuff .emusic-admin input[type="text"] {margin: 5px 0}
#poststuff .emusic-admin p img {margin: 10px 0; display: block}
</style>
<script type="text/javascript">
jQuery(document).ready(function () {
var $ = jQuery;
$('.add-media-image').click(function () {
window.send_to_editor = function (html) {
html = JSON.parse(html);
var item = $('#' + html.send_to);
item.val(html.image_id);
item.parent()
.find('img')
.attr({
src : html.url,
width : html.w,
height : html.h
});
tb_remove();
};
});
$('.add-reg-box').click(function (e) {
e.preventDefault();
var elem = $(this), selects, boxes;
boxes = elem.parent();
selects = boxes.find('select');
$.ajax({
url : ajaxurl,
data : {
action : 'reg-box-select',
page : elem.attr('data-page'),
index : selects.length
},
success: function (data) {
boxes.find('p').append($(data));
}
});
});
$('.add-catalog-list').click(function (e) {
e.preventDefault();
var elem = $(this), selects, lists;
lists = elem.parent();
selects = lists.find('select');
$.ajax({
url : ajaxurl,
data : {
action : 'catalog-list-select',
page : elem.attr('data-page'),
index : selects.length
},
success: function (data) {
lists.find('p').append($(data));
}
});
});
});
</script>
<?php
}
function send_to_editor( $html, $id ) {
$ref = wp_get_referer();
$params = parse_url( $ref, PHP_URL_QUERY );
parse_str( $params, $bits );
if ( isset( $bits['send_to'] ) ) {
$atts = wp_get_attachment_image_src( $id, array( 200, 9999 ) );
$src = 'http://wp-images.emusic.com' . parse_url( $atts[0], PHP_URL_PATH );
return json_encode( array( 'image_id' => $id, 'url' => $src, 'send_to' => $bits['send_to'], 'w' => $atts[1], 'h' => $atts[2] ) );
}
return $html;
}
function type() {
$this->type_clean( array(
'type' => $this->post_type,
'publicly_queryable' => false,
'exclude_from_search' => true,
'show_ui' => true,
'show_in_menu' => 'emusic-legacy',
'labels' => $this->inflection( 'Legacy Offer Config' ),
'supports' => array( 'title', 'editor', 'thumbnail' ),
'taxonomies' => array( 'region' ),
'callback' => array( $this, 'config_ui' )
) );
}
/**
* AJAX actions
*
*/
function catalog_list_select() {
extract( $_REQUEST );
?>
<select name="pitch_config[page][<?php echo $page ?>][catalogList][<?php echo $index ?>]">
<?php $this->_catalog_list_cache() ?>
</select><br/>
<?php
exit();
}
function reg_box_select() {
extract( $_REQUEST );
?>
<select name="reg_config[page][<?php echo $page ?>][boxes][<?php echo $index ?>]">
<?php $this->_box_cache() ?>
</select><br/>
<?php
exit();
}
/**
* Exists, then echo and escape
*/
function _eee( $data, $prop ) {
if ( isset( $data[$prop] ) ) {
echo esc_attr( $data[$prop] );
}
}
function _media_trigger( $params = array() ) {
$defaults = array(
'tab' => 'library',
'send_to' => '',
'post_id' => isset( $_GET['post'] ) ? $_GET['post'] : '-112376575887',
'emusic-hijack' => 1,
'type' => 'image',
'TB_iframe' => 'true'
);
$params = wp_parse_args( $params, $defaults );
$params = http_build_query( $params );
?>
<a class="add-media-image thickbox" href="media-upload.php?<?php echo $params ?>">
Select Image From Library
</a><br/>
<?php
}
function _qa_cache( $ids = array() ) {
if ( !$this->qas ) {
global $wpdb;
$this->qas = $wpdb->get_results( "SELECT post_title, ID FROM {$wpdb->posts} WHERE post_type = 'legacy_reg_qa'" );
}
if ( !empty( $this->qas ) ) {
printf( '<option value=""> --- SELECT A Q&amp;A --- </option>' );
foreach ( $this->qas as $qa ) {
printf(
'<option value="%d" %s>%s</option>',
$qa->ID,
selected( in_array( $qa->ID, $ids ) ),
$qa->post_title
);
}
}
}
function _box_cache( $id = 0 ) {
if ( !$this->boxes ) {
global $wpdb;
$this->boxes = $wpdb->get_results( "SELECT post_title, ID FROM {$wpdb->posts} WHERE post_type = 'legacy_reg_box'" );
}
if ( !empty( $this->boxes ) ) {
printf( '<option value=""> --- SELECT A BOX --- </option>' );
foreach ( $this->boxes as $box ) {
printf(
'<option value="%d" %s>%s</option>',
$box->ID,
selected( $box->ID == $id ),
$box->post_title
);
}
}
}
function _catalog_list_cache( $id = 0 ) {
if ( !$this->lists ) {
global $wpdb;
$this->lists = $wpdb->get_results( "SELECT post_title, ID FROM {$wpdb->posts} WHERE post_type = 'legacy_catalog_list'" );
}
if ( !empty( $this->lists ) ) {
printf( '<option value=""> --- SELECT A CATALOG LIST --- </option>' );
foreach ( $this->lists as $list ) {
printf(
'<option value="%d" %s>%s</option>',
$list->ID,
selected( $list->ID == $id ),
$list->post_title
);
}
}
}
function pitch_page_1_ui( $data ) {
$prefix = 'pitch_config[page][0]';
?>
<h4>Page 1</h4>
<div class="emusic-admin">
<h4>Title</h4>
<p>
<input type="text" class="widefat" name="<?php echo $prefix ?>[title]" value="<?php $this->_eee( $data, 'title' ) ?>"/>
</p>
<h4>Bullets</h4>
<div class="emusic-admin">
<?php if ( !empty( $data['bullet'] ) ) foreach ( $data['bullet'] as $i => $bullet ): ?>
<h4>Bullet <?php echo $i + 1 ?></h4>
<p>
<label>Heading:</label>
<input type="text" class="widefat" name="<?php echo $prefix ?>[bullet][<?php echo $i ?>][heading]" value="<?php $this->_eee( $bullet, 'heading' ) ?>"/><br/>
<label>Line 1:</label>
<input type="text" class="widefat" name="<?php echo $prefix ?>[bullet][<?php echo $i ?>][line1]" value="<?php $this->_eee( $bullet, 'line1' ) ?>"/><br/>
<label>Line 2:</label>
<input type="text" class="widefat" name="<?php echo $prefix ?>[bullet][<?php echo $i ?>][line2]" value="<?php $this->_eee( $bullet, 'line2' ) ?>"/>
</p>
<?php endforeach ?>
</div>
<?php
$this->parse_offer( $data['offer'], 0 );
$this->parse_signup( $data['signup'], 0 );
?>
</div>
<?php
}
function pitch_page_2_ui( $data ) {
?>
<h4>Page 2</h4>
<div class="emusic-admin">
<h4>Title</h4>
<p>
<input type="text" class="widefat" name="pitch_config[page][1][title]" value="<?php $this->_eee( $data, 'title' ) ?>"/>
</p>
<h4>Q and A</h4>
<select name="pitch_config[page][1][qa]">
<?php $this->_qa_cache( array( isset( $data['qa'] ) ? $data['qa'] : '' ) ) ?>
</select><br/>
<?php
$this->parse_offer( $data['offer'], 1 );
$this->parse_signup( $data['signup'], 1 );
?>
</div>
<?php
}
function pitch_page_3_ui( $data ) {
?>
<h4>Page 3</h4>
<div class="emusic-admin">
<h4>Title</h4>
<p>
<input type="text" class="widefat" name="pitch_config[page][2][title]" value="<?php $this->_eee( $data, 'title' ) ?>"/>
</p>
<h4>Text</h4>
<p>
<textarea rows="10" class="widefat" name="pitch_config[page][2][text]"><?php
if ( isset( $data['text'] ) ) {
echo esc_html( $data['text'] );
}
?></textarea>
</p>
<h4>Heading</h4>
<p>
<input type="text" class="widefat" name="pitch_config[page][2][heading]" value="<?php $this->_eee( $data, 'heading' ) ?>"/>
</p>
<?php
$this->parse_offer( $data['offer'], 2 );
$this->parse_signup( $data['signup'], 2 );
?>
<h4>Catalog Lists</h4>
<div class="lists">
<a class="add-catalog-list" data-page="2">Add a Catalog List</a>
<p>
<?php if ( !empty( $data['catalogList'] ) ) foreach ( array_filter( $data['catalogList'] ) as $i => $list ): ?>
<select name="pitch_config[page][2][catalogList][<?php echo $i ?>]">
<?php $this->_catalog_list_cache( $list ) ?>
</select><br/>
<?php endforeach ?>
</p>
</div>
</div>
<?php
}
function pitch_page_4_ui( $data ) {
?>
<h4>Page 4</h4>
<div class="emusic-admin">
<h4>Title</h4>
<p>
<input type="text" class="widefat" name="pitch_config[page][3][title]" value="<?php $this->_eee( $data, 'title' ) ?>"/>
</p>
<h4>Comparison Image</h4>
<?php
$key = 'pitch-page-4-comparison-image';
$this->_media_trigger( array( 'send_to' => $key ) );
if ( isset( $data['comparisonImage'] ) ) {
$html = $this->_resolve_image_by_id( $data['comparisonImage'] );
if ( !empty( $html ) )
echo $html;
}
?>
<input id="<?php echo $key ?>" type="hidden" name="pitch_config[page][3][comparisonImage]" value="<?php $this->_eee( $data, 'comparisonImage' ) ?>"/><br/>
<?php
$this->parse_offer( $data['offer'], 3 );
$this->parse_signup( $data['signup'], 3 );
?>
</div>
<?php
}
function reg_page_ui( $data, $i ) {
$page_number = $i + 1;
?>
<h4>Page <?php echo $page_number ?></h4>
<div class="emusic-admin">
<h4>Headline</h4>
<p>
<input type="text" class="widefat" name="reg_config[page][<?php echo $i ?>][headline]" value="<?php $this->_eee( $data, 'headline' ) ?>"/>
</p>
<h4>Motivation</h4>
<p>
<input type="text" class="widefat" name="reg_config[page][<?php echo $i ?>][motivation]" value="<?php $this->_eee( $data, 'motivation' ) ?>"/>
</p>
<h4>Instructions</h4>
<p>
<textarea rows="4" class="widefat" name="reg_config[page][<?php echo $i ?>][instructions]"><?php
if ( isset( $data['instructions'] ) ) {
echo esc_html( $data['instructions'] );
}
?></textarea>
</p>
<h4>Boxes</h4>
<div class="boxes">
<a class="add-reg-box" data-page="<?php echo $i ?>">Add a box</a>
<p>
<?php if ( !empty( $data['boxes'] ) ): foreach ( $data['boxes'] as $j => $box ): ?>
<select name="reg_config[page][<?php echo $i ?>][boxes][<?php echo $j ?>]">
<?php $this->_box_cache( $box ) ?>
</select><br/>
<?php endforeach; endif ?>
</p>
</div>
<h4>Continue Button</h4>
<?php
if ( isset( $data['button'] ) )
$data['continueButton'] = $data['button'];
?>
<p>
<label>Line 1:</label>
<input type="text" class="widefat" name="reg_config[page][<?php echo $i ?>][continueButton][line1]" value="<?php if ( isset( $data['continueButton'] ) ) $this->_eee( $data['continueButton'], 'line1' ) ?>"/><br/>
<label>Line 2:</label>
<input type="text" class="widefat" name="reg_config[page][<?php echo $i ?>][continueButton][line2]" value="<?php if ( isset( $data['continueButton'] ) ) $this->_eee( $data['continueButton'], 'line2' ) ?>"/>
</p>
</div>
<?php
}
function _resolve_image_id_by_hash( $hash ) {
global $wpdb;
return $wpdb->get_var( "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_name = '{$hash}'" );
}
function _resolve_image_by_hash( $hash, $width = 200 ) {
$id = $this->_resolve_image_id_by_hash( $hash );
$atts = wp_get_attachment_image_src( $id, array( $width, 9999 ) );
if ( !empty( $atts ) )
return sprintf( '<img src="%s" width="%d" height="%d"/>', $atts[0], $atts[1], $atts[2] );
}
function _resolve_image_by_id( $id, $width = 200 ) {
$atts = wp_get_attachment_image_src( $id, array( $width, 9999 ) );
if ( !empty( $atts ) )
return sprintf( '<img src="%s" width="%d" height="%d"/>', $atts[0], $atts[1], $atts[2] );
}
function parse_offer( $offer, $page = null ) {
$prefix = is_null( $page ) ? 'pitch_config[offer]' : sprintf( 'pitch_config[page][%d][offer]', $page );
$heading = is_null( $page ) ? __( 'Main Offer' ) : __( 'Offer' );
?>
<h4><?php echo $heading ?></h4>
<div class="emusic-admin">
<p>
<label>Overlay:</label>
<?php
$key = is_null( $page ) ? 'overlay-main-offer' : ( 'overlay-page-offer-' . $page );
$this->_media_trigger( array( 'send_to' => $key ) );
if ( isset( $offer['overlay'] ) ) {
$html = $this->_resolve_image_by_id( $offer['overlay'] );
if ( !empty( $html ) )
echo $html;
}
?>
<input id="<?php echo $key ?>" type="hidden" name="<?php echo $prefix ?>[overlay]" value="<?php $this->_eee( $offer, 'overlay' ) ?>"/><br/>
<label>Box:</label>
<select name="<?php echo $prefix ?>[box]">
<?php $this->_box_cache( isset( $offer['box'] ) ? $offer['box'] : 0 ) ?>
</select>
</p>
</div>
<?php
}
function parse_signup( $signup, $page = null ) {
global $post_ID;
$prefix = is_null( $page ) ? 'pitch_config[signup]' : sprintf( 'pitch_config[page][%d][signup]', $page );
$heading = is_null( $page ) ? __( 'Main Signup' ) : __( 'Signup' );
?>
<h4><?php echo $heading ?></h4>
<div class="emusic-admin">
<p>
<label>Headline:</label>
<input type="text" class="widefat" name="<?php echo $prefix ?>[headline]" value="<?php $this->_eee( $signup, 'headline' ) ?>"/><br/>
</p>
<p>
<label>Optional Image:</label>
<?php
$key = is_null( $page ) ? 'opt-image-main-signup' : ( 'opt-image-page-signup-' . $page );
$this->_media_trigger( array( 'send_to' => $key ) );
if ( isset( $signup['optionalImage'] ) ) {
$html = $this->_resolve_image_by_id( $signup['optionalImage'] );
if ( !empty( $html ) )
echo $html;
}
?>
<input id="<?php echo $key ?>" type="hidden" name="<?php echo $prefix ?>[optionalImage]" value="<?php $this->_eee( $signup, 'optionalImage' ) ?>"/><br/>
</p>
<h4>Button</h4>
<p>
<label>Line 1:</label>
<input type="text" class="widefat" name="<?php echo $prefix ?>[button][line1]" value="<?php $this->_eee( $signup['button'], 'line1' ) ?>"/><br/>
<label>Line 2:</label>
<input type="text" class="widefat" name="<?php echo $prefix ?>[button][line2]" value="<?php $this->_eee( $signup['button'], 'line2' ) ?>"/>
</p>
<?php if ( 3 === $page ): ?>
<h4>Bullet</h4>
<div class="emusic-admin">
<p>
<label>Heading:</label>
<input type="text" class="widefat" name="<?php echo $prefix ?>[bullet][heading]" value="<?php empty( $signup['bullet'] ) ? '' : $this->_eee( $signup['bullet'], 'heading' ) ?>"/><br/>
<label>Line 1:</label>
<input type="text" class="widefat" name="<?php echo $prefix ?>[bullet][line1]" value="<?php empty( $signup['bullet'] ) ? '' : $this->_eee( $signup['bullet'], 'line1' ) ?>"/><br/>
<label>Line 2:</label>
<input type="text" class="widefat" name="<?php echo $prefix ?>[bullet][line2]" value="<?php empty( $signup['bullet'] ) ? '' : $this->_eee( $signup['bullet'], 'line2' ) ?>"/>
<label>Image:</label>
<?php
$key = 'pitch-page-3-bullet-image';
$this->_media_trigger( array( 'send_to' => $key ) );
$id = '';
if ( isset( $signup['bullet']['image'] ) ) {
if ( strstr( $signup['bullet']['image'], '-' ) ) {
$html = $this->_resolve_image_by_hash( $signup['bullet']['image'] );
$id = $this->_resolve_image_id_by_hash( $signup['bullet']['image'] );
} else {
$html = $this->_resolve_image_by_id( $signup['bullet']['image'] );
$id = $signup['bullet']['image'];
}
if ( !empty( $html ) )
echo $html;
}
?>
<input id="<?php echo $key ?>" type="hidden" name="<?php echo $prefix ?>[bullet][image]" value="<?php echo $id ?>"/>
</p>
</div>
<?php endif ?>
</div>
<?php
}
function _raw_meta( $id, $key ) {
global $wpdb;
$data = $wpdb->get_var( "SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key = '{$key}' AND post_id = {$id}" );
return unserialize( $data );
}
function config_ui( $post ) {
$pitch = $this->_raw_meta( $post->ID, 'pitch_config' );
?>
<div class="postbox closed">
<div class="handlediv"><br/></div>
<h3>Pitch Config</h3>
<?php wp_nonce_field( 'legacy-offer-config', 'pitch_config_nonce' ) ?>
<div class="emusic-admin inside">
<?php
$this->parse_offer( $pitch['offer'] );
$this->parse_signup( $pitch['signup'] );
/**
* The JAVA side expects 4 pages, and it's not changing
* This is all for legacy support
*
*/
for ( $i = 0; $i < 4; $i++ )
call_user_func( array( $this, sprintf( 'pitch_page_%d_ui', $i + 1 ) ), $pitch['page'][$i] );
?>
</div>
</div>
<?php
/**
* Registration Config
*
*/
$reg = $this->_raw_meta( $post->ID, 'reg_config' );
$acpt = isset( $reg['allowCatalogPassThru'] ) && in_array( $reg['allowCatalogPassThru'], array( '1', 'true', true, 1 ) );
?>
<div class="postbox closed">
<div class="handlediv"><br/></div>
<h3>Registration Config</h3>
<?php wp_nonce_field( 'legacy-offer-config', 'reg_config_nonce' ) ?>
<div class="emusic-admin inside">
<p>Allow Catalog Pass-thru: &nbsp;
<input type="radio" name="reg_config[allowCatalogPassThru]" value="true" <?php checked( $acpt ) ?> /> Yes&nbsp;
<input type="radio" name="reg_config[allowCatalogPassThru]" value="false" <?php checked( empty( $acpt ) ) ?>/> No&nbsp;
</p>
<?php
/**
* The JAVA side expects 4 pages, and it's not changing
* This is all for legacy support
*
*/
for ( $i = 0; $i < 4; $i++ )
$this->reg_page_ui( $reg['page'][$i], $i );
?>
</div>
</div>
<?php
}
function save( $id, $post ) {
$this->save_postdata( $id, $post, array( $this, 'callback' ) );
}
function _fix_and_cast_data( &$arr ) {
array_walk_recursive( $arr, function ( &$value, $key ) {
if ( is_numeric( $value ) ) {
$value = (int) $value;
} else if ( is_string( $value ) && !seems_utf8( $value ) ) {
$value = Encoding::fixUTF8( $value );
}
} );
}
function callback( $id, $post ) {
if ( $this->post_type === $post->post_type ) {
if ( wp_verify_nonce( $_POST['pitch_config_nonce'], 'legacy-offer-config' ) ) {
$arr = stripslashes_deep( $_POST['pitch_config'] );
$this->_fix_and_cast_data( $arr );
update_post_meta( $id, 'pitch_config', $arr );
}
if ( wp_verify_nonce( $_POST['reg_config_nonce'], 'legacy-offer-config' ) ) {
$arr = stripslashes_deep( $_POST['reg_config'] );
$this->_fix_and_cast_data( $arr );
update_post_meta( $id, 'reg_config', $arr );
}
}
}
}
$_legacy_offer_config = new LegacyOfferConfig();
$_legacy_offer_config->init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment