ajax-functions.php 38.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
<?php
/**
 * AJAX Support Functions
 *
 * @package BadgeOS
 * @subpackage AJAX
 * @author LearningTimes, LLC
 * @license http://www.gnu.org/licenses/agpl.txt GNU AGPL v3.0
 * @link https://credly.com
 */

// Setup our Badgeos AJAX actions
$badgeos_ajax_actions = array(
	'get-achievements',
	'get-earned-achievements',
	'get-earned-ranks',
	'get-achievements-select2',
	'get-achievement-types',
	'get-users',
	'badgeos-get-users-list',
	'get-ranks-list',
	'get-achievements-award-list',
);

// Register core Ajax calls.
foreach ( $badgeos_ajax_actions as $action ) {
	add_action( 'wp_ajax_' . $action, 'badgeos_ajax_' . str_replace( '-', '_', $action ), 1 );
	add_action( 'wp_ajax_nopriv_' . $action, 'badgeos_ajax_' . str_replace( '-', '_', $action ), 1 );
}

/**
 * AJAX Helper for selecting users in Shortcode Embedder.
 *
 * @since 1.4.0
 */
function badgeos_ajax_get_achievements_award_list() {

	if( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ) {
		return wp_send_json_error( 'Request could not be completed' );	
	}

	// Check for nonce security.
	if ( isset( $_REQUEST['nonce'] ) && ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['nonce'] ) ), 'bos-security' ) ) {
		wp_send_json_error( 'Request blocked' );
	}

	// If no query was sent, die here.
	if ( ! isset( $_REQUEST['q'] ) ) {
		die();
	}

	global $wpdb;

	$badgeos_settings = ( $exists = badgeos_utilities::get_option( 'badgeos_settings' ) ) ? $exists : array();
	$achievement_id   = isset( $_REQUEST['achievement_id'] ) ? sanitize_text_field( $_REQUEST['achievement_id'] ) : '';
	$user_id          = isset( $_REQUEST['user_id'] ) ? sanitize_text_field( $_REQUEST['user_id'] ) : '';
	$q                = isset( $_REQUEST['q'] ) ? sanitize_text_field( $_REQUEST['q'] ) : '';
	$table_name       = $wpdb->prefix . 'badgeos_achievements';

	$sql = "SELECT entry_id as id, CONCAT(achievement_title, ' : ', entry_id) as label, entry_id as value FROM {$table_name} 
    WHERE post_type != '{$badgeos_settings['achievement_step_post_type']}'";

	// Build our query
	if ( ! empty( $q ) ) {
		$sql .= " and achievement_title LIKE '%{$q}%'";
	}

	// Build our query
	if ( ! empty( $achievement_id ) ) {
		$sql .= " and ID = '{$achievement_id}'";
	}

	// Build our query
	if ( ! empty( $user_id ) ) {
		$sql .= " and user_id = '{$user_id}'";
	}
	// Fetch our results (store as associative array).
	$results = $wpdb->get_results( $sql, ARRAY_A  );
	// Return our results
	wp_send_json( $results );
}

/**
 * AJAX Helper for returning earned ranks.
 *
 * @since 1.0.0
 * @return void
 */
