| @@ -1163,22 +1163,26 @@ | ||
| 1163 | 1163 | return ( json_last_error() == JSON_ERROR_NONE ); |
| 1164 | 1164 | } |
| 1165 | 1165 | |
| 1166 | 1166 | /** |
| 1167 | - * Check whether a specific class::method exists in the current call stack. | |
| 1167 | + * Check whether a specific class::method, or a plain function, exists in | |
| 1168 | + * the current call stack. | |
| 1168 | 1169 | * |
| 1169 | 1170 | * Useful for detecting callers like WooCommerce image regeneration |
| 1170 | 1171 | * without hard dependencies. |
| 1171 | 1172 | * |
| 1172 | 1173 | * @since 1.3.7 |
| 1173 | - * @param string $class Fully qualified class name. | |
| 1174 | - * @param string|null $function Method name (optional). | |
| 1174 | + * @since 1.4.2 $class made nullable; when null, matches on $function alone | |
| 1175 | + * against frames with no class (plain procedural functions). | |
| 1176 | + * @param string|null $class Fully qualified class name, or null to match | |
| 1177 | + * a plain function by name instead. | |
| 1178 | + * @param string|null $function Method (or, with $class null, function) name. | |
| 1175 | 1179 | * @param int $depth Backtrace depth limit. |
| 1176 | 1180 | * |
| 1177 | 1181 | * @return bool |
| 1178 | 1182 | */ |
| 1179 | 1183 | public static function is_called_from( |
| 1180 | - string $class, | |
| 1184 | + ?string $class, | |
| 1181 | 1185 | ?string $function = null, |
| 1182 | 1186 | int $depth = 15 |
| 1183 | 1187 | ) : bool { |
| 1184 | 1188 | |
| @@ -1184,8 +1188,21 @@ | ||
| 1184 | 1188 | |
| 1185 | 1189 | $trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, $depth ); |
| 1186 | 1190 | |
| 1187 | 1191 | foreach ( $trace as $frame ) { |
| 1192 | + | |
| 1193 | + if ( $class === null ) { | |
| 1194 | + // Match a plain function call — these frames have no 'class' key. | |
| 1195 | + if ( ! empty( $frame['class'] ) ) { | |
| 1196 | + continue; | |
| 1197 | + } | |
| 1198 | + | |
| 1199 | + if ( isset( $frame['function'] ) && $frame['function'] === $function ) { | |
| 1200 | + return true; | |
| 1201 | + } | |
| 1202 | + | |
| 1203 | + continue; | |
| 1204 | + } | |
| 1188 | 1205 | |
| 1189 | 1206 | if ( empty( $frame['class'] ) ) { |
| 1190 | 1207 | continue; |
| 1191 | 1208 | } |