diff --git a/README.md b/README.md index 27adc2e..bc65368 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Plugins List -This is a simple Wordpress plugin aimed at giving credit where credit is due. +This is a simple community Wordpress plugin aimed at giving credit where credit is due. The plugin inserts an XHTML list into any post/page through a shortcode. If you're into customization, you can specify a format argument and indicate the exact output you are after. There's also an option to display inactive plugins as well. @@ -10,6 +10,7 @@ Features include... * Template tags are available for automatically linked items as well as simple text * Choose from a number of pieces of plugin data to be output * Display inactive plugins as well as active plugins if you wish +* Automatically limit long descriptions to specific lengths, and even remove emojis from the output * Output is cached to provide a super-quick response * A separate shortcode allows you to display how many plugins you have! diff --git a/plugins-list.php b/plugins-list.php index 5a90bb7..1977c6f 100755 --- a/plugins-list.php +++ b/plugins-list.php @@ -1,14 +1,29 @@ {{LinkedTitle}} by {{LinkedAuthor}}.' ); @@ -64,11 +79,15 @@ function plugins_list_shortcode( $paras ) { 'nofollow' => '', 'target' => '', 'by_author' => '', + 'chars' => '', + 'words' => '', + 'emoji' => '', + 'end' => '', ), $paras ); - $output = get_plugins_list( $atts['format'], $atts['show_inactive'], $atts['show_active'], $atts['cache'], $atts['nofollow'], $atts['target'], $atts['by_author'] ); + $output = get_plugins_list( $atts['format'], $atts['show_inactive'], $atts['show_active'], $atts['cache'], $atts['nofollow'], $atts['target'], $atts['by_author'], $atts['chars'], $atts['words'], $atts['emoji'], $atts['end'] ); return $output; } @@ -118,9 +137,13 @@ function plugin_number_shortcode( $paras ) { * @param string $nofollow Whether to add nofollow to link. * @param string $target Link target. * @param string $by_author Which author. + * @param string $characters Maximum characters for description. + * @param string $words Maximum words for description. + * @param string $emoji True or false, whether to strip emoji from description. + * @param string $end When the description is truncated, what to place at the end of the string. * @return string Output. */ -function get_plugins_list( $format, $show_inactive, $show_active, $cache, $nofollow, $target, $by_author ) { +function get_plugins_list( $format, $show_inactive, $show_active, $cache, $nofollow, $target, $by_author, $characters, $words, $emoji, $end ) { // Set default values. @@ -149,6 +172,14 @@ function get_plugins_list( $format, $show_inactive, $show_active, $cache, $nofol if ( '' !== $by_author ) { $by_author = 'true'; } + if ( 'false' == $emoji ) { + $emoji = false; + } else { + $emoji = true; + } + if ( '' == $end ) { + $end = '…'; + } // Get plugin data. @@ -173,7 +204,7 @@ function( $a, $b ) { if ( ( is_plugin_active( $plugin_file ) && 'true' === $show_active ) || ( ! is_plugin_active( $plugin_file ) && 'true' === $show_inactive ) ) { - $output .= format_plugin_list( $plugin_data, $format, $nofollow, $target ); + $output .= format_plugin_list( $plugin_data, $format, $nofollow, $target, $characters, $words, $emoji, $end ); } } @@ -265,9 +296,13 @@ function get_plugin_list_data( $cache ) { * @param string $format Format to use. * @param string $nofollow Nofollow text. * @param string $target Target text. + * @param string $characters Maximum characters for description. + * @param string $words Maximum words for description. + * @param string $emoji True or false, whether to strip emoji from description. + * @param string $end When the description is truncated, what to place at the end of the string. * @return string Output. */ -function format_plugin_list( $plugin_data, $format, $nofollow, $target ) { +function format_plugin_list( $plugin_data, $format, $nofollow, $target, $characters, $words, $emoji, $end ) { // Allowed tag. @@ -291,6 +326,50 @@ function format_plugin_list( $plugin_data, $format, $nofollow, $target ) { $plugin_data['Version'] = wp_kses( $plugin_data['Version'], $plugins_allowedtags ); $plugin_data['Author'] = wp_kses( $plugin_data['Author'], $plugins_allowedtags ); + // Strip emoji, HTML and unnecessary space from the description. + if ( false == $emoji ) { + $plugin_data['Description'] = remove_emoji_from_plugin_desc( $plugin_data['Description'] ); + } + $plugin_data['Description'] = strip_spaces_from_plugin_desc( wp_strip_all_tags( $plugin_data['Description'] ) ); + + // Truncate the description, if required. + + if ( '' != $characters || '' != $words ) { + + // Use WordPress function to truncate description at a set number of words (ellipsis added automatically). + + if ( '' != $words ) { + $word_limited = wp_trim_words( $plugin_data['Description'], $words, $end ); + $plugin_data['Description'] = $word_limited; + } + + // Manually truncate description to a set number of characters. This is done cleanly, however, by doing so to + // the previous space. Then an ellipsis is added. + + if ( '' != $characters ) { + $character_limited = $plugin_data['Description']; + // Make sure the description is greater than the required length. + if ( strlen( $character_limited ) > $characters ) { + $space = strrpos( substr( $character_limited, 0, $characters + 1 ), ' ' ); + + if ( false == $space ) { + // If there is no space before the truncation length, just truncate. + $character_limited = substr( $character_limited, 0, $characters ); + } else { + // If there is a space within the truncated area, trim to that. + $character_limited = substr( $character_limited, 0, $space ); + } + $plugin_data['Description'] = rtrim( $character_limited ) . $end; + } + } + + // If both words and character limits are used, take whichever results in the shortest result. + + if ( ( '' != $characters && '' != $words ) && ( $word_limited < $character_limited ) ) { + $plugin_data['Description'] = $word_limited; + } + } + // Replace the tags. $format = replace_plugin_list_tags( $plugin_data, $format, $nofollow, $target ); @@ -339,3 +418,47 @@ function replace_plugin_list_tags( $plugin_data, $format, $nofollow, $target ) { return $format; } + +/** + * Remove emoji + * + * Function to strip emoji from the plugin description. + * + * @param string $description The plugin description. + * @return string Stripped description. + */ +function remove_emoji_from_plugin_desc( $description ) { + + $symbols = "\x{1F100}-\x{1F1FF}" // Enclosed Alphanumeric Supplement. + . "\x{1F300}-\x{1F5FF}" // Miscellaneous Symbols and Pictographs. + . "\x{1F600}-\x{1F64F}" // Emoticons. + . "\x{1F680}-\x{1F6FF}" // Transport And Map Symbols. + . "\x{1F900}-\x{1F9FF}" // Supplemental Symbols and Pictographs. + . "\x{2600}-\x{26FF}" // Miscellaneous Symbols. + . "\x{2700}-\x{27BF}"; // Dingbats. + + $description = preg_replace( '/[' . $symbols . ']+/u', '', $description ); + + return $description; +} + +/** + * Strip spaces + * + * Function to strip extra spaces from the plugin description. + * + * @param string $description The plugin description. + * @return string Stripped description. + */ +function strip_spaces_from_plugin_desc( $description ) { + + $continue = true; + while ( true === $continue ) { + $replace = str_replace( ' ', ' ', $description ); + if ( $replace == $description ) { + $continue = false; + } + $description = $replace; + } + return trim( $description ); +} diff --git a/readme.txt b/readme.txt index da1ac0f..3867d77 100755 --- a/readme.txt +++ b/readme.txt @@ -5,7 +5,7 @@ Tags: plugin, list, show, installed, display Requires at least: 4.6 Tested up to: 6.1 Requires PHP: 7.4 -Stable tag: 2.4.4 +Stable tag: 2.5 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -13,7 +13,7 @@ License URI: http://www.gnu.org/licenses/gpl-2.0.html == Description == -This is a simple Wordpress plugin aimed at giving credit where credit is due. +This is a simple community Wordpress plugin aimed at giving credit where credit is due. The plugin inserts an XHTML list into any post/page through a shortcode. If you're into customization, you can specify a format argument and indicate the exact output you are after. There's also an option to display inactive plugins as well. @@ -22,7 +22,8 @@ Key features include... * A simple template system allows you to format how you'd like the plugin information to be shown * Template tags are available for automatically linked items as well as simple text * Choose from a number of pieces of plugin data to be output -* Display inactive plugins as well as active plugins if you wish +* Display inactive plugins as well as active plugins if you wish +* Automatically limit long descriptions to specific lengths, and even remove emojis from the output * Output is cached to provide a super-quick response * A separate shortcode allows you to display how many plugins you have! @@ -32,36 +33,29 @@ Iconography is courtesy of the very talented [Janki Rathod](https://www.fiverr.c 👉 Please visit the [Github page](https://github.com/dartiss/plugins-list "Github") for the latest code development, planned enhancements and known issues 👈 -== Instructions on use == +== Getting Started == To get a list of the plugins that are installed and activated in your website, insert the following into any post or page: -`