function badgeos_ajax_get_ranks_list() {

	if( ! check_ajax_referer( 'bos-security', 'nonce' ) ) {
		return;
	}

	// Check for nonce security.
	if ( isset( $_REQUEST['nonce'] ) && ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['nonce'] ) ), 'bos-security' ) ) {
		wp_send_json_error( 'Request blocked' );
	}

	global $blog_id, $wpdb;

	$badgeos_settings                    = ( $exists = badgeos_utilities::get_option( 'badgeos_settings' ) ) ? $exists : array();
	$earned_ranks_shortcode_default_view = ( ! empty( $badgeos_settings['earned_ranks_shortcode_default_view'] ) ) ? $badgeos_settings['earned_ranks_shortcode_default_view'] : 'list';

	// Setup our AJAX query vars.
	$type             = isset( $_REQUEST['types'] ) ? sanitize_text_field( $_REQUEST['types'] ) : false;
	$limit            = isset( $_REQUEST['limit'] ) ? sanitize_text_field( $_REQUEST['limit'] ) : false;
	$offset           = isset( $_REQUEST['offset'] ) ? sanitize_text_field( $_REQUEST['offset'] ) : false;
	$count            = isset( $_REQUEST['count'] ) ? sanitize_text_field( $_REQUEST['count'] ) : false;
	$search           = isset( $_REQUEST['search'] ) ? sanitize_text_field( $_REQUEST['search'] ) : false;
	$user_id          = isset( $_REQUEST['user_id'] ) ? sanitize_text_field( $_REQUEST['user_id'] ) : get_current_user_id();
	$orderby          = isset( $_REQUEST['orderby'] ) ? sanitize_text_field( $_REQUEST['orderby'] ) : 'rank_id';
	$order            = isset( $_REQUEST['order'] ) ? sanitize_text_field( $_REQUEST['order'] ) : 'ASC';
	$show_title       = isset( $_REQUEST['show_title'] ) ? sanitize_text_field( $_REQUEST['show_title'] ) : 'true';
	$show_thumb       = isset( $_REQUEST['show_thumb'] ) ? sanitize_text_field( $_REQUEST['show_thumb'] ) : 'true';
	$show_description = isset( $_REQUEST['show_description'] ) ? sanitize_text_field( $_REQUEST['show_description'] ) : 'true';

	$image_width  = isset( $_REQUEST['image_width'] ) ? sanitize_text_field( $_REQUEST['image_width'] ) : '';
	$image_height = isset( $_REQUEST['image_height'] ) ? sanitize_text_field( $_REQUEST['image_height'] ) : '';

	// Convert $type to properly support multiple rank types.
	$earned_ranks_shortcode_default_view = ! empty( $_REQUEST['default_view'] ) ? sanitize_text_field( $_REQUEST['default_view'] ) : $earned_ranks_shortcode_default_view;

	if ( 'all' === $type ) {
		$type = badgeos_get_rank_types_slugs();
		// Drop steps from our list of "all" achievements.
		$step_key = array_search( trim( $badgeos_settings['ranks_step_post_type'] ), $type );
		if ( $step_key ) {
			unset( $type[ $step_key ] );
		}
	} else {
		$type = explode( ',', $type );
	}

	// Get the current user if one wasn't specified.
	if ( ! $user_id ) {
		$user_id = get_current_user_id();
	}

	// Initialize our output and counters.
	if ( $offset > 0 ) {
		$ranks = '';
	} else {
		$ranks = '<div class="badgeos-arrange-buttons"><button class="list buttons ' . ( $earned_ranks_shortcode_default_view === 'list' ? 'selected' : '' ) . '"><i class="fa fa-bars"></i> ' . __( 'List', 'badgeos' ) . '</button><button class="grid buttons ' . ( $earned_ranks_shortcode_default_view === 'grid' ? 'selected' : '' ) . '"><i class="fa fa-th-large"></i> ' . __( 'Grid', 'badgeos' ) . '</button></div><ul class="ls_grid_container ' . $earned_ranks_shortcode_default_view . '">';
	}

	$last_earned_id = 0;
	$settings       = ( $exists = badgeos_utilities::get_option( 'badgeos_settings' ) ) ? $exists : array();
	$last_rank      = $wpdb->get_results( $wpdb->prepare( 'select * from ' . $wpdb->prefix . 'badgeos_ranks where rank_type!=%s and user_id=%d order by actual_date_earned desc limit 1', trim( $settings['ranks_step_post_type'] ), $user_id ) );
	if ( count( $last_rank ) > 0 ) {
		$last_earned_id = $last_rank[0]->rank_id;
	}

	$ranks_count = 0;

	// If we're polling all sites, grab an array of site IDs.
	$sites = array( $blog_id );

	// Loop through each site (default is current site only).
	$query_count = 0;
	foreach ( $sites as $site_blog_id ) {

		// Query Achievements.
		$args = array(
			'post_type'      => $type,
			'orderby'        => $orderby,
			'order'          => $order,
			'posts_per_page' => $limit,
			'offset'         => $offset,
			'post_status'    => 'publish',
		);

		// Search.
		if ( $search ) {
			$args['s'] = $search;
		}
		// Loop ranks.
		$rank_posts = new WP_Query( $args );

		$query_count += $rank_posts->found_posts;
		while ( $rank_posts->have_posts() ) :
			$rank_posts->the_post();

			$user_ranks  = badgeos_user_has_a_rank( get_the_ID(), $user_id );
			$earned_this = 'badgeos_not_earned_rank badgeos_not_earned_rank_' . get_the_ID();
			if ( count( $user_ranks ) > 0 ) {
				$earned_this = 'badgeos_already_earned_rank badgeos_already_earned_rank_' . get_the_ID();
			}
			$output = '<li><div id="badgeos-list-item-' . get_the_ID() . '" class="badgeos-list-item badgeos-list-item-' . get_the_ID() . ' ' . $earned_this . '">';

			// Achievement Image.
			if ( $show_thumb === 'true' ) {
				$output .= '<div class="badgeos-item-image">';
				$output .= '<a href="' . get_permalink( get_the_ID() ) . '">' . badgeos_get_rank_image( get_the_ID(), $image_width, $image_height ) . '</a>';
				$output .= '</div><!-- .badgeos-item-image -->';
			}

			// Achievement Content.
			$output .= '<div class="badgeos-item-detail">';

			$check_mark = '';
			if ( $show_title === 'true' ) {
				// Achievement Title
				if ( $last_earned_id == get_the_ID() ) {
					$check_mark = '<span class="badgeos-last-earned-checkmark"> &#10004</span>';
				}
				$output .= '<h2 class="badgeos-item-title"><a href="' . get_permalink( get_the_ID() ) . '">' . get_the_title() . $check_mark . '</a></h2>';
			}

			// Achievement Short Description.
			if ( $show_description === 'true' ) {
				$post = badgeos_utilities::badgeos_get_post( get_the_ID() );
				if ( $post ) {
					$output .= '<div class="badgeos-item-excerpt">';
					$excerpt = get_the_excerpt();
					$excerpt = ! empty( $excerpt ) ? $excerpt : get_the_content();
					$output .= wpautop( apply_filters( 'get_the_excerpt', $excerpt ) );
					$output .= '</div><!-- .badgeos-item-excerpt -->';
				}
			}

			$output .= '</div><!-- .badgeos-item-description -->';
			$output .= apply_filters( 'badgeos_after_ranks_list_item', '', $rank_posts );
			$output .= '</div></li><!-- .badgeos-ranks-list-item -->';

			$ranks .= $output;
			$ranks_count++;
		endwhile;

		$ranks .= '</ul>';

		// Display a message for no results.
		if ( intval( $query_count ) === 0 ) {
			$current = current( $type );
			// If we have exactly one achievement type, get its plural name, otherwise use "ranks".
			$post_type_plural = ( 1 === count( $type ) && ! empty( $current ) ) ? get_post_type_object( $current )->labels->name : __( 'achievements', 'badgeos' );

			// Setup our completion message.
			$ranks .= '<div class="badgeos-no-results">';
			$ranks .= '<p>' . sprintf( __( 'No completed %s to display at this time.', 'badgeos' ), strtolower( $post_type_plural ) ) . '</p>';
			$ranks .= '</div><!-- .badgeos-no-results -->';
		}

		if ( $blog_id !== $site_blog_id ) {
			// Come back to current blog.
			restore_current_blog();
		}
	}

	// Send back our successful response
	wp_send_json_success(
		array(
			'message'     => $ranks,
			'offset'      => $offset + $limit,
			'query_count' => $query_count,
			'badge_count' => $ranks_count,
		)
	);

}

