| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * The React initial state. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @package automattic/jetpack-backup | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Automattic\Jetpack\Constants; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Automattic\Jetpack\Device_Detection\User_Agent_Info; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * The React initial state. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 14 |  | View Code Duplication | class Initial_State { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	 * Get the initial state data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	private function get_data() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 		return array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 			'API'            => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 				'WP_API_root'       => esc_url_raw( rest_url() ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 				'WP_API_nonce'      => wp_create_nonce( 'wp_rest' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 				'registrationNonce' => wp_create_nonce( 'jetpack-registration-nonce' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 			), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 			'connectionData' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 				'doNotUseConnectionIframe' => ! $this->can_use_connection_iframe(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 			), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	 * Whether we can the connection iframe. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	 * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 37 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  | 	private function can_use_connection_iframe() { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  | 		global $is_safari; | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  | 		/** | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  | 		 * Filters whether the connection manager should use the iframe authorization | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  | 		 * flow instead of the regular redirect-based flow. | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  | 		 * | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  | 		 * @since 8.3.0 | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  | 		 * | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  | 		 * @param Boolean $is_iframe_flow_used should the iframe flow be used, defaults to false. | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  | 		 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  | 		$iframe_flow = apply_filters( 'jetpack_use_iframe_authorization_flow', false ); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  | 		if ( ! $iframe_flow ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  | 			return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  | 		return ! $is_safari && ! User_Agent_Info::is_opera_desktop() && ! Constants::is_true( 'JETPACK_SHOULD_NOT_USE_CONNECTION_IFRAME' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	 * Render the initial state into a JavaScript variable. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	public function render() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		add_action( 'jetpack_use_iframe_authorization_flow', '__return_true' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		return 'var JPBACKUP_INITIAL_STATE=JSON.parse(decodeURIComponent("' . rawurlencode( wp_json_encode( $this->get_data() ) ) . '"));'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 69 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.