Skip to content

Instantly share code, notes, and snippets.

@2ndkauboy
Created March 28, 2017 22:17
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 2ndkauboy/17791dceb1f4728400e196139ab4ea0a to your computer and use it in GitHub Desktop.
Save 2ndkauboy/17791dceb1f4728400e196139ab4ea0a to your computer and use it in GitHub Desktop.
diff --git addons/shortcodes.php addons/shortcodes.php
index af0a7e3..069165a 100644
--- addons/shortcodes.php
+++ addons/shortcodes.php
@@ -63,17 +63,23 @@ class CampTix_Addon_Shortcodes extends CampTix_Addon {
function shortcode_attendees( $attr ) {
global $post, $camptix;
+ $max_attendees = 10000;
+
$attr = shortcode_atts( array(
'order' => 'ASC',
'orderby' => 'title',
- 'posts_per_page' => 10000,
+ 'posts_per_page' => $max_attendees,
'tickets' => false,
'columns' => 3,
'questions' => '',
+ 'with_search' => false,
), $attr, 'camptix_attendees' );
$camptix_options = $camptix->get_options();
+ $attr['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
+ $attr['search'] = get_query_var( 'search' ) ? get_query_var( 'search' ) : '';
+
// Lazy load the camptix js.
wp_enqueue_script( 'camptix' );
@@ -113,24 +119,67 @@ class CampTix_Addon_Shortcodes extends CampTix_Addon {
}
}
+ if ( $attr['search'] ) {
+ if ( ! empty( $query_args['meta_query'] ) ) {
+ $query_args['meta_query'] = array(
+ 'relation' => 'AND',
+ $query_args['meta_query'],
+ );
+ }
+
+ $query_args['meta_query'][] = array(
+ 'relation' => 'OR',
+ array(
+ 'key' => 'tix_first_name',
+ 'compare' => 'LIKE',
+ 'value' => $attr['search'],
+ ),
+ array(
+ 'key' => 'tix_last_name',
+ 'compare' => 'LIKE',
+ 'value' => $attr['search'],
+ )
+ );
+ }
+
$attr['posts_per_page'] = absint( $attr['posts_per_page'] );
$questions = $this->get_questions_from_titles( $attr['questions'] );
- $paged = 0;
+ if ( $attr['posts_per_page'] < $max_attendees ) {
+ $with_pagination = true;
+ $posts_per_query = $attr['posts_per_page'];
+ $max_num_pages = 0;
+ } else {
+ $with_pagination = true;
+ $posts_per_query = 200;
+ $max_num_pages = 0;
+ }
+
+ $paged = $attr['paged'];
$printed = 0;
do_action( 'camptix_attendees_shortcode_init' );
?>
<div id="tix-attendees">
+ <?php if ( $attr['with_search'] ) : ?>
+ <div class="attendees_search">
+ <form role="search" method="get" class="search-form" action="<?php echo esc_url( get_permalink( get_the_ID() ) ); ?>">
+ <label>
+ <span class="screen-reader-text"><?php echo esc_html_x( 'Search for:', 'label', 'camptix' ); ?></span>
+ <input type="search" class="search-field" placeholder="<?php echo esc_attr_x( 'Search &hellip;', 'placeholder', 'camptix' ); ?>" value="<?php echo get_query_var( 'search' ); ?>" name="search" title="<?php echo esc_attr_x( 'Search for:', 'label', 'camptix' ); ?>" />
+ </label>
+ <input type="submit" class="search-submit" value="<?php echo esc_attr_x( 'Search', 'submit button', 'camptix' ); ?>" />
+ </form>
+ </div>
+ <?php endif; ?>
<ul class="tix-attendee-list tix-columns-<?php echo absint( $attr['columns'] ); ?>">
<?php
while ( true && $printed < $attr['posts_per_page'] ) {
- $paged++;
$attendee_args = apply_filters( 'camptix_attendees_shortcode_query_args', array_merge(
array(
'post_type' => 'tix_attendee',
- 'posts_per_page' => 200,
+ 'posts_per_page' => $posts_per_query,
'post_status' => array( 'publish', 'pending' ),
'paged' => $paged,
'order' => $attr['order'],
@@ -140,7 +189,10 @@ class CampTix_Addon_Shortcodes extends CampTix_Addon {
),
$query_args
), $attr );
- $attendees_raw = get_posts( $attendee_args );
+
+ $attendees_query = new WP_Query( $attendee_args );
+ $attendees_raw = $attendees_query->posts;
+ $max_num_pages = $attendees_query->max_num_pages;
if ( ! is_array( $attendees_raw ) || count( $attendees_raw ) < 1 )
break; // life saver!
@@ -198,12 +250,28 @@ class CampTix_Addon_Shortcodes extends CampTix_Addon {
} // foreach
$camptix->filter_post_meta = false; // cleanup
+ $paged++;
} // while true
?>
</ul>
</div>
<br class="tix-clear" />
<?php
+
+ if ( $with_pagination ) {
+ $big = 999999999; // need an unlikely integer
+ $translated = __( 'Page', 'camptix' ); // Supply translatable string
+
+ echo paginate_links( array(
+ 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
+ 'format' => '?paged=%#%',
+ 'current' => max( 1, get_query_var( 'paged' ) ),
+ 'total' => $max_num_pages,
+ 'before_page_number' => '<span class="screen-reader-text">' . $translated . ' </span>',
+ ) );
+ }
+ ?>
+ <?php
$this->log( sprintf( __( 'Generated attendees list in %s seconds', 'camptix' ), microtime(true) - $start ) );
wp_reset_postdata();
$content = ob_get_contents();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment