File "utilities.php"

Full Path: /home/elegucvf/public_html/video/wp-content/plugins/atlas-core/functions/utilities.php
File size: 6.77 KB
MIME-type: text/x-php
Charset: utf-8

<?php
/**
 * Utilities
 *
 * @package Atlas Core
 */

if ( ! defined( 'ABSPATH' ) ) {
	 exit; // Exit if accessed directly
}

/**
 * -----------------------------------------------------------------------------
 * Custom image scrset
 * -----------------------------------------------------------------------------
 */
if ( ! function_exists( 'th90_custom_image_srcset' ) ) {
	function th90_custom_image_srcset( $sources, $size_array, $image_src, $image_meta, $attachment_id ) {
	    $remove = [ '15', '20' ];
	    $sources = array_diff_key( $sources, array_flip( $remove ) );
	    return $sources;
	}
}
add_filter( 'wp_calculate_image_srcset', 'th90_custom_image_srcset', 10, 5);

/**
 * -------------------------------------------------------------------------
 *  Get term array
 * -------------------------------------------------------------------------
 */
if( ! function_exists('th90_custom_search_form') ) {
	function th90_custom_search_form( $placeholder = 'Search...' ) {
		?>
		<form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>">

			<input type="search" id="<?php echo esc_attr( uniqid( 'search-form-' ) ); ?>" placeholder="<?php echo esc_attr( $placeholder ); ?>" class="search-input" value="<?php echo get_search_query(); ?>" name="s">

			<button type="submit" class="search-button">
				<?php th90_svg_icon( 'search' ); ?>
			</button>

		</form>

		<?php
	}
}
/**
 * -------------------------------------------------------------------------
 *  Get term array
 * -------------------------------------------------------------------------
 */
if( ! function_exists('th90_get_terms') ) {
	function th90_get_terms( $tax = 'category', $key = 'slug' ) {
		$terms = array();
		if( ! taxonomy_exists( $tax ) ) {
			return false;
		}
		if ( $key === 'id' ) {
			foreach ( (array) get_terms( $tax, array( 'hide_empty' => false ) ) as $term ) {
				$terms[$term->term_id] = $term->name;
			}
		} elseif ( $key === 'slug' ) {
			foreach ( (array) get_terms( $tax, array( 'hide_empty' => false ) ) as $term ) {
				$terms[$term->slug] = $term->name;
			}
		}
		return $terms;
	}
}


/* AJAX SEARCH Select2 Field */
if ( ! function_exists( 'th90_get_query_post_list' ) ) {
    function th90_get_query_post_list($post_type = 'any', $limit = -1, $search = '') {
        global $wpdb;
        $where = '';
        $data = [];

        if (-1 == $limit) {
            $limit = '';
        } elseif (0 == $limit) {
            $limit = "limit 0,1";
        } else {
            $limit = $wpdb->prepare(" limit 0,%d", esc_sql($limit));
        }

        if ('any' === $post_type) {
            $in_search_post_types = get_post_types(['exclude_from_search' => false]);
            if (empty($in_search_post_types)) {
                $where .= ' AND 1=0 ';
            } else {
                $where .= " AND {$wpdb->posts}.post_type IN ('" . join("', '",
                    array_map('esc_sql', $in_search_post_types)) . "')";
            }
        } elseif (!empty($post_type)) {
            $where .= $wpdb->prepare(" AND {$wpdb->posts}.post_type = %s", esc_sql($post_type));
        }

        if (!empty($search)) {
            $where .= $wpdb->prepare(" AND {$wpdb->posts}.post_title LIKE %s", '%' . esc_sql($search) . '%');
        }

        $query = "select post_title,ID  from $wpdb->posts where post_status = 'publish' $where $limit";
        $results = $wpdb->get_results($query);
        if (!empty($results)) {
            foreach ($results as $row) {
                $data[$row->ID] = $row->post_title;
            }
        }
        return $data;
    }
}

