/home/fdhrevqn/public_html/wp-content/plugins.disabled/fox-framework/inc/query.php
<?php

if ( ! function_exists( 'foxele_query' ) ) :
/**
 * Returns new WP_Query
 *
 * @since 4.0
 */
function foxele_query( $settings ) {
    
    // write it from beginning to make sure it works
    $query_args = [

        'number' => $settings[ 'number' ],
        'orderby' => $settings[ 'orderby' ],
        'order' => $settings[ 'order' ],

        'categories' => $settings[ 'categories' ],
        'exclude_categories' => $settings[ 'exclude_categories' ],

        'tags' => $settings[ 'tags' ],
        'exclude_tags' => $settings[ 'exclude_tags' ],

        'offset' => $settings[ 'offset' ],

        'format' => $settings[ 'format' ],

        'author' => $settings[ 'author' ],

        'include' => $settings[ 'include' ],
        'exclude' => $settings[ 'exclude' ],

        'featured' => $settings[ 'featured' ],

        'custom_query' => $settings[ 'custom_query' ],

        'unique_posts' => ( $settings[ 'unique_posts' ] == 'yes' ),

        'pagination' => isset( $settings[ 'pagination' ] ) && $settings[ 'pagination' ],

        'post_type' => $settings[ 'post_type' ],

        // for elementor, we don't use sticky feature
        'sticky' => false,
    ];

    /**
     * process a bit problem with custom taxonomy
     */
    $tax_query = [];
    for ( $j = 1; $j <=2; $j++ ) {
        if ( isset( $settings[ 'tax_' . $j ] ) && $settings[ 'tax_' . $j ] && isset( $settings[ 'tax_' . $j . '_value' ] ) && $settings[ 'tax_' . $j . '_value' ] ) {

            $terms = $settings[ 'tax_' . $j . '_value' ];
            $terms = explode( ',', $terms );
            $terms = array_map( 'trim', $terms );
            $tax_query[] = [
                'taxonomy' => $settings[ 'tax_' . $j ],
                'field'    => 'name',
                'terms'    => $terms,
            ];

        }
    }

    $query_args[ 'tax_query' ] = $tax_query;

    if ( function_exists( 'fox_query' ) ) {
        $query = fox_query( $query_args );
        
    // a safe fallback
    } else {
        global $wp_query;
        $query = $wp_query;
    }
    
    return $query;

}

endif;

if ( ! function_exists( 'foxele_query_params' ) ) :
/**
 * Return query params used commonly
 * $exclude is the array of ids to exclude
 *
 *
 * @since 4.0
 */
function foxele_query_params( $exclude = array(), $override = [] ) {
    
    $params = array();
    
    $params[ 'number' ] = array(
        'title' => 'Number of posts:',
        'type' => 'text',
        'std' => '4',
        
        'section' => 'query',
        'section_title' => 'Query',
    );
    
    $params[ 'orderby' ] = array(
        'title' => 'Order by',
        'type' => 'select',
        'options' => fox_orderby_support(),
        'std' => 'date',
    );
    
    $params[ 'order' ] = array(
        'title' => 'Order?',
        'type' => 'select',
        'options' => fox_order_support(),
        'std' => 'desc',
    );
    
    $cat_options = array();
    $all_cats = get_terms( array( 'hide_empty' => false, 'taxonomy' => 'category', 'orderby' => 'name', 'order' => 'ASC' ) );
    foreach ( $all_cats as $cat ) {
        $cat_options[ 'cat_' . strval( $cat->slug ) ] = $cat->name;
    }
    
    $author_options = array( 'all' => 'All' );
    
    $all_authors = get_users( array( 'who' => 'authors', 'orderby' => 'display_name', 'order' => 'ASC' ) );
    foreach ( $all_authors as $at ) {
        $author_options [ 'author_' . strval( $at->user_nicename ) ] = $at->display_name;
    }
    
    $params[ 'categories' ] = array(
        'title' => 'Only from categories:',
        'type' => 'select2',
        'multiple' => true,
        'options' => $cat_options,
    );
    
    $params[ 'exclude_categories' ] = array(
        'title' => 'Exclude categories:',
        'type' => 'select2',
        'multiple' => true,
        'options' => $cat_options,
    );
    
    $params[ 'tags' ] = array(
        'title' => 'Only from tags:',
        'type' => 'text',
        'desc' => 'Enter tag IDs or names, separate them by commas.',
    );
    
    $params[ 'exclude_tags' ] = array(
        'title' => 'Exclude tags:',
        'type' => 'text',
        'desc' => 'Enter tag IDs or names, separate them by commas.',
    );
    
    $params[ 'author' ] = array(
        'title' => 'Only posts from author:',
        'type' => 'select',
        'std' => 'all',
        'options' => $author_options,
    );
    
    $params[ 'format' ] = array(
        'title' => 'Only post format:',
        'type' => 'select',
        'options' => [
            '' => 'All',
            'video' => 'Video',
            'audio' => 'Audio',
            'gallery' => 'Gallery',
            'link' => 'Link',
        ],
        'std' => '',
    );
    
    $params[ 'include' ] = array(
        'title' => 'Display only posts from IDs:',
        'desc' => 'Enter post IDs, separated by commas',
        'type' => 'text',
    );
    
    $params[ 'exclude' ] = array(
        'title' => 'Exclude post IDs:',
        'desc' => 'Enter post IDs, separated by commas',
        'type' => 'text',
    );
    
    // since 1.4
    $params[ 'offset' ] = array(
        'title' => 'Offset',
        'desc' => 'Number of posts to pass by',
        'type' => 'text',
    );
    
    $params[ 'featured' ] = array(
        'title' => 'Show only featured posts?',
        'type' => 'switcher',
        'std' => '',
    );
    
    $params[ 'pagination' ] = array(
        'title' => 'Pagination?',
        'type' => 'switcher',
        'std' => '',
    );
    
    /* deprecated
    $params[ 'paged_disable' ] = array(
        'title' => 'Disable on page 2, 3..',
        'type' => 'switcher',
        'std' => '',
    );
    */
    
    $params[ 'unique_posts' ] = array(
        'title' => 'Unique posts?',
        'desc' => 'If enabled, it prevents a post appears twice.',
        'type' => 'switcher',
        'std' => '',
    );
    
    $params[ 'custom_query' ] = array(
        'title' => 'Your Custom Query',
        'type' => 'code',
        'desc' => 'Please make sure you understand what you\'re writing when using this option',
    );
    
    /**
     * custom post type options
     * @since 1.4
     */
    $params[ 'post_type' ] = array(
        'title' => 'Custom Post Type',
        'type' => 'text',
        'placeholder' => 'Eg. movie',
        'desc' => 'Enter your post type slug, eg. movie. If you have few post types, use commas to separate them. Enter "any" (without quotes) to display any post type.',
    );
    
    for ( $i = 1; $i <= 2; $i++ ) {
        
        $params[ 'tax_' . $i ] = array(
            'title' => 'Taxonomy ' . $i . ' slug',
            'type' => 'text',
            'placeholder' => 'Eg. genre',
            'desc' => 'If your taxonomy is Genre, the slug is probably genre but sometimes it may have prefix. Make sure you enter correct the taxonomy slug. Otherwise it won\'t work as expected',
        );
        
        $params[ 'tax_' . $i . '_value' ] = array(
            'title' => 'Taxonomy ' . $i . ' values',
            'type' => 'text',
            'placeholder' => 'Eg. Comedy, Fiction',
            'desc' => 'Enter name values for your taxonomy. Separate them by commas.',
        );
        
    }
    
    // exclude
    // and set section 'query'
    $first = true;
    foreach ( $params as $id => $param ) {
        if ( in_array( $id, $exclude ) ) {
            unset( $params[ $id ] );
        } elseif ( $first ) {
            $params[ $id ][ 'section' ] = 'query';
            $params[ $id ][ 'section_title' ] = 'Query';
            $first = false;
        }
    }
    
    // OVERRIDE
    if ( ! empty( $override ) ) {
        foreach ( $override as $id => $arr ) {
            if ( isset( $params[ $id ] ) ) $params[ $id ] = $arr;
        }
    }
    
    return apply_filters( 'fox_default_query_params', $params );

}
endif;