/**
 * AJAX Helper for returning earned ranks.
 *
 * @since 1.0.0
 * @return void
 */
function badgeos_ajax_get_earned_ranks() {

	if( ! check_ajax_referer( 'bos-security', 'nonce' ) ) {
		return;
	}

	// Check for nonce security.
	if ( isset( $_REQUEST['nonce'] ) && ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['nonce'] ) ), 'bos-security' ) ) {
		wp_send_json_error( 'Request blocked' );
	}

	global $blog_id, $wpdb;

	$badgeos_settings                    = ( $exists = badgeos_utilities::get_option( 'badgeos_settings' ) ) ? $exists : array();
	$earned_ranks_shortcode_default_view = ( ! empty( $badgeos_settings['earned_ranks_shortcode_default_view'] ) ) ? $badgeos_settings['earned_ranks_shortcode_default_view'] : 'list';

	// Setup our AJAX query vars
	$type             = isset( $_REQUEST['rank_type'] ) ? sanitize_text_field( $_REQUEST['rank_type'] ) : false;
	$limit            = isset( $_REQUEST['limit'] ) ? sanitize_text_field( $_REQUEST['limit'] ) : false;
	$offset           = isset( $_REQUEST['offset'] ) ? sanitize_text_field( $_REQUEST['offset'] ) : false;
	$count            = isset( $_REQUEST['count'] ) ? sanitize_text_field( $_REQUEST['count'] ) : false;
	$search           = isset( $_REQUEST['search'] ) ? sanitize_text_field( $_REQUEST['search'] ) : false;
	$user_id          = isset( $_REQUEST['user_id'] ) ? sanitize_text_field( $_REQUEST['user_id'] ) : get_current_user_id();
	$orderby          = isset( $_REQUEST['orderby'] ) ? sanitize_text_field( $_REQUEST['orderby'] ) : 'rank_id';
	$order            = isset( $_REQUEST['order'] ) ? sanitize_text_field( $_REQUEST['order'] ) : 'ASC';
	$show_title       = isset( $_REQUEST['show_title'] ) ? sanitize_text_field( $_REQUEST['show_title'] ) : 'true';
	$show_thumb       = isset( $_REQUEST['show_thumb'] ) ? sanitize_text_field( $_REQUEST['show_thumb'] ) : 'true';
	$show_description = isset( $_REQUEST['show_description'] ) ? sanitize_text_field( $_REQUEST['show_description'] ) : 'true';
	$image_width      = isset( $_REQUEST['image_width'] ) ? sanitize_text_field( $_REQUEST['image_width'] ) : '';
	$image_height     = isset( $_REQUEST['image_height'] ) ? sanitize_text_field( $_REQUEST['image_height'] ) : '';

	// Convert $type to properly support multiple rank types.
	$earned_ranks_shortcode_default_view = ! empty( $_REQUEST['default_view'] ) ? sanitize_text_field( $_REQUEST['default_view'] ) : $earned_ranks_shortcode_default_view;

	if ( 'all' === $type ) {
		$type = badgeos_get_rank_types_slugs();
		// Drop steps from our list of "all" achievements.
		$step_key = array_search( trim( $badgeos_settings['ranks_step_post_type'] ), $type );
		if ( $step_key ) {
			unset( $type[ $step_key ] );
		}
	} else {
		$type = explode( ',', $type );
	}

	// Get the current user if one wasn't specified.
	if ( ! $user_id ) {
		$user_id = get_current_user_id();
	}

	// Initialize our output and counters.
	if ( $offset > 0 ) {
		$ranks = '';
	} else {
		$ranks = '<div class="badgeos-arrange-buttons"><button class="list buttons ' . ( $earned_ranks_shortcode_default_view === 'list' ? 'selected' : '' ) . '"><i class="fa fa-bars"></i> ' . __( 'List', 'badgeos' ) . '</button><button class="grid buttons ' . ( $earned_ranks_shortcode_default_view === 'grid' ? 'selected' : '' ) . '"><i class="fa fa-th-large"></i> ' . __( 'Grid', 'badgeos' ) . '</button></div><ul class="ls_grid_container ' . $earned_ranks_shortcode_default_view . '">';
	}

	$ranks_count = 0;

	// If we're polling all sites, grab an array of site IDs.
	$sites = array( $blog_id );

	// Loop through each site (default is current site only).
	$query_count = 0;
	$table_name  = $wpdb->prefix . 'badgeos_ranks';
	foreach ( $sites as $site_blog_id ) {

		$qry       = "SELECT * FROM {$table_name} WHERE user_id={$user_id}";
		$total_qry = "SELECT count(ID) as total FROM {$table_name} WHERE user_id={$user_id}";

		if ( is_array( $type ) && count( $type ) > 0 ) {
			$types      = implode( "', '", $type );
			$qry       .= " and rank_type in ('{$types}')  ";
			$total_qry .= " and rank_type in ('{$types}') ";
		}

		if ( $search ) {
			$qry       .= " and rank_title like '%{$search}%' ";
			$total_qry .= " and rank_title like '%{$search}%' ";
		}

		$query_count += intval( $wpdb->get_var( $wpdb->prepare( "\'%s\'", $total_qry ) ) );
		if ( ! empty( $orderby ) ) {
			if ( ! empty( $order ) ) {
				$qry .= " ORDER BY {$orderby} {$order}";
			} else {
				$qry .= " ORDER BY {$orderby} ASC";
			}
		}

		$qry       .= " limit {$offset}, {$limit}";
		$user_ranks = $wpdb->get_results( $qry );

		// Loop ranks
		foreach ( $user_ranks as $rank ) {

			$output = '<li><div id="badgeos-list-item-' . $rank->rank_id . '" class="badgeos-list-item badgeos-earned-rank-item badgeos-earned-rank-item-' . $rank->rank_id . '">';

			// Achievement Image
			if ( $show_thumb === 'true' ) {
				$output .= '<div class="badgeos-item-image">';
				$output .= '<a href="' . get_permalink( $rank->rank_id ) . '">' . badgeos_get_rank_image( $rank->rank_id, $image_width, $image_height ) . '</a>';
				$output .= '</div><!-- .badgeos-item-image -->';
			}

			// Achievement Content
			$output .= '<div class="badgeos-item-detail">';

			if ( $show_title === 'true' ) {
				$title = get_the_title( $rank->rank_id );
				$title = $rank->rank_title;
				// Achievement Title
				$output .= '<h2 class="badgeos-item-title"><a href="' . get_permalink( $rank->rank_id ) . '">' . $title . '</a></h2>';
			}

			// Achievement Short Description
			if ( $show_description === 'true' ) {
				$post = get_post( $rank->rank_id );
				if ( $post ) {
					$output .= '<div class="badgeos-item-excerpt">';
					$excerpt = ! empty( $post->post_excerpt ) ? $post->post_excerpt : $post->post_content;
					$output .= wpautop( apply_filters( 'get_the_excerpt', $excerpt ) );
					$output .= '</div><!-- .badgeos-item-excerpt -->';
				}
			}

			$output .= apply_filters( 'badgeos_after_earned_ranks', '', $rank );
			$output .= '</div></li><!-- .badgeos-list-item -->';

			$ranks .= $output;
			$ranks_count++;
		}

		$ranks .= '</ul>';

		// Display a message for no results
		if ( intval( $query_count ) === 0 ) {
			$current = current( $type );
			// If we have exactly one achievement type, get its plural name, otherwise use "ranks"
			$post_type_plural = ( 1 === count( $type ) && ! empty( $current ) ) ? get_post_type_object( $current )->labels->name : __( 'achievements', 'badgeos' );

			// Setup our completion message
			$ranks .= '<div class="badgeos-no-results">';
			$ranks .= '<p>' . sprintf( __( 'No completed %s to display at this time.', 'badgeos' ), strtolower( $post_type_plural ) ) . '</p>';
			$ranks .= '</div><!-- .badgeos-no-results -->';
		}

		if ( $blog_id !== $site_blog_id ) {
			// Come back to current blog
			restore_current_blog();
		}
	}

	// Send back our successful response
	wp_send_json_success(
		array(
			'message'     => $ranks,
			'offset'      => $offset + $limit,
			'query_count' => $query_count,
			'badge_count' => $ranks_count,
		)
	);

}

