Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.2 kB
1
Indexable
Never
  public static function handleShortCodes($post, $platform){

        // hashtags to database or config ::TODO
        // testing hashtags
        $hashtags = config('constants.hashtags');

        // get brand
        $brand = Brand::find($post->brand_id);
        // get time zone
        $time_zone = $brand->time_zone;

        $body = $post->body;

        // detetect custome short code created by the user
        $custom_shortcodes = json_decode($brand->short_code, true);
        if($custom_shortcodes){
            foreach ($custom_shortcodes as $key => $value) {
                if(str_contains($post->body, '{{'.$key.'}}')){
                    $valueArray = explode('|', $value);
                    $randomValue = $valueArray[array_rand($valueArray)]; 
                    $body = str_replace('{{'.$key.'}}', $randomValue, $body);
                }
            }
        }

        // detetect day short code
        if(str_contains($post->body, '{{day}}')){
            $day = Carbon::now()->setTimezone($time_zone)->dayName; 
            $body = str_replace('{{day}}', $day, $body);
        }

        // detetect month short code
        if(str_contains($post->body, '{{month}}')){
            $month = Carbon::now()->setTimezone($time_zone)->monthName; 
            $body = str_replace('{{month}}', $month, $body);
        }
        // detetect year short code
        if(str_contains($post->body, '{{year}}')){
            $year = Carbon::now()->setTimezone($time_zone)->year; 
            $body = str_replace('{{year}}', $year, $body);
        }

        // detetect platform short code
        if(str_contains($post->body, '{{platform}}')){
            $body = str_replace('{{platform}}', $platform, $body);
        }
        
        // detetect hashtag short code
        if(preg_match_all('/{{hashtag-\d+}}/', $body, $matches)){
            foreach($matches[0] as $match){
                if(array_key_exists($hash = str_replace(['{{', '}}'], '', $match), $hashtags)){ 
                    $body = str_replace($match, $hashtags[$hash], $body);
                }
            }
        }

        return $body;
    }