Plugin Directory

Changeset 1724186


Ignore:
Timestamp:
09/04/2017 10:28:25 AM (8 years ago)
Author:
wearelittle
Message:

Version 1.3 - plugin improvements, more admin options, bug fixes

Location:
little-social/trunk
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • little-social/trunk/admin/class-social-admin.php

    r1618483 r1724186  
    11<?php
     2
     3require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/little-social-media-scheduler.php';
    24
    35/**
     
    122124            // add settings saved message with the class of "updated"
    123125            add_settings_error('wporg_messages', 'wporg_message', __('Settings Saved', $this->page), 'updated');
     126
     127
     128            //if settings have been changed - reset the cron with new value
     129            $scheduler = new SocialMediaScheduler();
     130            $scheduler->addSocialScheduleEvent();
     131
    124132        }
    125133       
     
    255263
    256264        register_setting("general_social_settings_section", "social_cron_frequency");
     265        register_setting("general_social_settings_section", "days_to_keep_social_posts");
    257266
    258267        //twitter
     
    261270        register_setting("twitter_settings_section", "social_twitter_username");
    262271        register_setting("twitter_settings_section", "social_twitter_show_flag");
     272        register_setting("twitter_settings_section", "social_twitter_show_retweet_flag");
     273        register_setting("twitter_settings_section", "social_twitter_show_media_flag");
     274        register_setting("twitter_settings_section", "social_twitter_profile_image");
    263275
    264276        //facebook
     
    267279        register_setting("facebook_settings_section", "social_facebook_username");
    268280        register_setting("facebook_settings_section", "social_facebook_show_flag");
     281        register_setting("facebook_settings_section", "social_facebook_show_media_flag");
     282        register_setting("facebook_settings_section", "social_facebook_profile_image");
    269283
    270284        //instagram
     
    274288        register_setting("instagram_settings_section", "social_instagram_access_token_link");   
    275289        register_setting("instagram_settings_section", "social_instagram_show_flag");
     290        register_setting("instagram_settings_section", "social_instagram_show_media_flag");
     291        register_setting("instagram_settings_section", "social_instagram_profile_image");
    276292
    277293        //linkedin
     
    280296        register_setting("linkedin_settings_section", "social_linkedin_access_token");
    281297        register_setting("linkedin_settings_section", "social_linkedin_page_url");
     298        register_setting("linkedin_settings_section", "social_linkedin_show_media_flag");
     299        register_setting("linkedin_settings_section", "social_linkedin_profile_image");
    282300       
    283301    }   
     
    350368            'general_social_settings_section_id'
    351369        );
     370
     371        add_settings_field(
     372            'days_to_keep_social_posts',
     373            'Number of days to keep posts in Database',
     374            array( $this, 'days_to_keep_social_posts_callback' ),
     375            'general_social_settings_section',
     376            'general_social_settings_section_id'
     377        );
    352378       
    353379
     
    362388        );
    363389
     390        add_settings_field(
     391            'social_facebook_show_media_flag',
     392            'Show Media',
     393            array( $this, 'social_facebook_show_media_flag_callback' ),
     394            'facebook_settings_section',
     395            'facebook_settings_section_id'
     396        );
     397
    364398        add_settings_field(
    365399            'social_facebook_api_key',
     
    386420        );
    387421
     422        add_settings_field(
     423            'social_facebook_profile_image',
     424            'Profile Image',
     425            array( $this, 'social_facebook_profile_image_callback' ),
     426            'facebook_settings_section',
     427            'facebook_settings_section_id'
     428        );
     429       
     430
    388431        /* TWITTER Fields */
    389432       
     
    397440
    398441        add_settings_field(
     442            'social_twitter_show_retweet_flag',
     443            'Show retweets',
     444            array( $this, 'social_twitter_show_retweet_flag_callback' ),
     445            'twitter_settings_section',
     446            'twitter_settings_section_id'
     447        );
     448       
     449        add_settings_field(
     450            'social_twitter_show_media_flag',
     451            'Show Media',
     452            array( $this, 'social_twitter_show_media_flag_callback' ),
     453            'twitter_settings_section',
     454            'twitter_settings_section_id'
     455        );
     456
     457        add_settings_field(
    399458            'social_twitter_api_key',
    400459            'Twitter API',
     
    420479        );
    421480
    422 
     481        add_settings_field(
     482            'social_twitter_profile_image',
     483            'Profile Image',
     484            array( $this, 'social_twitter_profile_image_callback' ),
     485            'twitter_settings_section',
     486            'twitter_settings_section_id'
     487        );
    423488
    424489        /* INSTAGRAM fields */
     
    433498       
    434499        add_settings_field(
     500            'social_instagram_show_media_flag',
     501            'Show Media',
     502            array( $this, 'social_instagram_show_media_flag_callback' ),
     503            'instagram_settings_section',
     504            'instagram_settings_section_id'
     505        );
     506
     507        add_settings_field(
    435508            'social_instagram_client_id',
    436509            'Client ID',
     
    460533            'Instagram Access Token',
    461534            array( $this, 'social_instagram_access_token_callback' ),
     535            'instagram_settings_section',
     536            'instagram_settings_section_id'
     537        );
     538
     539        add_settings_field(
     540            'social_instagram_profile_image',
     541            'Profile Image',
     542            array( $this, 'social_instagram_profile_image_callback' ),
    462543            'instagram_settings_section',
    463544            'instagram_settings_section_id'
     
    477558
    478559        add_settings_field(
     560            'social_linkedin_show_media_flag',
     561            'Show Media',
     562            array( $this, 'social_linkedin_show_media_flag_callback' ),
     563            'linkedin_settings_section',
     564            'linkedin_settings_section_id'
     565        );
     566
     567        add_settings_field(
    479568            'social_linkedin_username',
    480569            'LinkedIn Username',
     
    499588            'linkedin_settings_section_id'
    500589        );
    501        
     590           
     591        add_settings_field(
     592            'social_linkedin_profile_image',
     593            'Profile Image',
     594            array( $this, 'social_linkedin_profile_image_callback' ),
     595            'linkedin_settings_section',
     596            'linkedin_settings_section_id'
     597        );
    502598
    503599    }
     
    536632    }
    537633
     634    function days_to_keep_social_posts_callback(){
     635        $this->outputInputSetting('days_to_keep_social_posts');
     636    }
     637
    538638    public function social_linkedin_show_flag_callback(){
    539639        $this->outputCheckboxSetting('social_linkedin_show_flag');
     
    615715        $this->outputCheckboxSetting('social_twitter_show_flag');
    616716    }
     717
     718    function social_twitter_show_retweet_flag_callback(){
     719        $this->outputCheckboxSetting('social_twitter_show_retweet_flag');
     720    }
     721
     722
     723    /*
     724        Show media callbacks
     725    */
     726
     727    function social_facebook_show_media_flag_callback(){
     728        $this->outputCheckboxSetting('social_facebook_show_media_flag');
     729    }
     730
     731    function social_twitter_show_media_flag_callback(){
     732        $this->outputCheckboxSetting('social_twitter_show_media_flag');
     733    }
     734
     735    function social_instagram_show_media_flag_callback(){
     736        $this->outputCheckboxSetting('social_instagram_show_media_flag');
     737    }
     738
     739    function social_linkedin_show_media_flag_callback(){
     740        $this->outputCheckboxSetting('social_linkedin_show_media_flag');
     741    }
     742
     743
     744    /*
     745        Profile image callbacks
     746    */
     747
     748    function social_facebook_profile_image_callback(){
     749        $this->outputInputSetting('social_facebook_profile_image');
     750    }
     751
     752    function social_twitter_profile_image_callback(){
     753        $this->outputInputSetting('social_twitter_profile_image');
     754    }
     755
     756    function social_instagram_profile_image_callback(){
     757        $this->outputInputSetting('social_instagram_profile_image');
     758    }
     759
     760    function social_linkedin_profile_image_callback(){
     761        $this->outputInputSetting('social_linkedin_profile_image');
     762    }
     763
    617764
    618765    function outputInputSetting($settingName, $classes = ''){
  • little-social/trunk/little-social-media.php

    r1659447 r1724186  
    66 * Author: Little Agency
    77 * Author URI: wearelittle.agency
    8  * Version: 1.0.1
     8 * Version: 1.3.0
    99 */
    1010
     
    7272require plugin_dir_path( __FILE__ ) . 'social/SocialConnector.php';
    7373
    74 
    7574/**
    7675 * Begins execution of the plugin.
     
    9190social();
    9291
    93 /*
    94 function test(){
    95     $smg = new SocialMediaGetter();
    96     //$smg->getPostsFromTwitter();
    97     //$smg->getPostsFromFacebook();
    98     $smg->getPostsFromLinkedin();
    99 
    100 }
    101 add_action('init','test');*/
    10292
    10393
    104 /*
    105 $socialRepo = new SocialRepository();
    106 $fb = $socialRepo->getSocialModelsFromDb('linkedin','Linkedin');
    107 echo '<pre>' . var_export($fb[0]->getLinkedinUrl(),true) . '</pre>';die();
    108 */
     94// $smg = new SocialMediaGetter();
     95// $smg->getPostsFromTwitter();
     96// $smg->getPostsFromFacebook();
     97// $smg->getPostsFromInstagram();
     98
    10999
    110100function getSocialPostsByPlatform(){
     
    138128
    139129}
    140 //var_dump(getSocialPosts());die();
    141 //echo '<pre>' . var_export(getAllSocialPosts(),true) . '</pre>';die();
     130
     131function showTwitterMedia(){
     132    return get_option('social_twitter_show_media_flag');
     133}
     134
     135function showFacebookMedia(){
     136    return get_option('social_facebook_show_media_flag');
     137}
     138
     139function showInstagramMedia(){
     140    return get_option('social_instagram_show_media_flag');
     141}
     142
     143function showLinkedinMedia(){
     144    return get_option('social_linkedin_show_media_flag');
     145}
     146
     147function showRetweets(){
     148    return get_option('social_twitter_show_retweet_flag');
     149}
     150
     151function getProfileImage($platform){
     152    $setting = 'social_'. $platform .'_profile_image';
     153
     154    if(!empty($setting) && !is_null($setting)){
     155        return get_option($setting);
     156    }
     157
     158    return '';
     159}
     160
  • little-social/trunk/social/FacebookConnector.php

    r1618483 r1724186  
    77
    88
    9     private $limit = 200;
    10     private $facebookUrl = 'https://graph.facebook.com/USERNAME/posts?limit=';
     9    private $limit = 100;
     10    private $facebookUrl = 'https://graph.facebook.com/USERNAME/posts?fields=id,message,created_time,picture,likes,type,permalink_url,
     11    source,link,description,from&limit=';
    1112
    1213    public function __construct(){
     
    3132
    3233        $fbPosts = $this->getLatestPosts($apiUrl,[]);
    33         return $this->createFacebookObjectsFromApi($fbPosts->data,"FBPost","facebook");
     34       
     35        //make sure no errors
     36        if(!property_exists($fbPosts, 'error')){   
     37
     38            foreach($fbPosts->data as $post){
     39                if(property_exists($post, 'message')){
     40                    $post->message = $this->removeEmoji($post->message);
     41                }       
     42            }
     43
     44            return $this->createFacebookObjectsFromApi($fbPosts->data,"FBPost","facebook");
     45        }
     46       
    3447    }
    3548
  • little-social/trunk/social/InstagramConnector.php

    r1618483 r1724186  
    2121        $apiUrl = 'https://api.instagram.com/v1/users/self/media/recent/?access_token=' . $accessToken;
    2222        $instaPosts = $this->getLatestPosts($apiUrl,[]);
    23         return $this->createInstagramObjectsFromApi($instaPosts->data,"Instagram","instagram");
    24 
     23       
     24        //make sure success
     25        if($instaPosts->meta->code == 200){
     26            foreach($instaPosts->data as $post){
     27                if(!is_null($post->caption->text)){
     28                    $post->caption->text = $this->removeEmoji($post->caption->text);
     29                }
     30            }
     31            return $this->createInstagramObjectsFromApi($instaPosts->data,"Instagram","instagram");
     32        }
    2533    }
    2634
  • little-social/trunk/social/SocialConnector.php

    r1618483 r1724186  
    3838        return $this->socialMediaFactory->createLinkedinObjectsFromApi($response,$model,$type);
    3939    }
    40    
     40       
     41    /*
     42        Helper function
     43    */
     44    //https://stackoverflow.com/questions/43097087/how-to-remove-non-text-chars-from-string-php
     45    function removeEmoji($text){
     46        $text=preg_replace('/[^ -\x{2122}]/u','',$text);  // no icons
     47        $text=trim(preg_replace('/(?<=\s)\s+/','',$text));  // no leading/trailing/multi spaces
     48        return $text;
     49    }
    4150
    4251}
  • little-social/trunk/social/SocialMediaFactory.php

    r1618483 r1724186  
    2929                if(strcasecmp('twitter', $type) == 0){
    3030                    $post->setHashtags($responseObj->entities->hashtags);
    31                 }
    32 
    33 
     31
     32                    //image
     33                    if(property_exists($responseObj->entities, 'media') && isset($responseObj->entities->media[0])){
     34                        $post->setContentImage($responseObj->entities->media[0]->media_url_https);
     35                    }
     36
     37                    if(property_exists($responseObj, 'retweeted_status')){
     38                        //was retweeted
     39                        $post->setRetweeted(true);
     40                    }
     41
     42                }
     43
     44               
    3445                //need to convert to unix timestamp so can order properly
    3546                $postDate = $this->convertDateToUnixTimestamp($responseObj->created_at);
     
    6879                $post = new $model;
    6980                $post->setId($responseObj->id);
    70                 $post->setType($type);
    71 
    72                 $post->setLikes($responseObj->likes);
    73                 $post->setContent($responseObj->message);
    74                 $post->setLink($responseObj->link);
    75                 $post->setPostType($responseObj->type);
    76                 $post->setVideo($responseObj->source);
    77                 //$post->setDate($responseObj->created_time);
     81               
     82                if(property_exists($responseObj, 'type')){
     83                    $post->setType($type);
     84                    $post->setPostType($responseObj->type);
     85                }
     86
     87                if(property_exists($responseObj, 'likes')){
     88                    $post->setLikes($responseObj->likes);
     89                }
     90               
     91                if(property_exists($responseObj, 'message')){
     92                    $post->setContent($responseObj->message);
     93                }
     94               
     95                if(property_exists($responseObj, 'link')){
     96                    $post->setLink($responseObj->link);
     97                }
     98
     99                if(property_exists($responseObj, 'source')){
     100                    $post->setVideo($responseObj->source);
     101                }
     102               
    78103
    79104                //need to convert to unix timestamp so can order properly
     
    81106                $post->setDate($postDate);
    82107
    83                 if(!is_null($responseObj->description)){
     108                if(property_exists($responseObj, 'description')){
    84109                    $post->setDescription($responseObj->description);
    85110                }
     
    88113                $post->setUserScreenName($responseObj->from->name);
    89114                $post->setUserId($responseObj->from->id);
    90                 $post->setProfilePic($responseObj->picture);
    91            
     115
     116                if(property_exists($responseObj, 'picture')){
     117                    $post->setProfilePic($responseObj->picture);
     118                }
     119               
    92120
    93121               
     
    153181                $post->setUserId($responseObj->user->id);
    154182                $post->setUserName($responseObj->user->username);
    155                 $post->setUserUrl($responseObj->user->url);
     183                if(!is_null($responseObj->user)){
     184                    if(property_exists($responseObj->user, 'url')){
     185                        $post->setUserUrl($responseObj->user->url);
     186                    }   
     187                }
    156188                $post->setLikes($responseObj->likes);
    157189                $post->setProfilePic($responseObj->user->profile_picture);
     
    218250                        }
    219251
     252                        if(array_key_exists ( 'contentImage' , $postMeta ) && isset($postMeta['contentImage'][0])){
     253                            $model->setContentImage($postMeta['contentImage'][0]);
     254                        }
     255
    220256                        if(array_key_exists ( 'date' , $postMeta ) && isset($postMeta['date'][0])){
    221257                            $model->setDate($postMeta['date'][0]);
     
    256292                        if(array_key_exists ( 'profile_pic' , $postMeta ) && isset($postMeta['profile_pic'][0])){
    257293                            $model->setProfilePic($postMeta['profile_pic'][0]);
     294                        }
     295
     296                        /*
     297                            Twitter
     298                        */
     299
     300                        if(array_key_exists ( 'retweeted' , $postMeta ) && isset($postMeta['retweeted'][0])){
     301                            $model->setRetweeted($postMeta['retweeted'][0]);
    258302                        }
    259303
  • little-social/trunk/social/SocialMediaGetter.php

    r1618483 r1724186  
    2626            $twitterConnector = new TwitterConnector();
    2727            $posts = $twitterConnector->getLatestTweets();
    28             $this->insertPostsToDb($posts);
     28            if(!is_null($posts)){
     29                $this->insertPostsToDb($posts);
     30            }
    2931        }
    3032    }
     
    3537            $fbConnector = new FacebookConnector();
    3638            $posts = $fbConnector->getLatestFbPosts();
    37             $this->insertPostsToDb($posts);
     39            if(!is_null($posts)){
     40                $this->insertPostsToDb($posts);
     41            }   
    3842        }
    3943    }
     
    4347            $instaConnector = new InstagramConnector();
    4448            $posts = $instaConnector->getLatestInstaPosts();
    45             $this->insertPostsToDb($posts,'instagram');
     49            if(!is_null($posts)){
     50                $this->insertPostsToDb($posts,'instagram');
     51            }
    4652        }
    4753    }
     
    5258            $linkedInConnector = new LinkedinConnector();
    5359            $posts = $linkedInConnector->getLatestLinkedInPosts();
    54             $this->insertPostsToDb($posts,'linkedin');
     60            if(!is_null($posts)){
     61                $this->insertPostsToDb($posts,'linkedin');
     62            }
    5563        }
    5664    }
  • little-social/trunk/social/SocialRepository.php

    r1618483 r1724186  
    11<?php
    22
     3require_once( ABSPATH . "wp-includes/pluggable.php" );
    34require_once( ABSPATH . 'wp-admin/includes/post.php' );
     5require plugin_dir_path( __FILE__ ) . 'Helpers.php';
    46
    57class SocialRepository {
     
    79    private $postType;
    810    private $socialMediaFactory;
     11    private $helpers;
    912
    1013    public function __construct(){
    1114        $this->postType = 'little_social';
    1215        $this->socialMediaFactory = new SocialMediaFactory();
     16        $this->helpers = new Helpers();
    1317    }
    1418
     
    120124            $posts = $this->getAllSocialPosts();
    121125        }
     126
     127        foreach($posts as $post){
     128            $id = $post->ID;
     129            $date = get_post_meta( $id, 'date');
     130
     131            if(isset($date[0])){
     132                $this->deleteIfOlderThanThreshold($id,$date[0]);
     133            }
     134           
     135        }
    122136       
    123137        return $this->socialMediaFactory->createSocialObjectsFromDB($posts,$model);
     
    222236            update_post_meta($id, 'comments', $socialPost->getComments());
    223237               
     238            $this->deleteIfOlderThanThreshold($id,$socialPost->getDate());
    224239
    225240        }catch(Exception $e){
     
    248263        return $id;
    249264    }
     265
     266    public function deleteIfOlderThanThreshold($id, $date){
     267        $deletePropertyRes = $this->helpers->checkIfSocialPostOlderThanKeepThreshold($date);
     268
     269        if($deletePropertyRes){
     270            wp_delete_post( $id , true );
     271        }
     272    }
     273
     274    // public function checkIfSocialPostOlderThanKeepThreshold($socialPostDate){
     275    //  $isOlderThanThreshold = false;
     276
     277    //  $numberOfDays = get_option('days_to_keep_social_posts');
     278
     279    //  if(empty($numberOfDays) || is_null($numberOfDays)){
     280    //      return false;
     281    //  }
     282
     283    //  $now = new DateTime();
     284
     285    //  $lastUpdatedFormatted =  date('Y-m-d H:i:s',$socialPostDate);
     286    //  $last = new DateTime($lastUpdatedFormatted);
     287
     288    //  if($last->diff($now)->days > $numberOfDays) {
     289    //     $isOlderThanThreshold = true;
     290    //  }
     291
     292    //  return $isOlderThanThreshold;
     293    // }
    250294
    251295    public function addSocialPost($socialPost){
     
    302346            if(strcasecmp($platform, 'twitter') == 0){
    303347                update_post_meta($id, 'hashtags', $socialPost->getHashtags());
    304             }
     348                update_post_meta($id, 'contentImage', $socialPost->getContentImage());
     349                update_post_meta($id, 'retweeted', $socialPost->getRetweeted());
     350            }
     351
     352            $this->deleteIfOlderThanThreshold($id,$socialPost->getDate());
    305353           
    306354        }catch(Exception $e){
  • little-social/trunk/social/TwitterConnector.php

    r1618483 r1724186  
    5454        $tweets = $this->getLatestPosts($apiUrl,$args);
    5555        foreach($tweets as $tweet){
    56             //echo '<pre>' . var_export($tweet->created_at) . '</pre>';
     56            $tweet->text = $this->removeEmoji($tweet->text);
    5757        }
    58         //die();
    5958        return $this->createSocialObjects($tweets,"Tweet","twitter");
    6059
  • little-social/trunk/social/models/Instagram.php

    r1659441 r1724186  
    111111                    $hours = $diff->h;
    112112
    113                     if($hours <= 2){
     113                    if($hours < 2){
    114114                        $age = $hours . ' hour ago';
    115115                    }else{
     
    123123                $days = $diff->days;
    124124
    125                 if($days <= 2){
     125                if($days < 2){
    126126                    $age = $days . ' day ago';
    127127                }else{
  • little-social/trunk/social/models/SocialPost.php

    r1659441 r1724186  
    1515    protected $profilePic;
    1616    protected $description;
     17    protected $contentImage;
    1718
    1819    public function setId($id){
     
    5859    public function setProfilePic($profilePic){
    5960        $this->profilePic = $profilePic;
     61    }
     62
     63    public function setContentImage($contentImage){
     64        $this->contentImage = $contentImage;
    6065    }
    6166
     
    108113    }
    109114
     115    public function getContentImage(){
     116        return $this->contentImage;
     117    }
     118
    110119    public function getDescription(){
    111120        return $this->description;
     
    145154                    $hours = $diff->h;
    146155
    147                     if($hours <= 2){
     156                    if($hours < 2){
    148157                        $age = $hours . ' hour ago';
    149158                    }else{
     
    157166                $days = $diff->days;
    158167
    159                 if($days <= 2){
     168                if($days < 2){
    160169                    $age = $days . ' day ago';
    161170                }else{
  • little-social/trunk/social/models/Tweet.php

    r1618483 r1724186  
    66
    77    protected $hashtags;
     8    protected $retweeted = false;
    89
    910    public function setHashtags($hashtags){
     
    1314    public function getHashtags(){
    1415        return $this->hashtags;
     16    }
     17
     18    public function setRetweeted($retweeted){
     19        $this->retweeted = $retweeted;
     20    }
     21
     22    public function getRetweeted(){
     23        return $this->retweeted;
    1524    }
    1625
     
    2433        return 'https://twitter.com/' . $screenname;
    2534    }
    26 
    27     /*
    28     public function daysOld(){
    29        
    30         $age;
    31 
    32         $diff = 0;
    33         if(!is_null($this->getDate())){
    34             $postDate = new DateTime($this->getDate());
    35             $now = new DateTime();
    36            
    37             $diff = date_diff($now, $postDate);
    38 
    39             $days = $diff->days;
    40            
    41             if($diff->days == 0){
    42 
    43                 if($diff->h == 0){
    44                     //minutes
    45                     $age = $diff->i . ' minutes ago';
    46                 }else{
    47                     //hours
    48                     $age = $diff->h . ' hours ago';
    49                 }
    50 
    51             }else{
    52                 //days
    53                 $age = $diff->days . ' days ago';
    54             }
    55 
    56         }
    57 
    58         return $age;
    59     }*/
    6035}
Note: See TracChangeset for help on using the changeset viewer.