/**
 * AJAX Helper for returning earned achievements
 *
 * @since 1.0.0
 * @return void
 */
function badgeos_ajax_get_earned_achievements() {

	if( ! check_ajax_referer( 'bos-security', 'nonce' ) ) {
		return;
	}

	// Check for nonce security
	if ( isset( $_REQUEST['nonce'] ) && ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['nonce'] ) ), 'bos-security' ) ) {
		wp_send_json_error( 'Request blocked' );
	}

	global $blog_id, $wpdb;

	// Convert $type to properly support multiple achievement types
	$badgeos_settings                           = ( $exists = badgeos_utilities::get_option( 'badgeos_settings' ) ) ? $exists : array();
	$earned_achievements_shortcode_default_view = ( ! empty( $badgeos_settings['earned_achievements_shortcode_default_view'] ) ) ? $badgeos_settings['earned_achievements_shortcode_default_view'] : 'list';

	// Setup our AJAX query vars
	$type    = isset( $_REQUEST['type'] ) ? sanitize_text_field( $_REQUEST['type'] ) : false;
	$limit   = isset( $_REQUEST['limit'] ) ? sanitize_text_field( $_REQUEST['limit'] ) : false;
	$offset  = isset( $_REQUEST['offset'] ) ? sanitize_text_field( $_REQUEST['offset'] ) : false;
	$count   = isset( $_REQUEST['count'] ) ? sanitize_text_field( $_REQUEST['count'] ) : false;
	$search  = isset( $_REQUEST['search'] ) ? sanitize_text_field( $_REQUEST['search'] ) : false;
	$user_id = isset( $_REQUEST['user_id'] ) ? sanitize_text_field( $_REQUEST['user_id'] ) : get_current_user_id();
	$orderby = isset( $_REQUEST['orderby'] ) ? sanitize_text_field( $_REQUEST['orderby'] ) : 'ID';
	$order   = isset( $_REQUEST['order'] ) ? sanitize_text_field( $_REQUEST['order'] ) : 'ASC';
	$wpms    = isset( $_REQUEST['wpms'] ) ? sanitize_text_field( $_REQUEST['wpms'] ) : false;
	$include = isset( $_REQUEST['include'] ) ? sanitize_text_field( $_REQUEST['include'] ) : array();
	$exclude = isset( $_REQUEST['exclude'] ) ? sanitize_text_field( $_REQUEST['exclude'] ) : array();

	$show_title                                 = isset( $_REQUEST['show_title'] ) ? sanitize_text_field( $_REQUEST['show_title'] ) : 'true';
	$show_thumb                                 = isset( $_REQUEST['show_thumb'] ) ? sanitize_text_field( $_REQUEST['show_thumb'] ) : 'true';
	$show_description                           = isset( $_REQUEST['show_description'] ) ? sanitize_text_field( $_REQUEST['show_description'] ) : 'true';
	$image_width                                = isset( $_REQUEST['image_width'] ) ? sanitize_text_field( $_REQUEST['image_width'] ) : '';
	$image_height                               = isset( $_REQUEST['image_height'] ) ? sanitize_text_field( $_REQUEST['image_height'] ) : '';
	$earned_achievements_shortcode_default_view = ! empty( $_REQUEST['default_view'] ) ? sanitize_text_field( $_REQUEST['default_view'] ) : $earned_achievements_shortcode_default_view;

	if ( 'all' === $type ) {
		$type = badgeos_get_achievement_types_slugs();
		// Drop steps from our list of "all" achievements
		$step_key = array_search( trim( $badgeos_settings['achievement_step_post_type'] ), $type );
		if ( $step_key ) {
			unset( $type[ $step_key ] );
		}
	} else {
		$type = explode( ',', $type );
	}

	// Get the current user if one wasn't specified
	// if( ! $user_id )
		// $user_id = get_current_user_id();

	// Initialize our output and counters
	if ( $offset > 0 ) {
		$achievements = '';
	} else {
		$achievements = '<div class="badgeos-arrange-buttons"><button class="list buttons ' . ( $earned_achievements_shortcode_default_view === 'list' ? 'selected' : '' ) . '"><i class="fa fa-bars"></i> ' . __( 'List', 'badgeos' ) . '</button><button class="grid buttons ' . ( $earned_achievements_shortcode_default_view === 'grid' ? 'selected' : '' ) . '"><i class="fa fa-th-large"></i> ' . __( 'Grid', 'badgeos' ) . '</button></div><ul class="ls_grid_container ' . $earned_achievements_shortcode_default_view . '">';
	}

	$achievement_count = 0;

	// If we're polling all sites, grab an array of site IDs
	if ( $wpms && $wpms !== 'false' ) {
		$sites = badgeos_get_network_site_ids();
	} else {
		$sites = array( $blog_id );
	}

	// Loop through each site (default is current site only)
	$table_name  = $wpdb->prefix . 'badgeos_achievements';
	$query_count = 0;
	foreach ( $sites as $site_blog_id ) {

		$qry       = "SELECT * FROM {$table_name} WHERE site_id={$site_blog_id}";
		$total_qry = "SELECT count(ID) as total FROM {$table_name} WHERE site_id={$site_blog_id}";

		if ( is_array( $type ) && count( $type ) > 0 ) {
			$types      = implode( "', '", $type );
			$qry       .= " and post_type in ('{$types}') ";
			$total_qry .= " and post_type in ('{$types}') ";
		}

		$qry       .= " and user_id = {$user_id} ";
		$total_qry .= " and user_id = {$user_id} ";

		if ( $search ) {
			$qry       .= " and achievement_title like '%{$search}%' ";
			$total_qry .= " and achievement_title like '%{$search}%' ";
		}

		// Build $include array
		if ( ! empty( $include ) ) {
			$qry       .= " and ID in ({$include}) ";
			$total_qry .= " and ID in ({$include}) ";
		}

		// Build $exclude array
		if ( ! empty( $exclude ) ) {
			$qry       .= " and ID not in ({$exclude}) ";
			$total_qry .= " and ID not in ({$exclude}) ";
		}
		$query_count += intval( $wpdb->get_var( $total_qry ) );
		if ( ! empty( $orderby ) ) {
			if ( ! empty( $order ) ) {
				$qry .= " ORDER BY {$orderby} {$order}";
			} else {
				$qry .= " ORDER BY {$orderby} ASC";
			}
		}

		$qry .= " limit {$offset}, {$limit}";

		$user_achievements = $wpdb->get_results( $qry );

		// Loop Achievements
		foreach ( $user_achievements as $achievement ) {

			$output = '<li><div id="badgeos-earned-list-item-' . $achievement->ID . '" class="badgeos-list-item badgeos-earned-list-item badgeos-earned-list-item-' . $achievement->ID . ' badgeos-earned-list-item-' . $achievement->ID . '-' . $user_id . '">';

			$open_badge_enable_baking = badgeos_get_option_open_badge_enable_baking( $achievement->ID );
			$permalink                = get_permalink( $achievement->ID );
			if ( $open_badge_enable_baking ) {
				$badgeos_evidence_page_id = badgeos_utilities::get_option( 'badgeos_evidence_url' );
				$badgeos_evidence_url     = get_permalink( $badgeos_evidence_page_id );
				$badgeos_evidence_url     = add_query_arg( 'bg', $achievement->ID, $badgeos_evidence_url );
				$badgeos_evidence_url     = add_query_arg( 'eid', $achievement->entry_id, $badgeos_evidence_url );
				$badgeos_evidence_url     = add_query_arg( 'uid', $achievement->user_id, $badgeos_evidence_url );
				$permalink                = $badgeos_evidence_url;
			}

			// Achievement Image
			if ( $show_thumb === 'true' ) {
				$output .= '<div class="badgeos-item-image">';

				$image_size = '';
				if ( ! empty( $image_width ) || ! empty( $image_height ) ) {
					$image_size = array( $image_width, $image_height );
				}

				$output .= '<a href="' . $permalink . '">' . badgeos_get_achievement_post_thumbnail( $achievement->ID, $image_size ) . '</a>';
				$output .= '</div><!-- .badgeos-item-image -->';
			}

			// Achievement Content
			$output .= '<div class="badgeos-item-detail">';

			// Achievement Title
			if ( $show_title === 'true' ) {
				$title = get_the_title( $achievement->ID );
				if ( empty( $title ) ) {
					$title = $achievement->achievement_title;
				}

				$output .= '<h2 class="badgeos-item-title"><a href="' . $permalink . '">' . $title . '</a></h2>';
			}

			// Achievement Short Description
			if ( $show_description === 'true' ) {
				$post = get_post( $achievement->ID );
				if ( $post ) {
					$output .= '<div class="badgeos-item-excerpt">';
					$excerpt = ! empty( $post->post_excerpt ) ? $post->post_excerpt : $post->post_content;
					$output .= wpautop( apply_filters( 'get_the_excerpt', $excerpt ) );
					$output .= '</div>';
				}
			}

			$output .= apply_filters( 'badgeos_after_earned_achievement', '', $achievement );
			$output .= '</div>';
			$output .= '</div></li>';

			$achievements .= $output;
			$achievement_count++;
		}

		if ( $offset === 0 ) {
			$achievements .= '</ul>';
		}

		// Display a message for no results
		if ( intval( $query_count ) === 0 ) {
			$current = current( $type );
			// If we have exactly one achievement type, get its plural name, otherwise use "achievements"
			$post_type_plural = ( 1 === count( $type ) && ! empty( $current ) ) ? get_post_type_object( $current )->labels->name : __( 'achievements', 'badgeos' );

			// Setup our completion message
			$achievements .= '<div class="badgeos-no-results">';
			$achievements .= '<p>' . sprintf( __( 'No %s to display at this time.', 'badgeos' ), strtolower( $post_type_plural ) ) . '</p>';
			$achievements .= '</div><!-- .badgeos-no-results -->';
		}

		if ( $blog_id !== $site_blog_id ) {
			// Come back to current blog
			restore_current_blog();
		}
	}

	// Send back our successful response
	wp_send_json_success(
		array(
			'message'     => $achievements,
			'offset'      => $offset + $limit,
			'query_count' => $query_count,
			'badge_count' => $achievement_count,

		)
	);
}

