|
| 1 | +<?php |
| 2 | +/** |
| 3 | +* Plugin Name: Conditional Smart Tags for WPForms |
| 4 | +* Plugin URI: https://wordpress.org/plugins/conditional-smart-tags-for-wpforms |
| 5 | +* Description: Adds a custom if-then-else smart tag syntax to conditionally show or hide content based on a field value in WPForms. |
| 6 | +* Version: 1.0.0 |
| 7 | +* Requires at least: 5.2 |
| 8 | +* Requires PHP: 7.2 |
| 9 | +* Author: Maximum.Software |
| 10 | +* Author URI: https://maximum.software |
| 11 | +* Text Domain: conditional-smart-tags-for-wpforms |
| 12 | +* License: GPL-2.0-or-later |
| 13 | +* License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 14 | +*/ |
| 15 | + |
| 16 | +// Exit if accessed directly. |
| 17 | +if( ! defined( 'ABSPATH' ) ) |
| 18 | + exit; |
| 19 | + |
| 20 | +class WPForms_Conditional_Smart_Tags |
| 21 | +{ |
| 22 | + /** |
| 23 | + * Constructor to hook into WPForms smart tag processing. |
| 24 | + */ |
| 25 | + public function __construct() |
| 26 | + { |
| 27 | + add_filter( 'wpforms_process_smart_tags', [ $this, 'process_if_else_smart_tags' ], 10, 4 ); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Callback to parse {if field_id="X" value="Y"} ... {else} ... {/if}. |
| 32 | + * |
| 33 | + * @param string $content The content (notification, confirmation) containing smart tags. |
| 34 | + * @param array $form_data Data about the current form. |
| 35 | + * @param array $fields Submitted fields data. |
| 36 | + * @param int $entry_id The current entry ID. |
| 37 | + * |
| 38 | + * @return string Processed content with if/else blocks replaced accordingly. |
| 39 | + */ |
| 40 | + public function process_if_else_smart_tags( $content, $form_data, $fields, $entry_id ) |
| 41 | + { |
| 42 | + $max_iterations = 10; // Prevent infinite loops |
| 43 | + $iteration = 0; |
| 44 | + |
| 45 | + while(strpos($content, '{if') !== false && $iteration < $max_iterations) |
| 46 | + { |
| 47 | + $content = $this->process_innermost_if($content, $fields); |
| 48 | + $iteration++; |
| 49 | + } |
| 50 | + |
| 51 | + return $content; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Process the innermost if statement in the content. |
| 56 | + * |
| 57 | + * @param string $content The content to process |
| 58 | + * @param array $fields The form fields |
| 59 | + * @return string Processed content |
| 60 | + */ |
| 61 | + private function process_innermost_if($content, $fields) |
| 62 | + { |
| 63 | + // Find the innermost {if} that doesn't contain another {if} |
| 64 | + $pattern = '/\{if\s+field_id="(\d+)"\s+value="([^"]*)"\}((?:(?!\{if).)*?)(?:\{else\}((?:(?!\{if).)*?))?\{\/if\}/s'; |
| 65 | + |
| 66 | + return preg_replace_callback($pattern, function($matches) use ($fields) { |
| 67 | + $field_id = $matches[1]; |
| 68 | + $required_value = $matches[2]; |
| 69 | + $if_content = $matches[3]; |
| 70 | + $else_content = isset($matches[4]) ? $matches[4] : ''; |
| 71 | + |
| 72 | + // Get the submitted value, if present |
| 73 | + $submitted_value = ''; |
| 74 | + if(isset($fields[$field_id]['value'])) |
| 75 | + $submitted_value = trim($fields[$field_id]['value']); |
| 76 | + |
| 77 | + // Compare and return if_content or else_content |
| 78 | + return ($submitted_value === $required_value) ? $if_content : $else_content; |
| 79 | + }, $content); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +$wpfcst_instance = new WPForms_Conditional_Smart_Tags(); |
0 commit comments