<?php

class ThemeMakersHelper {

    public static function get_post_featured_image($post_id, $alias) {
        $img_src = wp_get_attachment_image_src(get_post_thumbnail_id($post_id), 'single-post-thumbnail');
        $img_src = $img_src[0];
        $url = self::get_image($img_src, $alias);
        return $url;
    }

    public static function resize_image($img_src, $alias) {
        return self::get_image($img_src, $alias);
    }

    //IMAGES start *********************************************************

    public static function get_image($img_src, $alias) {
        $sizes = self::get_theme_image_sizes();
        $img_src = explode(".", $img_src);
        $ext = $img_src[count($img_src) - 1];
        unset($img_src[count($img_src) - 1]);
        $img_src = implode(".", $img_src);
        $new_img_src=$img_src . "-" . $sizes[$alias]['width'] . 'x' . $sizes[$alias]['height'] . "." . $ext;
		
		if (!self::is_file_url_exists($new_img_src)) {
		return $img_src.'.'.$ext;
		}
		else
		return $new_img_src;
		
    }
	public static function is_file_url_exists($url) {
		if (@file_get_contents($url, 0, NULL, 0, 1)) {
				return 1;
		}
				return 0;
		}

    public static function get_theme_image_sizes() {
        $data = array();
		
		$data['archive_last'] = array();
        $data['archive_last']['name'] = '159x100';
        $data['archive_last']['width'] = 159;
        $data['archive_last']['height'] = 100;
        $data['archive_last']['crop'] = true;
		
		$data['archive_first'] = array();
        $data['archive_first']['name'] = '646x453';
        $data['archive_first']['width'] = 646;
        $data['archive_first']['height'] = 453;
        $data['archive_first']['crop'] = true;
		
		
        $data['slider_showcase'] = array();
        $data['slider_showcase']['name'] = 'SLIDER SHOWCASE';
        $data['slider_showcase']['width'] = 730;
        $data['slider_showcase']['height'] = 410;
        $data['slider_showcase']['crop'] = true;

        $data['slider_showcase_thumb'] = array();
        $data['slider_showcase_thumb']['name'] = 'SLIDER THUMB IMAGE SIZE';
        $data['slider_showcase_thumb']['width'] = 86;
        $data['slider_showcase_thumb']['height'] = 52;
        $data['slider_showcase_thumb']['crop'] = true;

        $data['carusell_thumb'] = array();
        $data['carusell_thumb']['name'] = 'CARUSELL THUMB IMAGE SIZE';
        $data['carusell_thumb']['width'] = 222;
        $data['carusell_thumb']['height'] = 137;
        $data['carusell_thumb']['crop'] = true;

        $data['post_thumb'] = array();
        $data['post_thumb']['name'] = 'POST IMAGE SIZE';
        $data['post_thumb']['width'] = 266;
        $data['post_thumb']['height'] = 155;
        $data['post_thumb']['crop'] = true;

        $data['single_thumb'] = array();
        $data['single_thumb']['name'] = 'SINGLE THUMB IMAGE SIZE';
        $data['single_thumb']['width'] = 146;
        $data['single_thumb']['height'] = 100;
        $data['single_thumb']['crop'] = true;


        $data['single_thumb2'] = array();
        $data['single_thumb2']['name'] = 'SINGLE THUMB2 IMAGE SIZE';
        $data['single_thumb2']['width'] = 150;
        $data['single_thumb2']['height'] = 120;
        $data['single_thumb2']['crop'] = true;


        $data['single_thumb3'] = array();
        $data['single_thumb3']['name'] = 'SINGLE THUMB2 IMAGE SIZE';
        $data['single_thumb3']['width'] = 94;
        $data['single_thumb3']['height'] = 60;
        $data['single_thumb3']['crop'] = true;

        $data['gallery_thumb'] = array();
        $data['gallery_thumb']['name'] = 'SINGLE THUMB IMAGE SIZE';
        $data['gallery_thumb']['width'] = 313;
        $data['gallery_thumb']['height'] = 220;
        $data['gallery_thumb']['crop'] = true;

        return $data;
    }

    //IMAGES end *********************************************************


    public static function get_contact_information() {
        $contact_info = ThemeMakersThemeModel::get_option('contact_info');
        return $contact_info;
    }

//Custom page navigation
    public static function pagenavi($query = null) {
        global $wp_query, $wp_rewrite;
        if (!$query)
            $query = $wp_query;
        $pages = '';
        $max = $query->max_num_pages;
        if (!$current = get_query_var('paged'))
            $current = 1;
        $a['base'] = str_replace(999999999, '%#%', get_pagenum_link(999999999));
        $a['total'] = $max;
        $a['current'] = $current;

        $total = 1; //1 - display the text "Page N of N", 0 - not display
        $a['mid_size'] = 5; //how many links to show on the left and right of the current
        $a['end_size'] = 1; //how many links to show in the beginning and end
        $a['prev_text'] = ''; //text of the "Previous page" link
        $a['next_text'] = ''; //text of the "Next page" link
        //if ($total == 1 && $max > 1)
        //$pages = '<span class="pages">Page (' . $current . ' ' . __('of', THEMEMAKERS_THEME_FOLDER_NAME) . ' ' . $max . ')</span>';
        echo $pages . paginate_links($a);
    }

    public function add_comment() {
        if (!empty($_REQUEST['comment_content'])) {
            $time = current_time('mysql');
            $user = get_userdata(get_current_user_id());
            $data = array(
                'comment_post_ID' => $_REQUEST['comment_post_ID'],
                'comment_author' => $user->data->user_nicename,
                'comment_author_email' => $user->data->user_email,
                'comment_author_url' => $user->data->user_url,
                'comment_content' => $_REQUEST['comment_content'],
                'comment_parent' => $_REQUEST['comment_parent'],
                'user_id' => $user->data->ID,
                'comment_date' => $time,
            );

            echo wp_insert_comment($data);
        }

        exit;
    }
	
    public static function get_post_sort_array() {
        return array('ID', 'date', 'post_date', 'title', 'post_title', 'name', 'post_name', 'modified',
            'post_modified', 'modified_gmt', 'post_modified_gmt', 'menu_order', 'parent', 'post_parent',
            'rand', 'comment_count', 'author', 'post_author');
    }

    public static function get_post_categories() {
        $post_categories_objects = get_categories(array(
            'orderby' => 'name',
            'order' => 'ASC',
            'style' => 'list',
            'show_count' => 0,
            'hide_empty' => 0,
            'use_desc_for_title' => 1,
            'child_of' => 0,
            'hierarchical' => true,
            'title_li' => '',
            'show_option_none' => '',
            'number' => NULL,
            'echo' => 0,
            'depth' => 0,
            'current_category' => 0,
            'pad_counts' => 0,
            'taxonomy' => 'category',
            'walker' => 'Walker_Category'));

        $post_categories = array();
        $post_categories[0] = __('All Categories', THEMEMAKERS_THEME_FOLDER_NAME);
        foreach ($post_categories_objects as $value) {
            $post_categories[$value->term_id] = $value->name;
        }

        return $post_categories;
    }

    public static function getDirectoryList($directory) {

        // create an array to hold directory list
        $results = array();

        // create a handler for the directory
        $handler = opendir($directory);

        // open directory and walk through the filenames
        while ($file = readdir($handler)) {

            // if file isn't this directory or its parent, add it to the results
            if ($file != "." && $file != "..") {
                $results[] = $file;
            }
        }

        // tidy up: close the handler
        closedir($handler);

        // done!
        return $results;
    }

    public static function cut_text($text, $charlength) {
        $charlength++;

        if (mb_strlen($text) > $charlength) {
            $subex = mb_substr($text, 0, $charlength);
            $exwords = explode(' ', $subex);
            $excut = - ( mb_strlen($exwords[count($exwords) - 1]) );
            if ($excut < 0) {
                return mb_substr($subex, 0, $excut);
            } else {
                return $subex;
            }
        } else {
            return $text;
        }
    }

    public static function sanitize_quotes($str) {
        $str = str_replace("'", '\'', $str);
        $str = str_replace('"', "\"", $str);
        return $str;
    }

    //for skeleton css framework
    public static function convert_css_class_name($num, $framework = 'skeleton') {
        $class_names = array(
            1 => 'one',
            2 => 'two',
            3 => 'three',
            4 => 'four',
            5 => 'five',
            6 => 'six',
            7 => 'seven',
            8 => 'eight',
            9 => 'nine',
            10 => 'ten',
            11 => 'eleven',
            12 => 'twelve',
            13 => 'thirteen',
            14 => 'fourteen',
            15 => 'fifteen',
            16 => 'sixteen'
        );

        return $class_names[$num];
    }

    public static function get_theme_buttons() {
        return array(
            'red' => __('Red', THEMEMAKERS_THEME_FOLDER_NAME),
            'yellow' => __('Yellow', THEMEMAKERS_THEME_FOLDER_NAME),
            'green' => __('Green', THEMEMAKERS_THEME_FOLDER_NAME),
            'blue' => __('Blue', THEMEMAKERS_THEME_FOLDER_NAME),
            'violet' => __('Violet', THEMEMAKERS_THEME_FOLDER_NAME),
            'dark' => __('Dark', THEMEMAKERS_THEME_FOLDER_NAME)
        );
    }

    public static function get_theme_buttons_sizes() {
        return array(
            'small' => __('Small', THEMEMAKERS_THEME_FOLDER_NAME),
            'medium' => __('Medium', THEMEMAKERS_THEME_FOLDER_NAME),
            'large' => __('Large', THEMEMAKERS_THEME_FOLDER_NAME),
			'large type-1' => __('Large type 1', THEMEMAKERS_THEME_FOLDER_NAME),
			'large type-2' => __('Large type 2', THEMEMAKERS_THEME_FOLDER_NAME),
			'large type-3' => __('Large type 3', THEMEMAKERS_THEME_FOLDER_NAME)
        );
    }

    /*
     * remeik of standart function
     */

