PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/ninja-forms/includes/MergeTags/Fields.php

https://bitbucket.org/wallindev/wallindev-wp
PHP | 371 lines | 217 code | 77 blank | 77 comment | 53 complexity | 5ba6d24739dabe303bbba53fe28fc71c MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. /**
  3. * Class NF_MergeTags_Fields
  4. */
  5. final class NF_MergeTags_Fields extends NF_Abstracts_MergeTags
  6. {
  7. protected $id = 'fields';
  8. protected $form_id;
  9. public function __construct()
  10. {
  11. parent::__construct();
  12. $this->title = __( 'Fields', 'ninja-forms' );
  13. $this->merge_tags = Ninja_Forms()->config( 'MergeTagsFields' );
  14. if( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
  15. $this->merge_tags = array_merge( $this->merge_tags, Ninja_Forms()->config( 'MergeTagsFieldsAJAX' ) );
  16. }
  17. add_filter( 'ninja_forms_calc_setting', array( $this, 'pre_parse_calc_settings' ), 9 );
  18. //add_filter( 'ninja_forms_calc_setting', array( $this, 'calc_replace' ) );
  19. }
  20. public function __call($name, $arguments)
  21. {
  22. if(isset($arguments[0]['calc'])) {
  23. return $this->merge_tags[ $name ][ 'calc_value' ];
  24. }
  25. return $this->merge_tags[ $name ][ 'field_value' ];
  26. }
  27. public function all_fields()
  28. {
  29. if( is_rtl() ){
  30. $return = '<table style="direction: rtl;">';
  31. } else {
  32. $return = '<table>';
  33. }
  34. $hidden_field_types = array( 'html', 'submit', 'password', 'passwordconfirm' );
  35. foreach( $this->get_fields_sorted() as $field ){
  36. if( ! isset( $field[ 'type' ] ) ) continue;
  37. if( in_array( $field[ 'type' ], array_values( $hidden_field_types ) ) ) continue;
  38. $field[ 'value' ] = apply_filters( 'ninja_forms_merge_tag_value_' . $field[ 'type' ], $field[ 'value' ], $field );
  39. if( is_array( $field[ 'value' ] ) ) $field[ 'value' ] = implode( ', ', $field[ 'value' ] );
  40. $return .= '<tr><td>' . apply_filters('ninja_forms_merge_label', $field[ 'label' ]) .':</td><td>' . $field[ 'value' ] . '</td></tr>';
  41. }
  42. $return .= '</table>';
  43. return $return;
  44. }
  45. public function all_fields_table()
  46. {
  47. if( is_rtl() ){
  48. $return = '<table style="direction: rtl;">';
  49. } else {
  50. $return = '<table>';
  51. }
  52. $hidden_field_types = array( 'submit', 'password', 'passwordconfirm' );
  53. $list_fields_types = array( 'listcheckbox', 'listmultiselect', 'listradio', 'listselect' );
  54. foreach( $this->get_fields_sorted() as $field ){
  55. if( ! isset( $field[ 'type' ] ) ) continue;
  56. // Skip specific field types.
  57. if( in_array( $field[ 'type' ], array_values( $hidden_field_types ) ) ) continue;
  58. $field[ 'value' ] = apply_filters( 'ninja_forms_merge_tag_value_' . $field[ 'type' ], $field[ 'value' ], $field );
  59. // Check to see if the type is a list field and if it is...
  60. if( in_array( $field[ 'type' ], array_values( $list_fields_types ) ) ) {
  61. // If we have a comma separated value...
  62. if( strpos( $field[ 'value' ], ',' ) ) {
  63. // ...build the value back into an array.
  64. $field[ 'value' ] = explode( ',', $field[ 'value' ] );
  65. }
  66. // ...then set the value equal to the field label.
  67. $field[ 'value' ] = $this->get_list_labels( $field );
  68. }
  69. if( is_array( $field[ 'value' ] ) ) $field[ 'value' ] = implode( ', ', $field[ 'value' ] );
  70. // Check to see if the type is a list field and if it is...
  71. $return .= '<tr><td valign="top">' . apply_filters('ninja_forms_merge_label', $field[ 'label' ]) .':</td><td>' . $field[ 'value' ] . '</td></tr>';
  72. }
  73. $return .= '</table>';
  74. return $return;
  75. }
  76. public function fields_table()
  77. {
  78. if( is_rtl() ){
  79. $return = '<table style="direction: rtl;">';
  80. } else {
  81. $return = '<table>';
  82. }
  83. $hidden_field_types = array( 'html', 'submit', 'password', 'passwordconfirm', 'hidden' );
  84. $list_fields_types = array( 'listcheckbox', 'listmultiselect', 'listradio', 'listselect' );
  85. foreach( $this->get_fields_sorted() as $field ){
  86. if( ! isset( $field[ 'type' ] ) ) continue;
  87. // Skip specific field types.
  88. if( in_array( $field[ 'type' ], array_values( $hidden_field_types ) ) ) continue;
  89. // TODO: Skip hidden fields, ie conditionally hidden.
  90. if( isset( $field[ 'visible' ] ) && false === $field[ 'visible' ] ) continue;
  91. // Check to see if the type is a list field and if it is...
  92. if( in_array( $field[ 'type' ], array_values( $list_fields_types ) ) ) {
  93. // If we have a comma separated value...
  94. if( strpos( $field[ 'value' ], ',' ) ) {
  95. // ...build the value back into an array.
  96. $field[ 'value' ] = explode( ',', $field[ 'value' ] );
  97. }
  98. // ...then set the value equal to the field label.
  99. $field[ 'value' ] = $this->get_list_labels( $field );
  100. }
  101. $field[ 'value' ] = apply_filters( 'ninja_forms_merge_tag_value_' . $field[ 'type' ], $field[ 'value' ], $field );
  102. // Skip fields without values.
  103. if( ! $field[ 'value' ] ) continue;
  104. if( is_array( $field[ 'value' ] ) ) $field[ 'value' ] = implode( ', ', $field[ 'value' ] );
  105. $return .= '<tr><td valign="top">' . apply_filters('ninja_forms_merge_label', $field[ 'label' ]) .':</td><td>' . $field[ 'value' ] . '</td></tr>';
  106. }
  107. $return .= '</table>';
  108. return $return;
  109. }
  110. // TODO: Is this being used?
  111. public function all_field_plain()
  112. {
  113. $return = '';
  114. foreach( $this->get_fields_sorted() as $field ){
  115. $field[ 'value' ] = apply_filters( 'ninja_forms_merge_tag_value_' . $field[ 'type' ], $field[ 'value' ], $field );
  116. if( is_array( $field[ 'value' ] ) ) $field[ 'value' ] = implode( ', ', $field[ 'value' ] );
  117. $return .= $field[ 'label' ] .': ' . $field[ 'value' ] . "\r\n";
  118. }
  119. return $return;
  120. }
  121. public function add_field( $field )
  122. {
  123. //print_r($field);
  124. $hidden_field_types = apply_filters( 'nf_sub_hidden_field_types', array() );
  125. if( in_array( $field[ 'type' ], $hidden_field_types )
  126. && 'html' != $field[ 'type' ] // Specifically allow the HTML field in merge tags.
  127. && 'password' != $field[ 'type' ] // Specifically allow the Password field in merge tags for actions, ie User Management
  128. ) return;
  129. $field_id = $field[ 'id' ];
  130. $callback = 'field_' . $field_id;
  131. $list_fields_types = array( 'listcheckbox', 'listmultiselect', 'listradio', 'listselect' );
  132. if( is_array( $field[ 'value' ] ) ) $field[ 'value' ] = implode( ',', $field[ 'value' ] );
  133. $field[ 'value' ] = strip_shortcodes( $field[ 'value' ] );
  134. $this->merge_tags[ 'all_fields' ][ 'fields' ][ $field_id ] = $field;
  135. $value = apply_filters('ninja_forms_merge_tag_value_' . $field['type'], $field['value'], $field);
  136. $this->add( $callback, $field['id'], '{field:' . $field['id'] . '}', $value );
  137. if( isset( $field[ 'key' ] ) ) {
  138. $field_key = $field[ 'key' ];
  139. $calc_value = apply_filters( 'ninja_forms_merge_tag_calc_value_' . $field[ 'type' ], $field['value'], $field );
  140. // Add Field Key Callback
  141. $callback = 'field_' . $field_key;
  142. $this->add( $callback, $field_key, '{field:' . $field_key . '}', $value, $calc_value );
  143. // Add Field by Key for All Fields
  144. $this->merge_tags[ 'all_fields_by_key' ][ 'fields' ][ $field_key ] = $field;
  145. // Add Field Calc Callabck
  146. if( '' == $calc_value ) $calc_value = '0';
  147. //var_dump($calc_value);
  148. //echo('myspace');
  149. $callback = 'field_' . $field_key . '_calc';
  150. $this->add( $callback, $field_key, '{field:' . $field_key . ':calc}', $calc_value, $calc_value );
  151. /*
  152. * Adds the ability to add :label to list field merge tags
  153. * this will cause the label to be displayed on the front end
  154. * instead of the value.
  155. *
  156. * @since 3.3.3
  157. */
  158. // Check to see if the type is a list field and if it is...
  159. if( in_array( $field[ 'type' ], array_values( $list_fields_types ) ) ) {
  160. // If we have a comma separated value...
  161. if ( strpos( $field[ 'value' ], ',' ) ) {
  162. // ...build the value back into an array.
  163. $field[ 'value' ] = explode( ',', $field[ 'value' ] );
  164. }
  165. // ...then set the value equal to the field label.
  166. $field[ 'value' ] = $this->get_list_labels( $field );
  167. // If we have multiple values in from the list field...
  168. if( is_array( $field[ 'value' ] ) ){
  169. // ...convert our values into an array.
  170. $field[ 'value' ] = implode( ', ', $field[ 'value' ] );
  171. }
  172. // Set callback and add this merge tag.
  173. $callback = 'field_' . $field_key . '_label';
  174. $this->add( $callback, $field_key, '{field:' . $field_key . ':label}', $field[ 'value' ] );
  175. }
  176. }
  177. }
  178. /**
  179. * Get List Labels
  180. * Accepts a field loops over options, compares field values and returns the labels.
  181. * @since 3.2.22
  182. *
  183. * @param $field array
  184. * @return array - label of the option.
  185. */
  186. public function get_list_labels( $field )
  187. {
  188. // Build our array to store our labels.
  189. $labels = array();
  190. // Loop over our options...
  191. $field[ 'options' ] = apply_filters( 'ninja_forms_render_options', $field[ 'options' ], $field );
  192. $field[ 'options' ] = apply_filters( 'ninja_forms_render_options_' . $field['type'], $field[ 'options' ], $field );
  193. foreach( $field[ 'options' ] as $options ) {
  194. // ...checks to see if our list has multiple values.
  195. if( is_array( $field[ 'value' ] ) ) {
  196. // Loop over our values...
  197. foreach( $field[ 'value' ] as $value ) {
  198. // ...See if our values match...
  199. if( $options[ 'value' ] == $value ) {
  200. // if they do build an array of the labels.
  201. $labels[] = $options[ 'label' ];
  202. }
  203. }
  204. // Otherwise if we are dealing with a single value, then...
  205. } elseif( $field[ 'value' ] == $options[ 'value' ] ) {
  206. // ...Set the label.
  207. $labels = $options[ 'label' ];
  208. }
  209. }
  210. return $labels;
  211. }
  212. /**
  213. * @param $callback
  214. * @param $id
  215. * @param $tag
  216. * @param $value
  217. * @param bool $calc_value
  218. */
  219. public function add( $callback, $id, $tag, $value, $calc_value = false )
  220. {
  221. $this->merge_tags[ $callback ] = array(
  222. 'id' => $id,
  223. 'tag' => $tag,
  224. 'callback' => $callback,
  225. 'field_value' => $value,
  226. 'calc_value' => ($calc_value === false) ? $value : $calc_value,
  227. );
  228. }
  229. public function set_form_id( $form_id )
  230. {
  231. $this->form_id = $form_id;
  232. }
  233. private function get_fields_sorted()
  234. {
  235. $fields = $this->merge_tags[ 'all_fields' ][ 'fields' ];
  236. // Filterable Sorting for Add-ons (ie Layout and Multi-Part ).
  237. if ( has_filter( 'ninja_forms_get_fields_sorted' ) ) {
  238. $fields_by_key = $this->merge_tags[ 'all_fields_by_key' ][ 'fields' ];
  239. $fields = apply_filters( 'ninja_forms_get_fields_sorted', array(), $fields, $fields_by_key, $this->form_id );
  240. } else {
  241. // Default Sorting by Field Order.
  242. uasort( $fields, array( $this, 'sort_fields' ) );
  243. }
  244. return $fields;
  245. }
  246. public static function sort_fields( $a, $b )
  247. {
  248. if ( $a[ 'order' ] == $b[ 'order' ] ) {
  249. return 0;
  250. }
  251. return ( $a[ 'order' ] < $b[ 'order' ] ) ? -1 : 1;
  252. }
  253. public function calc_replace( $subject ) {
  254. if( is_array( $subject ) ){
  255. foreach( $subject as $i => $s ){
  256. $subject[ $i ] = $this->replace( $s );
  257. }
  258. return $subject;
  259. }
  260. //print_r($subject);
  261. preg_match_all("/{(.*?)}/", $subject, $matches );
  262. if( empty( $matches[0] ) ) return $subject;
  263. foreach( $this->merge_tags as $merge_tag ){
  264. if( ! in_array( $merge_tag[ 'tag' ], $matches[0] ) ) continue;
  265. if( ! isset($merge_tag[ 'callback' ])) continue;
  266. //print_r($merge_tag);
  267. //echo( ' = ' );
  268. $replace = ( is_callable( array( $this, $merge_tag[ 'callback' ] ) ) ) ? $this->{$merge_tag[ 'callback' ]}(array('calc' => true)) : '0';
  269. //print_r($replace);
  270. //echo(' myspace ');
  271. if( '' == $replace ) $replace = '0';
  272. $subject = str_replace( $merge_tag[ 'tag' ], $replace, $subject );
  273. }
  274. return $subject;
  275. }
  276. /*
  277. |--------------------------------------------------------------------------
  278. | Calculations
  279. |--------------------------------------------------------------------------
  280. | Force {field:...:calc} in this context of calculations.
  281. | Example: {field:list} -> {field:list:calc}
  282. | When parsing the {field:...:calc} tag, if no calc value is found then the value will be used.
  283. | TODO: This makes explicit list field "values" inaccessible in calculations.
  284. */
  285. public function pre_parse_calc_settings( $eq )
  286. {
  287. return preg_replace_callback( '/{field:([a-z0-9]|_|-)*}/',
  288. array( $this, 'force_field_calc_tags' ), $eq );
  289. }
  290. private function force_field_calc_tags( $matches )
  291. {
  292. return str_replace( '}', ':calc}', $matches[0] );
  293. }
  294. } // END CLASS NF_MergeTags_Fields