| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Flynt\Features\GoogleAnalytics; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Flynt\Features\AdminNotices\AdminNoticeManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Timber\Timber; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class GoogleAnalytics | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     private $gaId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     private $anonymizeIp; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     private $skippedUserRoles; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     private $skippedIps; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     public function __construct($options) | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |         $this->gaId = $options['gaId']; | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |         $this->anonymizeIp = $options['anonymizeIp']; | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |         $this->skippedUserRoles = $options['skippedUserRoles']; | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |         $this->skippedIps = $options['skippedIps']; | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |         if ($this->skippedIps) { | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |             $skippedIps = explode(',', $this->skippedIps); | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |             $this->skippedIps = array_map('trim', $skippedIps); | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         if ($this->gaId && $this->isValidId($this->gaId)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |             add_action('wp_footer', [$this, 'addScript'], 20, 1); | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         } else if ($this->gaId != '' && !isset($_POST['acf'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |             $manager = AdminNoticeManager::getInstance(); | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |             $message = ["Invalid Google Analytics Id: {$this->gaId}"]; | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |             $options = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |                 'type' => 'error', | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |                 'title' => 'Google Analytics Error', | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |                 'dismissible' => true, | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |                 'filenames' => 'functions.php' | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |             ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |             $manager->addNotice($message, $options); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function addScript() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $user = wp_get_current_user(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $trackingEnabled = !( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             $this->gaId === 'debug' // debug mode enabled | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             || $this->skippedUserRoles && array_intersect($this->skippedUserRoles, $user->roles) // current user role should be skipped | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             || is_array($this->skippedIps) && in_array($_SERVER['REMOTE_ADDR'], $this->skippedIps) // current ip should be skipped | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         Timber::render('script.twig', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             'gaId' => $this->gaId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             'trackingEnabled' => $trackingEnabled, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             'anonymizeIp' => $this->anonymizeIp | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     private function isValidId($gaId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         if ($gaId === 'debug') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             return preg_match('/^ua-\d{4,10}-\d{1,4}$/i', (string) $gaId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 64 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |  |