| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace Jet_Form_Builder\Classes; |
| 5 |
|
| 6 |
// If this file is called directly, abort. |
| 7 |
if ( ! defined( 'WPINC' ) ) { |
| 8 |
die; |
| 9 |
} |
| 10 |
|
| 11 |
class Regexp_Tools { |
| 12 |
|
| 13 |
private $field_names_regexp = array(); |
| 14 |
private $macro_exist = false; |
| 15 |
|
| 16 |
public function has_macro( $value ): bool { |
| 17 |
if ( ! is_scalar( $value ) || ! is_string( $value ) ) { |
| 18 |
return false; |
| 19 |
} |
| 20 |
|
| 21 |
if ( preg_match( '/%[\w\-]+::.*?%/', $value ) ) { |
| 22 |
return true; |
| 23 |
} |
| 24 |
|
| 25 |
$this->macro_exist = false; |
| 26 |
|
| 27 |
preg_replace_callback_array( |
| 28 |
$this->field_names_regexp, |
| 29 |
$value |
| 30 |
); |
| 31 |
|
| 32 |
return $this->macro_exist; |
| 33 |
} |
| 34 |
|
| 35 |
public function set_field_names( array $names ) { |
| 36 |
foreach ( $names as $name => $counter ) { |
| 37 |
$this->field_names_regexp[ "/%({$name}).*?%/" ] = array( $this, 'regexp_replace_callback' ); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* @param array $matches |
| 43 |
* |
| 44 |
* @return mixed |
| 45 |
*/ |
| 46 |
private function regexp_replace_callback( array $matches ) { |
| 47 |
$this->macro_exist = true; |
| 48 |
|
| 49 |
return $matches[0]; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* @param string $content |
| 54 |
* |
| 55 |
* @return array |
| 56 |
*/ |
| 57 |
public function get_form_ids_from_shortcode( string $content ): array { |
| 58 |
$matches = array(); |
| 59 |
|
| 60 |
preg_match_all( '/\[jet_fb_form.*?form_id="(\d+)"/', $content, $matches ); |
| 61 |
|
| 62 |
return $matches[1] ?? array(); |
| 63 |
} |
| 64 |
|
| 65 |
public function get_form_ids_from_block( string $content ): array { |
| 66 |
$matches = array(); |
| 67 |
|
| 68 |
preg_match_all( |
| 69 |
'/<!-- wp:jet\-forms\/form\-block.*?"form_id":(\d+)/', |
| 70 |
$content, |
| 71 |
$matches |
| 72 |
); |
| 73 |
|
| 74 |
return $matches[1] ?? array(); |
| 75 |
} |
| 76 |
|
| 77 |
} |
| 78 |
|