Making WordPress.org


Ignore:
Timestamp:
07/24/2023 08:44:49 AM (20 months ago)
Author:
dd32
Message:

Plugin Directory: Author Notice: Style ordered lists and pre tags better. Add a set() method for other code to set the notice.

See #6880.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/admin/metabox/class-author-notice.php

    r12506 r12756  
    106106    static function save_post( $post ) {
    107107        $post          = get_post( $post );
    108         $post_meta     = get_post_meta( $post->ID, self::POST_META_KEY, true );
    109         $author_notice = $post_meta ?: [ 'type' => '', 'html' => '' ];
    110108
    111109        if (
     
    115113            current_user_can( 'plugin_admin_edit', $post->ID )
    116114        ) {
    117             $new_author_notice         = wp_unslash( $_REQUEST['author_notice'] );
    118             $new_author_notice['type'] = sanitize_key( $new_author_notice['type'] );
    119             $new_author_notice['html'] = wp_kses( trim( $new_author_notice['html'] ), self::ALLOWED_TAGS );
     115            $new_author_notice = wp_unslash( $_REQUEST['author_notice'] );
     116            self::set( $post, $new_author_notice['html'], $new_author_notice['type'] );
     117        }
     118    }
    120119
    121             // Check it's not empty with tags removed.
    122             if (
    123                 $new_author_notice['html'] &&
    124                 ! trim( wp_strip_all_tags( $new_author_notice['html'] ) )
    125             ) {
    126                 $new_author_notice['html'] = '';
    127             }
     120    /**
     121     * Set the Author notice.
     122     */
     123    static function set( $post, $notice, $type = 'error' ) {
     124        $post_meta         = get_post_meta( $post->ID, self::POST_META_KEY, true );
     125        $author_notice     = $post_meta ?: [ 'type' => '', 'html' => '' ];
     126        $new_author_notice = [
     127            'type' => sanitize_key( $type ),
     128            'html' => wp_kses( trim( $notice ), self::ALLOWED_TAGS ),
     129        ];
    128130
    129             // Default or no text, remove the notice.
    130             if ( empty( $new_author_notice['html'] ) || self::DEFAULT_TEXT === $new_author_notice['html'] ) {
    131                 $new_author_notice['type'] = '';
    132                 $new_author_notice['html'] = '';
    133             }
     131        // Check it's not empty with tags removed.
     132        if (
     133            $new_author_notice['html'] &&
     134            ! trim( wp_strip_all_tags( $new_author_notice['html'] ) )
     135        ) {
     136            $new_author_notice['html'] = '';
     137        }
    134138
    135             // Remove it.
    136             if ( $post_meta && ! $new_author_notice['type'] && ! $new_author_notice['html'] ) {
    137                 delete_post_meta( $post->ID, self::POST_META_KEY );
    138                 Tools::audit_log( 'Author notice removed.' );
    139                 return;
    140             }
     139        // Default or no text, remove the notice.
     140        if ( empty( $new_author_notice['html'] ) || self::DEFAULT_TEXT === $new_author_notice['html'] ) {
     141            $new_author_notice['type'] = '';
     142            $new_author_notice['html'] = '';
     143        }
    141144
    142             // Value changed?
    143             if (
    144                 $author_notice['html'] != $new_author_notice['html'] ||
    145                 $author_notice['type'] != $new_author_notice['type']
    146             ) {
    147                 $author_notice = [
    148                     'type' => $new_author_notice['type'],
    149                     'html' => $new_author_notice['html'],
    150                     'when' => time(),
    151                     'who'  => get_current_user_id(),
    152                 ];
     145        // Remove it.
     146        if ( $post_meta && ! $new_author_notice['type'] && ! $new_author_notice['html'] ) {
     147            delete_post_meta( $post->ID, self::POST_META_KEY );
     148            Tools::audit_log( 'Author notice removed.' );
     149            return;
     150        }
    153151
    154                 update_post_meta( $post->ID, self::POST_META_KEY, $author_notice );
     152        // Value changed?
     153        if (
     154            $author_notice['html'] != $new_author_notice['html'] ||
     155            $author_notice['type'] != $new_author_notice['type']
     156        ) {
     157            $author_notice = [
     158                'type' => $new_author_notice['type'],
     159                'html' => $new_author_notice['html'],
     160                'when' => time(),
     161                'who'  => get_current_user_id(),
     162            ];
    155163
    156                 $type_text = $author_notice['type'] ?: __( 'Do not show', 'wporg-plugins' );
    157                 Tools::audit_log(
    158                     sprintf(
    159                         'Author notice set to: [%s] %s',
    160                         $type_text,
    161                         wp_strip_all_tags( $author_notice['html'] )
    162                     )
    163                 );
    164             }
     164            update_post_meta( $post->ID, self::POST_META_KEY, $author_notice );
     165
     166            $type_text = $author_notice['type'] ?: __( 'Do not show', 'wporg-plugins' );
     167            Tools::audit_log(
     168                sprintf(
     169                    'Author notice set to: [%s] %s',
     170                    $type_text,
     171                    wp_strip_all_tags( $author_notice['html'] )
     172                )
     173            );
    165174        }
    166175    }
Note: See TracChangeset for help on using the changeset viewer.