PluginProbe
Plugins List / 2.4.4
Plugins List v2.4.4
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 / plugins-list.php

plugins-list.php in Plugins List 2.4.4, at plugins-list.php

342 lines 9.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 Plugin Name: Plugins List
4 Plugin URI: https://github.com/dartiss/plugins-list
5 Description: 🔌 Allows you to insert a list of the WordPress plugins you are using into any post/page.
6 Version: 2.4.4
7 Author: David Artiss
8 Author URI: https://artiss.blog
9 Text Domain: plugins-list
10
11 @package plugins-list
12 */
13
14 define( 'DEFAULT_PLUGIN_LIST_FORMAT', '<li>{{LinkedTitle}} by {{LinkedAuthor}}.</li>' );
15
16 if ( ! function_exists( 'get_plugins' ) ) {
17 require_once ABSPATH . 'wp-admin/includes/plugin.php'; }
18
19 /**
20 * Add meta to plugin details
21 *
22 * Add options to plugin meta line
23 *
24 * @param string $links Current links.
25 * @param string $file File in use.
26 * @return string Links, now with settings added.
27 */
28 function set_plugins_list_meta( $links, $file ) {
29
30 if ( false !== strpos( $file, 'plugins-list.php' ) ) {
31
32 $links = array_merge( $links, array( '<a href="https://github.com/dartiss/plugins-list">' . __( 'Github', 'plugins-list' ) . '</a>' ) );
33
34 $links = array_merge( $links, array( '<a href="http://wordpress.org/support/plugin/plugins-list">' . __( 'Support', 'plugins-list' ) . '</a>' ) );
35
36 $links = array_merge( $links, array( '<a href="https://artiss.blog/donate">' . __( 'Donate', 'plugins-list' ) . '</a>' ) );
37
38 $links = array_merge( $links, array( '<a href="https://wordpress.org/support/plugin/plugins-list/reviews/#new-post">' . __( 'Write a Review', 'plugins-list' ) . '&nbsp;⭐️⭐️⭐️⭐️⭐️</a>' ) );
39 }
40
41 return $links;
42 }
43
44 add_filter( 'plugin_row_meta', 'set_plugins_list_meta', 10, 2 );
45
46 /**
47 * Main Shortcode Function
48 *
49 * Extract shortcode parameters and call main function to output results
50 *
51 * @uses get_plugins_list Get the list of plugins
52 *
53 * @param string $paras Shortcode parameters.
54 * @return stri Output.
55 */
56 function plugins_list_shortcode( $paras ) {
57
58 $atts = shortcode_atts(
59 array(
60 'format' => '',
61 'show_inactive' => '',
62 'show_active' => '',
63 'cache' => '',
64 'nofollow' => '',
65 'target' => '',
66 'by_author' => '',
67 ),
68 $paras
69 );
70
71 $output = get_plugins_list( $atts['format'], $atts['show_inactive'], $atts['show_active'], $atts['cache'], $atts['nofollow'], $atts['target'], $atts['by_author'] );
72
73 return $output;
74 }
75
76 add_shortcode( 'plugins_list', 'plugins_list_shortcode' );
77
78 /**
79 * Number of plugins shortcode
80 *
81 * Shortcode to return number of plugins
82 *
83 * @uses get_plugin_number Get the number of plugins
84 *
85 * @param string $paras Shortcode parameters.
86 * @return string Output.
87 */
88 function plugin_number_shortcode( $paras ) {
89
90 $atts = shortcode_atts(
91 array(
92 'active' => 'true',
93 'inactive' => 'false',
94 'cache' => 5,
95 ),
96 $paras
97 );
98
99 $output = get_plugin_number( $atts['active'], $atts['inactive'], $atts['cache'] );
100
101 return $output;
102 }
103
104 add_shortcode( 'plugins_number', 'plugin_number_shortcode' );
105
106 /**
107 * Get Plugins List
108 *
109 * Get list of plugins and, optionally, format them
110 *
111 * @uses format_plugin_list Format the plugin list
112 * @uses get_plugin_list_data Get the plugin data
113 *
114 * @param string $format Requires format.
115 * @param string $show_inactive Whether to format or not.
116 * @param string $show_active Whether to show active or not.
117 * @param string $cache Cache time.
118 * @param string $nofollow Whether to add nofollow to link.
119 * @param string $target Link target.
120 * @param string $by_author Which author.
121 * @return string Output.
122 */
123 function get_plugins_list( $format, $show_inactive, $show_active, $cache, $nofollow, $target, $by_author ) {
124
125 // Set default values.
126
127 if ( '' === $format ) {
128 $format = DEFAULT_PLUGIN_LIST_FORMAT;
129 }
130 if ( '' === $show_inactive ) {
131 $show_inactive = 'false';
132 }
133 if ( '' === $show_active ) {
134 $show_active = 'true';
135 }
136 if ( '' === $cache ) {
137 $cache = 5;
138 }
139 if ( $nofollow ) {
140 $nofollow = ' rel="nofollow"';
141 } else {
142 $nofollow = '';
143 }
144 if ( '' !== $target ) {
145 $target = ' target="' . $target . '"';
146 } else {
147 $target = '';
148 }
149 if ( '' !== $by_author ) {
150 $by_author = 'true';
151 }
152
153 // Get plugin data.
154
155 $plugins = get_plugin_list_data( $cache );
156
157 // Sort the plugin array if required in author sequence.
158
159 if ( 'true' === $by_author ) {
160 uasort(
161 $plugins,
162 function( $a, $b ) {
163 return strtoupper( $a['Author'] ) <=> strtoupper( $b['Author'] );
164 }
165 );
166 }
167
168 // Extract each plugin and format the output.
169
170 $output = '';
171
172 foreach ( $plugins as $plugin_file => $plugin_data ) {
173
174 if ( ( is_plugin_active( $plugin_file ) && 'true' === $show_active ) || ( ! is_plugin_active( $plugin_file ) && 'true' === $show_inactive ) ) {
175
176 $output .= format_plugin_list( $plugin_data, $format, $nofollow, $target );
177 }
178 }
179
180 // Return the code, with HTML comments.
181
182 return "\n" . $output . "\n";
183
184 }
185
186 /**
187 * Get Plugin number
188 *
189 * Get number of plugins
190 *
191 * @since 2.2
192 *
193 * @uses get_plugin_list_data Get the plugin data
194 *
195 * @param string $show_active Whether to include active plugins or not.
196 * @param string $show_inactive Whether to include inactive plugins or not.
197 * @param string $cache Cache time.
198 * @return string Plugin number.
199 */
200 function get_plugin_number( $show_active, $show_inactive, $cache ) {
201
202 // Get plugin data.
203
204 $plugins = get_plugin_list_data( $cache );
205
206 // Get count.
207
208 if ( 'true' === $show_inactive && 'true' === $show_active ) {
209 $output = count( $plugins );
210 } else {
211 $output = 0;
212 foreach( $plugins as $plugin_file => $plugin_data ) { // @codingStandardsIgnoreLine -- require $plugin_data for array values but not doing anything with it
213 if ( is_plugin_active( $plugin_file ) ) {
214 $output++;
215 }
216 }
217 if ( 'true' === $show_inactive ) {
218 $output = count( $plugins ) - $output; }
219 }
220
221 return $output;
222 }
223
224 /**
225 * Get Plugins data
226 *
227 * Get plugin data and cache it
228 *
229 * @uses format_plugin_list Format the plugin list.
230 *
231 * @param string $cache Cache time.
232 * @return string Plugin data.
233 */
234 function get_plugin_list_data( $cache ) {
235
236 // Attempt to get plugin list from cache.
237
238 if ( ! $cache ) {
239 $cache = 'no'; }
240
241 $plugins = false;
242 $cache_key = 'plugins_list';
243 if ( is_numeric( $cache ) ) {
244 $plugins = get_transient( $cache_key ); }
245
246 // If not using cache, generate a new list and cache that.
247
248 if ( ! $plugins ) {
249 $plugins = get_plugins();
250 if ( ( '' !== $plugins ) && ( is_numeric( $cache ) ) ) {
251 set_transient( $cache_key, $plugins, MINUTE_IN_SECONDS * $cache ); }
252 }
253
254 return $plugins;
255 }
256
257 /**
258 * Format Plugin List
259 *
260 * Format the plugin list
261 *
262 * @uses replace_plugin_list_tags Replace the tags
263 *
264 * @param string $plugin_data The plugin list.
265 * @param string $format Format to use.
266 * @param string $nofollow Nofollow text.
267 * @param string $target Target text.
268 * @return string Output.
269 */
270 function format_plugin_list( $plugin_data, $format, $nofollow, $target ) {
271
272 // Allowed tag.
273
274 $plugins_allowedtags = array(
275 'a' => array(
276 'href' => array(),
277 'title' => array(),
278 ),
279 'abbr' => array( 'title' => array() ),
280 'acronym' => array( 'title' => array() ),
281 'code' => array(),
282 'em' => array(),
283 'strong' => array(),
284 );
285
286 // Sanitize all displayed data.
287
288 $plugin_data['Title'] = wp_kses( $plugin_data['Title'], $plugins_allowedtags );
289 $plugin_data['PluginURI'] = wp_kses( $plugin_data['PluginURI'], $plugins_allowedtags );
290 $plugin_data['AuthorURI'] = wp_kses( $plugin_data['AuthorURI'], $plugins_allowedtags );
291 $plugin_data['Version'] = wp_kses( $plugin_data['Version'], $plugins_allowedtags );
292 $plugin_data['Author'] = wp_kses( $plugin_data['Author'], $plugins_allowedtags );
293
294 // Replace the tags.
295
296 $format = replace_plugin_list_tags( $plugin_data, $format, $nofollow, $target );
297
298 return $format;
299 }
300
301 /**
302 * Replace tags
303 *
304 * Replace the tags in the provided format
305 *
306 * @param string $plugin_data The plugin list.
307 * @param string $format Format to use.
308 * @param string $nofollow Nofollow text.
309 * @param string $target Target text.
310 * @return string Output.
311 */
312 function replace_plugin_list_tags( $plugin_data, $format, $nofollow, $target ) {
313
314 $format = strtr(
315 $format,
316 array(
317 '{{Title}}' => $plugin_data['Title'],
318 '{{PluginURI}}' => $plugin_data['PluginURI'],
319 '{{AuthorURI}}' => $plugin_data['AuthorURI'],
320 '{{Version}}' => $plugin_data['Version'],
321 '{{Description}}' => $plugin_data['Description'],
322 '{{Author}}' => $plugin_data['Author'],
323 '{{LinkedTitle}}' => "<a href='" . $plugin_data['PluginURI'] . "' title='" . $plugin_data['Title'] . "'" . $nofollow . $target . '>' . $plugin_data['Title'] . '</a>',
324 '{{LinkedAuthor}}' => "<a href='" . $plugin_data['AuthorURI'] . "' title='" . $plugin_data['Author'] . "'" . $nofollow . $target . '>' . $plugin_data['Author'] . '</a>',
325 '#Title#' => $plugin_data['Title'],
326 '#PluginURI#' => $plugin_data['PluginURI'],
327 '#AuthorURI#' => $plugin_data['AuthorURI'],
328 '#Version#' => $plugin_data['Version'],
329 '#Description#' => $plugin_data['Description'],
330 '#Author#' => $plugin_data['Author'],
331 '#LinkedTitle#' => "<a href='" . $plugin_data['PluginURI'] . "' title='" . $plugin_data['Title'] . "'" . $nofollow . $target . '>' . $plugin_data['Title'] . '</a>',
332 '#LinkedAuthor#' => "<a href='" . $plugin_data['AuthorURI'] . "' title='" . $plugin_data['Author'] . "'" . $nofollow . $target . '>' . $plugin_data['Author'] . '</a>',
333 '{{' => '<',
334 '}}' => '>',
335 '{' => '<',
336 '}' => '>',
337 )
338 );
339
340 return $format;
341 }
342