Changeset 10769
- Timestamp:
- 03/05/2021 12:21:47 AM (4 years ago)
- Location:
- sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/handbook.php
r10768 r10769 14 14 */ 15 15 public $post_type = ''; 16 17 /** 18 * The handbook's settings. 19 * 20 * @var array 21 */ 22 public $settings = []; 16 23 17 24 /** … … 60 67 61 68 /** 69 * Returns handbook default config. 70 * 71 * @return array 72 */ 73 public static function get_default_handbook_config() { 74 /** 75 * Filters default handbook configuration array. 76 * 77 * @param string $slug The slug for the post type. Default is post type. 78 */ 79 return (array) apply_filters( 'handbook_default_handbook_config', [ 80 'label' => '', 81 'slug' => '', 82 ] ); 83 } 84 85 /** 62 86 * Returns the handbook name. 63 87 * … … 95 119 * 96 120 * @param string $type The post type for the handbook. 97 */ 98 public function __construct( $type ) { 121 * @param array $config The config array for the handbook. 122 */ 123 public function __construct( $type, $config = [] ) { 99 124 $this->post_type = sanitize_title( $type ); 100 125 101 $this->label = ucwords( str_replace( [ '-', '_' ], ' ', $this->post_type ) ); 102 $this->label = apply_filters( 'handbook_label', $this->label, $this->post_type ); 126 $config = $this->config = wp_parse_args( $config, self::get_default_handbook_config() ); 127 128 $this->label = apply_filters( 129 'handbook_label', 130 $config['label'] ?: ucwords( str_replace( [ '-', '_' ], ' ', $this->post_type ) ), 131 $this->post_type 132 ); 103 133 104 134 $this->setting_name = $this->post_type . '_name'; … … 127 157 128 158 /** 159 * Returns the configuration array for handbooks. 160 * 161 * @return array 162 */ 163 public function get_config() { 164 return $this->config; 165 } 166 167 /** 129 168 * Adds 'Handbook Front Page' post state indicator for handbook landing pages. 130 169 * … … 235 274 */ 236 275 public function register_post_type() { 237 if ( 'handbook' != $this->post_type ) { 238 $slug = substr( $this->post_type, 0, -9 ); 276 $config = $this->get_config(); 277 278 if ( ! empty( $config['slug'] ) ) { 279 $slug = $config['slug']; 280 } elseif ( 'handbook' !== $this->post_type ) { 281 $slug = str_replace( '-handbook', '', $this->post_type ); 239 282 } else { 240 283 $slug = 'handbook'; -
sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/init.php
r10768 r10769 9 9 10 10 /** 11 * A rray of WPorg_Handbook objects.11 * Asociative array of WPorg_Handbook objects. 12 12 * 13 13 * @var array … … 42 42 */ 43 43 public static function get_post_types() { 44 /** 45 * Filters the handbook post types for creating handbooks. 46 * 47 * @param array $handbooks Array of handbook post types. Default 'handbook'. 48 */ 49 $post_types = (array) apply_filters( 'handbook_post_types', [ 'handbook' ] ); 50 return array_map( 51 function( $pt ) { 52 $pt = sanitize_title( $pt ); 53 return ( in_array( $pt, [ 'handbook', 'page' ] ) || false !== strpos( $pt, '-handbook' ) ) ? $pt : $pt . '-handbook'; 54 }, 55 $post_types 56 ); 44 return array_keys( self::$handbooks ); 45 } 46 47 /** 48 * Returns the handbooks configurations. 49 * 50 * @param string $handbook Optional. A specific handbook to return config for. 51 * If none specified, then all are returned. Default ''. 52 * @return array|false If $handbook defined, then the config for that handbook 53 * if handbook exists, else false. If no $handbook specified, 54 * then an associative array of config for all handbooks, 55 * keyed by post type. 56 */ 57 public static function get_handbooks_config( $handbook = '' ) { 58 $return = false; 59 $handbooks = self::get_handbook_objects(); 60 61 // If no handbook specified, return configs for all handbooks. 62 if ( ! $handbook ) { 63 $return = []; 64 foreach ( $handbooks as $type => $handbook_obj ) { 65 $return[ $type ] = $handbook_obj->get_config(); 66 } 67 } 68 69 return ( $handbook && ! empty( $handbooks[ $handbook ] ) && is_a( $handbooks[ $handbook ], 'WPorg_Handbook' ) ) 70 ? $handbooks[ $handbook ]->get_config() 71 : $return; 57 72 } 58 73 … … 77 92 */ 78 93 public static function init() { 94 $config = []; 95 $handbooks_config = []; 96 79 97 /** 80 98 * Fires before handbooks have been initialized. 81 99 */ 82 100 do_action( 'before_handbooks_init' ); 101 102 /** 103 * Filters the handbook post types for creating handbooks. 104 * 105 * @deprecated Use {@see 'handbooks_config'} instead. 106 * 107 * @param array $handbooks Array of handbook post types. Default empty 108 * array, which later will be interpreted to 109 * be 'handbook'. 110 */ 111 $post_types = (array) apply_filters( 'handbook_post_types', [] ); 112 113 foreach ( $post_types as $post_type ) { 114 $config[ $post_type ] = []; 115 } 116 117 /** 118 * Defines and configures all handbooks. 119 * 120 * @param array $config { 121 * Associative array of post types and their configuration options. The keys should actually be 122 * the post type base, hypenated and without appending '-handbook' (which will automatically get 123 * appended when the handbook post type is created, if the key isn't "handbook"). Default is 'handbook'. 124 * 125 * @type string $label The label for the handbook. Default is the 126 * post type slug converted to titlecase (e.g. 127 * plugin-handbok => "Plugin Handbook"). 128 * @type string $slug The slug for the post type. Default is the 129 * post type. 130 * } 131 */ 132 $config = (array) apply_filters( 'handbooks_config', $config ); 133 134 // If no handbooks were configured, default to a basic handbook. 135 if ( ! $config ) { 136 $config = [ 'handbook' => [] ]; 137 } 138 139 // Get default settings for a handbook. 140 $defaults = WPorg_Handbook::get_default_handbook_config(); 141 142 // Determine each handbook's config. 143 foreach ( $config as $key => $value ) { 144 $key = sanitize_title( $key ); 145 $post_type = ( 'handbook' === $key || false !== strpos( $key, '-handbook' ) ) ? $key : $key . '-handbook'; 146 147 $handbooks_config[ $post_type ] = wp_parse_args( $value, $defaults ); 148 149 // Set slug if not explicitly set. 150 if ( empty( $handbooks_config[ $post_type ]['slug'] ) ) { 151 $handbooks_config[ $post_type ]['slug'] = $key; 152 } 153 } 83 154 84 155 $post_types = self::get_post_types(); … … 89 160 // Instantiate each of the handbooks. 90 161 self::$handbooks = []; 91 foreach ( $ post_types as $type) {92 self::$handbooks[ ] = new WPorg_Handbook( $type);162 foreach ( $handbooks_config as $type => $conf ) { 163 self::$handbooks[ $type ] = new WPorg_Handbook( $type, $conf ); 93 164 } 94 165 -
sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/phpunit/tests/init.php
r10768 r10769 15 15 16 16 WPorg_Handbook_Init::reset( true ); 17 } 18 19 20 // 21 // 22 // DATA PROVIDERS 23 // 24 // 25 26 27 public static function get_default_config() { 28 return [ 29 [ 'label', '' ], 30 [ 'manifest', '' ], 31 [ 'slug', '' ], 32 [ 'with_front', '' ], 33 ]; 17 34 } 18 35 … … 143 160 } 144 161 162 /* 163 * get_handbooks_config() 164 */ 165 166 /** 167 * @dataProvider get_default_config 168 */ 169 public function test_get_handbooks_config_default( $key, $default ) { 170 $configs = WPorg_Handbook_Init::get_handbooks_config(); 171 $this->assertArrayHasKey( 'handbook', $configs ); 172 $this->assertEquals( 1, count( $configs ) ); 173 174 $config = $configs['handbook']; 175 176 $this->assertArrayHasKey( $key, $config ); 177 $value = $config[ $key ]; 178 179 if ( 'slug' === $key ) { 180 $this->assertEquals( 'handbook', $value ); 181 } elseif ( '' === $default ) { 182 $this->assertEmpty( $value ); 183 } else { 184 $this->assertEquals( $default, $value ); 185 } 186 } 187 188 public function test_get_handbooks_config_non_handbook() { 189 $this->assertEmpty( WPorg_Handbook_Init::get_handbooks_config( 'nonexistent-handbook' ) ); 190 } 191 192 /** 193 * @dataProvider get_default_config 194 */ 195 public function test_get_handbooks_config_specific_handbook_default( $key, $default ) { 196 $config = WPorg_Handbook_Init::get_handbooks_config( 'handbook' ); 197 198 $this->assertArrayHasKey( $key, $config ); 199 $value = $config[ $key ]; 200 201 if ( 'slug' === $key ) { 202 $this->assertEquals( 'handbook', $value ); 203 } elseif ( '' === $default ) { 204 $this->assertEmpty( $value ); 205 } else { 206 $this->assertEquals( $default, $value ); 207 } 208 } 209 210 /** 211 * @dataProvider get_default_config 212 */ 213 public function test_get_handbooks_config_specific_custom_handbook( $key, $default ) { 214 reinit_handbooks( [ 'plugins' => [] ] ); 215 216 $config = WPorg_Handbook_Init::get_handbooks_config( 'plugins-handbook' ); 217 218 $this->assertArrayHasKey( $key, $config ); 219 $value = $config[ $key ]; 220 221 if ( 'slug' === $key ) { 222 $this->assertEquals( 'plugins', $value ); 223 } elseif ( '' === $default ) { 224 $this->assertEmpty( $value ); 225 } else { 226 $this->assertEquals( $default, $value ); 227 } 228 } 229 145 230 }
Note: See TracChangeset
for help on using the changeset viewer.