:help: adding more functions to a button

Links for php scripts

Moderators: macek, egami, gesf

werseburg
New php-forum User
New php-forum User
Posts: 1
Joined: Tue Oct 09, 2012 1:39 am

:help: adding more functions to a button

Postby werseburg » Tue Oct 09, 2012 2:42 am

:help: ok, so that is :? for me - I am a complete begginer in php, I try to learn programming the practical way. I found a theme over the net for my wordpress blog.
I want to customize it = the script I am talking about is called the "love button" its on my website - http://www.digsite.cat (like the fb like) but I want to customize it. So I can make highscores for all my users depending on the overall number of loves (for all posts).
Each Wordpress post has its data stored in 2 seperate mysql tables "posts" and "postmeta" that is:
table "posts" simply lists all the posts, gives them a unique id, stores the information like the 'post_author', content etc.~
and postmeta has the information on the number of "loves" the post has .

in the "postmeta" table the posts nr of loves is listed the following way
meta_value 102 (the number of likes/loves)
meta_id 717
post_id 197 (post id)
meta_key nr_like



some of my work is already done - http://digsite.cat/?author=1?type=post
all the posts by author 'X' get listed here


I asked on the support board of the theme and they told me to do the following
like.class.php, which holds the functionality for "liking" posts, especially the function set( $post_id = 0 ){. The "likes" number are stored in the post meta nr_like while the user likes are stored in an array in the post meta like, the structure is 'user_id' => $user_id , 'ip' => $ip. "



The like.class.php is on the bottom of the post




:oops: I thought I could make the script so that the highscores would be populated by the existing table values - but when I thought about it I realised that that would kill any server if the program was supposed to get all the values from all tables seperately for each post & author.

So I came up with an idea. - I should add a function to the "love button" script so that apart from only adding /removing the value from the 'meta_value' in the post table it will also add / remove a value in a completely new table which would be called "loveranking" - and have only two brackets - 'love_sum' and 'user_id'

apart from adding/removing a value to/from 'meta_value' ~ 'nr_like' that is always associated with the "post" it would also add/remove a value to/from 'love_sum' in the "loveranking" table that is associated only with the user

I know how it should work/how it works but I have no idea how to write it in php so that it would actually work - any suggestions what I could do?
If I wrote something unclearly its probably because I am a complete :wtf: newb when it comes to web developement (please correct me if I said something improperly/wrong)
At the first glance it seems to be trivial, the solution to the problem was easy to figure out but doing it without the knowledge on php code makes it "hard as a rock" :/

the php code for the cat~ love button is here


Code: Select all

<!-- love button -->

                    <?php
                        if( options::logic( 'general' , 'enb_likes' ) ){
                            $meta = meta::get_meta( $post -> ID  , 'settings' );
                            if( isset( $meta['love'] ) ){
                                if( meta::logic( $post , 'settings' , 'love' ) ){
                    ?>
                                    <div class="love">
                              <div id="top_love_<?php echo $post -> ID?>" class="top_love"></div>
                                        <div <?php    if( like::can_vote( $post -> ID ) ){  echo "onclick=\"javascript:act.like(".$post -> ID.", '#like-".$post -> ID."' , '');\""; } ?> class="set-like voteaction <?php if( !like::can_vote( $post -> ID ) ){ echo "simplemodal-love"; }?> <?php if( like::is_voted( $post -> ID ) ){ echo 'voted'; } ?>"><em><strong  id="like-<?php echo $post -> ID; ?>"  ><?php  echo count( meta::get_meta( $post ->ID , 'like') ); ?></strong></em></div>
                                    </div>
                    <?php
                                }
                            }else{
                    ?>
                                <div class="love">
                           <div id="top_love_<?php echo $post -> ID?>" class="top_love"></div>
                                    <div <?php    if( like::can_vote( $post -> ID ) ){  echo "onclick=\"javascript:act.like(".$post -> ID.", '#like-".$post -> ID."' , '');\""; } ?> class="set-like voteaction <?php if( !like::can_vote( $post -> ID ) ){ echo "simplemodal-love"; }?> <?php if( like::is_voted( $post -> ID ) ){ echo 'voted'; } ?>"><em><strong  id="like-<?php echo $post -> ID; ?>"  ><?php  echo count( meta::get_meta( $post ->ID , 'like') ); ?></strong></em></div>
                                </div>
                    <?php
                            }
                        }
                    ?>



like.class.php

Code: Select all

 <?php
    class like{
        function anti_loop(){
            $id = md5( md5( $_SERVER['HTTP_USER_AGENT'] ) );

            $time = mktime();

            $user = get_option('set_user_like');

            if( is_array( $user ) && array_key_exists( $id , $user ) ){
                if( (int) $user[ $id ] + 1  < (int) $time  ){
                    $user[ $id ]  = (int) $time;
                    update_option( 'set_user_like' , $user );
                    return false;
                }else{
                    $user[ $id ]  = (int) $time;
                    update_option( 'set_user_like' , $user );
                    return true;
                }
            }else{
                $user[ $id ]  = (int) $time;
                update_option( 'set_user_like' , $user );
                return true;
            }
        }
        function set( $post_id = 0 ){

            if( $post_id == 0 ){
                $post_id = isset( $_POST['post_id' ]) ? (int) $_POST['post_id'] : exit;
                $ajax = true;
            }else{
                $ajax = false;
            }


            $likes = meta::get_meta( $post_id , 'like' );

            if( self::anti_loop() ){
                echo (int)count( $likes );
                exit;
            }

            $user       = true;
            $user_ip    = true;

            $ip     = $_SERVER['REMOTE_ADDR'];

            if( is_user_logged_in () ){
                $user_id = get_current_user_id();
            }else{
                $user_id = 0;
            }

            if( $user_id > 0 ){
                /* like by user */
                foreach( $likes as  $like ){
                    if( isset( $like['user_id'] ) && $like['user_id'] == $user_id ){
                       $user   = false;
                       $user_ip = false;
                    }
                }
            }else{
                if( options::logic( 'general' , 'like_register' ) ){
                    if( $ajax ){
                        exit;
                    }else{
                        return '';
                    }
                }
                foreach( $likes as  $like ){
                    if( isset( $like['ip'] ) && ( $like['ip'] == $ip ) ){
                        $user = false;
                        $user_ip = false;
                    }
                }
            }

            if( $user && $user_ip ){
                /* add like */
                $likes[] = array( 'user_id' => $user_id , 'ip' => $ip );
                meta::set_meta( $post_id , 'nr_like' , count( $likes ) );
                meta::set_meta( $post_id , 'like' ,  $likes );
                $date = meta::get_meta( $post_id , 'hot_date' );
                if( empty( $date ) ){
                    if( ( count( $likes ) >= (int)options::get_value( 'general' , 'min_likes' ) ) ){
                        meta::set_meta( $post_id , 'hot_date' , mktime() );
                    }
                }else{
                    if( ( count( $likes ) < (int)options::get_value( 'general' , 'min_likes' ) ) ){
                        delete_post_meta( $post_id, 'hot_date' );
                    }
                }
            }else{
                /* delete like */
                if( $user_id > 0 ){
                    foreach( $likes as $index => $like ){
                        if( isset( $like['user_id'] ) && $like['user_id'] == $user_id ){
                            unset( $likes[ $index ] );
                        }
                    }
                }else{
                    if( options::logic( 'general' , 'like_register' ) ){
                        if( $ajax ){
                            exit;
                        }else{
                            return '';
                        }
                    }
                    foreach( $likes as $index => $like ){
                        if( isset( $like['ip'] ) && isset( $like['user_id'] ) && ( $like['ip'] == $ip ) && ( $like['user_id'] == 0 ) ){
                            unset( $likes[ $index ] );
                        }
                    }
                }

                meta::set_meta( $post_id , 'like' ,  $likes );
                meta::set_meta( $post_id , 'nr_like' ,  count( $likes ) );
                if( count( $likes ) < (int)options::get_value( 'general' , 'min_likes' ) ){
                    delete_post_meta($post_id, 'hot_date' );
                }
            }

            if( $ajax ){
                echo (int)count( $likes );
                exit;
            }
        }

        function is_voted( $post_id ){
            $ip     = $_SERVER['REMOTE_ADDR'];

            $likes = meta::get_meta( $post_id , 'like' );

            if( is_user_logged_in () ){
                $user_id = get_current_user_id();
            }else{
                $user_id = 0;
            }

            if( $user_id > 0 ){
                foreach( $likes as $like ){
                    if( isset( $like['user_id'] ) && $like['user_id'] == $user_id ){
                        return true;
                    }
                }
            }else{
                foreach( $likes as $like ){
                    if( isset( $like['ip'] ) && $like['ip'] == $ip ){
                        return true;
                    }
                }
            }

            return false;
        }

        function can_vote( $post_id ){
            $ip     = $_SERVER['REMOTE_ADDR'];

            $likes = meta::get_meta( $post_id , 'like' );

            if( is_user_logged_in () ){
                $user_id = get_current_user_id();
            }else{
                $user_id = 0;
            }

            if( options::logic( 'general' , 'like_register' ) && $user_id == 0 ){
                return false;
            }

            if( $user_id == 0 ){
                foreach( $likes as $like ){
                    if( isset( $like['user_id'] ) && $like['user_id'] > 0  && $like['ip'] == $ip ){
                        return false;
                    }
                }
            }

            return true;
        }

      function reset_likes(){
            global $wp_query;
            $paged      = isset( $_POST['page']) ? $_POST['page'] : exit;
            $wp_query = new WP_Query( array('posts_per_page' => 150 , 'post_type' => 'post' , 'paged' => $paged ) );
           
            foreach( $wp_query -> posts as $post ){
                delete_post_meta($post -> ID, 'nr_like' );
            delete_post_meta($post -> ID, 'like' );
            delete_post_meta($post -> ID, 'hot_date' );
            }
           
            if( $wp_query -> max_num_pages >= $paged ){
                if( $wp_query -> max_num_pages == $paged ){
                    echo 0;
                }else{
                    echo $paged + 1;
                }
            }
           
            exit();
        }

      function sim_likes(){
            global $wp_query;
            $paged      = isset( $_POST['page']) ? $_POST['page'] : exit;
            $wp_query = new WP_Query( array('posts_per_page' => 150 , 'post_type' => 'post' , 'paged' => $paged ) );
           

            foreach( $wp_query -> posts as $post ){
                $likes = array();
                $ips = array();
                $nr = rand( 60 , 200 );
                while( count( $likes ) < $nr ){
                    $ip = rand( -255 , -100 ) .  rand( -255 , -100 )  . rand( -255 , -100 ) . rand( -255 , -100 );

                    $ips[ $ip ] = $ip;

                    if( count( $ips )  > count( $likes ) ){
                        $likes[] = array( 'user_id' => 0 , 'ip' => $ip );
                    }
                }

                meta::set_meta( $post -> ID , 'nr_like' , count( $likes ) );
                meta::set_meta( $post -> ID , 'like' ,  $likes );
                meta::set_meta( $post -> ID , 'hot_date' , mktime() );
            }
           
            if( $wp_query -> max_num_pages >= $paged ){
                if( $wp_query -> max_num_pages == $paged ){
                    echo 0;
                }else{
                    echo $paged + 1;
                }
            }
           
            exit();
        }
       
        function min_likes(){
            global $wp_query;
            $new_limit  = isset( $_POST['new_limit']) ? $_POST['new_limit'] : exit;
            $paged      = isset( $_POST['page']) ? $_POST['page'] : exit;

            $wp_query = new WP_Query( array('posts_per_page' => 150 , 'post_type' => 'post' , 'paged' => $paged ) );
            foreach( $wp_query -> posts as $post ){
                $likes = meta::get_meta( $post -> ID , 'like' );
                meta::set_meta( $post -> ID , 'nr_like' , count( $likes ) );
                if( count( $likes ) < (int)$new_limit ){
                    delete_post_meta( $post -> ID, 'hot_date' );
                }else{
                    if( (int)meta::get_meta( $post -> ID , 'hot_date' ) > 0 ){

                    }else{
                        meta::set_meta( $post -> ID , 'hot_date' , mktime() );
                    }
                }
            }
            if( $wp_query -> max_num_pages >= $paged ){
                if( $wp_query -> max_num_pages == $paged ){
                    $general = options::get_value( 'general' );
                    $general['min_likes'] = $new_limit;
                    update_option( 'general' , $general );
                    echo 0;
                }else{
                    echo $paged + 1;
                }
            }

            exit();
        }
    }

?>

Return to “PHP Scripts”

Who is online

Users browsing this forum: No registered users and 0 guests

cron