| 1 |  |  | <?php | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace GV; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | /** If this file is called directly, abort. */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | if ( ! defined( 'GRAVITYVIEW_DIR' ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | 	die(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * The Gravity Forms Form class implementation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Accessible as an array for back-compatibility. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class GF_Form extends Form implements \ArrayAccess { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	 * @var string The identifier of the backend used for this form. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	 * @api | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	 * @since future | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	public static $backend = self::BACKEND_GRAVITYFORMS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	 * Initialization. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	 * @throws \RuntimeException if the Gravity Forms plugin is not active. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 2 |  | 	private function __construct() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 2 |  | 		if ( ! class_exists( 'GFAPI' ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 			throw new \RuntimeException( 'Gravity Forms plugin not active.' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 2 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	 * Construct a \GV\GF_Form instance by ID. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	 * @param int|string $form_id The internal form ID. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	 * @api | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	 * @since future | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	 * @return \GV\GF_Form|null An instance of this form or null if not found. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 3 |  | 	public static function by_id( $form_id ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 3 |  | 		$form = \GFAPI::get_form( $form_id ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 3 |  | 		if ( !$form ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 1 |  | 			return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 3 |  | 		$self = new self(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 3 |  | 		$self->form = $form; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 3 |  | 		$self->ID = $self->form['id']; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 3 |  | 		return $self; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	 * Get all entries for this form. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	 * @api | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	 * @since future | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	 * @return \GV\Entry_Collection The \GV\Entry_Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	public function get_entries() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		$entries = new \GV\Entry_Collection(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		$form = &$this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		/** Add the fetcher lazy callback. */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		$entries->add_fetch_callback( function( $filters, $sorts, $offset ) use ( $form ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 			$entries = new \GV\Entry_Collection(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 			$search_criteria = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 			$sorting = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 			$paging = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 			/** Apply the filters */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 			foreach ( $filters as $filter ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 				$search_criteria = $filter::merge_search_criteria( $search_criteria, $filter->as_search_criteria() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 			/** Apply the sorts */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 			foreach ( $sorts as $sort ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 				/** Gravity Forms does not have multi-sorting, so just overwrite. */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 				$sorting = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 					'key' => $sort->field->ID, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 					'direction' => $sort->direction, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 				); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 			/** The offset and limit */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 			if ( ! empty( $offset->limit ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 				$paging['page_size'] = $offset->limit; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 			if ( ! empty( $offset->offset ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 				$paging['offset'] = $offset->offset; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 			foreach ( \GFAPI::get_entries( $form->ID, $search_criteria, $sorting, $paging ) as $entry ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 				$entries->add( \GV\GF_Entry::by_id( $entry['id'] ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 			return $entries; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 		} ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 		/** Add the counter lazy callback. */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 		$entries->add_count_callback( function( $filters ) use ( $form ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 			$search_criteria = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 			$sorting = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 			/** Apply the filters */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 			foreach ( $filters as $filter ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 				$search_criteria = $filter::merge_search_criteria( $search_criteria, $filter->as_search_criteria() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 			return \GFAPI::count_entries( $form->ID, $search_criteria ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 		} ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 		return $entries; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 	 * ArrayAccess compatibility layer with a Gravity Forms form array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	 * @internal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	 * @deprecated | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 	 * @since future | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 	 * @return bool Whether the offset exists or not. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 132 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  | 	public function offsetExists( $offset ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  | 		return isset( $this->form[$offset] ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 	 * ArrayAccess compatibility layer with a Gravity Forms form array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 	 * Maps the old keys to the new data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 	 * @internal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 	 * @deprecated | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 	 * @since future | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 	 * @throws \RuntimeException during tests if called outside of whiteliested cases. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 	 * @return mixed The value of the requested form data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 	public function offsetGet( $offset ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 		return $this->form[$offset]; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 	 * ArrayAccess compatibility layer with a Gravity Forms form array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 	 * @internal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 	 * @deprecated | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 	 * @since future | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 	 * @throws \RuntimeException The underlying form data is immutable. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 	 * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 	public function offsetSet( $offset, $value ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 		throw new \RuntimeException( 'The underlying Gravity Forms form is immutable. This is a \GV\Form object and should not be accessed as an array.' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | 	 * ArrayAccess compatibility layer with a Gravity Forms form array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 	 * @internal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 	 * @deprecated | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | 	 * @since future | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 	 * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 	public function offsetUnset( $offset ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 		throw new \RuntimeException( 'The underlying Gravity Forms form is immutable. This is a \GV\Form object and should not be accessed as an array.' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 180 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 181 |  |  |  | 
            
                        
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.