Skip to content

Instantly share code, notes, and snippets.

@setola
Created July 9, 2012 16:15
Show Gist options
  • Save setola/3077392 to your computer and use it in GitHub Desktop.
Save setola/3077392 to your computer and use it in GitHub Desktop.
Portfolio image list
<?php
/**
* Manage the portfolio feature
* @author etessore
*
*/
class portfolio{
const url_field_name = 'url';
const customer_box_id = 'customer-box';
const client_custom_post_name = 'clients';
/**
* Set the portfolio up.
* If this was a plugin this method should be called on enable
*/
public function setup(){
$this->register_custom_posts()->hook()->thumbnail_sizes();
}
/**
* Manages various hooks to wordpress
* @return $this for chainability
*/
public function hook(){
add_action('admin_init', array(__CLASS__, 'add_metaboxes'));
add_shortcode('portfolio-thumb', array(__CLASS__, 'portfolio_thumb'));
return $this;
}
/**
* Manages images sizes for the portfolio
* @return $this for chainability
*/
public function thumbnail_sizes(){
add_theme_support('post-thumbnails');
add_image_size('portfolio-thumb-2', 140, 120, true);
add_image_size('portfolio-thumb-3', 220, 120, true);
add_image_size('portfolio-thumb-4', 300, 120, true);
return $this;
}
/**
* Register the custom post types
* needed for the porfolio.
* @return $this for chainability
*/
public function register_custom_posts(){
register_post_type(
'clients', array(
'label' => 'Clients',
'description' => '',
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => array('slug' => ''),
'query_var' => true,
'supports' => array(
'title',
'editor',
'excerpt',
'trackbacks',
'custom-fields',
'comments',
'revisions',
'thumbnail',
'author',
'page-attributes',
),
'labels' => array (
'name' => 'Clients',
'singular_name' => 'Client',
'menu_name' => 'Clients',
'add_new' => 'Add Client',
'add_new_item' => 'Add New Client',
'edit' => 'Edit',
'edit_item' => 'Edit Client',
'new_item' => 'New Client',
'view' => 'View Client',
'view_item' => 'View Client',
'search_items' => 'Search Clients',
'not_found' => 'No Clients Found',
'not_found_in_trash' => 'No Clients Found in Trash',
'parent' => 'Parent Client',
)
)
);
return $this;
}
/**
* Adds the needed metaboxed for the porfolio posts
*/
public function add_metaboxes(){
add_meta_box(
self::customer_box_id,
__('Customer Details'),
array(__CLASS__, 'url_metabox'),
self::client_custom_post_name,
'normal',
'high'
);
}
/**
* Url metabox manager
*/
public function url_metabox(){
global $post;
$urllink = get_post_meta( $post->ID, 'urllink', true );
$urldesc = get_post_meta( $post->ID, 'urldesc', true );
if ( !preg_match( "/http(s?):\/\//", $urllink )) {
$errors = 'Url not valid';
$urllink = 'http://';
}
// output invlid url message and add the http:// to the input field
if( $errors ) {
echo '<p class="error">'.$errors.'</p>';
} ?>
<p>
<label for="siteurl">Url:<br /> <input id="siteurl" size="37"
name="siteurl" value="<?php if( $urllink ) { echo $urllink; } ?>" />
</label>
</p>
<p>
<label for="urldesc">Description:<br /> <textarea id="urldesc"
name="urldesc" cols="45" rows="4">
<?php if( $urldesc ) {
echo $urldesc;
} ?>
</textarea>
</label>
</p>
<?php
}
/**
* Process the custom metabox fields
*/
public function save_custom_url( $post_id ) {
global $post;
if( $_POST ) {
update_post_meta( $post->ID, 'urllink', $_POST['siteurl'] );
update_post_meta( $post->ID, 'urldesc', $_POST['urldesc'] );
}
}
/**
* Get the markup for the portfolio thumb
*/
public function portfolio_thumb(){
$posts = get_posts(array(
'post_type' => self::client_custom_post_name,
'numberposts' => 13,
'post_status' => 'publish'
));
$toret = '';
$count = count($posts);
$permutations = array(
'3' => array(
array(4,4,4)
),
'4' => array(
array(4,4,2,2),
array(4,3,3,2),
array(3,3,3,3)
),
'5' => array(
array(2,2,2,2,4)
),
/*'6' => array(
array(2,2,2,2,2,2)
)*/
);
//TEST ONLYYYYY!!!!!
//$count = 19;
$module = $count % 4;
$n5 = $n3 = 0;
switch($module){
case 1:
case 2:
$n5 = $module;
break;
case 3:
$n3 = 1;
break;
}
$n4 = intval($count / 4) - $n5;
$rows = array();
for($i=0; $i<$n3; $i++){
array_push($rows, '3');
}
for($i=0; $i<$n4; $i++){
array_push($rows, '4');
}
for($i=0; $i<$n5; $i++){
array_push($rows, '5');
}
shuffle($rows);
if($count){
global $post;
$n_post = 0;
foreach($rows as $k => $n_images){
shuffle($permutations[$n_images]);
$choosed_permutation = $permutations[$n_images][0];
shuffle($choosed_permutation);
foreach($choosed_permutation as $size){
$post = $posts[$n_post++];
setup_postdata($post);
$img_big = wp_get_attachment_image_src(get_post_thumbnail_id(), 'full');
$toret .= '<div class="portfolio-element">';
$toret .= theme_helpers::anchor(
$img_big[0],
get_the_post_thumbnail(get_the_id(), 'portfolio-thumb-'.$size).'<span class="overlay">'.get_the_title().'</span>'
);
$toret .= '</div>';
wp_reset_postdata();
}
}
}
return $toret;
}
}
$portfolio = new portfolio();
$portfolio->setup();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment