WordPress.org

Make WordPress Core

Changeset 22127


Ignore:
Timestamp:
10/06/12 15:19:29 (21 months ago)
Author:
ryan
Message:

Better UI for doing "Page on Front".

Props SergeyBiryukov, lessbloat, nacin.

see #16379

Location:
trunk
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/css/wp-admin-rtl.css

    r22031 r22127  
    916916} 
    917917 
    918 #front-page-warning, 
    919 #front-static-pages ul, 
     918#front-static-pages .sub-option, 
    920919ul.export-filters, 
    921920.inline-editor ul.cat-checklist ul, 
  • trunk/wp-admin/css/wp-admin.css

    r22099 r22127  
    29302930} 
    29312931 
     2932#front-static-pages #edit-slug-box { 
     2933    padding: 0; 
     2934} 
     2935 
    29322936#editable-post-name-full { 
    29332937    display: none; 
     
    34153419} 
    34163420 
    3417 #front-page-warning, 
    3418 #front-static-pages ul, 
     3421#front-static-pages .sub-option, 
    34193422ul.export-filters, 
    34203423.inline-editor ul.cat-checklist ul, 
     
    49894992} 
    49904993 
     4994.js.options-reading-php .if-page-on-front, 
     4995.js.options-reading-php .if-page-for-posts, 
     4996.options-reading-php .if-new-front-page { 
     4997    display: none; 
     4998} 
     4999.options-reading-php .page-on-front .if-page-on-front, 
     5000.options-reading-php .page-for-posts .if-page-for-posts { 
     5001    display: block; 
     5002} 
     5003.options-reading-php .new-front-page .if-new-front-page { 
     5004    display: inline; 
     5005} 
     5006 
    49915007/*------------------------------------------------------------------------------ 
    49925008  21.0 - Admin Footer 
  • trunk/wp-admin/includes/post.php

    r21949 r22127  
    10631063    $post = get_post($id); 
    10641064 
     1065    $context = isset( $_POST['context'] ) ? $_POST['context'] : get_current_screen()->id; 
    10651066    list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug); 
    10661067 
     
    10741075 
    10751076    if ( false === strpos($permalink, '%postname%') && false === strpos($permalink, '%pagename%') ) { 
     1077        if ( 'options-reading' == $context ) 
     1078            return ''; 
    10761079        $return = '<strong>' . __('Permalink:') . "</strong>\n" . '<span id="sample-permalink" tabindex="-1">' . $permalink . "</span>\n"; 
    10771080        if ( '' == get_option( 'permalink_structure' ) && current_user_can( 'manage_options' ) && !( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') ) ) 
     
    11021105    $display_link = str_replace(array('%pagename%','%postname%'), $post_name_html, $permalink); 
    11031106    $view_link = str_replace(array('%pagename%','%postname%'), $post_name, $permalink); 
    1104     $return =  '<strong>' . __('Permalink:') . "</strong>\n"; 
     1107    $return  = ( 'options-reading' == $context ) ? __( 'Located at' ) . "\n" : '<strong>' . __( 'Permalink:' ) . "</strong>\n"; 
    11051108    $return .= '<span id="sample-permalink" tabindex="-1">' . $display_link . "</span>\n"; 
    11061109    $return .= '&lrm;'; // Fix bi-directional text display defect in RTL languages. 
    11071110    $return .= '<span id="edit-slug-buttons"><a href="#post_name" class="edit-slug button button-small hide-if-no-js" onclick="editPermalink(' . $id . '); return false;">' . __('Edit') . "</a></span>\n"; 
    11081111    $return .= '<span id="editable-post-name-full">' . $post_name . "</span>\n"; 
    1109     if ( isset($view_post) ) 
     1112    if ( isset( $view_post ) && 'options-reading' != $context ) 
    11101113        $return .= "<span id='view-post-btn'><a href='$view_link' class='button button-small'>$view_post</a></span>\n"; 
    11111114 
     
    13231326    return $url; 
    13241327} 
     1328 
     1329/** 
     1330 * Creates new pages to be set as a front page or a page for posts in Reading Settings. 
     1331 * 
     1332 * @todo Make sure we are doing adequate sanitization on success, and cleanup/reset on failure. 
     1333 * 
     1334 * @since 3.5.0 
     1335 * @access private 
     1336 */ 
     1337function _create_pages_for_reading_settings() { 
     1338    // If we're saving the Reading Settings screen, intercept. 
     1339    if ( ! isset( $_POST['show_on_front'] ) ) 
     1340        return; 
     1341 
     1342    // If a new front page was meant to be created, go forth and create it. 
     1343    if ( isset( $_POST['page_on_front'] ) && 'new' == $_POST['page_on_front'] ) { 
     1344        if ( ! current_user_can( 'create_posts', 'page' ) ) { 
     1345            $_POST['page_on_front'] = 0; 
     1346            $_POST['show_on_front'] = 'posts'; 
     1347            add_settings_error( 'page_on_front', __( 'You are not allowed to create pages on this site.' ) ); 
     1348        } 
     1349 
     1350        $existing_page = get_page_by_title( stripslashes( $_POST['page_on_front_title'] ) ); 
     1351 
     1352        // If page already exists and it's public, there's no need to create a new page 
     1353        if ( $existing_page && 'publish' == $existing_page->post_status ) { 
     1354            $page_id = $existing_page->ID; 
     1355        } else { 
     1356            $page_id = wp_insert_post( array( 
     1357                'post_title' => $_POST['page_on_front_title'], 
     1358                'post_type' => 'page', 
     1359                'post_status' => 'publish', 
     1360                'comment_status' => 'closed', 
     1361                'ping_status' => 'closed', 
     1362                // @todo Create some sort of a 'context' in postmeta so we know we created a page through these means. 
     1363                //       Consider then showing that context in the list table as a good-first-step. 
     1364            ), true ); 
     1365        } 
     1366 
     1367        // Make sure page_on_front is properly saved by options.php. 
     1368        if ( is_wp_error( $page_id ) ) 
     1369            $_POST['page_on_front'] = 0; 
     1370        else 
     1371            $_POST['page_on_front'] = $page_id; 
     1372    } 
     1373 
     1374    // If a page for posts was meant to be specified, update/create it. 
     1375    if ( ! isset( $_POST['page_for_posts'] ) ) 
     1376        return; 
     1377 
     1378    $page_for_posts = (int) $_POST['page_for_posts']; 
     1379    if ( ! $page_for_posts || ! $page = get_post( $page_for_posts, ARRAY_A ) ) { 
     1380        $_POST['page_for_posts'] = 0; 
     1381        return; 
     1382    } 
     1383 
     1384    // @todo The UI (see @todo's in options-reading) should cover the next 3 conditionals, 
     1385    //       which means we shouldn't need to bother with setting a settings error here. 
     1386    //       However, we may wish to restore settings before bailing, beyond setting 
     1387    //       page_for_posts to 0 (which we then expect to get cleaned up by options.php). 
     1388    if ( 'page' != $page['post_type'] || ! current_user_can( 'edit_post', $page_for_posts ) ) { 
     1389        $_POST['page_for_posts'] = 0; 
     1390        return; 
     1391    } 
     1392 
     1393    if ( 'publish' != $page['post_status'] && ! current_user_can( 'publish_post', $page_for_posts ) ) { 
     1394        $_POST['page_for_posts'] = 0; 
     1395        return; 
     1396    } 
     1397 
     1398    $args = add_magic_quotes( $page ); 
     1399    $args['post_title']  = $_POST['page_for_posts_title']; 
     1400    $args['post_name']   = $_POST['post_name']; 
     1401    $args['post_status'] = 'publish'; 
     1402    if ( 'auto-draft' == $page['post_status'] ) { 
     1403        $args['comment_status'] = 'closed'; 
     1404        $args['ping_status'] = 'closed'; 
     1405    } 
     1406 
     1407    $page_id = wp_insert_post( $args, true ); 
     1408    if ( is_wp_error( $page_id ) ) 
     1409        $_POST['page_for_posts'] = 0; 
     1410} 
     1411add_filter( 'admin_init', '_create_pages_for_reading_settings' ); 
  • trunk/wp-admin/js/post.js

    r22019 r22127  
    560560    } // end submitdiv 
    561561 
    562     // permalink 
    563     if ( $('#edit-slug-box').length ) { 
    564         editPermalink = function(post_id) { 
    565             var i, c = 0, e = $('#editable-post-name'), revert_e = e.html(), real_slug = $('#post_name'), revert_slug = real_slug.val(), b = $('#edit-slug-buttons'), revert_b = b.html(), full = $('#editable-post-name-full').html(); 
    566  
    567             $('#view-post-btn').hide(); 
    568             b.html('<a href="#" class="save button button-small">'+postL10n.ok+'</a> <a class="cancel" href="#">'+postL10n.cancel+'</a>'); 
    569             b.children('.save').click(function() { 
    570                 var new_slug = e.children('input').val(); 
    571                 if ( new_slug == $('#editable-post-name-full').text() ) { 
    572                     return $('.cancel', '#edit-slug-buttons').click(); 
    573                 } 
    574                 $.post(ajaxurl, { 
    575                     action: 'sample-permalink', 
    576                     post_id: post_id, 
    577                     new_slug: new_slug, 
    578                     new_title: $('#title').val(), 
    579                     samplepermalinknonce: $('#samplepermalinknonce').val() 
    580                 }, function(data) { 
    581                     $('#edit-slug-box').html(data); 
    582                     b.html(revert_b); 
    583                     real_slug.val(new_slug); 
    584                     makeSlugeditClickable(); 
    585                     $('#view-post-btn').show(); 
    586                 }); 
    587                 return false; 
    588             }); 
    589  
    590             $('.cancel', '#edit-slug-buttons').click(function() { 
    591                 $('#view-post-btn').show(); 
    592                 e.html(revert_e); 
    593                 b.html(revert_b); 
    594                 real_slug.val(revert_slug); 
    595                 return false; 
    596             }); 
    597  
    598             for ( i = 0; i < full.length; ++i ) { 
    599                 if ( '%' == full.charAt(i) ) 
    600                     c++; 
    601             } 
    602  
    603             slug_value = ( c > full.length / 4 ) ? '' : full; 
    604             e.html('<input type="text" id="new-post-slug" value="'+slug_value+'" />').children('input').keypress(function(e){ 
    605                 var key = e.keyCode || 0; 
    606                 // on enter, just save the new slug, don't save the post 
    607                 if ( 13 == key ) { 
    608                     b.children('.save').click(); 
    609                     return false; 
    610                 } 
    611                 if ( 27 == key ) { 
    612                     b.children('.cancel').click(); 
    613                     return false; 
    614                 } 
    615                 real_slug.val(this.value); 
    616             }).focus(); 
    617         } 
    618  
    619         makeSlugeditClickable = function() { 
    620             $('#editable-post-name').click(function() { 
    621                 $('#edit-slug-buttons').children('.edit-slug').click(); 
    622             }); 
    623         } 
    624         makeSlugeditClickable(); 
    625     } 
    626  
    627562    // word count 
    628563    if ( typeof(wpWordCount) != 'undefined' ) { 
  • trunk/wp-admin/options-reading.php

    r21872 r22127  
    1515$title = __( 'Reading Settings' ); 
    1616$parent_file = 'options-general.php'; 
     17 
     18wp_enqueue_script( 'sample-permalink' ); 
    1719 
    1820/** 
     
    2325function options_reading_add_js() { 
    2426?> 
    25 <script type="text/javascript"> 
    26 //<![CDATA[ 
    27     jQuery(document).ready(function($){ 
    28         var section = $('#front-static-pages'), 
    29             staticPage = section.find('input:radio[value="page"]'), 
    30             selects = section.find('select'), 
    31             check_disabled = function(){ 
    32                 selects.prop( 'disabled', ! staticPage.prop('checked') ); 
    33             }; 
    34         check_disabled(); 
    35         section.find('input:radio').change(check_disabled); 
     27<script> 
     28jQuery(document).ready( function($) { 
     29    var section = $('#front-static-pages'); 
     30    $('#show_on_front').change( function() { 
     31        var checked = $(this).prop('checked'); 
     32        section.toggleClass('page-on-front', checked); 
     33        if ( checked ) 
     34            $('#page_for_posts').prop('checked', true).change(); 
    3635    }); 
    37 //]]> 
     36    $('#page_for_posts').change( function() { 
     37        section.toggleClass('page-for-posts', $(this).prop('checked')); 
     38    }); 
     39    $('#page_on_front').change( function() { 
     40        section.toggleClass('new-front-page', 'new' === $(this).val()); 
     41    }); 
     42}); 
    3843</script> 
    3944<?php 
    4045} 
    41 add_action('admin_head', 'options_reading_add_js'); 
     46add_action( 'admin_head', 'options_reading_add_js' ); 
    4247 
    4348/** 
     
    8388<?php 
    8489settings_fields( 'reading' ); 
    85  
     90wp_nonce_field( 'samplepermalink', 'samplepermalinknonce', false ); 
     91?> 
     92<table class="form-table"> 
     93<?php 
    8694if ( ! in_array( get_option( 'blog_charset' ), array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) ) ) 
    8795    add_settings_field( 'blog_charset', __( 'Encoding for pages and feeds' ), 'options_reading_blog_charset', 'reading', 'default', array( 'label_for' => 'blog_charset' ) ); 
    88 ?> 
    89  
    90 <?php if ( ! get_pages() ) : ?> 
    91 <input name="show_on_front" type="hidden" value="posts" /> 
    92 <table class="form-table"> 
    93 <?php 
    94     if ( 'posts' != get_option( 'show_on_front' ) ) : 
     96 
     97$classes = ''; 
     98if ( 'page' == get_option( 'show_on_front' ) ) { 
     99    if ( ! get_pages() || ! get_option( 'page_on_front' ) && ! get_option( 'page_for_posts' ) ) { 
    95100        update_option( 'show_on_front', 'posts' ); 
    96     endif; 
    97  
    98 else : 
    99     if ( 'page' == get_option( 'show_on_front' ) && ! get_option( 'page_on_front' ) && ! get_option( 'page_for_posts' ) ) 
    100         update_option( 'show_on_front', 'posts' ); 
    101 ?> 
    102 <table class="form-table"> 
    103 <tr valign="top"> 
    104 <th scope="row"><?php _e( 'Front page displays' ); ?></th> 
    105 <td id="front-static-pages"><fieldset><legend class="screen-reader-text"><span><?php _e( 'Front page displays' ); ?></span></legend> 
    106     <p><label> 
    107         <input name="show_on_front" type="radio" value="posts" class="tog" <?php checked( 'posts', get_option( 'show_on_front' ) ); ?> /> 
    108         <?php _e( 'Your latest posts' ); ?> 
    109     </label> 
     101    } else { 
     102        $classes = 'page-on-front'; 
     103        if ( get_option( 'page_for_posts' ) ) 
     104            $classes .= ' page-for-posts'; 
     105    } 
     106} 
     107 
     108$all_pages = get_pages(); 
     109$new_front_page_only = ! get_option( 'page_on_front' ) && ( ! $all_pages || ( 1 == count( $all_pages ) && __( 'sample-page' ) == $all_pages[0]->post_name ) ); 
     110 
     111if ( current_user_can( 'create_posts', 'page' ) && ! ( get_option( 'page_for_posts' ) && $page_for_posts = get_post( get_option( 'page_for_posts' ) ) ) ) { 
     112    $title = _x( 'Blog', 'default page for posts title' ); 
     113    // @todo What if the found page is post_type = attachment or post_status != publish? 
     114    //       We could go ahead and create a new one, but we would not be able to take over 
     115    //       the slug from another page. (We could for an attachment.) 
     116    //       We must also check that the user can edit this page and publish a page. 
     117    //       Otherwise, we must assume they cannot create pages (throughout), and thus 
     118    //       should fall back to the dropdown. 
     119    if ( ! $page_for_posts = get_page_by_path( sanitize_title( $title ) ) ) { 
     120        $page_for_posts = get_default_post_to_edit( 'page', true ); 
     121        $page_for_posts->post_title = $title; 
     122        $page_for_posts->post_name = sanitize_title( $title ); 
     123    } 
     124} 
     125 
     126if ( ! $new_front_page_only || current_user_can( 'create_posts', 'page' ) ) : ?> 
     127<tr valign="top"> 
     128<th scope="row"><?php _e( 'Enable a static front page' ); ?></th> 
     129<td id="front-static-pages" class="<?php echo $classes; ?>"> 
     130    <fieldset><legend class="screen-reader-text"><span><?php _e( 'Enable a static front page' ); ?></span></legend> 
     131    <p><label for="show_on_front"> 
     132        <input id="show_on_front" name="show_on_front" type="checkbox" value="page" <?php checked( 'page', get_option( 'show_on_front' ) ); ?> /> 
     133        <?php printf( __( 'Show a <a href="%s">page</a> instead of your latest posts' ), 'edit.php?post_type=page' ); ?> 
     134    </label></p> 
     135    <p class="if-page-on-front sub-option"> 
     136    <?php if ( $new_front_page_only ) : // If no pages, or only sample page, only allow a new page to be added ?> 
     137        <label for="page_on_front_title"><?php _e( 'Add new page titled:' ); ?> 
     138    <?php else : ?> 
     139        <label for="page_on_front"> 
     140            <select name="page_on_front" id="page_on_front"> 
     141                <option value="0"><?php _e( '&mdash; Select &mdash;' ); ?></option> 
     142                <?php if ( current_user_can( 'create_posts', 'page' ) ) : ?> 
     143                <option value="new" id="new-page"><?php _e( '&mdash; Add new page &mdash;' ); ?></option> 
     144                <?php endif; ?> 
     145                <?php echo walk_page_dropdown_tree( $all_pages, 0, array( 'selected' => get_option( 'page_on_front' ) ) ); ?> 
     146            </select> 
     147        </label> 
     148        <?php if ( current_user_can( 'create_posts', 'page' ) ) : ?> 
     149        <label for="page_on_front_title" class="if-new-front-page"><?php _e( 'titled:' ); ?> 
     150        <?php endif; ?> 
     151    <?php endif; ?> 
     152    <?php if ( current_user_can( 'create_posts', 'page' ) ) : ?> 
     153            <input name="page_on_front_title" type="text" id="page_on_front_title" value="<?php echo esc_attr_x( 'Home', 'default page on front title' ); ?>" /> 
     154        </label> 
     155    <?php endif; ?> 
    110156    </p> 
    111     <p><label> 
    112         <input name="show_on_front" type="radio" value="page" class="tog" <?php checked( 'page', get_option( 'show_on_front' ) ); ?> /> 
    113         <?php printf( __( 'A <a href="%s">static page</a> (select below)' ), 'edit.php?post_type=page' ); ?> 
    114     </label> 
     157    <p class="if-page-on-front"><label for="page_for_posts"> 
     158        <input id="page_for_posts" name="page_for_posts" type="checkbox" value="<?php echo $page_for_posts->ID; ?>" <?php checked( (bool) get_option( 'page_for_posts' ) ); ?> /> 
     159        <?php _e( 'Show latest posts on a separate page' ); ?> 
     160    </label></p> 
     161    <?php if ( current_user_can( 'create_posts', 'page' ) ) : ?> 
     162    <p class="if-page-for-posts sub-option"><label for="page_for_posts_title"><?php _e( 'Page title:' ); ?> 
     163        <input name="page_for_posts_title" type="text" id="page_for_posts_title" value="<?php echo esc_attr( htmlspecialchars( $page_for_posts->post_title ) ); ?>" /> 
     164    </label></p> 
     165    <p class="if-page-for-posts sub-option" id="edit-slug-box"> 
     166        <?php echo get_sample_permalink_html( $page_for_posts->ID, $page_for_posts->post_title, $page_for_posts->post_name ); ?> 
    115167    </p> 
    116 <ul> 
    117     <li><label for="page_on_front"><?php printf( __( 'Front page: %s' ), wp_dropdown_pages( array( 'name' => 'page_on_front', 'echo' => 0, 'show_option_none' => __( '&mdash; Select &mdash;' ), 'option_none_value' => '0', 'selected' => get_option( 'page_on_front' ) ) ) ); ?></label></li> 
    118     <li><label for="page_for_posts"><?php printf( __( 'Posts page: %s' ), wp_dropdown_pages( array( 'name' => 'page_for_posts', 'echo' => 0, 'show_option_none' => __( '&mdash; Select &mdash;' ), 'option_none_value' => '0', 'selected' => get_option( 'page_for_posts' ) ) ) ); ?></label></li> 
    119 </ul> 
    120 <?php if ( 'page' == get_option( 'show_on_front' ) && get_option( 'page_for_posts' ) == get_option( 'page_on_front' ) ) : ?> 
    121 <div id="front-page-warning" class="error inline"><p><?php _e( '<strong>Warning:</strong> these pages should not be the same!' ); ?></p></div> 
    122 <?php endif; ?> 
    123 </fieldset></td> 
    124 </tr> 
    125 <?php endif; ?> 
     168    <input name="post_name" type="hidden" id="post_name" value="<?php echo esc_attr( apply_filters( 'editable_slug', $page_for_posts->post_name ) ); ?>" /> 
     169    <?php if ( 'page' == get_option( 'show_on_front' ) && get_option( 'page_for_posts' ) == get_option( 'page_on_front' ) ) : ?> 
     170    <div class="error inline"><p><strong><?php _e( 'ERROR:' ); ?></strong> <?php _e( 'These pages should not be the same!' ); ?></p></div> 
     171    <?php endif; ?> 
     172    </fieldset> 
     173    <?php else : // cannot create pages, so fall back to a selector of existing pages ?> 
     174    <p class="if-page-for-posts sub-option"><label for="page_for_posts"> 
     175        <?php wp_dropdown_pages( array( 
     176            'name' => 'page_for_posts', 'show_option_none' => __( '&mdash; Select &mdash;' ), 
     177            'option_none_value' => '0', 'selected' => get_option( 'page_for_posts' ) 
     178        ) ); ?> 
     179    <?php endif; // create pages ?> 
     180</td> 
     181</tr> 
     182<?php endif; // if no pages to choose from and can't create pages ?> 
     183 
    126184<tr valign="top"> 
    127185<th scope="row"><label for="posts_per_page"><?php _e( 'Blog pages show at most' ); ?></label></th> 
  • trunk/wp-includes/script-loader.php

    r22076 r22127  
    370370        $scripts->add( 'postbox', "/wp-admin/js/postbox$suffix.js", array('jquery-ui-sortable'), false, 1 ); 
    371371 
    372         $scripts->add( 'post', "/wp-admin/js/post$suffix.js", array('suggest', 'wp-lists', 'postbox'), false, 1 ); 
    373         did_action( 'init' ) && $scripts->localize( 'post', 'postL10n', array( 
     372        $scripts->add( 'sample-permalink', "/wp-admin/js/sample-permalink.js", array(), false, 1 ); 
     373        did_action( 'init' ) && $scripts->localize( 'sample-permalink', 'samplePermalinkL10n', array( 
    374374            'ok' => __('OK'), 
    375375            'cancel' => __('Cancel'), 
     376        ) ); 
     377 
     378        $scripts->add( 'post', "/wp-admin/js/post$suffix.js", array('suggest', 'wp-lists', 'postbox', 'sample-permalink' ), false, 1 ); 
     379        did_action( 'init' ) && $scripts->localize( 'post', 'postL10n', array( 
    376380            'publishOn' => __('Publish on:'), 
    377381            'publishOnFuture' =>  __('Schedule for:'), 
Note: See TracChangeset for help on using the changeset viewer.