public
Last active

Mini Loops Ajax Paging

  • Download Gist
miniloops-ajax-paging.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
<?php
// Plugin Name: Mini Loops Ajax Paging
// Description: Enables ajax paging in widget.
// Version: 2013.03.02
// Author: Kailey Lampert
// Author URI: kaileylampert.com
 
add_action( 'init', 'miniloops_ajax_paging_init' );
function miniloops_ajax_paging_init() {
if ( ! function_exists( 'miniloops' ) ) return;
add_filter( 'miniloops_after_items_format', 'mlap_after_the_miniloop_html', 10, 4 );
 
add_action( 'in_widget_form', 'mlap_add_navigation_label_fields', 10, 3 );
add_filter( 'widget_update_callback', 'mlap_widget_update_callback', 10, 2 );
 
add_action( 'wp_ajax_ml_next_page', 'ml_next_page_cb' );
add_action( 'wp_ajax_nopriv_ml_next_page', 'ml_next_page_cb' );
}
 
function mlap_after_the_miniloop_html( $after_items, $query, $miniloop, $args ) {
if ( apply_filters( 'mlap_use_default_styles', true ) ) {
?><style>
.hide-if-no-js { display: none; }
.ml-page-nav { overflow: hidden; }
.ml-next, .ml-prev { width: 50%; }
.ml-next { float: right; text-align: right;}
.ml-prev { float: left; text-align: left; }
</style><?php
}
wp_enqueue_script( 'jquery' );
add_action( 'wp_footer', 'mlap_footer_script', 99 );
 
$max = $miniloop->max_num_pages;
$paged = (int) $miniloop->query_vars['paged'];
$nav = array();
if ( $paged > 1 ) {
$prev_page = $paged-1;
$label = $args['prev_label'];
$nav[] = "<a class='ml-previous' href='#$prev_page'>$label</a>";
}
if ( $paged < $max ) {
$next_page = $paged+1;
$label = $args['next_label'];
$nav[] = "<a class='ml-next' href='#$next_page'>$label</a>";
}
 
$after_items .= '<div class="ml-page-nav hide-if-no-js">'. implode( ' ', $nav ) .'</div>';
return $after_items;
 
}
 
function mlap_footer_script() {
?><script>
var mlajax = '<?php echo admin_url('admin-ajax.php'); ?>';
jQuery('.hide-if-no-js').removeClass('hide-if-no-js');
jQuery('.miniloops').on('click', '.ml-next, .ml-previous', function(ev) {
var elm = jQuery(this),
par = elm.closest('.miniloops');
ev.preventDefault();
jQuery.post( mlajax, {
action: 'ml_next_page',
widget: par.attr('id'),
href: jQuery(this).attr('href')
}, function( response ) {
par.fadeOut( 'fast', function() {
par.html( response ).fadeIn();
jQuery('.hide-if-no-js').removeClass('hide-if-no-js');
});
});
});
</script><?php
}
function ml_next_page_cb() {
$id = str_replace('miniloops-', '', $_POST['widget']);
$widget_options = get_option('widget_miniloops');
$this_instance = $widget_options[ $id ];
 
$href = explode( '#', $_POST['href'] );
$paged = array_pop( array_filter( $href ) );
 
$this_instance['paged'] = $paged;
 
// look up which sidebar this widget is in, get the sidebar's args
global $wp_registered_sidebars;
$sidebars = wp_get_sidebars_widgets();
$this_widget_id = 'miniloops-'.$id;
foreach( $sidebars as $sb => $widgets ) {
if ( in_array( $this_widget_id, $widgets ) ) {
$sidebar = $sb;
break;
}
}
$widget_args = isset( $sidebar ) ? $wp_registered_sidebars[ $sidebar ] : array();
// the js will put the contents into the preexisting before/after widget markup
$widget_args['before_widget'] = $widget_args['after_widget'] = '';
 
the_widget( 'miniloops', $this_instance, $widget_args );
 
die();
}
 
function mlap_add_navigation_label_fields( $widget, $return, $instance ) {
if ( ! is_a( $widget, 'miniloops' ) ) return;
 
$prev_label = isset( $instance['prev_label'] ) ? $instance['prev_label'] : '&laquo; Previous';
$next_label = isset( $instance['next_label'] ) ? $instance['next_label'] : 'Next &raquo;';
?>
<p style="width:48%; float:left;">
<label for "<?php echo $widget->get_field_id( 'prev_label'); ?>"><?php _e( '"Previous" Label:', 'mini-loops' ); ?>
<input class="widefat" id="<?php echo $widget->get_field_id( 'prev_label' ); ?>" name="<?php echo $widget->get_field_name('prev_label'); ?>" type="text" value="<?php echo esc_attr( $prev_label ); ?>" />
</label>
</p>
<p style="width:48%; float:right;">
<label for "<?php echo $widget->get_field_id( 'next_label'); ?>"><?php _e( '"Next" Label:', 'mini-loops' ); ?>
<input class="widefat" id="<?php echo $widget->get_field_id( 'next_label' ); ?>" name="<?php echo $widget->get_field_name('next_label'); ?>" type="text" value="<?php echo esc_attr( $next_label ); ?>" />
</label>
</p>
<div style="clear:both;"></div>
<?php
 
}
 
function mlap_widget_update_callback( $instance, $new_instance ) {
$instance['prev_label'] = wp_kses( $new_instance['prev_label'] );
$instance['next_label'] = wp_kses( $new_instance['next_label'] );
return $instance;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.