| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace GeminiLabs\Pollux\MetaBox; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use GeminiLabs\Pollux\Application; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use GeminiLabs\Pollux\Component; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use GeminiLabs\Pollux\MetaBox\Condition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use GeminiLabs\Pollux\MetaBox\Instruction; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use RecursiveArrayIterator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use RecursiveIteratorIterator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class MetaBox extends Component | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	use Condition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	use Instruction; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	 * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	public $metaboxes = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	 * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	public function init() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 		$this->normalize(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 		add_filter( 'rwmb_show',       [$this, 'show'], 10, 2 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		add_filter( 'rwmb_meta_boxes', [$this, 'register'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	 * @filter rwmb_meta_boxes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	public function register() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		if( current_user_can( 'switch_themes' )) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 			$this->addInstructions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 		$metaboxes = func_num_args() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 			? $this->toArray( func_get_arg(0) ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 			: []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		return array_merge( $metaboxes, $this->metaboxes ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	 * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	 * @filter rwmb_show | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 51 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  | 	public function show( $bool, array $metabox ) | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  | 		if( defined( 'DOING_AJAX' ) | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  | 			|| !isset( $metabox['condition'] ) | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  | 			|| !$this->hasPostType( $metabox )) { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  | 			return $bool; | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  | 		return $this->validate( $metabox['condition'] ); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	 * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	protected function getPostId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		if( !( $postId = filter_input( INPUT_GET, 'post' ))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 			$postId = filter_input( INPUT_POST, 'post_ID' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		return intval( $postId ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	protected function getPostTypes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 		return array_unique( iterator_to_array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 			new RecursiveIteratorIterator( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 				new RecursiveArrayIterator( array_column( $this->metaboxes, 'post_types' )) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 			), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 			false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	 * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	protected function hasPostType( array $metabox ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		if( !isset( $metabox['post_types'] )) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 			return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 		return in_array( get_post_type( $this->getPostId() ), $metabox['post_types'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	 * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 100 |  | View Code Duplication | 	protected function normalize() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 		foreach( $this->app->config['meta_boxes'] as $id => $metabox ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 			$defaults = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 				'condition' => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 				'fields' => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 				'id' => $id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 				'post_types' => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 				'slug' => $id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 			]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 			$this->metaboxes[] = $this->setDependencies( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 				$this->normalizeThis( $metabox, $defaults, $id ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 			); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	 * @param string $depends | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	 * @param string $parentId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 	protected function normalizeDepends( $depends, array $data, $parentId ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 		return is_string( $depends ) && !empty( $depends ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 			? $this->normalizeId( $depends, $data, $parentId ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 			: ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 	protected function normalizeFields( array $fields, array $data, $parentId ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 		return array_map( function( $id, $field ) use( $parentId ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 			$defaults =  [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 				'attributes' => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 				// 'condition' => [], | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 				'depends' => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 				'id' => $id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 				'field_name' => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 				'slug' => $id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 			]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 			return $this->normalizeThis( $field, $defaults, $parentId ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 		}, array_keys( $fields ), $fields ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 	 * @param string $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 	 * @param string $parentId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 	protected function normalizeId( $id, array $data, $parentId ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 		return apply_filters( 'pollux/prefix', Application::PREFIX ) . $id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 	 * @param mixed $types | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 	 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 	protected function normalizePostTypes( $types ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 		return $this->toArray( $types ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 	 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 	protected function setDependencies( array $metabox ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | 		$fields = &$metabox['fields']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 		$depends = array_column( $fields, 'depends' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 		array_walk( $depends, function( $value, $index ) use( &$fields ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 			if( empty( $value ))return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | 			$fields[$index]['attributes']['data-depends'] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 		}); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 		return $metabox; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 178 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 179 |  |  |  | 
            
                        
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.