PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/ninja-forms/includes/admin/upgrades/convert-notifications.php

https://gitlab.com/stevie007/cinostaging
PHP | 341 lines | 225 code | 76 blank | 40 comment | 47 complexity | 360dd74cbff7e3550f62ab66622dec1e MD5 | raw file
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. final class NF_Upgrade_Notifications extends NF_Upgrade
  3. {
  4. public $name = 'notifications';
  5. public $priority = '2.9.2';
  6. public $description = 'An update is necessary for the new Emails & Actions tab to function properly. This new section gives the user much more control over what happens when a form is submitted.';
  7. public $completed_forms = array();
  8. public $args = array();
  9. public $errors = array();
  10. public function loading()
  11. {
  12. global $wpdb;
  13. $this->removeOldEmailSettings();
  14. $form_count = $this->getFormCount();
  15. $forms = $this->getAllForms();
  16. $x = 1;
  17. if ( is_array( $forms ) ) {
  18. foreach ( $forms as $form ) {
  19. $this->args['forms'][$x] = $form['id'];
  20. $x++;
  21. }
  22. }
  23. if( empty( $this->total_steps ) || $this->total_steps <= 1 ) {
  24. $this->total_steps = $form_count;
  25. }
  26. }
  27. public function _beforeStep( $step )
  28. {
  29. // Get a list of forms that we've already converted.
  30. $this->completed_forms = get_option( 'nf_convert_notifications_forms', array() );
  31. if ( ! is_array( $this->completed_forms ) ) {
  32. $this->completed_forms = array();
  33. }
  34. }
  35. public function step( $step )
  36. {
  37. global $ninja_forms_fields;
  38. // Get our form ID
  39. $form_id = $this->args['forms'][ $step ];
  40. // Bail if we've already converted the notifications for this form.
  41. if ( in_array( $form_id, $this->completed_forms ) )
  42. return false;
  43. // Grab our form from the database
  44. $form_settings = Ninja_Forms()->form( $form_id )->settings;
  45. $fields = Ninja_Forms()->form( $form_id )->fields;
  46. $process_fields = array();
  47. foreach( $fields as $field_id => $field ) {
  48. $label = strip_tags( nf_get_field_admin_label( $field_id ) );
  49. if ( strlen( $label ) > 30 ) {
  50. $tmp_label = substr( $label, 0, 30 );
  51. } else {
  52. $tmp_label = $label;
  53. }
  54. $tmp_array = array( 'value' => $field_id, 'label' => $tmp_label . ' - ID: ' . $field_id );
  55. $admin_label = $label;
  56. $label = isset( $field['data']['label'] ) ? $field['data']['label'] : '';
  57. // Check to see if this field is supposed to be "processed"
  58. $type = $field['type'];
  59. if ( isset ( $ninja_forms_fields[ $type ]['process_field'] ) && $ninja_forms_fields[ $type ]['process_field'] ) {
  60. $process_fields[ $field_id ] = array( 'field_id' => $field_id, 'label' => $label, 'admin_label' => $admin_label );
  61. }
  62. }
  63. // Create a notification for our admin email
  64. if ( ( isset ( $form_settings['admin_email_msg'] ) && ! empty ( $form_settings['admin_email_msg'] ) ) || ( isset ( $form_settings['admin_email_fields'] ) && 1 == $form_settings['admin_email_fields'] ) ) {
  65. // Create a notification
  66. $n_id = nf_insert_notification( $form_id );
  67. // Update our notification type
  68. nf_update_object_meta( $n_id, 'type', 'email' );
  69. // Activate our notification
  70. Ninja_Forms()->notification( $n_id )->activate();
  71. // Update our notification name
  72. Ninja_Forms()->notification( $n_id )->update_setting( 'name', __( 'Admin Email', 'ninja-forms' ) );
  73. $admin_mailto = isset ( $form_settings['admin_mailto'] ) ? $form_settings['admin_mailto'] : array();
  74. // Implode our admin email addresses
  75. $to = implode('`', $admin_mailto );
  76. // Update our to setting
  77. Ninja_Forms()->notification( $n_id )->update_setting( 'to', $to );
  78. // Update our Format Setting
  79. Ninja_Forms()->notification( $n_id )->update_setting( 'email_format', $form_settings['email_type'] );
  80. // Update our attach csv option
  81. Ninja_Forms()->notification( $n_id )->update_setting( 'attach_csv', $form_settings['admin_attach_csv'] );
  82. // Update our subject
  83. $subject = $this->replace_shortcodes( $form_settings['admin_subject'] );
  84. Ninja_Forms()->notification( $n_id )->update_setting( 'email_subject', $subject );
  85. // Update our From Name
  86. if ( isset ( $form_settings['email_from_name'] ) ) {
  87. Ninja_Forms()->notification( $n_id )->update_setting( 'from_name', $form_settings['email_from_name'] );
  88. }
  89. foreach ( $fields as $field ) {
  90. if ( isset ( $field['data']['from_name'] ) && $field['data']['from_name'] == 1 ) {
  91. // Update our From Name
  92. Ninja_Forms()->notification( $n_id )->update_setting( 'from_name', 'field_' . $field['id'] );
  93. break;
  94. }
  95. }
  96. // Update our From Address
  97. Ninja_Forms()->notification( $n_id )->update_setting( 'from_address', $form_settings['email_from'] );
  98. // Get our reply-to address
  99. foreach ( $fields as $field ) {
  100. if ( isset ( $field['data']['replyto_email'] ) && $field['data']['replyto_email'] == 1 ) {
  101. Ninja_Forms()->notification( $n_id )->update_setting( 'reply_to', 'field_' . $field['id'] );
  102. break;
  103. }
  104. }
  105. $email_message = $form_settings['admin_email_msg'];
  106. // Check to see if the "include list of fields" checkbox was checked.
  107. if ( isset ( $form_settings['admin_email_fields'] ) && $form_settings['admin_email_fields'] == 1 ) {
  108. $email_message .= '<br />[ninja_forms_all_fields]';
  109. }
  110. // Update our email message
  111. Ninja_Forms()->notification( $n_id )->update_setting( 'email_message', $email_message );
  112. Ninja_Forms()->notification( $n_id )->update_setting( 'admin_email', true );
  113. }
  114. // Create a notification for our user email
  115. if ( ! empty ( $fields ) ) {
  116. $addresses = array();
  117. foreach ( $fields as $field_id => $field ) {
  118. if ( isset ( $field['data']['send_email'] ) && $field['data']['send_email'] == 1 ) {
  119. // Add this field to our $addresses variable.
  120. $addresses[] = 'field_' . $field_id;
  121. unset( $field['data']['send_email'] );
  122. unset( $field['data']['replyto_email'] );
  123. unset( $field['data']['from_name'] );
  124. $args = array(
  125. 'update_array' => array(
  126. 'data' => serialize( $field['data'] ),
  127. ),
  128. 'where' => array(
  129. 'id' => $field_id,
  130. ),
  131. );
  132. ninja_forms_update_field( $args );
  133. }
  134. }
  135. if ( ! empty ( $addresses ) ) {
  136. // We have a user email, so create a notification
  137. $n_id = nf_insert_notification( $form_id );
  138. // Update our notification type
  139. nf_update_object_meta( $n_id, 'type', 'email' );
  140. // Activate our notification
  141. Ninja_Forms()->notification( $n_id )->activate();
  142. // Update our notification name
  143. Ninja_Forms()->notification( $n_id )->update_setting( 'name', __( 'User Email', 'ninja-forms' ) );
  144. // Implode our admin email addresses
  145. $n_var = count ( $addresses ) > 1 ? 'bcc' : 'to';
  146. $addresses = implode( '`', $addresses );
  147. Ninja_Forms()->notification( $n_id )->update_setting( $n_var, $addresses );
  148. // Update our Format Setting
  149. Ninja_Forms()->notification( $n_id )->update_setting( 'email_format', $form_settings['email_type'] );
  150. // Update our subject
  151. $subject = $this->replace_shortcodes( $form_settings['user_subject'] );
  152. Ninja_Forms()->notification( $n_id )->update_setting( 'email_subject', $subject );
  153. // Update our From Name
  154. if ( isset ( $form_settings['email_from_name'] ) ) {
  155. Ninja_Forms()->notification( $n_id )->update_setting( 'from_name', $form_settings['email_from_name'] );
  156. }
  157. // Update our From Address
  158. Ninja_Forms()->notification( $n_id )->update_setting( 'from_address', $form_settings['email_from'] );
  159. $email_message = $form_settings['user_email_msg'];
  160. // Check to see if the "include list of fields" checkbox was checked. If so, add our table to the end of the email message.
  161. if ( isset ( $form_settings['user_email_fields'] ) && $form_settings['user_email_fields'] == 1 ) {
  162. $email_message .= '<br />[ninja_forms_all_fields]';
  163. }
  164. // Update our email message
  165. Ninja_Forms()->notification( $n_id )->update_setting( 'email_message', $email_message );
  166. Ninja_Forms()->notification( $n_id )->update_setting( 'user_email', true );
  167. }
  168. }
  169. if ( isset ( $form_settings['success_msg'] ) && ! empty( $form_settings['success_msg'] ) ) {
  170. // Create a notification for our success message
  171. $n_id = nf_insert_notification( $form_id );
  172. nf_update_object_meta( $n_id, 'name', __( 'Success Message', 'ninja-forms' ) );
  173. nf_update_object_meta( $n_id, 'type', 'success_message' );
  174. nf_update_object_meta( $n_id, 'active', 1 );
  175. nf_update_object_meta( $n_id, 'success_msg', $form_settings['success_msg'] );
  176. }
  177. if ( isset ( $form_settings['landing_page'] ) && ! empty( $form_settings['landing_page'] ) ) {
  178. // Create a notification for our redirect
  179. $n_id = nf_insert_notification( $form_id );
  180. nf_update_object_meta( $n_id, 'name', __( 'Redirect', 'ninja-forms' ) );
  181. nf_update_object_meta( $n_id, 'type', 'redirect' );
  182. nf_update_object_meta( $n_id, 'active', 1 );
  183. nf_update_object_meta( $n_id, 'redirect_url', $form_settings['landing_page'] );
  184. }
  185. $completed_forms = get_option( 'nf_convert_notifications_forms' );
  186. if ( ! is_array( $completed_forms ) || empty ( $completed_forms ) ) {
  187. $completed_forms = array( $form_id );
  188. } else {
  189. $completed_forms[] = $form_id;
  190. }
  191. update_option( 'nf_convert_notifications_forms', $completed_forms );
  192. // Unset our admin email settings and save the form.
  193. if ( isset ( $form_settings['admin_mailto'] ) ) {
  194. unset( $form_settings['admin_mailto'] );
  195. unset( $form_settings['admin_attach_csv'] );
  196. unset( $form_settings['admin_subject'] );
  197. unset( $form_settings['admin_email_msg'] );
  198. unset( $form_settings['admin_email_fields'] );
  199. unset( $form_settings['success_msg'] );
  200. unset( $form_settings['user_email_msg'] );
  201. unset( $form_settings['user_subject'] );
  202. unset( $form_settings['landing_page'] );
  203. $args = array(
  204. 'update_array' => array(
  205. 'data' => serialize( $form_settings ),
  206. ),
  207. 'where' => array(
  208. 'id' => $form_id,
  209. ),
  210. );
  211. ninja_forms_update_form( $args );
  212. }
  213. }
  214. public function complete()
  215. {
  216. update_option( 'nf_convert_notifications_complete', true );
  217. }
  218. public function isComplete()
  219. {
  220. return get_option( 'nf_convert_notifications_complete', false );
  221. }
  222. /*
  223. * PRIVATE METHODS
  224. */
  225. private function removeOldEmailSettings()
  226. {
  227. nf_change_email_fav();
  228. nf_remove_old_email_settings();
  229. }
  230. private function getFormCount()
  231. {
  232. $forms = Ninja_Forms()->forms()->get_all();
  233. return count( $forms );
  234. }
  235. private function getAllForms()
  236. {
  237. $forms = Ninja_Forms()->forms()->get_all();
  238. $tmp_array = array();
  239. $x = 0;
  240. foreach ( $forms as $form_id ) {
  241. $tmp_array[ $x ]['id'] = $form_id;
  242. $tmp_array[ $x ]['data'] = Ninja_Forms()->form( $form_id )->get_all_settings();
  243. $tmp_array[ $x ]['name'] = Ninja_Forms()->form( $form_id )->get_setting( 'form_title' );
  244. $x++;
  245. }
  246. return $tmp_array;
  247. }
  248. private function replace_shortcodes( $text ) {
  249. $matches = array();
  250. $pattern = get_shortcode_regex();
  251. preg_match_all('/'.$pattern.'/s', $text, $matches);
  252. if ( is_array( $matches ) && ! empty( $matches[2] ) ) {
  253. foreach ( $matches[2] as $key => $shortcode ) {
  254. if ( $shortcode == 'ninja_forms_field' ) {
  255. if ( isset ( $matches[3][ $key ] ) ) {
  256. $atts = shortcode_parse_atts( $matches[3][ $key ] );
  257. $id = $atts['id'];
  258. $text = str_replace( $matches[0][ $key ], '`field_' . $id . '`', $text );
  259. }
  260. }
  261. }
  262. }
  263. return $text;
  264. }
  265. }