2
2
3
3
if ( ! class_exists ( 'PP_Dependencies ' ) ) :
4
4
5
- /**
6
- * Prospress Dependency Checker
7
- *
8
- * Checks if WooCommerce and Subscriptions are enabled
9
- */
10
- class PP_Dependencies {
5
+ /**
6
+ * Prospress Dependency Checker
7
+ *
8
+ * Checks if WooCommerce and Subscriptions are enabled
9
+ */
10
+ class PP_Dependencies {
11
11
12
- protected static $ admin_notices = array ();
12
+ protected static $ admin_notices = array ();
13
13
14
- protected static $ plugin_filenames = array ();
14
+ protected static $ plugin_filenames = array ();
15
15
16
- public static function init () {
17
- add_action ( 'admin_notices ' , __CLASS__ . '::maybe_display_admin_notices ' );
18
- }
16
+ public static function init () {
17
+ add_action ( 'admin_notices ' , __CLASS__ . '::maybe_display_admin_notices ' );
18
+ }
19
19
20
- /** API Functions **/
20
+ /** API Functions **/
21
21
22
- /**
23
- * Check if the if the WooCommerce plugin is installed and active.
24
- *
25
- * @since 1.0.0
26
- * @return boolean
27
- */
28
- public static function is_woocommerce_active ( $ minimum_version = false ) {
29
- return self ::is_plugin_active ( 'woocommerce.php ' , $ minimum_version , 'woocommerce_db_version ' );
30
- }
22
+ /**
23
+ * Check if the if the WooCommerce plugin is installed and active.
24
+ *
25
+ * @since 1.0.0
26
+ * @return boolean
27
+ */
28
+ public static function is_woocommerce_active ( $ minimum_version = false ) {
29
+ return self ::is_plugin_active ( 'woocommerce.php ' , $ minimum_version , 'woocommerce_db_version ' );
30
+ }
31
31
32
- /**
33
- * Check if the if the WooCommerce Subscriptions plugin is installed and active.
34
- *
35
- * @since 1.0.0
36
- * @return boolean
37
- */
38
- public static function is_subscriptions_active ( $ minimum_version = false ) {
39
- return self ::is_plugin_active ( 'woocommerce-subscriptions.php ' , $ minimum_version , 'woocommerce_subscriptions_active_version ' );
40
- }
32
+ /**
33
+ * Check if the if the WooCommerce Subscriptions plugin is installed and active.
34
+ *
35
+ * @since 1.0.0
36
+ * @return boolean
37
+ */
38
+ public static function is_subscriptions_active ( $ minimum_version = false ) {
39
+ return self ::is_plugin_active ( 'woocommerce-subscriptions.php ' , $ minimum_version , 'woocommerce_subscriptions_active_version ' );
40
+ }
41
41
42
- /** Admin Notices **/
42
+ /** Admin Notices **/
43
43
44
- /**
45
- * Display any admin notices about plugin dependency failures to admins in the admin area.
46
- *
47
- * @since 1.0.0
48
- */
49
- public static function maybe_display_admin_notices () {
50
- if ( ! empty ( self ::$ admin_notices ) && current_user_can ( 'activate_plugins ' ) ) {
51
- foreach ( self ::$ admin_notices as $ admin_notice ) { ?>
44
+ /**
45
+ * Display any admin notices about plugin dependency failures to admins in the admin area.
46
+ *
47
+ * @since 1.0.0
48
+ */
49
+ public static function maybe_display_admin_notices () {
50
+ if ( ! empty ( self ::$ admin_notices ) && current_user_can ( 'activate_plugins ' ) ) {
51
+ foreach ( self ::$ admin_notices as $ admin_notice ) { ?>
52
52
<div id="message" class="error">
53
- <p><?php
53
+ <p>
54
+ <?php
54
55
if ( $ admin_notice ['version_dependency ' ] ) {
55
56
printf ( esc_html ( '%1$s is inactive. This version of %1$s requires %2$s version %3$s or newer. %4$sPlease install or update %2$s to version %3$s or newer »%5$s ' ), '<strong> ' . esc_html ( $ admin_notice ['plugin_name ' ] ) . '</strong> ' , esc_html ( $ admin_notice ['dependency_name ' ] ), esc_html ( $ admin_notice ['version_dependency ' ] ), '<a href=" ' . esc_url ( admin_url ( 'plugins.php ' ) ) . '"> ' , '</a> ' );
56
57
} else {
57
58
// translators: 1$-2$: opening and closing <strong> tags, 3$ plugin name, 4$ required plugin version, 5$-6$: opening and closing link tags, leads to plugins.php in admin
58
- printf ( esc_html ( '%1$s is inactive. %1$s requires %2$s to work correctly. %3$sPlease install or activate %2$s »%4$s ' ), '<strong> ' . esc_html ( $ admin_notice ['plugin_name ' ] ) . '</strong> ' , esc_html ( $ admin_notice ['dependency_name ' ] ) , '<a href=" ' . esc_url ( admin_url ( 'plugins.php ' ) ) . '"> ' , '</a> ' );
59
- } ?>
59
+ printf ( esc_html ( '%1$s is inactive. %1$s requires %2$s to work correctly. %3$sPlease install or activate %2$s »%4$s ' ), '<strong> ' . esc_html ( $ admin_notice ['plugin_name ' ] ) . '</strong> ' , esc_html ( $ admin_notice ['dependency_name ' ] ), '<a href=" ' . esc_url ( admin_url ( 'plugins.php ' ) ) . '"> ' , '</a> ' );
60
+ }
61
+ ?>
60
62
</p>
61
63
</div>
62
- <?php
64
+ <?php
65
+ }
63
66
}
64
67
}
65
- }
66
68
67
- /**
68
- * Queue an admin notice about a plugin dependency failure.
69
- *
70
- * Not passed through i18n functions because we cant use a dynamic text domain.
71
- *
72
- * @since 1.0.0
73
- * @param string $plugin_name The name displayed to the store owner to identify the plugin requiring another plugin.
74
- * @param string $dependency_name The required plugin that is not active, as displayed to the store owner.
75
- * @param bool|string $version_dependency The minimum version of the plugin required, if any.
76
- * @return boolean true if the named plugin is installed and active
77
- */
78
- public static function enqueue_admin_notice ( $ plugin_name , $ dependency_name , $ version_dependency = false ) {
79
- self ::$ admin_notices [] = array (
80
- 'plugin_name ' => $ plugin_name ,
81
- 'dependency_name ' => $ dependency_name ,
82
- 'version_dependency ' => $ version_dependency ,
83
- );
84
- }
69
+ /**
70
+ * Queue an admin notice about a plugin dependency failure.
71
+ *
72
+ * Not passed through i18n functions because we cant use a dynamic text domain.
73
+ *
74
+ * @since 1.0.0
75
+ * @param string $plugin_name The name displayed to the store owner to identify the plugin requiring another plugin.
76
+ * @param string $dependency_name The required plugin that is not active, as displayed to the store owner.
77
+ * @param bool|string $version_dependency The minimum version of the plugin required, if any.
78
+ * @return boolean true if the named plugin is installed and active
79
+ */
80
+ public static function enqueue_admin_notice ( $ plugin_name , $ dependency_name , $ version_dependency = false ) {
81
+ self ::$ admin_notices [] = array (
82
+ 'plugin_name ' => $ plugin_name ,
83
+ 'dependency_name ' => $ dependency_name ,
84
+ 'version_dependency ' => $ version_dependency ,
85
+ );
86
+ }
85
87
86
- /** Helper functions **/
88
+ /** Helper functions **/
87
89
88
- /**
89
- * Helper function to determine whether a plugin is active in the most reliably way possible.
90
- *
91
- * Based on SkyVerge's WooCommerce Plugin Framework - SV_WC_Plugin class.
92
- *
93
- * @since 1.0.0
94
- * @param string $plugin_name plugin name, as the plugin-filename.php
95
- * @param string $minimum_version (optional) Check if the plugin is active that a certain minimum version is also active.
96
- * @param string $version_option_name (optional) The key used to identify the plugin's active version in the wp_options table.
97
- * @return boolean true if the named plugin is installed and active
98
- */
99
- protected static function is_plugin_active ( $ plugin_name , $ minimum_version = false , $ version_option_name = '' ) {
90
+ /**
91
+ * Helper function to determine whether a plugin is active in the most reliably way possible.
92
+ *
93
+ * Based on SkyVerge's WooCommerce Plugin Framework - SV_WC_Plugin class.
94
+ *
95
+ * @since 1.0.0
96
+ * @param string $plugin_name plugin name, as the plugin-filename.php
97
+ * @param string $minimum_version (optional) Check if the plugin is active that a certain minimum version is also active.
98
+ * @param string $version_option_name (optional) The key used to identify the plugin's active version in the wp_options table.
99
+ * @return boolean true if the named plugin is installed and active
100
+ */
101
+ protected static function is_plugin_active ( $ plugin_name , $ minimum_version = false , $ version_option_name = '' ) {
100
102
101
- if ( empty ( self ::$ plugin_filenames ) ) {
103
+ if ( empty ( self ::$ plugin_filenames ) ) {
102
104
103
- $ active_plugins = (array ) get_option ( 'active_plugins ' , array () );
105
+ $ active_plugins = (array ) get_option ( 'active_plugins ' , array () );
104
106
105
- if ( is_multisite () ) {
106
- $ active_plugins = array_merge ( $ active_plugins , array_keys ( get_site_option ( 'active_sitewide_plugins ' , array () ) ) );
107
- }
107
+ if ( is_multisite () ) {
108
+ $ active_plugins = array_merge ( $ active_plugins , array_keys ( get_site_option ( 'active_sitewide_plugins ' , array () ) ) );
109
+ }
108
110
109
- foreach ( $ active_plugins as $ plugin ) {
111
+ foreach ( $ active_plugins as $ plugin ) {
110
112
111
- if ( self ::str_exists ( $ plugin , '/ ' ) ) {
113
+ if ( self ::str_exists ( $ plugin , '/ ' ) ) {
112
114
113
- // normal plugin name (plugin-dir/plugin-filename.php)
114
- list ( , $ filename ) = explode ( '/ ' , $ plugin );
115
+ // normal plugin name (plugin-dir/plugin-filename.php)
116
+ list ( , $ filename ) = explode ( '/ ' , $ plugin );
115
117
116
- } else {
118
+ } else {
117
119
118
- // no directory, just plugin file
119
- $ filename = $ plugin ;
120
+ // no directory, just plugin file
121
+ $ filename = $ plugin ;
120
122
121
- }
123
+ }
122
124
123
- self ::$ plugin_filenames [] = $ filename ;
125
+ self ::$ plugin_filenames [] = $ filename ;
126
+ }
124
127
}
125
- }
126
128
127
- $ is_plugin_active = in_array ( $ plugin_name , self ::$ plugin_filenames );
129
+ $ is_plugin_active = in_array ( $ plugin_name , self ::$ plugin_filenames, true );
128
130
129
- if ( $ minimum_version ) {
130
- return $ is_plugin_active && version_compare ( get_option ( $ version_option_name ), $ minimum_version , '>= ' );
131
- } else {
132
- return $ is_plugin_active ;
131
+ if ( $ minimum_version ) {
132
+ return $ is_plugin_active && version_compare ( get_option ( $ version_option_name ), $ minimum_version , '>= ' );
133
+ } else {
134
+ return $ is_plugin_active ;
135
+ }
133
136
}
134
- }
135
-
136
- /**
137
- * Returns true if the needle exists in haystack
138
- *
139
- * Note: case-sensitive
140
- *
141
- * Based on SkyVerge's WooCommerce Plugin Framework - SV_WC_Helper class.
142
- *
143
- * @since 1.0.0
144
- * @param string $haystack
145
- * @param string $needle
146
- * @return bool
147
- */
148
- protected static function str_exists ( $ haystack , $ needle ) {
149
137
150
- if ( extension_loaded ( 'mbstring ' ) ) {
138
+ /**
139
+ * Returns true if the needle exists in haystack
140
+ *
141
+ * Note: case-sensitive
142
+ *
143
+ * Based on SkyVerge's WooCommerce Plugin Framework - SV_WC_Helper class.
144
+ *
145
+ * @since 1.0.0
146
+ * @param string $haystack
147
+ * @param string $needle
148
+ * @return bool
149
+ */
150
+ protected static function str_exists ( $ haystack , $ needle ) {
151
+
152
+ if ( extension_loaded ( 'mbstring ' ) ) {
153
+
154
+ if ( '' === $ needle ) {
155
+ return false ;
156
+ }
151
157
152
- if ( '' === $ needle ) {
153
- return false ;
154
- }
158
+ return false !== mb_strpos ( $ haystack , $ needle , 0 , 'UTF-8 ' );
155
159
156
- return false !== mb_strpos ( $ haystack , $ needle , 0 , ' UTF-8 ' );
160
+ } else {
157
161
158
- } else {
162
+ $ needle = self :: str_to_ascii ( $ needle );
159
163
160
- $ needle = self ::str_to_ascii ( $ needle );
164
+ if ( '' === $ needle ) {
165
+ return false ;
166
+ }
161
167
162
- if ( '' === $ needle ) {
163
- return false ;
168
+ return false !== strpos ( self ::str_to_ascii ( $ haystack ), self ::str_to_ascii ( $ needle ) );
164
169
}
165
-
166
- return false !== strpos ( self ::str_to_ascii ( $ haystack ), self ::str_to_ascii ( $ needle ) );
167
170
}
168
- }
169
171
170
- /**
171
- * Returns a string with all non-ASCII characters removed. This is useful
172
- * for any string functions that expect only ASCII chars and can't
173
- * safely handle UTF-8. Note this only allows ASCII chars in the range
174
- * 33-126 (newlines/carriage returns are stripped)
175
- *
176
- * Based on SkyVerge's WooCommerce Plugin Framework - SV_WC_Helper class.
177
- *
178
- * @since 1.0.0
179
- * @param string $string string to make ASCII
180
- * @return string
181
- */
182
- protected static function str_to_ascii ( $ string ) {
183
-
184
- // strip ASCII chars 32 and under
185
- $ string = filter_var ( $ string , FILTER_SANITIZE_STRING , FILTER_FLAG_STRIP_LOW );
172
+ /**
173
+ * Returns a string with all non-ASCII characters removed. This is useful
174
+ * for any string functions that expect only ASCII chars and can't
175
+ * safely handle UTF-8.
176
+ *
177
+ * @since 1.0.0
178
+ * @param string $string string to make ASCII
179
+ * @return string
180
+ */
181
+ protected static function str_to_ascii ( $ string ) {
182
+ // Remove non-ASCII characters
183
+ $ string = preg_replace ( ' /[^\x20-\x7E]/ ' , '' , $ string );
184
+
185
+ // Return the cleaned string
186
+ return $ string ;
187
+ }
186
188
187
- // strip ASCII chars 127 and higher
188
- return filter_var ( $ string , FILTER_SANITIZE_STRING , FILTER_FLAG_STRIP_HIGH );
189
189
}
190
190
191
- }
192
-
193
- PP_Dependencies::init ();
191
+ PP_Dependencies::init ();
194
192
195
- endif ;
193
+ endif ;
0 commit comments