    public static function wp_get_archives($args = '') {
        global $wpdb, $wp_locale;

        $defaults = array(
            'type' => 'monthly', 'limit' => '',
            'format' => 'link', 'before' => '',
            'after' => '', 'show_post_count' => false,
            'echo' => 1, 'current_year' => (int) date('Y'),
            'current_month' => (int) date('m'),
            'current_week' => (int) date('W'),
            'post_type' => 'post'
        );

        $r = wp_parse_args($args, $defaults);
        extract($r, EXTR_SKIP);

        if ('' == $type)
            $type = 'monthly';

        if ('' != $limit) {
            $limit = absint($limit);
            $limit = ' LIMIT ' . $limit;
        }

        // this is what will separate dates on weekly archive links
        $archive_week_separator = '&#8211;';

        // over-ride general date format ? 0 = no: use the date format set in Options, 1 = yes: over-ride
        $archive_date_format_over_ride = 0;

        // options for daily archive (only if you over-ride the general date format)
        $archive_day_date_format = 'Y/m/d';

        // options for weekly archive (only if you over-ride the general date format)
        $archive_week_start_date_format = 'Y/m/d';
        $archive_week_end_date_format = 'Y/m/d';

		
        if (!$archive_date_format_over_ride) {
            $archive_day_date_format = get_option('date_format');
            $archive_week_start_date_format = get_option('date_format');
            $archive_week_end_date_format = get_option('date_format');
        }
		 
		 

        //filters
        $where = apply_filters('getarchives_where', "WHERE post_type = '" . $post_type . "' AND post_status = 'publish'", $r);
        $join = apply_filters('getarchives_join', '', $r);

        $output = '';

        if ('monthly' == $type) {
            $query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where AND YEAR(post_date)=$current_year GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC $limit";
            $key = md5($query);
            $cache = wp_cache_get('wp_get_archives', 'general');
            if (!isset($cache[$key])) {
                $arcresults = $wpdb->get_results($query);
                $cache[$key] = $arcresults;
                wp_cache_set('wp_get_archives', $cache, 'general');
            } else {
                $arcresults = $cache[$key];
            }
           if ($arcresults) {
                $afterafter = $after;
                foreach ((array) $arcresults as $arcresult) {
                    $url = ThemeMakersHelper::get_month_link($arcresult->year, $arcresult->month);
                    
                    $text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($arcresult->month), $arcresult->year);
                    if ($show_post_count)
                        $after = '&nbsp;(' . $arcresult->posts . ')' . $afterafter;
                    if ($arcresult->posts > 0) {
                        $before = '<a href="javascript:thememakers_expand_archive_month(' . $arcresult->month . ',' . $arcresult->year . ',\'' . $post_type . '\');void(0);" class="archive_arrow">+</a>&nbsp;';
                    } else {
                        $before = "";
                    }
                    $output .= '<li class="tmk_archive_month_' . $arcresult->year . '_' . $arcresult->month . '">' . ThemeMakersHelper::get_archives_link($url, $text, $before, $after, $post_type) . '</li>';
                }
            } 
			
			//wp_get_archives('type=monthly');
        } elseif ('yearly' == $type) {
            $query = "SELECT YEAR(post_date) AS `year`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date) ORDER BY post_date DESC $limit";
            $key = md5($query);
            $cache = wp_cache_get('wp_get_archives', 'general');
            if (!isset($cache[$key])) {
                $arcresults = $wpdb->get_results($query);
                $cache[$key] = $arcresults;
                wp_cache_set('wp_get_archives', $cache, 'general');
            } else {
                $arcresults = $cache[$key];
            }
            if ($arcresults) {
                $afterafter = $after;
                foreach ((array) $arcresults as $arcresult) {
                    $url = ThemeMakersHelper::get_year_link($arcresult->year);
                    $text = sprintf('%d', $arcresult->year);
                    if ($show_post_count)
                        $after = '&nbsp;(' . $arcresult->posts . ')' . $afterafter;
                    if ($arcresult->posts > 0) {
                        $before = '<a href="javascript:thememakers_expand_archive_year(' . $arcresult->year . ',\'' . $post_type . '\');void(0);" class="archive_arrow">+</a>&nbsp;';
                    } else {
                        $before = "";
                    }
                    $output .= '<li class="tmk_archive_year_' . $arcresult->year . '">' . ThemeMakersHelper::get_archives_link($url, $text, $before, $after, $post_type) . '</li>';
                }
            } 
			//wp_get_archives('type=yearly');
        } elseif ('daily' == $type) {
							
            $first_day = (int) date('d', strtotime($current_year . "W" . $current_week . 1));
            $last_day = (int) date('d', strtotime($current_year . "W" . $current_week . 7));

            //$query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, DAYOFMONTH(post_date) AS `dayofmonth`, count(ID) as posts FROM $wpdb->posts $join $where AND MONTH(post_date)=$current_month AND YEAR(post_date)=$current_year AND DAYOFMONTH(post_date)>=$first_day AND DAYOFMONTH(post_date)<=$last_day GROUP BY YEAR(post_date), MONTH(post_date), DAYOFMONTH(post_date) ORDER BY post_date DESC $limit";
			
			$query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, DAYOFMONTH(post_date) AS `dayofmonth`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date), DAYOFMONTH(post_date) ORDER BY post_date $order $limit";
			
