Make WordPress Core

Ticket #49602: #49602_(2).patch

File #49602_(2).patch, 14.0 KB (added by arena, 5 years ago)

new patch with help + one form + settings api (settings_fields, do_settings ) + changed create and update messages + code review and indent to fit with look and feel of other setting pages

  • wp-admin/options-privacy.php

     
    1313        wp_die( __( 'Sorry, you are not allowed to manage privacy on this site.' ) );
    1414}
    1515
    16 $action = isset( $_POST['action'] ) ? $_POST['action'] : '';
     16$title       = __( 'Privacy Settings' );
     17$parent_file = 'options-general.php';
    1718
     19$action      = isset( $_POST['action'] )      ? $_POST['action']      : '';
     20$option_page = isset( $_POST['option_page'] ) ? $_POST['option_page'] : '';
     21
     22global $wpdb;
     23
    1824if ( ! empty( $action ) ) {
    19         check_admin_referer( $action );
     25        check_admin_referer( $option_page . '-options' );
    2026
    21         if ( 'set-privacy-page' === $action ) {
    22                 $privacy_policy_page_id = isset( $_POST['page_for_privacy_policy'] ) ? (int) $_POST['page_for_privacy_policy'] : 0;
     27        $update_page = ( isset( $_POST['page_for_privacy_policy'] ) && ( $_POST['page_for_privacy_policy'] != get_option( 'wp_page_for_privacy_policy' ) ) );
     28        $create_page = isset( $_POST['create_privacy_page'] );
     29
     30        if ( $update_page ) {
     31                $privacy_policy_page_id = (int) $_POST['page_for_privacy_policy'];
    2332                update_option( 'wp_page_for_privacy_policy', $privacy_policy_page_id );
    2433
    2534                $privacy_page_updated_message = __( 'Privacy Policy page updated successfully.' );
    2635
    2736                if ( $privacy_policy_page_id ) {
    28                         /*
    29                         * Don't always link to the menu customizer:
    30                         *
    31                         * - Unpublished pages can't be selected by default.
    32                         * - `WP_Customize_Nav_Menus::__construct()` checks the user's capabilities.
    33                         * - Themes might not "officially" support menus.
    34                         */
     37                /*
     38                * Don't always link to the menu customizer:
     39                *
     40                * - Unpublished pages can't be selected by default.
     41                * - `WP_Customize_Nav_Menus::__construct()` checks the user's capabilities.
     42                * - Themes might not "officially" support menus.
     43                */
    3544                        if (
    3645                                'publish' === get_post_status( $privacy_policy_page_id )
    3746                                && current_user_can( 'edit_theme_options' )
     
    4352                                        esc_url( add_query_arg( 'autofocus[panel]', 'nav_menus', admin_url( 'customize.php' ) ) )
    4453                                );
    4554                        }
    46                 }
     55                }
     56                if ( !$create_page ) add_settings_error( 'page_for_privacy_policy', 'page_for_privacy_policy', $privacy_page_updated_message, 'success' );
     57        }
    4758
    48                 add_settings_error( 'page_for_privacy_policy', 'page_for_privacy_policy', $privacy_page_updated_message, 'success' );
    49         } elseif ( 'create-privacy-page' === $action ) {
     59        if ( $create_page ) {
    5060
    5161                if ( ! class_exists( 'WP_Privacy_Policy_Content' ) ) {
    5262                        require_once ABSPATH . 'wp-admin/includes/class-wp-privacy-policy-content.php';
    5363                }
    5464
     65                $has_pages_with_default_title = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = %s AND post_status IN ( 'publish', 'draft' ) AND post_title = %s;", 'page', __( 'Privacy Policy' ) ) );
     66
    5567                $privacy_policy_page_content = WP_Privacy_Policy_Content::get_default_content();
    5668                $privacy_policy_page_id      = wp_insert_post(
    5769                        array(
    58                                 'post_title'   => __( 'Privacy Policy' ),
     70                                'post_title'   => ( $has_pages_with_default_title ) ? sprintf( __( 'Privacy Policy (%1$d)' ), $has_pages_with_default_title + 1 ) : __( 'Privacy Policy' ),
    5971                                'post_status'  => 'draft',
    6072                                'post_type'    => 'page',
    6173                                'post_content' => $privacy_policy_page_content,
     
    7082                                __( 'Unable to create a Privacy Policy page.' ),
    7183                                'error'
    7284                        );
     85                        $privacy_policy_page_id = false;
    7386                } else {
    7487                        update_option( 'wp_page_for_privacy_policy', $privacy_policy_page_id );
    75 
    76                         wp_redirect( admin_url( 'post.php?post=' . $privacy_policy_page_id . '&action=edit' ) );
    77                         exit;
     88                        add_settings_error( 'page_for_privacy_policy', 'page_for_privacy_policy', __( 'Privacy Policy page created successfully.' ), 'success' );
    7889                }
    7990        }
    8091}
    8192
    8293// If a Privacy Policy page ID is available, make sure the page actually exists. If not, display an error.
    83 $privacy_policy_page_exists = false;
    84 $privacy_policy_page_id     = (int) get_option( 'wp_page_for_privacy_policy' );
    8594
     95$privacy_policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' );
     96
     97$tools_privacy_edit = '';
     98
    8699if ( ! empty( $privacy_policy_page_id ) ) {
    87100
    88101        $privacy_policy_page = get_post( $privacy_policy_page_id );
     
    94107                        __( 'The currently selected Privacy Policy page does not exist. Please create or select a new page.' ),
    95108                        'error'
    96109                );
    97         } else {
    98                 if ( 'trash' === $privacy_policy_page->post_status ) {
     110        } elseif ( 'trash' === $privacy_policy_page->post_status ) {
    99111                        add_settings_error(
    100112                                'page_for_privacy_policy',
    101113                                'page_for_privacy_policy',
     
    106118                                ),
    107119                                'error'
    108120                        );
    109                 } else {
    110                         $privacy_policy_page_exists = true;
    111                 }
    112         }
    113 }
     121        } else {        /* $privacy_policy_page_exists = true; */
    114122
    115 $title       = __( 'Privacy Settings' );
    116 $parent_file = 'options-general.php';
    117 
    118 require_once ABSPATH . 'wp-admin/admin-header.php';
    119 
    120 ?>
    121 <div class="wrap">
    122         <h1><?php echo $title; ?></h1>
    123         <h2><?php _e( 'Privacy Policy Page' ); ?></h2>
    124         <p>
    125                 <?php _e( 'As a website owner, you may need to follow national or international privacy laws. For example, you may need to create and display a Privacy Policy.' ); ?>
    126                 <?php _e( 'If you already have a Privacy Policy page, please select it below. If not, please create one.' ); ?>
    127         </p>
    128         <p>
    129                 <?php _e( 'The new page will include help and suggestions for your Privacy Policy.' ); ?>
    130                 <?php _e( 'However, it is your responsibility to use those resources correctly, to provide the information that your Privacy Policy requires, and to keep that information current and accurate.' ); ?>
    131         </p>
    132         <p>
    133                 <?php _e( 'After your Privacy Policy page is set, we suggest that you edit it.' ); ?>
    134                 <?php _e( 'We would also suggest reviewing your Privacy Policy from time to time, especially after installing or updating any themes or plugins. There may be changes or new suggested information for you to consider adding to your policy.' ); ?>
    135         </p>
    136         <?php
    137 
    138         if ( $privacy_policy_page_exists ) {
    139123                $edit_href = add_query_arg(
    140124                        array(
    141125                                'post'   => $privacy_policy_page_id,
     
    145129                );
    146130
    147131                $view_href = get_permalink( $privacy_policy_page_id );
    148                 ?>
    149                 <p class="tools-privacy-edit"><strong>
    150                         <?php
    151                         if ( 'publish' === get_post_status( $privacy_policy_page_id ) ) {
    152                                 printf(
    153                                         /* translators: 1: URL to edit Privacy Policy page, 2: URL to view Privacy Policy page. */
    154                                         __( '<a href="%1$s">Edit</a> or <a href="%2$s">view</a> your Privacy Policy page content.' ),
     132
     133                if ( $create_page ) {
     134                        $privacy_page_create_message = sprintf(
     135                                        /* translators: 1: URL to edit Privacy Policy page, 2: URL to preview Privacy Policy page, 3: URL to Customizer -> Menus */
     136                                        __( 'You can already <a href="%1$s">edit</a> or <a href="%2$s">preview</a> your Privacy Policy page content. Remember to <a href="%3$s">update your menus</a>!' ),
    155137                                        esc_url( $edit_href ),
     138                                        esc_url( $view_href ),
     139                                        esc_url( add_query_arg( 'autofocus[panel]', 'nav_menus', admin_url( 'customize.php' ) ) )
     140                        );
     141                        add_settings_error( 'page_for_privacy_policy', 'page_for_privacy_policy', $privacy_page_create_message, 'success' );
     142
     143                } else {
     144                        $tools_privacy_edit = ( 'publish' === get_post_status( $privacy_policy_page_id ) )
     145                        ? sprintf(
     146                                /* translators: 1: URL to edit Privacy Policy page, 2: URL to view Privacy Policy page. */
     147                                __( 'You can already <a href="%1$s">edit</a> or <a href="%2$s">view</a> your Privacy Policy page content.' ),
     148                                esc_url( $edit_href ),
    156149                                        esc_url( $view_href )
    157                                 );
    158                         } else {
    159                                 printf(
    160                                         /* translators: 1: URL to edit Privacy Policy page, 2: URL to preview Privacy Policy page. */
    161                                         __( '<a href="%1$s">Edit</a> or <a href="%2$s">preview</a> your Privacy Policy page content.' ),
    162                                         esc_url( $edit_href ),
    163                                         esc_url( $view_href )
    164                                 );
    165                         }
    166                         ?>
    167                 </strong></p>
    168                 <?php
     150                        )
     151                        : sprintf(
     152                                /* translators: 1: URL to edit Privacy Policy page, 2: URL to preview Privacy Policy page. */
     153                                __( 'You can already <a href="%1$s">edit</a> or <a href="%2$s">preview</a> your Privacy Policy page content.' ),
     154                                esc_url( $edit_href ),
     155                                esc_url( $view_href )
     156                        );
     157                }
    169158        }
    170         ?>
    171         <p>
    172                 <?php
    173                 printf(
    174                         /* translators: 1: Privacy Policy guide URL, 2: Additional link attributes, 3: Accessibility text. */
    175                         __( 'Need help putting together your new Privacy Policy page? <a href="%1$s" %2$s>Check out our guide%3$s</a> for recommendations on what content to include, along with policies suggested by your plugins and theme.' ),
    176                         esc_url( admin_url( 'privacy-policy-guide.php' ) ),
    177                         '',
    178                         ''
    179                 );
    180                 ?>
    181         </p>
     159}
    182160
    183         <hr>
    184         <table class="form-table tools-privacy-policy-page" role="presentation">
    185                 <tr>
    186                         <th scope="row">
    187                                 <label for="page_for_privacy_policy">
    188                                         <?php
    189                                         if ( $privacy_policy_page_exists ) {
    190                                                 _e( 'Change your Privacy Policy page' );
    191                                         } else {
    192                                                 _e( 'Select a Privacy Policy page' );
    193                                         }
    194                                         ?>
    195                                 </label>
    196                         </th>
    197                         <td>
    198                                 <?php
    199                                 $has_pages = (bool) get_posts(
    200                                         array(
    201                                                 'post_type'      => 'page',
    202                                                 'posts_per_page' => 1,
    203                                                 'post_status'    => array(
    204                                                         'publish',
    205                                                         'draft',
    206                                                 ),
    207                                         )
    208                                 );
    209161
    210                                 if ( $has_pages ) :
    211                                         ?>
    212                                         <form method="post" action="">
    213                                                 <input type="hidden" name="action" value="set-privacy-page" />
    214                                                 <?php
    215                                                 wp_dropdown_pages(
    216                                                         array(
    217                                                                 'name'              => 'page_for_privacy_policy',
    218                                                                 'show_option_none'  => __( '&mdash; Select &mdash;' ),
    219                                                                 'option_none_value' => '0',
    220                                                                 'selected'          => $privacy_policy_page_id,
    221                                                                 'post_status'       => array( 'draft', 'publish' ),
    222                                                         )
    223                                                 );
     162get_current_screen()->add_help_tab(
     163        array(
     164                'id'      => 'overview',
     165                'title'   => __( 'Overview' ),
     166                'content' => '<p>' . __( 'This screen contains the settings that help you to set up or affect your privacy policy.' ) . '</p>' .
     167                        '<p>' . __( 'As a website owner, you may need to follow national or international privacy laws. For example, you may need to create and display a Privacy Policy.' ) . '</p>',
     168        )
     169);
    224170
    225                                                 wp_nonce_field( 'set-privacy-page' );
     171get_current_screen()->add_help_tab(
     172        array(
     173                'id'      => 'privacy-policy-page',
     174                'title'   => __( 'Policy Page' ),
     175                'content' => '<p>' . __( 'If you already have a Privacy Policy page, please select it. If not, please create one.' ) . '</p>' .
     176                        '<p>' . __( 'The new page will include help and suggestions for your Privacy Policy.' ) . '</p>' .
     177                        '<p>' . __( 'However, it is your responsibility to use those resources correctly, to provide the information that your Privacy Policy requires, and to keep that information current and accurate.' ) . '</p>' .
     178                        '<p>' . __( 'After your Privacy Policy page is set, we suggest that you edit it.' ) . '</p>' .
     179                        '<p>' . __( 'We would also suggest reviewing your Privacy Policy from time to time, especially after installing or updating any themes or plugins. There may be changes or new suggested information for you to consider adding to your policy.' ) . '</p>',
     180        )
     181);
    226182
    227                                                 submit_button( __( 'Use This Page' ), 'primary', 'submit', false, array( 'id' => 'set-page' ) );
    228                                                 ?>
    229                                         </form>
    230                                 <?php endif; ?>
     183get_current_screen()->set_help_sidebar(
     184        '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
     185        /* translators: 1: Privacy Policy guide URL */
     186        '<p>' . sprintf( __( '<a href="%1$s">Privacy Policy guide</a>' ), esc_url( admin_url( 'privacy-policy-guide.php' ) ) ) . '</p>' .
     187        '<p>' . __( '<a href="https://wordpress.org/support/article/settings-privacy-screen/">Documentation on Privacy Settings</a>' ) . '</p>' .
     188        '<p>' . __( '<a href="https://wordpress.org/support/">Support</a>' ) . '</p>'
     189);
    231190
    232                                 <form class="wp-create-privacy-page" method="post" action="">
    233                                         <input type="hidden" name="action" value="create-privacy-page" />
    234                                         <span>
    235                                                 <?php
    236                                                 if ( $has_pages ) {
    237                                                         _e( 'Or:' );
    238                                                 } else {
    239                                                         _e( 'There are no pages.' );
    240                                                 }
    241                                                 ?>
    242                                         </span>
    243                                         <?php
    244                                         wp_nonce_field( 'create-privacy-page' );
     191require_once ABSPATH . 'wp-admin/admin-header.php';
    245192
    246                                         submit_button( __( 'Create New Page' ), 'primary', 'submit', false, array( 'id' => 'create-page' ) );
    247                                         ?>
    248                                 </form>
    249                         </td>
    250                 </tr>
    251         </table>
     193?>
     194<div class="wrap">
     195<h1><?php echo $title; ?></h1>
     196
     197<form method="post" action="">
     198<?php settings_fields( 'privacy' ); ?>
     199
     200<h2><?php _e( 'Policy Page' ); ?></h2>
     201<p class="tools-privacy-edit"><?php echo $tools_privacy_edit; ?></p>
     202
     203<table class="form-table tools-privacy-policy-page" role="presentation">
     204<tr>
     205<th scope="row"><label for="page_for_privacy_policy"><?php _e( 'Your Privacy Policy page' ); ?></label></th>
     206<td>
     207<?php
     208
     209$has_pages = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = %s AND post_status IN ( 'publish', 'draft' );", 'page' ) );
     210
     211if ( $has_pages ) {
     212        wp_dropdown_pages(
     213                array(
     214                        'name'              => 'page_for_privacy_policy',
     215                        'show_option_none'  => __( '&mdash; Select &mdash;' ),
     216                        'option_none_value' => '0',
     217                        'selected'          => $privacy_policy_page_id,
     218                        'post_status'       => array( 'draft', 'publish' ),
     219                )
     220        );
     221        echo '<br /><br />';
     222        // Allegedly, the admin has already been through the privacy page creation process !       
     223 
     224        $create_privacy_page_text  = __( 'Or create a new one : ' );
     225        $create_privacy_page_text2 = '';
     226}
     227else
     228{
     229        $create_privacy_page_text  = __( 'Create page : ' );
     230        $create_privacy_page_text2 = __( 'Once saved, you will have a link to modify (and publish) your new privacy page' );
     231}
     232?>
     233<span style="margin-left: 6px;">
     234<?php echo $create_privacy_page_text; ?>
     235<input name="create_privacy_page" type="checkbox" id="create_privacy_page" value="on" />
     236</span>
     237<?php if ( !empty( $create_privacy_page_text2 ) ) echo '<br />' . $create_privacy_page_text2; ?>
     238</td>
     239</tr>
     240
     241<?php do_settings_fields( 'privacy', 'default' ); ?>
     242</table>
     243
     244<?php do_settings_sections( 'privacy' ); ?>
     245
     246<?php submit_button(); ?>
     247
     248</form>
     249
    252250</div>
    253251<?php
    254252