Skip to content

Instantly share code, notes, and snippets.

@danielpowney
Last active August 29, 2015 14:21
Show Gist options
  • Save danielpowney/69b8e0d75bff4f9621c4 to your computer and use it in GitHub Desktop.
Save danielpowney/69b8e0d75bff4f9621c4 to your computer and use it in GitHub Desktop.
rating-form.php template with custom order for rating items and custom fields
<div class="rating-form <?php echo $class; ?>">
<?php
if ( ! empty( $title ) ) {
$before_title = apply_filters( 'mrp_rating_form_before_title', $before_title, $post_id, $rating_form_id );
$after_title = apply_filters( 'mrp_rating_form_after_title', $after_title, $post_id, $rating_form_id );
echo "$before_title" . esc_html( $title ) . "$after_title";
}
// if an entry exists, show message
if ( $rating_entry_id != null && $show_status_message == true ) {
if ( $entry_status != 'approved' ) {
?>
<p class="message mrp"><?php echo esc_html( $rating_awaiting_moderation_message ); ?></p>
<?php
} else {
?>
<p class="message mrp"><?php echo esc_html( $already_submitted_rating_form_message ); ?></p>
<?php
}
}
$user_can_update_delete = apply_filters( 'mrp_user_can_update_delete', $user_can_update_delete, $user_id, $rating_entry_id, $post_id, $rating_form_id );
// if already submitted a rating and user cannot update or delete, do not show rating form
if ( ! ($rating_entry_id != null && ! $user_can_update_delete ) ) {
?>
<form id="rating-form-<?php echo $rating_form_id; ?>-<?php echo $post_id; ?>-<?php echo $sequence; ?>" action="#">
<?php
do_action( 'mrp_rating_form_before_rating_items', $post_id, $rating_form_id, $rating_items );
$form_items = array();
/*
* Requires PHP knowledge to change the order how you like. Here's two examples: rating item then
* custom field repeat, all rating items and then all custom fields
*/
if ( $rating_form_id == 2 ) {
$object = array_shift( $rating_items );
array_push( $form_items, array( 'type' => 'rating_item', 'object' => $object ) );
$object = array_shift( $custom_fields );
array_push( $form_items, array( 'type' => 'custom_field', 'object' => $object ) );
$object = array_shift( $rating_items );
array_push( $form_items, array( 'type' => 'rating_item', 'object' => $object ) );
$object = array_shift( $custom_fields );
array_push( $form_items, array( 'type' => 'custom_field', 'object' => $object ) );
} else {
foreach ( $rating_items as $rating_item ) {
array_push( $form_items, array( 'type' => 'rating_item', 'object' => $rating_item ) );
}
foreach ( $custom_fields as $custom_field ) {
array_push( $form_items, array( 'type' => 'custom_field', 'object' => $custom_field ) );
}
}
/**
* Rating Items
*/
foreach ( (array) $form_items as $form_item ) {
if ( $form_item['type'] == 'rating_item' ) {
$rating_item = $form_item['object'];
$rating_item_id = $rating_item['rating_item_id'];
$element_id = 'rating-item-' . $rating_item_id . '-' . $sequence ;
$description = $rating_item['description'];
$rating_item_type = $rating_item['type'];
$max_option_value = $rating_item['max_option_value'];
$default_option_value = $rating_item['default_option_value'];
$required = $rating_item['required'];
$option_value_text = $rating_item['option_value_text'];
$rating_item_type = $rating_item['type'];
if ( isset( $selected_option_lookup[$rating_item_id] ) ) {
$default_option_value = $selected_option_lookup[$rating_item_id];
}
// FIXME use API function get_rating_item_option_value_text_lookup()
// lookup the option text descriptions for select and radio buttons
$option_value_text_lookup = array();
$option_value_text_array = preg_split( '/[\r\n,]+/' , $option_value_text, -1, PREG_SPLIT_NO_EMPTY );
foreach ( (array) $option_value_text_array as $current_option_value_text ) {
$parts = explode( '=', $current_option_value_text );
if ( count( $parts ) == 2 && is_numeric( $parts[0] ) ) {
$option_value_text_lookup[intval( $parts[0] )] = stripslashes( $parts[1] ); // TOD stripslashes?
}
}
mrp_get_template_part( 'rating-form', 'rating-item', true, array(
'rating_item_id' => $rating_item_id,
'element_id' => $element_id,
'description' => $description,
'max_option_value' => $max_option_value,
'default_option_value' => $default_option_value,
'required' => $required,
'option_value_text' => $option_value_text,
'class' => null,
'style' => null,
'icon_classes' => $icon_classes,
'use_custom_star_images' => $use_custom_star_images,
'rating_item_type' => $rating_item_type,
'option_value_text_lookup' => $option_value_text_lookup,
'rating_entry_id' => $rating_entry_id
) );
?>
<!-- hidden field to get rating item id -->
<input type="hidden" value="<?php echo $rating_item_id; ?>" class="rating-item-<?php echo $rating_form_id; ?>-<?php echo $post_id; ?>-<?php echo $sequence; ?>" id="hidden-rating-item-id-<?php echo $rating_item_id; ?>" />
<?php
} else {
$custom_field = $form_item['object'];
$custom_field_id = $custom_field['custom_field_id'];
$label = $custom_field['label'];
$required = $custom_field['required'];
$max_length = $custom_field['max_length'];
$type = $custom_field['type'];
$placeholder = $custom_field['placeholder'];
$element_id = 'custom-field-' . $custom_field_id . '-' . $sequence;
$value = '';
if ( isset( $custom_field_values_lookup[$custom_field_id] ) ) {
$value = $custom_field_values_lookup[$custom_field_id];
}
mrp_get_template_part( 'rating-form', 'custom-fields', true, array(
'custom_field_id' => $custom_field_id,
'label' => $label,
'required' => $required,
'max_length' => $max_length,
'type' => $type,
'placeholder' => $placeholder,
'value' => $value,
'element_id' => $element_id
) );
?>
<!-- hidden field to get custom field id -->
<input type="hidden" value="<?php echo $custom_field_id; ?>" class="custom-field-<?php echo $rating_form_id; ?>-<?php echo $post_id; ?>-<?php echo $sequence; ?>" id="hidden-custom-field-id-<?php echo $custom_field_id; ?>" />
<?php
}
}
mrp_get_template_part( 'rating-form', 'optional-fields', true, array(
'show_name_input' => $show_name_input,
'show_email_input' => $show_email_input,
'show_comment_textarea' => $show_comment_textarea,
'comment_id' => $comment_id,
'user_id' => $user_id,
'name' => $name,
'email' => $email,
'comment' => $comment,
'sequence' => $sequence
) );
do_action( 'mrp_rating_form_before_buttons' );
$button_text = $submit_button_text;
if ( $rating_entry_id != null ) {
$button_text = $update_button_text;
?>
<input type="hidden" value="<?php echo $rating_entry_id; ?>" id="ratingEntryId-<?php echo $rating_form_id; ?>-<?php echo $post_id; ?>-<?php echo $sequence; ?>" />
<input type="button" class="btn btn-default delete-rating" id="deleteBtn-<?php echo $rating_form_id; ?>-<?php echo $post_id; ?>-<?php echo $sequence; ?>" value="<?php echo esc_attr( $delete_button_text ); ?>"></input>
<?php
}
?>
<input type="button" class="btn btn-default save-rating" id="saveBtn-<?php echo $rating_form_id; ?>-<?php echo $post_id; ?>-<?php echo $sequence; ?>" value="<?php echo esc_attr( $button_text ); ?>"></input>
<input type="hidden" name="sequence" value="<?php echo $sequence; ?>" />
<?php
do_action( 'mrp_rating_form_after_buttons' );
?>
</form>
<?php
}
?>
</div>
Write Preview Parsed as Markdown Edit in fullscreen
Comment
Status API Blog About © 2015 GitHub, Inc. Terms Privacy Security Contact
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment