Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Mini Loops Ajax Paging
<?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;
}
@iRolf

hi Kailey,
I've got a question, a bug and a feature for you:

this plugin addon alters the widget out put by addinng navigation links, okay. but why does it still displays all the n-posts I liked to show in the widget? or: if I set the widget to 1 and I only wan't to let the user step back n steps, how can I limit this?

when adding images in the output stream, the first time they are scaled miniloop shows RAW output like �����JFIF������;CREATOR: gd-jpeg v1.0 (using IJG JPEG v90), quality = 90 ��C��������������������� ���� � �� ��� ��������������������������C������� �� � � ������������������������������������������������������22��"������������������������� �����������������}�������!1A��Qa�"q�2����#B���R��$3br� ���

and finally: could you make the ajax feature optional within the widget? so I can use them accordingly my needs or do I missed that spot?

thank you at all for mini loops!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.