in the content. * * @since 1.6.7 * * @param string $content Content. * * @return array */ private function get_all_smart_tags( $content ) { /** * A smart tag should start and end with a curly brace. * ([a-z0-9_]+) a smart tag name and also the first capturing group. Lowercase letters, digits, and an underscore. * (|[ =][^\n}]*) - second capturing group: * | no characters at all or the following: * [ =][^\n}]* space or equal sign and any number of any characters except new line and closing curly brace. */ preg_match_all( '~{([a-z0-9_]+)(|[ =][^\n}]*)}~', $content, $smart_tags ); return array_combine( $smart_tags[0], $smart_tags[1] ); } /** * Process smart tags. * * @since 1.6.7 * @since 1.8.7 Added `$context` parameter. * * @param string $content Content. * @param array $form_data Form data. * @param array $fields List of fields. * @param string $entry_id Entry ID. * @param string $context Context. * * @return string */ public function process( $content, $form_data, $fields = [], $entry_id = '', $context = '' ) { $smart_tags = $this->get_all_smart_tags( $content ); if ( empty( $smart_tags ) ) { return $content; } foreach ( $smart_tags as $smart_tag => $tag_name ) { $class_name = $this->get_smart_tag_class_name( $tag_name ); $smart_tag_object = new $class_name( $smart_tag, $context ); $value = $smart_tag_object->get_value( $form_data, $fields, $entry_id ); $field_id = $smart_tag_object->get_attributes()['field_id'] ?? 0; $field_id = (int) explode( '|', $field_id )[0]; if ( $context === 'confirmation_redirect' && $field_id > 0 && in_array( $fields[ $field_id ]['type'], wpforms_get_multi_fields(), true ) ) { // Protect from the case where user already placed a pipe in the value. $value = str_replace( [ "\r\n", "\r", "\n", '|' ], [ rawurlencode( '|' ), '|', '|', '|' ], $value ); } /** * Modify the smart tag value. * * @since 1.6.7 * @since 1.6.7.1 Added the 5th argument. * @since 1.9.0 Added the 6th argument. * * @param scalar|null $value Smart Tag value. * @param array $form_data Form data. * @param string $fields List of fields. * @param int $entry_id Entry ID. * @param SmartTag $smart_tag_object The smart tag object or the Generic object for those cases when class unregistered. * @param string $context Context. */ $value = apply_filters( // phpcs:ignore WPForms.PHP.ValidateHooks.InvalidHookName "wpforms_smarttags_process_{$tag_name}_value", $value, $form_data, $fields, $entry_id, $smart_tag_object, $context ); /** * Modify a smart tag value. * * @since 1.6.7.1 * * @param scalar|null $value Smart Tag value. * @param string $tag_name Smart tag name. * @param array $form_data Form data. * @param string $fields List of fields. * @param int $entry_id Entry ID. * @param SmartTag $smart_tag_object The smart tag object or the Generic object for those cases when class unregistered. */ $value = apply_filters( 'wpforms_smarttags_process_value', $value, $tag_name, $form_data, $fields, $entry_id, $smart_tag_object ); if ( $value !== null ) { $content = $this->replace( $smart_tag, $value, $content ); } /** * Modify content with smart tags. * * @since 1.4.0 * @since 1.6.7.1 Added 3rd, 4th, 5th, 6th arguments. * * @param string $content Content of the Smart Tag. * @param string $tag_name Tag name of the Smart Tag. * @param array $form_data Form data. * @param string $fields List of fields. * @param int $entry_id Entry ID. * @param SmartTag $smart_tag_object The smart tag object or the Generic object for those cases when class unregistered. */ $content = (string) apply_filters( 'wpforms_smart_tag_process', $content, $tag_name, $form_data, $fields, $entry_id, $smart_tag_object ); } return $content; } /** * Determine if the smart tag is registered. * * @since 1.6.7 * * @param string $smart_tag_name Smart tag name. * * @return bool */ protected function has_smart_tag( $smart_tag_name ) { return array_key_exists( $smart_tag_name, $this->get_smart_tags() ); } /** * Get smart tag class name. * * @since 1.6.7 * * @param string $smart_tag_name Smart tag name. * * @return string */ protected function get_smart_tag_class_name( $smart_tag_name ) { if ( ! $this->has_smart_tag( $smart_tag_name ) ) { return Generic::class; } $class_name = str_replace( ' ', '', ucwords( str_replace( '_', ' ', $smart_tag_name ) ) ); $full_class_name = '\\WPForms\\SmartTags\\SmartTag\\' . $class_name; if ( class_exists( $full_class_name ) ) { return $full_class_name; } /** * Modify a smart tag class name that describes the smart tag logic. * * @since 1.6.7 * * @param string $class_name The value. * @param string $smart_tag_name Smart tag name. */ $full_class_name = apply_filters( 'wpforms_smarttags_get_smart_tag_class_name', '', $smart_tag_name ); return class_exists( $full_class_name ) ? $full_class_name : Generic::class; } /** * Retrieve the builder's special tags. * * @since 1.6.7 * * @return array */ protected function get_replacement_builder_tags() { return [ 'date' => 'date format="m/d/Y"', 'query_var' => 'query_var key=""', 'user_meta' => 'user_meta key=""', ]; } /** * Hide smart tags in the builder. * * @since 1.6.7 * * @return array */ protected function get_hidden_builder_tags() { return [ 'field_id', 'field_html_id', 'field_value_id', ]; } /** * Builder tags. * * @since 1.6.7 * * @return array */ public function builder() { $smart_tags = $this->get_smart_tags(); $replacement_tags = $this->get_replacement_builder_tags(); $hidden_tags = $this->get_hidden_builder_tags(); foreach ( $replacement_tags as $tag => $replacement_tag ) { $smart_tags = wpforms_array_insert( $smart_tags, [ $replacement_tag => $smart_tags[ $tag ] ], $tag ); unset( $smart_tags[ $tag ] ); } foreach ( $hidden_tags as $hidden_tag ) { unset( $smart_tags[ $hidden_tag ] ); } return $smart_tags; } /** * Replace a found smart tag with the final value. * * @since 1.6.7 * * @param string $tag The tag. * @param string $value The value. * @param string $content Content. * * @return string */ private function replace( $tag, $value, $content ) { return str_replace( $tag, strip_shortcodes( $value ), $content ); } /** * Replace a found smart tag with the final value. * * @codeCoverageIgnore * * @since 1.5.9 * @deprecated 1.6.7 * * @param string $tag The tag. * @param string $value The value. * @param string $content Content. * * @return string */ public function parse( $tag, $value, $content ) { _deprecated_function( __METHOD__, '1.6.7 of the WPForms plugin' ); return $this->replace( $tag, $value, $content ); } }