Overview

Namespaces

  • None
  • Quform
    • Traduttore_Registry

Classes

  • Quform
  • Quform_Admin_InsertForm
  • Quform_Admin_Page
  • Quform_Admin_Page_Controller
  • Quform_Admin_Page_Dashboard
  • Quform_Admin_Page_Entries
  • Quform_Admin_Page_Entries_Edit
  • Quform_Admin_Page_Entries_List
  • Quform_Admin_Page_Entries_View
  • Quform_Admin_Page_Factory
  • Quform_Admin_Page_Forms_Add
  • Quform_Admin_Page_Forms_Edit
  • Quform_Admin_Page_Forms_List
  • Quform_Admin_Page_Help
  • Quform_Admin_Page_Preview
  • Quform_Admin_Page_Settings
  • Quform_Admin_Page_Tools
  • Quform_Admin_Page_Tools_ExportEntries
  • Quform_Admin_Page_Tools_ExportForm
  • Quform_Admin_Page_Tools_Home
  • Quform_Admin_Page_Tools_ImportForm
  • Quform_Admin_Page_Tools_Migrate
  • Quform_Admin_Page_Tools_Uninstall
  • Quform_Api
  • Quform_Block
  • Quform_Builder
  • Quform_Captcha
  • Quform_ClassLoader
  • Quform_Confirmation
  • Quform_Container
  • Quform_Dashboard_Widget
  • Quform_Dispatcher
  • Quform_Element
  • Quform_Element_Captcha
  • Quform_Element_Checkbox
  • Quform_Element_Column
  • Quform_Element_Container
  • Quform_Element_Container_Iterator
  • Quform_Element_Date
  • Quform_Element_Email
  • Quform_Element_Factory
  • Quform_Element_Field
  • Quform_Element_File
  • Quform_Element_Group
  • Quform_Element_Hidden
  • Quform_Element_Honeypot
  • Quform_Element_Html
  • Quform_Element_Multi
  • Quform_Element_Multiselect
  • Quform_Element_Name
  • Quform_Element_Page
  • Quform_Element_Password
  • Quform_Element_Radio
  • Quform_Element_Recaptcha
  • Quform_Element_Row
  • Quform_Element_Select
  • Quform_Element_Submit
  • Quform_Element_Text
  • Quform_Element_Textarea
  • Quform_Element_Time
  • Quform_Entry_Controller
  • Quform_Entry_Exporter
  • Quform_Entry_List_Settings
  • Quform_Entry_List_Table
  • Quform_Entry_Processor
  • Quform_Entry_UserSearcher
  • Quform_Filter_Abstract
  • Quform_Filter_Alpha
  • Quform_Filter_AlphaNumeric
  • Quform_Filter_Digits
  • Quform_Filter_Regex
  • Quform_Filter_Static
  • Quform_Filter_StripTags
  • Quform_Filter_Trim
  • Quform_Form
  • Quform_Form_Controller
  • Quform_Form_Exporter
  • Quform_Form_Factory
  • Quform_Form_Importer
  • Quform_Form_Iterator
  • Quform_Form_List_Settings
  • Quform_Form_List_Table
  • Quform_Form_Processor
  • Quform_License
  • Quform_Migrator
  • Quform_NonceRefresher
  • Quform_Notification
  • Quform_Notification_Resender
  • Quform_Options
  • Quform_Permissions
  • Quform_Repository
  • Quform_ScriptLoader
  • Quform_Session
  • Quform_Settings
  • Quform_Shortcode
  • Quform_Themes
  • Quform_TokenReplacer
  • Quform_Toolbar
  • Quform_Translations
  • Quform_Updater
  • Quform_Upgrader
  • Quform_Uploader
  • Quform_Validator_Abstract
  • Quform_Validator_Alpha
  • Quform_Validator_AlphaNumeric
  • Quform_Validator_Array
  • Quform_Validator_Captcha
  • Quform_Validator_Date
  • Quform_Validator_Digits
  • Quform_Validator_Duplicate
  • Quform_Validator_Email
  • Quform_Validator_FileUpload
  • Quform_Validator_GreaterThan
  • Quform_Validator_Honeypot
  • Quform_Validator_Identical
  • Quform_Validator_InArray
  • Quform_Validator_Length
  • Quform_Validator_LessThan
  • Quform_Validator_Recaptcha
  • Quform_Validator_Regex
  • Quform_Validator_Required
  • Quform_Validator_Static
  • Quform_Validator_Time
  • Quform_View
  • Quform_ViewFactory
  • Quform_Widget_Form
  • Quform_Widget_Popup

