ajax.php
2 months ago
custom-post.php
3 years ago
metabox.php
2 years ago
migration.php
2 years ago
resize.php
3 years ago
settings.php
4 years ago
shortcode.php
1 year ago
widget.php
4 years ago
ajax.php
128 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Exit if accessed directly |
| 4 | */ |
| 5 | if ( ! defined( 'ABSPATH' ) ) { |
| 6 | exit; |
| 7 | } |
| 8 | |
| 9 | class PGCU_Ajax |
| 10 | { |
| 11 | public function __construct () { |
| 12 | |
| 13 | add_action('wp_ajax_pgcu_post_type', array( $this, 'post_type_ajax_handler') ); // wp_ajax_{action} |
| 14 | |
| 15 | add_action('wp_ajax_pgcu_sortable', array( $this, 'sortable_ajax_handler') ); // wp_ajax_{action} |
| 16 | add_action('wp_ajax_nopriv_pgcu_sortable', array( $this, 'sortable_ajax_handler') ); |
| 17 | |
| 18 | } |
| 19 | |
| 20 | function post_type_ajax_handler(){ |
| 21 | if ( ! current_user_can('edit_posts') ) { |
| 22 | wp_send_json_error( ['message' => 'Unauthorized access.'] ); |
| 23 | exit; |
| 24 | } |
| 25 | ob_start(); |
| 26 | |
| 27 | $post_type = ! empty( $_POST['post_type'] ) ? sanitize_text_field( $_POST['post_type'] ) : 'post'; ?> |
| 28 | |
| 29 | <?php |
| 30 | $terms = get_object_taxonomies( (object) array( 'post_type' => $post_type, 'hide_empty' => false ) ); |
| 31 | if( $terms ) { |
| 32 | foreach( $terms as $term ) { |
| 33 | ?> |
| 34 | <option value="<?php echo esc_attr( $term ); ?>" ><?php echo esc_html( $term ); ?></option> |
| 35 | <?php } } |
| 36 | $value = ob_get_clean(); |
| 37 | |
| 38 | wp_send_json( $value ); |
| 39 | |
| 40 | die; // here we exit the script and even no wp_reset_query() required! |
| 41 | } |
| 42 | |
| 43 | function sortable_ajax_handler() { |
| 44 | if (!current_user_can('edit_posts')) { |
| 45 | wp_send_json_error(['message' => 'Unauthorized access.']); |
| 46 | exit; |
| 47 | } |
| 48 | |
| 49 | $term_id = !empty($_POST['term_id']) ? sanitize_text_field($_POST['term_id']) : 'all'; |
| 50 | $post_id = !empty($_POST['id']) ? intval($_POST['id']) : 0; |
| 51 | $query = !empty($_POST['query']) ? json_decode(stripslashes($_POST['query']), true) : []; |
| 52 | |
| 53 | $data = get_post_meta( $post_id, 'gc', true ); |
| 54 | $data = post_grid_and_carousel_ultimate::json_decoded( $data ); |
| 55 | $value = is_array( $data ) ? $data : array(); |
| 56 | extract( $value ); |
| 57 | |
| 58 | $layout = ! empty( $layout ) ? sanitize_text_field( $layout ) : 'carousel'; |
| 59 | $allowed_themes = ['theme-1', 'theme-2', 'theme-3', 'theme-3']; // Define allowed themes |
| 60 | $theme = isset( $theme ) && in_array( $theme, $allowed_themes, true ) ? sanitize_text_field( $theme ) : 'theme-1'; |
| 61 | $post_type = ! empty( $post_type ) ? sanitize_text_field( $post_type ) : 'post'; |
| 62 | $term_from = ! empty( $term_from ) ? sanitize_text_field( $term_from ) : 'category'; |
| 63 | $display_term = ! empty( $display_term ) ? sanitize_text_field( $display_term ) : 'yes'; |
| 64 | $display_title = ! empty( $display_title ) ? sanitize_text_field( $display_title ) : 'yes'; |
| 65 | $display_content = ! empty( $display_content ) ? sanitize_text_field( $display_content ) : 'yes'; |
| 66 | $content_word_limit = ! empty( $content_word_limit ) ? sanitize_text_field( $content_word_limit ) : '16'; |
| 67 | $display_read_more = ! empty( $display_read_more ) ? sanitize_text_field( $display_read_more ) : 'yes'; |
| 68 | $read_more_text = ! empty( $read_more_text ) ? sanitize_text_field( $read_more_text ) : 'Read More'; |
| 69 | $read_more_type = ! empty( $read_more_type ) ? sanitize_text_field( $read_more_type ) : 'link'; |
| 70 | $display_author = ! empty( $display_author ) ? sanitize_text_field( $display_author ) : 'yes'; |
| 71 | $display_date = ! empty( $display_date ) ? sanitize_text_field( $display_date ) : 'yes'; |
| 72 | |
| 73 | $image_resize_crop = ! empty( $image_resize_crop ) ? sanitize_text_field( $image_resize_crop ) : "yes"; |
| 74 | $image_width = ! empty( $image_width ) ? sanitize_text_field( $image_width ) : 300; |
| 75 | $image_height = ! empty( $image_height ) ? sanitize_text_field( $image_height ) : 290; |
| 76 | |
| 77 | $g_sort = ! empty( $g_sort ) ? sanitize_text_field( $g_sort ) : 'category'; |
| 78 | |
| 79 | if( 'all' != $term_id ) { |
| 80 | $query['tax_query'] = array( |
| 81 | array( |
| 82 | 'taxonomy' => $g_sort, |
| 83 | 'field' => 'term_id', |
| 84 | 'terms' => $term_id, |
| 85 | ) |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | // it is always better to use WP_Query but not here |
| 90 | query_posts( $query ); |
| 91 | |
| 92 | |
| 93 | if( have_posts() ) : |
| 94 | |
| 95 | // run the loop |
| 96 | while( have_posts() ): the_post(); |
| 97 | |
| 98 | $thumb = get_post_thumbnail_id(); |
| 99 | // crop the image if the cropping is enabled. |
| 100 | if( 'yes' === $image_resize_crop ){ |
| 101 | $pgcu_img = pgcu_image_cropping( $thumb, $image_width, $image_height, true, 100 )['url']; |
| 102 | }else{ |
| 103 | $aazz_thumb = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID()), 'large' ); |
| 104 | $pgcu_img = $aazz_thumb['0']; |
| 105 | } |
| 106 | |
| 107 | $get_terms = get_the_terms( get_the_ID(), $term_from ); |
| 108 | $post_views = get_post_meta( get_the_id(), '_pgcu_post_views_count', true ); |
| 109 | |
| 110 | $template_path = PGCU_INC_DIR . 'templates/' . sanitize_file_name( $theme ) . '.php'; |
| 111 | if (file_exists($template_path)) { |
| 112 | include $template_path; |
| 113 | } else { |
| 114 | wp_send_json_error(['message' => 'Invalid template specified.']); |
| 115 | exit; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | endwhile; |
| 120 | |
| 121 | endif; |
| 122 | |
| 123 | die; // here we exit the script and even no wp_reset_query() required! |
| 124 | } |
| 125 | |
| 126 | |
| 127 | |
| 128 | }//end class |