';
if(empty($name_filter['character']) && empty($search_value) && $directory['show_current_num_names'])
{
if(empty($directory['name_term'])) {
echo sprintf(__('There are currently %d names in this directory', 'name-directory'), $num_names);
} else {
if( $num_names == 1 ) {
echo sprintf(__('There is currently %d %s in this directory', 'name-directory'), $num_names, $directory['name_term_singular']);
} else {
echo sprintf(__('There are currently %d %s in this directory', 'name-directory'), $num_names, $directory['name_term']);
}
}
echo '.';
}
else if(empty($name_filter['character']) && ! empty($search_value))
{
if(empty($directory['name_term'])) {
echo sprintf(__('There are %d names in this directory containing the search term %s.', 'name-directory'), $num_names, "" . $search_value . "");
} else {
if( $num_names == 1 ) {
echo sprintf(__('There is currently %d %s in this directory containing the search term %s.', 'name-directory'), $num_names, $directory['name_term_singular'], "" . $search_value . "");
} else {
echo sprintf(__('There are currently %d %s in this directory containing the search term %s.', 'name-directory'), $num_names, $directory['name_term'], "" . $search_value . "");
}
}
echo " " . __('Clear results', 'name-directory') . ". ";
}
else if($directory['show_current_num_names'] && isset($name_filter['character']) && $name_filter['character'] == 'latest')
{
if(empty($directory['name_term'])) {
echo sprintf(__('Showing %d most recent names in this directory', 'name-directory'), $num_names);
} else {
echo sprintf(__('Showing %d most recent %s in this directory', 'name-directory'), $num_names, $directory['name_term']);
}
echo '.';
}
else if($directory['show_current_num_names'])
{
if(empty($directory['name_term'])) {
if( $num_names == 1 ) {
echo sprintf(__('There is currently %d %s in this directory beginning with the letter %s.', 'name-directory'), $num_names, strtolower(__('Name', 'name-directory')), $name_filter['character']);
} else {
echo sprintf(__('There are currently %d %s in this directory beginning with the letter %s.', 'name-directory'), $num_names, __('names', 'name-directory'), $name_filter['character']);
}
} else {
if( $num_names == 1 ) {
echo sprintf(__('There is currently %d %s in this directory beginning with the letter %s.', 'name-directory'), $num_names, $directory['name_term_singular'], $name_filter['character']);
} else {
echo sprintf(__('There are currently %d %s in this directory beginning with the letter %s.', 'name-directory'), $num_names, $directory['name_term'], $name_filter['character']);
}
}
}
echo '
';
if(empty($directory['name_term'])) {
echo __('There are no entries in this directory at the moment', 'name-directory');
} else {
echo sprintf(__('There are no %s in this directory at the moment', 'name-directory'), $directory['name_term']);
}
echo '.
';
if(empty($directory['name_term'])) {
echo trim(__('Please select a letter from the index (above) to see entries', 'name-directory'));
} else {
echo trim(sprintf(__('Please select a letter from the index (above) to see %s', 'name-directory'), $directory['name_term']));
}
echo '.
';
$i = 1;
$split_i = 0;
$this_letter = '---';
foreach($names as $entry)
{
/* Show the header of the next starting letter */
if(! empty($directory['show_character_header']))
{
if ($entry['letter'] !== $this_letter) {
$this_letter = $entry['letter'];
echo '
" . $submit_string . "";
}
/** Sad to print it like this, but this is needed for translating the show more/less buttons */
echo "";
if($highlight_search_term == true)
{
wp_enqueue_script( 'name-directory-highlight', 'https://cdn.jsdelivr.net/mark.js/8.6.0/mark.min.js', array() );
wp_add_inline_script( 'name-directory-highlight', 'var markInstance = new Mark(document.querySelector(".name_directory_names"));
markInstance.mark("' . $search_value_js . '");');
}
return ob_get_clean();
}
add_shortcode('namedirectory', 'name_directory_show_directory');
/**
* Display a random name from a given directory
* Thanks to @mastababa
* @param $attributes
* @return mixed
*/
function name_directory_show_random($attributes)
{
$dir = null;
extract(shortcode_atts(
array('dir' => '1'),
$attributes
));
$directory = name_directory_get_directory_properties($dir);
if($directory === null)
{
echo sprintf(__('Error: Name Directory #%d does not exist (anymore). If you are the webmaster, please change the shortcode.', 'name-directory'), $dir);
return false;
}
$names = name_directory_get_directory_names($directory);
if (! count($names))
{
echo __('There are no entries in this directory at the moment', 'name-directory');
}
$entry = $names[array_rand($names)];
ob_start();
echo '
';
return ob_get_clean();
}
add_shortcode('namedirectory_random', 'name_directory_show_random');
/**
* Display a single name by ID
* -> Mind you, the name does have to be published!
* @param $attributes
* @return mixed
*/
function name_directory_show_single_name($attributes)
{
$id = null;
extract(shortcode_atts(
array('id' => '1'),
$attributes
));
$name_entry = name_directory_get_single_name($id);
$directory = name_directory_get_directory_properties($name_entry['directory']);
ob_start();
echo '
';
return ob_get_clean();
}
add_shortcode('namedirectory_single', 'name_directory_show_single_name');
/**
* Search the Name Directory entries whenever necessary
* Add the search results to the WordPress search results
* @param $where (is passed by WordPress)
* @return string
*/
function name_directory_insert_sitewide_search_results($where)
{
/* Only perform actions whenever we are on a search page and within the main query */
if (is_search())
{
$name_directory_settings = get_option('name_directory_general_option');
/* If WordPress search for Name Directory is disabled, just return */
if(empty($name_directory_settings) || empty($name_directory_settings['search_on']))
{
return $where;
}
$directories = name_directory_get_directory_by_search_query(
get_search_query(),
$name_directory_settings['search_description'],
$name_directory_settings['search_wildcard']
);
$page_ids = array();
global $wpdb;
/* If directories were found, get the page they are on */
foreach($directories as $found => $dir)
{
/* Use a plain sql query, WP_Query unfortunately didn't work (infinite loop) */
$directory_id = intval($dir['directory']);
$host_pages = $wpdb->get_results("
SELECT *
FROM `{$wpdb->prefix}posts`
WHERE
`post_type` IN ('page', 'post')
AND `post_status` = 'publish'
AND (`post_content` LIKE '%[namedirectory dir=\"{$directory_id}%'
OR `post_content` LIKE '%[namedirectory dir={$directory_id}%'
OR `post_content` LIKE '%[namedirectory dir=''{$directory_id}%')");
if ($host_pages)
{
/* Add the pages to the results that WordPress will present to the user */
foreach($host_pages as $host_page)
{
$page_ids[] = $host_page->ID;
}
}
}
$page_ids = array_unique($page_ids);
if(! empty($page_ids))
{
$where .= " OR {$wpdb->posts}.ID IN (" . implode(",", $page_ids) . ")";
}
}
return $where;
}
add_filter('posts_where', 'name_directory_insert_sitewide_search_results');
/**
* Also add our results to Relevanssi
*/
function name_directory_insert_relevanssi_search_results($where)
{
$new_where = name_directory_insert_sitewide_search_results($where);
if(! empty($new_where)) {
global $wpdb;
$new_where = str_replace("OR {$wpdb->posts}.ID IN", " OR doc IN ", $new_where);
}
return $new_where;
}
add_filter('relevanssi_where', 'name_directory_insert_relevanssi_search_results');