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