PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 2.8.16
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v2.8.16
3.13.3 3.13.2 3.13.1 3.13.0 3.12.13 3.12.12 3.12.11 3.12.10 3.12.9 3.12.8 3.12.7 3.12.6 3.12.5 3.12.4 3.12.3 3.12.1 3.12.2 3.12.0 3.11.1 3.11.0 3.10.9 3.10.8 3.10.7 3.10.6 2.8.16 All 261 releases
wpfunnels / admin / modules / template-library / class-manager.php

class-manager.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell 2.8.16, at admin/modules/template-library/class-manager.php

459 lines 11.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Template library manager
4 *
5 * @package
6 */
7 namespace WPFunnels\TemplateLibrary;
8
9 use WPFunnels\Wpfnl;
10
11 class Manager
12 {
13
14 /**
15 * Register funnels sources
16 *
17 * @var array
18 */
19 protected $_registered_sources = [];
20
21 /**
22 * Ajax validation args
23 *
24 * @var Validation
25 */
26 protected $validations;
27
28
29 /**
30 * View of the template-library module
31 *
32 * @var View
33 */
34 protected $view;
35
36
37 /**
38 * Manager constructor.
39 * Initializing the template library manger with
40 * Ajax registering sources and ajax hooks
41 *
42 * @since 1.0.0
43 */
44 public function __construct()
45 {
46 $this->add_actions();
47 $this->init_ajax();
48 $this->register_default_sources();
49 }
50
51
52 /**
53 * Initialize all ajax actions for template-library
54 *
55 * @since 1.0.0
56 */
57 public function init_ajax()
58 {
59 $this->validations = [
60 'logged_in' => true,
61 'user_can' => 'manage_options',
62 ];
63 wp_ajax_helper()->handle('wpfunnel-get-templates-data')
64 ->with_callback([$this, 'get_funnel_templates'])
65 ->with_validation($this->validations);
66
67 wp_ajax_helper()->handle('wpfunnel-import-funnel')
68 ->with_callback([$this, 'import_funnel'])
69 ->with_validation($this->validations);
70
71 wp_ajax_helper()->handle('wpfunnel-import-variation-step')
72 ->with_callback([$this, 'import_variation_step'])
73 ->with_validation($this->validations);
74
75 wp_ajax_helper()->handle('wpfunnel-after-funnel-creation')
76 ->with_callback([$this, 'after_funnel_creation'])
77 ->with_validation($this->validations);
78
79 wp_ajax_helper()->handle('wpfunnel-get-step-templates-data')
80 ->with_callback([$this, 'get_step_templates'])
81 ->with_validation($this->validations);
82
83 wp_ajax_helper()->handle('wpfunnel-after-step-creation')
84 ->with_callback([$this, 'after_step_creation'])
85 ->with_validation($this->validations);
86
87 wp_ajax_helper()->handle('wpfunnels-activate-plugin')
88 ->with_callback([$this, 'activate_plugin'])
89 ->with_validation($this->validations);
90 }
91
92
93 /**
94 * Add view for template-library
95 *
96 * @since 1.0.0
97 */
98 public function add_actions()
99 {
100 add_action('admin_footer', [$this, 'add_default_view']);
101 add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts']);
102 }
103
104 public function enqueue_scripts($hook)
105 {
106 if (in_array($hook, ['toplevel_page_wp_funnels'])) {
107 $funnel_id = 0;
108 if (isset($_GET['page'])) {
109 if ($_GET['page'] === 'edit_funnel') {
110 if (isset($_GET['id'])) {
111 $funnel_id = sanitize_text_field($_GET['id']);
112 }
113 }
114 }
115 wp_enqueue_script('funnel-template-library', WPFNL_URL . 'admin/assets/dist/js/template-library.min.js', ['jquery', 'wp-util'], '1.0.0', true);
116 }
117 }
118
119
120 /**
121 * Register template source for import/export
122 *
123 * @since 1.0.0
124 */
125 private function register_default_sources()
126 {
127 $sources = [
128 'local',
129 'remote',
130 ];
131 foreach ($sources as $source_filename) {
132 $class_name = ucwords($source_filename);
133 $class_name = str_replace('-', '_', $class_name);
134 $this->register_source(__NAMESPACE__ . '\Wpfnl_Source_' . $class_name);
135 }
136 }
137
138
139 /**
140 * Register sources
141 *
142 * @param $source_class
143 * @param array $args
144 *
145 * @return bool|\WP_Error
146 * @since 1.0.0
147 */
148 public function register_source($source_class, $args = [])
149 {
150 if (!class_exists($source_class)) {
151 return new \WP_Error('source_class_name_not_exists');
152 }
153 $source_instance = new $source_class($args);
154 $source_id = $source_instance->get_source();
155
156 if (isset($this->_registered_sources[$source_id])) {
157 return new \WP_Error('source_exists');
158 }
159 $this->_registered_sources[$source_id] = $source_instance;
160 return true;
161 }
162
163
164 /**
165 * Get all registered sources
166 *
167 * @return array
168 * @since 1.0.0
169 */
170 public function get_registered_sources()
171 {
172 return $this->_registered_sources;
173 }
174
175
176 /**
177 * Get template source by source name.
178 *
179 * @param $id
180 *
181 * @return false|Wpfnl_Source_Base
182 * @since 1.0.0
183 */
184 public function get_source($id)
185 {
186 $sources = $this->get_registered_sources();
187 if (!isset($sources[$id])) {
188 return false;
189 }
190 return $sources[$id];
191 }
192
193
194 /**
195 * Get step templates
196 *
197 * @param $payload
198 *
199 * @return array
200 */
201 public function get_step_templates($payload)
202 {
203 $template_data = $this->get_source('remote')->get_funnels();
204 return [
205 'success' => true,
206 'data' => $template_data,
207 ];
208 }
209
210 /**
211 * Get funnel templates for viewing
212 *
213 * @return array
214 * @since 1.0.0
215 */
216 public function get_funnel_templates()
217 {
218 $template_data = $this->get_source('remote')->get_funnels();
219 return [
220 'success' => true,
221 'data' => $template_data,
222 ];
223 }
224
225 /**
226 * Add default view for template library
227 *
228 * @since 1.0.0
229 */
230 public function add_default_view()
231 {
232 $default_view = [
233 'admin/template-library/view/template-library.php',
234 ];
235 foreach ($default_view as $view) {
236 $this->add_view(WPFNL_DIR . $view);
237 }
238 $this->print_template();
239 }
240
241
242 /**
243 * Add view
244 *
245 * @param $view
246 * @param string $type
247 *
248 * @since 1.0.0
249 */
250 public function add_view($view, $type = 'path')
251 {
252 if ('path' === $type) {
253 ob_start();
254 if (file_exists($view)) {
255 include $view;
256 }
257 $this->view = ob_get_clean();
258 }
259 }
260
261 /**
262 * Print template
263 *
264 * @since 1.0.0
265 */
266 public function print_template()
267 {
268 echo $this->view;
269 }
270
271
272 /**
273 * Import templates from
274 * Remote server
275 *
276 * @param $payload
277 *
278 * @return array
279 * @since 1.0.0
280 */
281 public function import_funnel($payload)
282 {
283 $source = $this->get_source($payload['source']);
284 return $source->import_funnel($payload);
285 }
286
287 /**
288 * Import wp funnel steps
289 * From remote servers
290 *
291 * @param $payload
292 *
293 * @return array
294 * @since 1.0.0
295 */
296 public function import_variation_step($payload)
297 {
298 $source = $this->get_source($payload['source']);
299 return $source->import_variation_step($payload);
300 }
301
302
303 /**
304 * After funnel import
305 * Redirect to new funnel edit url
306 *
307 * @param $payload
308 *
309 * @return array
310 * @since 1.0.0
311 */
312 public function after_funnel_creation($payload)
313 {
314 $source = $this->get_source($payload['source']);
315 return $source->after_funnel_creation($payload);
316 }
317
318 /**
319 * After funnel import
320 * Redirect to new funnel edit url
321 *
322 * @param $payload
323 *
324 * @return array
325 * @since 1.0.0
326 */
327 public function after_step_creation($payload)
328 {
329 $funnel_id = $payload['funnelID'];
330 $step_id = $payload['stepID'];
331 $step = get_post($step_id);
332 $step_type = get_post_meta($step_id, '_step_type', true);
333 $step_name = $step->post_title;
334 $step_component = $step_type . $step_id;
335 $node_id = 0;
336 $step_edit_link = base64_encode(get_edit_post_link($step_id));
337 $step_view_link = base64_encode(get_post_permalink($step_id));
338
339 $funnel_json = get_post_meta($funnel_id, 'funnel_data', true);
340 $funnel_data = $funnel_json;
341
342 $node_data = $funnel_data['drawflow']['Home']['data'];
343
344 foreach ($node_data as $node_key => $node_value) {
345 $node_id = $node_value['id'] + 1;
346 }
347
348 $step_data = array(
349 'step_id' => $step_id,
350 'step_type' => $step_type,
351 'step_edit_link' => $step_edit_link,
352 'step_view_link' => $step_view_link,
353 );
354 $step_array = array(
355 'id' => $node_id,
356 'name' => $step_type,
357 'data' => $step_data,
358 'class' => $step_type,
359 'html' => $step_component,
360 'step_name' => $step_name,
361 'typenode' => 'vue',
362 'inputs' => $this->get_connector_input($step_type),
363 'outputs' => $this->get_connector_output($step_type),
364 'pos_x' => 100,
365 'pos_y' => 100,
366 );
367
368 $funnel_data['drawflow']['Home']['data'][] = $step_array;
369
370 $final_data = json_encode($funnel_data);
371
372 $identifier_json = get_post_meta($funnel_id, 'funnel_identifier', true);
373 $identifier_json = preg_replace('/\: *([0-9]+\.?[0-9e+\-]*)/', ':"\\1"', $identifier_json);
374 $identifier = json_decode($identifier_json, true);
375 $identifier[$node_id] = $step_id;
376 $final_identifier = json_encode($identifier);
377
378 update_post_meta($funnel_id, 'funnel_data', $funnel_data);
379 update_post_meta($funnel_id, 'funnel_identifier', $final_identifier);
380
381 $source = $this->get_source($payload['source']);
382 return $source->after_step_creation($payload);
383 }
384
385 public function get_connector_input($type)
386 {
387 if ($type == 'landing') {
388 $input = array();
389 } elseif ($type == 'checkout') {
390 $input = array(
391 "input_1" => array(
392 "connections" => array(),
393 ),
394 );
395 } else {
396 $input = array(
397 "input_1" => array(
398 "connections" => array(),
399 ),
400 );
401 }
402 return $input;
403 }
404
405 public function get_connector_output($type)
406 {
407 if ($type == 'landing') {
408 $output = array(
409 "output_1" => array(
410 "connections" => array(),
411 ),
412 );
413 } elseif ($type == 'checkout') {
414 $output = array(
415 "output_1" => array(
416 "connections" => array(),
417 ),
418 );
419 } else {
420 $output = array();
421 }
422 return $output;
423 }
424
425
426 /**
427 * Activate plugin with ajax call
428 *
429 * @param $payload
430 *
431 * @since 2.0.0
432 */
433 public function activate_plugin( $payload ) {
434 if ( ! current_user_can( 'manage_options' ) ) {
435 wp_send_json_error( array(
436 'message' => __('Sorry you are not allowed to do this operation', 'wpfnl'),
437 ) );
438 }
439 \wp_clean_plugins_cache();
440 $plugin_file = ( isset( $payload['pluginFile'] ) ) ? esc_attr( $payload['pluginFile'] ) : '';
441 $activate = \activate_plugin( $plugin_file, '', false, true );
442
443 if ( is_wp_error( $activate ) ) {
444 wp_send_json_error(
445 array(
446 'success' => false,
447 'message' => $activate->get_error_message(),
448 )
449 );
450 }
451
452 wp_send_json_success(
453 array(
454 'message' => __('Plugin is installed successfully', 'wpfnl'),
455 )
456 );
457 }
458 }
459