<?php
/* wppa-filter.php
* Package: wp-photo-album-plus
*
* get the albums via shortcode handler
* Version 8.0.09.001
*
*/

if ( ! defined( 'ABSPATH' ) ) die( "Can't load this file directly" );

// Declare action hook actions
add_action('init', 'wppa_do_filter');

function wppa_do_filter() {

	add_filter( 'the_content', 'wppa_add_shortcode_to_post' );
}

// Add a specific shortcode at the end of a post in runtime
// The content filter must be executed ( normal priority 10 )
// before shortcode processing ( normal at priority 11 )
// for this to work
function wppa_add_shortcode_to_post( $post ) {

	$new_post = $post;
	if ( ! wppa( 'ajax' ) && wppa_switch( 'add_shortcode_to_post' ) ) {
		$id = wppa_get_the_ID();
		$p = get_post( $id, ARRAY_A );
		if ( $p['post_type'] == 'post' ) $new_post .= wppa_opt( 'shortcode_to_add' );
	}
	return $new_post;
}

// Shortcode [wppa_div style="{style specs}"][/wppa_div]
function wppa_shortcode_div( $xatts, $content = '' ) {
static $seqno;
global $wppa_current_shortcode;
global $wppa_current_shortcode_atts;

	// Init
	$wppa_current_shortcode_atts = $xatts;
	$wppa_current_shortcode = wppa_get_shortcode( 'wppa_div', $xatts );

	if ( ! $seqno ) $seqno = 0;
	$seqno++;

	$atts = shortcode_atts( array(
		'class' 		=> '',
		'nicescroll' 	=> '',
		'height' 		=> '',
		'max-height' 	=> '',
		'overflow' 		=> '',
		), $xatts );

	if ( $atts['nicescroll'] ) {
		if ( wppa_is_mobile() ) {
			$nices = false;
			$style = 'clear:both;position:relative;overflow:auto;';
			$class = $atts['class'] . ' wppa-div';
		}
		else {
			$nices = true;
			$style = 'clear:both;position:relative;overflow:hidden;';
			$class = $atts['class'] . ' wppa-div';
		}
	}
	else {
		$nices = false;
		$style = 'clear:both;position:relative;';
		if ( in_array( $atts['overflow'], array( 'hidden', 'auto', 'visible', 'scroll', 'initial', 'inherit' ) ) ) {
			$style .= 'overflow:' . $atts['overflow'] . ';';
		}
		$class = $atts['class'] . ' wppa-div';
	}
	$height = $atts['height'];
	if ( $height ) {
		$style .= 'height:' . strval( intval( $height ) ) . 'px;';
	}
	$data_maxheight = '';
	$max_height = $atts['max-height'];
	if ( $max_height ) {
		if ( $max_height > 1 ) {
			$style .= 'max-height:' . strval( intval( $max_height ) ) . 'px;';
		}
		else {
			$class .= ' wppa-autodiv';
			$data_maxheight = ' data-max-height="' . esc_attr( $max_height ) . '"';
		}
	}
	$class = trim( $class );

	$result = 	'<div
					  id="wppa-div-' . $seqno . '"
					  style="' . esc_attr( $style ) . '"
					  class="' . esc_attr( $class ) . '" ' .
					  $data_maxheight .
					  ' >' .
					( $nices ? '<div class="wppa-divnicewrap" >' : '' ) .
						do_shortcode( $content ) .
					( $nices ? '</div>': '' ) .
				'</div>';

				if ( $nices  ) {
					$result .= '
<script>
	jQuery(document).ready(function(){
		jQuery("#wppa-div-' . $seqno . '").niceScroll(".wppa-divnicewrap",{' . wppa_opt( 'nicescroll_opts' ) . '});' .
	'});
</script>';
				}

	return $result;
}

add_shortcode( 'wppa_div', 'wppa_shortcode_div' );

// The shortcode handler
function wppa_shortcodes( $xatts ) {
global $wppa;
global $wppa_postid;
global $wppa_api_version;
global $wppa_revno;
global $wppa_no_timer;
global $wpdb;
global $wppa_current_shortcode;
global $wppa_current_shortcode_atts;
global $albums_used;
global $photos_used;
global $other_deps;

	// Init
	wppa_reset_occurrance();
	$wppa_current_shortcode_atts = $xatts;
	$wppa_current_shortcode = wppa_get_shortcode( 'wppa', $xatts );
	$wppa_no_timer = false;

	$atts = shortcode_atts( [
		'type'  	=> 'generic',
		'album' 	=> '',
		'photo' 	=> '',
		'photos' 	=> '',
		'size'		=> 'auto',
		'align'		=> '',
		'taglist'	=> '',
		'cols'		=> '',
		'sub' 		=> '',
		'root' 		=> '',
		'calendar' 	=> '',
		'all' 		=> '',
		'reverse' 	=> '',
		'landing' 	=> '',
		'admin' 	=> '',
		'parent' 	=> '',
		'timeout' 	=> '',
		'button' 	=> '',
		'delay' 	=> '',
		'year' 		=> '',
		'month' 	=> '',
		'cache' 	=> '',
		'login' 	=> '',
	], $xatts );

	// Login requested?
	if ( $atts['login'] == 'yes' && ! is_user_logged_in() ) {
		wppa_bump_mocc();
		return '';
	}
	if ( $atts['login'] == 'admin' && ! wppa_user_is( 'administrator' ) ) {
		wppa_bump_mocc();
		return '';
	}

	// Sanitize input
	foreach ( array_keys( $atts ) as $key ) {
		$atts[$key] = strip_tags( $atts[$key] ); // NOT htmlspecialchars because of album="$cat,René" has allowed funny chars
	}

	// Caching?
	switch( wppa_opt( 'cache_overrule' ) ) {
		case 'always':
			$wppa['cache'] = true;
			break;
		case 'never':
			$wppa['cache'] = false;
			break;
		default:
			$wppa['cache'] = ! in_array( $atts['cache'], array( '', '0', 'off', 'no' ) );
	}

	// Void caching types
	$void_cache_types = array( 'landing',
							   'upload',
							   'search' );
	if ( in_array( $atts['type'], $void_cache_types ) ) {
		$wppa['cache'] = '0';
	}

	// Void caching virtual albums / photos
	if ( strpos( $atts['album'], '#me' ) !== false ) $wppa['cache'] = '0';
	if ( strpos( $atts['album'], '#owner' ) !== false ) $wppa['cache'] = '0';
	if ( strpos( $atts['album'], '#upldr' ) !== false ) $wppa['cache'] = '0';
	if ( strpos( $atts['photo'], '#potd' ) !== false ) $wppa['cache'] = '0';

	// Assume all toplevel albums used when generic
	if ( $wppa['cache'] && $atts['type'] == 'generic' ) {
		$temp = $wpdb->get_col( "SELECT id FROM $wpdb->wppa_albums WHERE a_parent = 0 ORDER BY id" );
		$albums_used = implode( '.', $temp );
	}

	// Find occur
	if ( wppa_get_the_ID() != $wppa_postid ) {		// New post
		$wppa['mocc'] = '0';						// Init this occurance
		$wppa['fullsize'] = '';						// Reset at each post
		$wppa_postid = wppa_get_the_ID();			// Remember the post id
	}

	// If parent given, overwrite album by children
	if ( $atts['parent'] !== '' ) {
		$temp = explode( ',', $atts['parent'] );

		// Test for virtual album
		if ( is_array( $temp ) && count( $temp ) > 1 && wppa_is_enum( $temp[1] ) ) {
			$temp[1] = wppa_alb_to_enum_children( $temp[1] );
			$atts['album'] = implode( ',', $temp );
		}
		elseif ( wppa_is_enum( $atts['parent'] ) ) {
			$atts['album'] = wppa_alb_to_enum_children( $atts['parent'] );
		}
		else {
			wppa_bump_mocc();
			$err = '<span style="color:red;" >[Error: Syntax error or unsupported parent specification: ' . $atts['parent'] . ']</span>';
			return $err;
		}
	}

	// Find type
	$type = $atts['type'];

	// Delay?
	$void_delay_types = array( 'upload',
							   'tagcloud',
							   'multitag',
							   'bestof',
							   'superview',
							   'search',
							   'supersearch',
							   'choice',
							   'stereo',
							   'grid',
							   'landing',
							   );
	$void_delay_albums = array( '#me',
								'#upldr',
								'#owner',
								'#potd',
								);

	switch( wppa_opt( 'delay_overrule' ) ) {

		case 'always':
			$atts['delay'] = 'yes';
			if ( in_array( $type, $void_delay_types ) ) {
				$atts['delay'] = '';
			}
			foreach( $void_delay_albums as $va ) {
				if ( strpos( $atts['album'], $va ) !== false ) {
					$atts['delay'] = false;
				}
			}
			break;
		case 'never':
			$atts['delay'] = '';
			break;
		default:
			if ( $atts['delay'] ) {
				if ( in_array( $type, $void_delay_types ) ) {
					wppa_you_can_not( 'delay', $type );
					$atts['delay'] = false;
				}
				foreach( $void_delay_albums as $va ) {
					if ( strpos( $atts['album'], $va ) !== false ) {
						wppa_you_can_not( 'delay', $va );
						$atts['delay'] = false;
					}
				}
			}
			break;
	}

	// Displatch on type
	switch ( $type ) {
		case 'version':
			return $wppa_api_version;
			break;
		case 'dbversion':
			return $wppa_revno;
			break;
		case 'landing':
			$wppa['is_landing'] = '1';
			break;
		case 'generic':
			break;
		case 'cover':
			$wppa['start_album'] = $atts['album'];
			$wppa['is_cover'] = '1';
			$wppa['albums_only'] = true;
			break;
		case 'album':
		case 'content':
			$wppa['start_album'] = $atts['album'];
			break;
		case 'thumbs':
			$wppa['start_album'] = $atts['album'];
			$wppa['photos_only'] = true;
			break;
		case 'covers':
			$wppa['start_album'] = $atts['album'];
			$wppa['albums_only'] = true;
			break;
		case 'slide':
			$wppa['start_album'] = $atts['album'];
			$wppa['is_slide'] = '1';
			$wppa['start_photo'] = $atts['photo'];
			if ( $atts['timeout'] ) {
				$wppa['in_widget_timeout'] = ( $atts['timeout'] == 'random' ? 'random' : strval( abs( intval( $atts['timeout'] ) ) ) );
			}
			if ( $atts['button'] ) {
				$wppa['is_button'] = esc_attr( __( $atts['button'] ) );
			}
			break;
		case 'slideonly':
			$wppa['start_album'] = $atts['album'];
			$wppa['is_slideonly'] = '1';
			$wppa['start_photo'] = $atts['photo'];
			if ( $atts['timeout'] ) {
				$wppa['in_widget_timeout'] = ( $atts['timeout'] == 'random' ? 'random' : strval( abs( intval( $atts['timeout'] ) ) ) );
			}
			break;
		case 'slideonlyf':
			$wppa['start_album'] = $atts['album'];
			$wppa['is_slideonly'] = '1';
			$wppa['is_slideonlyf'] = '1';
			$wppa['film_on'] = '1';
			$wppa['start_photo'] = $atts['photo'];
			if ( $atts['timeout'] ) {
				$wppa['in_widget_timeout'] = ( $atts['timeout'] == 'random' ? 'random' : strval( abs( intval( $atts['timeout'] ) ) ) );
			}
			break;
		case 'slidef':
			$wppa['start_album'] = $atts['album'];
			$wppa['is_slide'] = '1';
			$wppa['film_on'] = '1';
			$wppa['is_slideonly'] = '1';
			$wppa['is_filmonly'] = '1';
			$wppa['start_photo'] = $atts['photo'];
			break;
		case 'filmonly':
			$wppa['start_album'] = $atts['album'];
			$wppa['is_slideonly'] = '1';
			$wppa['is_filmonly'] = '1';
			$wppa['film_on'] = '1';
			$wppa['start_photo'] = $atts['photo'];
			break;
		case 'photo':
		case 'sphoto':
			$wppa['single_photo'] = $atts['photo'];
			$wppa['start_album'] = $atts['album'];
			break;
		case 'mphoto':
			$wppa['single_photo'] = $atts['photo'];
			$wppa['start_album'] = $atts['album'];
			$wppa['is_mphoto'] = '1';
			break;
		case 'xphoto':
			$wppa['single_photo'] = $atts['photo'];
			$wppa['start_album'] = $atts['album'];
			$wppa['is_xphoto'] = '1';
			break;
		case 'slphoto':
			$wppa['is_slide'] = '1';
			$wppa['single_photo'] = $atts['photo'];
			$wppa['start_photo'] = $atts['photo'];
			$wppa['start_album'] = $atts['album'];
			$wppa['is_single'] = '1';
			break;
		case 'autopage':
			$wppa['is_autopage'] = '1';
			break;
		case 'upload':
			if ( $atts['parent'] ) {
				$wppa['start_album'] = wppa_alb_to_enum_children( $atts['parent'] );
			}
			else {
				$wppa['start_album'] = $atts['album'];
			}
			if ( ! $wppa['start_album'] ) {
				$wppa['start_album'] = '0';
			}
			$wppa['is_upload'] = true;
			break;
		case 'multitag':
			$wppa['taglist'] = wppa_sanitize_tags($atts['taglist']);
			$wppa['is_multitagbox'] = true;
			if ( $atts['cols'] ) {
				$cols = explode( ',', $atts['cols'] );
				$col = $cols[0];
				if ( isset( $cols[1] ) && wppa_is_mobile() ) {
					$col = $cols[1];
				}
				if ( ! wppa_is_int( $col ) || $col < '1' ) $col = '2'; // On error use default
				$wppa['tagcols'] = $col;
			}
			break;
		case 'tagcloud':
			$wppa['taglist'] = wppa_sanitize_tags($atts['taglist']);
			$wppa['is_tagcloudbox'] = true;
			break;
		case 'bestof':
			$wppa['bestof'] = true;
			$wppa['bestof_args'] = $xatts;
			$photos_used = '*';
			$other_deps = 'R';
			break;
		case 'superview':
			$wppa['is_superviewbox'] = true;
			$wppa['start_album'] = $atts['album'];
			break;
		case 'search':
			$wppa['is_searchbox'] = true;
			$wppa['may_sub'] = $atts['sub'];
			if ( $atts['root'] ) {
				if ( substr( $atts['root'], 0, 1 ) == '#' ) {
					$wppa['forceroot'] = strval( intval( substr( $atts['root'], 1 ) ) );
				}
				else {
					$wppa['may_root'] = $atts['root'];
				}
			}
			$wppa['landingpage'] = $atts['landing'];
			break;
		case 'supersearch':
			$wppa['is_supersearch'] = true;
			break;
		case 'calendar':
			$wppa['is_calendar'] = true;
			$wppa['calendar'] = 'timestamp';
			if ( in_array( $atts['calendar'], array( 'exifdtm', 'timestamp', 'modified', 'realexifdtm', 'realtimestamp', 'realmodified' ) ) ) {
				$wppa['calendar'] = $atts['calendar'];
			}
			if ( $atts['delay'] && substr( $atts['calendar'], 0, 4 ) != 'real' ) {
				wppa_you_can_not( 'delay', $type . ' ' . $atts['calendar'] );
				$atts['delay'] = '';
			}
			if ( $atts['all'] ) {
				$wppa['calendarall'] = true;
			}
			$wppa['reverse'] 		= $atts['reverse'];
			$wppa['start_album'] 	= $atts['album'];
			if ( $atts['parent'] ) {
				$wppa['start_album'] = wppa_alb_to_enum_children( $atts['parent'] );
			}
			$wppa['year'] = strval( intval( $atts['year'] ) );
			$wppa['month'] = strval( intval( $atts['month'] ) );
			break;
		case 'stereo':
			$wppa['is_stereobox'] = true;
			break;
		case 'url':
			$wppa['is_url'] = true;
			$wppa['single_photo'] = $atts['photo'];
			$wppa_no_timer = true;
			break;
		case 'choice':
			$wppa['is_admins_choice'] = true;
			$wppa['admins_choice_users'] = $atts['admin'];
			break;
		case 'acount':
		case 'pcount':
			$is_acount = ( $atts['type'] == 'acount' );
			$is_pcount = ( $atts['type'] == 'pcount' );
			$is_album  = ( isset( $xatts['album'] ) && ! isset( $xatts['parent'] ) && wppa_is_enum( $xatts['album'] ) );
			$is_parent = ( isset( $xatts['parent'] ) && ! isset( $xatts['album'] ) && wppa_is_enum( $xatts['parent'] ) );
			if ( $is_album ) {
				$alb = $xatts['album'];
			}
			elseif ( $is_parent ) {
				$alb = $xatts['parent'];
			}
			else {
				$alb = '0';
			}
			if ( ! $alb || $alb < '1' ) {
				$err = '
				<span style="color:red;" >
				Error in shortcode spec for type="' . $atts['type'] . '":
				either attribute album="" or parent="" should supply a positive integer or enumeration
				</span>';
				return $err;
			}

			$albs = explode( '.', wppa_expand_enum( $alb ) );
			$total = 0;
			foreach( $albs as $alb ) {
				$counts = wppa_get_treecounts_a( $alb );
				if ( is_array( $counts ) ) {
					if ( $is_album ) {
						if ( $is_acount ) {
							$total += $counts['selfalbums'];
						}
						else {
							$total += $counts['selfphotos'];
						}
					}
					else {
						if ( $is_acount ) {
							$total += $counts['treealbums'];
						}
						else {
							$total += $counts['treephotos'];
						}
					}
				}
			}
			return $total;
			break;
		case 'share':
			$result = wppa_get_share_page_html();
			return $result;
			break;
		case 'lastupdate':
			$album = $atts['album'] ? $atts['album'] : '0';
			if ( $album ) {
				$timestamp = $wpdb->get_var( $wpdb->prepare( "SELECT timestamp FROM $wpdb->wppa_photos WHERE album = %d ORDER BY timestamp DESC LIMIT 1", $album ) );
			}
			else {
				$timestamp = $wpdb->get_var( "SELECT timestamp FROM $wpdb->wppa_photos ORDER BY timestamp DESC LIMIT 1" );
			}
			if ( $timestamp ) {
				$result = wppa_local_date( get_option( 'date_format' ), $timestamp );
				return $result;
			}
			else {
				return ( __( 'Unavailable', 'wp-photo-album-plus' ) );
			}
			break;
		case 'contest':
			$album = $atts['album'] ? $atts['album'] : '0';
			if ( strpos( $album, '..' ) !== false ) {
				$album = wppa_expand_enum( $album );
			}
			if ( ! $album ) {
				$err = '
				<span style="color:red;" >
				Error in shortcode spec for type="contest":
				Missing or invalid attribute album="<album id>"<br />
				album=' . $atts['album'] . ', parent=' . $atts['parent'] . '</span>';
				return $err;
			}
			$wppa['start_album'] = $atts['album'];
			$wppa['is_contest'] = true;
			break;
		case 'grid':
			$wppa['is_grid'] = true;
			$wppa['photos_only'] = true;
			if ( $atts['photos'] ) {
				$wppa['start_photos'] = $atts['photos'];
			}
			elseif ( $atts['album'] ) {
				$wppa['start_album'] = $atts['album'];
			}
			if ( isset( $atts['cols'] ) ) {
				$cols = explode( ',', $atts['cols'] );
				$col = $cols[0];
				if ( isset( $cols[1] ) && wppa_is_mobile() ) {
					$col = $cols[1];
				}
				if ( ! wppa_is_int( $col ) || $col < '1' ) $col = '2'; // On error use default
				$wppa['gridcols'] = $col;
			}
			break;

		default:
			wppa_dbg_msg ( 'Invalid type: ' . htmlentities( $atts['type'] ) . ' in wppa shortcode ' . $wppa_current_shortcode . '.', 'red', 'force' );
			return '';
	}

	// Count (internally to wppa_albums)

	// Find size. Assume default responsive
	if ( $atts['size'] == 'auto' ) {
		$wppa['auto_colwidth'] = true;
		$wppa['fullsize'] = '';
		$wppa['max_width'] = '';
	}
	elseif ( $atts['size'] && is_numeric( $atts['size'] ) && $atts['size'] < 1.0 ) {
		$wppa['auto_colwidth'] = true;
		$wppa['fullsize'] = $atts['size'];
	}
	elseif ( substr( $atts['size'], 0, 4 ) == 'auto' ) {
		$wppa['auto_colwidth'] = true;
		$wppa['fullsize'] = '';
		$wppa['max_width'] = substr( $atts['size'], 5 );
	}
	else {
		$wppa['auto_colwidth'] = false;
		$wppa['fullsize'] = $atts['size'];
	}

	// Find align
	$wppa['align'] = $atts['align'];

	// Delay
	if ( $atts['delay'] && ! in_array( $atts['type'], array( 'photo', 'mphoto', 'xphoto' ) ) ) {
		if ( substr( $atts['delay'], 0, 3 ) == 'yes' || substr( $atts['delay'], 0, 4 ) == 'text' || substr( $atts['delay'], 0, 6 ) == 'button' ) {
			$wppa['delay'] = $atts['delay'];
		}
	}
	elseif ( $atts['delay'] ) {
		return '<div style="color:red; font-weight:bold;" >' . __( 'Can not delay a single image shortcode', 'wp-photo-album-plus' ) . '</div>';
	}

	// Can not delay when in ajax
	if ( defined( 'DOING_AJAX' ) ) {
		$wppa['delay'] = '';
	}

	// Remember albums and photos used if caching.
	// This is used to know what caches must be cleared in cas a photo or album gets changed.
	if ( $wppa['cache'] ) {
		$albums_used = wppa_expand_enum( $wppa['start_album'] );
		$photos_used = $atts['photos'] ? wppa_expand_enum( $atts['photos'] ) : $atts['photo'];
	}
	else {
		$albums_used = '';
		$photos_used = '';
		$other_deps  = '';
	}

	// Ready to render
	$result =  wppa_albums();						// Get the HTML

	// Relative urls?
	$result = wppa_make_relative( $result );

	// Compress
	$result = wppa_compress_html( $result );

	// Done
	return $result;

}

// Declare the shortcode handler
add_shortcode( 'wppa', 'wppa_shortcodes' );

// The runtime modifiable settings are processed by the wppa_set shortcode
function wppa_set_shortcodes( $xatts, $content = '' ) {
global $wppa;
global $wppa_opt;
global $wppa_runtime_settings;

	if ( ! $wppa_runtime_settings ) {
		$wppa_runtime_settings = array();
	}

	$atts = shortcode_atts( array(
		'name' 		=> '',
		'value' 	=> ''
	), $xatts );

	// Reset?
	if ( ! $atts['name'] ) {
		$wppa_opt = false;
		wppa_initialize_runtime();
		wppa_reset_occurrance();
		$wppa_runtime_settings = array();
	}

	// Option?
	elseif ( substr( $atts['name'], 0, 5 ) == 'wppa_' ) {
		if ( isset( $wppa_opt[$atts['name']] ) ) {
			$wppa_opt[$atts['name']] = $atts['value'];
			$wppa_runtime_settings[$atts['name']] = $atts['value'];
		}
		else {
			wppa_dbg_msg( $atts['name'] . ' is not an option value.', 'red', 'force' );
		}
	}
	else {
		if ( isset( $wppa[$atts['name']] ) ) {
			$wppa[$atts['name']] = $atts['value'];
			$wppa_runtime_settings[$atts['name']] = $atts['value'];
		}
		else {
			wppa_dbg_msg( $atts['name'] . ' is not a runtime value.', 'red', 'force' );
		}
	}
}

// Enable wppa_set shortcode conditionally
if ( wppa_get_option( 'wppa_enable_shortcode_wppa_set', 'no' ) == 'yes' ) {
	add_shortcode( 'wppa_set', 'wppa_set_shortcodes' );
}

// Declare the simple photo shortcode handler optionally
add_action( 'init', 'wppa_add_photo_shortcode' );

function wppa_add_photo_shortcode() {
	if ( wppa_switch( 'photo_shortcode_enabled' ) ) {
		add_shortcode( 'photo', 'wppa_photo_shortcodes' );
	}
}

function wppa_photo_shortcodes( $xatts ) {
global $wppa;
global $wppa_postid;
global $wpdb;
static $seed;
global $wppa_current_shortcode;
global $wppa_current_shortcode_atts;
global $photos_used;

	// Init
	wppa_reset_occurrance();
	$wppa_current_shortcode_atts = $xatts;
	$wppa_current_shortcode = wppa_get_shortcode( 'photo', $xatts );

	// Get and validate photo id
	if ( isset( $xatts[0] ) ) {
		$photo = $xatts[0];
		if ( is_numeric( $photo ) && ! wppa_photo_exists( $photo ) ) {
			return sprintf( __( 'Photo %d does not exist', 'wp-photo-album-plus' ), $photo );
		}
	}
	else {
		return __( 'Missing photo id', 'wp-photo-album-plus' );
	}

	// Find occur
	if ( wppa_get_the_ID() != $wppa_postid ) {		// New post
		$wppa['mocc'] = '0';					// Init this occurance
		$wppa['fullsize'] = '';					// Reset at each post
		$wppa_postid = wppa_get_the_ID();			// Remember the post id
	}

	// Random photo?
	if ( $wppa_postid && $photo == 'random' ) {

		if ( ! $seed ) {
			$seed = time();
		}
		$seed = floor( $seed * 0.9 );

		if ( wppa_opt( 'photo_shortcode_random_albums' ) != '-2' ) {
			$albs  = str_replace( '.', ',', wppa_expand_enum( wppa_opt( 'photo_shortcode_random_albums' ) ) );
			$photo = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->wppa_photos
													  WHERE album IN (" . $albs . ")
													  ORDER BY RAND(%d)
													  LIMIT 1", $seed ) );
		}
		else {
			$photo = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->wppa_photos
													  ORDER BY RAND(%d)
													  LIMIT 1", $seed ) );
		}
		if ( $photo ) {
			if ( wppa_switch( 'photo_shortcode_random_fixed' ) ) {
				$post_content = $wpdb->get_var( $wpdb->prepare( "SELECT post_content
																 FROM $wpdb->posts
																 WHERE ID = %d", $wppa_postid ) );
				if ( wppa_switch( 'photo_shortcode_random_fixed_html' ) ) {
					$post_content = preg_replace( '/\[photo random\]/', do_shortcode('[photo '.$photo.']'), $post_content, 1, $done );
				}
				else {
					$post_content = preg_replace( '/\[photo random\]/', '[photo '.$photo.']', $post_content, 1, $done );
				}
				$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts
											   SET post_content = %s
											   WHERE ID = %d", $post_content, $wppa_postid ) );
			}
		}
		else {
			return __( 'No random photo found', 'wp-photo-album-plus' );
		}
	}

	// Get configuration settings
	$type 	= wppa_opt( 'photo_shortcode_type' ); // 'xphoto';
	$size 	= wppa_opt( 'photo_shortcode_size' ); // '350';
	$align 	= wppa_opt( 'photo_shortcode_align' ); //'left';

	switch ( $type ) {
		case 'photo':
		case 'sphoto':
			$wppa['single_photo'] 	= $photo;
			break;
		case 'mphoto':
			$wppa['single_photo'] 	= $photo;
			$wppa['is_mphoto'] 		= '1';
			break;
		case 'xphoto':
			$wppa['single_photo'] 	= $photo;
			$wppa['is_xphoto'] 		= '1';
			break;
		case 'slphoto':
			$wppa['is_slide'] 		= '1';
			$wppa['single_photo'] 	= $photo;
			$wppa['start_photo'] 	= $photo;
			$wppa['is_single'] 		= '1';
			break;
		default:
			wppa_log( 'err', "Unimplemented photo_shortcode_type: $type in wppa_photo_shortcodes()" );
			break;
	}

	// Process size
	if ( $size && is_numeric( $size ) && $size < 1.0 ) {
		$wppa['auto_colwidth'] 		= true;
		$wppa['fullsize'] 			= $size;
	}
	elseif ( substr( $size, 0, 4 ) == 'auto' ) {
		$wppa['auto_colwidth'] 		= true;
		$wppa['fullsize'] 			= '';
		$wppa['max_width'] 			= substr( $size, 5 );
	}
	else {
		$wppa['auto_colwidth'] 		= false;
		$wppa['fullsize'] 			= $size;
	}

	// Find align
	$wppa['align'] = $align;

	// Cache?
	if ( isset( $xatts['cache'] ) ) {
		$photos_used = $photo;
		$wppa['cache'] = ! in_array( $xatts['cache'], array( '', '0', 'off', 'no' ) );
	}

	// Delay
	if ( isset( $xatts['delay'] ) ) {
		wppa_you_can_not( 'delay', 'single image' );
		$xatts['delay'] = '';
	}

	return wppa_albums();
}

// Yuo can not cache/delay a type xxx shortocde
function wppa_you_can_not( $xaction, $xtype, $useless = true ) {
	$action = __( $xaction, 'wp-photo-album-plus' );
	$type   = __( $xtype, 'wp-photo-album-plus' );
	$result = sprintf( __( 'You can not %1s a %2s shortcode display.', 'wp-photo-album-plus' ), $action, $type ) .
			  ( $useless ? ' ' . __( 'It is useless anyway.', 'wp-photo-album-plus' ) : '' );
	wppa_log( 'dbg', $result );
	return $result;
}

// This function is no longer needed in 8.0
function wppa_insert_shortcode_output( $result ) {
	wppa_log( 'err', 'wppa_insert_shortcode_output() is deprecated and no longer needed' );
	return $result;
}
