WYSIJA_help_form_engine

The MailPoet Newsletters WYSIJA help form engine class.

Defined (1)

The class is defined in the following location(s).

/helpers/form_engine.php  
  1. class WYSIJA_help_form_engine extends WYSIJA_object { 
  2. // debug mode 
  3. private $_debug = false; 
  4.  
  5. // rendering context (editor, web) 
  6. private $_context = 'editor'; 
  7. // rendering mode (live, preview) 
  8. private $_mode = 'live'; 
  9.  
  10. // data holders 
  11. private $_data = null; 
  12. private $_styles = null; 
  13. private $_lists = null; 
  14. private $_fields = null; 
  15.  
  16. // static form fields 
  17. private $_static_fields = array('email', 'submit'); 
  18.  
  19. // unique form fields 
  20. private $_unique_fields = array('firstname', 'lastname', 'list'); 
  21.  
  22. // constructor 
  23. function __construct() { 
  24. parent::__construct(); 
  25.  
  26.  
  27. // i18n methods 
  28. public function get_translations() { 
  29. return array( 
  30. 'savingnl' => __('Saving form...', WYSIJA),  
  31. 'save' => __('Save', WYSIJA),  
  32. 'edit_settings' => __('Edit display', WYSIJA),  
  33. 'list_cannot_be_empty' => __('You have to select at least 1 list', WYSIJA),  
  34. 'not_enough_options' => __('Your subscriber needs at least 2 options to select from', WYSIJA),  
  35. 'missing_checkbox_label' => __('You need to specify a value for your checkbox', WYSIJA),  
  36. 'add_field' => __('Add new field', WYSIJA),  
  37. 'edit_field' => __('Edit custom field', WYSIJA),  
  38. 'delete_field_confirmation' => __('Are you sure you want to delete this custom field?', WYSIJA),  
  39. 'date_select_year' => __('Year', WYSIJA),  
  40. 'date_select_month' => __('Month', WYSIJA),  
  41. 'date_select_day' => __('Day', WYSIJA) 
  42. ); 
  43.  
  44. public function get_months($options = array()) { 
  45.  
  46. $defaults = array( 
  47. 'selected' => null 
  48. ); 
  49. // is default today 
  50. if(isset($options['params']['is_default_today']) && (bool)$options['params']['is_default_today'] === true) { 
  51. $defaults['selected'] = (int)strftime('%m'); 
  52.  
  53. // merge options with defaults 
  54. $options = array_merge($defaults, $options); 
  55.  
  56. $month_names = array( 
  57. __('January', WYSIJA),  
  58. __('February', WYSIJA),  
  59. __('March', WYSIJA),  
  60. __('April', WYSIJA),  
  61. __('May', WYSIJA),  
  62. __('June', WYSIJA),  
  63. __('July', WYSIJA),  
  64. __('August', WYSIJA),  
  65. __('September', WYSIJA),  
  66. __('October', WYSIJA),  
  67. __('November', WYSIJA),  
  68. __('December', WYSIJA) 
  69. ); 
  70.  
  71. $months = array(); 
  72. for($i = 0; $i < 12; $i++) { 
  73. $months[] = array( 
  74. 'month' => ($i + 1),  
  75. 'month_name' => $month_names[$i],  
  76. 'is_selected' => (($i + 1) === $options['selected']) 
  77. ); 
  78.  
  79. return $months; 
  80.  
  81. public function get_years($options = array()) { 
  82. $defaults = array( 
  83. 'selected' => null,  
  84. 'from' => (int)strftime('%Y') - 100,  
  85. 'to' => (int)strftime('%Y') 
  86. ); 
  87. // is default today 
  88. if(isset($options['params']['is_default_today']) && (bool)$options['params']['is_default_today'] === true) { 
  89. $defaults['selected'] = (int)strftime('%Y'); 
  90.  
  91. // merge options with defaults 
  92. $options = array_merge($defaults, $options); 
  93.  
  94. $years = array(); 
  95.  
  96. // return years as an array 
  97. for($i = (int)$options['from']; $i < (int)($options['to'] + 1); $i++) { 
  98. $years[] = array( 
  99. 'year' => $i,  
  100. 'is_selected' => ($i === $options['selected']) 
  101. ); 
  102.  
  103. return array_reverse($years); 
  104.  
  105. public function get_days($options = array()) { 
  106. $defaults = array( 
  107. 'selected' => null 
  108. ); 
  109. // is default today 
  110. if(isset($options['params']['is_default_today']) && (bool)$options['params']['is_default_today'] === true) { 
  111. $defaults['selected'] = (int)strftime('%d'); 
  112.  
  113. // merge options with defaults 
  114. $options = array_merge($defaults, $options); 
  115.  
  116. $days = array(); 
  117.  
  118. // return days as an array 
  119. for($i = 1; $i < 32; $i++) { 
  120. $days[] = array( 
  121. 'day' => $i,  
  122. 'is_selected' => ($i === $options['selected']) 
  123. ); 
  124.  
  125. return $days; 
  126.  
  127. // getters/setters 
  128. public function get_data($type = null) { 
  129. if($type !== null) { 
  130. if(array_key_exists($type, $this->_data)) { 
  131. return $this->_data[$type]; 
  132. } else { 
  133. // return default value 
  134. $defaults = $this->get_default_data(); 
  135. return $defaults[$type]; 
  136. return $this->_data; 
  137.  
  138. public function set_data($value = null, $decode = false) { 
  139. if(!$value) { 
  140. $this->_data = $this->get_default_data(); 
  141. } else { 
  142. $this->_data = $value; 
  143. if($decode) { 
  144. $this->_data = $this->get_decoded('data'); 
  145.  
  146. public function set_lists($lists = array()) { 
  147. $this->_lists = $lists; 
  148.  
  149. public function get_formatted_lists() { 
  150.  
  151. $lists = $this->get_lists(); 
  152. $formatted_lists = array(); 
  153.  
  154. foreach($lists as $list) { 
  155. $formatted_lists[$list['list_id']] = $list['name']; 
  156. return $formatted_lists; 
  157.  
  158. public function get_lists() { 
  159. if($this->_lists === null) { 
  160. // get available lists which users can subscribe to 
  161. $model_list = WYSIJA::get('list', 'model'); 
  162.  
  163. // get lists users can subscribe to (aka "enabled list") 
  164. $lists = $model_list->get(array('name', 'list_id', 'is_public'), array('is_enabled' => 1)); 
  165.  
  166. $this->set_lists($lists); 
  167. return $this->_lists; 
  168.  
  169. private function get_context() { 
  170. return $this->_context; 
  171.  
  172. private function set_context($value = null) { 
  173. if($value !== null) $this->_context = $value; 
  174.  
  175. public function set_mode($value = null) { 
  176. if($value !== null) $this->_mode = $value; 
  177.  
  178. private function get_mode() { 
  179. return $this->_mode; 
  180.  
  181. public function get_encoded($type = 'data') { 
  182. return base64_encode(serialize($this->{'get_'.$type}())); 
  183.  
  184. public function get_decoded($type = 'data') { 
  185. return unserialize(base64_decode($this->{'get_'.$type}())); 
  186.  
  187. private function get_default_data() { 
  188.  
  189. $lists = $this->get_lists(); 
  190.  
  191. // select default list 
  192. $default_list = array(); 
  193. if(!empty($lists)) { 
  194. $default_list[] = $lists[0]['list_id']; 
  195.  
  196. return array( 
  197. 'version' => '0.4',  
  198. 'settings' => array( 
  199. 'on_success' => 'message',  
  200. 'success_message' => __('Check your inbox or spam folder now to confirm your subscription.', WYSIJA),  
  201. 'lists' => $default_list,  
  202. 'lists_selected_by' => 'admin' 
  203. ),  
  204. 'body' => array( 
  205. array( 
  206. 'name' => __('Email', WYSIJA),  
  207. 'type' => 'input',  
  208. 'field' => 'email',  
  209. 'params' => array( 
  210. 'label' => __('Email', WYSIJA),  
  211. 'required' => true 
  212. ),  
  213. array( 
  214. 'name' => __('Submit', WYSIJA),  
  215. 'type' => 'submit',  
  216. 'field' => 'submit',  
  217. 'params' => array( 
  218. 'label' => __('Subscribe!', WYSIJA) 
  219. ); 
  220.  
  221. public function get_setting($key = null) { 
  222. if($key === null) return null; 
  223.  
  224. if($this->is_data_valid() === true) { 
  225. $settings = $this->get_data('settings'); 
  226. if(array_key_exists($key, $settings)) { 
  227. // otherwise, simply return the value 
  228. return $settings[$key]; 
  229. } else { 
  230. return null; 
  231.  
  232. // common methods 
  233. private function is_debug() { 
  234. return ($this->_debug === true); 
  235.  
  236. private function is_data_valid() { 
  237. return ($this->get_data() !== null); 
  238.  
  239. public function get_custom_fields() { 
  240. if($this->_fields === null) { 
  241. // get available custom fields 
  242. $custom_fields = WJ_Field::get_all(array('order_by' => 'name ASC')); 
  243. $user_fields = array(); 
  244.  
  245. if(!empty($custom_fields)) { 
  246. foreach($custom_fields as $custom_field) { 
  247. $user_fields[] = array( 
  248. 'field_id' => $custom_field->id,  
  249. 'name' => $custom_field->name,  
  250. 'column_name' => $custom_field->user_column_name(),  
  251. 'column_type' => $custom_field->type,  
  252. 'params' => $custom_field->settings 
  253. ); 
  254.  
  255. // we need to figure out the default list for the "List selection" widget 
  256. $lists = $this->get_lists(); 
  257.  
  258. // select default list 
  259. $default_list = array(); 
  260. if(!empty($lists)) { 
  261. $default_list[] = array( 
  262. 'list_id' => $lists[0]['list_id'],  
  263. 'is_checked' => 0 
  264. ); 
  265.  
  266. // extra widgets that can be added more than once 
  267. $extra_fields = array( 
  268. array( 
  269. 'name' => __('Divider', WYSIJA),  
  270. 'column_name' => 'divider',  
  271. 'column_type' => 'divider' 
  272. ),  
  273. array( 
  274. 'name' => __('First name', WYSIJA),  
  275. 'column_name' => 'firstname',  
  276. 'column_type' => 'input' 
  277. ),  
  278. array( 
  279. 'name' => __('Last name', WYSIJA),  
  280. 'column_name' => 'lastname',  
  281. 'column_type' => 'input' 
  282. ),  
  283. array( 
  284. 'name' => __('List selection', WYSIJA),  
  285. 'column_name' => 'list',  
  286. 'column_type' => 'list',  
  287. 'params' => array( 
  288. 'label' => __('Select list(s):', WYSIJA),  
  289. 'values' => $default_list 
  290. ),  
  291. array( 
  292. 'name' => __('Random text or HTML', WYSIJA),  
  293. 'column_name' => 'html',  
  294. 'column_type' => 'html',  
  295. 'params' => array( 
  296. 'text' => __('Subscribe to our newsletter and join our [total_subscribers] subscribers.', WYSIJA) 
  297. ); 
  298.  
  299. // set data to be passed to the view 
  300. $this->_fields = array_merge($user_fields, $extra_fields); 
  301.  
  302. return $this->_fields; 
  303.  
  304. // editor rendering methods 
  305. public function render_editor_toolbar() { 
  306.  
  307. // get custom fields 
  308. $fields = $this->get_custom_fields(); 
  309.  
  310. $output = ''; 
  311. $output .= '<li style="text-align:center;"><a id="wysija-add-field" class="button" href="javascript:;" href2="admin.php?page=wysija_config&action=form_widget_settings&field_id=0">'.__('Add New Field', WYSIJA).'</a></li>'; 
  312.  
  313. foreach($fields as $field) { 
  314. // get field type or defaults to "input" 
  315. $type = (isset($field['column_type'])) ? $field['column_type'] : 'input'; 
  316.  
  317. // set unique if the field type matches the unique_fields OR is a custom field 
  318. $is_unique = (bool)(in_array($field['column_name'], $this->_unique_fields) or isset($field['field_id'])); 
  319.  
  320. // check whether it's an actual custom field 
  321. $is_custom_field = $this->is_custom_field($field); 
  322.  
  323. // actions 
  324. $actions = ''; 
  325. if($is_custom_field) { 
  326. $actions = '<a class="wysija_form_item_settings settings" title="'.__('Edit field', WYSIJA).'" href="javascript:;" href2="admin.php?page=wysija_config&action=form_widget_settings&field_id='.((int)$field['field_id']).'" data-field-id="'.((int)$field['field_id']).'"><span class="dashicons dashicons-admin-generic"></span></a>'; 
  327. $actions .= '<a class="wysija_form_item_delete delete" title="'.__('Delete field', WYSIJA).'" href="javascript:;" data-field-id="'.((int)$field['field_id']).'"><span class="dashicons dashicons-dismiss"></span></a>'; 
  328.  
  329. // generate html for toolbar item 
  330. $output .= '<li><a class="wysija_form_item" id="'.$field['column_name'].'" wysija_field="'.$field['column_name'].'" wysija_name="'.$field['name'].'" wysija_unique="'.$is_unique.'" wysija_type="'.$type.'">'.$field['name'].'</a>'.$actions.'</li>'; 
  331.  
  332. return $output; 
  333.  
  334. // renders all widgets' templates 
  335. function render_editor_templates() { 
  336. $this->set_context('editor'); 
  337.  
  338. // get custom fields 
  339. $fields = $this->get_custom_fields(); 
  340.  
  341. // get parser helper 
  342. $helper_render_engine = WYSIJA::get('render_engine', 'helper'); 
  343. $helper_render_engine->setTemplatePath(WYSIJA_EDITOR_TOOLS); 
  344.  
  345. // define html output string 
  346. $output = ''; 
  347.  
  348. foreach($fields as $field) { 
  349. // get field type or defaults to "input" 
  350. $type = (isset($field['column_type'])) ? $field['column_type'] : 'input'; 
  351.  
  352. // get label from params, defaults to field name 
  353. $label = (isset($field['params']['label'])) ? $field['params']['label'] : $field['name']; 
  354.  
  355. // build field data in order to pass it to the widget template 
  356. $block = array( 
  357. 'field' => $field['column_name'],  
  358. 'type' => $type,  
  359. 'name' => $field['name'],  
  360. 'unique' => (in_array($field['column_name'], $this->_unique_fields)),  
  361. 'static' => (in_array($field['column_name'], $this->_static_fields)),  
  362. 'params' => array( 
  363. 'label' => $label 
  364. ),  
  365. 'i18n' => $this->get_translations() 
  366. ); 
  367.  
  368. // get field extra params if specified 
  369. if(isset($field['params'])) { 
  370. // merge the params 
  371. $block['params'] = array_merge($field['params'], $block['params']); 
  372.  
  373. // get extra data depending on field type 
  374. $block = $this->_get_extra_data($block); 
  375.  
  376. // render widget templates 
  377. $output .= $helper_render_engine->render($block, 'templates/form/editor/widgets/template.html'); 
  378. return $output; 
  379.  
  380. private function set_lists_names($block = array()) { 
  381. // get lists using each list id as key 
  382. $lists = $this->get_formatted_lists(); 
  383.  
  384. if($this->get_context() === 'editor') { 
  385. $block['lists'] = $lists; 
  386. } else { 
  387. // if the block has no list, then simply return the block 
  388. if(!isset($block['params']['values']) or empty($block['params']['values'])) return $block; 
  389.  
  390. $values = array(); 
  391.  
  392. foreach($block['params']['values'] as $list) { 
  393. // check if the list id exists in the lists 
  394. if(isset($lists[$list['list_id']])) { 
  395. $is_checked = (isset($list['is_checked']) ? (int)$list['is_checked'] : 0); 
  396. $values[] = array('name' => $lists[$list['list_id']], 'list_id' => $list['list_id'], 'is_checked' => $is_checked); 
  397.  
  398. $block['params']['values'] = $values; 
  399.  
  400. return $block; 
  401.  
  402. // renders a single widget's template 
  403. public function render_editor_template($block = array()) { 
  404. $this->set_context('editor'); 
  405.  
  406. if(empty($block)) return null; 
  407.  
  408. // get parser helper 
  409. $helper_render_engine = WYSIJA::get('render_engine', 'helper'); 
  410. $helper_render_engine->setTemplatePath(WYSIJA_EDITOR_TOOLS); 
  411.  
  412. // get extra data depending on field type 
  413. $block = $this->_get_extra_data($block); 
  414.  
  415. $block = array_merge($block, array( 
  416. 'unique' => (in_array($block['field'], $this->_unique_fields)),  
  417. 'static' => (in_array($block['field'], $this->_static_fields)),  
  418. 'i18n' => $this->get_translations() 
  419. )); 
  420.  
  421. // render widget templates 
  422. return $helper_render_engine->render($block, 'templates/form/editor/widgets/template.html'); 
  423.  
  424. public function refresh_custom_field($form_id = null, $field = array()) { 
  425. if($form_id === null or empty($field)) return null; 
  426.  
  427. // check if refreshing the field is necessary 
  428. if($this->is_custom_field($field)) { 
  429. // get all forms 
  430. $model_forms = WYSIJA::get('forms', 'model'); 
  431. $forms = $model_forms->getRows(); 
  432.  
  433. $updated_block = null; 
  434.  
  435. // check if there's at least one form otherwise it's kinda pointless 
  436. if(is_array($forms) && count($forms) > 0) { 
  437. // loop over each form 
  438. foreach ($forms as $i => $form) { 
  439. $requires_update = false; 
  440.  
  441. // decode form data 
  442. $data = unserialize(base64_decode($form['data'])); 
  443.  
  444. // loop through each block 
  445. foreach ($data['body'] as $j => $block) { 
  446. // in case we find an instance of the custom field that needs to be updated 
  447. if($block['field'] === $field['field']) { 
  448. $updated_params = $field['settings']; 
  449. $display_fields = array('label', 'label_within', 'lines'); 
  450.  
  451. // apply block display options 
  452. foreach($display_fields as $display_field) { 
  453. if(array_key_exists($display_field, $block['params'])) { 
  454. $updated_params[$display_field] = $block['params'][$display_field]; 
  455.  
  456. // apply new parameters 
  457. $data['body'][$j]['params'] = $updated_params; 
  458. // set flag in order to save changes 
  459. $requires_update = true; 
  460.  
  461. // if it's in the current form, we need to return an updated version of this block 
  462. if((int)$form['form_id'] === $form_id) { 
  463. $updated_block = $data['body'][$j]; 
  464.  
  465. // if the form requires update, let's do it 
  466. if($requires_update === true) { 
  467. $model_forms->reset(); 
  468. $model_forms->update(array('data' => base64_encode(serialize($data))), array('form_id' => (int)$form['form_id'])); 
  469.  
  470. // return false if there's no need to update any block in the current form 
  471. return $updated_block; 
  472.  
  473. private function _get_extra_data($block = array()) { 
  474. // special case for lists 
  475. if($block['field'] === 'list') { 
  476. $block = $this->set_lists_names($block); 
  477.  
  478. // special case for "date" types 
  479. if($block['type'] === 'date') { 
  480. $block['days'] = $this->get_days($block); 
  481. $block['months'] = $this->get_months($block); 
  482. $block['years'] = $this->get_years($block); 
  483.  
  484. $display_date_fields = explode('_', $block['params']['date_type']); 
  485.  
  486. // date order 
  487. $date_orders = array( 
  488. 'year_month_day' => array('mm/dd/yyyy', 'dd/mm/yyyy', 'yyyy/mm/dd'),  
  489. 'year_month' => array('mm/yyyy', 'yyyy/mm'),  
  490. 'year' => array('yyyy'),  
  491. 'month' => array('mm') 
  492. ); 
  493.  
  494. if(isset($block['params']['date_order']) && in_array($block['params']['date_order'], $date_orders[$block['params']['date_type']])) { 
  495. $fields = explode('/', $block['params']['date_order']); 
  496. } else { 
  497. $fields = explode('/', $date_orders[$block['params']['date_type']][0]); 
  498. $block['date_fields'] = $fields; 
  499.  
  500. return $block; 
  501.  
  502. // renders the editor 
  503. function render_editor() { 
  504. $this->set_context('editor'); 
  505.  
  506. if($this->is_data_valid() === false) { 
  507. throw new Exception('data is not valid'); 
  508. } else { 
  509. $helper_render_engine = WYSIJA::get('render_engine', 'helper'); 
  510. $helper_render_engine->setTemplatePath(WYSIJA_EDITOR_TOOLS); 
  511.  
  512. $data = array( 
  513. 'body' => $this->render_editor_body(),  
  514. 'is_debug' => $this->is_debug(),  
  515. 'i18n' => $this->get_translations() 
  516. ); 
  517.  
  518. return $helper_render_engine->render($data, 'templates/form/editor/template.html'); 
  519.  
  520. // renders editor's body 
  521. function render_editor_body() { 
  522. $helper_render_engine = WYSIJA::get('render_engine', 'helper'); 
  523. $helper_render_engine->setTemplatePath(WYSIJA_EDITOR_TOOLS); 
  524.  
  525. $blocks = $this->get_data('body'); 
  526. if(empty($blocks)) return ''; 
  527.  
  528. $body = ''; 
  529. foreach($blocks as $block) { 
  530. // get extra data depending on field type 
  531. $block = $this->_get_extra_data($block); 
  532.  
  533. // generate block template 
  534. $data = array_merge($block, array( 
  535. 'unique' => (in_array($block['field'], $this->_unique_fields)),  
  536. 'static' => (in_array($block['field'], $this->_static_fields)),  
  537. 'i18n' => $this->get_translations()) 
  538. ); 
  539.  
  540. $body .= $helper_render_engine->render($data, 'templates/form/editor/widgets/template.html'); 
  541.  
  542. return $body; 
  543.  
  544. // web rendering methods 
  545. public function render_web($data = array()) { 
  546. $this->set_context('web'); 
  547.  
  548. if($this->is_data_valid() === false) { 
  549. throw new Exception('data is not valid'); 
  550. } else { 
  551. $helper_render_engine = WYSIJA::get('render_engine', 'helper'); 
  552. $helper_render_engine->setTemplatePath(WYSIJA_EDITOR_TOOLS); 
  553.  
  554. $data = array( 
  555. 'preview' => ($this->get_mode() === 'preview'),  
  556. 'settings' => $this->get_data('settings'),  
  557. 'body' => $this->render_web_body() 
  558. ); 
  559.  
  560. // in live mode, we need to specify the form id 
  561. if($this->get_mode() === 'live') { 
  562. $data['form_id'] = (int)$this->get_data('form_id'); 
  563.  
  564. $helper_render_engine = WYSIJA::get('render_engine', 'helper'); 
  565. $helper_render_engine->setTemplatePath(WYSIJA_EDITOR_TOOLS); 
  566.  
  567. // make sure we get the messages 
  568. $output = ''; 
  569.  
  570. $posted_form = (isset($_POST['form_id']) && (int)$_POST['form_id'] > 0) ? (int)$_POST['form_id'] : 0; 
  571.  
  572. /** if($data['form_id'] === $posted_form) { 
  573. $view = WYSIJA::get('widget_nl', 'view', 'front'); 
  574. if(count($view->getMsgs()) > 0) { 
  575. $output .= $view->messages(); 
  576. }*/ 
  577.  
  578. try { 
  579. $output .= $helper_render_engine->render($data, 'templates/form/web/template.html'); 
  580. return $output; 
  581. } catch(Exception $e) { 
  582. return ''; 
  583.  
  584. protected function get_validation_class($block) { 
  585. $rules = array(); 
  586.  
  587. // if it's the email field, it's mandatory and needs to be valid 
  588. if($block['field'] === 'email') { 
  589. $rules[] = 'required'; 
  590. $rules[] = 'custom[email]'; 
  591.  
  592. // if it's the list field, at least one option needs to be selected 
  593. if($block['field'] === 'list') { 
  594. $rules[] = 'required'; 
  595.  
  596. // check if the field is required 
  597. if(isset($block['params']['required']) && (bool)$block['params']['required'] === true) { 
  598. $rules[] = 'required'; 
  599.  
  600. // check for validation rules 
  601. if(isset($block['params']['validate'])) { 
  602. if(is_array($block['params']['validate'])) { 
  603. // handle multiple validation rules 
  604. foreach($block['params']['validate'] as $rule) { 
  605. $rules[] = 'custom['.$rule.']'; 
  606. } else if(strlen(trim($block['params']['validate'])) > 0) { 
  607. // handle single validation rule 
  608. $rules[] = 'custom['.$block['params']['validate'].']'; 
  609.  
  610. // generate string if there is at least one rule to validate against 
  611. if(empty($rules)) { 
  612. return ''; 
  613. } else { 
  614. // make sure rules are not duplicated 
  615. $rules = array_unique($rules); 
  616. return 'validate['.join(', ', $rules).']'; 
  617.  
  618. protected function render_web_body() { 
  619. $helper_render_engine = WYSIJA::get('render_engine', 'helper'); 
  620. $helper_render_engine->setTemplatePath(WYSIJA_EDITOR_TOOLS); 
  621.  
  622. $blocks = $this->get_data('body'); 
  623.  
  624. if(empty($blocks)) return ''; 
  625.  
  626. $body = ''; 
  627. foreach($blocks as $key => $block) { 
  628. // get extra data depending on field type 
  629. $block = $this->_get_extra_data($block); 
  630.  
  631. // special case for email widget 
  632. if($block['field'] === 'email') { 
  633. $user_email = WYSIJA::wp_get_userdata('user_email'); 
  634.  
  635. if($user_email && is_string($user_email) && is_user_logged_in() && !current_user_can('switch_themes') && !is_admin()) { 
  636. $block['value'] = $user_email; 
  637.  
  638. // set field name 'prefix' depending whether it's a custom field or not 
  639. if($this->is_custom_field($block)) { 
  640. $field_prefix = 'wysija[field]'; 
  641. } else { 
  642. $field_prefix = 'wysija[user]'; 
  643.  
  644. // generate block template 
  645. $data = array_merge($block, array( 
  646. 'field_prefix' => $field_prefix,  
  647. 'preview' => ($this->get_mode() === 'preview'),  
  648. 'i18n' => $this->get_translations(),  
  649. 'validation' => $this->get_validation_class($block) 
  650. )); 
  651. $body .= $helper_render_engine->render($data, 'templates/form/web/widgets/template.html'); 
  652.  
  653. return $body; 
  654.  
  655. public function is_custom_field($field = array()) { 
  656. return (bool)(isset($field['field_id']) || (isset($field['field']) && strpos($field['field'], 'cf_') === 0)); 
  657.  
  658. public function get_exports($form_id) { 
  659. return array( 
  660. 'iframe' => base64_encode($this->export($form_id, 'iframe')),  
  661. 'php' => base64_encode($this->export($form_id, 'php')),  
  662. 'html' => base64_encode($this->export($form_id, 'html')) 
  663. ); 
  664.  
  665. public function render_editor_export($form_id) { 
  666. $helper_render_engine = WYSIJA::get('render_engine', 'helper'); 
  667. $helper_render_engine->setTemplatePath(WYSIJA_EDITOR_TOOLS); 
  668.  
  669. $data = array( 
  670. 'types' => array( 
  671. 'iframe' => $this->export($form_id, 'iframe'),  
  672. 'php' => $this->export($form_id, 'php'),  
  673. 'html' => $this->export($form_id, 'html'),  
  674. 'shortcode' => $this->export($form_id, 'shortcode') 
  675. ); 
  676.  
  677. return $helper_render_engine->render($data, 'templates/form/web/export.html'); 
  678.  
  679. public function export($form_id, $type) { 
  680. switch($type) { 
  681. case 'iframe': 
  682. $url_params = array( 
  683. 'wysija-page' => 1,  
  684. 'controller' => 'subscribers',  
  685. 'action' => 'wysija_outter',  
  686. 'wysija_form' => $form_id 
  687. ); 
  688.  
  689. $url_params['external_site'] = 1; 
  690.  
  691. $model_config = WYSIJA::get('config', 'model'); 
  692. $source_url = WYSIJA::get_permalink($model_config->getValue('confirm_email_link'), $url_params, true); 
  693.  
  694. return '<iframe width="100%" scrolling="no" frameborder="0" src="'.$source_url.'" class="iframe-wysija" vspace="0" tabindex="0" style="position: static; top: 0pt; margin: 0px; border-style: none; height: 330px; left: 0pt; visibility: visible;" marginwidth="0" marginheight="0" hspace="0" allowtransparency="true" title="'.__('Subscription MailPoet', WYSIJA).'"></iframe>'; 
  695. break; 
  696. case 'php': 
  697. $output = array( 
  698. '$widgetNL = new WYSIJA_NL_Widget(true);',  
  699. 'echo $widgetNL->widget(array(\'form\' => '.(int)$form_id.', \'form_type\' => \'php\'));' 
  700. ); 
  701. return join("\n", $output); 
  702. break; 
  703. case 'html': 
  704. //need some language for the validation 
  705. $helper_toolbox = WYSIJA::get('toolbox', 'helper'); 
  706. $wp_language_code = $helper_toolbox->get_language_code(); 
  707.  
  708. $wysija_version = WYSIJA::get_version(); 
  709. $scripts_to_include = '<!--START Scripts : this is the script part you can add to the header of your theme-->'."\n"; 
  710. $scripts_to_include .= '<script type="text/javascript" src="'.includes_url().'js/jquery/jquery.js'.'?ver='.$wysija_version.'"></script>'."\n"; 
  711. if(file_exists(WYSIJA_DIR.'js'.DS.'validate'.DS.'languages'.DS.'jquery.validationEngine-'.$wp_language_code.'.js')) { 
  712. $scripts_to_include .= '<script type="text/javascript" src="'.WYSIJA_URL.'js/validate/languages/jquery.validationEngine-'.$wp_language_code.'.js'.'?ver='.$wysija_version.'"></script>'."\n"; 
  713. }else{ 
  714. $scripts_to_include .= '<script type="text/javascript" src="'.WYSIJA_URL.'js/validate/languages/jquery.validationEngine-en.js'.'?ver='.$wysija_version.'"></script>'."\n"; 
  715. $scripts_to_include .= '<script type="text/javascript" src="'.WYSIJA_URL.'js/validate/jquery.validationEngine.js'.'?ver='.$wysija_version.'"></script>'."\n"; 
  716. $scripts_to_include .= '<script type="text/javascript" src="'.WYSIJA_URL.'js/front-subscribers.js'.'?ver='.$wysija_version.'"></script>'."\n"; 
  717. $scripts_to_include .= '<script type="text/javascript"> 
  718. /* <![CDATA[ */ 
  719. var wysijaAJAX = {"action":"wysija_ajax", "controller":"subscribers", "ajaxurl":"'.admin_url('admin-ajax.php', 'absolute').'", "loadingTrans":"'.__('Loading...', WYSIJA).'"}; 
  720. /* ]]> */ 
  721. </script>'; 
  722. $scripts_to_include .= '<script type="text/javascript" src="'.WYSIJA_URL.'js/front-subscribers.js?ver='.$wysija_version.'"></script>'."\n"; 
  723. $scripts_to_include .= '<!--END Scripts-->'."\n"."\n"; 
  724.  
  725. //enqueue the scripts 
  726. $html_result = $scripts_to_include; 
  727.  
  728. // add the html for the form 
  729. $widget_NL = new WYSIJA_NL_Widget(true); 
  730. $html_result .= $widget_NL->widget(array('form' => (int)$form_id, 'form_type' => 'html')); 
  731.  
  732. return $html_result; 
  733. break; 
  734. case 'shortcode': 
  735. return '[wysija_form id="'.(int)$form_id.'"]'; 
  736. break;