<?php

if ( ! class_exists( 'rtTPGHelper' ) ):

	class rtTPGHelper {
		function verifyNonce() {
			global $rtTPG;
			$nonce     = isset( $_REQUEST[ $this->nonceId() ] ) ? $_REQUEST[ $this->nonceId() ] : null;
			$nonceText = $rtTPG->nonceText();
			if ( ! wp_verify_nonce( $nonce, $nonceText ) ) {
				return false;
			}

			return true;
		}

		function nonceText() {
			return "rttpg_nonce_secret";
		}

		function nonceId() {
			return "rttpg_nonce";
		}

		function rtAllOptionFields() {
			global $rtTPG;
			$fields  = array();
			$fieldsA = array_merge(
				$rtTPG->rtTPGCommonFilterFields(),
				$rtTPG->rtTPGLayoutSettingFields(),
				$rtTPG->rtTPGStyleFields()
			);
			foreach ( $fieldsA as $field ) {
				$fields[] = $field;
			}
			array_push( $fields, $rtTPG->rtTPGPostType() );
			array_push( $fields, $rtTPG->rtTPAdvanceFilters() );
			array_push( $fields, $rtTPG->itemFields() );

			return $fields;
		}

		function rt_get_all_term_by_taxonomy( $taxonomy = null ) {
			$terms = array();
			if ( $taxonomy ) {
				$tList = get_terms( array( $taxonomy ), array( 'hide_empty' => 0 ) );
				if ( is_array( $tList ) && ! empty( $tList ) && empty( $tList['errors'] ) ) {
					foreach ( $tList as $term ) {
						$terms[ $term->term_id ] = $term->name;
					}
				}
			}

			return $terms;
		}

		function rt_get_taxonomy_for_isotope_filter( $post_type = null ) {
			if ( ! $post_type ) {
				$post_type = get_post_meta( get_the_ID(), 'tpg_post_type', true );
			}
			if ( ! $post_type ) {
				$post_type = 'post';
			}

			return $this->rt_get_all_taxonomy_by_post_type( $post_type );
		}

		function rt_get_all_taxonomy_by_post_type( $post_type = null ) {
			$taxonomies = array();
			if ( $post_type && post_type_exists( $post_type ) ) {
				$taxObj = get_object_taxonomies( $post_type, 'objects' );
				if ( is_array( $taxObj ) && ! empty( $taxObj ) ) {
					foreach ( $taxObj as $tKey => $taxonomy ) {
						$taxonomies[ $tKey ] = $taxonomy->label;
					}
				}
			}
			if ( $post_type == 'post' ) {
				unset( $taxonomies['post_format'] );
			}

			return $taxonomies;
		}

		function rt_get_users() {
			$users = array();
			$u     = get_users();
			if ( ! empty( $u ) ) {
				foreach ( $u as $user ) {
					$users[ $user->ID ] = $user->display_name;
				}
			}

			return $users;
		}

		function getAllTPGShortCodeList() {
			global $rtTPG;
			$scList = null;
			$scQ    = get_posts( array(
				'post_type'      => $rtTPG->post_type,
				'order_by'       => 'title',
				'order'          => 'ASC',
				'post_status'    => 'publish',
				'posts_per_page' => - 1
			) );
			if ( ! empty( $scQ ) ) {
				foreach ( $scQ as $sc ) {
					$scList[ $sc->ID ] = $sc->post_title;
				}
			}

			return $scList;
		}

		function rtFieldGenerator( $fields = array(), $multi = false ) {
			$html = null;
			if ( is_array( $fields ) && ! empty( $fields ) ) {
				$rtField = new rtTPGField();
				if ( $multi ) {
					foreach ( $fields as $field ) {
						$html .= $rtField->Field( $field );
					}
				} else {
					$html .= $rtField->Field( $fields );
				}
			}

			return $html;
		}

		function rtFieldGeneratorBackup( $fields = array(), $multi = false ) {
			$html = null;
			if ( is_array( $fields ) && ! empty( $fields ) ) {
				$rtField = new rtTPGField();
				if ( $multi ) {
					$i       = 0;
					$trigger = 0;
					foreach ( $fields as $field ) {
						$html .= ( $trigger == 0 ? "<div class='rt-row'>" : null );
						$html .= $rtField->Field( $field );
						$i ++;
						$trigger ++;
						if ( $trigger == 2 || count( $fields ) == $i ) {
							$html    .= "</div>";
							$trigger = 0;
						}
					}
				} else {
					$html .= "<div class='rt-row'>";
					$html .= $rtField->Field( $fields );
					$html .= "</div>";
				}
			}

			return $html;
		}

		function get_image_sizes() {
			global $_wp_additional_image_sizes;

			$sizes = array();

			foreach ( get_intermediate_image_sizes() as $_size ) {
				if ( in_array( $_size, array( 'thumbnail', 'medium', 'large' ) ) ) {
					$sizes[ $_size ]['width']  = get_option( "{$_size}_size_w" );
					$sizes[ $_size ]['height'] = get_option( "{$_size}_size_h" );
					$sizes[ $_size ]['crop']   = (bool) get_option( "{$_size}_crop" );
				} elseif ( isset( $_wp_additional_image_sizes[ $_size ] ) ) {
					$sizes[ $_size ] = array(
						'width'  => $_wp_additional_image_sizes[ $_size ]['width'],
						'height' => $_wp_additional_image_sizes[ $_size ]['height'],
						'crop'   => $_wp_additional_image_sizes[ $_size ]['crop'],
					);
				}
			}

			$imgSize = array();
			foreach ( $sizes as $key => $img ) {
				$imgSize[ $key ] = ucfirst( $key ) . " ({$img['width']}*{$img['height']})";
			}

			return $imgSize;
		}

		function getFeatureImageSrc( $post_id = null, $fImgSize = 'medium', $mediaSource = 'feature_image' ) {
			$imgSrc = null;
			if ( $mediaSource == 'feature_image' ) {
				if ( $aID = get_post_thumbnail_id( $post_id ) ) {
					$image  = wp_get_attachment_image_src( $aID, $fImgSize );
					$imgSrc = $image[0];
				}
			} else if ( $mediaSource == 'first_image' ) {
				if ( $img = preg_match_all( '/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', get_the_content( $post_id ),
					$matches ) ) {
					$imgSrc = $matches[1][0];
				}
			}

			return $imgSrc;
		}

		function strip_tags_content( $excerpt, $type = "character", $limit = 0, $more = null ) {
			$type = ( $type == "character" || $type == "word" ? $type : "character" );
			/* $excerpt    = preg_replace( '@<(\w+)\b.*?>.*?</\1>@si', '', $excerpt );*/
			$excerpt = preg_replace( '`\[[^\]]*\]`', '', $excerpt );
			$excerpt = strip_shortcodes( $excerpt );
			$excerpt = preg_replace( '`[[^]]*]`', '', $excerpt );
			$excerpt = str_replace( '…', '', $excerpt );
//		    $excerpt    = preg_replace( " ([.*?])", '', $excerpt );
			$oldExcerpt = $excerpt;
			if ( $type == "word" ) {
				$limit   = $limit + 1;
				$excerpt = explode( ' ', $excerpt, $limit );
				if ( count( $excerpt ) >= $limit ) {
					array_pop( $excerpt );
					$excerpt = implode( " ", $excerpt );
					$excerpt = ( $more ? $excerpt . " " . $more : $excerpt );
				} else {
					$excerpt = $oldExcerpt;
				}
			} else {
				if ( $limit > 0 && strlen( $excerpt ) > $limit ) {
					$excerpt = substr( $excerpt, 0, $limit );
					$excerpt = substr( $excerpt, 0, strripos( $excerpt, " " ) );
					$excerpt = ( $more ? $excerpt . " " . $more : $excerpt );
				}
			}

			return $excerpt;
		}


		function rt_pagination( $pages = '', $range = 4 ) {

			$html      = null;
			$showitems = ( $range * 2 ) + 1;
			global $paged;
			if ( empty( $paged ) ) {
				if ( get_query_var('paged') ) {
					$paged = get_query_var('paged');
				} elseif ( get_query_var('page') ) {
					$paged = get_query_var('page');
				} else {
					$paged = 1;
				}
			}
			if ( $pages == '' ) {
				global $wp_query;
				$pages = $wp_query->max_num_pages;
				if ( ! $pages ) {
					$pages = 1;
				}
			}

			if ( 1 != $pages ) {

				$html .= '<div class="rt-pagination">';
				$html .= '<ul class="pagination">';

				if ( $paged > 1 && $showitems < $pages ) {
					$html .= "<li><a href='" . get_pagenum_link( $paged - 1 ) . "' aria-label='Previous'><i class='fa fa-chevron-left' aria-hidden='true'></i></li>";
				}

				for ( $i = 1; $i <= $pages; $i ++ ) {

					if ( 1 != $pages && ( ! ( $i >= $paged + $range + 1 || $i <= $paged - $range - 1 ) || $pages <= $showitems ) ) {
						$html .= ( $paged == $i ) ? "<li class=\"active\"><span>" . $i . "</span>

    </li>" : "<li><a href='" . get_pagenum_link( $i ) . "'>" . $i . "</a></li>";

					}

				}

				if ( $paged < $pages && $showitems < $pages ) {
					$html .= "<li><a href=\"" . get_pagenum_link( $paged + 1 ) . "\"  aria-label='Next'><i class='fa fa-chevron-right' aria-hidden='true'></i></a></li>";
				}

				$html .= "</ul>";
				$html .= "</div>";
			}

			return $html;

		}

		function get_pro_feature_list() {
			$html = "<ol>
				<li>Fully responsive and mobile friendly.</li>
				<li>55 Different Layouts</li>
				<li>Even and Masonry Grid.</li>
				<li>WooCommerce supported.</li>
				<li>Custom Post Type Supported</li>
				<li>Display posts by any Taxonomy like category(s), tag(s), author(s), keyword(s)</li>
				<li>Order by Id, Title, Created date, Modified date and Menu order.</li>
				<li>Display image size (thumbnail, medium, large, full)</li>
				<li>Isotope filter for any taxonomy ie. categories, tags...</li>
				<li>Query Post with Relation.</li>
				<li>Fields Selection.</li>
				<li>All Text and Color control.</li>
				<li>Enable/Disable Pagination.</li>
				<li>AJAX Pagination (Load more and Load on Scrolling)</li>
				<li> and many more .......</li>
			</ol>";

			return $html;
		}

	}

endif;