Ticket #18375: 18375.3.diff
File 18375.3.diff, 22.7 KB (added by , 9 years ago) |
---|
-
src/wp-admin/edit-form-advanced.php
259 259 add_meta_box( $tax_meta_box_id, $label, $taxonomy->meta_box_cb, null, 'side', 'core', array( 'taxonomy' => $tax_name ) ); 260 260 } 261 261 262 if ( post_type_supports($post_type, 'page-attributes') ) 263 add_meta_box('pageparentdiv', 'page' == $post_type ? __('Page Attributes') : __('Attributes'), 'page_attributes_meta_box', null, 'side', 'core'); 262 if ( post_type_supports( $post_type, 'page-attributes' ) ) { 263 add_meta_box( 'pageparentdiv', esc_html( $post_type_object->labels->singular_name . __('Attributes') ), 'page_attributes_meta_box', null, 'side', 'core' ); 264 } 264 265 265 266 if ( $thumbnail_support && current_user_can( 'upload_files' ) ) 266 267 add_meta_box('postimagediv', esc_html( $post_type_object->labels->featured_image ), 'post_thumbnail_meta_box', null, 'side', 'low'); -
src/wp-admin/includes/class-wp-posts-list-table.php
1529 1529 </label> 1530 1530 1531 1531 <?php endif; // !$bulk 1532 1533 if ( 'page' === $screen->post_type ) :1534 1532 ?> 1535 1533 1536 1534 <label> … … 1544 1542 $default_title = apply_filters( 'default_page_template_title', __( 'Default Template' ), 'quick-edit' ); 1545 1543 ?> 1546 1544 <option value="default"><?php echo esc_html( $default_title ); ?></option> 1547 <?php page_template_dropdown( ) ?>1545 <?php page_template_dropdown( $post->page_template, $screen->post_type ) ?> 1548 1546 </select> 1549 1547 </label> 1550 1548 1551 1549 <?php 1552 endif; // page post_type1553 1550 endif; // page-attributes 1554 1551 ?> 1555 1552 -
src/wp-admin/includes/meta-boxes.php
788 788 * @param object $post 789 789 */ 790 790 function page_attributes_meta_box($post) { 791 $post_type_object = get_post_type_object($post->post_type); 792 if ( $post_type_object->hierarchical ) { 791 if ( is_post_type_hierarchical( $post->post_type ) ) { 793 792 $dropdown_args = array( 794 793 'post_type' => $post->post_type, 795 794 'exclude_tree' => $post->ID, … … 820 819 <?php 821 820 } // end empty pages check 822 821 } // end hierarchical check. 823 if ( 'page' == $post->post_type && 0 != count( get_page_templates( $post ) ) && get_option( 'page_for_posts' ) != $post->ID ) { 824 $template = !empty($post->page_template) ? $post->page_template : false; 822 823 if ( 0 < count( get_page_templates( $post ) ) && get_option( 'page_for_posts' ) != $post->ID ) { 824 $template = ! empty( $post->page_template ) ? $post->page_template : false; 825 825 ?> 826 826 <p><strong><?php _e('Template') ?></strong><?php 827 827 /** … … 835 835 */ 836 836 do_action( 'page_attributes_meta_box_template', $template, $post ); 837 837 ?></p> 838 <label class="screen-reader-text" for="page_template"><?php _e(' PageTemplate') ?></label><select name="page_template" id="page_template">838 <label class="screen-reader-text" for="page_template"><?php _e('Template') ?></label><select name="page_template" id="page_template"> 839 839 <?php 840 840 /** 841 841 * Filters the title of the default page template displayed in the drop-down. … … 849 849 $default_title = apply_filters( 'default_page_template_title', __( 'Default Template' ), 'meta-box' ); 850 850 ?> 851 851 <option value="default"><?php echo esc_html( $default_title ); ?></option> 852 <?php page_template_dropdown( $template); ?>852 <?php page_template_dropdown( $template, $post->post_type ); ?> 853 853 </select> 854 854 <?php 855 855 } ?> -
src/wp-admin/includes/template.php
293 293 <div class="ss">' . mysql2date( 's', $post->post_date, false ) . '</div> 294 294 <div class="post_password">' . esc_html( $post->post_password ) . '</div>'; 295 295 296 if ( $post_type_object->hierarchical ) 296 if ( $post_type_object->hierarchical ) { 297 297 echo '<div class="post_parent">' . $post->post_parent . '</div>'; 298 } 298 299 299 if ( $post->post_type == 'page' ) 300 echo '<div class="page_template">' . esc_html( get_post_meta( $post->ID, '_wp_page_template', true ) ) . '</div>'; 300 echo '<div class="page_template">' . esc_html( $post->page_template ) . '</div>'; 301 301 302 if ( post_type_supports( $post->post_type, 'page-attributes' ) ) 302 if ( post_type_supports( $post->post_type, 'page-attributes' ) ) { 303 303 echo '<div class="menu_order">' . $post->menu_order . '</div>'; 304 } 304 305 305 306 $taxonomy_names = get_object_taxonomies( $post->post_type ); 306 307 foreach ( $taxonomy_names as $taxonomy_name) { … … 761 762 * Print out option HTML elements for the page templates drop-down. 762 763 * 763 764 * @since 1.5.0 765 * @since 4.7.0 Added the `$post_type` parameter. 764 766 * 765 * @param string $default Optional. The template file name. Default empty. 767 * @param string $default Optional. The template file name. Default empty. 768 * @param string $post_type Optional. Post type to get templates for. Default 'post'. 766 769 */ 767 function page_template_dropdown( $default = '' ) {768 $templates = get_page_templates( get_post());770 function page_template_dropdown( $default = '', $post_type = 'page' ) { 771 $templates = get_page_templates( null, $post_type ); 769 772 ksort( $templates ); 770 773 foreach ( array_keys( $templates ) as $template ) { 771 774 $selected = selected( $default, $templates[ $template ], false ); -
src/wp-admin/includes/theme.php
102 102 * Get the Page Templates available in this theme 103 103 * 104 104 * @since 1.5.0 105 * @since 4.7.0 Added the `$post_type` parameter. 105 106 * 106 * @param WP_Post|null $post Optional. The post being edited, provided for context. 107 * @param WP_Post|null $post Optional. The post being edited, provided for context. 108 * @param string $post_type Optional. Post type to get the templates for. Default 'page'. 107 109 * @return array Key is the template name, value is the filename of the template 108 110 */ 109 function get_page_templates( $post = null ) {110 return array_flip( wp_get_theme()->get_page_templates( $post ) );111 function get_page_templates( $post = null, $post_type = 'page' ) { 112 return array_flip( wp_get_theme()->get_page_templates( $post, $post_type ) ); 111 113 } 112 114 113 115 /** -
src/wp-includes/class-wp-post.php
254 254 return true; 255 255 256 256 if ( 'page_template' == $key ) 257 return ( 'page' == $this->post_type );257 return true; 258 258 259 259 if ( 'post_category' == $key ) 260 260 return true; -
src/wp-includes/class-wp-theme.php
1006 1006 } 1007 1007 1008 1008 /** 1009 * Returns the theme's p agetemplates.1009 * Returns the theme's post templates. 1010 1010 * 1011 * @since 3.4.01011 * @since 4.7.0 1012 1012 * @access public 1013 1013 * 1014 * @ param WP_Post|null $post Optional. The post being edited, provided for context.1015 * @return array Array of page templates, keyed by filename,with the value of the translated header name.1014 * @return array Array of page templates, keyed by filename and post type, 1015 * with the value of the translated header name. 1016 1016 */ 1017 public function get_p age_templates( $post = null) {1017 public function get_post_templates() { 1018 1018 // If you screw up your current theme and we invalidate your parent, most things still work. Let it slide. 1019 if ( $this->errors() && $this->errors()->get_error_codes() !== array( 'theme_parent_invalid' ) ) 1019 if ( $this->errors() && $this->errors()->get_error_codes() !== array( 'theme_parent_invalid' ) ) { 1020 1020 return array(); 1021 } 1021 1022 1022 $p age_templates = $this->cache_get( 'page_templates' );1023 $post_templates = $this->cache_get( 'post_templates' ); 1023 1024 1024 if ( ! is_array( $p age_templates ) ) {1025 $p age_templates = array();1025 if ( ! is_array( $post_templates ) ) { 1026 $post_templates = array(); 1026 1027 1027 1028 $files = (array) $this->get_files( 'php', 1 ); 1028 1029 1029 1030 foreach ( $files as $file => $full_path ) { 1030 if ( ! preg_match( '|Template Name:(.*)$|mi', file_get_contents( $full_path ), $header ) ) 1031 $template_data = get_file_data( $full_path, array( 'Template Name' => 'Template Name', 'Template Type' => 'Template Type' ) ); 1032 if ( empty( $template_data[ 'Template Name' ] ) ) { 1031 1033 continue; 1032 $page_templates[ $file ] = _cleanup_header_comment( $header[1] ); 1034 } 1035 1036 $types = array( 'page' ); 1037 if ( !empty( $template_data[ 'Template Type' ] ) ) { 1038 $types = explode( ',', _cleanup_header_comment( $template_data[ 'Template Type' ] ) ); 1039 } 1040 1041 foreach ( $types as $type ) { 1042 $type = trim( $type ); 1043 if ( ! isset( $post_templates[ $type ] ) ) { 1044 $post_templates[ $type ] = array(); 1045 } 1046 1047 $post_templates[ $type ][ $file ] = _cleanup_header_comment( $template_data[ 'Template Name' ] ); 1048 } 1033 1049 } 1034 1050 1035 $this->cache_add( 'page_templates', $p age_templates );1051 $this->cache_add( 'page_templates', $post_templates ); 1036 1052 } 1037 1053 1038 1054 if ( $this->load_textdomain() ) { 1039 foreach ( $page_templates as &$page_template ) { 1040 $page_template = $this->translate_header( 'Template Name', $page_template ); 1055 foreach ( $post_templates as &$post_type ) { 1056 foreach ( $post_type as &$post_template ) { 1057 $post_template = $this->translate_header( 'Template Name', $post_template ); 1058 } 1041 1059 } 1042 1060 } 1043 1061 1044 if ( $this->parent() )1045 $page_templates += $this->parent()->get_page_templates( $post );1062 return $post_templates; 1063 } 1046 1064 1065 /** 1066 * Returns the theme's post templates for a given post type. 1067 * 1068 * @since 3.4.0 1069 * @since 4.7.0 Added the `$post_type` parameter. 1070 * @access public 1071 * 1072 * @param WP_Post|null $post Optional. The post being edited, provided for context. 1073 * @param string $post_type Optional. Post type to get the templates for. Default 'page'. 1074 * If a post is provided, its post type is used. 1075 * @return array Array of page templates, keyed by filename, with the value of the translated header name. 1076 */ 1077 public function get_page_templates( $post = null, $post_type = 'page' ) { 1078 if ( $post ) { 1079 $post_type = get_post_type( $post ); 1080 } 1081 1082 $post_templates = $this->get_post_templates(); 1083 $post_templates = isset( $post_templates[ $post_type ] ) ? $post_templates[ $post_type ] : array(); 1084 1085 if ( $this->parent() ) { 1086 $post_templates += $this->parent()->get_page_templates( $post ); 1087 } 1088 1047 1089 /** 1048 1090 * Filters list of page templates for a theme. 1049 1091 * 1050 1092 * @since 3.9.0 1051 1093 * @since 4.4.0 Converted to allow complete control over the `$page_templates` array. 1094 * @since 4.7.0 Added the `$post_type` parameter. 1052 1095 * 1053 * @param array $p age_templates Array of page templates. Keys are filenames,1096 * @param array $post_templates Array of page templates. Keys are filenames, 1054 1097 * values are translated names. 1055 1098 * @param WP_Theme $this The theme object. 1056 1099 * @param WP_Post|null $post The post being edited, provided for context, or null. 1100 * @param string $post_type Post type to get the templates for. 1057 1101 */ 1058 return (array) apply_filters( 'theme_page_templates', $p age_templates, $this, $post);1102 return (array) apply_filters( 'theme_page_templates', $post_templates, $this, $post, $post_type ); 1059 1103 } 1060 1104 1061 1105 /** -
src/wp-includes/post-template.php
1621 1621 * 1622 1622 * @since 2.5.0 1623 1623 * @since 4.2.0 The `$template` parameter was changed to also accept an array of page templates. 1624 * @since 4.7.0 Now supports any post type, not just pages. 1624 1625 * 1625 1626 * @param string|array $template The specific template name or array of templates to match. 1626 1627 * @return bool True on success, false on failure. 1627 1628 */ 1628 1629 function is_page_template( $template = '' ) { 1629 if ( ! is_page() )1630 return false;1631 1632 1630 $page_template = get_page_template_slug( get_queried_object_id() ); 1633 1631 1634 1632 if ( empty( $template ) ) … … 1649 1647 } 1650 1648 1651 1649 /** 1652 * Get the specific template name for a page.1650 * Get the specific template name for a given post. 1653 1651 * 1654 1652 * @since 3.4.0 1653 * @since 4.7.0 Now supports all post types. 1655 1654 * 1656 * @param int $post_id Optional. The page ID to check. Defaults to the current post, when used in the loop.1655 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. 1657 1656 * @return string|false Page template filename. Returns an empty string when the default page template 1658 * is in use. Returns false if the post is not a page.1657 * is in use. Returns false if the post does not exist. 1659 1658 */ 1660 function get_page_template_slug( $post_id = null ) { 1661 $post = get_post( $post_id ); 1662 if ( ! $post || 'page' != $post->post_type ) 1659 function get_page_template_slug( $post = null ) { 1660 $post = get_post( $post ); 1661 1662 if ( ! $post ) { 1663 1663 return false; 1664 } 1665 1664 1666 $template = get_post_meta( $post->ID, '_wp_page_template', true ); 1665 if ( ! $template || 'default' == $template ) 1667 1668 if ( ! $template || 'default' == $template ) { 1666 1669 return ''; 1670 } 1671 1667 1672 return $template; 1668 1673 } 1669 1674 -
src/wp-includes/post.php
1326 1326 * post types. Default is 'Parent Page:'. 1327 1327 * - `all_items` - Label to signify all items in a submenu link. Default is 'All Posts' / 'All Pages'. 1328 1328 * - `archives` - Label for archives in nav menus. Default is 'Post Archives' / 'Page Archives'. 1329 * - `attributes` - Label for the attributes meta box. Default is 'Post Attributes' / 'Page Attributes'. 1329 1330 * - `insert_into_item` - Label for the media frame button. Default is 'Insert into post' / 'Insert into page'. 1330 1331 * - `uploaded_to_this_item` - Label for the media frame filter. Default is 'Uploaded to this post' / 1331 1332 * 'Uploaded to this page'. … … 1611 1612 * @since 4.4.0 1612 1613 * @since 4.5.0 Added the ability to pass a post type name in addition to object. 1613 1614 * @since 4.6.0 Converted the `$post_type` parameter to accept a WP_Post_Type object. 1615 * @since 4.7.0 Added the `attributes` label. 1614 1616 * 1615 1617 * @param string|WP_Post_Type $post_type Post type name or object. 1616 1618 * @return bool Whether the post type should be considered viewable. … … 3393 3395 3394 3396 $post = get_post( $post_ID ); 3395 3397 3396 if ( ! empty( $postarr['page_template'] ) && 'page' == $data['post_type']) {3398 if ( ! empty( $postarr['page_template'] ) ) { 3397 3399 $post->page_template = $postarr['page_template']; 3398 3400 $page_templates = wp_get_theme()->get_page_templates( $post ); 3399 3401 if ( 'default' != $postarr['page_template'] && ! isset( $page_templates[ $postarr['page_template'] ] ) ) { -
src/wp-includes/template.php
477 477 $templates = array(); 478 478 479 479 if ( ! empty( $object->post_type ) ) { 480 $template = get_page_template_slug( $object ); 481 if ( $template && 0 === validate_file( $template ) ) { 482 $templates[] = $template; 483 } 480 484 481 485 $name_decoded = urldecode( $object->post_name ); 482 486 if ( $name_decoded !== $object->post_name ) { -
tests/phpunit/tests/admin/includesTheme.php
48 48 49 49 switch_theme( $theme['Template'], $theme['Stylesheet'] ); 50 50 51 $t emplates = get_page_templates();52 $this->assertCount( 3, $templates );53 $this->assertEquals( "template-top-level.php", $templates['Top Level'] );54 $this->assertEquals( "subdir/template-sub-dir.php", $templates['Sub Dir'] );55 $this->assertEquals( "template-header.php", $templates['This Template Header Is On One Line']);51 $this->assertEqualSetsWithIndex( array( 52 'Top Level' => 'template-top-level.php', 53 'Sub Dir' => 'subdir/template-sub-dir.php', 54 'This Template Header Is On One Line' => 'template-header.php', 55 ), get_page_templates() ); 56 56 57 57 $theme = wp_get_theme( 'page-templates' ); 58 58 $this->assertNotEmpty( $theme ); … … 59 59 60 60 switch_theme( $theme['Template'], $theme['Stylesheet'] ); 61 61 62 $t emplates = get_page_templates();63 $this->assertCount( 3, $templates );64 $this->assertEquals( "template-top-level.php", $templates['Top Level'] );65 $this->assertEquals( "subdir/template-sub-dir.php", $templates['Sub Dir'] );66 $this->assertEquals( "template-header.php", $templates['This Template Header Is On One Line']);62 $this->assertEqualSetsWithIndex( array( 63 'Top Level' => 'template-top-level.php', 64 'Sub Dir' => 'subdir/template-sub-dir.php', 65 'This Template Header Is On One Line' => 'template-header.php', 66 ), get_page_templates() ); 67 67 } 68 69 /** 70 * @ticket 18375 71 */ 72 function test_page_templates_different_post_types() { 73 $theme = wp_get_theme( 'page-templates' ); 74 $this->assertNotEmpty( $theme ); 75 76 switch_theme( $theme['Template'], $theme['Stylesheet'] ); 77 78 $this->assertEqualSetsWithIndex( array( 79 'Top Level' => 'template-top-level-post-types.php', 80 'Sub Dir' => 'subdir/template-sub-dir-post-types.php', 81 ), get_page_templates( null, 'foo' ) ); 82 $this->assertEqualSetsWithIndex( array( 83 'Top Level' => 'template-top-level-post-types.php', 84 'Sub Dir' => 'subdir/template-sub-dir-post-types.php', 85 ), get_page_templates( null, 'post' ) ); 86 $this->assertEquals( array(), get_page_templates( null, 'bar' ) ); 87 } 68 88 } -
tests/phpunit/tests/post/objects.php
134 134 $this->assertEquals( array( 'Bar', 'Baz', 'Foo' ), $post['tags_input'] ); 135 135 } 136 136 137 /** 138 * @ticket 18375 139 */ 137 140 function test_get_page_template_property() { 138 141 $post_id = self::factory()->post->create(); 139 142 $post = get_post( $post_id ); … … 145 148 update_post_meta( $post_id, '_wp_page_template', 'foo.php' ); 146 149 $template = get_post_meta( $post->ID, '_wp_page_template', true ); 147 150 $this->assertEquals( 'foo.php', $template ); 148 // The post is not a page so the template is still empty149 $this->assertEquals( '', $post->page_template );150 151 // Now the post is a page and should retrieve the template152 wp_update_post( array( 'ID' => $post->ID, 'post_type' => 'page' ) );153 $post = get_post( $post_id );154 151 $this->assertEquals( $template, $post->page_template ); 155 152 } 156 153 -
tests/phpunit/tests/post/template.php
271 271 272 272 /** 273 273 * @ticket 31389 274 * @ticket 18375 274 275 */ 275 276 public function test_get_page_template_slug_non_page() { 276 $post_id = self::factory()->post->create( array( 277 'post_type' => 'post', 278 ) ); 277 $post_id = self::factory()->post->create(); 279 278 280 $this->assert False(get_page_template_slug( $post_id ) );279 $this->assertEquals( '', get_page_template_slug( $post_id ) ); 281 280 281 update_post_meta( $post_id, '_wp_page_template', 'default' ); 282 283 $this->assertEquals( '', get_page_template_slug( $post_id ) ); 284 285 update_post_meta( $post_id, '_wp_page_template', 'example.php' ); 286 $this->assertEquals( 'example.php', get_page_template_slug( $post_id ) ); 287 } 288 289 /** 290 * @ticket 18375 291 */ 292 public function test_get_page_template_slug_non_page_from_loop() { 293 $post_id = self::factory()->post->create(); 294 295 update_post_meta( $post_id, '_wp_page_template', 'example.php' ); 296 282 297 $this->go_to( get_permalink( $post_id ) ); 283 $this->assertFalse( get_page_template_slug() ); 298 299 $this->assertEquals( 'example.php', get_page_template_slug() ); 284 300 } 285 301 286 302 /** -
tests/phpunit/tests/query/conditionals.php
1044 1044 } 1045 1045 1046 1046 /** 1047 * @ticket 18375 1048 */ 1049 function test_is_page_template_other_post_type() { 1050 $post_id = self::factory()->post->create( array( 'post_type' => 'post' ) ); 1051 update_post_meta( $post_id, '_wp_page_template', 'example.php' ); 1052 $this->go_to( get_post_permalink( $post_id ) ); 1053 $this->assertFalse( is_page_template( array( 'test.php' ) ) ); 1054 $this->assertTrue( is_page_template( array( 'test.php', 'example.php' ) ) ); 1055 } 1056 1057 /** 1047 1058 * @ticket 35902 1048 1059 */ 1049 1060 public function test_is_attachment_should_not_match_numeric_id_to_post_title_beginning_with_id() { -
tests/phpunit/tests/template.php
36 36 'post_date' => '1984-02-25 12:34:56', 37 37 ) ); 38 38 set_post_format( self::$post, 'quote' ); 39 add_post_meta( self::$post->ID, '_wp_page_template', 'templates/post.php' ); 39 40 } 40 41 41 42 public function setUp() { … … 203 204 ) ); 204 205 } 205 206 207 /** 208 * @ticket 18375 209 */ 206 210 public function test_single_template_hierarchy_for_post() { 207 211 $this->assertTemplateHierarchy( get_permalink( self::$post ), array( 212 'templates/post.php', 208 213 'single-post-post-name-😀.php', 209 214 'single-post-post-name-%f0%9f%98%80.php', 210 215 'single-post.php', … … 228 233 ) ); 229 234 } 230 235 236 /** 237 * @ticket 18375 238 */ 239 public function test_single_template_hierarchy_for_custom_post_type_with_page_template() { 240 $cpt = self::factory()->post->create_and_get( array( 241 'post_type' => 'cpt', 242 'post_name' => 'cpt-name-😀', 243 ) ); 244 add_post_meta( $cpt->ID, '_wp_page_template', 'templates/cpt.php' ); 245 246 $this->assertTemplateHierarchy( get_permalink( $cpt ), array( 247 'templates/cpt.php', 248 'single-cpt-cpt-name-😀.php', 249 'single-cpt-cpt-name-%f0%9f%98%80.php', 250 'single-cpt.php', 251 'single.php', 252 'singular.php', 253 ) ); 254 } 255 231 256 public function test_attachment_template_hierarchy() { 232 257 $attachment = self::factory()->attachment->create_and_get( array( 233 258 'post_name' => 'attachment-name-😀',