/**
 * AJAX Helper for returning achievements
 *
 * @since 1.0.0
 * @return void
 */
function badgeos_ajax_get_achievements() {

	if( ! check_ajax_referer( 'bos-security', 'nonce' ) ) {
		return;
	}

	// Check for nonce security
	if ( isset( $_REQUEST['nonce'] ) && ! wp_verify_nonce( $_REQUEST['nonce'] , 'bos-security' ) ) {
		wp_send_json_error( 'Request blocked' );
	}

	global $blog_id;

	// Convert $type to properly support multiple achievement types
	$badgeos_settings              = ( $exists = badgeos_utilities::get_option( 'badgeos_settings' ) ) ? $exists : array();
	$achievement_list_default_view = ( ! empty( $badgeos_settings['achievement_list_shortcode_default_view'] ) ) ? $badgeos_settings['achievement_list_shortcode_default_view'] : 'list';

	$earned_ids = array();
	// Setup our AJAX query vars
	$type             = isset( $_REQUEST['type'] ) ? sanitize_text_field( $_REQUEST['type'] ) : 'all';
	$limit            = isset( $_REQUEST['limit'] ) ? sanitize_text_field( $_REQUEST['limit'] ) : false;
	$offset           = isset( $_REQUEST['offset'] ) ? sanitize_text_field( $_REQUEST['offset'] ) : false;
	$count            = isset( $_REQUEST['count'] ) ? sanitize_text_field( $_REQUEST['count'] ) : false;
	$filter           = isset( $_REQUEST['filter'] ) ? sanitize_text_field( $_REQUEST['filter'] ) : false;
	$search           = isset( $_REQUEST['search'] ) ? sanitize_text_field( $_REQUEST['search'] ) : false;
	$user_id          = isset( $_REQUEST['user_id'] ) ? sanitize_text_field( $_REQUEST['user_id'] ) : false;
	$orderby          = isset( $_REQUEST['orderby'] ) ? sanitize_text_field( $_REQUEST['orderby'] ) : false;
	$order            = isset( $_REQUEST['order'] ) ? sanitize_text_field( $_REQUEST['order'] ) : false;
	$wpms             = isset( $_REQUEST['wpms'] ) ? sanitize_text_field( $_REQUEST['wpms'] ) : false;
	$include          = isset( $_REQUEST['include'] ) ? sanitize_text_field( $_REQUEST['include'] ) : array();
	$exclude          = isset( $_REQUEST['exclude'] ) ? sanitize_text_field( $_REQUEST['exclude'] ) : array();
	$meta_key         = isset( $_REQUEST['meta_key'] ) ? sanitize_text_field( $_REQUEST['meta_key'] ) : '';
	$meta_value       = isset( $_REQUEST['meta_value'] ) ? sanitize_text_field( $_REQUEST['meta_value'] ) : '';
	$show_title       = isset( $_REQUEST['show_title'] ) ? sanitize_text_field( $_REQUEST['show_title'] ) : 'true';
	$show_thumb       = isset( $_REQUEST['show_thumb'] ) ? sanitize_text_field( $_REQUEST['show_thumb'] ) : 'true';
	$show_description = isset( $_REQUEST['show_description'] ) ? sanitize_text_field( $_REQUEST['show_description'] ) : 'true';
	$show_steps       = isset( $_REQUEST['show_steps'] ) ? sanitize_text_field( $_REQUEST['show_steps'] ) : 'true';

	$image_width  = isset( $_REQUEST['image_width'] ) ? sanitize_text_field( $_REQUEST['image_width'] ) : '';
	$image_height = isset( $_REQUEST['image_height'] ) ? sanitize_text_field( $_REQUEST['image_height'] ) : '';

	$achievement_list_default_view = ! empty( $_REQUEST['default_view'] ) ? sanitize_text_field( $_REQUEST['default_view'] ) : $achievement_list_default_view;

	if ( 'all' === $type ) {
		$type = badgeos_get_achievement_types_slugs();
		// Drop steps from our list of "all" achievements
		$step_key = array_search( trim( $badgeos_settings['achievement_step_post_type'] ), $type );
		if ( $step_key ) {
			unset( $type[ $step_key ] );
		}
	} else {
		$type = explode( ',', $type );
	}

	// Get the current user if one wasn't specified
	if ( ! $user_id ) {
		$user_id = get_current_user_id();
	}

	// Build $include array
	if ( ! is_array( $include ) && ! empty( $include ) ) {
		$include = explode( ',', $include );
	}

	// Build $exclude array
	if ( ! is_array( $exclude ) && ! empty( $exclude ) ) {
		$exclude = explode( ',', $exclude );
	}

	// Initialize our output and counters
	if ( $offset > 0 ) {
		$achievements = '';
	} else {
		$achievements = '<div class="badgeos-arrange-buttons"><button class="list buttons ' . ( $achievement_list_default_view === 'list' ? 'selected' : '' ) . '"><i class="fa fa-bars"></i> ' . __( 'List', 'badgeos' ) . '</button><button class="grid buttons ' . ( $achievement_list_default_view === 'grid' ? 'selected' : '' ) . '""><i class="fa fa-th-large"></i> ' . __( 'Grid', 'badgeos' ) . '</button></div><ul class="ls_grid_container ' . $achievement_list_default_view . '">';
	}

	$achievement_count = 0;
	$query_count       = 0;

	// Grab our hidden badges (used to filter the query)
	$hidden = badgeos_get_hidden_achievement_ids( $type );

	// If we're polling all sites, grab an array of site IDs
	if ( $wpms && $wpms !== 'false' ) {
		$sites = badgeos_get_network_site_ids();
	}
	// Otherwise, use only the current site
	else {
		$sites = array( $blog_id );
	}

	// Loop through each site (default is current site only)
	foreach ( $sites as $site_blog_id ) {

		// Grab our earned badges (used to filter the query)
		$earned_ids = badgeos_get_user_earned_achievement_ids( $user_id, $type );

		// Query Achievements
		$args = array(
			'post_type'      => $type,
			'orderby'        => $orderby,
			'order'          => $order,
			'posts_per_page' => $limit,
			'offset'         => $offset,
			'post_status'    => 'publish',
			'post__not_in'   => $hidden,
			'post__in'       => $hidden,
		);

		if ( ! is_array( $args['post__not_in'] ) ) {
			$args['post__not_in'] = array();
		}

		// Filter - query completed or non completed achievements
		if ( $filter === 'completed' ) {
			$args['post__in'] = array_merge( array( 0 ), $earned_ids );
		} elseif ( $filter === 'not-completed' ) {
			$args['post__not_in'] = array_merge( $hidden, $earned_ids );
		} elseif ( $filter === 'last-earned' ) {
			$total                = $earned_ids;
			$last_earned          = array_pop( $total );
			$args['post__not_in'] = $total;
			$args['post__in']     = array( $last_earned );
		}

		if ( '' !== $meta_key && '' !== $meta_value ) {
			$args['meta_key']   = $meta_key;
			$args['meta_value'] = $meta_value;
		}

		// Include certain achievements
		if ( ! empty( $include ) ) {
			$args['post__not_in'] = array_diff( $args['post__not_in'], $include );
			if ( ! is_array( $args['post__in'] ) ) {
				$args['post__in'] = array();
			}

			$args['post__in'] = array_merge( array( 0 ), array_diff( $include, $args['post__in'] ) );
		}

		// Exclude certain achievements
		if ( ! empty( $exclude ) ) {
			$args['post__not_in'] = array_merge( $args['post__not_in'], $exclude );
		}

		// Search
		if ( $search ) {
			$args['s'] = $search;
		}

		// Loop Achievements
		$achievement_posts = new WP_Query( $args );
		$query_count      += $achievement_posts->found_posts;
		while ( $achievement_posts->have_posts() ) :
			$achievement_posts->the_post();
			$achievements .= '<li id="badgeos-achivements-list-item-' . get_the_ID() . '" class="badgeos-achivements-list-item badgeos-achivements-list-item-' . get_the_ID() . '">' . badgeos_render_achievement( get_the_ID(), $show_title, $show_thumb, $show_description, $show_steps, $image_width, $image_height ) . '</li>';
			$achievement_count++;
		endwhile;

		// Sanity helper: if we're filtering for complete and we have no
		// earned achievements, $achievement_posts should definitely be false
		/*
		if ( 'completed' === $filter && empty( $earned_ids ) )
			$achievements = '';*/

		// Display a message for no results
		if ( $query_count === 0 ) {
			$current = current( $type );
			// If we have exactly one achievement type, get its plural name, otherwise use "achievements"
			$post_type_plural = ( 1 === count( $type ) && ! empty( $current ) ) ? get_post_type_object( $current )->labels->name : __( 'achievements', 'badgeos' );

			// Setup our completion message
			$achievements .= '<div class="badgeos-no-results">';
			if ( 'completed' === $filter ) {
				$achievements .= '<p>' . sprintf( __( 'No completed %s to display at this time.', 'badgeos' ), strtolower( $post_type_plural ) ) . '</p>';
			} else {
				$achievements .= '<p>' . sprintf( __( 'No %s to display at this time.', 'badgeos' ), strtolower( $post_type_plural ) ) . '</p>';
			}
			$achievements .= '</div><!-- .badgeos-no-results -->';
		}

		if ( $blog_id !== $site_blog_id ) {
			// Come back to current blog
			restore_current_blog();
		}
	}

	if ( $offset === 0 ) {
		$achievements .= '</ul>';
	}

	// Send back our successful response
	wp_send_json_success(
		array(
			'message'     => $achievements,
			'offset'      => $offset + $limit,
			'query_count' => $query_count,
			'badge_count' => $achievement_count,
			'type'        => $earned_ids,
		)
	);
}

