<?php

class Intense_Filler extends Intense_Shortcode {

	function __construct() {
        parent::__construct();

		global $intense_visions_options;

		$this->title = __( 'Filler', 'intense' );
		$this->description = __( 'The filler shortcode helps you quickly mock up content by adding filler text and images. When designing a page or post, it is helpful to drop in text and images so that you can get the idea of how things will lay out. The filler shortcode is perfect for quickly putting together demo sites for your clients.
			<br><br>
			Often the hardest thing is just getting started. The filler shortcode can help you get over the hurdle of a “blank page.”', 'intense' );
		$this->category = __( 'Typography', 'intense' );
		$this->show_preview = true;		
	}

	function shortcode( $atts, $content = null ) {
		global $intense_visions_options;
		extract( shortcode_atts( $this->get_shortcode_defaults( null, $atts ), $atts ) );

		if ( $type == 'text' ) {			
			require INTENSE_PLUGIN_FOLDER . '/inc/shortcodes/data/filler-text.php';

			$output = '';

			if ( $paragraphs > 0 || $words > 0 || $bytes > 0 ) {
				$content = '';

				if ( $words > 0 ) $paragraphs = ceil( $words * .5 ); //pull off enough words to use later
				if ( $bytes > 0 ) $paragraphs = ceil( $bytes * .5 ); //pull off enough bytes to use later

				for ( $i=0; $i < $paragraphs; $i++ ) {
					$sentences = rand( 3, 12 );

					$content .= '<p>';

					for ( $j=0; $j < $sentences; $j++ ) {
						if ( $content === '<p>' && $start_with_lorem ) {
							$content .= $lipsum[ 0 ] . ' ';
						} else {
							$content .= $lipsum[ rand( 0, count( $lipsum ) - 1 ) ] . ' ';
						}
					}

					$content .= '</p>';
				}

				if ( $words > 0 ) {
					$word_list = explode( ' ', $content );

					for ( $i=0; $i < $words; $i++ ) {
						$output .= $word_list[ $i ] . ' ';
					}

					if ( substr_compare( $output, '</p>', -strlen( '</p>' ), strlen( '</p>' ) ) !== 0 ) {
						$output = trim( $output ) . '.</p>';
					}

					if ( $is_title ) {
						$output = str_replace( array( "<p>", "</p>", "." ), '', $output );
						$output = rtrim( $output, ',' );
						$output = ucwords( $output );
					}
				} else if ( $bytes > 0 ) {
						$output = str_replace( array( "<p>", "</p>", "." ), '', $content );
						$output = substr( $output, 0, $bytes );

						if ( $is_title ) {
							$output = str_replace( array( "<p>", "</p>", "." ), '', $output );
							$output = rtrim( $output, ',' );
							$output = ucwords( $output );
						} else {
							$output = '<p>' . $output . '</p>';
						}
					} else {
					$output = $content;
				}
			} else if ( $list > 0 ) {
					$start_index = 0;

					$output .= '<ul>';

					if ( $start_with_lorem ) {
						$start_index = 0;
					} else {
						$start_index = rand( 0, count( $lipsum ) - 1 );
					}

					for ( $i=0; $i < $list; $i++ ) {
						$output .= '<li>' . $lipsum[ $start_index ] . '</li>';

						$start_index = rand( 0, count( $lipsum ) - 1 );
					}

					$output .= '</ul>';
				}

			if ( $paragraph_separator == 'br' ) {
				$output = str_replace( array( "</p><p>" ), "<br /><br />", $output );
				$output = str_replace( array( "<p>" ), "", $output );
			} else if ( $paragraph_separator == 'span' ) {
				$output = str_replace( array( "<p>" ), "<span>", $output );
				$output = str_replace( array( "</p>" ), "</span>", $output );				
			}
		} else if ( $type == 'image' ) {
				$output = '<img src="http://lorempixel.com/' .
					( $grayscale ? 'g/' : '' ) . $width . '/' . $height . '/' . $category . '" ' .
					( !empty( $align ) ? ' align="' . $align . '" style="padding: 15px;"' : '' ) . ' />';
			}

		return $output;
	}	
}
