block-editor
1 year ago
config-validator
11 months ago
css
11 months ago
js
1 year ago
swv
10 months ago
capabilities.php
7 years ago
contact-form-functions.php
11 months ago
contact-form-template.php
11 months ago
contact-form.php
11 months ago
controller.php
11 months ago
file.php
10 months ago
filesystem.php
11 months ago
form-tag.php
11 months ago
form-tags-manager.php
11 months ago
formatting.php
11 months ago
functions.php
10 months ago
html-formatter.php
10 months ago
integration.php
11 months ago
l10n.php
11 months ago
mail-tag.php
11 months ago
mail.php
11 months ago
pipe.php
11 months ago
pocket-holder.php
3 years ago
rest-api.php
11 months ago
shortcodes.php
11 months ago
special-mail-tags.php
10 months ago
submission.php
10 months ago
upgrade.php
11 months ago
validation-functions.php
11 months ago
validation.php
11 months ago
mail-tag.php
199 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class that represents a mail-tag. |
| 5 | */ |
| 6 | class WPCF7_MailTag { |
| 7 | |
| 8 | private $tag; |
| 9 | private $tagname = ''; |
| 10 | private $name = ''; |
| 11 | private $options = array(); |
| 12 | private $values = array(); |
| 13 | private $form_tag = null; |
| 14 | |
| 15 | |
| 16 | /** |
| 17 | * The constructor method. |
| 18 | */ |
| 19 | public function __construct( $tag, $tagname, $values ) { |
| 20 | $this->tag = $tag; |
| 21 | $this->name = $this->tagname = $tagname; |
| 22 | |
| 23 | $this->options = array( |
| 24 | 'do_not_heat' => false, |
| 25 | 'format' => '', |
| 26 | ); |
| 27 | |
| 28 | if ( ! empty( $values ) ) { |
| 29 | preg_match_all( '/"[^"]*"|\'[^\']*\'/', $values, $matches ); |
| 30 | $this->values = wpcf7_strip_quote_deep( $matches[0] ); |
| 31 | } |
| 32 | |
| 33 | if ( preg_match( '/^_raw_(.+)$/', $tagname, $matches ) ) { |
| 34 | $this->name = trim( $matches[1] ); |
| 35 | $this->options['do_not_heat'] = true; |
| 36 | } |
| 37 | |
| 38 | if ( preg_match( '/^_format_(.+)$/', $tagname, $matches ) ) { |
| 39 | $this->name = trim( $matches[1] ); |
| 40 | $this->options['format'] = $this->values[0]; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | |
| 45 | /** |
| 46 | * Returns the name part of this mail-tag. |
| 47 | */ |
| 48 | public function tag_name() { |
| 49 | return $this->tagname; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | /** |
| 54 | * Returns the form field name corresponding to this mail-tag. |
| 55 | */ |
| 56 | public function field_name() { |
| 57 | return strtr( $this->name, '.', '_' ); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | /** |
| 62 | * Returns the value of the specified option. |
| 63 | */ |
| 64 | public function get_option( $option ) { |
| 65 | return $this->options[$option]; |
| 66 | } |
| 67 | |
| 68 | |
| 69 | /** |
| 70 | * Returns the values part of this mail-tag. |
| 71 | */ |
| 72 | public function values() { |
| 73 | return $this->values; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | /** |
| 78 | * Retrieves the WPCF7_FormTag object that corresponds to this mail-tag. |
| 79 | */ |
| 80 | public function corresponding_form_tag() { |
| 81 | if ( $this->form_tag instanceof WPCF7_FormTag ) { |
| 82 | return $this->form_tag; |
| 83 | } |
| 84 | |
| 85 | if ( $submission = WPCF7_Submission::get_instance() ) { |
| 86 | $contact_form = $submission->get_contact_form(); |
| 87 | |
| 88 | $tags = $contact_form->scan_form_tags( array( |
| 89 | 'name' => $this->field_name(), |
| 90 | 'feature' => '! zero-controls-container', |
| 91 | ) ); |
| 92 | |
| 93 | if ( $tags ) { |
| 94 | $this->form_tag = $tags[0]; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return $this->form_tag; |
| 99 | } |
| 100 | |
| 101 | } |
| 102 | |
| 103 | |
| 104 | use Contactable\SWV; |
| 105 | |
| 106 | /** |
| 107 | * Mail-tag output calculator. |
| 108 | */ |
| 109 | class WPCF7_MailTag_OutputCalculator { |
| 110 | |
| 111 | const email = 0b100; |
| 112 | const text = 0b010; |
| 113 | const blank = 0b001; |
| 114 | |
| 115 | private $contact_form; |
| 116 | |
| 117 | public function __construct( WPCF7_ContactForm $contact_form ) { |
| 118 | $this->contact_form = $contact_form; |
| 119 | } |
| 120 | |
| 121 | public function calc_output( WPCF7_MailTag $mail_tag ) { |
| 122 | return $this->calc_swv_result( |
| 123 | $mail_tag, |
| 124 | $this->contact_form->get_schema() |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | private function calc_swv_result( WPCF7_MailTag $mail_tag, SWV\Rule $rule ) { |
| 129 | |
| 130 | if ( $rule instanceof SWV\AnyRule ) { |
| 131 | $result = 0b000; |
| 132 | |
| 133 | foreach ( $rule->rules() as $child_rule ) { |
| 134 | $result |= $this->calc_swv_result( $mail_tag, $child_rule ); |
| 135 | } |
| 136 | |
| 137 | return $result; |
| 138 | } |
| 139 | |
| 140 | if ( $rule instanceof SWV\CompositeRule ) { |
| 141 | $result = 0b111; |
| 142 | |
| 143 | foreach ( $rule->rules() as $child_rule ) { |
| 144 | $result &= $this->calc_swv_result( $mail_tag, $child_rule ); |
| 145 | } |
| 146 | |
| 147 | return $result; |
| 148 | } |
| 149 | |
| 150 | $field_prop = $rule->get_property( 'field' ); |
| 151 | |
| 152 | if ( empty( $field_prop ) or $field_prop !== $mail_tag->field_name() ) { |
| 153 | return self::email | self::text | self::blank; |
| 154 | } |
| 155 | |
| 156 | if ( $rule instanceof SWV\RequiredRule ) { |
| 157 | return ~ self::blank; |
| 158 | } |
| 159 | |
| 160 | if ( $rule instanceof SWV\EmailRule ) { |
| 161 | return self::email | self::blank; |
| 162 | } |
| 163 | |
| 164 | if ( $rule instanceof SWV\EnumRule ) { |
| 165 | $acceptable_values = (array) $rule->get_property( 'accept' ); |
| 166 | $acceptable_values = array_map( 'strval', $acceptable_values ); |
| 167 | $acceptable_values = array_filter( $acceptable_values ); |
| 168 | $acceptable_values = array_unique( $acceptable_values ); |
| 169 | |
| 170 | if ( ! $mail_tag->get_option( 'do_not_heat' ) ) { |
| 171 | $pipes = $this->contact_form->get_pipes( |
| 172 | $mail_tag->field_name() |
| 173 | ); |
| 174 | |
| 175 | $acceptable_values = array_map( |
| 176 | static function ( $val ) use ( $pipes ) { |
| 177 | return $pipes->do_pipe( $val ); |
| 178 | }, |
| 179 | $acceptable_values |
| 180 | ); |
| 181 | } |
| 182 | |
| 183 | $email_values = array_filter( |
| 184 | $acceptable_values, |
| 185 | 'wpcf7_is_mailbox_list' |
| 186 | ); |
| 187 | |
| 188 | if ( count( $email_values ) === count( $acceptable_values ) ) { |
| 189 | return self::email | self::blank; |
| 190 | } else { |
| 191 | return self::email | self::text | self::blank; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | return self::email | self::text | self::blank; |
| 196 | } |
| 197 | |
| 198 | } |
| 199 |