PluginProbe
Page Builder: Pagelayer – Drag and Drop website builder / 0.9.8
Page Builder: Pagelayer – Drag and Drop website builder v0.9.8
2.2.1 2.2.0 2.1.9 2.1.8 2.1.7 2.1.6 2.1.5 2.1.4 2.1.3 trunk 0.9.0 0.9.1 0.9.2 0.9.3 0.9.4 0.9.5 0.9.6 0.9.7 0.9.8 0.9.9 1.0.0 1.0.2 1.0.3 1.0.4 1.0.5 All 129 releases
pagelayer / main / ajax.php

ajax.php in Page Builder: Pagelayer – Drag and Drop website builder 0.9.8, at main/ajax.php

1,219 lines 30.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 //////////////////////////////////////////////////////////////
4 //===========================================================
5 // ajax.php
6 //===========================================================
7 // PAGELAYER
8 // Inspired by the DESIRE to be the BEST OF ALL
9 // ----------------------------------------------------------
10 // Started by: Pulkit Gupta
11 // Date: 23rd Jan 2017
12 // Time: 23:00 hrs
13 // Site: http://pagelayer.com/wordpress (PAGELAYER)
14 // ----------------------------------------------------------
15 // Please Read the Terms of use at http://pagelayer.com/tos
16 // ----------------------------------------------------------
17 //===========================================================
18 // (c)Pagelayer Team
19 //===========================================================
20 //////////////////////////////////////////////////////////////
21
22 // Are we being accessed directly ?
23 if(!defined('PAGELAYER_VERSION')) {
24 exit('Hacking Attempt !');
25 }
26
27 // Is the nonce there ?
28 if(empty($_REQUEST['pagelayer_nonce'])){
29 return;
30 }
31
32 // The ajax handler
33 add_action('wp_ajax_pagelayer_wp_widget', 'pagelayer_wp_widget_ajax');
34 function pagelayer_wp_widget_ajax(){
35
36 global $pagelayer;
37
38 // Some AJAX security
39 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
40
41 pagelayer_load_shortcodes();
42
43 header('Content-Type: application/json');
44
45 $ret = [];
46 $tag = @$_POST['tag'];
47 //pagelayer_print($pagelayer->shortcodes[$tag]);
48
49 // No tag ?
50 if(empty($pagelayer->shortcodes[$tag])){
51 $ret['error'][] = __pl('no_tag');
52 pagelayer_json_output($ret);
53 }
54
55 // Include the widgets
56 include_once(ABSPATH . 'wp-admin/includes/widgets.php');
57
58 $class = $pagelayer->shortcodes[$tag]['widget'];
59
60 // Check the widget class exists ?
61 if(empty($class) || !class_exists($class)){
62 $ret['error'][] = __pl('no_widget_class');
63 pagelayer_json_output($ret);
64 }
65
66 $instance = [];
67 $widget = new $class();
68 $widget->_set('pagelayer-widget-1234567890');
69
70 // Is there any existing data ?
71 if(!empty($_POST['widget_data'])){
72 $json = json_decode(stripslashes($_POST['widget_data']), true);
73 //pagelayer_print($json);die();
74 if(!empty($json)){
75 $instance = $json;
76 }
77 }
78
79 // Are there any form values ?
80 if(!empty($_POST['values'])){
81 parse_str(stripslashes($_POST['values']), $data);
82 //pagelayer_print($data);die();
83
84 // Any data ?
85 if(!empty($data)){
86
87 // First key is useless
88 $data = current($data);
89
90 // Do we still have valid data ?
91 if(!empty($data)){
92
93 // 2nd key is useless and just over-ride instance
94 $instance = current($data);
95
96 }
97 }
98 }
99
100 // Get the form
101 ob_start();
102 $widget->form($instance);
103 $ret['form'] = ob_get_contents();
104 ob_end_clean();
105
106 // Get the html
107 ob_start();
108 $widget->widget([], $instance);
109 $ret['html'] = ob_get_contents();
110 ob_end_clean();
111
112 // Widget data to set
113 if(!empty($instance)){
114 $ret['widget_data'] = $instance;
115 }
116
117 pagelayer_json_output($ret);
118
119 }
120
121 // Update Post content
122 add_action('wp_ajax_pagelayer_save_content', 'pagelayer_save_content');
123 function pagelayer_save_content(){
124
125 // Some AJAX security
126 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
127
128 $content = $_POST['pagelayer_update_content'];
129
130 $postID = (int) $_GET['postID'];
131
132 if(empty($postID)){
133 $msg['error'] = __pl('invalid_post_id');
134 }
135
136 // Check if the post exists
137
138 if(!empty($postID) && !empty($content)){
139
140 $post = array(
141 'ID' => $postID,
142 'post_content' => $content,
143 );
144
145 // Update the post into the database
146 wp_update_post($post);
147
148 if (is_wp_error($postID)) {
149 $msg['error'] = __pl('post_update_err');
150 }else{
151 $msg['success'] = __pl('post_update_success');
152 }
153
154 }else{
155 $msg['error'] = __pl('post_update_err');
156 }
157
158 pagelayer_json_output($msg);
159
160 }
161
162 // Shortcodes Widget Handler
163 add_action('wp_ajax_pagelayer_do_shortcodes', 'pagelayer_do_shortcodes');
164 function pagelayer_do_shortcodes(){
165
166 // Some AJAX security
167 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
168
169 $data = '';
170 if(isset($_REQUEST['shortcode_data'])){
171 $data = stripslashes($_REQUEST['shortcode_data']);
172 }
173
174 // Load shortcodes
175 pagelayer_load_shortcodes();
176
177 $data = do_shortcode($data);
178
179 // Create the HTML object
180 $node = pQuery::parseStr($data);
181 $node->query('.pagelayer-ele')->removeClass('pagelayer-ele');
182 echo $node->html();
183
184 wp_die();
185
186 }
187
188 // Get the Site Title
189 add_action('wp_ajax_pagelayer_fetch_site_title', 'pagelayer_fetch_site_title');
190 function pagelayer_fetch_site_title(){
191
192 // Some AJAX security
193 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
194
195 echo get_bloginfo('name');
196 wp_die();
197 }
198
199 // Update the Site Title
200 add_action('wp_ajax_pagelayer_update_site_title', 'pagelayer_update_site_title');
201 function pagelayer_update_site_title(){
202 global $wpdb;
203
204 // Some AJAX security
205 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
206
207 $site_title = $_POST['site_title'];
208
209 update_option('blogname', $site_title);
210
211 $wpdb->query("UPDATE `sm_sitemeta`
212 SET meta_value = '".$site_title."'
213 WHERE meta_key = 'site_name'");
214 wp_die();
215 }
216
217 // Show the SideBars
218 add_action('wp_ajax_pagelayer_fetch_sidebar', 'pagelayer_fetch_sidebar');
219 function pagelayer_fetch_sidebar(){
220
221 global $wp_registered_sidebars;
222
223 // Some AJAX security
224 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
225
226 // Create a list
227 $pagelayer_wp_widgets = array();
228
229 foreach($wp_registered_sidebars as $v){
230 $pagelayer_wp_widgets[$v['id']] = $v['name'];
231 }
232
233 $id = @$_REQUEST['sidebar'];
234
235 if(function_exists('dynamic_sidebar') && !empty($pagelayer_wp_widgets[$id])) {
236 ob_start();
237 dynamic_sidebar($id);
238 $result = ob_get_clean();
239 }else{
240 $result = __pl('no_widget_area');
241 }
242
243 echo $result;
244 wp_die();
245
246 }
247
248 // Show the primary menu !
249 add_action('wp_ajax_pagelayer_fetch_primary_menu', 'pagelayer_fetch_primary_menu');
250 function pagelayer_fetch_primary_menu(){
251
252 // Some AJAX security
253 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
254
255 if(isset($_POST['nav_list'])){
256 echo wp_nav_menu([
257 'menu' => wp_get_nav_menu_object($_POST['nav_list']),
258 'menu_id' => $_POST["nav_list"],
259 //'theme_location' => 'primary',
260 //'menu_class' => 'primary-menu',
261 ]);
262 }
263
264 wp_die();
265 }
266
267 // Get post revision
268 add_action('wp_ajax_pagelayer_get_revision', 'pagelayer_get_revision');
269 function pagelayer_get_revision(){
270
271 // Some AJAX security
272 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
273
274 $postID = (int) $_GET['postID'];
275 $post_revisions = array();
276
277 if(empty($postID)){
278 $post_revisions['error'] = __pl('invalid_post_id');
279 }else{
280 $post_revisions = pagelayer_get_post_revision_by_id($postID);
281 }
282
283 pagelayer_json_output($post_revisions);
284
285 }
286
287 // Get post revision
288 add_action('wp_ajax_pagelayer_apply_revision', 'pagelayer_apply_revision');
289 function pagelayer_apply_revision(){
290
291 // Some AJAX security
292 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
293
294 $revisionID = (int) $_REQUEST['revisionID'];
295 $post_data = array();
296
297 if(empty($revisionID)){
298 $post_data['error'] = __pl('invalid_post_id');
299 }else{
300
301 $post = get_post( $revisionID );
302
303 if ( empty( $post ) ) {
304 $post_data['error'] = __pl('invalid_revision');
305 pagelayer_json_output($post_data);
306 }
307
308 // Need to make the reviews post global
309 $GLOBALS['post'] = $post;
310
311 // Need to reload the shortcodes
312 pagelayer_load_shortcodes();
313
314 $post_data['content'] = do_shortcode($post->post_content);
315
316 if (is_wp_error($postID)) {
317 $post_data['error'] = __pl('rev_load_error');
318 }else{
319 $post_data['success'] = __pl('rev_load_success');
320 }
321
322 wp_reset_postdata();
323 }
324
325 pagelayer_json_output($post_data);
326
327 }
328
329 // Get post revision
330 add_action('wp_ajax_pagelayer_delete_revision', 'pagelayer_delete_revision');
331 function pagelayer_delete_revision() {
332
333 // Some AJAX security
334 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
335
336 $revisionID = (int) $_REQUEST['revisionID'];
337
338 if(empty($revisionID)){
339 $post_data['error'] = __pl('invalid_post_id');
340 }else{
341
342 $revision = get_post( $revisionID );
343
344 if ( empty( $revision ) ) {
345 $post_data['error'] = __pl('invalid_revision');
346 }else{
347
348 if ( ! current_user_can( 'delete_post', $revision->ID ) ) {
349 $post_data['error'] = __pl('access_denied');
350 pagelayer_json_output($post_data);
351 return false;
352 }
353
354 $deleted = wp_delete_post_revision( $revision->ID );
355
356 if ( ! $deleted || is_wp_error( $deleted ) ) {
357 $post_data['error'] = __pl('delete_rev_error');
358 }else{
359 $post_data['success'] = __pl('delete_rev_success');
360 }
361 }
362 }
363
364 pagelayer_json_output($post_data);
365
366 }
367
368 // Get post revision
369 add_action('wp_ajax_pagelayer_post_nav', 'pagelayer_post_nav');
370 function pagelayer_post_nav() {
371
372 // Some AJAX security
373 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
374
375 if(!isset($_REQUEST['data']) || !isset($_REQUEST['postID'])){
376 return;
377 }
378
379 $el['atts'] = $_REQUEST['data'];
380
381 $post = get_post($_REQUEST['postID']);
382
383 // Need to make this post global
384 $GLOBALS['post'] = $post;
385
386 $in_same_term = false;
387 $taxonomies = 'category';
388 $title = '';
389 $arrows_list = $el['atts']['arrows_list'];
390
391 if($el['atts']['in_same_term']){
392 $in_same_term = true;
393 $taxonomies = $el['atts']['taxonomies'];
394 }
395
396 if($el['atts']['post_title']){
397 $title = '<span class="pagelayer-post-nav-title">%title</span>';
398 }
399
400 $next_label = '<span class="pagelayer-next-holder">
401 <span class="pagelayer-post-nav-link"> '.$el["atts"]["next_label"].'</span>'.$title.'
402 </span>
403 <span class="pagelayer-post-nav-icon fa fa-'.$arrows_list.'-right"></span>';
404
405 $prev_label = '<span class="pagelayer-post-nav-icon fa fa-'.$arrows_list.'-left"></span>
406 <span class="pagelayer-next-holder">
407 <span class="pagelayer-post-nav-link"> '.$el["atts"]["prev_label"].'</span>'.$title.'
408 </span>';
409
410 $el['atts']['next_link'] = get_next_post_link('%link', $next_label, $in_same_term, '', $taxonomies);
411
412 $el['atts']['prev_link'] = get_previous_post_link('%link', $prev_label, $in_same_term, '', $taxonomies );
413
414 pagelayer_json_output($el);
415
416 }
417
418 // Get post comment template
419 add_action('wp_ajax_pagelayer_post_comment', 'pagelayer_post_comment');
420 function pagelayer_post_comment() {
421 global $post;
422
423 // Some AJAX security
424 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
425
426 if(!isset($_REQUEST['postID'])){
427 return true;
428 }
429
430 $GLOBALS['post'] = get_post($_REQUEST['postID']);
431 $GLOBALS['withcomments'] = true;
432
433 if ( comments_open() || get_comments_number() ) {
434 echo '<div class="pagelayer-comments-template">'.comments_template().'</div>';
435 }else{
436 echo '<div class="pagelayer-comments-close">
437 <h2>Comments are closed!</h2>
438 </div>';
439 }
440 wp_die();
441
442 }
443
444 // Get post comment template
445 add_action('wp_ajax_pagelayer_post_info', 'pagelayer_post_info');
446 function pagelayer_post_info() {
447 global $post;
448
449 // Some AJAX security
450 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
451
452 if(!isset($_REQUEST['postID']) || !isset($_REQUEST['el'])){
453 return true;
454 }
455
456 $el['atts'] = $_REQUEST['el'];
457
458 $GLOBALS['post'] = get_post($_REQUEST['postID']);
459
460 $post_info_content ='';
461 $link ='';
462 $info_content ='';
463 $avatar_url ='';
464
465 switch($el['atts']['type']){
466 case 'author':
467
468 $link = get_author_posts_url( get_the_author_meta( 'ID' ) );
469 $avatar_url = get_avatar_url( get_the_author_meta( 'ID' ), 96 );
470 $post_info_content = get_the_author_meta( 'display_name', $post->post_author );
471 break;
472
473 case 'date':
474
475 $format = [
476 'default' => 'F j, Y',
477 '0' => 'F j, Y',
478 '1' => 'Y-m-d',
479 '2' => 'm/d/Y',
480 '3' => 'd/m/Y',
481 'custom' => empty( $el['atts']['date_format_custom'] ) ? 'F j, Y' : $el['atts']['date_format_custom'],
482 ];
483
484 $post_info_content = get_the_time( $format[ $el['atts']['date_format'] ] );
485 $link = get_day_link( get_post_time( 'Y' ), get_post_time( 'm' ), get_post_time( 'j' ) );
486
487 break;
488
489 case 'time':
490
491 $format = [
492 'default' => 'g:i a',
493 '0' => 'g:i a',
494 '1' => 'g:i A',
495 '2' => 'H:i',
496 'custom' => empty( $el['atts']['time_format_custom'] ) ? 'F j, Y' : $el['atts']['time_format_custom'],
497 ];
498 $post_info_content = get_the_time( $format[ $el['atts']['time_format'] ] );
499
500 break;
501
502 case 'comments':
503
504 if (comments_open()) {
505 $post_info_content = (int) get_comments_number();
506 $link = get_comments_link();
507 }
508
509 break;
510
511 case 'terms':
512
513 $taxonomy = $el['atts']['taxonomy'];
514 $terms = wp_get_post_terms( get_the_ID(), $taxonomy );
515 foreach ( $terms as $term ) {
516 $post_info_content .= ' <a if-ext="{{info_link}}" href="'. get_term_link( $term ) .'" class="pagelayer-post-info-list-link"> '. $term->name .' </a>';
517 }
518
519 break;
520
521 case 'custom':
522
523 $post_info_content = $el['atts']['type_custom'];
524 $link = $el['atts']['info_custom_link'];
525
526 break;
527 }
528
529 $el['atts']['post_info_content'] = $post_info_content;
530 $el['atts']['avatar_url'] = $avatar_url;
531 $el['atts']['link'] = $link;
532
533 pagelayer_json_output($el['atts']);
534
535 }
536
537 // Get the Featured Image
538 add_action('wp_ajax_pagelayer_fetch_featured_img', 'pagelayer_fetch_featured_img');
539 function pagelayer_fetch_featured_img(){
540
541 // Some AJAX security
542 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
543
544 if($_POST['size']){
545 echo get_the_post_thumbnail_url($_POST['post_id'], $_POST['size']);
546 }else{
547 echo get_the_post_thumbnail_url($_POST['post_id']);
548 }
549 wp_die();
550 }
551
552 // Get the postfolio posts
553 add_action('wp_ajax_pagelayer_fetch_posts', 'pagelayer_fetch_posts');
554 function pagelayer_fetch_posts(){
555
556 // Some AJAX security
557 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
558
559 echo pagelayer_widget_posts($_POST);
560
561 wp_die();
562 }
563
564 // Get the Posts
565 add_action('wp_ajax_pagelayer_posts_data', 'pagelayer_posts_data');
566 function pagelayer_posts_data(){
567
568 // Some AJAX security
569 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
570
571 // Load shortcodes
572 pagelayer_load_shortcodes();
573
574 echo pagelayer_posts($_POST);
575 wp_die();
576 }
577
578 // Get the Posts
579 add_action('wp_ajax_pagelayer_archive_posts_data', 'pagelayer_archive_posts_data');
580 function pagelayer_archive_posts_data(){
581
582 // Some AJAX security
583 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
584
585 // Set excerpt length
586 if($_POST['exc_length']){
587 $exc_length = (int) $params['exc_length'];
588 add_filter( 'excerpt_length', function($length) use($exc_length){
589 return $exc_length;
590 }, 999 );
591 }
592
593 // Load shortcodes
594 pagelayer_load_shortcodes();
595
596 echo pagelayer_posts($_POST, $_POST['pagelayer_wp_query']);
597 wp_die();
598 }
599
600 // Handle Contact Form Data
601 add_action('wp_ajax_pagelayer_contact_submit', 'pagelayer_contact_submit');
602 add_action('wp_ajax_nopriv_pagelayer_contact_submit', 'pagelayer_contact_submit' );
603 function pagelayer_contact_submit(){
604
605 $to_mail = get_option('pagelayer_cf_to_email');
606 $subject = get_option('pagelayer_cf_subject');
607
608 $fdata = $_POST['form_data'];
609 parse_str($fdata, $formdata);
610
611 // Make the email content
612 foreach($formdata as $k => $i){
613 $data .= ''.$k.'\t : \t'.$i.'\n';
614 }
615
616 // Send the email
617 $r = wp_mail( $to_mail, $subject, $data );
618
619 if($r == TRUE){
620 $wp['success'] = get_option( 'pagelayer_cf_success' );
621 }else{
622 $wp['failed'] = get_option( 'pagelayer_cf_failed' );
623 }
624
625 pagelayer_json_output($wp);
626
627 }
628
629 // Fetch Google reCaptcha Key
630 add_action('wp_ajax_pagelayer_fetch_grecaptcha_key', 'pagelayer_fetch_grecaptcha_key');
631 function pagelayer_fetch_grecaptcha_key(){
632
633 $data['key'] = get_option('pagelayer_google_captcha');
634
635 pagelayer_json_output($data);
636
637 }
638
639 // Handle Login Submit
640 add_action('wp_ajax_pagelayer_login_submit', 'pagelayer_login_submit');
641 add_action('wp_ajax_nopriv_pagelayer_login_submit', 'pagelayer_login_submit');
642 function pagelayer_login_submit(){
643
644 $fdata = $_POST['form_data'];
645 parse_str($fdata, $formdata);
646
647 $creds = array();
648 $creds['user_login'] = $formdata['username'];
649 $creds['user_password'] = $formdata['password'];
650 $creds['remember'] = $formdata['remember_me'];
651
652 // If After logout URL, then save
653 if(!empty($formdata['logout_url'])){
654 update_user_option('pagelayer_logout_url', $formdata['logout_url']);
655 }
656
657 // Login the user
658 $user = wp_signon( $creds, false );
659
660 if ( is_wp_error($user) ){
661 $data['error'] = $user->get_error_message();
662 }else{
663 $data['redirect'] = (empty($formdata['login_url']) ? '' : $formdata['login_url']);
664 $data['error'] = '';
665 }
666
667 pagelayer_json_output($data);
668
669 }
670
671 // Handle Logout Redirect here
672 add_action('wp_logout', 'pagelayer_after_logout');
673 function pagelayer_after_logout(){
674
675 $url = get_user_option('pagelayer_logout_url');
676
677 // We will redirect if we have the given item set.
678 if(!empty($url)){
679 wp_redirect( $url );
680 exit();
681 }
682
683 }
684
685 // Get Page List for SiteMap
686 add_action('wp_ajax_pagelayer_get_pages_list', 'pagelayer_get_pages_list');
687 add_action('wp_ajax_nopriv_pagelayer_get_pages_list', 'pagelayer_get_pages_list');
688 function pagelayer_get_pages_list(){
689
690 $args = array(
691 'post_type' => $_POST['type'],
692 'orderby' => $_POST['post_order'],
693 'order' => $_POST['order'],
694 'hierarchical' => (empty($_POST['hier']) || $_POST['hier'] == null ? '' : $_POST['hier']),
695 'number' => (empty($_POST['depth']) || $_POST['depth'] == null ? '' : $_POST['depth']),
696 );
697
698 $option = '<ul>';
699 $pages = new WP_Query($args);
700 $posts = $pages->posts;
701 foreach ( $posts as $page ) {
702 $option .= '<li class="pagelayer-sitemap-list-item" data-postID="'.$page->ID.'"><a class="pagelayer-ele-link" href="'.$page->guid.'">'.$page->post_name.'</a></li>';
703 }
704 $option .= '</ul>';
705
706
707 echo $option;
708
709 wp_die();
710 }
711
712 // Get the data for template
713 add_action('wp_ajax_pagelayer_search_ids', 'pagelayer_search_ids');
714 function pagelayer_search_ids() {
715
716 // Some AJAX security
717 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
718
719 if ( empty( $_POST['filter_type'] ) || empty( $_POST['search'] ) ) {
720 wp_die();
721 }
722
723 $sel_opt = '';
724
725 switch ( $_POST['filter_type'] ) {
726 case 'taxonomy':
727 $query_params = [
728 'taxonomy' => $_POST['object_type'],
729 'search' => $_POST['search'],
730 'hide_empty' => false,
731 ];
732
733 $terms = get_terms( $query_params );
734
735 global $wp_taxonomies;
736
737 foreach ( $terms as $term ) {
738 $sel_opt .= '<span class="pagelayer-temp-search-sel-span" value="'. $term->term_taxonomy_id .'">'. $term->name .'</span>';
739 }
740
741 break;
742
743 case 'post':
744 $query_params = [
745 'post_type' => $_POST['object_type'], //$this->extract_post_type( $data ),
746 's' => $_POST['search'],
747 'posts_per_page' => -1,
748 ];
749
750 if ( 'attachment' === $query_params['post_type'] ) {
751 $query_params['post_status'] = 'inherit';
752 }
753
754 $query = new \WP_Query( $query_params );
755
756 foreach ( $query->posts as $post ) {
757 $sel_opt .= '<span class="pagelayer-temp-search-sel-span" value="'. $post->ID .'">'. $post->post_title .'</span>';
758 }
759 break;
760
761 case 'author':
762 $query_params = [
763 'who' => 'authors',
764 'fields' => [
765 'ID',
766 'display_name',
767 ],
768 'search' => '*' . $_POST["search"] . '*',
769 'search_columns' => [
770 'user_login',
771 'user_nicename',
772 ],
773 ];
774
775 $user_query = new \WP_User_Query( $query_params );
776
777 foreach ( $user_query->get_results() as $author ) {
778 $sel_opt .= '<span class="pagelayer-temp-search-sel-span" value="'. $author->ID .'">'. $author->display_name .'</span>';
779 }
780 break;
781 default:
782 $sel_opt = 'Result Not Found';
783 }
784
785 if(!empty($sel_opt)){
786 echo $sel_opt;
787 }else{
788 echo 'Result Not Found';
789 }
790
791 wp_die();
792 }
793
794
795 // Save the post data from pagelayer setting page
796 add_action('wp_ajax_pagelayer_save_template', 'pagelayer_save_template');
797 function pagelayer_save_template() {
798
799 // Some AJAX security
800 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
801
802 $done = [];
803
804 $post_id = (int) $_GET['postID'];
805
806 // We need to create the post
807 if(empty($post_id)){
808
809 // Get the template type
810 if(empty($_POST['pagelayer_template_type'])){
811 $done['error'] = __pl('temp_error_type');
812 pagelayer_json_output($done);
813 }
814
815 $ret = wp_insert_post([
816 'post_title' => $_POST['pagelayer_lib_title'],
817 'post_type' => 'pagelayer-template',
818 'post_status' => 'publish',
819 'comment_status' => 'closed',
820 'ping_status' => 'closed'
821 ]);
822
823 // An error occured
824 if(is_wp_error($ret)){
825 $done['error'] = __pl('temp_error').' : '.$ret->get_error_message();
826 pagelayer_json_output($done);
827 }
828
829 $post_id = $ret;
830 $done['id'] = $post_id;
831
832 // Save our template type
833 $ret = update_post_meta($post_id, 'pagelayer_template_type', $_POST['pagelayer_template_type']);
834
835 }
836
837 // The ID in consideration
838 $done['id'] = $post_id;
839
840 // Check if the post title in not empty
841 if(!empty($_POST['pagelayer_lib_title'])){
842
843 $post = array(
844 'ID' => $post_id,
845 'post_title' => $_POST['pagelayer_lib_title'],
846 );
847
848 // Update the post into the database
849 $ret = wp_update_post($post);
850
851 }
852
853 // Save template library display conditions
854 $condi_array = array();
855 $condi_len = count($_POST['pagelayer_condition_type']);
856 if($_POST['pagelayer_template_type'] != 'section'){
857 for( $i =0; $i < $condi_len; $i++ ){
858 $condi_array[$i] = array(
859 'type' => $_POST['pagelayer_condition_type'][$i],
860 'template' => $_POST['pagelayer_condition_name'][$i],
861 'sub_template' => $_POST['pagelayer_condition_sub_template'][$i],
862 'id' => $_POST['pagelayer_condition_id'][$i],
863 );
864 }
865 }
866 //print_r($condi_array);
867
868 $ret = update_post_meta($post_id, 'pagelayer_template_conditions', $condi_array);
869
870 if(is_wp_error($post_id)){
871 $done['error'] = __pl('temp_error').' : '.$ret->get_error_message();
872 }else{
873 $done['success'] = __pl('temp_update_success');
874 }
875
876 pagelayer_json_output($done);
877
878 }
879
880 // Product Images Handler
881 add_action('wp_ajax_pagelayer_product_images', 'pagelayer_product_images');
882 function pagelayer_product_images(){
883 global $product;
884
885 // Some AJAX security
886 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
887
888 if ( !isset($_REQUEST['postID']) ) {
889 return;
890 }
891
892 $product = wc_get_product($_REQUEST['postID']);
893
894 if ( empty( $product ) ) {
895 return ;
896 }
897
898 if ( isset($_POST['sale_flash']) ) {
899 wc_get_template( 'loop/sale-flash.php' );
900 }
901 wc_get_template( 'single-product/product-image.php' );
902
903 // On render widget from Editor - trigger the init manually.
904 echo '
905 <script>
906 jQuery(".woocommerce-product-gallery").each( function() {
907 jQuery(this).wc_product_gallery();
908 } );
909 </script>
910 ';
911
912 wp_die();
913 }
914
915 // Related Products Handler
916 add_action('wp_ajax_pagelayer_product_related', 'pagelayer_product_related');
917 function pagelayer_product_related(){
918 global $product;
919
920 // Some AJAX security
921 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
922
923 if ( !isset($_REQUEST['postID']) ) {
924 return;
925 }
926
927 $product = wc_get_product($_REQUEST['postID']);
928
929 if ( empty( $product ) ) {
930 return ;
931 }
932
933 $args = $_REQUEST['pagelayer_args'];
934
935 if(function_exists( 'woocommerce_related_products' )){
936 woocommerce_related_products($args);
937 }
938
939 wp_die();
940 }
941
942 // Upsell Products Handler
943 add_action('wp_ajax_pagelayer_product_upsell', 'pagelayer_product_upsell');
944 function pagelayer_product_upsell(){
945 global $product;
946
947 // Some AJAX security
948 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
949
950 if ( !isset($_REQUEST['postID']) ) {
951 return;
952 }
953
954 $product = wc_get_product($_REQUEST['postID']);
955
956 if ( empty( $product ) ) {
957 return ;
958 }
959
960 if(function_exists( 'woocommerce_related_products' )){
961 woocommerce_upsell_display( $_REQUEST['limit'], $_REQUEST['columns'], $_REQUEST['orderby'], $_REQUEST['order'] );
962 }
963
964 wp_die();
965 }
966
967 // Products Categories Handler
968 add_action('wp_ajax_pagelayer_product_categories', 'pagelayer_product_categories');
969 function pagelayer_product_categories(){
970
971 // Some AJAX security
972 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
973
974 $attributes = '';
975 $attributes .= ' number="'. $_POST['atts']['number'] .'" ';
976 $attributes .= ' columns="'. $_POST['atts']['columns'] .'" ';
977 $attributes .= ' hide_empty="'. (!empty($_POST['atts']['hide_empty']) ? 1 : 0) .'" ';
978 $attributes .= ' orderby="'. $_POST['atts']['nuorderbymber'] .'" ';
979 $attributes .= ' order="'. $_POST['atts']['order'] .'" ';
980
981 if ( 'by_id' === $_POST['atts']['source'] ) {
982 $attributes .= ' ids="'. $_POST['atts']['by_id'] .'" ';
983 } elseif ( 'by_parent' === $_POST['atts']['source'] ) {
984 $attributes .= ' parent="'. $_POST['atts']['parent'] .'" ';
985 } elseif ( 'current_subcategories' === $_POST['atts']['source'] ) {
986 $attributes .= ' parent="'. get_queried_object_id() .'" ';
987 }
988
989 $shortcode = '[product_categories '. $attributes .']';
990
991 // do_shortcode the shortcode
992 echo do_shortcode($shortcode);
993
994 wp_die();
995 }
996
997 // Products Categories Handler
998 add_action('wp_ajax_pagelayer_product_archives', 'pagelayer_product_archives');
999 function pagelayer_product_archives(){
1000
1001 // Some AJAX security
1002 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
1003
1004 if ( WC()->session ) {
1005 wc_print_notices();
1006 }
1007
1008 $atts['paginate'] = true;
1009 $atts['cache'] = false;
1010 $no_found = $_POST['atts']['no_found'];
1011
1012 if( empty($_POST['atts']['allow_order']) ){
1013 remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );
1014 }
1015 if( empty($_POST['atts']['show_result']) ){
1016 remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
1017 }
1018
1019 $type = 'pagelayer_current_query';
1020
1021 // Set the current query
1022 add_action( 'woocommerce_shortcode_products_query', 'pagelayer_shortcode_current_query_query', 10, 10);
1023
1024 // If product not found
1025 add_action( "woocommerce_shortcode_{$type}_loop_no_results", function ($attributes) use ($no_found){
1026 echo '<div class="pagelayer-product-no-found">'.$no_found.'</div>';
1027 } );
1028
1029 // Get the products list
1030 $shortcode = new WC_Shortcode_Products( $atts, $type );
1031
1032 echo $shortcode->get_content();
1033
1034 wp_die();
1035 }
1036
1037 // Products Categories Handler
1038 add_action('wp_ajax_pagelayer_products_ajax', 'pagelayer_products_ajax');
1039 function pagelayer_products_ajax(){
1040
1041 // Some AJAX security
1042 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
1043
1044 if ( WC()->session ) {
1045 wc_print_notices();
1046 }
1047
1048 $no_found = $_POST['atts']['no_found'];
1049
1050 $attributes = '';
1051 $type = $_POST['atts']['source'];
1052 $attributes .= ' columns="'. $_POST['atts']['columns'] .'" ';
1053 $attributes .= ' rows="'. $_POST['atts']['rows'] .'" ';
1054 $attributes .= ' paginate="'. (!empty($_POST['atts']['paginate']) ? true : false) .'" ';
1055 $attributes .= ' orderby="'. $_POST['atts']['orderby'] .'" ';
1056 $attributes .= ' order="'. $_POST['atts']['order'] .'" ';
1057 $attributes .= ' cache="false" ';
1058
1059 // Hide the catalog order
1060 if( empty($_POST['atts']['allow_order']) ){
1061 remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );
1062 }
1063
1064 // Hide the result count
1065 if( empty($_POST['atts']['show_result']) ){
1066 remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
1067 }
1068
1069 if( $type == 'by_id' ){
1070 $type = 'products';
1071 $attributes .= ' ids="'. (!empty($_POST['atts']['ids']) ? $_POST['atts']['ids'] : '') .'" ';
1072 }elseif( $type == 'pagelayer_current_query' ){
1073
1074 $atts['paginate'] = (!empty($_POST['atts']['paginate']) ? true : false);
1075 $atts['cache'] = false;
1076
1077 $type = 'pagelayer_current_query';
1078
1079 // Set the current query
1080 add_action( 'woocommerce_shortcode_products_query', 'pagelayer_shortcode_current_query_query', 10, 10);
1081
1082 // If product not found
1083 add_action( "woocommerce_shortcode_{$type}_loop_no_results", function ($attributes) use ($no_found){
1084 echo '<div class="pagelayer-product-no-found">'.$no_found.'</div>';
1085 } );
1086
1087 // Get the products list
1088 $shortcode = new WC_Shortcode_Products( $atts, $type );
1089
1090 echo $shortcode->get_content();
1091 return true;
1092 }
1093
1094 $shortcode = '['.$type.' '. $attributes .']';
1095
1096 $content = do_shortcode($shortcode);
1097
1098 // If product not found
1099 if('<div class="woocommerce columns-'.$_POST['atts']['columns'] .' "></div>' == $content){
1100 $content = '<div class="pagelayer-product-no-found">'. $no_found .'</div>';
1101 }
1102
1103 echo $content;
1104
1105 wp_die();
1106 }
1107
1108 // Get Taxamony List for SiteMap
1109 add_action('wp_ajax_pagelayer_get_taxonomy_list', 'pagelayer_get_taxonomy_list');
1110 add_action('wp_ajax_nopriv_pagelayer_get_taxonomy_list', 'pagelayer_get_taxonomy_list');
1111 function pagelayer_get_taxonomy_list(){
1112
1113 $args = array(
1114 'title_li' => 0,
1115 'orderby' => $_POST['post_order'],
1116 'order' => $_POST['order'],
1117 'style' => '',
1118 'hide_empty' => $_POST['empty'],
1119 'echo' => false,
1120 'hierarchical' => (empty($_POST['hier']) || $_POST['hier'] == null ? '' : $_POST['hier']),
1121 'taxonomy' => $_POST['type'],
1122 'depth' => (empty($_POST['depth']) || $_POST['depth'] == null ? '' : $_POST['depth']),
1123 );
1124
1125 $taxonomies = get_categories( $args );
1126
1127 $option = '<ul>';
1128 foreach ( $taxonomies as $taxonomy ) {
1129 $option .= '<li class="pagelayer-sitemap-list-item" data-postID="'.$taxonomy->term_id.'"><a class="pagelayer-ele-link" href="'.get_term_link($taxonomy->term_id).'">'.$taxonomy->name.'</a></li>';
1130 }
1131 $option .= '</ul>';
1132
1133 echo $option;
1134 wp_die();
1135 }
1136
1137 // Export the template
1138 add_action('wp_ajax_pagelayer_export_template', 'pagelayer_export_template');
1139 function pagelayer_export_template(){
1140
1141 global $pagelayer;
1142
1143 // Some AJAX security
1144 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
1145
1146 $done = [];
1147
1148 // Load the templates
1149 pagelayer_builder_load_templates();
1150
1151 if(empty($pagelayer->templates)){
1152 $done['error'] = __pl('temp_export_empty');
1153 pagelayer_json_output($done);
1154 }
1155
1156 // Get the active theme
1157 $theme_dir = get_template_directory();
1158 $conf = [];
1159
1160 // Write the files
1161 foreach($pagelayer->templates as $k => $v){
1162 file_put_contents($theme_dir.'/'.$v->post_name.'.pgl', $v->post_content);
1163 $conf[$v->post_name] = [
1164 'type' => get_post_meta($v->ID, 'pagelayer_template_type', true),
1165 'conditions' => get_post_meta($v->ID, 'pagelayer_template_conditions', true),
1166 ];
1167 }
1168
1169 // Write the config
1170 file_put_contents($theme_dir.'/pagelayer.conf', json_encode($conf, JSON_PRETTY_PRINT));
1171
1172 $done['success'] = __pl('temp_export_success');
1173
1174 // Output and die
1175 pagelayer_json_output($done);
1176
1177 }
1178
1179 // Export the template
1180 add_action('wp_ajax_pagelayer_save_settings', 'pagelayer_save_settings');
1181 function pagelayer_save_settings(){
1182
1183 // Some AJAX security
1184 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
1185
1186 if(!current_user_can('edit_posts')){
1187 $msg['error'] = __pl('current_user_can_not');
1188 pagelayer_json_output($msg);
1189 }
1190
1191 $postID = (int) $_GET['postID'];
1192
1193 if(empty($postID)){
1194 $msg['error'] = __pl('invalid_post_id');
1195 pagelayer_json_output($msg);
1196 }
1197
1198 // Check if the post exists
1199
1200 if(!empty($_POST['post_title'])){
1201
1202 $post = array(
1203 'ID' => $postID,
1204 'post_title' => $_POST['post_title'],
1205 );
1206
1207 // Update the post into the database
1208 $ret = wp_update_post($post);
1209
1210 if (is_wp_error($ret)) {
1211 $msg['error'] = __pl('post_update_err');
1212 }else{
1213 $msg['success'] = __pl('post_update_success');
1214 }
1215 }
1216
1217 pagelayer_json_output($msg);
1218 }
1219