if ( ! function_exists( 'th90_get_query_post_term' ) ) {
    function th90_get_query_post_term($post_type = 'all', $limit = -1, $search = '') {
		$args = [
			'hide_empty' => false,
			'orderby'    => 'name',
			'order'      => 'ASC',
			'search'     => $search,
			'number'     => $limit,
		];

		if ( $post_type !== 'all' ) {
			$args['taxonomy'] = $post_type;
		}

		$post_list = wp_list_pluck( get_terms( $args ), 'name', 'slug' );

		return $post_list;
	}
}

add_action( 'wp_ajax_nopriv_th90_selectajax_elementor', 'th90_ajax_posts_filter_elementor' );
add_action( 'wp_ajax_th90_selectajax_elementor', 'th90_ajax_posts_filter_elementor' );
function th90_ajax_posts_filter_elementor() {
	$post_type   = 'post';
	$source_name = 'post_type';

	if ( ! empty( $_POST['post_type'] ) ) {
		$post_type = sanitize_text_field( $_POST['post_type'] );
	}

	if ( ! empty( $_POST['source_name'] ) ) {
		$source_name = sanitize_text_field( $_POST['source_name'] );
	}

	$search  = ! empty( $_POST['term'] ) ? sanitize_text_field( $_POST['term'] ) : '';
	$results = $post_list = [];
	switch ( $source_name ) {
		case 'taxonomy':

			$post_list = th90_get_query_post_term( $post_type, 5, $search );
			break;
		case 'user':
			if ( ! current_user_can( 'list_users' ) ) {
				$post_list = [];
				break;
			}

			$users = [];

			foreach ( get_users( [ 'search' => "*{$search}*" ] ) as $user ) {
				$user_id           = $user->ID;
				$user_name         = $user->display_name;
				$users[ $user_id ] = $user_name;
			}

			$post_list = $users;
			break;
		default:
			$post_list = th90_get_query_post_list( $post_type, 10, $search );
	}

	if ( ! empty( $post_list ) ) {
		foreach ( $post_list as $key => $item ) {
			$results[] = [ 'text' => $item, 'id' => $key ];
		}
	}

	wp_send_json( [ 'results' => $results ] );
}


add_action( 'wp_ajax_nopriv_th90_selectajax_title', 'th90_ajax_get_posts_value_titles' );
add_action( 'wp_ajax_th90_selectajax_title', 'th90_ajax_get_posts_value_titles' );
function th90_ajax_get_posts_value_titles() {

	if ( empty( $_POST['id'] ) ) {
		wp_send_json_error( [] );
	}

	if ( empty( array_filter( $_POST['id'] ) ) ) {
		wp_send_json_error( [] );
	}
	$ids         = array_map( 'intval', $_POST['id'] );
	$source_name = ! empty( $_POST['source_name'] ) ? sanitize_text_field( $_POST['source_name'] ) : '';

	switch ( $source_name ) {
		case 'taxonomy':
			$args = [
				'hide_empty' => false,
				'orderby'    => 'name',
				'order'      => 'ASC',
				'include'    => implode( ',', $ids ),
			];

			if ( $_POST['post_type'] !== 'all' ) {
				$args['taxonomy'] = sanitize_text_field( $_POST['post_type'] );
			}

			$response = wp_list_pluck( get_terms( $args ), 'name', 'slug' );
			break;
		case 'user':
			$users = [];

			foreach ( get_users( [ 'include' => $ids ] ) as $user ) {
				$user_id           = $user->ID;
				$user_name         = $user->display_name;
				$users[ $user_id ] = $user_name;
			}

			$response = $users;
			break;
		default:
			$post_info = get_posts( [
				'post_type' => sanitize_text_field( $_POST['post_type'] ),
				'include'   => implode( ',', $ids )
			] );
			$response  = wp_list_pluck( $post_info, 'post_title', 'ID' );
	}

	if ( ! empty( $response ) ) {
		wp_send_json_success( [ 'results' => $response ] );
	} else {
		wp_send_json_error( [] );
	}
}