options ################
class PhotoTileForFlickrTertiary extends PhotoTileForFlickrSecondary{
// For Reference:
// http://www.flickr.com/services/api/response.json.html
// sq = thumbnail 75x75
// t = 100 on longest side
// s = 240 on longest side
// n = 320 on longest side
// m = 500 on longest side
// z = 640 on longest side
// c = 800 on longest side
// b = 1024 on longest side*
// o = original image, either a jpg, gif or png, depending on source format**
// *Before May 25th 2010 large photos only exist for very large original images.
// **Original photos behave a little differently. They have their own secret (called originalsecret in responses) and a variable file extension (called originalformat in responses). These values are returned via the API only when the caller has permission to view the original size (based on a user preference and various other criteria). The values are returned by the flickr.photos.getInfo method and by any method that returns a list of photos and allows an extras parameter (with a value of original_format), such as flickr.photos.search. The flickr.photos.getSizes method, as always, will return the full original URL where permissions allow.
//////////////////////////////////////////////////////////////////////////////////////
////////////////// Unique Feed Fetch Functions /////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
/**
* Alpine PhotoTile for Flickr: Photo Retrieval Function.
* The PHP for retrieving content from Flickr.
*
* @ Since 1.0.0
* @ Updated 1.2.4
*/
function photo_retrieval(){
$flickr_options = $this->options;
$defaults = $this->option_defaults();
$key_input = array(
'name' => 'flickr',
'info' => array(
'vers' => $this->vers,
'src' => $flickr_options['flickr_source'],
'uid' => $flickr_options['flickr_user_id'],
'groupid' => $flickr_options['flickr_group_id'],
'set' => $flickr_options['flickr_set_id'],
'tags' => $flickr_options['flickr_tags'],
'num' => $flickr_options['flickr_photo_number'],
'off' => $flickr_options['photo_feed_offset'],
'link' => $flickr_options['flickr_display_link'],
'text' => $flickr_options['flickr_display_link_text'],
'size' => $flickr_options['flickr_photo_size']
)
);
$key = $this->key_maker( $key_input ); // Make Key
if( $this->retrieve_from_cache( $key ) ){ return; } // Check Cache
$this->set_size_id(); // Set image size (translate size to Flickr id)
//$this->options['api_key'] = '68b8278a33237f1f369cbbf3c9a9f45c';
if( $this->options['api_key'] ){
$this->hidden .= '';
}else{
$this->hidden .= '';
}
if( function_exists('unserialize') ) {
$this->try_php_serial();
}
if ( !($this->success) && function_exists('simplexml_load_file') ) {
if( $this->options['api_key'] ){
$this->try_rest();
}else{
// Use my API key
$this->hidden .= '';
$this->options['api_key'] = '68b8278a33237f1f369cbbf3c9a9f45c';
$this->try_rest();
}
}
if( $this->success ){
$this->make_display_link();
}else{
if( $this->feed_found ){
$this->message .= '- Flickr feed was successfully retrieved, but no photos found.';
}else{
$this->message .= '- Please recheck your ID(s).';
}
}
$this->results = array('continue'=>$this->success,'message'=>$this->message,'hidden'=>$this->hidden,'photos'=>$this->photos,'user_link'=>$this->userlink);
$this->store_in_cache( $key ); // Store in cache
}
/**
* Function for forming Flickr request
*
* @ Since 1.2.4
*/
function get_flickr_request($format){
$options = $this->options;
$offset = ($options['photo_feed_offset']?$options['photo_feed_offset']:0);
$num = $offset + $options['flickr_photo_number'];
if( $options['photo_feed_shuffle'] && function_exists('shuffle') ){ // Shuffle the results
$num = min( 200, $num*6 );
}
$flickr_uid = apply_filters( $this->hook, empty($options['flickr_user_id']) ? 'uid' : $options['flickr_user_id'], $options );
$request = false;
if( !empty( $options['api_key'] ) ){
$key = $options['api_key'];
switch ($options['flickr_source']) {
case 'user':
$request = 'http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key='.$key.'&per_page='.$num.'&format='.$format.'&privacy_filter=1&user_id='. $flickr_uid .'&page=1&extras=description,url_sq,url_t,url_s,url_m,url_n,url_z,url_c';
break;
case 'favorites':
$request = 'http://api.flickr.com/services/rest/?method=flickr.favorites.getPublicList&api_key='.$key.'&per_page='.$num.'&format='.$format.'&privacy_filter=1&user_id='. $flickr_uid .'&page=1&extras=description,url_sq,url_t,url_s,url_m,url_n,url_z,url_c';
break;
case 'group':
$flickr_groupid = apply_filters( $this->hook, empty($options['flickr_group_id']) ? '' : $options['flickr_group_id'], $options );
$request = 'http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key='.$key.'&per_page='.$num.'&format='.$format.'&privacy_filter=1&group_id='. $flickr_groupid .'&page=1&extras=description,url_sq,url_t,url_s,url_m,url_n,url_z,url_c';
break;
case 'set':
$flickr_set = apply_filters( $this->hook, empty($options['flickr_set_id']) ? '' : $options['flickr_set_id'], $options );
$request = 'http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key='.$key.'&per_page='.$num.'&format='.$format.'&privacy_filter=1&photoset_id='. $flickr_set .'&page=1&extras=description,url_sq,url_t,url_s,url_m,url_n,url_z,url_c,url_o'; // API claims no n, z, or c. Add o to cover missing sizes
break;
case 'community':
$flickr_tags = apply_filters( $this->hook, empty($options['flickr_tags']) ? '' : $options['flickr_tags'], $options );
$request = 'http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key='.$key.'&per_page='.$num.'&format='.$format.'&privacy_filter=1&tags='. $flickr_tags .'&page=1&extras=description,url_sq,url_t,url_s,url_m,url_n,url_z,url_c';
break;
}
}else{
switch ($options['flickr_source']) {
case 'user':
$request = 'http://api.flickr.com/services/feeds/photos_public.gne?id='. $flickr_uid .'&lang=en-us&format='.$format.'';
break;
case 'favorites':
$request = 'http://api.flickr.com/services/feeds/photos_faves.gne?nsid='. $flickr_uid .'&lang=en-us&format='.$format.'';
break;
case 'group':
$flickr_groupid = apply_filters( $this->hook, empty($options['flickr_group_id']) ? '' : $options['flickr_group_id'], $options );
$request = 'http://api.flickr.com/services/feeds/groups_pool.gne?id='. $flickr_groupid .'&lang=en-us&format='.$format.'';
break;
case 'set':
$flickr_set = apply_filters( $this->hook, empty($options['flickr_set_id']) ? '' : $options['flickr_set_id'], $options );
$request = 'http://api.flickr.com/services/feeds/photoset.gne?set=' . $flickr_set . '&nsid='. $flickr_uid .'&lang=en-us&format='.$format.'';
break;
case 'community':
$flickr_tags = apply_filters( $this->hook, empty($options['flickr_tags']) ? '' : $options['flickr_tags'], $options );
$request = 'http://api.flickr.com/services/feeds/photos_public.gne?tags='. $flickr_tags .'&lang=en-us&format='.$format.'';
break;
}
}
return $request;
}
/**
* Determine image size id
*
* @ Since 1.2.4
*/
function set_size_id(){
$this->options['size_id'] = '.'; // Default is 500
switch ($this->options['flickr_photo_size']) {
case 75:
$this->options['size_id'] = 'url_sq';
break;
case 100:
$this->options['size_id'] = 'url_t';
break;
case 240:
$this->options['size_id'] = 'url_s';
break;
case 320:
$this->options['size_id'] = 'url_n';
break;
case 500:
$this->options['size_id'] = 'url_m';
break;
case 640:
$this->options['size_id'] = 'url_z';
break;
case 800:
$this->options['size_id'] = 'url_c';
break;
}
}
/**
* Function getting image url given size setting
*
* @ Since 1.2.2
* @ Updated 1.2.4
*/
function get_image_url($info){
$size = $this->options['size_id'];
if( !empty( $this->options['api_key'] ) ){
if( isset($info[$size]) ){
return $info[$size];
}elseif( 'url_c' == $size && (isset($info['url_o']) && ($info['height_o']<1200 && $info['width_o']<1200) ) ){ // Checking url_o is same as src==set
return $info['url_o'];
}elseif( 'url_c' == $size && isset($info['url_z']) ){
return $info['url_z'];
}elseif( isset($info['url_m']) ){
return $info['url_m'];
}elseif( isset($info['url_n']) ){
return $info['url_n'];
}
}else{
if( ('url_s' == $size || 'url_t' == $size) && isset($info['m_url']) ){ // Checking url_o is same as src==set
return $info['m_url'];
}elseif( ('url_sq' == $size) && isset($info['thumb_url']) ){ // Checking url_o is same as src==set
return $info['thumb_url'];
}elseif( ('url_n' == $size || 'url_m' == $size) && isset($info['l_url']) ){ // Checking url_o is same as src==set
return $info['l_url'];
}elseif( ('url_z' == $size || 'url_c' == $size )&& isset($info['photo_url']) ){
return $info['photo_url'];
}
}
return false;
}
/**
* Function getting original image url given size setting
*
* @ Since 1.2.2
* @ Updated 1.2.4
*/
function get_image_orig($info){
$size = $this->options['size_id'];
if( !empty( $this->options['api_key'] ) ){
if( isset($info['url_c']) ){
return $info['url_c'];
}elseif( isset($info['url_o']) && $info['height_o']<1200 && $info['width_o']<1200 ){
return $info['url_o'];
}elseif( isset($info['url_z']) ){ // Checking url_o is same as src==set
return $info['url_z'];
}elseif( isset($info['url_m']) ){
return $info['url_m'];
}
}else{
if( isset($info['photo_url']) ){
return $info['photo_url'];
}elseif( isset($info['l_url']) ){
return $info['l_url'];
}elseif( isset($info['m_url']) ){
return $info['m_url'];
}
}
return false;
}
/**
* Function for making Flickr request with php_serial return format ( API v1 and v2 )
*
* @ Since 1.2.4
*/
function try_php_serial(){
// Retrieve content using wp_remote_get and PHP_serial
$request = $this->get_flickr_request('php_serial');
$_flickr_php = array();
$response = wp_remote_get($request,
array(
'method' => 'GET',
'timeout' => 20,
)
);
if( is_wp_error( $response ) || !isset($response['body']) ) {
$this->hidden .= '';
}else{
$_flickr_php = @unserialize($response['body']);
}
if( empty($_flickr_php) || (!$_flickr_php['photos'] && !$_flickr_php['photoset'] && !$_flickr_php['items']) ){
$this->hidden .= '';
if( $_flickr_php['message'] ){
$this->message .= '- Attempt 1: '.$_flickr_php['message'].'
';
}else{
$this->message .= '- Attempt 1: Flickr feed not found
';
}
$this->success = false;
}else{
if( !empty( $this->options['api_key'] ) ){
$this->parse_php_serial_v2($_flickr_php);
}else{
$this->parse_php_serial_v1($_flickr_php);
}
if(!empty($this->photos) ){
$this->success = true;
$this->hidden .= '';
}else{
$this->success = false;
$this->feed_found = true;
$this->hidden .= '';
}
}
}
/**
* Function for parsing results in php_serial format ( API v2 )
*
* @ Since 1.2.4
*/
function parse_php_serial_v2($_flickr_php){
$content = $_flickr_php['photos'];
$photos = $_flickr_php['photos']['photo'];
// Check for photosets
if( 'set' == $this->options['flickr_source']) {
$content = $_flickr_php['photoset'];
$photos = $_flickr_php['photoset']['photo'];
}
if( is_array( $photos ) ){
// Remove offset
for($j=0;$j<$this->options['photo_feed_offset'];$j++){
if( !empty( $photos ) ){
array_shift( $photos );
}
}
foreach( $photos as $info ){
$the_photo = array();
$the_photo['image_link'] = 'http://www.flickr.com/photos/'.($info['owner']?$info['owner']:$this->options['flickr_user_id']).'/'.$info['id'].'/';
$the_photo['image_title'] = (string) @str_replace('"','', @str_replace("'","\'",$info['title']) );
$the_photo['image_caption'] = (string) $info['description']['_content'];
$the_photo['image_caption'] = str_replace("'","\'",$the_photo['image_caption'] );
$the_photo['image_source'] = (string) $this->get_image_url($info);
$the_photo['image_original'] = (string) $this->get_image_orig($info);
$this->photos[] = $the_photo;
}
}
$this->set_user_link($content);
}
/**
* Function for parsing results in php_serial format ( API v1 )
*
* @ Since 1.2.4
*/
function parse_php_serial_v1($_flickr_php){
$this->userlink = $_flickr_php['url']; // Store userlink for later
$content = $_flickr_php['items'];
if( is_array( $content ) ){
foreach( $content as $info ){
$the_photo = array();
$the_photo['image_link'] = (string) $info['url'];
$the_photo['image_title'] = (string) @str_replace('"','', @str_replace("'","\'",$info['title']) );
$the_photo['image_caption'] = (string) $info['description_raw']; // retrieve image title
$the_photo['image_caption'] = str_replace("'","\'",$the_photo['image_caption'] );
$the_photo['image_source'] = (string) $this->get_image_url($info);
$the_photo['image_original'] = (string) $this->get_image_orig($info);
$this->photos[] = $the_photo;
}
}
}
/**
* Function for making flickr request with xml return format ( API v2 )
*
* @ Since 1.2.4
*/
function try_rest(){
$request = $this->get_flickr_request('rest');
$_flickrurl = @urlencode( $request ); // just for compatibility
$_flickr_xml = @simplexml_load_file( $_flickrurl,"SimpleXMLElement",LIBXML_NOCDATA); // @ is shut-up operator
if( $_flickr_xml === false || !$_flickr_xml || (!$_flickr_xml->photos && !$_flickr_xml->photoset) ){
$this->hidden .= '';
if( $_flickr_xml->err['msg'] ){
$this->message .= '- Attempt 2: '.$_flickr_xml->err['msg'].'
';
}
$this->success = false;
}else{
$this->parse_rest_v2($_flickr_xml);
if(!empty($this->photos) ){
$this->success = true;
$this->hidden .= '';
}else{
$this->success = false;
$this->feed_found = true;
$this->hidden .= '';
}
}
}
/**
* Function for parsing results in xml format ( API v2 )
*
* @ Since 1.2.4
*/
function parse_rest_v2($_flickr_xml){
$_flickr_xml = $this->xml2array($_flickr_xml);
$content = $_flickr_xml['photos'];
$photos = $content['photo'];
// Check for photosets
if( 'set' == $this->options['flickr_source']) {
$content = $_flickr_xml['photoset'];
$photos = $content['photo'];
}
if( is_array( $photos ) ){
// Remove offset
for($j=0;$j<$this->options['photo_feed_offset'];$j++){
if( !empty( $photos ) ){
array_shift( $photos );
}
}
foreach( $photos as $info ){ // $photos not indexed with ints
$the_photo = array();
if( is_array( $info['description'] ) ){
$the_photo['image_caption'] = "";
}else{
$the_photo['image_caption'] = (string) $info['description'];
$the_photo['image_caption'] = str_replace("'","\'",$the_photo['image_caption'] );
}
$info = $info['@attributes'];
$the_photo['image_link'] = (string) 'http://www.flickr.com/photos/'.($info['owner']?$info['owner']:$this->options['flickr_user_id']).'/'.$info['id'].'/';
$the_photo['image_title'] = (string) @str_replace('"','', @str_replace("'","\'",$info['title']) );
$the_photo['image_source'] = (string) $this->get_image_url($info);
$the_photo['image_original'] = (string) $this->get_image_orig($info);
$this->photos[] = $the_photo;
}
}
$this->set_user_link($content);
}
/**
* Convert SimpleXMLObject to PHP Array
*
* @ Since 1.2.4
*/
function xml2array ( $input, $out = array () ){
foreach ( (array) $input as $index => $node ){
$out[$index] = ( is_object ( $node ) || is_array ( $node ) ) ? $this->xml2array ( $node ) : $node;
}
return $out;
}
function set_user_link($content){
if( 'community' != $this->options['flickr_source'] && $this->options['flickr_display_link'] && $this->options['flickr_display_link_text']) {
switch ($this->options['flickr_source']) {
case 'user':
$this->userlink = 'http://www.flickr.com/photos/'.$this->options['flickr_user_id'].'/';
break;
case 'favorites':
$this->userlink = 'http://www.flickr.com/photos/'.$this->options['flickr_user_id'].'/favorites/';
break;
case 'group':
$this->userlink = 'http://www.flickr.com/groups/'.$this->options['flickr_group_id'].'/';
break;
case 'set':
if($content['owner'] && $content['id']){
$this->userlink = 'http://www.flickr.com/photos/'.$content['owner'].'/sets/'.$content['id'].'/';
}
break;
}
}
}
//////////////////////////////////////////////////////////////////////////////////////
//////////////////// Unique Admin Functions ////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
/**
* Alpine PhotoTile: Options Page
*
* @ Since 1.1.1
* @ Updated 1.2.4
*/
function build_settings_page(){
$currenttab = $this->get_current_tab();
echo '
info.'" target="_blank">let me know if these directions become outdated.');?>