Skip to content

Commit ac6b9ad

Browse files
authored
Merge pull request #32 from woocommerce/fix/hpos_compat
Fix/hpos compat
2 parents b9c136a + dc56b8d commit ac6b9ad

File tree

2 files changed

+176
-163
lines changed

2 files changed

+176
-163
lines changed

includes/class-pp-dependencies.php

Lines changed: 142 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -2,194 +2,192 @@
22

33
if ( ! class_exists( 'PP_Dependencies' ) ) :
44

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 {
1111

12-
protected static $admin_notices = array();
12+
protected static $admin_notices = array();
1313

14-
protected static $plugin_filenames = array();
14+
protected static $plugin_filenames = array();
1515

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+
}
1919

20-
/** API Functions **/
20+
/** API Functions **/
2121

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+
}
3131

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+
}
4141

42-
/** Admin Notices **/
42+
/** Admin Notices **/
4343

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 ) { ?>
5252
<div id="message" class="error">
53-
<p><?php
53+
<p>
54+
<?php
5455
if ( $admin_notice['version_dependency'] ) {
5556
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 &raquo;%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>' );
5657
} else {
5758
// 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 &raquo;%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 &raquo;%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+
?>
6062
</p>
6163
</div>
62-
<?php
64+
<?php
65+
}
6366
}
6467
}
65-
}
6668

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+
}
8587

86-
/** Helper functions **/
88+
/** Helper functions **/
8789

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 = '' ) {
100102

101-
if ( empty( self::$plugin_filenames ) ) {
103+
if ( empty( self::$plugin_filenames ) ) {
102104

103-
$active_plugins = (array) get_option( 'active_plugins', array() );
105+
$active_plugins = (array) get_option( 'active_plugins', array() );
104106

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+
}
108110

109-
foreach ( $active_plugins as $plugin ) {
111+
foreach ( $active_plugins as $plugin ) {
110112

111-
if ( self::str_exists( $plugin, '/' ) ) {
113+
if ( self::str_exists( $plugin, '/' ) ) {
112114

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 );
115117

116-
} else {
118+
} else {
117119

118-
// no directory, just plugin file
119-
$filename = $plugin;
120+
// no directory, just plugin file
121+
$filename = $plugin;
120122

121-
}
123+
}
122124

123-
self::$plugin_filenames[] = $filename;
125+
self::$plugin_filenames[] = $filename;
126+
}
124127
}
125-
}
126128

127-
$is_plugin_active = in_array( $plugin_name, self::$plugin_filenames );
129+
$is_plugin_active = in_array( $plugin_name, self::$plugin_filenames, true );
128130

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+
}
133136
}
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 ) {
149137

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+
}
151157

152-
if ( '' === $needle ) {
153-
return false;
154-
}
158+
return false !== mb_strpos( $haystack, $needle, 0, 'UTF-8' );
155159

156-
return false !== mb_strpos( $haystack, $needle, 0, 'UTF-8' );
160+
} else {
157161

158-
} else {
162+
$needle = self::str_to_ascii( $needle );
159163

160-
$needle = self::str_to_ascii( $needle );
164+
if ( '' === $needle ) {
165+
return false;
166+
}
161167

162-
if ( '' === $needle ) {
163-
return false;
168+
return false !== strpos( self::str_to_ascii( $haystack ), self::str_to_ascii( $needle ) );
164169
}
165-
166-
return false !== strpos( self::str_to_ascii( $haystack ), self::str_to_ascii( $needle ) );
167170
}
168-
}
169171

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+
}
186188

187-
// strip ASCII chars 127 and higher
188-
return filter_var( $string, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH );
189189
}
190190

191-
}
192-
193-
PP_Dependencies::init();
191+
PP_Dependencies::init();
194192

195-
endif;
193+
endif;

0 commit comments

Comments
 (0)