44/*
55Class Name: WPU Base Toolbox
66Description: Cool helpers for WordPress Plugins
7- Version: 0.23.0
7+ Version: 0.24.1
88Class URI: https://github.com/WordPressUtilities/wpubaseplugin
99Author: Darklg
1010Author URI: https://darklg.me/
1515defined ('ABSPATH ' ) || die;
1616
1717class WPUBaseToolbox {
18- private $ plugin_version = '0.23.0 ' ;
18+ private $ plugin_version = '0.24.1 ' ;
1919 private $ args = array ();
2020 private $ missing_plugins = array ();
21+ private $ invalid_plugins_versions = array ();
2122 private $ default_module_args = array (
2223 'need_form_js ' => true ,
2324 'need_table_js ' => false ,
@@ -37,7 +38,7 @@ public function __construct($args = array()) {
3738 ));
3839 }
3940
40- public function table_scripts (){
41+ public function table_scripts () {
4142 if ($ this ->args ['need_table_js ' ]) {
4243 wp_enqueue_script (__NAMESPACE__ . '-wpubasetoolbox-table-sort ' , plugins_url ('assets/table-sort.js ' , __FILE__ ), array (), $ this ->plugin_version );
4344 }
@@ -676,7 +677,9 @@ public function check_plugins_dependencies($plugins = array()) {
676677
677678 foreach ($ plugin ['path ' ] as $ plugin_path ) {
678679 if (is_plugin_active ($ plugin_path ) || is_plugin_active_for_network ($ plugin_path )) {
680+ $ this ->check_plugin_version ($ plugin , WP_PLUGIN_DIR . '/ ' . $ plugin_path );
679681 $ has_plugin = true ;
682+ break ;
680683 }
681684
682685 /* Get active must-use plugins list */
@@ -686,6 +689,7 @@ public function check_plugins_dependencies($plugins = array()) {
686689 );
687690 foreach ($ mu_plugins_path as $ mu_plugins_dir ) {
688691 if (is_dir ($ mu_plugins_dir ) && file_exists ($ mu_plugins_dir . '/ ' . $ plugin_path )) {
692+ $ this ->check_plugin_version ($ plugin , $ mu_plugins_dir . '/ ' . $ plugin_path );
689693 $ has_plugin = true ;
690694 break ;
691695 }
@@ -702,6 +706,26 @@ public function check_plugins_dependencies($plugins = array()) {
702706 'set_error_missing_plugins '
703707 ));
704708 }
709+ if (!empty ($ this ->invalid_plugins_versions )) {
710+ add_action ('admin_notices ' , array (&$ this ,
711+ 'set_error_invalid_plugins_versions '
712+ ));
713+ }
714+ }
715+
716+ public function check_plugin_version ($ plugin , $ plugin_path ) {
717+ if (!isset ($ plugin ['min_version ' ])) {
718+ return ;
719+ }
720+
721+ $ plugin_data = get_plugin_data ($ plugin_path );
722+ if (version_compare ($ plugin_data ['Version ' ], $ plugin ['min_version ' ], '< ' )) {
723+ $ this ->invalid_plugins_versions [] = array (
724+ 'name ' => $ plugin_data ['Name ' ],
725+ 'current_version ' => $ plugin_data ['Version ' ],
726+ 'required_version ' => $ plugin ['min_version ' ]
727+ );
728+ }
705729 }
706730
707731 public function set_error_missing_plugins () {
@@ -723,6 +747,26 @@ public function set_error_missing_plugins() {
723747 echo '</div> ' ;
724748 }
725749
750+ public function set_error_invalid_plugins_versions () {
751+
752+ if (!$ this ->invalid_plugins_versions ) {
753+ return ;
754+ }
755+
756+ echo '<div class="error"> ' ;
757+ if (count ($ this ->invalid_plugins_versions ) > 1 ) {
758+ echo '<p> ' . sprintf (__ ('The plugin <b>%s</b> needs specific plugins versions. Please update them: ' , __NAMESPACE__ ), $ this ->args ['plugin_name ' ]) . '</p><ul> ' ;
759+ foreach ($ this ->invalid_plugins_versions as $ plugin ) {
760+ echo '<li>- ' . esc_html ($ plugin ['name ' ]) . ' (current version: ' . esc_html ($ plugin ['current_version ' ]) . ', required version: ' . esc_html ($ plugin ['required_version ' ]) . ')</li> ' ;
761+ }
762+ echo '</ul> ' ;
763+ } else {
764+ $ plugin = $ this ->invalid_plugins_versions [0 ];
765+ echo '<p> ' . sprintf (__ ('The plugin <b>%s</b> needs a specific <b>%s</b> plugin version. Please update it to at least version %s (current version: %s). ' , __NAMESPACE__ ), $ this ->args ['plugin_name ' ], esc_html ($ plugin ['name ' ]), esc_html ($ plugin ['required_version ' ]), esc_html ($ plugin ['current_version ' ])) . '</p> ' ;
766+ }
767+ echo '</div> ' ;
768+ }
769+
726770 public function get_missing_plugin_display_name ($ plugin ) {
727771 $ name = $ plugin ['name ' ];
728772
@@ -859,4 +903,36 @@ function create_custom_user_role($capacities = array(), $args = array()) {
859903
860904 }
861905
906+ /* ----------------------------------------------------------
907+ Markdown to HTML
908+ ---------------------------------------------------------- */
909+
910+ function markdown_to_html ($ text ) {
911+ $ text = trim (wp_strip_all_tags ($ text ));
912+
913+ /* Bold */
914+ $ text = preg_replace ('/\*\*(.+?)\*\*/ ' , '<strong>$1</strong> ' , $ text );
915+
916+ /* Italic */
917+ $ text = preg_replace ('/\*(.+?)\*/ ' , '<em>$1</em> ' , $ text );
918+
919+ /* Links */
920+ $ text = preg_replace ('/\[(.+?)\]\((.+?)\)/ ' , '<a href="$2" rel="noopener noreferrer" target="_blank">$1</a> ' , $ text );
921+
922+ /* Convert dashes to ul/li */
923+ $ lines = explode ("\n" , $ text );
924+ foreach ($ lines as $ line ) {
925+ if (preg_match ('/^\s*-\s+(.+)/ ' , $ line , $ matches )) {
926+ $ new_line = '<ul><li> ' . trim ($ matches [1 ]) . '</li></ul> ' ;
927+ $ text = str_replace ($ line , $ new_line , $ text );
928+ }
929+ }
930+ $ text = preg_replace ('/<\/ul>\s*<ul>/ ' , '' , $ text );
931+
932+ /* Line breaks */
933+ $ text = preg_replace ('/\n/ ' , '<br /> ' , $ text );
934+ $ text = force_balance_tags ($ text );
935+
936+ return $ text ;
937+ }
862938}
0 commit comments