Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "Status" and other results fields. Limit search results to current site for WP multisite. #2

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ This way it's in the correct folder and you can update the plugin by going to th
directory and use git pull to obtain the latest version.

Don't forget to activate the plugin through the 'Plugins' menu in WordPress

**Permissions**

Roles that have the `list_users` capability can access the search page. Alternatively, users with the custom `as_em_search` capability can access the search page. (The plugin does not add or remove this custom capability, but checks for it. You can add it using WP-CLI or a role editor plugin.)
56 changes: 46 additions & 10 deletions as-em-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
*
* @wordpress-plugin
* Plugin Name: AS EM Search
* Description: Makes it possible to search in bookings and events
* Version: 1.1.0
* Description: Makes it possible to search in Events Manager bookings and events
* Version: 1.1.3
* Author: KoenG
* Text Domain: as-em-search
* Domain Path: /languages
Expand All @@ -27,6 +27,20 @@
add_filter('em_create_events_submenu', 'as_em_search_admin_menu');

function as_em_search_admin_menu($plugin_pages){
// first check if current user has custom capability 'as_em_search'
if (current_user_can( 'as_em_search' )) {
$plugin_pages['search_form'] =
add_submenu_page(
'edit.php?post_type='.EM_POST_TYPE_EVENT,
__('Search bookings','as-em-search'),
__('Search bookings','as-em-search'),
'as_em_search',
"events-manager-search-form",
'as_em_search_show_form'
);
return $plugin_pages;
}
// fall back to the default 'list_users' capability
$plugin_pages['search_form'] =
add_submenu_page(
'edit.php?post_type='.EM_POST_TYPE_EVENT,
Expand All @@ -46,22 +60,44 @@ function as_em_search_show_form(){
function as_em_search( $search_term ) {
global $wpdb;
$table_name = EM_BOOKINGS_TABLE;

$query = $wpdb->prepare(
"SELECT booking_id, event_id, booking_spaces, booking_meta FROM $table_name WHERE booking_meta LIKE %s ORDER BY event_id",
'%' . $search_term . '%'
);
$events_tbl = EM_EVENTS_TABLE;
if (is_multisite()) {
# for multisite, join on blog_id
$blog_id = get_current_blog_id();
$sql = <<<EOD
SELECT b.booking_id, b.event_id, b.booking_spaces, booking_status, b.booking_meta, b.booking_date
FROM $table_name b
INNER JOIN $events_tbl e on b.event_id = e.event_id
WHERE b.booking_meta LIKE %s
AND e.blog_id = %d
ORDER BY e.event_start desc, b.event_id desc, b.booking_date desc
LIMIT 101
EOD;
$query = $wpdb->prepare($sql, '%' . $search_term . '%', $blog_id);
} else {
# if not multisite, the wp_em_events.blog_id might be null, 0 or 1
$sql = <<<EOD
SELECT b.booking_id, b.event_id, b.booking_spaces, booking_status, b.booking_meta, b.booking_date
FROM $table_name b
INNER JOIN $events_tbl e on b.event_id = e.event_id
WHERE b.booking_meta LIKE %s
AND (e.blog_id IS NULL or e.blog_id <= 1)
ORDER BY e.event_start desc, b.event_id desc, b.booking_date desc
LIMIT 101
EOD;
$query = $wpdb->prepare($sql, '%' . $search_term . '%');
}

return $wpdb->get_results($query) ;
}

function as_em_get_event_name( $event_id ) {
function as_em_get_event_info( $event_id ) {
global $wpdb;

$query = $wpdb->prepare('SELECT event_name FROM ' . EM_EVENTS_TABLE . ' WHERE event_id = %d', $event_id);
$query = $wpdb->prepare('SELECT event_name, event_start_date, event_start_time, event_start FROM ' . EM_EVENTS_TABLE . ' WHERE event_id = %d', $event_id);
$event = $wpdb->get_row($query, ARRAY_A);

return $event['event_name'];
return $event;
}

function as_em_search_load_plugin_textdomain() {
Expand Down
26 changes: 23 additions & 3 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ Contributors: (this should be a list of wordpress.org userid's)
Donate link: http://example.com/
Tags: search, bookings, events, find
Requires at least: 4.0
Tested up to: 4.0
Stable tag: 1.0.0
Tested up to: 5.4.1
Stable tag: 1.1.3
License: The MIT License (MIT)
License URI: http://opensource.org/licenses/MIT

Expand Down Expand Up @@ -41,4 +41,24 @@ Yes.

= How do I get support? =

Please post an issue in the github issue tracker.
Please post an issue in the github issue tracker.


== Changelog ==

= 1.1.3 =
* Sort events by most recent first.
* Improve event name formatting, and include start date and time.

= 1.1.2 =
* Added permissions check for custom 'as_em_search' capability.

= 1.1.1 =
* Added 'Booking Date', 'Booking Status', and 'Country' to the results columns.
* Multisite compatibility: will search the current site only.
* Limit results to first 100 records (sorted by most recent booking date first).
* Added a simple help prompt and "no results" message.
* Added standard WP formatting to results.

= 1.1.0 =
* Original version
39 changes: 35 additions & 4 deletions search-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@
} else {
$search_results = null;
}
$as_em_status_array = array(
0 => 'Pending',
1 => 'Approved',
2 => 'Rejected',
3 => 'Cancelled',
4 => 'Awaiting Payment'
);
?>
<div><?php _e('Search bookings','as-em-search');?></div>
<h1><?php _e('Search bookings','as-em-search');?></h1>
<form method="get">
<input type="hidden" name="post_type" value="<?php echo EM_POST_TYPE_EVENT;?>">
<input type="hidden" name="page" value="events-manager-search-form">
Expand All @@ -21,14 +28,17 @@

<div>
<?php if( null != $search_results ): ?>
<table>
<table class="wp-list-table widefat fixed striped pages">
<thead>
<tr>
<th><?php _e('Name', 'as-em-search');?></th>
<th><?php _e('Email', 'as-em-search');?></th>
<th><?php _e('City','as-em-search');?></th>
<th><?php _e('Country','as-em-search');?></th>
<th><?php _e('Phone','as-em-search');?></th>
<th><?php _e('Spaces','as-em-search');?></th>
<th><?php _e('Booked On','as-em-search');?></th>
<th><?php _e('Status','as-em-search');?></th>
</tr>
</thead>
<tbody>
Expand All @@ -41,10 +51,13 @@
<?php if( $event_id != $result->event_id ):?>
<?php
$event_id = $result->event_id;
$event_name = as_em_get_event_name( $event_id );
$event_info = as_em_get_event_info( $event_id );
?>
<tr>
<td colspan="5"><?php echo $event_name;?></td>
<td colspan="3" class="as_evtname"><h3><?php echo $event_info['event_name'];?></h3></td>
<td colspan="5" style="vertical-align:middle;" class="as_evtstart">
starts on: <?php echo $event_info['event_start_date'].' '.$event_info['event_start_time'].' (local) / '.$event_info['event_start'].' UTC';?>
</td>
</tr>
<?php endif;?>
<tr>
Expand All @@ -65,15 +78,33 @@
<td>
<?php echo $meta['dbem_city'];?>
</td>
<td>
<?php echo $meta['dbem_country'];?>
</td>
<td>
<?php echo $meta['dbem_phone'];?>
</td>
<td>
<?php echo $result->booking_spaces;?>
</td>
<td>
<?php echo $result->booking_date;?>
</td>
<td>
<?php echo ($result->booking_status < 4 ? $as_em_status_array[$result->booking_status] : "Awaiting Payment (#$result->booking_status)"); ?>
</td>
</tr>
<?php endforeach;?>
<?php if( count($search_results) > 100 ):?>
<tr>
<td colspan="8">Only the first 100 results are shown</td>
</tr>
<?php endif;?>
</tbody>
</table>
<?php elseif( ! empty( $_GET ) && isset( $_GET['as_em_search'] ) ): ?>
<p>No results for "<?php echo $_GET['as_em_search']?>".</p>
<?php else: ?>
<p>Enter your search term above.&nbsp; Up to 100 rows are returned, sorted by most recent events and most recent bookings first.</p>
<?php endif; ?>
</div>