ActionNewsletter.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. /**
  3. * Class NF_Abstracts_ActionNewsletter
  4. */
  5. abstract class NF_Abstracts_ActionNewsletter extends NF_Abstracts_Action
  6. {
  7. /**
  8. * @var array
  9. */
  10. protected $_tags = array( 'newsletter' );
  11. /**
  12. * @var string
  13. */
  14. protected $_timing = 'normal';
  15. /**
  16. * @var int
  17. */
  18. protected $_priority = '10';
  19. protected $_settings = array();
  20. protected $_transient = '';
  21. protected $_transient_expiration = '';
  22. protected $_setting_labels = array(
  23. 'list' => 'List',
  24. 'fields' => 'List Field Mapping',
  25. 'groups' => 'Interest Groups',
  26. );
  27. /**
  28. * Constructor
  29. */
  30. public function __construct()
  31. {
  32. parent::__construct();
  33. if( ! $this->_transient ){
  34. $this->_transient = $this->get_name() . '_newsletter_lists';
  35. }
  36. add_action( 'wp_ajax_nf_' . $this->_name . '_get_lists', array( $this, '_get_lists' ) );
  37. $this->get_list_settings();
  38. }
  39. /*
  40. * PUBLIC METHODS
  41. */
  42. public function save( $action_settings )
  43. {
  44. }
  45. public function process( $action_settings, $form_id, $data )
  46. {
  47. }
  48. public function _get_lists()
  49. {
  50. check_ajax_referer( 'ninja_forms_builder_nonce', 'security' );
  51. $lists = $this->get_lists();
  52. array_unshift( $lists, array( 'value' => 0, 'label' => '-', 'fields' => array(), 'groups' => array() ) );
  53. $this->cache_lists( $lists );
  54. echo wp_json_encode( array( 'lists' => $lists ) );
  55. wp_die(); // this is required to terminate immediately and return a proper response
  56. }
  57. /*
  58. * PROTECTED METHODS
  59. */
  60. abstract protected function get_lists();
  61. /*
  62. * PRIVATE METHODS
  63. */
  64. private function get_list_settings()
  65. {
  66. $label_defaults = array(
  67. 'list' => 'List',
  68. 'fields' => 'List Field Mapping',
  69. 'groups' => 'Interest Groups',
  70. );
  71. $labels = array_merge( $label_defaults, $this->_setting_labels );
  72. $prefix = $this->get_name();
  73. $lists = get_transient( $this->_transient );
  74. if( ! $lists ) {
  75. $lists = $this->get_lists();
  76. $this->cache_lists( $lists );
  77. }
  78. if( empty( $lists ) ) return;
  79. $this->_settings[ $prefix . 'newsletter_list' ] = array(
  80. 'name' => 'newsletter_list',
  81. 'type' => 'select',
  82. 'label' => $labels[ 'list' ] . ' <a class="js-newsletter-list-update extra"><span class="dashicons dashicons-update"></span></a>',
  83. 'width' => 'full',
  84. 'group' => 'primary',
  85. 'value' => '0',
  86. 'options' => array(),
  87. );
  88. $fields = array();
  89. foreach( $lists as $list ) {
  90. $this->_settings[ $prefix . 'newsletter_list' ][ 'options' ][] = $list;
  91. //Check to see if list has fields array set.
  92. if ( isset( $list[ 'fields' ] ) ) {
  93. foreach ( $list[ 'fields' ] as $field ) {
  94. $name = $list[ 'value' ] . '_' . $field[ 'value' ];
  95. $fields[] = array(
  96. 'name' => $name,
  97. 'type' => 'textbox',
  98. 'label' => $field[ 'label' ],
  99. 'width' => 'full',
  100. 'use_merge_tags' => array(
  101. 'exclude' => array(
  102. 'user', 'post', 'system', 'querystrings'
  103. )
  104. )
  105. );
  106. }
  107. }
  108. }
  109. $this->_settings[ $prefix . 'newsletter_list_fields' ] = array(
  110. 'name' => 'newsletter_list_fields',
  111. 'label' => __( 'List Field Mapping', 'ninja-forms' ),
  112. 'type' => 'fieldset',
  113. 'group' => 'primary',
  114. 'settings' => array()
  115. );
  116. $this->_settings[ $prefix . 'newsletter_list_groups' ] = array(
  117. 'name' => 'newsletter_list_groups',
  118. 'label' => __( 'Interest Groups', 'ninja-forms' ),
  119. 'type' => 'fieldset',
  120. 'group' => 'primary',
  121. 'settings' => array()
  122. );
  123. }
  124. private function cache_lists( $lists )
  125. {
  126. set_transient( $this->_transient, $lists, $this->_transient_expiration );
  127. }
  128. }