NF_Notifications

Main Notifications Class.

Defined (1)

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

/deprecated/classes/notifications.php  
  1. class NF_Notifications 
  2. /** 
  3. * Get things rolling 
  4. * @access public 
  5. * @since 2.8 
  6. */ 
  7. function __construct() { 
  8. global $pagenow; 
  9.  
  10. // Register our notification types 
  11. Ninja_Forms()->notification_types['email'] = require_once( NF_PLUGIN_DIR . 'classes/notification-email.php' ); 
  12. Ninja_Forms()->notification_types['redirect'] = require_once( NF_PLUGIN_DIR . 'classes/notification-redirect.php' ); 
  13. Ninja_Forms()->notification_types['success_message'] = require_once( NF_PLUGIN_DIR . 'classes/notification-success-message.php' ); 
  14.  
  15. Ninja_Forms()->notification_types = apply_filters( 'nf_notification_types', Ninja_Forms()->notification_types ); 
  16.  
  17. // Register our notification tab 
  18. add_action( 'admin_init', array( $this, 'register_tab' ) ); 
  19.  
  20. // Only add these actions if we are actually on the notification tab. 
  21. if ( 'admin.php' == $pagenow && isset ( $_REQUEST['page'] ) && $_REQUEST['page'] == 'ninja-forms' && isset ( $_REQUEST['tab'] ) && $_REQUEST['tab'] == 'notifications' ) { 
  22. add_action( 'admin_init', array( $this, 'add_js' ) ); 
  23. add_action( 'admin_init', array( $this, 'add_css' ) ); 
  24. add_action( 'admin_init', array( $this, 'bulk_actions' ) ); 
  25. add_action( 'admin_init', array( $this, 'duplicate_notification' ) ); 
  26. add_filter( 'media_buttons_context', array( $this, 'tinymce_buttons' ) ); 
  27.  
  28. add_action( 'wp_ajax_nf_delete_notification', array( $this, 'delete_notification' ) ); 
  29. add_action( 'wp_ajax_nf_activate_notification', array( $this, 'activate_notification' ) ); 
  30. add_action( 'wp_ajax_nf_deactivate_notification', array( $this, 'deactivate_notification' ) ); 
  31.  
  32.  
  33.  
  34. // Add our hook to add notification types processors. 
  35. add_action( 'ninja_forms_post_process', array( $this, 'notification_processing' ), 999 ); 
  36.  
  37. /** 
  38. * Register our setting tab. 
  39. * @access public 
  40. * @since 2.8 
  41. * @return void 
  42. */ 
  43. public function register_tab() { 
  44. $form_id = isset ( $_REQUEST['form_id'] ) ? absint( $_REQUEST['form_id'] ) : ''; 
  45. $action = isset ( $_REQUEST['notification-action'] ) ? esc_html( $_REQUEST['notification-action'] ) : ''; 
  46. $output_form = false; 
  47. $show_save = false; 
  48. if ( 'edit' == $action || 'new' == $action ) { 
  49. $output_form = true; 
  50. $show_save = true; 
  51.  
  52. $args = array( 
  53. 'name' => __( 'Email & Actions', 'ninja-forms' ),  
  54. 'page' => 'ninja-forms',  
  55. 'display_function' => array( $this, 'output_admin' ),  
  56. 'save_function' => array( $this, 'save_admin' ),  
  57. 'disable_no_form_id' => true,  
  58. 'show_save' => $show_save,  
  59. 'tab_reload' => true,  
  60. 'output_form' => $output_form,  
  61. ); 
  62.  
  63. ninja_forms_register_tab( 'notifications', $args ); 
  64.  
  65. /** 
  66. * Enqueue JS 
  67. * @access public 
  68. * @since 2.8 
  69. * @return void 
  70. */ 
  71. public function add_js() { 
  72. global $ninja_forms_fields; 
  73.  
  74. $form_id = isset ( $_REQUEST['form_id'] ) ? absint( $_REQUEST['form_id'] ) : ''; 
  75. if ( empty ( $form_id ) ) 
  76. return false; 
  77.  
  78. if ( defined( 'NINJA_FORMS_JS_DEBUG' ) && NINJA_FORMS_JS_DEBUG ) { 
  79. $suffix = ''; 
  80. $src = 'dev'; 
  81. } else { 
  82. $suffix = '.min'; 
  83. $src = 'min'; 
  84.  
  85. wp_enqueue_script( 'nf-notifications',  
  86. NF_PLUGIN_URL . 'assets/js/' . $src .'/notifications' . $suffix . '.js',  
  87. array( 'jquery', 'jquery-ui-autocomplete' ) ); 
  88.  
  89. wp_enqueue_script( 'nf-tokenize',  
  90. NF_PLUGIN_URL . 'assets/js/' . $src .'/bootstrap-tokenfield' . $suffix . '.js',  
  91. array( 'jquery', 'jquery-ui-autocomplete' ) ); 
  92.  
  93. wp_enqueue_script( 'nf-combobox',  
  94. NF_PLUGIN_URL . 'assets/js/' . $src .'/combobox' . $suffix . '.js',  
  95. array( 'jquery', 'jquery-ui-core', 'jquery-ui-button', 'jquery-ui-autocomplete' ) ); 
  96.  
  97. $all_fields = Ninja_Forms()->form( $form_id )->fields; 
  98. $process_fields = array(); 
  99. $search_fields = array(); 
  100. $search_fields['email'] = array(); 
  101. $search_fields['name'] = array(); 
  102. $fields = array(); 
  103. // Generate our search fields JS var. 
  104. foreach( $all_fields as $field_id => $field ) { 
  105. $label = esc_attr( nf_get_field_admin_label( $field_id ) ); 
  106.  
  107. $fields[ $field_id ] = array( 'field_id' => $field_id, 'label' => $label ); 
  108.  
  109. if ( strlen( $label ) > 30 ) { 
  110. $tmp_label = substr( $label, 0, 30 ); 
  111. } else { 
  112. $tmp_label = $label; 
  113.  
  114. $tmp_array = array( 'value' => 'field_' . $field_id, 'label' => $tmp_label . ' - ID: ' . $field_id ); 
  115.  
  116. $admin_label = $label; 
  117.  
  118. $label = isset( $field['data']['label'] ) ? $field['data']['label'] : ''; 
  119.  
  120. // Check to see if this field is supposed to be "processed" 
  121. $type = $field['type']; 
  122. if ( isset ( $ninja_forms_fields[ $type ]['process_field'] ) && $ninja_forms_fields[ $type ]['process_field'] ) { 
  123. $process_fields[ $field_id ] = array( 'field_id' => $field_id, 'label' => $label, 'admin_label' => $admin_label ); 
  124. $search_fields['all'][] = $tmp_array; 
  125.  
  126. if ( $field['type'] == '_text' && isset ( $field['data']['email'] ) && $field['data']['email'] == 1 ) { 
  127. $search_fields['email'][] = $tmp_array; 
  128. } else if ( $field['type'] == '_text' && isset ( $field['data']['first_name'] ) && $field['data']['first_name'] == 1 ) { 
  129. $search_fields['name'][] = $tmp_array; 
  130. } else if ( $field['type'] == '_text' && isset ( $field['data']['last_name'] ) && $field['data']['last_name'] == 1 ) { 
  131. $search_fields['name'][] = $tmp_array; 
  132.  
  133. // Add our "process_fields" to our form global 
  134. Ninja_Forms()->form( $form_id )->process_fields = $process_fields; 
  135.  
  136. $js_vars = apply_filters( 'nf_notification_admin_js_vars', array( 
  137. 'activate' => __( 'Activate', 'ninja-forms' ),  
  138. 'deactivate' => __( 'Deactivate', 'ninja-forms' ),  
  139. 'search_fields' => $search_fields,  
  140. 'tokens' => array(),  
  141. 'all_fields' => $fields,  
  142. 'process_fields' => $process_fields,  
  143. 'filter_type' => esc_url_raw( remove_query_arg( array( 'type' ) ) ),  
  144. ) ); 
  145.  
  146. wp_localize_script( 'nf-notifications', 'nf_notifications', $js_vars ); 
  147.  
  148.  
  149. /** 
  150. * Enqueue CSS 
  151. * @access public 
  152. * @since 2.8 
  153. * @return void 
  154. */ 
  155. public function add_css() { 
  156. wp_enqueue_style( 'nf-notifications',  
  157. NF_PLUGIN_URL . 'assets/css/notifications.css' ); 
  158.  
  159. wp_enqueue_style( 'nf-tokenize',  
  160. NF_PLUGIN_URL . 'assets/css/bootstrap-tokenfield.css' ); 
  161.  
  162. wp_enqueue_style( 'nf-combobox',  
  163. NF_PLUGIN_URL . 'assets/css/combobox.css' ); 
  164.  
  165. // wp_enqueue_style( 'nf-bootstrap',  
  166. // 'http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css' ); 
  167.  
  168. /** 
  169. * Output our notifications admin. 
  170. * @access public 
  171. * @since 2.8 
  172. * @return void 
  173. */ 
  174. public function output_admin() { 
  175. $action = isset ( $_REQUEST['notification-action'] ) ? esc_html( $_REQUEST['notification-action'] ) : ''; 
  176.  
  177. ?> 
  178. <div class="wrap"> 
  179. <?php 
  180. if ( '' == $action ) { 
  181. ?> 
  182. <h2><?php _e( 'Email & Actions', 'ninja-forms' ); ?> <a href="<?php echo esc_url( add_query_arg( array( 'notification-action' => 'new' ) ) ); ?>" class="add-new-h2"><?php _e( 'Add New', 'ninja-forms' );?></a></h2> 
  183.  
  184. <!-- Forms are NOT created automatically, so you need to wrap the table in one to use features like bulk actions --> 
  185. <form id="forms-filter" method="get"> 
  186. <!-- For plugins, we also need to ensure that the form posts back to our current page --> 
  187. <input type="hidden" name="page" value="<?php echo esc_attr( $_REQUEST['page'] ); ?>" /> 
  188. <input type="hidden" name="tab" value="<?php echo esc_attr( $_REQUEST['tab'] ); ?>" /> 
  189. <input type="hidden" name="form_id" value="<?php echo esc_attr( $_REQUEST['form_id'] ); ?>" /> 
  190. <?php 
  191. //Create an instance of our package class... 
  192. $nf_all_forms = new NF_Notifications_List_Table(); 
  193. //Fetch, prepare, sort, and filter our data... 
  194. $nf_all_forms->prepare_items(); 
  195. // Now we can render the completed list table 
  196. $nf_all_forms->display(); 
  197. ?> 
  198. </form> 
  199. <?php 
  200. } else { 
  201. $id = isset ( $_REQUEST['id'] ) ? absint( $_REQUEST['id'] ) : ''; 
  202. if ( $id == '' ) { 
  203. $id = 'new'; 
  204. $this_type = 'email'; 
  205. $title = __( 'New Action', 'ninja-forms' ); 
  206. } else { 
  207. $this_type = Ninja_Forms()->notification( $id )->type; 
  208. $title = __( 'Edit Action', 'ninja-forms' ) . ' - ID ' . $id; 
  209.  
  210. ?> 
  211. <h2><?php echo $title; ?> <a href="<?php echo esc_url( remove_query_arg( array( 'notification-action', 'id', 'update_message' ) ) );?>" class="button-secondary"><?php _e( 'Back To List', 'ninja-forms' );?></a></h2> 
  212.  
  213. <input type="hidden" id="notification_id" name="notification_id" value="<?php echo $id; ?>" /> 
  214. <table class="form-table"> 
  215. <tbody id="notification-main"> 
  216. <tr> 
  217. <th scope="row"><label for="setting-name"><?php _e( 'Action Name', 'ninja-forms' ); ?></label></th> 
  218. <td><input name="settings[name]" type="text" id="settings-name" value="<?php echo nf_get_object_meta_value( $id, 'name' ); ?>" class="regular-text"></td> 
  219. </tr> 
  220. <tr> 
  221. <th scope="row"><label for="type"><?php _e( 'Type', 'ninja-forms' ); ?></label></th> 
  222. <td> 
  223. <select name="settings[type]" id="settings-type"> 
  224. <?php 
  225. foreach ( $this->get_types() as $slug => $nicename ) { 
  226. ?> 
  227. <option value="<?php echo $slug; ?>" <?php selected ( $this_type, $slug ); ?>><?php echo $nicename; ?></option> 
  228. <?php 
  229. ?> 
  230. </select> 
  231. <span class="nf-more-actions"><a href="<?php echo nf_aff_link( 'https://ninjaforms.com/extensions/?display=actions&utm_medium=plugin&utm_source=action-single&utm_campaign=Ninja+Forms+Upsell&utm_content=Ninja+Forms+Actions' ); ?>" target="_blank"><?php _e( 'Get More Actions', 'ninja-forms' ); ?> <span class="dashicons dashicons-external"></span></a></span> 
  232. </td> 
  233. </tr> 
  234. </tbody> 
  235. <?php 
  236. do_action( 'nf_edit_notification_settings', $id ); 
  237. foreach ( $this->get_types() as $slug => $nicename ) { 
  238. if ( $this_type == $slug ) { 
  239. $display = ''; 
  240. } else { 
  241. $display = 'display:none;'; 
  242. ?> 
  243. <tbody id="notification-<?php echo $slug; ?>" class="notification-type" style="<?php echo $display;?>"> 
  244. <?php 
  245. // Call our type edit screen. 
  246. Ninja_Forms()->notification_types[ $slug ]->edit_screen( $id ); 
  247. ?> 
  248. </tbody> 
  249. <?php 
  250. ?> 
  251. </table> 
  252. <?php 
  253. } ?> 
  254.  
  255. </div> 
  256. <?php 
  257.  
  258. /** 
  259. * Save our notifications admin. 
  260. * @access public 
  261. * @since 2.8 
  262. * @return void 
  263. */ 
  264. public function save_admin( $form_id, $data ) { 
  265. if ( ! isset ( $data['notification_id'] ) || empty ( $data['notification_id'] ) ) 
  266. return false; 
  267.  
  268. $n_id = $data['notification_id']; 
  269. $settings = $data['settings']; 
  270.  
  271. if ( 'new' == $n_id ) { 
  272. $type = $settings['type']; 
  273. $n_id = $this->create( $form_id ); 
  274. $new = true; 
  275. } else { 
  276. $type = Ninja_Forms()->notification( $n_id )->type; 
  277. $new = false; 
  278.  
  279. $data = Ninja_Forms()->notification_types[ $type ]->save_admin( $n_id, $data ); 
  280.  
  281. foreach ( $settings as $meta_key => $meta_value ) { 
  282. nf_update_object_meta( $n_id, $meta_key, nf_wp_kses_post_deep( $meta_value ) ); 
  283.  
  284. do_action( 'nf_save_notification', $n_id, $data, $new ); 
  285.  
  286. if ( $new ) { 
  287. $redirect = esc_url_raw( remove_query_arg( array( 'notification-action' ) ) ); 
  288. $redirect = esc_url_raw( add_query_arg( array( 'id' => $n_id, 'notification-action' => 'edit', 'update_message' => urlencode( __( 'Action Updated', 'ninja-forms' ) ) ), $redirect ) ); 
  289. wp_redirect( $redirect ); 
  290. die(); 
  291.  
  292. return __( 'Action Updated', 'ninja-forms' ); 
  293.  
  294. /** 
  295. * Get our registered notification types 
  296. * @access public 
  297. * @since 2.8 
  298. * @return array $types 
  299. */ 
  300. public function get_types() { 
  301. $types = array(); 
  302. foreach ( Ninja_Forms()->notification_types as $slug => $object ) { 
  303. $types[ $slug ] = $object->name; 
  304. return $types; 
  305.  
  306. /** 
  307. * Delete a notification. 
  308. * Hooked into the ajax action for nf_delete_notification 
  309. * @access public 
  310. * @since 2.8 
  311. * @return void 
  312. */ 
  313. public function delete_notification() { 
  314. // Bail if our nonce doesn't verify. 
  315. check_ajax_referer( 'nf_ajax', 'nf_ajax_nonce' ); 
  316.  
  317. $n_id = absint( $_REQUEST['n_id'] ); 
  318. Ninja_Forms()->notification( $n_id )->delete(); 
  319.  
  320. /** 
  321. * Activate a notification. 
  322. * Hooked into the ajax action for nf_activate_notification 
  323. * @access public 
  324. * @since 2.8 
  325. * @return void 
  326. */ 
  327. public function activate_notification() { 
  328. // Bail if our nonce doesn't verify. 
  329. check_ajax_referer( 'nf_ajax', 'nf_ajax_nonce' ); 
  330.  
  331. $n_id = absint( $_REQUEST['n_id'] ); 
  332. Ninja_Forms()->notification( $n_id )->activate(); 
  333.  
  334. /** 
  335. * Deactivate a notification. 
  336. * Hooked into the ajax action for nf_deactivate_notification 
  337. * @access public 
  338. * @since 2.8 
  339. * @return void 
  340. */ 
  341. public function deactivate_notification() { 
  342. // Bail if our nonce doesn't verify. 
  343. check_ajax_referer( 'nf_ajax', 'nf_ajax_nonce' ); 
  344.  
  345. $n_id = absint( $_REQUEST['n_id'] ); 
  346. Ninja_Forms()->notification( $n_id )->deactivate(); 
  347.  
  348. /** 
  349. * Duplicate our notification 
  350. * @access public 
  351. * @since 2.8 
  352. * @return void 
  353. */ 
  354. public function duplicate_notification() { 
  355. if ( ! isset ( $_REQUEST['notification-action'] ) || $_REQUEST['notification-action'] != 'duplicate' ) 
  356. return false; 
  357.  
  358. $n_id = isset ( $_REQUEST['id'] ) ? absint( $_REQUEST['id'] ) : ''; 
  359.  
  360. // Bail if we don't have an ID. 
  361. if ( '' === $n_id ) 
  362. return false; 
  363.  
  364. Ninja_Forms()->notification( $n_id )->duplicate(); 
  365.  
  366. wp_redirect( esc_url_raw( remove_query_arg( array( 'notification-action' ) ) ) ); 
  367. die(); 
  368.  
  369. /** 
  370. * Create a new notification 
  371. * @access public 
  372. * @since 2.8 
  373. * @return int $n_id 
  374. */ 
  375. public function create( $form_id = '' ) { 
  376. // Bail if we don't have a form_id 
  377. if ( '' == $form_id ) 
  378. return false; 
  379.  
  380. $n_id = nf_insert_notification( $form_id ); 
  381.  
  382. // Activate our new notification 
  383. Ninja_Forms()->notification( $n_id )->activate(); 
  384.  
  385. return $n_id; 
  386.  
  387. /** 
  388. * Handle bulk actions 
  389. * @access public 
  390. * @since 2.8 
  391. * @return void 
  392. */ 
  393. public function bulk_actions() { 
  394. $action = ''; 
  395.  
  396. if ( isset( $_REQUEST['action2'] ) && -1 != $_REQUEST['action2'] ) 
  397. $action = esc_html( $_REQUEST['action2'] ); 
  398.  
  399. if ( isset( $_REQUEST['action'] ) && -1 != $_REQUEST['action'] ) 
  400. $action = esc_html( $_REQUEST['action'] ); 
  401.  
  402. $n_ids = isset ( $_REQUEST['notification'] ) ? esc_html( $_REQUEST['notification'] ) : ''; 
  403.  
  404. if ( ! is_array( $n_ids ) || empty( $n_ids ) ) 
  405. return false; 
  406.  
  407. if( 'delete' === $action ) { 
  408. foreach ( $n_ids as $n_id ) { 
  409. Ninja_Forms()->notification( $n_id )->delete(); 
  410. } else if ( 'activate' === $action ) { 
  411. foreach ( $n_ids as $n_id ) { 
  412. Ninja_Forms()->notification( $n_id )->activate(); 
  413. } else if ( 'deactivate' === $action ) { 
  414. foreach ( $n_ids as $n_id ) { 
  415. Ninja_Forms()->notification( $n_id )->deactivate(); 
  416.  
  417. wp_redirect( esc_url_raw( remove_query_arg( array( 'notification', '_wpnonce', '_wp_http_referer', 'action', 'action2' ) ) ) ); 
  418. die(); 
  419.  
  420. /** 
  421. * Output our tinyMCE field buttons 
  422. * @access public 
  423. * @since 2.8 
  424. * @return void 
  425. */ 
  426. public function tinymce_buttons( $context ) { 
  427. $form_id = isset ( $_REQUEST['form_id'] ) ? absint( $_REQUEST['form_id'] ) : ''; 
  428. if ( empty ( $form_id ) ) 
  429. return $context; 
  430.  
  431. $all_fields = Ninja_Forms()->form( $form_id )->process_fields; 
  432. $first_option = __( 'Select a field or type to search', 'ninja-forms' ); 
  433.  
  434. $fields = array(); 
  435. $html = '<select class="nf-fields-combobox" data-first-option="' . $first_option . '">'; 
  436. $html .= '<option value="">' . $first_option .'</option>'; 
  437. foreach( $all_fields as $field_id => $field ) { 
  438. $label = esc_html( $field['label'] ); 
  439. if ( strlen( $label ) > 30 ) 
  440. $label = substr( $label, 0, 30 ) . '...'; 
  441.  
  442. $html .= '<option value="' . $field_id . '">' . $label . ' - ID: ' . $field_id . '</option>'; 
  443. $html .= '</select>'; 
  444. $html .= ' <a href="#" class="button-secondary nf-insert-field">' . __( 'Insert Field', 'ninja-forms' ) . '</a> <a href="#" class="button-secondary nf-insert-all-fields">' . __( 'Insert All Fields', 'ninja-forms' ) . '</a>'; 
  445.  
  446. return $html; 
  447.  
  448. /** 
  449. * Loop through our notifications and add their processing functions to the appropriate hook. 
  450. * @access public 
  451. * @since 2.8 
  452. * @return void 
  453. */ 
  454. public function notification_processing() { 
  455. global $ninja_forms_processing; 
  456.  
  457. $form_id = $ninja_forms_processing->get_form_ID(); 
  458. $notifications = nf_get_notifications_by_form_id( $form_id, false ); 
  459. if ( is_array( $notifications ) ) { 
  460. foreach ( $notifications as $id ) { 
  461. do_action( 'nf_notification_before_process', $id ); 
  462. if ( Ninja_Forms()->notification( $id )->active ) { 
  463. Ninja_Forms()->notification( $id )->process(); 
  464.