PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/ninja-forms/deprecated/includes/shortcode.php

https://gitlab.com/lamovible/grand-regis
PHP | 250 lines | 163 code | 40 blank | 47 comment | 44 complexity | 3f72bf030734f7a7665eaabb5830bb71 MD5 | raw file
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. function ninja_forms_shortcode( $atts ){
  3. if ( is_admin() && ( !defined( 'DOING_AJAX' ) || !DOING_AJAX ) ) {
  4. $return = '[ninja_forms_display_form';
  5. if ( is_array ( $atts ) ) {
  6. foreach ( $atts as $key => $value ) {
  7. $return .= ' ' . $key . '=' . $value;
  8. }
  9. }
  10. $return .= ']';
  11. return $return;
  12. } else {
  13. $form = ninja_forms_return_echo( 'ninja_forms_display_form', $atts['id'] );
  14. return $form;
  15. }
  16. }
  17. add_shortcode( 'ninja_forms', 'ninja_forms_shortcode' );
  18. add_shortcode( 'ninja_form', 'ninja_forms_shortcode' );
  19. /**
  20. * Old Ninja Forms shortcode
  21. */
  22. add_shortcode( 'ninja_forms_display_form', 'ninja_forms_shortcode' );
  23. function ninja_forms_field_shortcode( $atts ){
  24. global $ninja_forms_processing;
  25. $field_id = $atts['id'];
  26. if ( is_object ( $ninja_forms_processing ) ) {
  27. $value = $ninja_forms_processing->get_field_value( $field_id );
  28. $value = apply_filters( 'ninja_forms_field_shortcode', $value, $atts );
  29. if( is_array( $value ) ){
  30. $value = implode( ',', $value );
  31. }
  32. } else {
  33. $value = '';
  34. }
  35. return nf_wp_kses_post_deep( $value );
  36. }
  37. add_shortcode( 'ninja_forms_field', 'ninja_forms_field_shortcode' );
  38. function ninja_forms_sub_date_shortcode( $atts ){
  39. global $ninja_forms_processing;
  40. if( isset( $atts['format'] ) ){
  41. $date_format = $atts['format'];
  42. }else{
  43. $date_format = 'm/d/Y';
  44. }
  45. $date = date( $date_format );
  46. return $date;
  47. }
  48. add_shortcode( 'ninja_forms_sub_date', 'ninja_forms_sub_date_shortcode' );
  49. function ninja_forms_pre_process_shortcode($content) {
  50. global $shortcode_tags;
  51. // Remove our previously registered shortcode.
  52. //remove_shortcode( 'ninja_forms_display_form' );
  53. // Backup current registered shortcodes and clear them all out
  54. $current_shortcodes = $shortcode_tags;
  55. $shortcode_tags = array();
  56. add_shortcode( 'ninja_forms_display_form', 'ninja_forms_shortcode' );
  57. // Do the shortcode (only the one above is registered)
  58. $content = do_shortcode($content);
  59. // Put the original shortcodes back
  60. $shortcode_tags = $current_shortcodes;
  61. return $content;
  62. }
  63. add_filter('the_content', 'ninja_forms_pre_process_shortcode', 9999);
  64. /**
  65. * Parse the [nf_sub_seq_num] shortcode
  66. *
  67. * @since 2.8.4
  68. * @return string $setting
  69. */
  70. function nf_parse_sub_seq_num_shortcode( $setting, $setting_name = '', $id = '' ) {
  71. global $ninja_forms_processing;
  72. if ( ! is_object( $ninja_forms_processing ) )
  73. return $setting;
  74. $sub_id = $ninja_forms_processing->get_form_setting( 'sub_id' );
  75. if ( empty ( $sub_id ) )
  76. return $setting;
  77. $seq_num = Ninja_Forms()->sub( $sub_id )->get_seq_num();
  78. $setting = str_replace( '[nf_sub_seq_num]', $seq_num, $setting );
  79. return $setting;
  80. }
  81. add_filter( 'nf_email_notification_process_setting', 'nf_parse_sub_seq_num_shortcode', 10, 3 );
  82. add_filter( 'nf_success_msg', 'nf_parse_sub_seq_num_shortcode', 10, 2 );
  83. /**
  84. * Shortcode for ninja_forms_all_fields
  85. *
  86. * @since 2.8
  87. * @return string $content
  88. */
  89. function nf_all_fields_shortcode( $atts, $content = '' ) {
  90. global $ninja_forms_fields, $ninja_forms_processing;
  91. if ( ! isset ( $ninja_forms_processing ) )
  92. return false;
  93. $html = isset ( $atts['html'] ) ? $atts['html'] : 1;
  94. if ( 1 == $html ) {
  95. // Generate our "all fields" table for use as a JS var.
  96. $field_list = '<table><tbody>';
  97. } else {
  98. $field_list = '';
  99. }
  100. foreach ( $ninja_forms_processing->get_all_fields() as $field_id => $user_value ) {
  101. if ( ! $user_value )
  102. continue;
  103. $field = $ninja_forms_processing->get_field_settings( $field_id );
  104. $type = $field['type'];
  105. if ( ! isset ( $ninja_forms_fields[ $type ] ) || ! $ninja_forms_fields[ $type ]['process_field'] )
  106. continue;
  107. $value = apply_filters( 'nf_all_fields_field_value', ninja_forms_field_shortcode( array( 'id' => $field_id ) ), $field_id );
  108. if( isset( $field[ 'data' ][ 'admin_label' ] ) && $field[ 'data' ][ 'admin_label' ] ){
  109. $label = $field[ 'data' ][ 'admin_label' ];
  110. } else {
  111. $label = $field['data']['label'];
  112. }
  113. $label = strip_tags( apply_filters( 'nf_all_fields_field_label', $label, $field_id ) );
  114. if ( 1 == $html ) {
  115. $field_list .= '<tr id="ninja_forms_field_' . $field_id . '"><td>' . $label .':</td><td>' . $value . '</td></tr>';
  116. } else {
  117. $field_list .= $label . ' - ' . $value . "\r\n";
  118. }
  119. }
  120. if ( 1 == $html )
  121. $field_list .= '</tbody></table>';
  122. return apply_filters( 'nf_all_fields_table', $field_list, $ninja_forms_processing->get_form_ID() );
  123. }
  124. add_shortcode( 'ninja_forms_all_fields', 'nf_all_fields_shortcode' );
  125. /**
  126. * Parse our [ninja_forms_field] shortcode, just incase the shortcode parser screwed up.
  127. *
  128. * @since 2.8.4
  129. * @return content
  130. */
  131. function nf_parse_fields_shortcode( $content ) {
  132. global $ninja_forms_processing;
  133. if ( ! isset ( $ninja_forms_processing ) )
  134. return $content;
  135. if ( is_array ( $content ) )
  136. return $content;
  137. $matches = array();
  138. $pattern = '\[(\[?)(ninja_forms_field|ninja_forms_all_fields)(?![\w-])([^\]\/]*(?:\/(?!\])[^\]\/]*)*?)(?:(\/)\]|\](?:([^\[]*+(?:\[(?!\/\2\])[^\[]*+)*+)\[\/\2\])?)(\]?)';
  139. preg_match_all('/'.$pattern.'/s', $content, $matches);
  140. if ( is_array( $matches ) && ! empty( $matches[2] ) ) {
  141. foreach ( $matches[2] as $key => $shortcode ) {
  142. if ( 'ninja_forms_field' == $shortcode ) {
  143. if ( isset ( $matches[3][ $key ] ) ) {
  144. $atts = shortcode_parse_atts( $matches[3][ $key ] );
  145. $id = $atts['id'];
  146. $value = $ninja_forms_processing->get_field_value( $id );
  147. if( is_array( $value ) ){
  148. $value = implode( ',', $value );
  149. }
  150. $content = str_replace( $matches[0][ $key ], $value, $content );
  151. }
  152. } else if ( 'ninja_forms_all_fields' == $shortcode ) {
  153. if ( isset ( $matches[3][ $key ] ) ) {
  154. $atts = shortcode_parse_atts( $matches[3][ $key ] );
  155. $content = str_replace( $matches[0][ $key ], nf_all_fields_shortcode( $atts, $content ), $content );
  156. }
  157. }
  158. }
  159. }
  160. return $content;
  161. }
  162. /**
  163. * Shortcode for ninja_forms_all_fields
  164. *
  165. * @since 2.8
  166. * @return string sub_limit_number
  167. */
  168. function ninja_forms_display_sub_limit_number_shortcode( $atts ){
  169. $form = Ninja_Forms()->form( $atts[ 'id' ] );
  170. if ( isset( $form->settings[ 'sub_limit_number' ] ) ) {
  171. return $form->settings[ 'sub_limit_number' ];
  172. }
  173. else {
  174. return null;
  175. }
  176. }
  177. add_shortcode( 'ninja_forms_display_sub_limit_number', 'ninja_forms_display_sub_limit_number_shortcode' );
  178. /**
  179. * Shortcode for ninja_forms_display_sub_number
  180. *
  181. * @since 2.8
  182. * @return int nf_get_sub_count()
  183. * @see nf_get_sub_count()
  184. */
  185. function ninja_forms_display_sub_number_shortcode( $atts ){
  186. $sub_count = nf_get_sub_count( $atts[ 'id' ] );
  187. return $sub_count;
  188. }
  189. add_shortcode( 'ninja_forms_display_sub_number', 'ninja_forms_display_sub_number_shortcode' );
  190. /**
  191. * Shortcode for ninja_forms_display_sub_number_remaining
  192. *
  193. * @since 2.8
  194. * @return int
  195. * @see nf_get_sub_count()
  196. */
  197. function ninja_forms_display_sub_number_remaining_shortcode( $atts ){
  198. $form = Ninja_Forms()->form($atts[ 'id' ]);
  199. if( isset( $form->settings[ 'sub_limit_number' ] ) ) {
  200. $sub_count = nf_get_sub_count( $atts[ 'id' ] );
  201. $sub_limit = (int) $form->settings[ 'sub_limit_number' ];
  202. if( $sub_count > $sub_limit )
  203. return 0;
  204. return $sub_limit - $sub_count;
  205. }
  206. else {
  207. return null;
  208. }
  209. }
  210. add_shortcode( 'ninja_forms_display_sub_number_remaining', 'ninja_forms_display_sub_number_remaining_shortcode' );