self::MAX_LENGTH) { continue; } $out[$line] = true; if (count($out) >= self::MAX_PATTERNS) { break; } } return array_map('strval', array_keys($out)); } public static function to_text(array $patterns) { return implode("\n", self::parse($patterns)); } public static function matches($subject, $pattern) { $subject = (string) $subject; $pattern = (string) $pattern; if ('' === $subject || '' === $pattern) { return false; } if (false === strpos($pattern, '*') && false === strpos($pattern, '?')) { return false !== mb_stripos($subject, $pattern); } $rx = preg_quote($pattern, '~'); $rx = str_replace(array('\*', '\?'), array('.*', '.'), $rx); return 1 === preg_match('~^' . $rx . '$~iu', $subject); } public static function matches_any($subject, array $patterns) { if ('' === (string) $subject || !$patterns) { return false; } foreach ($patterns as $pattern) { if (self::matches($subject, $pattern)) { return true; } } return false; } public static function any_matches_any(array $subjects, array $patterns) { if (!$patterns) { return false; } foreach ($subjects as $subject) { if (self::matches_any($subject, $patterns)) { return true; } } return false; } public static function help_text() { return __('One pattern per line, matched against the product\'s SKU and barcode. * stands for any run of characters and ? for exactly one; a pattern with neither matches anywhere in the code. So "101.55.3*" catches only codes that START with it, "*-EL" only those that END with it, and a plain "EL-" any code containing it. Upper and lower case are the same; spaces are not.', 'ai-eshop-optimizer'); } public static function render_field(array $args) { $name = isset($args['name']) ? (string) $args['name'] : 'code_patterns'; $label = isset($args['label']) ? (string) $args['label'] : __('Product codes (optional)', 'ai-eshop-optimizer'); $help = array_key_exists('help', $args) ? (string) $args['help'] : self::help_text(); $value = self::to_text((array) ($args['selected'] ?? array())); $ph = isset($args['placeholder']) ? (string) $args['placeholder'] : "101.55.3*\nEL-"; $id = 'aieo-codepat-' . $name; return AIEO_Kit::row( $label, AIEO_Kit::textarea( $name, $value, array( 'id' => $id, 'rows' => 3, 'code' => true, 'placeholder' => $ph, 'help' => '' !== $help ) ), $help, array( 'id' => $id ) ); } public static function collect($field) { if (!isset($_POST[$field])) { return array(); } return self::parse(wp_unslash($_POST[$field])); } public static function to_json(array $patterns) { return wp_json_encode(self::parse($patterns)); } }