| 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 |
|
| 28 |
// The ajax handler |
| 29 |
add_action('wp_ajax_pagelayer_wp_widget', 'pagelayer_wp_widget_ajax'); |
| 30 |
function pagelayer_wp_widget_ajax(){ |
| 31 |
|
| 32 |
global $pagelayer; |
| 33 |
|
| 34 |
// Some AJAX security |
| 35 |
check_ajax_referer('pagelayer_ajax', 'nonce'); |
| 36 |
|
| 37 |
pagelayer_load_shortcodes(); |
| 38 |
|
| 39 |
header('Content-Type: application/json'); |
| 40 |
|
| 41 |
$ret = []; |
| 42 |
$tag = @$_POST['tag']; |
| 43 |
//pagelayer_print($pagelayer->shortcodes[$tag]); |
| 44 |
|
| 45 |
// No tag ? |
| 46 |
if(empty($pagelayer->shortcodes[$tag])){ |
| 47 |
$ret['error'][] = __pl('no_tag'); |
| 48 |
echo json_encode($ret); |
| 49 |
wp_die(); |
| 50 |
} |
| 51 |
|
| 52 |
// Include the widgets |
| 53 |
include_once(ABSPATH . 'wp-admin/includes/widgets.php'); |
| 54 |
|
| 55 |
$class = $pagelayer->shortcodes[$tag]['widget']; |
| 56 |
|
| 57 |
// Check the widget class exists ? |
| 58 |
if(empty($class) || !class_exists($class)){ |
| 59 |
$ret['error'][] = __pl('no_widget_class'); |
| 60 |
echo json_encode($ret); |
| 61 |
wp_die(); |
| 62 |
} |
| 63 |
|
| 64 |
$instance = []; |
| 65 |
$widget = new $class(); |
| 66 |
$widget->_set('pagelayer-widget-1234567890'); |
| 67 |
|
| 68 |
// Is there any existing data ? |
| 69 |
if(!empty($_POST['widget_data'])){ |
| 70 |
$json = json_decode(stripslashes($_POST['widget_data']), true); |
| 71 |
//pagelayer_print($json);die(); |
| 72 |
if(!empty($json)){ |
| 73 |
$instance = $json; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
// Are there any form values ? |
| 78 |
if(!empty($_POST['values'])){ |
| 79 |
parse_str(stripslashes($_POST['values']), $data); |
| 80 |
//pagelayer_print($data);die(); |
| 81 |
|
| 82 |
// Any data ? |
| 83 |
if(!empty($data)){ |
| 84 |
|
| 85 |
// First key is useless |
| 86 |
$data = current($data); |
| 87 |
|
| 88 |
// Do we still have valid data ? |
| 89 |
if(!empty($data)){ |
| 90 |
|
| 91 |
// 2nd key is useless and just over-ride instance |
| 92 |
$instance = current($data); |
| 93 |
|
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
// Get the form |
| 99 |
ob_start(); |
| 100 |
$widget->form($instance); |
| 101 |
$ret['form'] = ob_get_contents(); |
| 102 |
ob_end_clean(); |
| 103 |
|
| 104 |
// Get the html |
| 105 |
ob_start(); |
| 106 |
$widget->widget([], $instance); |
| 107 |
$ret['html'] = ob_get_contents(); |
| 108 |
ob_end_clean(); |
| 109 |
|
| 110 |
// Widget data to set |
| 111 |
if(!empty($instance)){ |
| 112 |
$ret['widget_data'] = $instance; |
| 113 |
} |
| 114 |
|
| 115 |
echo json_encode($ret); |
| 116 |
wp_die(); |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
// Update Post content |
| 121 |
add_action('wp_ajax_pagelayer_save_content', 'pagelayer_save_content'); |
| 122 |
function pagelayer_save_content(){ |
| 123 |
|
| 124 |
// Some AJAX security |
| 125 |
check_ajax_referer('pagelayer_ajax', 'nonce'); |
| 126 |
|
| 127 |
$content = $_POST['pagelayer_update_content']; |
| 128 |
|
| 129 |
$postID = (int) $_GET['postID']; |
| 130 |
|
| 131 |
if(empty($postID)){ |
| 132 |
$msg['error'] = __pl('invalid_post_id'); |
| 133 |
} |
| 134 |
|
| 135 |
// Check if the post exists |
| 136 |
|
| 137 |
if(!empty($postID) && !empty($content)){ |
| 138 |
|
| 139 |
$post = array( |
| 140 |
'ID' => $postID, |
| 141 |
'post_content' => $content, |
| 142 |
); |
| 143 |
|
| 144 |
// Update the post into the database |
| 145 |
wp_update_post($post); |
| 146 |
|
| 147 |
if (is_wp_error($postID)) { |
| 148 |
$msg['error'] = __pl('post_update_err'); |
| 149 |
}else{ |
| 150 |
$msg['success'] = __pl('post_update_success'); |
| 151 |
} |
| 152 |
|
| 153 |
}else{ |
| 154 |
$msg['error'] = __pl('post_update_err'); |
| 155 |
} |
| 156 |
|
| 157 |
echo json_encode($msg); |
| 158 |
wp_die(); |
| 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', 'nonce'); |
| 168 |
|
| 169 |
$data = ''; |
| 170 |
if(isset($_REQUEST['shortcode_data'])){ |
| 171 |
$data = stripslashes($_REQUEST['shortcode_data']); |
| 172 |
} |
| 173 |
|
| 174 |
echo do_shortcode($data); |
| 175 |
wp_die(); |
| 176 |
|
| 177 |
} |
| 178 |
|
| 179 |
// Get the Site Title |
| 180 |
add_action('wp_ajax_pagelayer_fetch_site_title', 'pagelayer_fetch_site_title'); |
| 181 |
function pagelayer_fetch_site_title(){ |
| 182 |
|
| 183 |
// Some AJAX security |
| 184 |
check_ajax_referer('pagelayer_ajax', 'nonce'); |
| 185 |
|
| 186 |
echo get_bloginfo('name'); |
| 187 |
wp_die(); |
| 188 |
} |
| 189 |
|
| 190 |
// Update the Site Title |
| 191 |
add_action('wp_ajax_pagelayer_update_site_title', 'pagelayer_update_site_title'); |
| 192 |
function pagelayer_update_site_title(){ |
| 193 |
global $wpdb; |
| 194 |
|
| 195 |
// Some AJAX security |
| 196 |
check_ajax_referer('pagelayer_ajax', 'nonce'); |
| 197 |
|
| 198 |
$site_title = $_POST['site_title']; |
| 199 |
|
| 200 |
update_option('blogname', $site_title); |
| 201 |
|
| 202 |
$wpdb->query("UPDATE `sm_sitemeta` |
| 203 |
SET meta_value = '".$site_title."' |
| 204 |
WHERE meta_key = 'site_name'"); |
| 205 |
wp_die(); |
| 206 |
} |
| 207 |
|
| 208 |
// Show the SideBars |
| 209 |
add_action('wp_ajax_pagelayer_fetch_sidebar', 'pagelayer_fetch_sidebar'); |
| 210 |
function pagelayer_fetch_sidebar(){ |
| 211 |
|
| 212 |
global $wp_registered_sidebars; |
| 213 |
|
| 214 |
// Some AJAX security |
| 215 |
check_ajax_referer('pagelayer_ajax', 'nonce'); |
| 216 |
|
| 217 |
// Create a list |
| 218 |
$pagelayer_wp_widgets = array(); |
| 219 |
|
| 220 |
foreach($wp_registered_sidebars as $v){ |
| 221 |
$pagelayer_wp_widgets[$v['id']] = $v['name']; |
| 222 |
} |
| 223 |
|
| 224 |
$id = @$_REQUEST['sidebar']; |
| 225 |
|
| 226 |
if(function_exists('dynamic_sidebar') && !empty($pagelayer_wp_widgets[$id])) { |
| 227 |
ob_start(); |
| 228 |
dynamic_sidebar($id); |
| 229 |
$result = ob_get_clean(); |
| 230 |
}else{ |
| 231 |
$result = __pl('no_widget_area'); |
| 232 |
} |
| 233 |
|
| 234 |
echo $result; |
| 235 |
wp_die(); |
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
// Show the primary menu ! |
| 240 |
add_action('wp_ajax_pagelayer_fetch_primary_menu', 'pagelayer_fetch_primary_menu'); |
| 241 |
function pagelayer_fetch_primary_menu(){ |
| 242 |
|
| 243 |
// Some AJAX security |
| 244 |
check_ajax_referer('pagelayer_ajax', 'nonce'); |
| 245 |
|
| 246 |
if(isset($_POST['nav_list'])){ |
| 247 |
echo wp_nav_menu([ |
| 248 |
'menu' => wp_get_nav_menu_object($_POST['nav_list']), |
| 249 |
'menu_id' => $_POST["nav_list"], |
| 250 |
//'theme_location' => 'primary', |
| 251 |
//'menu_class' => 'primary-menu', |
| 252 |
]); |
| 253 |
} |
| 254 |
|
| 255 |
wp_die(); |
| 256 |
} |
| 257 |
|
| 258 |
// Get post revision |
| 259 |
add_action('wp_ajax_pagelayer_get_revision', 'pagelayer_get_revision'); |
| 260 |
function pagelayer_get_revision(){ |
| 261 |
|
| 262 |
// Some AJAX security |
| 263 |
check_ajax_referer('pagelayer_ajax', 'nonce'); |
| 264 |
|
| 265 |
$postID = (int) $_GET['postID']; |
| 266 |
$post_revisions = array(); |
| 267 |
|
| 268 |
if(empty($postID)){ |
| 269 |
$post_revisions['error'] = __pl('invalid_post_id'); |
| 270 |
}else{ |
| 271 |
$post_revisions = pagelayer_get_post_revision_by_id($postID); |
| 272 |
} |
| 273 |
|
| 274 |
echo json_encode($post_revisions); |
| 275 |
wp_die(); |
| 276 |
} |
| 277 |
|
| 278 |
// Get post revision |
| 279 |
add_action('wp_ajax_pagelayer_apply_revision', 'pagelayer_apply_revision'); |
| 280 |
function pagelayer_apply_revision(){ |
| 281 |
|
| 282 |
// Some AJAX security |
| 283 |
check_ajax_referer('pagelayer_ajax', 'nonce'); |
| 284 |
|
| 285 |
$revisionID = (int) $_REQUEST['revisionID']; |
| 286 |
$post_data = array(); |
| 287 |
|
| 288 |
if(empty($revisionID)){ |
| 289 |
$post_data['error'] = __pl('invalid_post_id'); |
| 290 |
}else{ |
| 291 |
|
| 292 |
$post = get_post( $revisionID ); |
| 293 |
|
| 294 |
if ( empty( $post ) ) { |
| 295 |
$post_data['error'] = __pl('invalid_revision'); |
| 296 |
echo json_encode($post_data); |
| 297 |
return false; |
| 298 |
} |
| 299 |
|
| 300 |
// Need to make the reviews post global |
| 301 |
$GLOBALS['post'] = $post; |
| 302 |
|
| 303 |
// Need to reload the shortcodes |
| 304 |
pagelayer_load_shortcodes(); |
| 305 |
|
| 306 |
$post_data['content'] = do_shortcode($post->post_content); |
| 307 |
|
| 308 |
if (is_wp_error($postID)) { |
| 309 |
$post_data['error'] = __pl('rev_load_error'); |
| 310 |
}else{ |
| 311 |
$post_data['success'] = __pl('rev_load_success'); |
| 312 |
} |
| 313 |
|
| 314 |
wp_reset_postdata(); |
| 315 |
} |
| 316 |
|
| 317 |
echo json_encode($post_data); |
| 318 |
wp_die(); |
| 319 |
} |
| 320 |
|
| 321 |
// Get post revision |
| 322 |
add_action('wp_ajax_pagelayer_delete_revision', 'pagelayer_delete_revision'); |
| 323 |
function pagelayer_delete_revision() { |
| 324 |
|
| 325 |
// Some AJAX security |
| 326 |
check_ajax_referer('pagelayer_ajax', 'nonce'); |
| 327 |
|
| 328 |
$revisionID = (int) $_REQUEST['revisionID']; |
| 329 |
|
| 330 |
if(empty($revisionID)){ |
| 331 |
$post_data['error'] = __pl('invalid_post_id'); |
| 332 |
}else{ |
| 333 |
|
| 334 |
$revision = get_post( $revisionID ); |
| 335 |
|
| 336 |
if ( empty( $revision ) ) { |
| 337 |
$post_data['error'] = __pl('invalid_revision'); |
| 338 |
}else{ |
| 339 |
|
| 340 |
if ( ! current_user_can( 'delete_post', $revision->ID ) ) { |
| 341 |
$post_data['error'] = __pl('access_denied'); |
| 342 |
echo json_encode($post_data); |
| 343 |
return false; |
| 344 |
} |
| 345 |
|
| 346 |
$deleted = wp_delete_post_revision( $revision->ID ); |
| 347 |
|
| 348 |
if ( ! $deleted || is_wp_error( $deleted ) ) { |
| 349 |
$post_data['error'] = __pl('delete_rev_error'); |
| 350 |
}else{ |
| 351 |
$post_data['success'] = __pl('delete_rev_success'); |
| 352 |
} |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
echo json_encode($post_data); |
| 357 |
wp_die(); |
| 358 |
} |
| 359 |
|
| 360 |
// Get post revision |
| 361 |
add_action('wp_ajax_pagelayer_post_nav', 'pagelayer_post_nav'); |
| 362 |
function pagelayer_post_nav() { |
| 363 |
|
| 364 |
// Some AJAX security |
| 365 |
check_ajax_referer('pagelayer_ajax', 'nonce'); |
| 366 |
|
| 367 |
if(!isset($_REQUEST['data']) || !isset($_REQUEST['postID'])){ |
| 368 |
return; |
| 369 |
} |
| 370 |
|
| 371 |
$el['atts'] = $_REQUEST['data']; |
| 372 |
|
| 373 |
$post = get_post($_REQUEST['postID']); |
| 374 |
|
| 375 |
// Need to make this post global |
| 376 |
$GLOBALS['post'] = $post; |
| 377 |
|
| 378 |
$in_same_term = false; |
| 379 |
$taxonomies = 'category'; |
| 380 |
$title = ''; |
| 381 |
$arrows_list = $el['atts']['arrows_list']; |
| 382 |
|
| 383 |
if($el['atts']['in_same_term']){ |
| 384 |
$in_same_term = true; |
| 385 |
$taxonomies = $el['atts']['taxonomies']; |
| 386 |
} |
| 387 |
|
| 388 |
if($el['atts']['post_title']){ |
| 389 |
$title = '<span class="pagelayer-post-nav-title">%title</span>'; |
| 390 |
} |
| 391 |
|
| 392 |
$next_label = '<span class="pagelayer-next-holder"> |
| 393 |
<span class="pagelayer-post-nav-link"> '.$el["atts"]["next_label"].'</span>'.$title.' |
| 394 |
</span> |
| 395 |
<span class="pagelayer-post-nav-icon fa fa-'.$arrows_list.'-right"></span>'; |
| 396 |
|
| 397 |
$prev_label = '<span class="pagelayer-post-nav-icon fa fa-'.$arrows_list.'-left"></span> |
| 398 |
<span class="pagelayer-next-holder"> |
| 399 |
<span class="pagelayer-post-nav-link"> '.$el["atts"]["prev_label"].'</span>'.$title.' |
| 400 |
</span>'; |
| 401 |
|
| 402 |
$el['atts']['next_link'] = get_next_post_link('%link', $next_label, $in_same_term, '', $taxonomies); |
| 403 |
|
| 404 |
$el['atts']['prev_link'] = get_previous_post_link('%link', $prev_label, $in_same_term, '', $taxonomies ); |
| 405 |
|
| 406 |
echo json_encode($el); |
| 407 |
wp_die(); |
| 408 |
|
| 409 |
} |
| 410 |
|
| 411 |
// Get post comment template |
| 412 |
add_action('wp_ajax_pagelayer_post_comment', 'pagelayer_post_comment'); |
| 413 |
function pagelayer_post_comment() { |
| 414 |
global $post; |
| 415 |
|
| 416 |
// Some AJAX security |
| 417 |
check_ajax_referer('pagelayer_ajax', 'nonce'); |
| 418 |
|
| 419 |
if(!isset($_REQUEST['postID'])){ |
| 420 |
return true; |
| 421 |
} |
| 422 |
|
| 423 |
$GLOBALS['post'] = get_post($_REQUEST['postID']); |
| 424 |
$GLOBALS['withcomments'] = true; |
| 425 |
|
| 426 |
if ( comments_open() || get_comments_number() ) { |
| 427 |
echo '<div class="pagelayer-comments-template">'.comments_template().'</div>'; |
| 428 |
}else{ |
| 429 |
echo '<div class="pagelayer-comments-close"> |
| 430 |
<h2>Comments are closed!</h2> |
| 431 |
</div>'; |
| 432 |
} |
| 433 |
wp_die(); |
| 434 |
|
| 435 |
} |
| 436 |
|
| 437 |
// Get post comment template |
| 438 |
add_action('wp_ajax_pagelayer_post_info', 'pagelayer_post_info'); |
| 439 |
function pagelayer_post_info() { |
| 440 |
global $post; |
| 441 |
|
| 442 |
// Some AJAX security |
| 443 |
check_ajax_referer('pagelayer_ajax', 'nonce'); |
| 444 |
|
| 445 |
if(!isset($_REQUEST['postID']) || !isset($_REQUEST['el'])){ |
| 446 |
return true; |
| 447 |
} |
| 448 |
|
| 449 |
$el['atts'] = $_REQUEST['el']; |
| 450 |
|
| 451 |
$GLOBALS['post'] = get_post($_REQUEST['postID']); |
| 452 |
|
| 453 |
$post_info_content =''; |
| 454 |
$link =''; |
| 455 |
$info_content =''; |
| 456 |
$avatar_url =''; |
| 457 |
|
| 458 |
switch($el['atts']['type']){ |
| 459 |
case 'author': |
| 460 |
|
| 461 |
$link = get_author_posts_url( get_the_author_meta( 'ID' ) ); |
| 462 |
$avatar_url = get_avatar_url( get_the_author_meta( 'ID' ), 96 ); |
| 463 |
$post_info_content = get_the_author_meta( 'display_name', $post->post_author ); |
| 464 |
break; |
| 465 |
|
| 466 |
case 'date': |
| 467 |
|
| 468 |
$format = [ |
| 469 |
'default' => 'F j, Y', |
| 470 |
'0' => 'F j, Y', |
| 471 |
'1' => 'Y-m-d', |
| 472 |
'2' => 'm/d/Y', |
| 473 |
'3' => 'd/m/Y', |
| 474 |
'custom' => empty( $el['atts']['date_format_custom'] ) ? 'F j, Y' : $el['atts']['date_format_custom'], |
| 475 |
]; |
| 476 |
|
| 477 |
$post_info_content = get_the_time( $format[ $el['atts']['date_format'] ] ); |
| 478 |
$link = get_day_link( get_post_time( 'Y' ), get_post_time( 'm' ), get_post_time( 'j' ) ); |
| 479 |
|
| 480 |
break; |
| 481 |
|
| 482 |
case 'time': |
| 483 |
|
| 484 |
$format = [ |
| 485 |
'default' => 'g:i a', |
| 486 |
'0' => 'g:i a', |
| 487 |
'1' => 'g:i A', |
| 488 |
'2' => 'H:i', |
| 489 |
'custom' => empty( $el['atts']['time_format_custom'] ) ? 'F j, Y' : $el['atts']['time_format_custom'], |
| 490 |
]; |
| 491 |
$post_info_content = get_the_time( $format[ $el['atts']['time_format'] ] ); |
| 492 |
|
| 493 |
break; |
| 494 |
|
| 495 |
case 'comments': |
| 496 |
|
| 497 |
if (comments_open()) { |
| 498 |
$post_info_content = (int) get_comments_number(); |
| 499 |
$link = get_comments_link(); |
| 500 |
} |
| 501 |
|
| 502 |
break; |
| 503 |
|
| 504 |
case 'terms': |
| 505 |
|
| 506 |
$taxonomy = $el['atts']['taxonomy']; |
| 507 |
$terms = wp_get_post_terms( get_the_ID(), $taxonomy ); |
| 508 |
foreach ( $terms as $term ) { |
| 509 |
$post_info_content .= ' <a if-ext="{{info_link}}" href="'. get_term_link( $term ) .'" class="pagelayer-post-info-list-link"> '. $term->name .' </a>'; |
| 510 |
} |
| 511 |
|
| 512 |
break; |
| 513 |
|
| 514 |
case 'custom': |
| 515 |
|
| 516 |
$post_info_content = $el['atts']['type_custom']; |
| 517 |
$link = $el['atts']['info_custom_link']; |
| 518 |
|
| 519 |
break; |
| 520 |
} |
| 521 |
|
| 522 |
$el['atts']['post_info_content'] = $post_info_content; |
| 523 |
$el['atts']['avatar_url'] = $avatar_url; |
| 524 |
$el['atts']['link'] = $link; |
| 525 |
|
| 526 |
echo json_encode($el['atts']); |
| 527 |
wp_die(); |
| 528 |
|
| 529 |
} |