/**
 * AJAX Helper for selecting users in Shortcode Embedder
 *
 * @since 1.4.0
 */
function badgeos_ajax_badgeos_get_users_list() {

	// If no query was sent, die here
	if ( ! isset( $_REQUEST['q'] ) ) {
		die();
	}

	global $wpdb;
	$str_query = sanitize_text_field( $_REQUEST['q'] );

	// Pull back the search string
	$search = esc_sql( $wpdb->esc_like( $str_query ) );

	$sql = "SELECT ID as id, user_login as label, ID as value FROM {$wpdb->users}";

	// Build our query
	if ( ! empty( $search ) ) {
		$sql .= " WHERE user_login LIKE '%{$str_query}%'";
	}

	// Fetch our results (store as associative array)
	$results = $wpdb->get_results( $sql, 'ARRAY_A' );

	// Return our results
	wp_send_json( $results );
}

/**
 * AJAX Helper for selecting users in Shortcode Embedder
 *
 * @since 1.4.0
 */
function badgeos_ajax_get_users() {

	// If no query was sent, die here
	if ( ! isset( $_REQUEST['q'] ) ) {
		die();
	}

	global $wpdb;
	$str_query = sanitize_text_field( $_REQUEST['q'] );

	// Pull back the search string
	$search = esc_sql( $wpdb->esc_like( $str_query ) );

	$sql = "SELECT ID as id, user_login as text FROM {$wpdb->users}";

	// Build our query
	if ( ! empty( $search ) ) {
		$sql .= " WHERE user_login LIKE '%{$str_query}%'";
	}

	// Fetch our results (store as associative array)
	$results = $wpdb->get_results( $sql, 'ARRAY_A' );

	// Return our results
	wp_send_json( array( 'results' => $results ) );
}

