About this topic

  • Posted by StiveG 1 year ago. There are 9 posts. The latest reply is from StiveG.
  • This topic is not resolved
  1. If I create a schedule post and upload a picture in it, the picture immediately appears in the gallery. Is there any way to have it appears only when the scheduled post is publish?

    Thanks,

    Steve

  2. My guess is that I would need to add something like this in the getGalleriesQuery function :

    WHERE .$wpdb->prefix."post.posts_status" = "publish"

    Using $excludesql?

    Uhhmm.. Yeah.. I have the logic (I think!) but not the MySQL programming skills :|

  3. no it's not the getGalleriesQuery function, looks like this is to sort the galleries names in the upload form..

    Doh!

  4. Ok,

    I think the function to modify is getGalleryImages (bwbps-layout.php)

    I've tried to add a LEFT OUTER JOIN in the SQL query, like this :

    (defined $publishDataJoin at around line 2810)

    'function getGalleryImages($g, $customFields=false){
    global $wpdb;
    global $current_user;

    $user_id = (int)$current_user->ID;

    $publishDataJoin = " LEFT OUTER JOIN ".$wpdb->posts
    ." ON ".PSIMAGESTABLE.".post_id = "
    .$wpdb->posts.".ID "
    ." AND " . $wpdb->posts . ".post_status = 'publish' OR " . $wpdb->posts . ".post_status = 'inherit'";'

    ...

    (added $publishDataJoin query part at around line 3160)

    '} else {
    //Non-Admins can see their own images and Approved images
    $uid = (int)$user_id ? (int)$user_id : -1;

    $sql = 'SELECT DISTINCT '.PSIMAGESTABLE.'.*, '
    .PSIMAGESTABLE.'.image_id as psimageID, '
    .$wpdb->users.'.user_nicename,'
    .$wpdb->users.'.display_name,'
    .$wpdb->users.'.user_login,'
    .$wpdb->users.'.user_url' . $gallery_selections
    .$custdata.' FROM '
    .PSIMAGESTABLE.' LEFT OUTER JOIN '.$wpdb->users.' ON '
    . $wpdb->users .'.ID = ' . PSIMAGESTABLE. '.user_id'.$custDataJoin . $favoriteDataJoin . $publishDataJoin
    . $sqlWhere . ' AND ( ' . PSIMAGESTABLE. '.status > 0 OR ' . PSIMAGESTABLE. '.user_id = '
    .$uid.') ORDER BY '.$sortby . $limitimages;

    $sql_count = 'SELECT DISTINCT '.PSIMAGESTABLE.'.image_id FROM '
    .PSIMAGESTABLE.' LEFT OUTER JOIN '.$wpdb->users.' ON '
    . $wpdb->users .'.ID = ' . PSIMAGESTABLE. '.user_id'.$custDataJoin . $favoriteDataJoin . $publishDataJoin
    . $sqlWhere . ' AND ( ' . PSIMAGESTABLE. '.status > 0 OR ' . PSIMAGESTABLE. '.user_id = '
    .$uid.')' . $hardlimit;

    }

    Now my SQL echo is:

    'SELECT DISTINCT wp2_bwbps_images.*, wp2_bwbps_images.image_id as psimageID, wp2_users.user_nicename,wp2_users.display_name,wp2_users.user_login,wp2_users.user_url, wp2_bwbps_galleries.post_id AS gal_post_id, wp2_bwbps_galleries.poll_id, wp2_bwbps_galleries.gallery_name AS image_gallery_name, wp2_bwbps_galleries.caption AS gallery_caption, wp2_bwbps_galleries.post_id AS gallery_post_id, wp2_bwbps_galleries.img_count AS gallery_image_count , wp2_bwbps_favorites.favorite_id , wp2_bwbps_customdata.* FROM wp2_bwbps_images LEFT OUTER JOIN wp2_users ON wp2_users.ID = wp2_bwbps_images.user_id LEFT OUTER JOIN wp2_bwbps_galleries ON wp2_bwbps_images.gallery_id = wp2_bwbps_galleries.gallery_id LEFT OUTER JOIN wp2_bwbps_customdata ON wp2_bwbps_images.image_id = wp2_bwbps_customdata.image_id LEFT OUTER JOIN wp2_bwbps_favorites ON wp2_bwbps_images.image_id = wp2_bwbps_favorites.image_id AND wp2_bwbps_favorites.user_id = 1 LEFT OUTER JOIN wp2_posts ON wp2_bwbps_images.post_id = wp2_posts.ID AND wp2_posts.post_status = 'publish' OR wp2_posts.post_status = 'inherit' WHERE wp2_bwbps_images.gallery_id = 17 ORDER BY wp2_bwbps_images.created_date DESC, wp2_bwbps_images.seq LIMIT 0, 20

    But still, not working. Still showing the images even if the post status is set to "future".

    Most be en error in my LEFT OUTER JOIN...

    Someone knows what is wrong?

    Thanks,

    Steve

  5. ok, found a way to do it (Only showing images from "publish" post in the galleries, not the ones from scheduled posts)

    It works for me but I DON'T KNOW IF THIS IS CORRECT SO USE AT YOUR OWN RISK.

    Modifications in bold.

    File : bwbps-layout.php


    function getGalleryImages($g, $customFields=false){
    global $wpdb;
    global $current_user;

    $user_id = (int)$current_user->ID;

    $publishDataJoin = " LEFT OUTER JOIN ".$wpdb->posts
    ." ON ".PSIMAGESTABLE.".post_id = "
    .$wpdb->posts.".ID";

    //Set up SQL for Custom Fields
    $custDataJoin = " LEFT OUTER JOIN ".PSCUSTOMDATATABLE
    ." ON ".PSIMAGESTABLE.".image_id = "
    .PSCUSTOMDATATABLE.".image_id ";
    $custdata = ", ".PSCUSTOMDATATABLE.".* ";

    $custDataJoin = " LEFT OUTER JOIN ". PSGALLERIESTABLE
    . " ON ". PSIMAGESTABLE . ".gallery_id = "
    . PSGALLERIESTABLE . ".gallery_id " . $custDataJoin;

    ... (268 lines of code after...)

    // Add the WHERE clause for the Smart Galleries
    if ( $g['smart_gallery'] ){

    if( is_array($g['smart_where'] ) ){
    $swhere[] = $this->getSmartWhereField( $g['smart_where'] );
    $sqlWhere = " WHERE " . implode( " AND ", $swhere );
    } else {

    $sqlWhere = " WHERE 1=1 ";

    }

    } else {

    $sqlWhere = " WHERE " . PSIMAGESTABLE . ".gallery_id = " . (int)$g['gallery_id'] ;

    }

    // Calculate paging
    if( (int)$g['limit_images'] && (int)$g['img_perpage'] ){
    if( $g['limit_images'] <= $g['img_perpage'] ){
    $g['img_perpage'] = 0;
    } else {
    $hardlimit = " LIMIT " . (int)$g['limit_images'];
    }
    }

    if( $g['img_perpage'] ){
    $limitimages = ' LIMIT ' . (int)$g['starting_image'] . ", " . $g['img_perpage'];
    }

    $sqlWhere .= " AND " . $wpdb->posts . ".post_status = 'publish'" . $sqlSpecialWhere;

    //Admins can see all images
    if(current_user_can('level_10')){
    $sql = 'SELECT DISTINCT '.PSIMAGESTABLE.'.*, '
    .PSIMAGESTABLE.'.image_id as psimageID, '
    .$wpdb->users.'.user_nicename,'
    .$wpdb->users.'.display_name,'
    .$wpdb->users.'.user_login,'
    .$wpdb->users.'.user_url' . $gallery_selections
    .$custdata.' FROM '
    .PSIMAGESTABLE.' LEFT OUTER JOIN '.$wpdb->users.' ON '
    . $wpdb->users .'.ID = '. PSIMAGESTABLE. '.user_id'.$custDataJoin . $favoriteDataJoin . $publishDataJoin
    . $sqlWhere . ' ORDER BY '.$sortby . $limitimages;

    $sql_count = 'SELECT DISTINCT '.PSIMAGESTABLE.'.image_id FROM '
    .PSIMAGESTABLE.' LEFT OUTER JOIN '.$wpdb->users.' ON '
    . $wpdb->users .'.ID = '. PSIMAGESTABLE. '.user_id'.$custDataJoin . $favoriteDataJoin . $publishDataJoin
    . $sqlWhere . $hardlimit;

    } else {
    //Non-Admins can see their own images and Approved images
    $uid = (int)$user_id ? (int)$user_id : -1;

    $sql = 'SELECT DISTINCT '.PSIMAGESTABLE.'.*, '
    .PSIMAGESTABLE.'.image_id as psimageID, '
    .$wpdb->users.'.user_nicename,'
    .$wpdb->users.'.display_name,'
    .$wpdb->users.'.user_login,'
    .$wpdb->users.'.user_url' . $gallery_selections
    .$custdata.' FROM '
    .PSIMAGESTABLE.' LEFT OUTER JOIN '.$wpdb->users.' ON '
    . $wpdb->users .'.ID = ' . PSIMAGESTABLE. '.user_id'.$custDataJoin . $favoriteDataJoin . $publishDataJoin
    . $sqlWhere . ' AND ( ' . PSIMAGESTABLE. '.status > 0 OR ' . PSIMAGESTABLE. '.user_id = '
    .$uid.') ORDER BY '.$sortby . $limitimages;

    $sql_count = 'SELECT DISTINCT '.PSIMAGESTABLE.'.image_id FROM '
    .PSIMAGESTABLE.' LEFT OUTER JOIN '.$wpdb->users.' ON '
    . $wpdb->users .'.ID = ' . PSIMAGESTABLE. '.user_id'.$custDataJoin . $favoriteDataJoin . $publishDataJoin
    . $sqlWhere . ' AND ( ' . PSIMAGESTABLE. '.status > 0 OR ' . PSIMAGESTABLE. '.user_id = '
    .$uid.')' . $hardlimit;

    }

    //echo $sql;

    // Get Count for Paging
    $this->total_records = 0;

    if( $g['img_perpage'] ){
    $count = $wpdb->get_results($sql_count, ARRAY_A);

    if($count){
    $this->total_records = $wpdb->num_rows;

    $images = $wpdb->get_results($sql, ARRAY_A);
    }
    } else {
    $images = $wpdb->get_results($sql, ARRAY_A);
    $this->total_records = $wpdb->num_rows;
    }

    return $images;
    }

    Now, the images inside sceduled posts are not showing in the galleries BUT one problem remaining, the count in the gallery viewer page is incorrect because those numbers come from bwbps_galleries(img_count) directly. ( Ref: 'function getCFFieldHTML -> case '[gallery_image_count]' : $ret = (int)$image['gallery_image_count'] ;')

    steve

  6. Hi Steve,

    Awesome! Glad you got it figured out. Clearly, I've got some UGLY code just to get the SQL statement for retrieving images...I hope to fix that in the re-write.

    So, to solve you other problem, there is a function that updates the image count, so if you fix that to only count the ones that have published posts, you should be good until your posts publish on schedule, at which point you'll need to run the count again.

    The code you'll need to Mod is in: bwb-photosmash/admin/image-functions.php, function: updateGalleryImageCount(...

    You might be able to hook into the publish_post hook or maybe post_updated hook to call your function to update the image counts. I don't know if publish_post fires before or after the publish occurs...if it's before, it won't do you any good since your images won't get picked up in the count until afterward.

    I'd recommend just updating the counts for all of your galleries on the publish, that way they'll be freshly counted and you don't have to try to figure out which gallery to update. But that might be just as easy.

    Hope that helps,
    Byron

  7. Hi Byron,

    Found another way to do it just before to read your reply...

    Here is what I did :

    In bwbps-layout.php, function getCFFieldHTML

    I CHANGED

    case '[gallery_image_count]' :
                    $ret = (int)$image['gallery_image_count'] ;
                    break;

    TO

    case '[gallery_image_count]' :
    
                    $total = $wpdb->get_row('SELECT COUNT(DISTINCT '.PSIMAGESTABLE.'.image_id) as total FROM '
                    .PSIMAGESTABLE.' LEFT OUTER JOIN '.$wpdb->posts.' ON '
                    . $wpdb->posts .'.ID = ' . PSIMAGESTABLE. '.post_id'.' WHERE ( ' . $wpdb->posts. '.post_status = "publish" AND '.PSIMAGESTABLE.'.gallery_id = '.(int)$image['gallery_id'].')');
    
                    $ret = $total->total;
    
                    break;

    Seems to work but, again, I don't know php and MYSQL enough (and Photosmash functions) to know if this is correct...

    Steve

    P.S. It's kind of ugly because it means more sql queries (one per gallery) only to get the count. But, it works...

  8. Yep, that should work. As long as you don't have 10,000's of pictures the query shouldn't be too time consuming. If it does get slow, then you can go with my suggestion above. It will save you the queries when galleries are viewed, but it's a bit more work.

    Cheers,
    Byron

  9. "You might be able to hook into the publish_post hook or maybe post_updated hook to call your function to update the image counts."

    I got headaches just reading it... I don't think I know PHP or Wordpress enough to do that :(

    I'll try to figure that out when i'll have some.. euhm.. plenty of time lol

RSS feed for this topic