<?php defined('ABSPATH') or die('No script kiddies please!!');
if ( !class_exists('EGPR_Library') ) {
    class EGPR_Library {
        /**
         * Prints array in pre format
         *
         * @since 1.0.0
         *
         * @param array $array
         */
        function displayArr($array) {
            echo "<pre>";
            print_r($array);
            echo "</pre>";
        }
        
        /*
         * Get All Pages
         */
        public function get_all_page_lists(){
             wp_reset_postdata();
            $pages = get_pages(array('posts_per_page' => -1, 'post_status' => 'publish'));
            $page_lists = array();
            if (count($pages) > 0) {
                foreach ($pages as $page) :
                    $page_lists[$page->ID] = $page->post_title;
                endforeach;
            }
            return $page_lists;
        }
        
         /*
         * Get all posts
        */
        function get_all_post_lists() {
            wp_reset_postdata();
            $posts = get_posts(array('posts_per_page' => -1, 'post_status' => 'publish'));
            $post_lists = array();
            if (count($posts) > 0) {
                foreach ($posts as $post) :
                    $post_lists[$post->ID] = $post->post_title;
                endforeach;
            }
            return $post_lists;
        }

        /*
         * Get all categories 
        */
        function get_categories($args = '') {
            $defaults = array('taxonomy' => 'category');
            $args = wp_parse_args($args, $defaults);
            $taxonomy = $args['taxonomy'];
            $taxonomy = apply_filters('get_categories_taxonomy', $taxonomy, $args);
            $categories = get_terms($taxonomy, $args);
            $categories_lists = array();
            if (count($categories) > 0) {
                foreach ($categories as $category):
                    $categories_lists[$category->term_id] = $category->name;
                endforeach;
            }
            return $categories_lists;
        }
        
        /*
         *  Get All Custom Post Types 
        */
        function get_post_types() {
            wp_reset_postdata();
            $args = array('public' => true, '_builtin' => false);
            $output = 'objects'; // names or objects, note names is the default
            $operator = 'and'; // 'and' or 'or'
            $post_types = get_post_types($args, $output, $operator);
            $post_type_lists = array();
            if (count($post_types) > 0) {
                foreach ($post_types as $post_type) :
                    $post_type_lists[] = $post_type->labels->name;
                endforeach;
                return $post_type_lists;
            }
        }
        
        /**
         * Sanitizes Multi Dimensional Array
         * @param array $array
         * @param array $sanitize_rule
         * @return array
         *
         * @since 1.0.0
         */
        function sanitize_array($array = array(), $sanitize_rule = array()) {
            if ( !is_array($array) || count($array) == 0 ) {
                return array();
            }

            foreach ( $array as $k => $v ) {
                if ( !is_array($v) ) {
                    $default_sanitize_rule = (is_numeric($k)) ? 'text' : 'html';
                    $sanitize_type = isset($sanitize_rule[ $k ]) ? $sanitize_rule[ $k ] : $default_sanitize_rule;
                    $array[ $k ] = $this->sanitize_value($v, $sanitize_type);
                }
                if ( is_array($v) ) {
                    $array[ $k ] = $this->sanitize_array($v, $sanitize_rule);
                }
            }

            return $array;
        }

        /**
         * Sanitizes Value
         *
         * @param type $value
         * @param type $sanitize_type
         * @return string
         *
         * @since 1.0.0
         */
        function sanitize_value($value = '', $sanitize_type = 'text') {
            switch ( $sanitize_type ) {
                case 'html':
                    $allowed_html = wp_kses_allowed_html('post');
                    return wp_kses($value, $allowed_html);
                    break;
                default:
                    return sanitize_text_field($value);
                    break;
            }
        }
        
        /*
         * Get all reviews filterd 
         * @number of review to display
         * @check show five star rating only options enabled or not?
         */
        function get_filtered_reviews($reviews, $number_of_reviews, $show_five_ratings_only) {
            $i=0;
            $new_reviews = array();
            foreach ($reviews as $key => $value) {
                if($i == $number_of_reviews)
                break;
                if( $show_five_ratings_only == 1 && $value['rating'] == 5) {
                    $new_reviews[] = $value;
                    $i++;
                }
                if($show_five_ratings_only == 0) {
                    $new_reviews[] = $value;
                    $i++;
                }
            }
            return $new_reviews;
        }

         /*
         * Get all reviews filterd 
         * @number of review to display
         * @check show four star rating only options enabled or not?
         */
        function get_filtered_reviews_4star($reviews, $number_of_reviews, $show_four_ratings_only) {
            $i=0;
            $new_reviews = array();
            foreach ($reviews as $key => $value) {
                if($i == $number_of_reviews)
                break;
                if( $show_four_ratings_only == 1 && $value['rating'] == 4) {
                    $new_reviews[] = $value;
                    $i++;
                }
                if($show_four_ratings_only == 0) {
                    $new_reviews[] = $value;
                    $i++;
                }
            }
            return $new_reviews;
        }

        /*
         * Get all reviews filterd 
         * @number of review to display
         * @check show both five and four star rating
         */
        function get_filtered_both_reviews($reviews, $number_of_reviews){
            $i=0;
            $new_reviews = array();
            foreach ($reviews as $key => $value) {
                if($i == $number_of_reviews)
                break;
                if( $value['rating'] == 4 || $value['rating'] == 5) {
                    $new_reviews[] = $value;
                    $i++;
                }
            }
            return $new_reviews;
        }
    }
}