/**
 * AJAX Helper for selecting posts in Shortcode Embedder
 *
 * @since 1.4.0
 */
function badgeos_ajax_get_achievements_select2() {
	global $wpdb;

	$badgeos_settings = ( $exists = badgeos_utilities::get_option( 'badgeos_settings' ) ) ? $exists : array();
	// Pull back the search string
	$search            = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( sanitize_text_field( $_REQUEST['q'] ) ) : '';
	$achievement_types = isset( $_REQUEST['post_type'] ) && 'all' !== $_REQUEST['post_type']
		? array( esc_sql( sanitize_text_field( $_REQUEST['post_type'] ) ) )
		: array_diff( badgeos_get_achievement_types_slugs(), array( trim( $badgeos_settings['achievement_step_post_type'] ) ) );
	$post_type         = sprintf( 'AND p.post_type IN(\'%s\')', implode( "','", $achievement_types ) );

	$results = $wpdb->get_results(
		$wpdb->prepare(
			"
		SELECT p.ID, p.post_title
		FROM   $wpdb->posts AS p 
		JOIN $wpdb->postmeta AS pm
		ON p.ID = pm.post_id
		WHERE  p.post_title LIKE %s
		       %s
		       AND p.post_status = 'publish'
		       AND pm.meta_key LIKE %s
		       AND pm.meta_value LIKE %s
		",
			"{$post_type}",
			"%%{$search}%%",
			'%%_badgeos_hidden%%',
			'%%show%%'
		)
	);

	// Return our results
	wp_send_json_success( $results );
}

