| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace GeminiLabs\SiteReviews\Handlers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use GeminiLabs\SiteReviews\Application; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use GeminiLabs\SiteReviews\Database\OptionManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use GeminiLabs\SiteReviews\Defaults\ValidationStringsDefaults; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use GeminiLabs\SiteReviews\Modules\Style; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use GeminiLabs\SiteReviews\Modules\Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class EnqueuePublicAssets | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	 * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	public function handle() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 		$this->enqueueAssets(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 		$this->enqueuePolyfillService(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 		$this->enqueueRecaptchaScript(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 		$this->inlineScript(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 		$this->inlineStyles(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	 * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	public function enqueueAssets() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		if( apply_filters( 'site-reviews/assets/css', true )) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 			wp_enqueue_style( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 				Application::ID, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 				$this->getStylesheet(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 				[], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 				glsr()->version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 			); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 		if( apply_filters( 'site-reviews/assets/js', true )) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 			$dependencies = apply_filters( 'site-reviews/assets/polyfill', true ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 				? [Application::ID.'/polyfill'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 				: []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 			$dependencies = apply_filters( 'site-reviews/enqueue/public/dependencies', $dependencies ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 			wp_enqueue_script( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 				Application::ID, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 				glsr()->url( 'assets/scripts/'.Application::ID.'.js' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 				$dependencies, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 				glsr()->version, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 				true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 			); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	 * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	public function enqueuePolyfillService() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		if( !apply_filters( 'site-reviews/assets/polyfill', true ))return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		wp_enqueue_script( Application::ID.'/polyfill', add_query_arg([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 			'features' => 'CustomEvent,Element.prototype.closest,Element.prototype.dataset,Event,MutationObserver', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 			'flags' => 'gated', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		], 'https://cdn.polyfill.io/v2/polyfill.min.js' )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	 * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	public function enqueueRecaptchaScript() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		// wpforms-recaptcha | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		// google-recaptcha | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		// nf-google-recaptcha | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 		if( !glsr( OptionManager::class )->isRecaptchaEnabled() )return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		$language = apply_filters( 'site-reviews/recaptcha/language', get_locale() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		wp_enqueue_script( Application::ID.'/google-recaptcha', add_query_arg([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 			'hl' => $language, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 			'render' => 'explicit', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 		], 'https://www.google.com/recaptcha/api.js' )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	 * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	public function inlineScript() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		$variables = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 			'action' => Application::PREFIX.'action', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 			'ajaxpagination' => $this->getFixedSelectorsForPagination(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 			'ajaxurl' => admin_url( 'admin-ajax.php' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 			'nameprefix' => Application::ID, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 			'validationconfig' => glsr( Style::class )->validation, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 			'validationstrings' => glsr( ValidationStringsDefaults::class )->defaults(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 		$variables = apply_filters( 'site-reviews/enqueue/public/localize', $variables ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		$script = ['window.hasOwnProperty("GLSR")||(window.GLSR={})']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 		foreach( $variables as $key => $value ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 			$script[] = sprintf( 'GLSR.%s = %s', $key, wp_json_encode( $value )); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		wp_add_inline_script( Application::ID, implode( ';', $script )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	/** | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 103 |  |  | 	 * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  | 	public function inlineStyles() | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  | 		$inlineStylesheetPath = glsr()->path( 'assets/styles/inline-styles.css' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  | 		if( !apply_filters( 'site-reviews/assets/css', true ))return; | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  | 		if( !file_exists( $inlineStylesheetPath )) { | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  | 			glsr_log()->error( 'Inline stylesheet is missing: '.$inlineStylesheetPath ); | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  | 			return; | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  | 		$inlineStylesheetValues = glsr()->config( 'inline-styles' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  | 		$stylesheet = str_replace( | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  | 			array_keys( $inlineStylesheetValues ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 			array_values( $inlineStylesheetValues ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 			file_get_contents( $inlineStylesheetPath ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		wp_add_inline_style( Application::ID, $stylesheet ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	protected function getFixedSelectorsForPagination() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 		$selectors = ['#wpadminbar','.site-navigation-fixed']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 		return apply_filters( 'site-reviews/enqueue/public/localize/ajax-pagination', $selectors ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 	protected function getStylesheet() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 		$currentStyle = glsr( Style::class )->style; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 		return file_exists( glsr()->path( 'assets/styles/custom/'.$currentStyle.'.css' )) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 			? glsr()->url( 'assets/styles/custom/'.$currentStyle.'.css' ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 			: glsr()->url( 'assets/styles/'.Application::ID.'.css' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 141 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 142 |  |  |  |