PluginProbe
Name Directory / 1.12
Name Directory v1.12
1.34.1 1.34.0 trunk 1.10 1.11 1.11.1 1.11.2 1.11.3 1.11.4 1.11.5 1.11.6 1.12 1.13 1.13.1 1.13.2 1.13.3 1.13.4 1.13.5 1.13.6 1.13.7 1.14 1.14.1 1.14.2 1.15 1.15.1 All 91 releases
name-directory / helpers.php

helpers.php in Name Directory 1.12, at helpers.php

387 lines 11.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * This file is part of the NameDirectory plugin for WordPress
4 */
5
6
7
8 /**
9 * Return the first character of a word,
10 * or hashtag, may the word begin with a number
11 * @param $name
12 * @return string
13 */
14 function name_directory_get_first_char($name)
15 {
16 $first_char = mb_strtoupper(mb_substr($name, 0, 1));
17 if(is_numeric($first_char))
18 {
19 $first_char = '#';
20 }
21
22 return $first_char;
23 }
24
25
26 /**
27 * Prepare an associative array to be used for the csv importer
28 * @param array $row (csv-row)
29 * @param int $published (optional)
30 * @return array|bool
31 */
32 function name_directory_prepared_import_row($row, $published=1)
33 {
34 // Don't continue when there is no name to add (first column in csv-row)
35 if(empty($row[0]))
36 {
37 return false;
38 }
39
40 $row_props = array('name', 'description', 'submitted_by');
41 $prepared_row = array('published' => $published);
42 foreach($row_props as $index=>$prop)
43 {
44 if(! empty($row[$index]))
45 {
46 $prepared_row[$prop] = $row[$index];
47 }
48 }
49
50 return $prepared_row;
51 }
52
53
54 /**
55 * Return localized yes or no based on a variable
56 * @param $var
57 * @return string
58 */
59 function name_directory_yesno($var)
60 {
61 if(! empty($var))
62 {
63 return __('Yes', 'name-directory');
64 }
65
66 return __('No', 'name-directory');
67 }
68
69
70 /**
71 * Switches the published state of a name and returns the human readable value
72 * @param (numeric) $name_id
73 * @return string
74 */
75 function name_directory_switch_name_published_status($name_id)
76 {
77 global $wpdb;
78 global $name_directory_table_directory_name;
79
80 $wpdb->query($wpdb->prepare("UPDATE `%s` SET `published`=1 XOR `published` WHERE id=%d",
81 esc_sql($name_directory_table_directory_name),
82 intval($name_id)));
83 sleep(0.1);
84
85 return name_directory_yesno($wpdb->get_var(sprintf("SELECT `published` FROM `%s` WHERE id=%d",
86 $name_directory_table_directory_name, intval($name_id))));
87 }
88
89
90 /**
91 * Check if a given name already exists in a Name Directory
92 * @param $name
93 * @param $directory
94 * @return bool
95 */
96 function name_directory_name_exists_in_directory($name, $directory)
97 {
98 global $wpdb;
99 global $name_directory_table_directory_name;
100
101 $wpdb->get_results(sprintf("SELECT 1 FROM `%s` WHERE `name` = '%s' AND `directory` = %d",
102 $name_directory_table_directory_name, esc_sql($name), intval($directory)));
103
104 return (bool)$wpdb->num_rows;
105 }
106
107
108 /**
109 * Construct a plugin URL
110 * @param string $index
111 * @param null $exclude
112 * @return string
113 */
114 function name_directory_make_plugin_url($index = 'name_directory_startswith', $exclude = null)
115 {
116 $url = array();
117 $parsed = parse_url($_SERVER['REQUEST_URI']);
118 if(! empty($parsed['query']))
119 {
120 parse_str($parsed['query'], $url);
121 }
122
123 if(! empty($exclude))
124 {
125 unset($url[$exclude]);
126 }
127
128 unset($url[$index]);
129 unset($url['page_id']);
130 $paste_char = '?';
131 if(strpos(get_permalink(), '?') !== false)
132 {
133 $paste_char = '&';
134 }
135 $url[$index] = '';
136
137 return get_permalink() . $paste_char . http_build_query($url);
138 }
139
140
141 /**
142 * Get the names of given directory, maybe only with the char?
143 * @param $directory
144 * @param array $name_filter
145 * @return mixed
146 */
147 function name_directory_get_directory_names($directory, $name_filter = array())
148 {
149 global $wpdb;
150 global $name_directory_table_directory_name;
151 $sql_filter = "";
152 $limit = "";
153 $order_by = "ORDER BY `letter`, `name` ASC";
154
155 if(! empty($name_filter['character']))
156 {
157 $sql_filter .= " AND `letter`='" . $name_filter['character'] . "' ";
158 }
159
160 if(! empty($directory['show_description']) && ! empty($name_filter['containing']))
161 {
162 $sql_filter .= " AND (`name` LIKE '%" . $name_filter['containing'] . "%' OR `description` LIKE '%" . $name_filter['containing'] . "%') ";
163 }
164 elseif(! empty($name_filter['containing']))
165 {
166 $sql_filter .= " AND `name` LIKE '%" . $name_filter['containing'] . "%' ";
167 }
168
169 if(! empty($name_filter['character']) && $name_filter['character'] == 'latest')
170 {
171 $sql_filter = "";
172 $order_by = "ORDER BY `id` DESC";
173 $limit = " LIMIT " . $directory['nr_most_recent'];
174 }
175
176 $names = $wpdb->get_results(sprintf("
177 SELECT *
178 FROM %s
179 WHERE `directory` = %d AND `published` = 1
180 %s
181 %s %s",
182 esc_sql($name_directory_table_directory_name),
183 esc_sql($directory['id']),
184 $sql_filter,
185 $order_by,
186 $limit),
187 ARRAY_A
188 );
189
190 return $names;
191 }
192
193 /**
194 * Get directory by search-query
195 * @param $search_query
196 * @param bool $include_description
197 * @param bool $wildcard
198 *
199 * @return array|null|object
200 */
201 function name_directory_get_directory_by_search_query($search_query, $include_description = false, $wildcard = false)
202 {
203 global $wpdb;
204 global $name_directory_table_directory_name;
205
206 $sql_filter = "";
207
208 if(! empty($wildcard))
209 {
210 $search_query = "%" . $search_query . "%";
211 }
212
213 if(empty($include_description))
214 {
215 $sql_filter = " AND (`name` LIKE '" . $search_query . "') ";
216 }
217 else
218 {
219 $sql_filter = " AND (`name` LIKE '" . $search_query . "' OR `description` LIKE '" . $search_query . "') ";
220 }
221
222 $directories = $wpdb->get_results(sprintf("
223 SELECT DISTINCT `directory`
224 FROM %s
225 WHERE `published` = 1
226 %s",
227 esc_sql($name_directory_table_directory_name),
228 $sql_filter),
229 ARRAY_A
230 );
231
232 return $directories;
233 }
234
235 /**
236 * Get the directory with the supplied ID
237 * @param $id
238 * @return mixed
239 */
240 function name_directory_get_directory_properties($id)
241 {
242 global $wpdb;
243 global $name_directory_table_directory;
244
245 $directory = $wpdb->get_row(sprintf("SELECT * FROM %s WHERE `id` = %d",
246 esc_sql($name_directory_table_directory),
247 esc_sql($id)), ARRAY_A);
248
249 return $directory;
250 }
251
252
253 /**
254 * Get names in a specified directory (specified by ID)
255 * @param $id
256 * @return mixed
257 */
258 function name_directory_get_directory_start_characters($id)
259 {
260 global $wpdb;
261 global $name_directory_table_directory_name;
262
263 $characters = $wpdb->get_col(sprintf("SELECT DISTINCT `letter` FROM %s WHERE `directory` = %d ORDER BY `letter` ASC",
264 esc_sql($name_directory_table_directory_name),
265 esc_sql($id)));
266
267 return array_values($characters);
268 }
269
270
271 /**
272 * Send an email to the WordPress admin e-mailaddress
273 * Notify the admin that a new name has been submitted to the directory and
274 * that this name has to be reviewed first before publishing
275 */
276 function name_directory_notify_admin_of_new_submission($directory, $input)
277 {
278 $admin_email = get_option('admin_email');
279 wp_mail($admin_email,
280 __('New submission for Name Directory', 'name-directory'),
281 __('Howdy,', 'name-directory') . "\n\n" .
282 sprintf(__('There was a new submission to the Name Directory on %s at %s', 'name-directory'), get_option('blogname'), get_option('home')) . "\n\n" .
283 sprintf("%s: %s", __('Name', 'name-directory'), $input['name_directory_name']) . "\n" .
284 sprintf("%s: %s", __('Description', 'name-directory'), $input['name_directory_description']) . "\n" .
285 sprintf("%s: %s", __('Submitted by', 'name-directory'), $input['name_directory_submitter']) . "\n\n" .
286 __('This new submission does not have the published status.', 'name-directory') . ' ' .
287 __('Please login to your WordPress admin to review and accept the submission.', 'name-directory') . "\n\n" .
288 sprintf("Link: %s/wp-admin/admin.php?page=name-directory&sub=manage-directory&dir=%d&status=unpublished", get_option('home'), $directory) . "\n\n" .
289 sprintf("Your %s WordPress site", get_option('blogname')));
290 }
291
292
293 /**
294 * Get the first X words of the sent description
295 * @param $description
296 * @param int $words
297 * @return string
298 */
299 function name_directory_get_words($description, $words = 10)
300 {
301 preg_match("/(?:\w+(?:\W+|$)){0,$words}/", $description, $matches);
302 return $matches[0];
303 }
304
305
306 /**
307 * Helper to render an admin options row with just a Yes/No question
308 * @param $directory
309 * @param $setting_name
310 * @param $setting_friendly_name
311 * @param $setting_description
312 */
313 function name_directory_render_admin_setting_boolean($directory, $setting_name, $setting_friendly_name, $setting_description)
314 {
315 echo '<tr>
316 <td>' . $setting_friendly_name . (empty($setting_description) ? '' : '<br><small>' . $setting_description . '</small>') . '</td>
317 <td>
318 <label for="' . $setting_name . '_yes">
319 <input type="radio" name="' . $setting_name . '" id="' . $setting_name . '_yes" value="1" checked="checked" />
320 &nbsp;' . __('Yes', 'name-directory') . '
321 </label>
322
323 &nbsp; &nbsp;
324
325 <label for="' . $setting_name . '_no">
326 <input type="radio" name="' . $setting_name . '" id="' . $setting_name . '_no" value="0"
327 ' . (empty($directory[$setting_name]) ? 'checked="checked"' : '') . '>' .
328 ' &nbsp; ' . __('No', 'name-directory') . '
329 </label>
330 </td>
331 </tr>';
332 }
333
334
335 /**
336 * Helper to render an admin options row with multiple choice options
337 * @param $directory
338 * @param $setting_name
339 * @param $setting_friendly_name
340 * @param $setting_description
341 * @param $options
342 */
343 function name_directory_render_admin_setting_options($directory, $setting_name, $setting_friendly_name, $setting_description, $options)
344 {
345 echo '<tr>
346 <td>' . $setting_friendly_name . (empty($setting_description) ? '' : '<br><small>' . $setting_description . '</small>') . '</td>
347 <td>
348 <select name="' . $setting_name . '">';
349 foreach($options as $option => $name)
350 {
351 $selected = null;
352 if(! empty($directory[$setting_name]) && $option == $directory[$setting_name])
353 {
354 $selected = " selected";
355 }
356 echo '<option value="' . $option . '" ' . $selected . '>' . $name . '</option>';
357 }
358 echo ' </select>
359 </td>
360 </tr>';
361 }
362
363 /**
364 * Render header and footer of the overview-table
365 */
366 function name_directory_render_admin_overview_table_headerfooter()
367 {
368 echo '<tr>
369 <th width="1%" scope="col" class="manage-column column-cb check-column">&nbsp;</th>
370 <th width="52%" scope="col" id="title" class="manage-column column-title sortable desc">
371 <span>' . __('Title', 'name-directory') . '</span>
372 </th>
373 <th width="13%" scope="col">' . __('Entries', 'name-directory') . '</th>
374 <th width="13%" scope="col">' . __('Published', 'name-directory') . '</th>
375 <th width="13%" scope="col">' . __('Unpublished', 'name-directory') . '</th>
376 </tr>';
377 }
378
379 /**
380 * Display a message after updating, let people know there is a new menu entry
381 */
382 function name_directory_after_update_menu_notice()
383 {
384 echo '<div class="notice notice-info is-dismissible"><p><strong>' . __('Name Directory has been updated.', 'name-directory') . '</strong></p>
385 <p>' . __('You can now find Name Directory directly in your Admin menu!', 'name-directory') . '</p></div>';
386 }
387