Interfaces

  • Quform_Attachable
  • Quform_Element_Editable
  • Quform_Filter_Interface
  • Quform_Validator_Interface

Constants

  • Quform\Traduttore_Registry\TRANSIENT_KEY_PLUGIN
  • Quform\Traduttore_Registry\TRANSIENT_KEY_THEME

Functions

  • Quform\Traduttore_Registry\add_project
  • Quform\Traduttore_Registry\clean_translations_cache
  • Quform\Traduttore_Registry\get_available_locales
  • Quform\Traduttore_Registry\get_installed_translations
  • Quform\Traduttore_Registry\get_translations
  • Quform\Traduttore_Registry\register_clean_translations_cache
  • Quform\Traduttore_Registry\sanitize_date
  • Overview
  • Namespace
  • Class
  1: <?php
  2: 
  3: /**
  4:  * @copyright Copyright (c) 2009-2022 ThemeCatcher (https://www.themecatcher.net)
  5:  */
  6: class Quform_TokenReplacer
  7: {
  8:     /**
  9:      * Newline for email content
 10:      *
 11:      * @var string
 12:      */
 13:     const NEWLINE = "\r\n";
 14: 
 15:     /**
 16:      * @var Quform_Options
 17:      */
 18:     protected $options;
 19: 
 20:     /**
 21:      * @param Quform_Options $options
 22:      */
 23:     public function __construct(Quform_Options $options)
 24:     {
 25:         $this->options = $options;
 26:     }
 27: 
 28:     /**
 29:      * Replace variables in the given text that are unaffected by form submission
 30:      *
 31:      * @param   string       $text
 32:      * @param   string       $format
 33:      * @param   Quform_Form  $form
 34:      * @return  string
 35:      */
 36:     public function replaceVariablesPreProcess($text, $format, Quform_Form $form)
 37:     {
 38:         if ( ! Quform::isNonEmptyString($text)) {
 39:             return '';
 40:         }
 41: 
 42:         if (strpos($text, '{') === false) {
 43:             return $text;
 44:         }
 45: 
 46:         return preg_replace_callback('/({(.+?)})/', function ($matches) use ($format, $form) {
 47:             return $this->replaceVariablePreProcess($matches, $format, $form);
 48:         }, $text);
 49:     }
 50: 
 51:     /**
 52:      * Handle the callback for replaceVariablesPreProcess
 53:      *
 54:      * @param   array        $matches  The matches array
 55:      * @param   string       $format   The format of the replacements: 'text', 'html', 'url' or 'rawurl'
 56:      * @param   Quform_Form  $form     The form instance
 57:      * @return  string
 58:      */
 59:     protected function replaceVariablePreProcess($matches, $format, Quform_Form $form)
 60:     {
 61:         $replaced = $matches[1];
 62:         $token = $this->parseToken($matches[2]);
 63: 
 64:         switch ($token['name']) {
 65:             case 'site_title':
 66:                 $replaced = get_bloginfo('name');
 67:                 break;
 68:             case 'site_tagline':
 69:                 $replaced = get_bloginfo('description');
 70:                 break;
 71:             case 'ip':
 72:                 $replaced = Quform::getClientIp();
 73:                 break;
 74:             case 'post':
 75:                 $replaced = Quform::getPostProperty(count($token['params']) ? key($token['params']) : 'ID');
 76:                 break;
 77:             case 'custom_field':
 78:                 $replaced = Quform::getPostMeta(count($token['params']) ? key($token['params']) : '');
 79:                 break;
 80:             case 'url':
 81:                 $replaced = Quform::getCurrentUrl();
 82:                 break;
 83:             case 'user':
 84:                 $replaced = Quform::getUserProperty(count($token['params']) ? key($token['params']) : 'display_name');
 85:                 break;
 86:             case 'user_meta':
 87:                 $replaced = Quform::getUserMeta(count($token['params']) ? key($token['params']) : '');
 88:                 break;
 89:             case 'referring_url': // TODO should this use entry source url if set
 90:                 $replaced = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
 91:                 break;
 92:             case 'user_agent':
 93:                 $replaced = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
 94:                 break;
 95:             case 'date':
 96:                 if (isset($token['params']['format'])) {
 97:                     $format = $token['params']['format'];
 98:                 } else {
 99:                     $format = $this->getDateFormat($form);
100:                 }
101: 
102:                 $replaced = Quform::date($format);
103:                 break;
104:             case 'time':
105:                 if (isset($token['params']['format'])) {
106:                     $format = $token['params']['format'];
107:                 } else {
108:                     $format = $this->getTimeFormat($form);
109:                 }
110: 
111:                 $replaced = Quform::date($format);
112:                 break;
113:             case 'datetime':
114:                 if (isset($token['params']['format'])) {
115:                     $format = $token['params']['format'];
116:                 } else {
117:                     $format = $this->getDateTimeFormat($form);
118:                 }
119: 
120:                 $replaced = Quform::date($format);
121:                 break;
122:             case 'uniqid':
123:                 $prefix = isset($token['params']['prefix']) ? $token['params']['prefix'] : '';
124:                 $moreEntropy = isset($token['params']['moreEntropy']) && $token['params']['moreEntropy'] === 'true';
125: 
126:                 $replaced = uniqid($prefix, $moreEntropy);
127:                 break;
128:         }
129: 
130:         $replaced = apply_filters('quform_replace_variables_pre_process', $replaced, $token, $format);
131: 
132:         if ($format == 'url') {
133:             $replaced = urlencode($replaced);
134:         } elseif ($format == 'rawurl') {
135:             $replaced = rawurlencode($replaced);
136:         }
137: 
138:         return $replaced;
139:     }
140: 
141:     /**
142:      * Get the format for the {date} variable
143:      *
144:      * @param   Quform_Form  $form  The form instance
145:      * @return  string
146:      */
147:     protected function getDateFormat(Quform_Form $form)
148:     {
149:         $format = $form->getDateFormat();
150: 
151:         if ( ! Quform::isNonEmptyString($format)) {
152:             $locale = Quform::getLocale($form->getLocale());
153:             $format = $locale['dateFormat'];
154:         }
155: 
156:         return $format;
157:     }
158: 
159:     /**
160:      * Get the format for the {time} variable
161:      *
162:      * @param   Quform_Form  $form  The form instance
163:      * @return  string
164:      */
165:     protected function getTimeFormat(Quform_Form $form)
166:     {
167:         $format = $form->getTimeFormat();
168: 
169:         if ( ! Quform::isNonEmptyString($format)) {
170:             $locale = Quform::getLocale($form->getLocale());
171:             $format = $locale['timeFormat'];
172:         }
173: 
174:         return $format;
175:     }
176: 
177:     /**
178:      * Get the format for the {datetime} variable
179:      *
180:      * @param   Quform_Form  $form  The form instance
181:      * @return string
182:      */
183:     protected function getDateTimeFormat(Quform_Form $form)
184:     {
185:         $format = $form->getDateTimeFormat();
186: 
187:         if ( ! Quform::isNonEmptyString($format)) {
188:             $locale = Quform::getLocale($form->getLocale());
189:             $format = $locale['dateTimeFormat'];
190:         }
191: 
192:         return $format;
193:     }
194: 
195:     /**
196:      * Replace all variables in the given text
197:      *
198:      * @param   string       $text    The text to check for replacements
199:      * @param   string       $format  The format of the replacements: 'text', 'html', 'url' or 'rawurl'
200:      * @param   Quform_Form  $form    The form instance
201:      * @return  string                The text with variables replaced
202:      */
203:     public function replaceVariables($text, $format, Quform_Form $form)
204:     {
205:         if ( ! Quform::isNonEmptyString($text)) {
206:             return '';
207:         }
208: 
209:         if (strpos($text, '{') === false) {
210:             return $text;
211:         }
212: 
213:         return preg_replace_callback('/({(.+?)})/', function ($matches) use ($format, $form) {
214:             return $this->replaceVariable($matches, $format, $form);
215:         }, $text);
216:     }
217: 
218:     /**
219:      * Handle the callback for replaceVariables
220:      *
221:      * @param   array        $matches  The matches array
222:      * @param   string       $format   The format of the replacements: 'text', 'html', 'url' or 'rawurl'
223:      * @param   Quform_Form  $form     The form instance
224:      * @return  string
225:      */
226:     protected function replaceVariable($matches, $format, Quform_Form $form)
227:     {
228:         $replaced = $original = $matches[1];
229:         $token = $this->parseToken($matches[2]);
230: 
231:         switch ($token['name']) {
232:             case 'post':
233:                 $replaced = Quform::getPostProperty(count($token['params']) ? key($token['params']) : 'ID', Quform::get($_POST, 'post_id'));
234:                 break;
235:             case 'custom_field':
236:                 $replaced = Quform::getPostMeta(count($token['params']) ? key($token['params']) : '', Quform::get($_POST, 'post_id'));
237:                 break;
238:             case 'referring_url':
239:                 $replaced = Quform::get($_POST, 'referring_url');
240:                 break;
241:             case 'default_email_address':
242:                 $replaced = $this->options->get('defaultEmailAddress');
243:                 break;
244:             case 'default_email_name':
245:                 $replaced = $this->options->get('defaultEmailName');
246:                 break;
247:             case 'default_from_email_address':
248:                 $replaced = $this->options->get('defaultFromEmailAddress');
249:                 break;
250:             case 'default_from_email_name':
251:                 $replaced = $this->options->get('defaultFromEmailName');
252:                 break;
253:             case 'admin_email':
254:                 $replaced = get_bloginfo('admin_email');
255:                 break;
256:             case 'element':
257:                 $replaced = $this->replaceElement($token, $format, $form);
258:                 break;
259:             case 'form_name':
260:                 $replaced = $form->config('name');
261:                 break;
262:             case 'entry_id':
263:                 $replaced = $form->getEntryId();
264:                 break;
265:             case 'all_form_data':
266:                 $replaced = $this->replaceAllSubmittedData($token, $format, $form);
267:                 break;
268:         }
269: 
270:         if ($replaced === $original) {
271:             $replaced = $this->replaceVariablesPreProcess($original, $format, $form);
272:         } else {
273:             $replaced = apply_filters('quform_replace_variables', $replaced, $token, $format, $form);
274: 
275:             if ($format == 'url') {
276:                 $replaced = urlencode($replaced);
277:             } elseif ($format == 'rawurl') {
278:                 $replaced = rawurlencode($replaced);
279:             }
280:         }
281: 
282:         return $replaced;
283:     }
284: 
285:     /**
286:      * Replace the element placeholder with its value
287:      *
288:      * @param   array        $token   The token parts
289:      * @param   string       $format  The format of the replacements: 'text', 'html', 'url' or 'rawurl'
290:      * @param   Quform_Form  $form    The form instance
291:      * @return  string
292:      */
293:     protected function replaceElement($token, $format, Quform_Form $form)
294:     {
295:         $value = '';
296: 
297:         if (isset($token['params']['id'])) {
298:             $id = $token['params']['id'];
299:             $part = null;
300: 
301:             if (strpos($id, '.') !== false) {
302:                 list($id, $part) = explode('.', $id, 2);
303:             }
304: 
305:             $element = $form->getElementById($id);
306: 
307:             if ($element instanceof Quform_Element_Field && ! $element->isConditionallyHidden()) {
308:                 $format = isset($token['params']['format']) ? $token['params']['format'] : $format;
309:                 $separator = isset($token['params']['separator']) ? $token['params']['separator'] : ', ';
310: 
311:                 if (Quform::isNonEmptyString($part)) {
312:                     // Get a single part from an array value
313:                     $elementValue = $element->getValue();
314: 
315:                     if (is_array($elementValue)) {
316:                         $partValue = Quform::get($elementValue, $part, '');
317: 
318:                         if (is_scalar($partValue)) {
319:                             $value = $format == 'html' ? Quform::escape($partValue) : $partValue;
320:                         }
321:                     }
322:                 } else {
323:                     $value = $format == 'html' ? $element->getValueHtml() : $element->getValueText($separator);
324:                 }
325: 
326:                 $value = apply_filters('quform_element_token_value_' . $element->getIdentifier(), $value, $element, $format, $separator, $token);
327:             }
328:         }
329: 
330:         return $value;
331:     }
332: 
333:     /**
334:      * Replace the token to display all submitted form data
335:      *
336:      * @param   array        $token   The token parts
337:      * @param   string       $format  The format of the replacements: 'text', 'html', 'url' or 'rawurl'
338:      * @param   Quform_Form  $form    The form instance
339:      * @return  string
340:      */
341:     protected function replaceAllSubmittedData($token, $format, Quform_Form $form)
342:     {
343:         $showEmptyFields = isset($token['params']['showEmptyFields']) && $token['params']['showEmptyFields'] === 'true';
344: 
345:         if ($format == 'html') {
346:             $content = $this->renderAllSubmittedDataHtml($showEmptyFields, $form);
347:         } else {
348:             $content = $this->renderAllSubmittedDataText($showEmptyFields, $form);
349:         }
350: 
351:         return $content;
352:     }
353: 
354:     /**
355:      * Render the submitted form data in plain text
356:      *
357:      * @param   bool         $showEmptyFields  Whether to show empty fields
358:      * @param   Quform_Form  $form             The form instance
359:      * @return  string
360:      */
361:     protected function renderAllSubmittedDataText($showEmptyFields, Quform_Form $form)
362:     {
363:         $content = '';
364: 
365:         foreach ($form->getRecursiveIterator(RecursiveIteratorIterator::SELF_FIRST) as $element) {
366:             if ( ! $element instanceof Quform_Element_Field && ! $element instanceof Quform_Element_Container && ! $element instanceof Quform_Element_Html) {
367:                 continue;
368:             }
369: 
370:             // Skip hidden elements
371:             if ($element->isHidden()) {
372:                 continue;
373:             }
374: 
375:             // Skip empty elements
376:             if ($element->isEmpty() && ! $showEmptyFields) {
377:                 continue;
378:             }
379: 
380:             if ($element instanceof Quform_Element_Html) {
381:                 if ($element->config('showInEmail')) {
382:                     $content .= $element->getContent('text');
383:                     $content .= self::NEWLINE . self::NEWLINE;
384:                 }
385:             } else if ($element instanceof Quform_Element_Group) {
386:                 if ($element->config('showLabelInEmail') && Quform::isNonEmptyString($label = $element->getLabel())) {
387:                     $content .= str_repeat('=', 25) . self::NEWLINE;
388:                     $content .= $label . self::NEWLINE;
389:                     $content .= str_repeat('=', 25);
390:                     $content .= self::NEWLINE . self::NEWLINE;
391:                 }
392:             } else if ($element instanceof Quform_Element_Field) {
393:                 if ($element->config('showInEmail')) {
394:                     $content .= $element->getAdminLabel() . self::NEWLINE;
395:                     $content .= str_repeat('-', 25) . self::NEWLINE;
396:                     $content .= $element->getValueText(self::NEWLINE);
397:                     $content .= self::NEWLINE . self::NEWLINE;
398:                 }
399:             }
400:         }
401: 
402:         $content = apply_filters('quform_all_form_data_text', $content, $form, $showEmptyFields);
403: 
404:         return $content;
405:     }
406: 
407:     /**
408:      * Render the HTML for the table containing all submitted form data
409:      *
410:      * @param   bool         $showEmptyFields  Whether to show empty fields
411:      * @param   Quform_Form  $form             The form instance
412:      * @return  string
413:      */
414:     protected function renderAllSubmittedDataHtml($showEmptyFields, Quform_Form $form)
415:     {
416:         $content = '<table width="100%" cellpadding="10" cellspacing="0" border="0" style="table-layout: fixed; background: #ffffff; border-spacing: 0; border-collapse: separate; border-bottom: 1px solid #d4d4d4; box-shadow: 0 2px 7px 0 rgba(0, 0, 0, 0.07);">' . self::NEWLINE;
417: 
418:         foreach ($form->getRecursiveIterator(RecursiveIteratorIterator::SELF_FIRST) as $element) {
419:             if ( ! $element instanceof Quform_Element_Field && ! $element instanceof Quform_Element_Container && ! $element instanceof Quform_Element_Html) {
420:                 continue;
421:             }
422: 
423:             // Skip hidden elements
424:             if ($element->isHidden()) {
425:                 continue;
426:             }
427: 
428:             // Skip empty elements
429:             if ($element->isEmpty() && ! $showEmptyFields) {
430:                 continue;
431:             }
432: 
433:             if ($element instanceof Quform_Element_Html) {
434:                 if ($element->config('showInEmail')) {
435:                     $content .= '<tr><td valign="top" style="padding: 10px; font-family: Helvetica, Arial, sans-serif; font-size: 16px; color: #282828; line-height: 130%; border: 1px solid #d4d4d4; border-bottom: 0; word-wrap: break-word;">' . $element->getContent() . '</td></tr>' . self::NEWLINE;
436:                 }
437:             } else if ($element instanceof Quform_Element_Page) {
438:                 if ($element->config('showLabelInEmail') && Quform::isNonEmptyString($label = $element->getLabel())) {
439:                     $content .= '<tr><td valign="top" style="padding: 10px; font-family: Helvetica, Arial, sans-serif; font-size: 22px; font-weight: bold; background-color: #c73412; color: #ffffff; border-bottom: 1px solid #e14e2c; word-wrap: break-word;">' . esc_html($label) . '</td></tr>' . self::NEWLINE;
440:                 }
441:             }  else if ($element instanceof Quform_Element_Group) {
442:                 if ($element->config('showLabelInEmail') && Quform::isNonEmptyString($label = $element->getLabel())) {
443:                     $content .= '<tr><td valign="top" style="padding: 10px; font-family: Helvetica, Arial, sans-serif; font-size: 17px; background-color: #c73412; color: #ffffff; word-wrap: break-word;">' . esc_html($label) . '</td></tr>' . self::NEWLINE;
444:                 }
445:             } else if ($element instanceof Quform_Element_Field) {
446:                 if ($element->config('showInEmail')) {
447:                     $content .= '<tr bgcolor="#efefef"><td valign="top" style="padding: 10px; font-family: Helvetica, Arial, sans-serif; font-size: 15px; font-weight: bold; color: #282828; border: 1px solid #d4d4d4; border-bottom: 0; word-wrap: break-word;">' . esc_html($element->getAdminLabel()) . '</td></tr>' . self::NEWLINE;
448:                     $content .= '<tr bgcolor="#fcfcfc"><td valign="top" style="padding: 10px; font-family: Helvetica, Arial, sans-serif; font-size: 14px; color: #282828; line-height: 130%; border: 1px solid #d4d4d4; border-bottom-color: #fff; word-wrap: break-word;">' . $element->getValueHtml() . '</td></tr>' . self::NEWLINE;
449:                 }
450:             }
451:         }
452: 
453:         $content .= '</table>' . self::NEWLINE;
454: 
455:         $content = apply_filters('quform_all_form_data_html', $content, $form, $showEmptyFields);
456: 
457:         return $content;
458:     }
459: 
460:     /**
461:      * Parse a single token into an array with the name and parameters
462:      *
463:      * @param   string  $token
464:      * @return  array
465:      */
466:     protected function parseToken($token)
467:     {
468:         $parts = explode('|', $token);
469: 
470:         // Remove token name and leave params in $parts
471:         $name = trim(array_shift($parts));
472: 
473:         // Build the params array
474:         $params = array();
475:         foreach ($parts as $part) {
476:             $paramParts = explode(':', $part, 2);
477:             $params[$paramParts[0]] = isset($paramParts[1]) ? $paramParts[1] : true; // A parameter without a value is just "true"
478:         }
479: 
480:         return array(
481:             'name' => $name,
482:             'params' => $params
483:         );
484:     }
485: }
486: 
API documentation generated by ApiGen