PluginProbe
Plugins List / 2.7
Plugins List v2.7
trunk 0.2.1 0.2.2 1.0 2.0 2.1 2.2.7 2.3.2 2.4.4 2.5.2 2.6 2.6.1 2.7
plugins-list / inc / generate-plugin-data.php

generate-plugin-data.php in Plugins List 2.7, at inc/generate-plugin-data.php

582 lines 16.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Shared Functions
4 *
5 * A group of functions shared across my plugins, for consistency.
6 *
7 * @package plugin-slug
8 */
9
10 // Exit if accessed directly.
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 /**
17 * Get Plugins List
18 *
19 * Get list of plugins and, optionally, format them
20 *
21 * @uses pll_format_plugin_list Format the plugin list
22 * @uses pll_get_data Get the plugin data
23 *
24 * @param string $format Requires format.
25 * @param string $show_inactive Whether to format or not.
26 * @param string $show_active Whether to show active or not.
27 * @param string $show_recent Whether to show recenty active or not.
28 * @param string $cache Cache time.
29 * @param string $nofollow Whether to add nofollow to link.
30 * @param string $target Link target.
31 * @param string $by_author Which author.
32 * @param string $characters Maximum characters for description.
33 * @param string $words Maximum words for description.
34 * @param string $emoji True or false, whether to strip emoji from description.
35 * @param string $end When the description is truncated, what to place at the end of the string.
36 * @return string Output.
37 */
38 function pll_get_plugins_list( $format, $show_inactive, $show_active, $show_recent, $cache, $nofollow, $target, $by_author, $characters, $words, $emoji, $end ) {
39
40 // Set default values.
41
42 if ( '' === $format ) {
43 $format = DEFAULT_PLUGIN_LIST_FORMAT;
44 }
45 if ( '' === $show_inactive ) {
46 $show_inactive = 'false';
47 }
48 if ( '' === $show_active ) {
49 $show_active = 'true';
50 }
51 if ( '' === $show_recent ) {
52 $show_recent = 'true';
53 }
54 if ( '' === $cache ) {
55 $cache = 5;
56 }
57 if ( $nofollow ) {
58 $nofollow = ' rel="nofollow"';
59 } else {
60 $nofollow = '';
61 }
62 if ( '' !== $target ) {
63 $target = ' target="' . $target . '"';
64 } else {
65 $target = '';
66 }
67 if ( '' !== $by_author ) {
68 $by_author = 'true';
69 }
70 if ( 'false' === $emoji ) {
71 $emoji = false;
72 } else {
73 $emoji = true;
74 }
75 if ( '' === $end ) {
76 $end = '&#8230;';
77 }
78
79 // Get plugin data.
80
81 $plugins = pll_get_data( $cache );
82
83 // Sort the plugin array if required in author sequence.
84
85 if ( 'true' === $by_author ) {
86 uasort(
87 $plugins,
88 function ( $a, $b ) {
89 return strtoupper( $a['Author'] ) <=> strtoupper( $b['Author'] );
90 }
91 );
92 }
93
94 // Get array of recently activate plugins.
95
96 if ( is_network_admin() ) {
97 $recently_activated = get_site_option( 'recently_activated', array() );
98 } else {
99 $recently_activated = get_option( 'recently_activated', array() );
100 }
101
102 // Extract each plugin and format the output.
103
104 $output = '';
105
106 foreach ( $plugins as $plugin_file => $plugin_data ) {
107
108 // Work out whether plugin is active or not.
109
110 $plugin_active = false;
111 if ( is_plugin_active( $plugin_file ) ) {
112 $plugin_active = true;
113 }
114
115 // Was this recently active?
116
117 $recently_active = false;
118 if ( is_array( $recently_activated ) && array_key_exists( $plugin_file, $recently_activated ) ) {
119 $recently_active = true;
120 }
121
122 // Depending on whhat was requested, format the current plugin for output.
123
124 if ( ( $recently_active && 'true' === $show_recent ) || ( $plugin_active && 'true' === $show_active ) || ( ! $plugin_active && 'true' === $show_inactive ) ) {
125
126 $output .= pll_format_plugin_list( $plugin_data, $format, $nofollow, $target, $characters, $words, $emoji, $end, $plugin_active );
127 }
128 }
129
130 // Return the code, with HTML comments.
131
132 return "\n" . $output . "\n";
133 }
134
135 /**
136 * Get Plugin number
137 *
138 * Get number of plugins
139 *
140 * @since 2.2
141 *
142 * @uses pll_get_data Get the plugin data
143 *
144 * @param string $show_active Whether to include active plugins or not.
145 * @param string $show_inactive Whether to include inactive plugins or not.
146 * @param string $cache Cache time.
147 * @return string Plugin number.
148 */
149 function pll_get_plugin_number( $show_active, $show_inactive, $cache ) {
150
151 // Get plugin data.
152
153 $plugins = pll_get_data( $cache );
154
155 // Get count.
156
157 if ( 'true' === $show_inactive && 'true' === $show_active ) {
158 $output = count( $plugins );
159 } else {
160 $output = 0;
161 foreach( $plugins as $plugin_file => $plugin_data ) { // @codingStandardsIgnoreLine -- require $plugin_data for array values but not doing anything with it
162 if ( is_plugin_active( $plugin_file ) ) {
163 ++$output;
164 }
165 }
166 if ( 'true' === $show_inactive ) {
167 $output = count( $plugins ) - $output; }
168 }
169
170 return $output;
171 }
172
173 /**
174 * Get Plugins data
175 *
176 * Get plugin data and cache it
177 *
178 * @uses pll_format_plugin_list Format the plugin list.
179 *
180 * @param string $cache Cache time.
181 * @return string Plugin data.
182 */
183 function pll_get_data( $cache ) {
184
185 // Attempt to get plugin list from cache.
186
187 if ( ! $cache ) {
188 $cache = 'no'; }
189
190 $plugins = false;
191 $cache_key = 'plugins_list';
192 if ( is_numeric( $cache ) ) {
193 $plugins = get_transient( $cache_key ); }
194
195 // If not using cache, generate a new list and cache that.
196
197 if ( ! $plugins ) {
198 $plugins = get_plugins();
199 if ( ( '' !== $plugins ) && ( is_numeric( $cache ) ) ) {
200 set_transient( $cache_key, $plugins, MINUTE_IN_SECONDS * $cache ); }
201 }
202
203 return $plugins;
204 }
205
206 /**
207 * Format Plugin List
208 *
209 * Format the plugin list
210 *
211 * @uses replace_plugin_list_tags Replace the tags
212 *
213 * @param string $plugin_data The plugin list.
214 * @param string $format Format to use.
215 * @param string $nofollow Nofollow text.
216 * @param string $target Target text.
217 * @param string $characters Maximum characters for description.
218 * @param string $words Maximum words for description.
219 * @param string $emoji True or false, whether to strip emoji from description.
220 * @param string $end When the description is truncated, what to place at the end of the string.
221 * @param string $plugin_active True or false, whether the plugin is active or not.
222 * @return string Output.
223 */
224 function pll_format_plugin_list( $plugin_data, $format, $nofollow, $target, $characters, $words, $emoji, $end, $plugin_active ) {
225
226 // Allowed tag.
227
228 $plugins_allowedtags = array(
229 'a' => array(
230 'href' => array(),
231 'title' => array(),
232 ),
233 'abbr' => array( 'title' => array() ),
234 'acronym' => array( 'title' => array() ),
235 'code' => array(),
236 'em' => array(),
237 'strong' => array(),
238 );
239
240 // Sanitize all displayed data.
241
242 $plugin_data['Title'] = wp_kses( $plugin_data['Title'], $plugins_allowedtags );
243 $plugin_data['PluginURI'] = wp_kses( $plugin_data['PluginURI'], $plugins_allowedtags );
244 $plugin_data['AuthorURI'] = wp_kses( $plugin_data['AuthorURI'], $plugins_allowedtags );
245 $plugin_data['Version'] = wp_kses( $plugin_data['Version'], $plugins_allowedtags );
246 $plugin_data['Author'] = wp_kses( $plugin_data['Author'], $plugins_allowedtags );
247 $plugin_data['RequiresWP'] = wp_kses( $plugin_data['RequiresWP'], $plugins_allowedtags );
248 $plugin_data['RequiresPHP'] = wp_kses( $plugin_data['RequiresPHP'], $plugins_allowedtags );
249
250 // Get plugin activity status.
251
252 if ( $plugin_active ) {
253 $plugin_data['Active'] = __( 'Active', 'plugins-list' );
254 } else {
255 $plugin_data['Active'] = __( 'Inactive', 'plugins-list' );
256 }
257
258 // Strip emoji, HTML and unnecessary space from the description.
259
260 if ( false === $emoji ) {
261 $plugin_data['Description'] = remove_emoji_from_plugin_desc( $plugin_data['Description'] );
262 }
263 $plugin_data['Description'] = strip_spaces_from_plugin_desc( wp_strip_all_tags( $plugin_data['Description'] ) );
264
265 // Truncate the description, if required.
266
267 if ( '' !== $characters || '' !== $words ) {
268
269 // Use WordPress function to truncate description at a set number of words (ellipsis added automatically).
270
271 if ( '' !== $words ) {
272 $word_limited = wp_trim_words( $plugin_data['Description'], $words, $end );
273 $plugin_data['Description'] = $word_limited;
274 }
275
276 // Manually truncate description to a set number of characters. This is done cleanly, however, by doing so to
277 // the previous space. Then an ellipsis is added.
278
279 if ( '' !== $characters ) {
280 $character_limited = $plugin_data['Description'];
281 // Make sure the description is greater than the required length.
282 if ( strlen( $character_limited ) > $characters ) {
283 $space = strrpos( substr( $character_limited, 0, $characters + 1 ), ' ' );
284
285 if ( false === $space ) {
286 // If there is no space before the truncation length, just truncate.
287 $character_limited = substr( $character_limited, 0, $characters );
288 } else {
289 // If there is a space within the truncated area, trim to that.
290 $character_limited = substr( $character_limited, 0, $space );
291 }
292 $plugin_data['Description'] = rtrim( $character_limited ) . $end;
293 }
294 }
295
296 // If both words and character limits are used, take whichever results in the shortest result.
297
298 if ( ( '' !== $characters && '' !== $words ) && ( $word_limited < $character_limited ) ) {
299 $plugin_data['Description'] = $word_limited;
300 }
301 }
302
303 // Replace the tags.
304
305 $format = replace_plugin_list_tags( $plugin_data, $format, $nofollow, $target );
306
307 return $format;
308 }
309
310 /**
311 * Replace tags
312 *
313 * Replace the tags in the provided format
314 *
315 * @param string $plugin_data The plugin list.
316 * @param string $format Format to use.
317 * @param string $nofollow Nofollow text.
318 * @param string $target Target text.
319 * @return string Output.
320 */
321 function replace_plugin_list_tags( $plugin_data, $format, $nofollow, $target ) {
322
323 $format = strtr(
324 $format,
325 array(
326 '{{Title}}' => $plugin_data['Title'],
327 '{{PluginURI}}' => $plugin_data['PluginURI'],
328 '{{AuthorURI}}' => $plugin_data['AuthorURI'],
329 '{{Version}}' => $plugin_data['Version'],
330 '{{Description}}' => $plugin_data['Description'],
331 '{{Author}}' => $plugin_data['Author'],
332 '{{RequiresWP}}' => $plugin_data['RequiresWP'],
333 '{{RequiresPHP}}' => $plugin_data['RequiresPHP'],
334 '{{Active}}' => $plugin_data['Active'],
335 '{{LinkedTitle}}' => "<a href='" . $plugin_data['PluginURI'] . "' title='" . $plugin_data['Title'] . "'" . $nofollow . $target . '>' . $plugin_data['Title'] . '</a>',
336 '{{LinkedAuthor}}' => "<a href='" . $plugin_data['AuthorURI'] . "' title='" . $plugin_data['Author'] . "'" . $nofollow . $target . '>' . $plugin_data['Author'] . '</a>',
337 '#Title#' => $plugin_data['Title'],
338 '#PluginURI#' => $plugin_data['PluginURI'],
339 '#AuthorURI#' => $plugin_data['AuthorURI'],
340 '#Version#' => $plugin_data['Version'],
341 '#Description#' => $plugin_data['Description'],
342 '#Author#' => $plugin_data['Author'],
343 '#RequiresWP#' => $plugin_data['RequiresWP'],
344 '#RequiresPHP#' => $plugin_data['RequiresPHP'],
345 '#Active#' => $plugin_data['Active'],
346 '#LinkedTitle#' => "<a href='" . $plugin_data['PluginURI'] . "' title='" . $plugin_data['Title'] . "'" . $nofollow . $target . '>' . $plugin_data['Title'] . '</a>',
347 '#LinkedAuthor#' => "<a href='" . $plugin_data['AuthorURI'] . "' title='" . $plugin_data['Author'] . "'" . $nofollow . $target . '>' . $plugin_data['Author'] . '</a>',
348 '{{' => '<',
349 '}}' => '>',
350 '{' => '<',
351 '}' => '>',
352 )
353 );
354
355 // Remove all HTML tags other than those used for formatting.
356
357 $allowed_tags = array(
358 'a' => array(
359 'href' => array(),
360 'hreflang' => array(),
361 'rel' => array(),
362 'target' => array(),
363 'type' => array(),
364 'class' => array(),
365 'style' => array(),
366 ),
367 'b' => array(),
368 'big' => array(),
369 'blockquote' => array(
370 'cite' => array(),
371 'class' => array(),
372 'style' => array(),
373 ),
374 'br' => array(),
375 'caption' => array(),
376 'center' => array(),
377 'cite' => array(),
378 'code' => array(
379 'pre' => array(),
380 'class' => array(),
381 'style' => array(),
382 ),
383 'col' => array(
384 'span' => array(),
385 'class' => array(),
386 'style' => array(),
387 ),
388 'colgroup' => array(
389 'span' => array(),
390 'class' => array(),
391 'style' => array(),
392 ),
393 'div' => array(
394 'class' => array(),
395 'style' => array(),
396 ),
397 'em' => array(
398 'class' => array(),
399 'style' => array(),
400 ),
401 'font' => array(
402 'class' => array(),
403 'style' => array(),
404 ),
405 'h1' => array(
406 'class' => array(),
407 'style' => array(),
408 ),
409 'h2' => array(
410 'class' => array(),
411 'style' => array(),
412 ),
413 'h3' => array(
414 'class' => array(),
415 'style' => array(),
416 ),
417 'h4' => array(
418 'class' => array(),
419 'style' => array(),
420 ),
421 'h5' => array(
422 'class' => array(),
423 'style' => array(),
424 ),
425 'h6' => array(
426 'class' => array(),
427 'style' => array(),
428 ),
429 'hr' => array(
430 'class' => array(),
431 'style' => array(),
432 ),
433 'i' => array(
434 'class' => array(),
435 'style' => array(),
436 ),
437 'img' => array(
438 'src' => array(),
439 'alt' => array(),
440 'class' => array(),
441 'style' => array(),
442 ),
443 'li' => array(
444 'class' => array(),
445 'style' => array(),
446 ),
447 'ol' => array(
448 'start' => array(),
449 'type' => array(),
450 'class' => array(),
451 'style' => array(),
452 ),
453 'p' => array(
454 'class' => array(),
455 'style' => array(),
456 ),
457 'pre' => array(
458 'code' => array(),
459 'samp' => array(),
460 'class' => array(),
461 'style' => array(),
462 ),
463 'q' => array(
464 'cite' => array(),
465 'class' => array(),
466 'style' => array(),
467 ),
468 's' => array(
469 'class' => array(),
470 'style' => array(),
471 ),
472 'small' => array(
473 'class' => array(),
474 'style' => array(),
475 ),
476 'span' => array(
477 'class' => array(),
478 'style' => array(),
479 ),
480 'strike' => array(
481 'class' => array(),
482 'style' => array(),
483 ),
484 'strong' => array(
485 'class' => array(),
486 'style' => array(),
487 ),
488 'style' => array(
489 'type' => array(),
490 'class' => array(),
491 'style' => array(),
492 ),
493 'sub' => array(
494 'class' => array(),
495 'style' => array(),
496 ),
497 'sup' => array(
498 'class' => array(),
499 'style' => array(),
500 ),
501 'table' => array(
502 'class' => array(),
503 'style' => array(),
504 ),
505 'td' => array(
506 'colspan' => array(),
507 'headers' => array(),
508 'rowspan' => array(),
509 'class' => array(),
510 'style' => array(),
511 ),
512 'th' => array(
513 'colspan' => array(),
514 'headers' => array(),
515 'rowspan' => array(),
516 'scope' => array(),
517 'class' => array(),
518 'style' => array(),
519 ),
520 'tr' => array(
521 'class' => array(),
522 'style' => array(),
523 ),
524 'u' => array(
525 'class' => array(),
526 'style' => array(),
527 ),
528 'ul' => array(
529 'class' => array(),
530 'style' => array(),
531 ),
532 );
533
534 $format = wp_kses( $format, $allowed_tags );
535
536 return $format;
537 }
538
539 /**
540 * Remove emoji
541 *
542 * Function to strip emoji from the plugin description.
543 *
544 * @param string $description The plugin description.
545 * @return string Stripped description.
546 */
547 function remove_emoji_from_plugin_desc( $description ) {
548
549 $symbols = "\x{1F100}-\x{1F1FF}" // Enclosed Alphanumeric Supplement.
550 . "\x{1F300}-\x{1F5FF}" // Miscellaneous Symbols and Pictographs.
551 . "\x{1F600}-\x{1F64F}" // Emoticons.
552 . "\x{1F680}-\x{1F6FF}" // Transport And Map Symbols.
553 . "\x{1F900}-\x{1F9FF}" // Supplemental Symbols and Pictographs.
554 . "\x{2600}-\x{26FF}" // Miscellaneous Symbols.
555 . "\x{2700}-\x{27BF}"; // Dingbats.
556
557 $description = preg_replace( '/[' . $symbols . ']+/u', '', $description );
558
559 return $description;
560 }
561
562 /**
563 * Strip spaces
564 *
565 * Function to strip extra spaces from the plugin description.
566 *
567 * @param string $description The plugin description.
568 * @return string Stripped description.
569 */
570 function strip_spaces_from_plugin_desc( $description ) {
571
572 $continue = true;
573 while ( true === $continue ) {
574 $replace = str_replace( ' ', ' ', $description );
575 if ( $replace === $description ) {
576 $continue = false;
577 }
578 $description = $replace;
579 }
580 return trim( $description );
581 }
582