| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace GeminiLabs\Pollux\PostType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use GeminiLabs\Pollux\Component; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use GeminiLabs\Pollux\PostType\Columns; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class PostType extends Component | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | 	use Columns; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 	const CUSTOM_KEYS = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 		'columns', 'menu_name', 'plural', 'single', 'slug', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	const POST_TYPE_DEFAULTS = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 		'capability_type' => 'post', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 		'columns' => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 		'has_archive' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 		'hierarchical' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 		'labels' => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 		'map_meta_cap' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 		'menu_icon' => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 		'menu_name' => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 		'menu_position' => 5, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 		'plural' => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 		'public' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 		'query_var' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 		'rewrite' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		'show_in_menu' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 		'show_ui' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 		'single' => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 		'slug' => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 		'supports' => ['title', 'editor', 'thumbnail'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 		'taxonomies' => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	 * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	public $types = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	 * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	public function init() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		$this->setColumns(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 		$this->normalize(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		$this->initColumns(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		add_action( 'init', [$this, 'register'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	 * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	 * @action init | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	public function register() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		$types = array_diff_key( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 			$this->types, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 			get_post_types( ['_builtin' => true] ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		array_walk( $types, function( $args, $type ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 			register_post_type( $type, array_diff_key( $args, array_flip( static::CUSTOM_KEYS ))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		}); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	 * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	protected function normalize() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 75 |  | View Code Duplication | 		foreach( $this->app->config['post_types'] as $type => $args ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 			$this->types[$type] = apply_filters( 'pollux/post_type/args', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 				$this->normalizeThis( $args, static::POST_TYPE_DEFAULTS, $type ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 				$type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 			); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	 * @param mixed $labels | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	protected function normalizeLabels( $labels, array $args ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		return wp_parse_args( $labels, $this->setLabels( $args )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	 * @param string $menuname | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	protected function normalizeMenuName( $menuname, array $args ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 		return empty( $menuname ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 			? $args['plural'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 			: $menuname; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	 * @param mixed $rewrite | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	 * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 	protected function normalizeRewrite( $rewrite, array $args ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 		if( $rewrite === true ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 			$slug = empty( $args['slug'] ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 				? sanitize_title( $args['plural'] ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 				: $args['slug']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 			$rewrite = ['slug' => $slug, 'with_front' => false]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 		return $rewrite; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 	 * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 120 |  |  | 	 */ | 
            
                                                        
            
                                    
            
            
                | 121 |  |  | 	protected function setLabels( array $args ) | 
            
                                                        
            
                                    
            
            
                | 122 |  |  | 	{ | 
            
                                                        
            
                                    
            
            
                | 123 |  |  | 		return apply_filters( 'pollux/post_type/labels', [ | 
            
                                                        
            
                                    
            
            
                | 124 |  |  | 			'add_new' => __( 'Add New', 'pollux' ), | 
            
                                                        
            
                                    
            
            
                | 125 |  |  | 			'add_new_item' => sprintf( _x( 'Add New %s', 'Add new post', 'pollux' ), $args['single'] ), | 
            
                                                        
            
                                    
            
            
                | 126 |  |  | 			'all_items' => sprintf( _x( 'All %s', 'All posts', 'pollux' ), $args['plural'] ), | 
            
                                                        
            
                                    
            
            
                | 127 |  |  | 			'edit_item' => sprintf( _x( 'Edit %s', 'Edit post', 'pollux' ), $args['single'] ), | 
            
                                                        
            
                                    
            
            
                | 128 |  |  | 			'menu_name' => $this->normalizeMenuName( $args['menu_name'], $args ), | 
            
                                                        
            
                                    
            
            
                | 129 |  |  | 			'name' => $args['plural'], | 
            
                                                        
            
                                    
            
            
                | 130 |  |  | 			'new_item' => sprintf( _x( 'New %s', 'New post', 'pollux' ), $args['single'] ), | 
            
                                                        
            
                                    
            
            
                | 131 |  |  | 			'not_found' => sprintf( _x( 'No %s found', 'No posts found', 'pollux' ), $args['plural'] ), | 
            
                                                        
            
                                    
            
            
                | 132 |  |  | 			'not_found_in_trash' => sprintf( _x( 'No %s found in Trash', 'No posts found in trash', 'pollux' ), $args['plural'] ), | 
            
                                                        
            
                                    
            
            
                | 133 |  |  | 			'search_items' => sprintf( _x( 'Search %s', 'Search posts', 'pollux' ), $args['plural'] ), | 
            
                                                        
            
                                    
            
            
                | 134 |  |  | 			'singular_name' => $args['single'], | 
            
                                                        
            
                                    
            
            
                | 135 |  |  | 			'view_item' => sprintf( _x( 'View %s', 'View post', 'pollux' ), $args['single'] ), | 
            
                                                        
            
                                    
            
            
                | 136 |  |  | 		], $args ); | 
            
                                                        
            
                                    
            
            
                | 137 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 138 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 139 |  |  |  |