| 1 |
<?php |
| 2 |
add_action('wp_enqueue_scripts', 'name_directory_add_stylesheet'); |
| 3 |
|
| 4 |
/** |
| 5 |
* Add the CSS file to output |
| 6 |
*/ |
| 7 |
function name_directory_add_stylesheet() |
| 8 |
{ |
| 9 |
wp_register_style('name-directory-style', plugins_url('name_directory.css', __FILE__)); |
| 10 |
wp_enqueue_style('name-directory-style'); |
| 11 |
} |
| 12 |
|
| 13 |
|
| 14 |
/** |
| 15 |
* Show and handle the submission form |
| 16 |
* @param $directory |
| 17 |
* @param $overview_url |
| 18 |
* @return string |
| 19 |
*/ |
| 20 |
function name_directory_show_submit_form($directory, $overview_url) |
| 21 |
{ |
| 22 |
global $wpdb; |
| 23 |
global $name_directory_table_directory_name; |
| 24 |
|
| 25 |
$directory_info= name_directory_get_directory_properties($directory); |
| 26 |
|
| 27 |
if(empty($directory_info['name_term_singular'])) |
| 28 |
{ |
| 29 |
$name = __('Name', 'name-directory'); |
| 30 |
$back_txt = __('Back to name directory', 'name-directory'); |
| 31 |
$error_empty_txt = __('Please fill in at least a name', 'name-directory'); |
| 32 |
} |
| 33 |
else |
| 34 |
{ |
| 35 |
$name = ucfirst($directory_info['name_term_singular']); |
| 36 |
$back_txt = sprintf(__('Back to %s directory', 'name-directory'), $directory_info['name_term_singular']); |
| 37 |
$error_empty_txt = sprintf(__('Fill in at least a %s', 'name-directory'), $directory_info['name_term_singular']); |
| 38 |
} |
| 39 |
|
| 40 |
$required = __('Required', 'name-directory'); |
| 41 |
$description = __('Description', 'name-directory'); |
| 42 |
$your_name = __('Your name', 'name-directory'); |
| 43 |
$submit = __('Submit', 'name-directory'); |
| 44 |
|
| 45 |
$result_class = ''; |
| 46 |
$form_result = null; |
| 47 |
|
| 48 |
if(! empty($_POST['name_directory_name'])) |
| 49 |
{ |
| 50 |
$wpdb->get_results( |
| 51 |
sprintf("SELECT `id` FROM `%s` WHERE `name` = '%s'", |
| 52 |
$name_directory_table_directory_name, |
| 53 |
esc_sql($_POST['name_directory_name'])) |
| 54 |
); |
| 55 |
|
| 56 |
if($wpdb->num_rows == 1) |
| 57 |
{ |
| 58 |
$result_class = 'form-result-error'; |
| 59 |
$form_result = sprintf(__('Sorry, %s was already on the list so your submission was not sent.', 'name-directory'), |
| 60 |
'<i>' . esc_sql($_POST['name_directory_name']) . '</i>'); |
| 61 |
} |
| 62 |
else |
| 63 |
{ |
| 64 |
$db_success = $wpdb->insert( |
| 65 |
$name_directory_table_directory_name, |
| 66 |
array( |
| 67 |
'directory' => intval($directory), |
| 68 |
'name' => esc_sql($_POST['name_directory_name']), |
| 69 |
'letter' => name_directory_get_first_char($_POST['name_directory_name']), |
| 70 |
'description' => esc_sql($_POST['name_directory_description']), |
| 71 |
'published' => 0, |
| 72 |
'submitted_by' => esc_sql($_POST['name_directory_submitter']), |
| 73 |
), |
| 74 |
array('%d', '%s', '%s', '%s', '%d', '%s') |
| 75 |
); |
| 76 |
|
| 77 |
if(! empty($db_success)) |
| 78 |
{ |
| 79 |
$result_class = 'form-result-success'; |
| 80 |
$form_result = __('Thank you for your submission! It will be reviewed shortly.', 'name-directory'); |
| 81 |
|
| 82 |
name_directory_notify_admin_of_new_submission($directory, $_POST); |
| 83 |
} |
| 84 |
else |
| 85 |
{ |
| 86 |
$result_class = 'form-result-error'; |
| 87 |
$form_result = __('Something must have gone terribly wrong. Would you please try it again?', 'name-directory'); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
else if($_SERVER['REQUEST_METHOD'] == 'POST') |
| 92 |
{ |
| 93 |
$result_class = 'form-result-error'; |
| 94 |
$form_result = $error_empty_txt; |
| 95 |
} |
| 96 |
|
| 97 |
$form = <<<HTML |
| 98 |
<form method='post' name='name_directory_submit'> |
| 99 |
|
| 100 |
<div class='name-directory-form-result {$result_class}'>{$form_result}</div> |
| 101 |
|
| 102 |
<p><a href="{$overview_url}">{$back_txt}</a></p> |
| 103 |
|
| 104 |
<div class='name_directory_forminput'> |
| 105 |
<label for='name_directory_name'>{$name} <small>{$required}</small></label> |
| 106 |
<br /> |
| 107 |
<input id='name_directory_name' type='text' name='name_directory_name' /> |
| 108 |
</div> |
| 109 |
|
| 110 |
<div class='name_directory_forminput'> |
| 111 |
<label for='name_directory_description'>{$description}</label> |
| 112 |
<br /> |
| 113 |
<textarea id='name_directory_description' name='name_directory_description'></textarea> |
| 114 |
</div> |
| 115 |
|
| 116 |
<div class='name_directory_forminput'> |
| 117 |
<label for='name_directory_submitter'>{$your_name}</label> |
| 118 |
<br /> |
| 119 |
<input id='name_directory_submitter' type='text' name='name_directory_submitter' /> |
| 120 |
</div> |
| 121 |
|
| 122 |
<div class='name_directory_forminput'> |
| 123 |
<br /> |
| 124 |
<button type='submit'>{$submit}</button> |
| 125 |
</div> |
| 126 |
|
| 127 |
</form> |
| 128 |
HTML; |
| 129 |
|
| 130 |
return $form; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
/** |
| 135 |
* Function that takes care of displaying.. stuff |
| 136 |
* @param $attributes |
| 137 |
* @return mixed |
| 138 |
*/ |
| 139 |
function name_directory_show_directory($attributes) |
| 140 |
{ |
| 141 |
$dir = null; |
| 142 |
$show_all_link = ''; |
| 143 |
$show_latest_link = ''; |
| 144 |
$jump_location = ''; |
| 145 |
extract(shortcode_atts( |
| 146 |
array('dir' => '1'), |
| 147 |
$attributes |
| 148 |
)); |
| 149 |
|
| 150 |
$name_filter = array(); |
| 151 |
if(! empty($_GET['name_directory_startswith']) && $_GET['name_directory_startswith'] == "latest") |
| 152 |
{ |
| 153 |
$name_filter['character'] = "latest"; |
| 154 |
} |
| 155 |
else if(isset($_GET['name_directory_startswith'])) |
| 156 |
{ |
| 157 |
$name_filter['character'] = mb_strtoupper(mb_substr($_GET['name_directory_startswith'], 0, 1)); |
| 158 |
} |
| 159 |
else if(! empty($attributes['start_with']) && empty($_GET['name-directory-search-value'])) |
| 160 |
{ |
| 161 |
$name_filter['character'] = $attributes['start_with']; |
| 162 |
} |
| 163 |
|
| 164 |
$str_all = __('All', 'name-directory'); |
| 165 |
$str_latest = __('Latest', 'name-directory'); |
| 166 |
$search_value = ''; |
| 167 |
if(! empty($_GET['name-directory-search-value'])) |
| 168 |
{ |
| 169 |
$search_value = htmlspecialchars($_GET['name-directory-search-value']); |
| 170 |
$name_filter['containing'] = $search_value; |
| 171 |
} |
| 172 |
|
| 173 |
$letter_url = name_directory_make_plugin_url('name_directory_startswith', 'name-directory-search-value'); |
| 174 |
$directory = name_directory_get_directory_properties($dir); |
| 175 |
$names = name_directory_get_directory_names($directory, $name_filter); |
| 176 |
$num_names = count($names); |
| 177 |
|
| 178 |
if(isset($_GET['show_submitform'])) |
| 179 |
{ |
| 180 |
return name_directory_show_submit_form($dir, name_directory_make_plugin_url('name_directory_startswith','show_submitform')); |
| 181 |
} |
| 182 |
|
| 183 |
ob_start(); |
| 184 |
|
| 185 |
if(! empty($directory['jump_to_search_results'])) |
| 186 |
{ |
| 187 |
$jump_location = "#name_directory_position"; |
| 188 |
} |
| 189 |
|
| 190 |
echo "<a name='name_directory_position'></a>"; |
| 191 |
|
| 192 |
if(! empty($directory['show_title'])) |
| 193 |
{ |
| 194 |
echo "<h3 class='name_directory_title'>" . $directory['name'] . "</h3>"; |
| 195 |
} |
| 196 |
|
| 197 |
if(! empty($directory['show_all_names_on_index'])) |
| 198 |
{ |
| 199 |
$show_all_link = '<a class="name_directory_startswith" href="' . $letter_url . $jump_location . '">' . $str_all . '</a> |'; |
| 200 |
} |
| 201 |
|
| 202 |
if(! empty($directory['nr_most_recent'])) |
| 203 |
{ |
| 204 |
$show_latest_link = ' <a class="name_directory_startswith" href="' . $letter_url . 'latest' . $jump_location . '">' . $str_latest . '</a> |'; |
| 205 |
} |
| 206 |
|
| 207 |
/* Prepare and print the index-letters */ |
| 208 |
echo '<div class="name_directory_index">'; |
| 209 |
echo $show_all_link; |
| 210 |
echo $show_latest_link; |
| 211 |
|
| 212 |
$index_letters = range('A', 'Z'); |
| 213 |
array_unshift($index_letters, '#'); |
| 214 |
$starting_letters = name_directory_get_directory_start_characters($dir); |
| 215 |
|
| 216 |
/* User does not want to show all the index characters */ |
| 217 |
if(empty($directory['show_all_index_letters'])) |
| 218 |
{ |
| 219 |
$index_letters = $starting_letters; |
| 220 |
} |
| 221 |
|
| 222 |
foreach($index_letters as $index_letter) |
| 223 |
{ |
| 224 |
$extra_classes = ''; |
| 225 |
if(! empty($name_filter['character']) && $name_filter['character'] == $index_letter) |
| 226 |
{ |
| 227 |
$extra_classes .= ' name_directory_active'; |
| 228 |
} |
| 229 |
|
| 230 |
if(! in_array($index_letter, $starting_letters)) |
| 231 |
{ |
| 232 |
$extra_classes .= ' name_directory_empty'; |
| 233 |
} |
| 234 |
|
| 235 |
echo ' <a class="name_directory_startswith ' . $extra_classes . '" href="' . $letter_url . urlencode($index_letter) . $jump_location . '">' . strtoupper($index_letter) . '</a> '; |
| 236 |
} |
| 237 |
|
| 238 |
if(! empty($directory['show_submit_form'])) |
| 239 |
{ |
| 240 |
if(empty($directory['name_term_singular'])) |
| 241 |
{ |
| 242 |
$submit_string = __('Submit a name', 'name-directory'); |
| 243 |
} |
| 244 |
else |
| 245 |
{ |
| 246 |
$submit_string = sprintf(__('Submit a %s', 'name-directory'), $directory['name_term_singular']); |
| 247 |
} |
| 248 |
|
| 249 |
echo " | <a href='" . $letter_url . "&show_submitform=true'>" . $submit_string . "</a>"; |
| 250 |
} |
| 251 |
|
| 252 |
if(! empty($directory['show_search_form'])) |
| 253 |
{ |
| 254 |
$parsed_url = parse_url($_SERVER['REQUEST_URI']); |
| 255 |
$search_get_url = array(); |
| 256 |
if(! empty($parsed_url['query'])) |
| 257 |
{ |
| 258 |
|
| 259 |
parse_str($parsed_url['query'], $search_get_url); |
| 260 |
} |
| 261 |
unset($search_get_url['name-directory-search-value']); |
| 262 |
|
| 263 |
echo "<br />"; |
| 264 |
echo "<form method='get' action='" . $jump_location . "'>"; |
| 265 |
foreach($search_get_url as $key_name=>$value) |
| 266 |
{ |
| 267 |
if($key_name == 'name_directory_startswith') |
| 268 |
{ |
| 269 |
continue; |
| 270 |
} |
| 271 |
echo "<input type='hidden' name='" . htmlspecialchars($key_name) . "' value='" . htmlspecialchars($value) . "' />"; |
| 272 |
} |
| 273 |
echo "<input type='text' name='name-directory-search-value' id='name-directory-search-input-box' placeholder='" . __('Search for...', 'name-directory') . "' />"; |
| 274 |
echo "<input type='submit' id='name-directory-search-input-button' value='" . __('Search', 'name-directory') . "' />"; |
| 275 |
echo "</form>"; |
| 276 |
} |
| 277 |
echo '</div>'; |
| 278 |
|
| 279 |
echo '<div class="name_directory_total">'; |
| 280 |
if(empty($name_filter['character']) && empty($search_value)) |
| 281 |
{ |
| 282 |
if(empty($directory['name_term'])) |
| 283 |
{ |
| 284 |
echo sprintf(__('There are currently %d names in this directory', 'name-directory'), $num_names); |
| 285 |
} |
| 286 |
else |
| 287 |
{ |
| 288 |
echo sprintf(__('There are currently %d %s in this directory', 'name-directory'), $num_names, $directory['name_term']); |
| 289 |
} |
| 290 |
} |
| 291 |
else if(empty($name_filter['character']) && ! empty($search_value)) |
| 292 |
{ |
| 293 |
if(empty($directory['name_term'])) |
| 294 |
{ |
| 295 |
echo sprintf(__('There are %d names in this directory containing the search term %s.', 'name-directory'), $num_names, "<em>" . $search_value . "</em>"); |
| 296 |
} |
| 297 |
else |
| 298 |
{ |
| 299 |
echo sprintf(__('There are %d %s in this directory containing the search term %s.', 'name-directory'), $num_names, $directory['name_term'], "<em>" . $search_value . "</em>"); |
| 300 |
} |
| 301 |
|
| 302 |
echo " <a href='" . get_permalink() . "'><small>" . __('Clear results', 'name-directory') . "</small></a>.<br />"; |
| 303 |
} |
| 304 |
else if($name_filter['character'] == 'latest') |
| 305 |
{ |
| 306 |
if(empty($directory['name_term'])) |
| 307 |
{ |
| 308 |
echo sprintf(__('Showing %d most recent names in this directory', 'name-directory'), $num_names); |
| 309 |
} |
| 310 |
else |
| 311 |
{ |
| 312 |
echo sprintf(__('Showing %d most recent %s in this directory', 'name-directory'), $num_names, $directory['name_term']); |
| 313 |
} |
| 314 |
} |
| 315 |
else |
| 316 |
{ |
| 317 |
if(empty($directory['name_term'])) |
| 318 |
{ |
| 319 |
echo sprintf(__('There are %d names in this directory beginning with the letter %s.', 'name-directory'), $num_names, $name_filter['character']); |
| 320 |
} |
| 321 |
else |
| 322 |
{ |
| 323 |
echo sprintf(__('There are %d %s in this directory beginning with the letter %s.', 'name-directory'), $num_names, $directory['name_term'], $name_filter['character']); |
| 324 |
} |
| 325 |
} |
| 326 |
echo '</div>'; |
| 327 |
|
| 328 |
echo '<div class="name_directory_names">'; |
| 329 |
if($num_names === 0 && empty($search_value)) |
| 330 |
{ |
| 331 |
echo '<p>' . __('There are no entries in this directory at the moment', 'name-directory') . '</p>'; |
| 332 |
} |
| 333 |
else if(isset($directory['show_all_names_on_index']) && $directory['show_all_names_on_index'] != 1 && empty($name_filter)) |
| 334 |
{ |
| 335 |
echo '<p>' . __('Please select a letter from the index (above) to see entries', 'name-directory') . '</p>'; |
| 336 |
} |
| 337 |
else |
| 338 |
{ |
| 339 |
$split_at = null; |
| 340 |
if(! empty($directory['nr_columns']) && $directory['nr_columns'] > 1) |
| 341 |
{ |
| 342 |
$split_at = round($num_names/$directory['nr_columns'])+1; |
| 343 |
} |
| 344 |
|
| 345 |
echo '<div class="name_directory_column name_directory_nr' . (int)$directory['nr_columns'] . '">'; |
| 346 |
|
| 347 |
$i = 1; |
| 348 |
$split_i = 1; |
| 349 |
foreach($names as $entry) |
| 350 |
{ |
| 351 |
echo '<div class="name_directory_name_box">'; |
| 352 |
echo '<a name="namedirectory_' . sanitize_html_class($entry['name']) . '"></a>'; |
| 353 |
echo '<strong>' . htmlspecialchars($entry['name']) . '</strong>'; |
| 354 |
if(! empty($directory['show_description']) && ! empty($entry['description'])) |
| 355 |
{ |
| 356 |
$print_description = html_entity_decode(stripslashes($entry['description'])); |
| 357 |
|
| 358 |
/* This toggles the read more/less indicators, these need extra html */ |
| 359 |
if(! empty($directory['nr_words_description'])) |
| 360 |
{ |
| 361 |
$num_words = intval($directory['nr_words_description']); |
| 362 |
$short_desc = name_directory_get_words($print_description, $num_words); |
| 363 |
$print_description = str_replace($short_desc, "", $print_description); |
| 364 |
if(! empty($print_description)) |
| 365 |
{ |
| 366 |
echo '<br /><div> |
| 367 |
<input type="checkbox" class="name-directory-readmore-state" id="name-' . htmlspecialchars($entry['id']) . '" /> |
| 368 |
<span class="name-directory-readmore-wrap">' . $short_desc . ' <span class="name-directory-readmore-target">' . $print_description .'</span></span> |
| 369 |
<label for="name-' . htmlspecialchars($entry['id']) . '" class="name-directory-readmore-trigger"></label> |
| 370 |
</div>'; |
| 371 |
} |
| 372 |
else |
| 373 |
{ |
| 374 |
echo '<br /><div>' . $short_desc . '</div>'; |
| 375 |
} |
| 376 |
|
| 377 |
} |
| 378 |
else { |
| 379 |
echo '<br /><div>' . $print_description . '</div>'; |
| 380 |
} |
| 381 |
} |
| 382 |
if(! empty($directory['show_submitter_name']) && ! empty($entry['submitted_by'])) |
| 383 |
{ |
| 384 |
echo "<small>" . __('Submitted by:', 'name-directory') . " " . $entry['submitted_by'] . "</small>"; |
| 385 |
} |
| 386 |
echo '</div>'; |
| 387 |
|
| 388 |
if(! empty($directory['show_line_between_names']) && $num_names != $i) |
| 389 |
{ |
| 390 |
echo '<hr />'; |
| 391 |
} |
| 392 |
|
| 393 |
$split_i++; |
| 394 |
$i++; |
| 395 |
|
| 396 |
if($split_at == $split_i) |
| 397 |
{ |
| 398 |
echo '</div><div class="name_directory_column name_directory_nr' . (int)$directory['nr_columns'] . '">'; |
| 399 |
$split_i = 0; |
| 400 |
} |
| 401 |
} |
| 402 |
echo '</div>'; |
| 403 |
} |
| 404 |
echo '</div>'; |
| 405 |
|
| 406 |
if(! empty($directory['nr_columns']) && $directory['nr_columns'] > 1) |
| 407 |
{ |
| 408 |
echo '<div class="name_directory_column_clear"></div>'; |
| 409 |
} |
| 410 |
|
| 411 |
if(! empty($directory['show_submit_form'])) |
| 412 |
{ |
| 413 |
echo "<br /><br /> |
| 414 |
<a href='" . $letter_url . "&show_submitform=true' class='name_directory_submit_bottom_link'>" . $submit_string . "</a>"; |
| 415 |
} |
| 416 |
|
| 417 |
/** Sad to print it like this, but this is needed for translating the show more/less buttons */ |
| 418 |
echo "<style> |
| 419 |
.name-directory-readmore-state ~ .name-directory-readmore-trigger:before { |
| 420 |
content: '… " . __('Show more', 'name-directory') . "'; |
| 421 |
} |
| 422 |
|
| 423 |
.name-directory-readmore-state:checked ~ .name-directory-read-more-trigger:before { |
| 424 |
content: '" . __('Show less', 'name-directory') . "'; |
| 425 |
} |
| 426 |
</style>"; |
| 427 |
|
| 428 |
return ob_get_clean(); |
| 429 |
} |
| 430 |
|
| 431 |
add_shortcode('namedirectory', 'name_directory_show_directory'); |
| 432 |
|
| 433 |
|
| 434 |
/** |
| 435 |
* Display a random name from a given directory |
| 436 |
* Thanks to @mastababa |
| 437 |
* @param $attributes |
| 438 |
* @return mixed |
| 439 |
*/ |
| 440 |
function name_directory_show_random($attributes) { |
| 441 |
$dir = null; |
| 442 |
extract(shortcode_atts( |
| 443 |
array('dir' => '1'), |
| 444 |
$attributes |
| 445 |
)); |
| 446 |
|
| 447 |
$name_filter = array(); |
| 448 |
$directory = name_directory_get_directory_properties($dir); |
| 449 |
$names = name_directory_get_directory_names($directory, $name_filter); |
| 450 |
$num_names = count($names); |
| 451 |
|
| 452 |
if ($num_names > 0) { |
| 453 |
$entry = $names[array_rand($names)]; |
| 454 |
|
| 455 |
ob_start(); |
| 456 |
|
| 457 |
echo '<div class="name_directory_random_name">'; |
| 458 |
echo '<div class="name_directory_name_box">'; |
| 459 |
echo '<a name="namedirectory_' . sanitize_html_class($entry['name']) . '"></a>'; |
| 460 |
echo '<strong>' . htmlspecialchars($entry['name']) . '</strong>'; |
| 461 |
if(! empty($directory['show_description']) && ! empty($entry['description'])) |
| 462 |
{ |
| 463 |
$print_description = html_entity_decode(stripslashes($entry['description'])); |
| 464 |
|
| 465 |
/* This toggles the read more/less indicators, these need extra html */ |
| 466 |
if(! empty($directory['nr_words_description'])) |
| 467 |
{ |
| 468 |
$num_words = intval($directory['nr_words_description']); |
| 469 |
$short_desc = name_directory_get_words($print_description, $num_words); |
| 470 |
$print_description = str_replace($short_desc, "", $print_description); |
| 471 |
if(! empty($print_description)) |
| 472 |
{ |
| 473 |
echo '<br /><div> |
| 474 |
<input type="checkbox" class="name-directory-readmore-state" id="name-' . htmlspecialchars($entry['id']) . '" /> |
| 475 |
<span class="name-directory-readmore-wrap">' . $short_desc . ' <span class="name-directory-readmore-target">' . $print_description .'</span></span> |
| 476 |
<label for="name-' . htmlspecialchars($entry['id']) . '" class="name-directory-readmore-trigger"></label> |
| 477 |
</div>'; |
| 478 |
} |
| 479 |
else |
| 480 |
{ |
| 481 |
echo '<br /><div>' . $short_desc . '</div>'; |
| 482 |
} |
| 483 |
|
| 484 |
} |
| 485 |
else { |
| 486 |
echo '<br /><div>' . $print_description . '</div>'; |
| 487 |
} |
| 488 |
} |
| 489 |
if(! empty($directory['show_submitter_name']) && ! empty($entry['submitted_by'])) |
| 490 |
{ |
| 491 |
echo "<small>" . __('Submitted by:', 'name-directory') . " " . $entry['submitted_by'] . "</small>"; |
| 492 |
} |
| 493 |
echo '</div>'; |
| 494 |
echo '</div>'; |
| 495 |
|
| 496 |
return ob_get_clean(); |
| 497 |
} |
| 498 |
|
| 499 |
} |
| 500 |
add_shortcode('namedirectory_random', 'name_directory_show_random'); |
| 501 |
|
| 502 |
|
| 503 |
/** |
| 504 |
* Search the Name Directory entries whenever necessary |
| 505 |
* Add the search results to the WordPress search results |
| 506 |
*/ |
| 507 |
function name_directory_insert_search_results($where) |
| 508 |
{ |
| 509 |
/* Only perform actions whenever we are on a search page and within the main query */ |
| 510 |
if (is_search()) |
| 511 |
{ |
| 512 |
$name_directory_settings = get_option('name_directory_general_option'); |
| 513 |
|
| 514 |
/* If WordPress search for Name Directory is disabled, just return */ |
| 515 |
if(empty($name_directory_settings) || empty($name_directory_settings['search_on'])) |
| 516 |
{ |
| 517 |
return $where; |
| 518 |
} |
| 519 |
|
| 520 |
$directories = name_directory_get_directory_by_search_query( |
| 521 |
get_search_query(), |
| 522 |
$name_directory_settings['search_description'], |
| 523 |
$name_directory_settings['search_wildcard'] |
| 524 |
); |
| 525 |
|
| 526 |
$page_ids = array(); |
| 527 |
global $wpdb; |
| 528 |
|
| 529 |
/* If directories were found, get the page they are on */ |
| 530 |
foreach($directories as $found => $dir) |
| 531 |
{ |
| 532 |
/* Use a plain sql query, WP_Query unfortunately didn't work (infinite loop) */ |
| 533 |
$host_pages = $wpdb->get_results(" |
| 534 |
SELECT * |
| 535 |
FROM `{$wpdb->prefix}posts` |
| 536 |
WHERE |
| 537 |
`post_type` IN ('page', 'post') |
| 538 |
AND `post_status` = 'publish' |
| 539 |
AND (`post_content` LIKE '%[namedirectory dir=\"{$dir['directory']}%' |
| 540 |
OR `post_content` LIKE '%[namedirectory dir={$dir['directory']}%' |
| 541 |
OR `post_content` LIKE '%[namedirectory dir=''{$dir['directory']}%')"); |
| 542 |
|
| 543 |
if ($host_pages) |
| 544 |
{ |
| 545 |
/* Add the pages to the results that WordPress will present to the user */ |
| 546 |
foreach($host_pages as $host_page) |
| 547 |
{ |
| 548 |
$page_ids[] = $host_page->ID; |
| 549 |
} |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
$page_ids = array_unique($page_ids); |
| 554 |
if(! empty($page_ids)) |
| 555 |
{ |
| 556 |
$where .= " OR {$wpdb->posts}.ID IN (" . implode(",", $page_ids) . ")"; |
| 557 |
} |
| 558 |
|
| 559 |
} |
| 560 |
|
| 561 |
return $where; |
| 562 |
} |
| 563 |
|
| 564 |
add_filter('posts_where', 'name_directory_insert_search_results'); |