add_action( 'foxele_after_render_post', 'foxele_add_rendered_article' );
/**
 * @since 4.0
 */
function foxele_add_rendered_article() {
    
    global $post, $rendered_articles;
    $rendered_articles[] = $post->ID;
    
}

/**
 * return a WP_Query instance for "related posts"
 * only used in loop
 * this function will be used for:
 *
 * related posts after main content in single post
 * related posts in standard blog layout
 * related posts in newspapers layout
 * bottom posts in single post
 * slide up posts in the footer of single post
 * top posts in single post
 *
 * $prefix is the position we want to take from the customizer, eg. single_related, single_bottom
 * $defaults is the default array of values: number => 5, source => category, ...
 *
 * @since 4.0
 * @generalized since 4.3
 */
function foxele_related_query( $prefix = '', $defaults = [], $number = null ) {
    
    $args = [
        
        'number' => get_theme_mod( 'wi_' . $prefix . '_number', $defaults[ 'number' ] ),
        
        'orderby' => get_theme_mod( 'wi_' . $prefix . '_orderby', $defaults[ 'orderby' ] ),
        'order' => get_theme_mod( 'wi_' . $prefix . '_order', $defaults[ 'order' ] ),
        
        'exclude' => get_the_ID(),
        
        'pagination' => false,
        'unique_posts'=> false
    ];
    
    // force number
    if ( $number ) {
        $args[ 'number' ] = $number;
    }
    
    $related_source = get_theme_mod( 'wi_' . $prefix . '_source', $defaults[ 'source' ] );
    
    if ( 'author' == $related_source ) {
        
        $args[ 'author' ] = get_the_author_meta( 'ID' );
        
    } elseif ( 'category' == $related_source ) {
        
        $terms = wp_get_post_terms( get_the_ID(), 'category', array( 'fields' => 'ids' ) );
        if ( ! $terms ) {
            return;
        }
        
        $primary_cat = get_post_meta( get_the_ID(), '_wi_primary_cat', true );
        if ( in_array( $primary_cat, $terms ) ) {
            $cat = $primary_cat;
        } else {
            $cat = $terms[0];
        }
        
        $args[ 'categories' ] = [ $cat ];
        
    } elseif ( 'date' == $related_source ) {
        
        // just nothing
        
    } elseif ( 'featured' == $related_source ) {
        
        $args[ 'featured' ] = 'true';
        
    // tag    
    } else {
        
        $terms = wp_get_post_terms( get_the_ID(), 'post_tag', array( 'fields' => 'ids' ) );
        if ( empty( $terms ) ) return;
        
        $args[ 'tags' ] = $terms;
    
    }
    
    return foxele_query( $args );
    
}