class-fl-builder-ajax.php
8.3 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
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
<?php
/**
* Front-end AJAX handler for the builder interface. We use this
* instead of wp_ajax because that only works in the admin and
* certain things like some shortcodes won't render there. AJAX
* requests handled through this method only run for logged in users
* for extra security. Developers creating custom modules that need
* AJAX should use wp_ajax instead.
*
* @since 1.7
*/
final class FLBuilderAJAX {
/**
* An array of registered action data.
*
* @since 1.7
* @access private
* @var array $actions
*/
static private $actions = array();
/**
* Initializes hooks.
*
* @since 1.8
* @return void
*/
static public function init()
{
add_action( 'wp', __CLASS__ . '::run' );
}
/**
* Runs builder's frontend AJAX.
*
* @since 1.7
* @return void
*/
static public function run()
{
self::add_actions();
self::call_action();
}
/**
* Adds a callable AJAX action.
*
* @since 1.7
* @param string $action The action name.
* @param string $method The method to call.
* @param array $args An array of method arg names that are present in the post data.
* @return void
*/
static public function add_action( $action, $method, $args = array() )
{
self::$actions[ $action ] = array(
'action' => $action,
'method' => $method,
'args' => $args
);
}
/**
* Removes an AJAX action.
*
* @since 1.8
* @param string $action The action to remove.
* @return void
*/
static public function remove_action( $action )
{
if ( isset( self::$actions[ $action ] ) ) {
unset( self::$actions[ $action ] );
}
}
/**
* Adds all callable AJAX actions.
*
* @since 1.7
* @access private
* @return void
*/
static private function add_actions()
{
// FLBuilder
self::add_action( 'render_settings_form', 'FLBuilder::render_settings_form', array( 'type', 'settings' ) );
self::add_action( 'render_row_settings', 'FLBuilder::render_row_settings', array( 'node_id' ) );
self::add_action( 'render_column_settings', 'FLBuilder::render_column_settings', array( 'node_id' ) );
self::add_action( 'render_module_settings', 'FLBuilder::render_module_settings', array( 'node_id', 'type', 'parent_id' ) );
self::add_action( 'render_layout_settings', 'FLBuilder::render_layout_settings' );
self::add_action( 'render_global_settings', 'FLBuilder::render_global_settings' );
self::add_action( 'render_template_selector', 'FLBuilder::render_template_selector' );
self::add_action( 'render_icon_selector', 'FLBuilder::render_icon_selector' );
// FLBuilderModel
self::add_action( 'delete_node', 'FLBuilderModel::delete_node', array( 'node_id' ) );
self::add_action( 'delete_col', 'FLBuilderModel::delete_col', array( 'node_id', 'new_width' ) );
self::add_action( 'reorder_node', 'FLBuilderModel::reorder_node', array( 'node_id', 'position' ) );
self::add_action( 'reorder_col', 'FLBuilderModel::reorder_col', array( 'node_id', 'position' ) );
self::add_action( 'move_node', 'FLBuilderModel::move_node', array( 'node_id', 'new_parent', 'position' ) );
self::add_action( 'move_col', 'FLBuilderModel::move_col', array( 'node_id', 'new_parent', 'position', 'resize' ) );
self::add_action( 'resize_cols', 'FLBuilderModel::resize_cols', array( 'col_id', 'col_width', 'sibling_id', 'sibling_width' ) );
self::add_action( 'reset_col_widths', 'FLBuilderModel::reset_col_widths', array( 'group_id' ) );
self::add_action( 'save_settings', 'FLBuilderModel::save_settings', array( 'node_id', 'settings' ) );
self::add_action( 'save_layout_settings', 'FLBuilderModel::save_layout_settings', array( 'settings' ) );
self::add_action( 'save_global_settings', 'FLBuilderModel::save_global_settings', array( 'settings' ) );
self::add_action( 'save_color_presets', 'FLBuilderModel::save_color_presets', array( 'presets' ) );
self::add_action( 'duplicate_post', 'FLBuilderModel::duplicate_post' );
self::add_action( 'duplicate_wpml_layout', 'FLBuilderModel::duplicate_wpml_layout', array( 'original_post_id', 'post_id' ) );
self::add_action( 'apply_user_template', 'FLBuilderModel::apply_user_template', array( 'template_id', 'append' ) );
self::add_action( 'apply_template', 'FLBuilderModel::apply_template', array( 'template_id', 'append' ) );
self::add_action( 'save_layout', 'FLBuilderModel::save_layout' );
self::add_action( 'save_draft', 'FLBuilderModel::save_draft' );
self::add_action( 'clear_draft_layout', 'FLBuilderModel::clear_draft_layout' );
self::add_action( 'disable_builder', 'FLBuilderModel::disable' );
// FLBuilderAJAXLayout
self::add_action( 'render_layout', 'FLBuilderAJAXLayout::render' );
self::add_action( 'render_new_row', 'FLBuilderAJAXLayout::render_new_row', array( 'cols', 'position', 'template_id', 'template_type' ) );
self::add_action( 'copy_row', 'FLBuilderAJAXLayout::copy_row', array( 'node_id' ) );
self::add_action( 'render_new_column_group', 'FLBuilderAJAXLayout::render_new_column_group', array( 'node_id', 'cols', 'position' ) );
self::add_action( 'render_new_columns', 'FLBuilderAJAXLayout::render_new_columns', array( 'node_id', 'insert', 'type', 'nested' ) );
self::add_action( 'render_new_module', 'FLBuilderAJAXLayout::render_new_module', array( 'parent_id', 'position', 'type', 'template_id', 'template_type' ) );
self::add_action( 'copy_module', 'FLBuilderAJAXLayout::copy_module', array( 'node_id' ) );
// FLBuilderServices
self::add_action( 'render_service_settings', 'FLBuilderServices::render_settings' );
self::add_action( 'render_service_fields', 'FLBuilderServices::render_fields' );
self::add_action( 'connect_service', 'FLBuilderServices::connect_service' );
self::add_action( 'delete_service_account', 'FLBuilderServices::delete_account' );
self::add_action( 'delete_service_account', 'FLBuilderServices::delete_account' );
// FLBuilderAutoSuggest
self::add_action( 'fl_builder_autosuggest', 'FLBuilderAutoSuggest::init' );
}
/**
* Runs the current AJAX action.
*
* @since 1.7
* @access private
* @return void
*/
static private function call_action()
{
// Only run for logged in users.
if ( ! is_user_logged_in() ) {
return;
}
// Verify the AJAX nonce.
if ( ! self::verify_nonce() ) {
return;
}
// Get the $_POST data.
$post_data = FLBuilderModel::get_post_data();
// Get the post ID.
$post_id = FLBuilderModel::get_post_id();
// Make sure we have a post ID.
if ( ! $post_id ) {
return;
}
// Make sure the user can edit this post.
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
// Get the action.
if ( ! empty( $_REQUEST['fl_action'] ) ) {
$action = $_REQUEST['fl_action'];
}
else if( ! empty( $post_data['fl_action'] ) ) {
$action = $post_data['fl_action'];
}
else {
return;
}
// Allow developers to modify actions before they are called.
do_action( 'fl_ajax_before_call_action', $action );
// Make sure the action exists.
if ( ! isset( self::$actions[ $action ] ) ) {
return;
}
// Get the action data.
$action = self::$actions[ $action ];
$args = array();
$keys_args = array();
// Build the args array.
foreach ( $action['args'] as $arg ) {
$args[] = $keys_args[ $arg ] = isset( $post_data[ $arg ] ) ? $post_data[ $arg ] : null;
}
// Tell WordPress this is an AJAX request.
define( 'DOING_AJAX', true );
// Allow developers to hook before the action runs.
do_action( 'fl_ajax_before_' . $action['action'], $keys_args );
// Call the action and allow developers to filter the result.
$result = apply_filters( 'fl_ajax_' . $action['action'], call_user_func_array( $action['method'], $args ), $keys_args );
// Allow developers to hook after the action runs.
do_action( 'fl_ajax_after_' . $action['action'], $keys_args );
// JSON encode the result.
echo json_encode( $result );
// Complete the request.
die();
}
/**
* Checks to make sure the AJAX nonce is valid.
*
* @since 1.7.2
* @access private
* @return bool
*/
static private function verify_nonce()
{
$post_data = FLBuilderModel::get_post_data();
$nonce = false;
if ( isset( $post_data['_wpnonce'] ) ) {
$nonce = $post_data['_wpnonce'];
}
else if ( isset( $_REQUEST['_wpnonce'] ) ) {
$nonce = $_REQUEST['_wpnonce'];
}
if ( ! $nonce || ! wp_verify_nonce( $nonce, 'fl_ajax_update' ) ) {
return false;
}
return true;
}
}
FLBuilderAJAX::init();