'; $html .= static::getStyles($form); $html .= ''; return $html; } public static function renderHTML($form = array()) { if(isset($form['data']['body']) && !empty($form['data']['body'])) { // render blocks return static::renderBlocks($form['data']['body']); } return ''; } public static function getStyles($form = array()) { $default = << 0) { return strip_tags($form['data']['styles']); } else { return $default; } } // public: date related methods public static function getDateTypes() { return array( 'year_month_day' => __('Year, month, day'), 'year_month' => __('Year, month'), 'month' => __('Month (January, February,...)'), 'year' => __('Year') ); } public static function getDateFormats() { return array( 'year_month_day' => array('mm/dd/yyyy', 'dd/mm/yyyy', 'yyyy/mm/dd'), 'year_month' => array('mm/yyyy', 'yyyy/mm'), 'year' => array('yyyy'), 'month' => array('mm') ); } public static function getMonthNames() { return array( __('January'), __('February'), __('March'), __('April'), __('May'), __('June'), __('July'), __('August'), __('September'), __('October'), __('November'), __('December') ); } // private: rendering methods private static function renderBlocks($blocks = array()) { $html = ''; // generate block foreach ($blocks as $key => $block) { $html .= static::renderBlock($block)."\n"; } return $html; } private static function renderBlock($block = array()) { // get field name $field_name = static::getFieldName($block); // set label $field_label = (isset($block['params']['label'])) ? $block['params']['label'] : ''; $html = ''; // render block template depending on its type switch ($block['type']) { case 'html': if(isset($block['params']['text']) && $block['params']['text']) { $text = html_entity_decode($block['params']['text'], ENT_QUOTES); } if(isset($block['params']['nl2br']) && $block['params']['nl2br']) { $text = nl2br($text); } $html .= '

'; $html .= $text; $html .= '

'; break; case 'divider': $html .= '
'; break; case 'checkbox': case 'radio': // BEGIN: container $html .= '

'; // render label $html .= static::renderLabel($block); // create hidden default value // $html .= ''; // display values foreach($block['params']['values'] as $value) { $is_checked = (isset($value['is_checked']) && $value['is_checked']) ? 'checked="checked"' : ''; if($block['type'] === 'radio') { $html .= '

'; break; case 'input': // BEGIN: container $html .= '

'; // render label $html .= static::renderLabel($block); $html .= ''; break; default: $html .= $block['type']; break; } return $html; } private static function getInputValidation($block) { return 'data-validation-engine="'.static::getInputValidationRules($block).'"'; } private static function getInputValidationRules($block) { $rules = array(); // if it's the email field, it's mandatory and needs to be valid if($block['field'] === 'email') { $rules[] = 'required'; $rules[] = 'custom[email]'; } // if it's the list field, at least one option needs to be selected if($block['field'] === 'list') { $rules[] = 'required'; } // check if the field is required if(isset($block['params']['required']) && (bool)$block['params']['required'] === true) { $rules[] = 'required'; } // check for validation rules if(isset($block['params']['validate'])) { if(is_array($block['params']['validate'])) { // handle multiple validation rules foreach($block['params']['validate'] as $rule) { $rules[] = 'custom['.$rule.']'; } } else if(strlen(trim($block['params']['validate'])) > 0) { // handle single validation rule $rules[] = 'custom['.$block['params']['validate'].']'; } } // generate string if there is at least one rule to validate against if(empty($rules)) { return ''; } else { // make sure rules are not duplicated $rules = array_unique($rules); return 'validate['.join(',', $rules).']'; } } private static function renderLabel($block) { $html = ''; // if the label is displayed as a placeholder, we don't display a label outside if(isset($block['params']['label_within']) && $block['params']['label_within']) { return $html; } if(isset($block['params']['label']) && strlen(trim($block['params']['label'])) > 0) { $html .= ''; } return $html; } private static function renderInputPlaceholder($block) { $html = ''; // if the label is displayed as a placeholder, if(isset($block['params']['label_within']) && $block['params']['label_within']) { // display only label if(isset($block['params']['label']) && strlen(trim($block['params']['label'])) > 0) { $html .= ' placeholder="'; $html .= $block['params']['label']; // add an asterisk if it's a required field if(isset($block['params']['required']) && $block['params']['required']) { $html .= ' *'; } $html .= '" '; } } return $html; } // return field name depending on block data private static function getFieldName($block = array()) { return $block['field']; } // render block wrapper's header private static function renderBlockHeader() { $html = ''; $html .= '

