Skip to content

Instantly share code, notes, and snippets.

@kitchin
Last active August 29, 2015 14:02
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 kitchin/6f534d0bc58cce1e107e to your computer and use it in GitHub Desktop.
Save kitchin/6f534d0bc58cce1e107e to your computer and use it in GitHub Desktop.
Testimonial Rotator Fix 201406
<?php
/*
Plugin Name: Testimonial Rotator Fix 201406
Description: Add/remove testimonials to/from a rotator. For the plugin Testimonial Rotator, https://wordpress.org/plugins/testimonial-rotator/ See: https://wordpress.org/support/topic/fix-for-missing-testimonials-in-widget-rotator-after-203 Needs more testing, use at your own risk.
Version: 0.1
Author: kitchin
Plugin URI: https://gist.github.com/kitchin/6f534d0bc58cce1e107e
Date: 2014/06, for Wordpress 3.9.1+, Testimonial Rotator 2.0.3
License: Needs more testing, use at your own risk.
*/
add_action('admin_enqueue_scripts', 'testimonial_rotator_fix201406_action_admin_enqueue_scripts');
/*
*/
function testimonial_rotator_fix201406_action_admin_enqueue_scripts() {
wp_enqueue_script( 'jquery' );
}
add_action('admin_menu', 'testimonial_rotator_fix201406_action_add_menu_items');
/*
*/
function testimonial_rotator_fix201406_action_add_menu_items() {
add_menu_page(
'Testimonial Rotator Fix', // page_title
'Testimonial Rotator Fix', // menu_title
'edit_pages', // capability
'testimonial_rotator_fix201406', // menu_slug
'testimonial_rotator_fix201406_render' // function
);
}
/*
*/
function testimonial_rotator_fix201406_render() {
global $wpdb;
$title= $GLOBALS['title'];
// $plugin_page= $GLOBALS['plugin_page'];
?>
<div class="wrap">
<h2><?php echo $title; ?></h2>
<div style="margin: 1em">
<p><i>Note:</i> You can remove this plugin when done.</p>
<?php
$rot_id= '';
if ( isset( $_POST['_testimonial_rotator_fix201406_nonce'] ) ) {
$maybe_id= (int)$_POST['rot_id'];
$action= preg_replace('/\W/', '', $_POST['action']);
check_admin_referer( $action, '_testimonial_rotator_fix201406_nonce' );
if ( $maybe_id ) {
if ( $rot= get_post( $maybe_id ) and $rot->post_type == 'testimonial_rotator' ) {
$rot_id = $maybe_id;
print "<p>Rotator found: #$rot_id, '$rot->post_title'</p>\n";
}
}
if (! $rot_id) {
print( "<p>ERROR: Not found: #$maybe_id</p>\n" );
}
}
$query= "SELECT `ID`, `post_title` FROM `$wpdb->posts` WHERE `post_type`='testimonial_rotator' ORDER BY `post_title`";
if ( $rots= $wpdb->get_results($query) ) {
?>
<form method="post">
<?php wp_nonce_field( 'select_rot', '_testimonial_rotator_fix201406_nonce' ); ?>
<input type="hidden" name="action" value="select_rot">
<p>Rotator ID: <select name="rot_id">
<?php
foreach ($rots as $rot) {
$sel= selected($rot->ID, $rot_id, false);
print "<option name='rot_id' value='$rot->ID' $sel>#$rot->ID, $rot->post_title</option>\n";
}
?>
</select>
<?php submit_button('Select Rotator', 'primary', 'selectrot', false); ?></p>
</form>
<?php
if ( $rot_id ) {
$query= "SELECT `ID` FROM `$wpdb->posts` WHERE `post_type`='testimonial' ORDER BY `post_title`";
if ( $ids= $wpdb->get_col($query) ) {
?>
<form method="post">
<?php wp_nonce_field( 'process_rot', '_testimonial_rotator_fix201406_nonce' ); ?>
<input type="hidden" name="action" value="process_rot">
<input type="hidden" name="rot_id" value="<?php echo $rot_id; ?>">
<p>Found <?php echo count($ids); ?> testimonial(s) total</p>
<table border="1" cellspacing="0" cellpadding="5">
<tr><th>#</th>
<th>In Rotator #<?php echo $rot_id; ?></th>
<th>All Rotators</th>
<th>Testimonial</th></tr>
<?php
foreach ($ids as $i=>$id) {
$testi= get_post($id);
$rots= get_post_meta($id, '_rotator_id', true);
$rots= array_filter( explode('|', (string) $rots), 'strlen' ); // from the plugin
$inrot_pos = array_search( $rot_id, $rots );
$inrot = $inrot_pos !== false;
$note = '';
if ( $action=='process_rot' ) {
$note= ' No change';
if ( empty($_POST["inrot_shown_$id"] ) ) {
$note= ' Error, data has changed since post.';
} elseif ( empty($_POST["inrot_$id"] ) ) {
if ( $inrot ) {
array_splice( $rots, $inrot_pos, 1 );
if ( $rots ) {
update_post_meta( $id, '_rotator_id', join( '|', $rots ) );
} else {
delete_post_meta( $id, '_rotator_id' );
}
$inrot= false;
$note= ' Removed';
}
} elseif ( ! $inrot ) {
$rots[]= $rot_id;
update_post_meta( $id, '_rotator_id', join( '|', $rots ) );
$inrot= true;
$note= ' Added';
}
}
$rots_st= join(', ', $rots) or $rots_st= '<i>none</i>';
$check= checked($inrot, true, false);
printf(
"<tr>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
</tr>\n",
$i + 1,
"<input type='hidden' name='inrot_shown_$id' value='yes'><input type='checkbox' name='inrot_$id' value='yes' $check> $note",
$rots_st,
"#$id, $testi->post_title"
);
}
?>
<tr><td>&nbsp;</td>
<td colspan="3"><input type="checkbox" value="yes" id="inrot-all"> All/none</td></tr>
</table>
<script>
jQuery(document).ready(function($){
$('#inrot-all').change(function(){
$(this).closest('form').find('input:checkbox').prop('checked', this.checked);
});
});
</script>
<?php submit_button('Update'); ?>
</form>
<?php
} else {
print "<p>No results</p>\n";
}
}
} else {
print "<p>No rotators</p>\n";
}
?>
</div>
</div>
<?php
}
@kitchin
Copy link
Author

kitchin commented Jun 4, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment