'widget_500px', 'title' => '500px', 'consumer_key' => '5JwOJabC89Cb5uvgHmCJgYDAGXG9TwJ5fjOEg9Pk', 'feature' => 1, 'feature_username' => '', 'feature_tag' => '', 'category' => -1, 'sort_by' => 1, 'count' => 6, 'thumb_size' => 1, 'cache_lifetime' => 3600, 'one_element_html' => "\n\"{photo_title}\"\n", 'categories' => array( '-1' => 'Any', '0' => 'Uncategorized', '10' => 'Abstract', '11' => 'Animals', '5' => 'Black and White', '1' => 'Celebrities', '9' => 'City and Architecture', '15' => 'Commercial', '16' => 'Concert', '20' => 'Family', '14' => 'Fashion', '2' => 'Film', '24' => 'Fine Art', '23' => 'Food', '3' => 'Journalism', '8' => 'Landscapes', '12' => 'Macro', '18' => 'Nature', '4' => 'Nude', '7' => 'People', '19' => 'Performing Arts', '17' => 'Sport', '6' => 'Still Life', '21' => 'Street', '26' => 'Transporation', '13' => 'Travel', '22' => 'Underwater', '27' => 'Urban Exploration', '25' => 'Wedding', ), ); /////////////////////////////////////////////////////////////////////////// /** * Register widget with WordPress. */ public function __construct() { parent::__construct( $this->defaults['widget_id'], '500px Widget', array( 'description' => 'Displays photos from 500px.com', 'classname' => $this->defaults['widget_id'], ), array( 'width' => 400, 'height' => 700, ) ); } /////////////////////////////////////////////////////////////////////////// /** * Front-end display of widget. * * @see WP_Widget::widget() * * @param array $args Widget arguments. * @param array $instance Saved values from database. */ public function widget( $args, $instance ) { /////////////////////////////////////////////////////////////////////// // args $args = array_merge( $this->defaults, $args ); // cache key $cache_key = $this->defaults['widget_id'].'_'.dechex(crc32( $args['widget_id'] )); // try to get cached data from transient cache $html = get_transient( $cache_key ); if( empty($html) ) { $html = $args['before_widget']; if( !empty( $instance['title'] ) ) { $html .= $args['before_title'].$instance['title'].$args['after_title']; } $photos = $this->getPhotos( $instance ); if( empty($photos) ) { return false; } if( is_array($photos) ) { $html .= $this->getHTML( $photos, $instance ); } else { $html .= ''.$photos.''; // its error } $html .= $args['after_widget']; if( is_array($photos) ) // if not error { // store result to cache set_transient( $cache_key, $html, $instance['cache_lifetime'] ); } } echo( $html ); /////////////////////////////////////////////////////////////////////// } /////////////////////////////////////////////////////////////////////////// /** * Returns Photos * * @param array $args * @return array */ public function getPhotos( $args = array() ) { /////////////////////////////////////////////////////////////////////// $url = 'https://api.500px.com/v1/photos'; /////////////////////////////////////////////////////////////////////// switch( $args['sort_by'] ) { case 1: // Time of upload (Most recent first) default: $url_sort = 'created_at'; break; case 2: // Rating (Highest rated first) $url_sort = 'rating'; break; case 3: // View count (Most viewed first) $url_sort = 'times_viewed'; break; case 4: // Votes count (Most voted first) $url_sort = 'votes_count'; break; case 5: // Favorites count (Most favorited first) $url_sort = 'favorites_count'; break; case 6: // Comments count (Most commented first) $url_sort = 'comments_count'; break; case 7: // Original date (Most recent first) $url_sort = 'taken_at'; break; } /////////////////////////////////////////////////////////////////////// #$url_params .= '?consumer_key='.$args['consumer_key'].'&sort='.$url_sort.'&rpp='.$args['count'].'&image_size='.$args['thumb_size']; $url_params = '?consumer_key='.$this->defaults['consumer_key'].'&sort='.$url_sort.'&rpp='.$args['count'].'&image_size='.$args['thumb_size']; /////////////////////////////////////////////////////////////////////// if( $args['feature']<10 && $args['category']>=0 && isset($this->defaults['categories'][$args['category']]) ) { $url_params .= '&only='.urlencode( $this->defaults['categories'][$args['category']] ); } /////////////////////////////////////////////////////////////////////// switch( $args['feature'] ) { case 1: // Popular Photos default: $url .= $url_params.'&feature=popular'; break; case 2: // Upcoming Photos $url .= $url_params.'&feature=upcoming'; break; case 3: // Editors' Choice Photos $url .= $url_params.'&feature=editors'; break; case 4: // Fresh Today Photos $url .= $url_params.'&feature=fresh_today'; break; case 5: // Fresh Yesterday Photos $url .= $url_params.'&feature=fresh_yesterday'; break; case 6: // Fresh This Week Photos $url .= $url_params.'&feature=fresh_week'; break; case 7: // User Photos $url .= $url_params.'&feature=user&username='.$args['feature_username']; break; case 8: // User Friends Photos $url .= $url_params.'&feature=user_friends&username='.$args['feature_username']; break; case 9: // User Favorites Photos $url .= $url_params.'&feature=user_favorites&username='.$args['feature_username']; break; case 10: // Tag Photos $url .= '/search'.$url_params.'&tag='.urlencode($args['feature_tag']); break; } /////////////////////////////////////////////////////////////////////// $data = wp_remote_get( $url, array( 'timeout' => 2 ) ); if( !empty($data) ) { if( is_object($data) ) { return $data->get_error_message(); } else if( isset($data['body']) && !empty($data['body']) ) { return json_decode( $data['body'], true ); } } return false; /////////////////////////////////////////////////////////////////////// } /////////////////////////////////////////////////////////////////////////// /** * Returns HTML of photos * * @param array $photos * @param array $args * @return string */ public function getHTML( $photos = array(), $args = array() ) { /////////////////////////////////////////////////////////////////////// if( empty($photos) || !isset($photos['photos']) || empty($photos['photos']) ) { return false; } /////////////////////////////////////////////////////////////////////// // args $args = array_merge( $this->defaults, $args ); switch( $args['thumb_size'] ) { case 1: default: $width = $height = '70'; break; case 2: $width = $height = '140'; break; case 3: $width = $height = '280'; break; case 4: $width = ''; $height = ''; break; } /////////////////////////////////////////////////////////////////////// $html = ''; foreach( $photos['photos'] as $photo ) { $html .= str_ireplace( array( '{photo_title}', '{photo_url}', '{photo_image_url}', '{photo_width}', '{photo_height}', ), array( $photo['name'], 'http://500px.com/photo/'.$photo['id'], $photo['image_url'], $width, $height, ), $args['one_element_html'] ); /* $html .= ''. ''. ''.esc_attr($photo['name']).''. ''. ' '; */ } return $html; /////////////////////////////////////////////////////////////////////// } /////////////////////////////////////////////////////////////////////////// /** * Clear transient widget cache * * @return bool */ public function clearCache() { /////////////////////////////////////////////////////////////////////// global $wpdb; $q = ' SELECT option_name as name FROM '.$wpdb->options.' WHERE option_name LIKE \'_transient_'.$this->defaults['widget_id'].'_%\''; $transients = $wpdb->get_results($q); if( !empty($transients) ) { foreach( $transients as $transient ) { delete_transient( str_replace( '_transient_', '', $transient->name ) ); } } return true; /////////////////////////////////////////////////////////////////////// } /////////////////////////////////////////////////////////////////////////// /** * Sanitize widget form values as they are saved. * * @see WP_Widget::update() * * @param array $new_instance Values just sent to be saved. * @param array $old_instance Previously saved values from database. * * @return array Updated safe values to be saved. */ public function update( $new_instance, $old_instance ) { /////////////////////////////////////////////////////////////////////// // drop cache $this->clearCache(); /////////////////////////////////////////////////////////////////////// // return sanitized data return array( 'title' => trim( strip_tags( $new_instance['title'], '
' ) ), #'consumer_key' => trim( preg_replace( '#[^0-9A-Za-z]#', '', strip_tags( $new_instance['consumer_key'] ) ) ), 'feature' => intval( preg_replace( '#[^0-9]#', '', $new_instance['feature'] ) ), 'feature_username' => trim( strip_tags( $new_instance['feature_username'] ) ), 'feature_tag' => trim( strip_tags( $new_instance['feature_tag'] ) ), 'category' => intval( preg_replace( '#[^0-9\-]#', '', $new_instance['category'] ) ), 'sort_by' => intval( preg_replace( '#[^0-9]#', '', $new_instance['sort_by'] ) ), 'count' => intval( preg_replace( '#[^0-9]#', '', $new_instance['count'] ) ), 'thumb_size' => intval( preg_replace( '#[^0-9]#', '', $new_instance['thumb_size'] ) ), 'cache_lifetime' => intval( preg_replace( '#[^0-9]#', '', $new_instance['cache_lifetime'] ) ), 'one_element_html' => trim( $new_instance['one_element_html'] ), ); /////////////////////////////////////////////////////////////////////// } /////////////////////////////////////////////////////////////////////////// /** * Back-end widget form. * * @see WP_Widget::form() * * @param array $instance Previously saved values from database. */ public function form( $instance ) { /////////////////////////////////////////////////////////////////////// // defaults $title = $this->defaults['title']; #$consumer_key = $this->defaults['consumer_key']; $feature = $this->defaults['feature']; $feature_username = $this->defaults['feature_username']; $feature_tag = $this->defaults['feature_tag']; $category = $this->defaults['category']; $sort_by = $this->defaults['sort_by']; $count = $this->defaults['count']; $thumb_size = $this->defaults['thumb_size']; $cache_lifetime = $this->defaults['cache_lifetime']; $one_element_html = $this->defaults['one_element_html']; /////////////////////////////////////////////////////////////////////// // set values if( isset($instance['title']) && strlen($instance['title'])>1 ) { $title = $instance['title']; } #if( isset($instance['consumer_key']) && strlen($instance['consumer_key'])>1 ) #{ #$consumer_key = $instance['consumer_key']; #} if( isset($instance['feature']) && intval($instance['feature'])>0 ) { $feature = intval($instance['feature']); } if( isset($instance['feature_username']) && strlen($instance['feature_username'])>1 ) { $feature_username = $instance['feature_username']; } if( isset($instance['feature_tag']) && strlen($instance['feature_tag'])>1 ) { $feature_tag = $instance['feature_tag']; } if( isset($instance['category']) ) { $category = intval($instance['category']); } if( isset($instance['sort_by']) && intval($instance['sort_by'])>0 ) { $sort_by = intval($instance['sort_by']); } if( isset($instance['count']) && intval($instance['count'])>0 ) { $count = intval($instance['count']); } if( isset($instance['thumb_size']) && intval($instance['thumb_size'])>0 ) { $thumb_size = intval($instance['thumb_size']); } if( isset($instance['cache_lifetime']) && intval($instance['cache_lifetime'])>0 ) { $cache_lifetime = intval($instance['cache_lifetime']); } if( isset($instance['one_element_html']) && strlen($instance['one_element_html'])>1 ) { $one_element_html = $instance['one_element_html']; } /////////////////////////////////////////////////////////////////////// $temp_select_categories = ''; foreach( $this->defaults['categories'] as $category_id => $category_title ) { $temp_select_categories .= ''; } /////////////////////////////////////////////////////////////////////// // html echo( ''. '
'. '

'. ''. ''. '

'. #'

'. #'500px applications):'. #''. #'

'. '

'. ''. ''. '

'. '

6 && $feature<10 ? ' style="display:block;"' : ' style="display:none;"' ).'>'. ''. ''. '

'. '

'. ''. ''. '

'. '

'. ''. ''. '

'. '

'. ''. ''. '

'. '

'. ''. ''. '

'. '

'. ''. ''. '

'. '

'. ''. ''. '

'. '

'. ''. ''. 'You can use this placeholders:'. '

    '. '
  • {photo_title} - Photo title on 500px.com
  • '. '
  • {photo_url} - URL to photo on 500px.com
  • '. '
  • {photo_image_url} - URL to photo image on 500px.com
  • '. '
  • {photo_width} - Thumbnail width (in px).
      Recommended to remove attribute for Thumb Size: 900x/x900
  • '. '
  • {photo_height} - Thumbnail height (in px).
      Recommended to remove attribute for Thumb Size: 900x/x900
  • '. '
'. '

'. '

'. 'I forgot something? You can write to me!'. '

'. '
' ); /////////////////////////////////////////////////////////////////////// } /////////////////////////////////////////////////////////////////////////// } /////////////////////////////////////////////////////////////////////////////// // register 500px Widget add_action( 'widgets_init', create_function( '', 'register_widget( "Widget_500px" );' ) ); ///////////////////////////////////////////////////////////////////////////////