'; return $html; } // render block wrapper's footer private static function renderBlockFooter() { $html = ''; // END: container $html .= '

'; return $html; } // css inlining private static function getMonths($block = array()) { $defaults = array( 'selected' => null ); // is default today if(isset($block['params']['is_default_today']) && (bool)$block['params']['is_default_today'] === true) { $defaults['selected'] = (int)strftime('%m'); } // merge block with defaults $block = array_merge($defaults, $block); $month_names = static::getMonthNames(); $html = ''; for($i = 1; $i < 13; $i++) { $is_selected = ($i === $block['selected']) ? 'selected="selected"' : ''; $html .= ''; } return $html; } private static function getYears($block = array()) { $defaults = array( 'selected' => null, 'from' => (int)strftime('%Y') - 100, 'to' => (int)strftime('%Y') ); // is default today if(isset($block['params']['is_default_today']) && (bool)$block['params']['is_default_today'] === true) { $defaults['selected'] = (int)strftime('%Y'); } // merge block with defaults $block = array_merge($defaults, $block); $html = ''; // return years as an array for($i = (int)$block['to']; $i > (int)($block['from'] - 1); $i--) { $is_selected = ($i === $block['selected']) ? 'selected="selected"' : ''; $html .= ''; } return $html; } private static function getDays($block = array()) { $defaults = array( 'selected' => null ); // is default today if(isset($block['params']['is_default_today']) && (bool)$block['params']['is_default_today'] === true) { $defaults['selected'] = (int)strftime('%d'); } // merge block with defaults $block = array_merge($defaults, $block); $html = ''; // return days as an array for($i = 1; $i < 32; $i++) { $is_selected = ($i === $block['selected']) ? 'selected="selected"' : ''; $html .= ''; } return $html; } private static function renderDateSelect($field_name, $block = array()) { $html = ''; // get date format $date_formats = static::getDateFormats(); // automatically select first date format $date_format = $date_formats[$block['params']['date_type']][0]; // set date format if specified if(isset($block['params']['date_order']) && strlen(trim($block['params']['date_order'])) > 0) { $date_format = $block['params']['date_order']; } // generate an array of selectors based on date format $date_selectors = explode('/', $date_format); // render each date selector foreach($date_selectors as $date_selector) { if($date_selector === 'dd') { // render days $html .= ''; } else if($date_selector === 'mm') { // render months $html .= ''; } else if($date_selector === 'yyyy') { // render years $html .= ''; } } return $html; } public static function getExports($form = null) { return array( 'html' => static::getExport('html', $form), 'php' => static::getExport('php', $form), 'iframe' => static::getExport('iframe', $form), 'shortcode' => static::getExport('shortcode', $form), ); } public static function getExport($type = 'html', $form = null) { switch($type) { case 'iframe': // generate url to load iframe's content $iframe_url = add_query_arg(array( 'mailpoet_page' => 'mailpoet_form_iframe', 'mailpoet_form' => $form['form'] ), site_url()); // generate iframe return ''; break; case 'php': $output = array( '$form_widget = new \MailPoet\Form\Widget();', 'echo $form_widget->widget(array(\'form\' => '.(int)$form['form'].', \'form_type\' => \'php\'));' ); return join("\n", $output); break; case 'html': // TODO: get locale setting in order to load translations $wp_locale = \get_locale(); $output = array(); $output[] = ''; // jQuery $output[] = ''; // (JS) form validation $output[] = ''; $output[] = ''; // (CSS) form validation styles $output[] = ''; // (JS) form submission $output[] = ''; // (JS) variables... $output[] = ''; $output[] = ''; $form_widget = new Widget(); $output[] = $form_widget->widget(array( 'form' => (int)$form['form'], 'form_type' => 'php' )); return join("\n", $output); break; case 'shortcode': return '[mailpoet_form id="'.(int)$form['form'].'"]'; break; } } }