            $key = md5($query);
            $cache = wp_cache_get('wp_get_archives', 'general');
            if (!isset($cache[$key])) {
					
                $arcresults = $wpdb->get_results($query);
                $cache[$key] = $arcresults;
                wp_cache_set('wp_get_archives', $cache, 'general');
            } else {
                $arcresults = $cache[$key];
            }
            if ($arcresults) {
					
					
                $afterafter = $after;
                foreach ((array) $arcresults as $arcresult) {
						
					apply_filters('day_link', home_url( '?m=' . $year . zeroise($month, 2)));
					
                    $url = ThemeMakersHelper::get_day_link($arcresult->year, $arcresult->month, $arcresult->dayofmonth);
					
					
					
                    $date = sprintf('%1$d-%2$02d-%3$02d 00:00:00', $arcresult->year, $arcresult->month, $arcresult->dayofmonth);
                    $text = mysql2date($archive_day_date_format, $date);
                    if ($show_post_count)
                        $after = '&nbsp;(' . $arcresult->posts . ')' . $afterafter;

                    $output .= '<li>' . ThemeMakersHelper::get_archives_link($url, $text, $before, $after, $post_type) . '</li>';
                }
            }
			//wp_get_archives('type=daily');
			
        } elseif ('weekly' == $type) {
            $week = _wp_mysql_week('`post_date`');
            $query = "SELECT DISTINCT $week AS `week`, YEAR( `post_date` ) AS `yr`, DATE_FORMAT( `post_date`, '%Y-%m-%d' ) AS `yyyymmdd`, count( `ID` ) AS `posts` FROM `$wpdb->posts` $join $where AND MONTH(post_date)=$current_month AND YEAR(post_date)=$current_year GROUP BY $week, YEAR( `post_date` ) ORDER BY `post_date` DESC $limit";
            $key = md5($query);
            $cache = wp_cache_get('wp_get_archives', 'general');
            if (!isset($cache[$key])) {
                $arcresults = $wpdb->get_results($query);
                $cache[$key] = $arcresults;
                wp_cache_set('wp_get_archives', $cache, 'general');
            } else {
                $arcresults = $cache[$key];
            }
            $arc_w_last = '';
            $afterafter = $after;
            if ($arcresults) {
                foreach ((array) $arcresults as $arcresult) {
                    if ($arcresult->week != $arc_w_last) {
                        $arc_year = $arcresult->yr;
                        $arc_w_last = $arcresult->week;
                        $arc_week = get_weekstartend($arcresult->yyyymmdd, get_option('start_of_week'));
                        $arc_week_start = date_i18n($archive_week_start_date_format, $arc_week['start']);
                        $arc_week_end = date_i18n($archive_week_end_date_format, $arc_week['end']);
                        $url = sprintf('%1$s/%2$s%3$sm%4$s%5$s%6$sw%7$s%8$d', home_url(), '', '?', '=', $arc_year, '&amp;', '=', $arcresult->week);
                        $text = $arc_week_start . $archive_week_separator . $arc_week_end;
                        if ($show_post_count)
                            $after = '&nbsp;(' . $arcresult->posts . ')' . $afterafter;
                        if ($arcresult->posts > 0) {
                            $before = '<a href="javascript:thememakers_expand_archive_week(' . $arcresult->week . ',' . $current_month . ',' . $current_year . ',\'' . $post_type . '\');void(0);" class="archive_arrow">+</a>&nbsp;';
                        } else {
                            $before = "";
                        }
                        $output .= '<li class="tmk_archive_week_' . $current_year . '_' . $current_month . '_' . $arcresult->week . '">' . ThemeMakersHelper::get_archives_link($url, $text, $before, $after, $post_type) . '</li>';
                    }
                }
            }
			
			//wp_get_archives('type=weekly');
        } elseif (( 'postbypost' == $type ) || ('alpha' == $type)) {
            $orderby = ('alpha' == $type) ? 'post_title ASC ' : 'post_date DESC ';
            $query = "SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit";
            $key = md5($query);
            $cache = wp_cache_get('wp_get_archives', 'general');
            if (!isset($cache[$key])) {
                $arcresults = $wpdb->get_results($query);
                $cache[$key] = $arcresults;
                wp_cache_set('wp_get_archives', $cache, 'general');
            } else {
                $arcresults = $cache[$key];
            }
            if ($arcresults) {
                foreach ((array) $arcresults as $arcresult) {
                    if ($arcresult->post_date != '0000-00-00 00:00:00') {
                        $url = get_permalink($arcresult);
                        if ($arcresult->post_title)
                            $text = strip_tags(apply_filters('the_title', $arcresult->post_title, $arcresult->ID));
                        else
                            $text = $arcresult->ID;
                        $output .= get_archives_link($url, $text, $before, $after, $post_type);
                    }
                }
            }
        }
        if ($echo)
            echo $output;
        else
            return $output;
    }
	function get_year_link($year) {
				
				if (!$year)
						$year = gmdate('Y', current_time('timestamp'));
				
				
						return apply_filters('year_link', home_url('?m=' . $year), $year);
				
		}
		function get_month_link($year, $month) {

				if (!$year)
						$year = gmdate('Y', current_time('timestamp'));
				if (!$month)
						$month = gmdate('m', current_time('timestamp'));

						return apply_filters('month_link', home_url('?m=' . $year . zeroise($month, 2)), $year, $month);

		}
	public static function get_day_link($year, $month, $day){
				
		if ( !$year )
				$year = gmdate('Y', current_time('timestamp'));
		if ( !$month )
				$month = gmdate('m', current_time('timestamp'));
		if ( !$day )
				$day = gmdate('j', current_time('timestamp'));
	
			return apply_filters('day_link', home_url( '?m=' . $year . zeroise($month, 2) . zeroise($day, 2) ), $year, $month, $day);
		
	}

	public static function get_archives_link($url, $text, $before, $after, $post_type) {
        $text = wptexturize($text);
        $title_text = esc_attr($text);
		
        $url = esc_url($url);

        $link_html = "";
        if ($post_type != 'post') {
            $link_html = "$before<a href='$url&post_type=$post_type' title='$title_text'>$text</a>$after";
        } else {
            $link_html = "$before<a href='$url' title='$title_text'>$text</a>$after";
        }
		
        $link_html = apply_filters('get_archives_link', $link_html);

        return $link_html;
    }

    public static function get_years_archive_links() {
        $year = $_REQUEST['year'];
        $args = array(
            'type' => 'monthly',
            'show_post_count' => false,
            'current_year' => $year,
            'post_type' => $_REQUEST['post_type'],
            'echo' => 1
        );
        ThemeMakersHelper::wp_get_archives($args);
        exit;
    }

    public static function get_monthes_archive_links() {
        $year = $_REQUEST['year'];
        $month = $_REQUEST['month'];
        $args = array(
            'type' => 'weekly',
            'show_post_count' => false,
            'current_year' => $year,
            'current_month' => $month,
            'post_type' => $_REQUEST['post_type'],
            'echo' => 1
        );
        ThemeMakersHelper::wp_get_archives($args);
        exit;
    }

    public static function get_weeks_archive_links() {
        $year = $_REQUEST['year'];
        $month = $_REQUEST['month'];
        $week = $_REQUEST['week'];
        $args = array(
            'type' => 'daily',
            'show_post_count' => false,
            'current_year' => $year,
            'current_month' => $month,
            'current_week' => $week,
            'post_type' => $_REQUEST['post_type'],
            'echo' => 1
        );
        ThemeMakersHelper::wp_get_archives($args);
        exit;
    }

    public static function get_user_ip() {
        if (getenv('REMOTE_ADDR'))
            $user_ip = getenv('REMOTE_ADDR');
        elseif (getenv('HTTP_FORWARDED_FOR'))
            $user_ip = getenv('HTTP_FORWARDED_FOR');
        elseif (getenv('HTTP_X_FORWARDED_FOR'))
            $user_ip = getenv('HTTP_X_FORWARDED_FOR');
        elseif (getenv('HTTP_X_COMING_FROM'))
            $user_ip = getenv('HTTP_X_COMING_FROM');
        elseif (getenv('HTTP_VIA'))
            $user_ip = getenv('HTTP_VIA');
        elseif (getenv('HTTP_XROXY_CONNECTION'))
            $user_ip = getenv('HTTP_XROXY_CONNECTION');
        elseif (getenv('HTTP_CLIENT_IP'))
            $user_ip = getenv('HTTP_CLIENT_IP');
        $user_ip = trim($user_ip);
        if (empty($user_ip))
            return false;
        if (!preg_match("/^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/", $user_ip))
            return false;
        return $user_ip;
    }

    public static function quotes_shield($text) {
        $text = str_replace("\'", "&#039;", $text);
        $text = str_replace("'", "&#039;", $text);
        $text = str_replace('"', "&quot;", $text);

        return $text;
    }

    public static function db_quotes_shield($data) {
        if (is_array($data)) {
            foreach ($data as $key => $value) {
                if (is_array($value)) {
                    $data[$key] = self::db_quotes_shield($value);
                } else {
                    $value = stripslashes($value);
                    $value = str_replace('\"', '"', $value);
                    $value = str_replace("\'", "'", $value);
                    $data[$key] = $value;
                }
            }
        }

        return $data;
    }

    public static function get_unique_ids() {
        $result = array();
        for ($i = 0; $i < 5; $i++) {
            $result[] = uniqid();
        }

        echo json_encode($result);
        exit;
    }

    public static function get_page_backround($page_id) {
        if ($page_id) {
            $page_bg = Thememakers_Entity_Page_Constructor::get_page_settings($page_id);

            if ($page_bg['pagebg_type'] == "image") {
                if (!empty($page_bg['pagebg_image'])) {

                    if (!$page_bg['pagebg_type_image_option']) {
                        $page_bg['pagebg_type_image_option'] = "repeat";
                    }

                    switch ($page_bg['pagebg_type_image_option']) {
                        case "repeat-x":
                            if (!empty($page_bg['pagebg_image'])) {
                                return "background: url(" . $page_bg['pagebg_image'] . ") repeat-x 0 0";
                            } else {
                                return "";
                            }
                            break;

                        case "fixed":
                            if (!empty($page_bg['pagebg_image'])) {
                                return "background: url(" . $page_bg['pagebg_image'] . ") no-repeat center top fixed;";
                            } else {
                                return "";
                            }
                            break;

                        default:
                            if (!empty($page_bg['pagebg_image'])) {
                                return "background: url(" . $page_bg['pagebg_image'] . ") repeat 0 0";
                            } else {
                                return "";
                            }
                            break;
                    }
                }
            }

            if ($page_bg['pagebg_type'] == "color") {
                if (!empty($page_bg['pagebg_color'])) {
                    return "background: " . $page_bg['pagebg_color'];
                }
            }

            return self::draw_body_bg();
        } else {
            return self::draw_body_bg();
        }
    }

    private static function draw_body_bg() {
        $disable_body_bg = ThemeMakersThemeModel::get_option('disable_body_bg');
        if (!$disable_body_bg) {
            $body_pattern = ThemeMakersThemeModel::get_option('body_pattern');
            $body_pattern_custom_color = ThemeMakersThemeModel::get_option('body_pattern_custom_color');
            $body_pattern_selected = (int) ThemeMakersThemeModel::get_option('body_pattern_selected');
            $body_bg_color_selected = ThemeMakersThemeModel::get_option('body_bg_color');

            if ($body_pattern_selected == 0 OR $body_pattern_selected == 1) {
                if (!empty($body_pattern)) {
                    return "background: url(" . $body_pattern . ") repeat 0 0 " . $body_bg_color_selected . ";";
                } else {
                    return "";
                }
            } else {
                return "background: " . $body_pattern_custom_color;
            }
        }
    }

}

