-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconditionals.php
360 lines (301 loc) · 11.3 KB
/
conditionals.php
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php
/*
* Plugin Name: Ninja Forms - Conditional Logic
* Plugin URI: https://ninjaforms.com/extensions/conditional-logic/
* Description: Conditional form logic add-on for Ninja Forms.
* Version: 3.1
* Author: The WP Ninjas
* Author URI: https://ninjaforms.com
* Text Domain: ninja-forms-conditionals
* Domain Path: /lang/
*/
if( ! class_exists( 'NF_ConditionalLogic_Conversion', false ) ) {
require_once 'lib/conversion.php';
}
if( version_compare( get_option( 'ninja_forms_version', '0.0.0' ), '3', '<' ) || get_option( 'ninja_forms_load_deprecated', FALSE ) ) {
if( ! defined( 'NINJA_FORMS_CON_DIR' ) ){
define("NINJA_FORMS_CON_DIR", plugin_dir_path(__FILE__) . '/deprecated');
}
if( ! defined( 'NINJA_FORMS_CON_URL' ) ) {
define("NINJA_FORMS_CON_URL", plugin_dir_url(__FILE__) . '/deprecated');
}
if( ! defined( 'NINJA_FORMS_CON_VERSION' ) ) {
define("NINJA_FORMS_CON_VERSION", "3.1");
}
include 'deprecated/conditionals.php';
} else {
if( class_exists( 'NF_ConditionalLogic', false ) ) return;
/**
* Class NF_ConditionalLogic
*/
final class NF_ConditionalLogic
{
const VERSION = '3.1';
const SLUG = 'conditional-logic';
const NAME = 'Conditional Logic';
const AUTHOR = 'The WP Ninjas';
const PREFIX = 'NF_ConditionalLogic';
/**
* Condition Triggers
*
* @since 3.0
* @var array
*/
public $triggers = array();
/**
* Condition Comparators
*
* @since 3.0
* @var array
*/
public $comparators = array();
/**
* Integrations
*
* @since 3.0
* @var array
*/
public $integrations = array();
/**
* NF_ConditionalLogic constructor.
*/
public function __construct()
{
// WordPress Hooks
add_action( 'init', array( $this, 'init' ) );
add_action( 'admin_init', array( $this, 'setup_license' ) );
// Ninja Forms Hooks
add_action( 'ninja_forms_loaded', array( $this, 'setup_admin' ) );
// Ninja Forms Admin Hooks
add_filter( 'ninja_forms_field_settings_groups', array( $this, 'register_settings_groups' ) );
add_filter( 'ninja_forms_action_settings', array( $this, 'register_action_settings' ) );
add_filter( 'ninja_forms_actions_settings_all', array( $this, 'action_settings_all' ) );
add_filter( 'nf_admin_enqueue_scripts', array( $this, 'builder_scripts' ) );
add_action( 'ninja_forms_builder_templates', array( $this, 'builder_templates' ) );
/*
* Add action conditions to the form conditions localised data
*/
add_filter( 'ninja_forms_display_form_settings', array( $this, 'localize_action_conditions' ), 10, 2 );
}
public function init()
{
new NF_ConditionalLogic_Submission();
self::$instance->integrations = array(
new NF_ConditionalLogic_Integrations_MultiPart()
);
self::$instance->triggers = NF_ConditionalLogic::config( 'Triggers' );
self::$instance->comparators = NF_ConditionalLogic::config( 'Comparators' );
}
public function setup_admin()
{
if( ! is_admin() ) return;
new NF_ConditionalLogic_Admin_Settings();
}
public function register_settings_groups( $settings_groups )
{
return array_merge( $settings_groups, self::config( 'ActionSettingsGroups' ) );
}
public function register_action_settings( $action_settings )
{
return array_merge( $action_settings, self::config( 'ActionSettings' ) );
}
public function action_settings_all( $settings_all )
{
array_push( $settings_all, 'conditions' ); // TODO: Add registered settings names.
return $settings_all;
}
public function builder_scripts()
{
$ver = self::VERSION;
wp_enqueue_style( 'nf-cl-builder', plugin_dir_url( __FILE__ ) . 'assets/css/builder.css', array(), $ver );
wp_enqueue_script( 'nf-cl-builder', plugin_dir_url( __FILE__ ) . 'assets/js/min/builder.js', array(), $ver );
wp_localize_script( 'nf-cl-builder', 'nfcli18n', self::config( 'i18nCLBuilder' ) );
wp_localize_script( 'nf-cl-builder', 'nfListCountries', Ninja_Forms::config( 'CountryList' ) );
}
public function builder_templates()
{
NF_ConditionalLogic::template( 'builder-edit-settings.html.php' );
}
/*
|--------------------------------------------------------------------------
| Internal API Methods
|--------------------------------------------------------------------------
*/
/**
* Comparator
*
* @since 3.0.0
* @param $key
* @return NF_ConditionalLogic_Comparator
* @throws Exception
*/
public function comparator( $key )
{
if( isset( $this->comparators[ $key ] ) ) return $this->comparators[ $key ][ 'instance' ];
return new NF_ConditionalLogic_Comparators_NullComparator();
}
/**
* Trigger
*
* @since 3.0.0
* @param $key
* @return NF_ConditionalLogic_Trigger
* @throws Exception
*/
public function trigger( $key )
{
if( isset( $this->triggers[ $key ] ) ) return $this->triggers[ $key ][ 'instance' ];
return false;
}
/*
|--------------------------------------------------------------------------
| Plugin Properties and Methods
|--------------------------------------------------------------------------
*/
/**
* @var NF_ConditionalLogic
* @since 3.0
*/
private static $instance;
/**
* Plugin Directory
*
* @since 3.0
* @var string $dir
*/
public static $dir = '';
/**
* Plugin URL
*
* @since 3.0
* @var string $url
*/
public static $url = '';
/**
* Main Plugin Instance
*
* Insures that only one instance of a plugin class exists in memory at any one
* time. Also prevents needing to define globals all over the place.
*
* @since 3.0
* @static
* @static var array $instance
* @return NF_ConditionalLogic Highlander Instance
*/
public static function instance()
{
if (!isset(self::$instance) && !(self::$instance instanceof NF_ConditionalLogic)) {
self::$instance = new NF_ConditionalLogic();
self::$dir = plugin_dir_path(__FILE__);
self::$url = plugin_dir_url(__FILE__);
/*
* Register our autoloader
*/
spl_autoload_register(array(self::$instance, 'autoloader'));
}
return self::$instance;
}
/**
* Template
*
* @param string $file_name
* @param array $data
*/
public static function template( $file_name = '', array $data = array() )
{
if( ! $file_name ) return;
extract( $data );
include self::$dir . 'includes/Templates/' . $file_name;
}
/**
* Config
*
* @param $file_name
* @return mixed
*/
public static function config( $file_name )
{
return include self::$dir . 'includes/Config/' . $file_name . '.php';
}
/**
* Autoloader
*
* Loads files using the class name to mimic the folder structure.
*
* @param $class_name
*/
public function autoloader($class_name)
{
if (class_exists($class_name)) return;
if ( false === strpos( $class_name, self::PREFIX ) ) return;
$class_name = str_replace( self::PREFIX, '', $class_name );
$classes_dir = realpath(plugin_dir_path(__FILE__)) . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR;
$class_file = str_replace('_', DIRECTORY_SEPARATOR, $class_name) . '.php';
if (file_exists($classes_dir . $class_file)) {
require_once $classes_dir . $class_file;
}
}
/**
* Setup License
*
* Registers the plugin with the extension updater.
*/
public function setup_license()
{
if ( ! class_exists( 'NF_Extension_Updater' ) ) return;
new NF_Extension_Updater( self::NAME, self::VERSION, self::AUTHOR, __FILE__, self::SLUG );
}
public function localize_action_conditions( $settings, $form_id ) {
/*
* Get any action conditions
*/
$action_conditions = array();
$form_actions = Ninja_Forms()->form( $form_id )->get_actions();
foreach( $form_actions as $action ){
$conditions = $action->get_setting( 'conditions' );
if( ! isset ( $conditions[ 'when' ][0][ 'key' ] ) || empty( $conditions[ 'when' ][0][ 'key' ] ) ) continue;
/*
* Make sure that we have a valid "then" setting.
*/
if ( ! isset ( $conditions[ 'then' ][0][ 'key' ] ) || empty( $conditions[ 'then' ][0][ 'key' ] ) ) {
$conditions[ 'then' ][0][ 'key' ] = $action->get_id();
$conditions[ 'then' ][0][ 'trigger' ] = 'activate_action';
$conditions[ 'then' ][0][ 'type' ] = 'action';
}
/*
* Make sure that we have a valid "else" setting.
*/
if ( ! isset ( $conditions[ 'else' ][0][ 'key' ] ) || empty( $conditions[ 'else' ][0][ 'key' ] ) ) {
$conditions[ 'else' ][0][ 'key' ] = $action->get_id();
$conditions[ 'else' ][0][ 'trigger' ] = 'deactivate_action';
$conditions[ 'else' ][0][ 'type' ] = 'action';
$conditions[ 'else' ][0][ 'modelType' ] = 'else';
}
$settings[ 'conditions' ][] = $conditions;
}
return $settings;
}
}
/**
* The main function responsible for returning The Highlander Plugin
* Instance to functions everywhere.
*
* Use this function like you would a global variable, except without needing
* to declare the global.
*
* @since 3.0
* @return NF_ConditionalLogic Highlander Instance
*/
function NF_ConditionalLogic()
{
return NF_ConditionalLogic::instance();
}
NF_ConditionalLogic();
/*
* Localize our mock conditional logic data
*/
function nf_cl_display_mock_data( $form_id ) {
wp_enqueue_script( 'nf-cl-front-end', plugin_dir_url( __FILE__ ) . 'assets/js/min/front-end.js', array( 'nf-front-end' ) );
}
add_action( 'ninja_forms_enqueue_scripts', 'nf_cl_display_mock_data' );
}