| 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 |
* Show and handle the submission form |
| 15 |
* @param $directory |
| 16 |
* @param $overview_url |
| 17 |
* @return string |
| 18 |
*/ |
| 19 |
function name_directory_show_submit_form($directory, $overview_url) |
| 20 |
{ |
| 21 |
global $wpdb; |
| 22 |
global $name_directory_table_directory_name; |
| 23 |
|
| 24 |
$name = __('Name', 'name-directory'); |
| 25 |
$required = __('Required', 'name-directory'); |
| 26 |
$description = __('Description', 'name-directory'); |
| 27 |
$your_name = __('Your name', 'name-directory'); |
| 28 |
$submit = __('Submit', 'name-directory'); |
| 29 |
$back_txt = __('Back to name directory', 'name-directory'); |
| 30 |
|
| 31 |
$result_class = ''; |
| 32 |
$form_result = null; |
| 33 |
|
| 34 |
if(! empty($_POST['name_directory_name'])) |
| 35 |
{ |
| 36 |
$wpdb->get_results( |
| 37 |
sprintf("SELECT `id` FROM `%s` WHERE `name` = '%s'", |
| 38 |
$name_directory_table_directory_name, |
| 39 |
esc_sql($_POST['name_directory_name'])) |
| 40 |
); |
| 41 |
|
| 42 |
if($wpdb->num_rows == 1) |
| 43 |
{ |
| 44 |
$result_class = 'form-result-error'; |
| 45 |
$form_result = sprintf(__('Sorry, %s was already on the list so your submission was not sent.', 'name-directory'), |
| 46 |
'<i>' . esc_sql($_POST['name_directory_name']) . '</i>'); |
| 47 |
} |
| 48 |
else |
| 49 |
{ |
| 50 |
$db_success = $wpdb->insert( |
| 51 |
$name_directory_table_directory_name, |
| 52 |
array( |
| 53 |
'directory' => intval($directory), |
| 54 |
'name' => esc_sql($_POST['name_directory_name']), |
| 55 |
'letter' => name_directory_get_first_char($_POST['name_directory_name']), |
| 56 |
'description' => esc_sql($_POST['name_directory_description']), |
| 57 |
'published' => 0, |
| 58 |
'submitted_by' => esc_sql($_POST['name_directory_submitter']), |
| 59 |
), |
| 60 |
array('%d', '%s', '%s', '%s', '%d', '%s') |
| 61 |
); |
| 62 |
|
| 63 |
if(! empty($db_success)) |
| 64 |
{ |
| 65 |
$result_class = 'form-result-success'; |
| 66 |
$form_result = __('Thank you for your submission! It will be reviewed shortly.', 'name-directory'); |
| 67 |
|
| 68 |
name_directory_notify_admin_of_new_submission($directory, $_POST); |
| 69 |
} |
| 70 |
else |
| 71 |
{ |
| 72 |
$result_class = 'form-result-error'; |
| 73 |
$form_result = __('Something must have gone terribly wrong. Would you please try it again?', 'name-directory'); |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
else if($_SERVER['REQUEST_METHOD'] == 'POST') |
| 78 |
{ |
| 79 |
$result_class = 'form-result-error'; |
| 80 |
$form_result = __('Please fill in at least a name', 'name-directory');; |
| 81 |
} |
| 82 |
|
| 83 |
$form = <<<HTML |
| 84 |
<form method='post' name='name_directory_submit'> |
| 85 |
|
| 86 |
<div class='name-directory-form-result {$result_class}'>{$form_result}</div> |
| 87 |
|
| 88 |
<p><a href="{$overview_url}">{$back_txt}</a></p> |
| 89 |
|
| 90 |
<div class='name_directory_forminput'> |
| 91 |
<label for='name_directory_name'>{$name} <small>{$required}</small></label> |
| 92 |
<br /> |
| 93 |
<input id='name_directory_name' type='text' name='name_directory_name' /> |
| 94 |
</div> |
| 95 |
|
| 96 |
<div class='name_directory_forminput'> |
| 97 |
<label for='name_directory_description'>{$description}</label> |
| 98 |
<br /> |
| 99 |
<textarea id='name_directory_description' name='name_directory_description'></textarea> |
| 100 |
</div> |
| 101 |
|
| 102 |
<div class='name_directory_forminput'> |
| 103 |
<label for='name_directory_submitter'>{$your_name}</label> |
| 104 |
<br /> |
| 105 |
<input id='name_directory_submitter' type='text' name='name_directory_submitter' /> |
| 106 |
</div> |
| 107 |
|
| 108 |
<div class='name_directory_forminput'> |
| 109 |
<br /> |
| 110 |
<button type='submit'>{$submit}</button> |
| 111 |
</div> |
| 112 |
|
| 113 |
</form> |
| 114 |
HTML; |
| 115 |
|
| 116 |
return $form; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
/** |
| 124 |
* Function that takes care of displaying.. stuff |
| 125 |
* @param $attributes |
| 126 |
* @return mixed |
| 127 |
*/ |
| 128 |
function name_directory_show_directory($attributes) |
| 129 |
{ |
| 130 |
$dir = null; |
| 131 |
$show_all_link = ''; |
| 132 |
$show_latest_link = ''; |
| 133 |
extract(shortcode_atts( |
| 134 |
array('dir' => '1'), |
| 135 |
$attributes |
| 136 |
)); |
| 137 |
|
| 138 |
$name_filter = array(); |
| 139 |
if($_GET['name_directory_startswith'] == "latest") |
| 140 |
{ |
| 141 |
$name_filter['character'] = "latest"; |
| 142 |
} |
| 143 |
else if(isset($_GET['name_directory_startswith'])) |
| 144 |
{ |
| 145 |
$cleaned_character_input = preg_replace("/[^A-Z#?!]/", '', strip_tags(strtoupper($_GET['name_directory_startswith']))); |
| 146 |
$name_filter['character'] = $cleaned_character_input[0]; |
| 147 |
} |
| 148 |
else if(! empty($attributes['start_with']) && empty($_GET['name-directory-search-value'])) |
| 149 |
{ |
| 150 |
$name_filter['character'] = $attributes['start_with']; |
| 151 |
} |
| 152 |
|
| 153 |
$str_all = __('All', 'name-directory'); |
| 154 |
$str_latest = __('Latest', 'name-directory'); |
| 155 |
$search_value = ''; |
| 156 |
if(! empty($_GET['name-directory-search-value'])) |
| 157 |
{ |
| 158 |
$search_value = htmlspecialchars($_GET['name-directory-search-value']); |
| 159 |
$name_filter['containing'] = $search_value; |
| 160 |
} |
| 161 |
|
| 162 |
$letter_url = name_directory_make_plugin_url('name_directory_startswith', 'name-directory-search-value'); |
| 163 |
$directory = name_directory_get_directory_properties($dir); |
| 164 |
$names = name_directory_get_directory_names($directory, $name_filter); |
| 165 |
$num_names = count($names); |
| 166 |
|
| 167 |
if(isset($_GET['show_submitform'])) |
| 168 |
{ |
| 169 |
return name_directory_show_submit_form($dir, name_directory_make_plugin_url('name_directory_startswith','show_submitform')); |
| 170 |
} |
| 171 |
|
| 172 |
ob_start(); |
| 173 |
|
| 174 |
echo "<a name='name_directory_position'></a>"; |
| 175 |
|
| 176 |
if(! empty($directory['show_title'])) |
| 177 |
{ |
| 178 |
echo "<h3 class='name_directory_title'>" . $directory['name'] . "</h3>"; |
| 179 |
} |
| 180 |
|
| 181 |
if(! empty($directory['show_all_names_on_index'])) |
| 182 |
{ |
| 183 |
$show_all_link = '<a class="name_directory_startswith" href="' . $letter_url . '">' . $str_all . '</a> |'; |
| 184 |
} |
| 185 |
|
| 186 |
if(! empty($directory['nr_most_recent'])) |
| 187 |
{ |
| 188 |
$show_latest_link = ' <a class="name_directory_startswith" href="' . $letter_url . 'latest">' . $str_latest . '</a> |'; |
| 189 |
} |
| 190 |
|
| 191 |
/* Prepare and print the index-letters */ |
| 192 |
echo '<div class="name_directory_index">'; |
| 193 |
echo $show_all_link; |
| 194 |
echo $show_latest_link; |
| 195 |
|
| 196 |
$index_letters = range('A', 'Z'); |
| 197 |
array_unshift($index_letters, '#'); |
| 198 |
$starting_letters = name_directory_get_directory_start_characters($dir); |
| 199 |
|
| 200 |
/* User does not want to show all the index characters */ |
| 201 |
if(empty($directory['show_all_index_letters'])) |
| 202 |
{ |
| 203 |
$index_letters = $starting_letters; |
| 204 |
} |
| 205 |
|
| 206 |
foreach($index_letters as $index_letter) |
| 207 |
{ |
| 208 |
$extra_classes = ''; |
| 209 |
if($name_filter['character'] == $index_letter) |
| 210 |
{ |
| 211 |
$extra_classes .= ' name_directory_active'; |
| 212 |
} |
| 213 |
|
| 214 |
if(! in_array($index_letter, $starting_letters)) |
| 215 |
{ |
| 216 |
$extra_classes .= ' name_directory_empty'; |
| 217 |
} |
| 218 |
|
| 219 |
echo ' <a class="name_directory_startswith ' . $extra_classes . '" href="' . $letter_url . urlencode($index_letter) . '">' . strtoupper($index_letter) . '</a> '; |
| 220 |
} |
| 221 |
|
| 222 |
if(! empty($directory['show_submit_form'])) |
| 223 |
{ |
| 224 |
echo " | <a href='" . $letter_url . "&show_submitform=true'>" . __('Submit a name', 'name-directory') . "</a>"; |
| 225 |
} |
| 226 |
|
| 227 |
if(! empty($directory['show_search_form'])) |
| 228 |
{ |
| 229 |
$parsed_url = parse_url($_SERVER['REQUEST_URI']); |
| 230 |
parse_str($parsed_url['query'], $search_get_url); |
| 231 |
unset($search_get_url['name-directory-search-value']); |
| 232 |
|
| 233 |
echo "<br />"; |
| 234 |
echo "<form method='get' action='"; |
| 235 |
if(! empty($directory['jump_to_search_results'])) |
| 236 |
{ |
| 237 |
echo "#name_directory_position"; |
| 238 |
} |
| 239 |
echo "'>"; |
| 240 |
foreach($search_get_url as $key_name=>$value) |
| 241 |
{ |
| 242 |
if($key_name == 'name_directory_startswith') |
| 243 |
{ |
| 244 |
continue; |
| 245 |
} |
| 246 |
echo "<input type='hidden' name='" . htmlspecialchars($key_name) . "' value='" . htmlspecialchars($value) . "' />"; |
| 247 |
} |
| 248 |
echo "<input type='text' name='name-directory-search-value' id='name-directory-search-input-box' placeholder='" . __('Search for...', 'name-directory') . "' />"; |
| 249 |
echo "<input type='submit' id='name-directory-search-input-button' value='" . __('Search', 'name-directory') . "' />"; |
| 250 |
echo "</form>"; |
| 251 |
} |
| 252 |
echo '</div>'; |
| 253 |
|
| 254 |
echo '<div class="name_directory_total">'; |
| 255 |
if(empty($name_filter['character']) && empty($search_value)) |
| 256 |
{ |
| 257 |
echo sprintf(__('There are currently %d names in this directory', 'name-directory'), $num_names); |
| 258 |
} |
| 259 |
else if(empty($name_filter['character']) && ! empty($search_value)) |
| 260 |
{ |
| 261 |
echo sprintf(__('There are %d names in this directory containing the search term %s.', 'name-directory'), $num_names, "<i>" . $search_value . "</i>"); |
| 262 |
echo " <a href='" . get_permalink() . "'><small>" . __('Clear results', 'name-directory') . "</small></a>.<br />"; |
| 263 |
} |
| 264 |
else if($name_filter['character'] == 'latest') |
| 265 |
{ |
| 266 |
echo sprintf(__('Showing %d most recent names in this directory', 'name-directory'), $num_names); |
| 267 |
} |
| 268 |
else |
| 269 |
{ |
| 270 |
echo sprintf(__('There are %d names in this directory beginning with the letter %s.', 'name-directory'), $num_names, $name_filter['character']); |
| 271 |
} |
| 272 |
echo '</div>'; |
| 273 |
|
| 274 |
echo '<div class="name_directory_names">'; |
| 275 |
if($num_names === 0 && empty($search_value)) |
| 276 |
{ |
| 277 |
echo '<p>' . __('There are no names in this directory at the moment', 'name-directory') . '</p>'; |
| 278 |
} |
| 279 |
else if(isset($directory['show_all_names_on_index']) && $directory['show_all_names_on_index'] != 1 && empty($name_filter)) |
| 280 |
{ |
| 281 |
echo '<p>' . __('Please select a letter from the index (above) to see entries', 'name-directory') . '</p>'; |
| 282 |
} |
| 283 |
else |
| 284 |
{ |
| 285 |
$split_at = null; |
| 286 |
if(! empty($directory['nr_columns']) && $directory['nr_columns'] > 1) |
| 287 |
{ |
| 288 |
$split_at = round($num_names/$directory['nr_columns'])+1; |
| 289 |
} |
| 290 |
|
| 291 |
echo '<div class="name_directory_column name_directory_nr' . (int)$directory['nr_columns'] . '">'; |
| 292 |
|
| 293 |
$i = 1; |
| 294 |
$split_i = 1; |
| 295 |
foreach($names as $entry) |
| 296 |
{ |
| 297 |
echo '<div class="name_directory_name_box">'; |
| 298 |
echo '<a name="namedirectory_' . sanitize_html_class($entry['name']) . '"></a>'; |
| 299 |
echo '<strong>' . htmlspecialchars($entry['name']) . '</strong>'; |
| 300 |
if(! empty($directory['show_description']) && ! empty($entry['description'])) |
| 301 |
{ |
| 302 |
$print_description = html_entity_decode(stripslashes($entry['description'])); |
| 303 |
|
| 304 |
/* This toggles the read more/less indicators, these need extra html */ |
| 305 |
if(! empty($directory['nr_words_description'])) |
| 306 |
{ |
| 307 |
$num_words = intval($directory['nr_words_description']); |
| 308 |
$short_desc = name_directory_get_words($print_description, $num_words); |
| 309 |
$print_description = str_replace($short_desc, "", $print_description); |
| 310 |
if(! empty($print_description)) |
| 311 |
{ |
| 312 |
echo '<br /><div> |
| 313 |
<input type="checkbox" class="name-directory-readmore-state" id="name-' . htmlspecialchars($entry['id']) . '" /> |
| 314 |
<span class="name-directory-readmore-wrap">' . $short_desc . ' <span class="name-directory-readmore-target">' . $print_description .'</span></span> |
| 315 |
<label for="name-' . htmlspecialchars($entry['id']) . '" class="name-directory-readmore-trigger"></label> |
| 316 |
</div>'; |
| 317 |
} |
| 318 |
else |
| 319 |
{ |
| 320 |
echo '<br /><div>' . $short_desc . '</div>'; |
| 321 |
} |
| 322 |
|
| 323 |
} |
| 324 |
else { |
| 325 |
echo '<br /><div>' . $print_description . '</div>'; |
| 326 |
} |
| 327 |
} |
| 328 |
if(! empty($directory['show_submitter_name']) && ! empty($entry['submitted_by'])) |
| 329 |
{ |
| 330 |
echo "<small>" . __('Submitted by:', 'name-directory') . " " . $entry['submitted_by'] . "</small>"; |
| 331 |
} |
| 332 |
echo '</div>'; |
| 333 |
|
| 334 |
if(! empty($directory['show_line_between_names']) && $num_names != $i) |
| 335 |
{ |
| 336 |
echo '<hr />'; |
| 337 |
} |
| 338 |
|
| 339 |
$split_i++; |
| 340 |
$i++; |
| 341 |
|
| 342 |
if($split_at == $split_i) |
| 343 |
{ |
| 344 |
echo '</div><div class="name_directory_column name_directory_nr' . (int)$directory['nr_columns'] . '">'; |
| 345 |
$split_i = 0; |
| 346 |
} |
| 347 |
} |
| 348 |
echo '</div>'; |
| 349 |
} |
| 350 |
echo '</div>'; |
| 351 |
|
| 352 |
if(! empty($directory['nr_columns']) && $directory['nr_columns'] > 1) |
| 353 |
{ |
| 354 |
echo '<div class="name_directory_column_clear"></div>'; |
| 355 |
} |
| 356 |
|
| 357 |
if(! empty($directory['show_submit_form'])) |
| 358 |
{ |
| 359 |
echo "<br /><br /> |
| 360 |
<a href='" . $letter_url . "&show_submitform=true' class='name_directory_submit_bottom_link'>" . __('Submit a name', 'name-directory') . "</a>"; |
| 361 |
} |
| 362 |
|
| 363 |
/** Sad to print it like this, but this is needed for translating the show more/less buttons */ |
| 364 |
echo "<style> |
| 365 |
.name-directory-readmore-state ~ .name-directory-readmore-trigger:before { |
| 366 |
content: '… " . __('Show more', 'name-directory') . "'; |
| 367 |
} |
| 368 |
|
| 369 |
.name-directory-readmore-state:checked ~ .name-directory-read-more-trigger:before { |
| 370 |
content: '" . __('Show less', 'name-directory') . "'; |
| 371 |
} |
| 372 |
</style>"; |
| 373 |
|
| 374 |
return ob_get_clean(); |
| 375 |
} |
| 376 |
|
| 377 |
add_shortcode('namedirectory', 'name_directory_show_directory'); |
| 378 |
|