/**
 * AJAX Helper for selecting achievement types in Shortcode Embedder
 *
 * @since 1.4.0
 */
function badgeos_ajax_get_achievement_types() {

	// If no query was sent, die here
	if ( ! isset( $_REQUEST['q'] ) ) {
		die();
	}

	if( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ) {
		wp_send_json_error( 'Request could not be completed' );	
	}
	
	$str_query         = sanitize_text_field( $_REQUEST['q'] );
	$badgeos_settings  = ( $exists = badgeos_utilities::get_option( 'badgeos_settings' ) ) ? $exists : array();
	$achievement_types = array_diff( badgeos_get_achievement_types_slugs(), array( trim( $badgeos_settings['achievement_step_post_type'] ) ) );
	$matches           = preg_grep( "/{$str_query}/", $achievement_types );
	$found             = array_map( 'get_post_type_object', $matches );

	// Include an "all" option as the first option
	array_unshift(
		$found,
		(object) array(
			'name'  => 'all',
			'label' => __(
				'All',
				'badgeos'
			),
		)
	);

	// Return our results
	wp_send_json_success( $found );
}

function delete_badgeos_log_entries() {
	$action = ( isset( $_POST['action'] ) ? sanitize_text_field( $_POST['action'] ) : '' );
	if ( $action !== 'delete_badgeos_log_entries' ) {
		exit;
	}
	if ( ! wp_next_scheduled( 'cron_delete_log_entries' ) ) {
		wp_schedule_single_event( time(), 'cron_delete_log_entries' );
	}
}
add_action( 'wp_ajax_delete_badgeos_log_entries', 'delete_badgeos_log_entries' );

function cron_delete_log_entries() {
	@set_time_limit( 3600 );

	global $wpdb;

	$badgeos_log_entry = $wpdb->get_results( "SELECT `ID` FROM $wpdb->posts WHERE post_type = 'badgeos-log-entry';" );
	if ( is_array( $badgeos_log_entry ) && ! empty( $badgeos_log_entry ) && ! is_null( $badgeos_log_entry ) ) {
		foreach ( $badgeos_log_entry as $log_entry ) {
			$wpdb->query(
				$wpdb->prepare(
					"DELETE FROM $wpdb->posts WHERE ID = %d",
					$log_entry->ID
				)
			);
			$wpdb->query(
				$wpdb->prepare(
					"DELETE FROM $wpdb->postmeta WHERE post_id = %d",
					$log_entry->ID
				)
			);
		}
	}
}
add_action( 'cron_delete_log_entries', 'cron_delete_log_entries' );

add_action( 'check_ajax_referer', 'check_ajax_referer_cb', 10, 2 );
function check_ajax_referer_cb( $action, $result ) {
    return __( 'Request could not be verified.', 'badgeos' );
}