PluginProbe
Page Builder: Pagelayer – Drag and Drop website builder / 0.9.7
Page Builder: Pagelayer – Drag and Drop website builder v0.9.7
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.7, at main/ajax.php

1,231 lines 30.5 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 // Posts Slider
713 add_action('wp_ajax_pagelayer_posts_slider_data', 'pagelayer_posts_slider_data');
714 function pagelayer_posts_slider_data(){
715
716 // Some AJAX security
717 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
718
719 // Load shortcodes
720 echo pagelayer_posts_slider($_POST);
721 wp_die();
722 }
723
724 // Get the data for template
725 add_action('wp_ajax_pagelayer_search_ids', 'pagelayer_search_ids');
726 function pagelayer_search_ids() {
727
728 // Some AJAX security
729 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
730
731 if ( empty( $_POST['filter_type'] ) || empty( $_POST['search'] ) ) {
732 wp_die();
733 }
734
735 $sel_opt = '';
736
737 switch ( $_POST['filter_type'] ) {
738 case 'taxonomy':
739 $query_params = [
740 'taxonomy' => $_POST['object_type'],
741 'search' => $_POST['search'],
742 'hide_empty' => false,
743 ];
744
745 $terms = get_terms( $query_params );
746
747 global $wp_taxonomies;
748
749 foreach ( $terms as $term ) {
750 $sel_opt .= '<span class="pagelayer-temp-search-sel-span" value="'. $term->term_taxonomy_id .'">'. $term->name .'</span>';
751 }
752
753 break;
754
755 case 'post':
756 $query_params = [
757 'post_type' => $_POST['object_type'], //$this->extract_post_type( $data ),
758 's' => $_POST['search'],
759 'posts_per_page' => -1,
760 ];
761
762 if ( 'attachment' === $query_params['post_type'] ) {
763 $query_params['post_status'] = 'inherit';
764 }
765
766 $query = new \WP_Query( $query_params );
767
768 foreach ( $query->posts as $post ) {
769 $sel_opt .= '<span class="pagelayer-temp-search-sel-span" value="'. $post->ID .'">'. $post->post_title .'</span>';
770 }
771 break;
772
773 case 'author':
774 $query_params = [
775 'who' => 'authors',
776 'fields' => [
777 'ID',
778 'display_name',
779 ],
780 'search' => '*' . $_POST["search"] . '*',
781 'search_columns' => [
782 'user_login',
783 'user_nicename',
784 ],
785 ];
786
787 $user_query = new \WP_User_Query( $query_params );
788
789 foreach ( $user_query->get_results() as $author ) {
790 $sel_opt .= '<span class="pagelayer-temp-search-sel-span" value="'. $author->ID .'">'. $author->display_name .'</span>';
791 }
792 break;
793 default:
794 $sel_opt = 'Result Not Found';
795 }
796
797 if(!empty($sel_opt)){
798 echo $sel_opt;
799 }else{
800 echo 'Result Not Found';
801 }
802
803 wp_die();
804 }
805
806
807 // Save the post data from pagelayer setting page
808 add_action('wp_ajax_pagelayer_save_template', 'pagelayer_save_template');
809 function pagelayer_save_template() {
810
811 // Some AJAX security
812 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
813
814 $done = [];
815
816 $post_id = (int) $_GET['postID'];
817
818 // We need to create the post
819 if(empty($post_id)){
820
821 // Get the template type
822 if(empty($_POST['pagelayer_template_type'])){
823 $done['error'] = __pl('temp_error_type');
824 pagelayer_json_output($done);
825 }
826
827 $ret = wp_insert_post([
828 'post_title' => $_POST['pagelayer_lib_title'],
829 'post_type' => 'pagelayer-template',
830 'post_status' => 'publish',
831 'comment_status' => 'closed',
832 'ping_status' => 'closed'
833 ]);
834
835 // An error occured
836 if(is_wp_error($ret)){
837 $done['error'] = __pl('temp_error').' : '.$ret->get_error_message();
838 pagelayer_json_output($done);
839 }
840
841 $post_id = $ret;
842 $done['id'] = $post_id;
843
844 // Save our template type
845 $ret = update_post_meta($post_id, 'pagelayer_template_type', $_POST['pagelayer_template_type']);
846
847 }
848
849 // The ID in consideration
850 $done['id'] = $post_id;
851
852 // Check if the post title in not empty
853 if(!empty($_POST['pagelayer_lib_title'])){
854
855 $post = array(
856 'ID' => $post_id,
857 'post_title' => $_POST['pagelayer_lib_title'],
858 );
859
860 // Update the post into the database
861 $ret = wp_update_post($post);
862
863 }
864
865 // Save template library display conditions
866 $condi_array = array();
867 $condi_len = count($_POST['pagelayer_condition_type']);
868 if($_POST['pagelayer_template_type'] != 'section'){
869 for( $i =0; $i < $condi_len; $i++ ){
870 $condi_array[$i] = array(
871 'type' => $_POST['pagelayer_condition_type'][$i],
872 'template' => $_POST['pagelayer_condition_name'][$i],
873 'sub_template' => $_POST['pagelayer_condition_sub_template'][$i],
874 'id' => $_POST['pagelayer_condition_id'][$i],
875 );
876 }
877 }
878 //print_r($condi_array);
879
880 $ret = update_post_meta($post_id, 'pagelayer_template_conditions', $condi_array);
881
882 if(is_wp_error($post_id)){
883 $done['error'] = __pl('temp_error').' : '.$ret->get_error_message();
884 }else{
885 $done['success'] = __pl('temp_update_success');
886 }
887
888 pagelayer_json_output($done);
889
890 }
891
892 // Product Images Handler
893 add_action('wp_ajax_pagelayer_product_images', 'pagelayer_product_images');
894 function pagelayer_product_images(){
895 global $product;
896
897 // Some AJAX security
898 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
899
900 if ( !isset($_REQUEST['postID']) ) {
901 return;
902 }
903
904 $product = wc_get_product($_REQUEST['postID']);
905
906 if ( empty( $product ) ) {
907 return ;
908 }
909
910 if ( isset($_POST['sale_flash']) ) {
911 wc_get_template( 'loop/sale-flash.php' );
912 }
913 wc_get_template( 'single-product/product-image.php' );
914
915 // On render widget from Editor - trigger the init manually.
916 echo '
917 <script>
918 jQuery(".woocommerce-product-gallery").each( function() {
919 jQuery(this).wc_product_gallery();
920 } );
921 </script>
922 ';
923
924 wp_die();
925 }
926
927 // Related Products Handler
928 add_action('wp_ajax_pagelayer_product_related', 'pagelayer_product_related');
929 function pagelayer_product_related(){
930 global $product;
931
932 // Some AJAX security
933 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
934
935 if ( !isset($_REQUEST['postID']) ) {
936 return;
937 }
938
939 $product = wc_get_product($_REQUEST['postID']);
940
941 if ( empty( $product ) ) {
942 return ;
943 }
944
945 $args = $_REQUEST['pagelayer_args'];
946
947 if(function_exists( 'woocommerce_related_products' )){
948 woocommerce_related_products($args);
949 }
950
951 wp_die();
952 }
953
954 // Upsell Products Handler
955 add_action('wp_ajax_pagelayer_product_upsell', 'pagelayer_product_upsell');
956 function pagelayer_product_upsell(){
957 global $product;
958
959 // Some AJAX security
960 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
961
962 if ( !isset($_REQUEST['postID']) ) {
963 return;
964 }
965
966 $product = wc_get_product($_REQUEST['postID']);
967
968 if ( empty( $product ) ) {
969 return ;
970 }
971
972 if(function_exists( 'woocommerce_related_products' )){
973 woocommerce_upsell_display( $_REQUEST['limit'], $_REQUEST['columns'], $_REQUEST['orderby'], $_REQUEST['order'] );
974 }
975
976 wp_die();
977 }
978
979 // Products Categories Handler
980 add_action('wp_ajax_pagelayer_product_categories', 'pagelayer_product_categories');
981 function pagelayer_product_categories(){
982
983 // Some AJAX security
984 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
985
986 $attributes = '';
987 $attributes .= ' number="'. $_POST['atts']['number'] .'" ';
988 $attributes .= ' columns="'. $_POST['atts']['columns'] .'" ';
989 $attributes .= ' hide_empty="'. (!empty($_POST['atts']['hide_empty']) ? 1 : 0) .'" ';
990 $attributes .= ' orderby="'. $_POST['atts']['nuorderbymber'] .'" ';
991 $attributes .= ' order="'. $_POST['atts']['order'] .'" ';
992
993 if ( 'by_id' === $_POST['atts']['source'] ) {
994 $attributes .= ' ids="'. $_POST['atts']['by_id'] .'" ';
995 } elseif ( 'by_parent' === $_POST['atts']['source'] ) {
996 $attributes .= ' parent="'. $_POST['atts']['parent'] .'" ';
997 } elseif ( 'current_subcategories' === $_POST['atts']['source'] ) {
998 $attributes .= ' parent="'. get_queried_object_id() .'" ';
999 }
1000
1001 $shortcode = '[product_categories '. $attributes .']';
1002
1003 // do_shortcode the shortcode
1004 echo do_shortcode($shortcode);
1005
1006 wp_die();
1007 }
1008
1009 // Products Categories Handler
1010 add_action('wp_ajax_pagelayer_product_archives', 'pagelayer_product_archives');
1011 function pagelayer_product_archives(){
1012
1013 // Some AJAX security
1014 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
1015
1016 if ( WC()->session ) {
1017 wc_print_notices();
1018 }
1019
1020 $atts['paginate'] = true;
1021 $atts['cache'] = false;
1022 $no_found = $_POST['atts']['no_found'];
1023
1024 if( empty($_POST['atts']['allow_order']) ){
1025 remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );
1026 }
1027 if( empty($_POST['atts']['show_result']) ){
1028 remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
1029 }
1030
1031 $type = 'pagelayer_current_query';
1032
1033 // Set the current query
1034 add_action( 'woocommerce_shortcode_products_query', 'pagelayer_shortcode_current_query_query', 10, 10);
1035
1036 // If product not found
1037 add_action( "woocommerce_shortcode_{$type}_loop_no_results", function ($attributes) use ($no_found){
1038 echo '<div class="pagelayer-product-no-found">'.$no_found.'</div>';
1039 } );
1040
1041 // Get the products list
1042 $shortcode = new WC_Shortcode_Products( $atts, $type );
1043
1044 echo $shortcode->get_content();
1045
1046 wp_die();
1047 }
1048
1049 // Products Categories Handler
1050 add_action('wp_ajax_pagelayer_products_ajax', 'pagelayer_products_ajax');
1051 function pagelayer_products_ajax(){
1052
1053 // Some AJAX security
1054 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
1055
1056 if ( WC()->session ) {
1057 wc_print_notices();
1058 }
1059
1060 $no_found = $_POST['atts']['no_found'];
1061
1062 $attributes = '';
1063 $type = $_POST['atts']['source'];
1064 $attributes .= ' columns="'. $_POST['atts']['columns'] .'" ';
1065 $attributes .= ' rows="'. $_POST['atts']['rows'] .'" ';
1066 $attributes .= ' paginate="'. (!empty($_POST['atts']['paginate']) ? true : false) .'" ';
1067 $attributes .= ' orderby="'. $_POST['atts']['orderby'] .'" ';
1068 $attributes .= ' order="'. $_POST['atts']['order'] .'" ';
1069 $attributes .= ' cache="false" ';
1070
1071 // Hide the catalog order
1072 if( empty($_POST['atts']['allow_order']) ){
1073 remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );
1074 }
1075
1076 // Hide the result count
1077 if( empty($_POST['atts']['show_result']) ){
1078 remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
1079 }
1080
1081 if( $type == 'by_id' ){
1082 $type = 'products';
1083 $attributes .= ' ids="'. (!empty($_POST['atts']['ids']) ? $_POST['atts']['ids'] : '') .'" ';
1084 }elseif( $type == 'pagelayer_current_query' ){
1085
1086 $atts['paginate'] = (!empty($_POST['atts']['paginate']) ? true : false);
1087 $atts['cache'] = false;
1088
1089 $type = 'pagelayer_current_query';
1090
1091 // Set the current query
1092 add_action( 'woocommerce_shortcode_products_query', 'pagelayer_shortcode_current_query_query', 10, 10);
1093
1094 // If product not found
1095 add_action( "woocommerce_shortcode_{$type}_loop_no_results", function ($attributes) use ($no_found){
1096 echo '<div class="pagelayer-product-no-found">'.$no_found.'</div>';
1097 } );
1098
1099 // Get the products list
1100 $shortcode = new WC_Shortcode_Products( $atts, $type );
1101
1102 echo $shortcode->get_content();
1103 return true;
1104 }
1105
1106 $shortcode = '['.$type.' '. $attributes .']';
1107
1108 $content = do_shortcode($shortcode);
1109
1110 // If product not found
1111 if('<div class="woocommerce columns-'.$_POST['atts']['columns'] .' "></div>' == $content){
1112 $content = '<div class="pagelayer-product-no-found">'. $no_found .'</div>';
1113 }
1114
1115 echo $content;
1116
1117 wp_die();
1118 }
1119
1120 // Get Taxamony List for SiteMap
1121 add_action('wp_ajax_pagelayer_get_taxonomy_list', 'pagelayer_get_taxonomy_list');
1122 add_action('wp_ajax_nopriv_pagelayer_get_taxonomy_list', 'pagelayer_get_taxonomy_list');
1123 function pagelayer_get_taxonomy_list(){
1124
1125 $args = array(
1126 'title_li' => 0,
1127 'orderby' => $_POST['post_order'],
1128 'order' => $_POST['order'],
1129 'style' => '',
1130 'hide_empty' => $_POST['empty'],
1131 'echo' => false,
1132 'hierarchical' => (empty($_POST['hier']) || $_POST['hier'] == null ? '' : $_POST['hier']),
1133 'taxonomy' => $_POST['type'],
1134 'depth' => (empty($_POST['depth']) || $_POST['depth'] == null ? '' : $_POST['depth']),
1135 );
1136
1137 $taxonomies = get_categories( $args );
1138
1139 $option = '<ul>';
1140 foreach ( $taxonomies as $taxonomy ) {
1141 $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>';
1142 }
1143 $option .= '</ul>';
1144
1145 echo $option;
1146 wp_die();
1147 }
1148
1149 // Export the template
1150 add_action('wp_ajax_pagelayer_export_template', 'pagelayer_export_template');
1151 function pagelayer_export_template(){
1152
1153 global $pagelayer;
1154
1155 // Some AJAX security
1156 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
1157
1158 $done = [];
1159
1160 // Load the templates
1161 pagelayer_builder_load_templates();
1162
1163 if(empty($pagelayer->templates)){
1164 $done['error'] = __pl('temp_export_empty');
1165 pagelayer_json_output($done);
1166 }
1167
1168 // Get the active theme
1169 $theme_dir = get_template_directory();
1170 $conf = [];
1171
1172 // Write the files
1173 foreach($pagelayer->templates as $k => $v){
1174 file_put_contents($theme_dir.'/'.$v->post_name.'.pgl', $v->post_content);
1175 $conf[$v->post_name] = [
1176 'type' => get_post_meta($v->ID, 'pagelayer_template_type', true),
1177 'conditions' => get_post_meta($v->ID, 'pagelayer_template_conditions', true),
1178 ];
1179 }
1180
1181 // Write the config
1182 file_put_contents($theme_dir.'/pagelayer.conf', json_encode($conf, JSON_PRETTY_PRINT));
1183
1184 $done['success'] = __pl('temp_export_success');
1185
1186 // Output and die
1187 pagelayer_json_output($done);
1188
1189 }
1190
1191 // Export the template
1192 add_action('wp_ajax_pagelayer_save_settings', 'pagelayer_save_settings');
1193 function pagelayer_save_settings(){
1194
1195 // Some AJAX security
1196 check_ajax_referer('pagelayer_ajax', 'pagelayer_nonce');
1197
1198 if(!current_user_can('edit_posts')){
1199 $msg['error'] = __pl('current_user_can_not');
1200 pagelayer_json_output($msg);
1201 }
1202
1203 $postID = (int) $_GET['postID'];
1204
1205 if(empty($postID)){
1206 $msg['error'] = __pl('invalid_post_id');
1207 pagelayer_json_output($msg);
1208 }
1209
1210 // Check if the post exists
1211
1212 if(!empty($_POST['post_title'])){
1213
1214 $post = array(
1215 'ID' => $postID,
1216 'post_title' => $_POST['post_title'],
1217 );
1218
1219 // Update the post into the database
1220 $ret = wp_update_post($post);
1221
1222 if (is_wp_error($ret)) {
1223 $msg['error'] = __pl('post_update_err');
1224 }else{
1225 $msg['success'] = __pl('post_update_success');
1226 }
1227 }
1228
1229 pagelayer_json_output($msg);
1230 }
1231