Plugin Directory

Changeset 2979825


Ignore:
Timestamp:
10/17/2023 01:45:32 AM (2 years ago)
Author:
web83info
Message:

Add: Option to display width only for the user who has the specific roles.

Location:
show-current-width
Files:
17 added
4 edited

Legend:

Unmodified
Added
Removed
  • show-current-width/trunk/class/class-showcurrentwidth-admin.php

    r2973725 r2979825  
    139139        add_settings_section(
    140140            ShowCurrentWidth_Core::PLUGIN_PREFIX . '-section2',
    141             __( 'Admin page settings', 'show-current-width' ),
     141            __( 'Display condition settings', 'show-current-width' ),
    142142            array( $this, 'register_section2_html' ),
    143143            ShowCurrentWidth_Core::PLUGIN_PREFIX
     
    152152            array(
    153153                'label_for' => ShowCurrentWidth_Core::PLUGIN_PREFIX . '_admin_show',
     154            ),
     155        );
     156        // Add field 2-2 (Role condition).
     157        add_settings_field(
     158            ShowCurrentWidth_Core::PLUGIN_PREFIX . '_condition_role',
     159            __( 'Role condition', 'show-current-width' ),
     160            array( $this, 'register_field_condition_role_html' ),
     161            ShowCurrentWidth_Core::PLUGIN_PREFIX,
     162            ShowCurrentWidth_Core::PLUGIN_PREFIX . '-section2',
     163            array(
     164                'label_for' => ShowCurrentWidth_Core::PLUGIN_PREFIX . '_condition_role',
    154165            ),
    155166        );
     
    304315     */
    305316    public function register_section2_html() {
    306         echo esc_html__( 'Setting about admin page', 'show-current-width' );
     317        echo esc_html__( 'Setting about display condition', 'show-current-width' );
    307318    }
    308319
     
    331342
    332343    /**
     344     * Field 2-2 HTML.
     345     *
     346     * @return void
     347     */
     348    public function register_field_condition_role_html() {
     349        global $wp_roles;
     350        $roles = $wp_roles->roles;
     351        echo esc_html__( 'Display width only for the user who has one of the following roles.', 'show-current-width' );
     352        echo '<ul>';
     353        foreach ( $roles as $role_key => $role_value ) {
     354            echo '<li>';
     355            printf(
     356                '<input type="checkbox" name="%s_condition_role[]" id="%s_condition_role_%s" value="%s" %s />',
     357                esc_attr( ShowCurrentWidth_Core::PLUGIN_PREFIX ),
     358                esc_attr( ShowCurrentWidth_Core::PLUGIN_PREFIX ),
     359                esc_attr( $role_key ),
     360                esc_attr( $role_key ),
     361                checked( in_array( $role_key, get_option( ShowCurrentWidth_Core::PLUGIN_PREFIX . '_condition_role' ), true ), true, false ),
     362            );
     363            printf(
     364                '<label for="%s_condition_role_%s">%s</label>',
     365                esc_attr( ShowCurrentWidth_Core::PLUGIN_PREFIX ),
     366                esc_attr( $role_key ),
     367                esc_attr( translate_user_role( $role_value['name'] ) )
     368            );
     369            echo '</li>';
     370        }
     371    }
     372
     373    /**
    333374     * Section 3 HTML.
    334375     *
     
    429470        register_setting(
    430471            ShowCurrentWidth_Core::PLUGIN_PREFIX . '-field1',
     472            ShowCurrentWidth_Core::PLUGIN_PREFIX . '_condition_role',
     473            array(
     474                'type'              => 'array',
     475                'sanitize_callback' => array( $this, 'sanitize_array' ),
     476            ),
     477        );
     478        register_setting(
     479            ShowCurrentWidth_Core::PLUGIN_PREFIX . '-field1',
    431480            ShowCurrentWidth_Core::PLUGIN_PREFIX . '_other_init',
    432481            'esc_attr',
     
    437486            'esc_attr',
    438487        );
     488    }
     489
     490    /**
     491     * Sanitize array.
     492     *
     493     * @param array $args array to be sanitized.
     494     * @return array array sanitized.
     495     */
     496    public function sanitize_array( $args ) {
     497        $args = isset( $args ) ? (array) $args : array();
     498        $args = array_map( 'esc_attr', $args );
     499        return $args;
    439500    }
    440501
  • show-current-width/trunk/class/class-showcurrentwidth-core.php

    r2973725 r2979825  
    2323     * Plugin constant.
    2424     */
    25     const PLUGIN_VERSION           = '1.2.0';
     25    const PLUGIN_VERSION           = '1.2.1';
    2626    const PLUGIN_PREFIX            = 'show-current-width';
    2727    const PLUGIN_PREFIX_DEPRECATED = 'w83-show-current-width';
     
    4242    const OPTION_DEFAULT_ANIMATION_SHOW             = 1;
    4343    const OPTION_DEFAULT_ADMIN_SHOW                 = 0;
     44    const OPTION_DEFAULT_CONDITION_ROLE             = array( 'administrator' );
    4445    const OPTION_DEFAULT_OTHER_INIT                 = 0;
    4546    const OPTION_DEFAULT_OTHER_UNINSTALL            = 0;
     
    5960        'animation_show'             => self::OPTION_DEFAULT_ANIMATION_SHOW,
    6061        'admin_show'                 => self::OPTION_DEFAULT_ADMIN_SHOW,
     62        'condition_role'             => self::OPTION_DEFAULT_CONDITION_ROLE,
    6163        'other_init'                 => self::OPTION_DEFAULT_OTHER_INIT,
    6264        'other_uninstall'            => self::OPTION_DEFAULT_OTHER_UNINSTALL,
     
    182184     */
    183185    public function display_width( $wp_admin_bar ) {
    184         if ( ! is_admin() || get_option( self::PLUGIN_PREFIX . '_admin_show' ) ) {
    185             if ( get_option( self::PLUGIN_PREFIX . '_breakpoints_show' ) ) {
    186                 // Display breakpoint.
    187                 $breakpoints_definition = str_replace(
    188                     array( "\r\n", "\r", "\n" ),
    189                     "\n",
    190                     get_option( self::PLUGIN_PREFIX . '_breakpoints_definition' )
    191                 );
    192                 $breakpoints            = explode( "\n", $breakpoints_definition );
    193                 foreach ( $breakpoints as $breakpoint_key => $breakpoint_value ) {
    194                     $breakpoints[ $breakpoint_key ] = explode( ',', $breakpoint_value );
    195                 }
    196 
     186        // Return if not allowed in admin page.
     187        if ( is_admin() && ! get_option( self::PLUGIN_PREFIX . '_admin_show' ) ) {
     188            return;
     189        }
     190
     191        // Return if not having an appropriate user role.
     192        $roles = get_option( self::PLUGIN_PREFIX . '_condition_role' );
     193        if ( 0 === count( array_intersect( wp_get_current_user()->roles, $roles ) ) ) {
     194            return;
     195        }
     196
     197        if ( get_option( self::PLUGIN_PREFIX . '_breakpoints_show' ) ) {
     198            // Display breakpoint.
     199            $breakpoints_definition = str_replace(
     200                array( "\r\n", "\r", "\n" ),
     201                "\n",
     202                get_option( self::PLUGIN_PREFIX . '_breakpoints_definition' )
     203            );
     204            $breakpoints            = explode( "\n", $breakpoints_definition );
     205            foreach ( $breakpoints as $breakpoint_key => $breakpoint_value ) {
     206                $breakpoints[ $breakpoint_key ] = explode( ',', $breakpoint_value );
     207            }
     208
     209            $wp_admin_bar->add_node(
     210                array(
     211                    'id'     => self::PLUGIN_PREFIX,
     212                    'class'  => 'menupop',
     213                    'title'  => '<span class="ab-icon" aria-hidden="true"><span class="width">0</span></span>' .
     214                                    '<span class="ab-label">' .
     215                                        '<span class="width-wrap"><span class="width">0</span>px</span>' .
     216                                        '<span class="breakpoint-wrap">(<span class="breakpoint"></span>)</span>' .
     217                                    '</span>',
     218                    'parent' => '',
     219                    'href'   => '#',
     220                )
     221            );
     222            $wp_admin_bar->add_node(
     223                array(
     224                    'id'     => self::PLUGIN_PREFIX . '-breakpoint',
     225                    'title'  => '<span class="label">' . esc_html__( 'Breakpoint:', 'show-current-width' ) . '</span>' .
     226                                    '<span class="breakpoint-wrap"><span class="breakpoint"></span></span>',
     227                    'parent' => self::PLUGIN_PREFIX,
     228                    'href'   => '#',
     229                )
     230            );
     231            $breakpoint_index = 0;
     232            foreach ( $breakpoints as $breakpoint ) {
    197233                $wp_admin_bar->add_node(
    198234                    array(
    199                         'id'     => self::PLUGIN_PREFIX,
    200                         'class'  => 'menupop',
    201                         'title'  => '<span class="ab-icon" aria-hidden="true"><span class="width">0</span></span>' .
    202                                         '<span class="ab-label">' .
    203                                             '<span class="width-wrap"><span class="width">0</span>px</span>' .
    204                                             '<span class="breakpoint-wrap">(<span class="breakpoint"></span>)</span>' .
    205                                         '</span>',
    206                         'parent' => '',
    207                         'href'   => '#',
    208                     )
    209                 );
    210                 $wp_admin_bar->add_node(
    211                     array(
    212                         'id'     => self::PLUGIN_PREFIX . '-breakpoint',
    213                         'title'  => '<span class="label">' . esc_html__( 'Breakpoint:', 'show-current-width' ) . '</span>' .
    214                                         '<span class="breakpoint-wrap"><span class="breakpoint"></span></span>',
     235                        'id'     => self::PLUGIN_PREFIX . '-breakpoint-' . $breakpoint_index,
     236                        'title'  =>
     237                            sprintf(
     238                                '<span class="icon"></span>' .
     239                                '<span class="name">%s:</span>' .
     240                                '<span class="range">%s &le; %s < %s</span>',
     241                                $breakpoint[3],
     242                                $breakpoint[0],
     243                                $breakpoint[2],
     244                                $breakpoint[1]
     245                            ),
    215246                        'parent' => self::PLUGIN_PREFIX,
    216247                        'href'   => '#',
    217248                    )
    218249                );
    219                 $breakpoint_index = 0;
    220                 foreach ( $breakpoints as $breakpoint ) {
    221                     $wp_admin_bar->add_node(
    222                         array(
    223                             'id'     => self::PLUGIN_PREFIX . '-breakpoint-' . $breakpoint_index,
    224                             'title'  =>
    225                                 sprintf(
    226                                     '<span class="icon"></span>' .
    227                                     '<span class="name">%s:</span>' .
    228                                     '<span class="range">%s &le; %s < %s</span>',
    229                                     $breakpoint[3],
    230                                     $breakpoint[0],
    231                                     $breakpoint[2],
    232                                     $breakpoint[1]
    233                                 ),
    234                             'parent' => self::PLUGIN_PREFIX,
    235                             'href'   => '#',
    236                         )
    237                     );
    238                     $breakpoint_index++;
    239                 }
    240             } else {
    241                 // No display breakpoint.
    242                 $wp_admin_bar->add_node(
    243                     array(
    244                         'id'     => self::PLUGIN_PREFIX,
    245                         'class'  => 'menupop',
    246                         'title'  => '<span class="ab-icon" aria-hidden="true"><span class="width">0</span></span>' .
    247                                         '<span class="ab-label">' .
    248                                             '<span class="width-wrap"><span class="width">0</span>px</span>' .
    249                                         '</span>',
    250                         'parent' => '',
    251                         'href'   => '#',
    252                     )
    253                 );
    254             }
     250                $breakpoint_index++;
     251            }
     252        } else {
     253            // No display breakpoint.
     254            $wp_admin_bar->add_node(
     255                array(
     256                    'id'     => self::PLUGIN_PREFIX,
     257                    'class'  => 'menupop',
     258                    'title'  => '<span class="ab-icon" aria-hidden="true"><span class="width">0</span></span>' .
     259                                    '<span class="ab-label">' .
     260                                        '<span class="width-wrap"><span class="width">0</span>px</span>' .
     261                                    '</span>',
     262                    'parent' => '',
     263                    'href'   => '#',
     264                )
     265            );
    255266        }
    256267    }
  • show-current-width/trunk/readme.txt

    r2973725 r2979825  
    11=== Show Current Width ===
    22Contributors: web83info
    3 Tags: admin,width,developement
     3Tags: admin,width,developement,design
    44Requires at least: 6.2
    5 Tested up to: 6.3.1
     5Tested up to: 6.3.2
    66Requires PHP: 7.4
    7 Stable tag: 1.2.0
     7Stable tag: 1.2.1
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    1919
    2020== Changelog ==
     21
     22= 1.2.1 - 2023-10-17 =
     23* Add: Option to display width only for the user who has the specific roles.
    2124
    2225= 1.2.0 - 2023-10-02 =
  • show-current-width/trunk/w83-show-current-width.php

    r2973725 r2979825  
    44 * Plugin URI:
    55 * Description:         This plugin shows a current screen width on WP adminbar.
    6  * Version:             1.2.0
     6 * Version:             1.2.1
    77 * Requires at least:   6.0
    8  * Tested up to:        6.3.1
     8 * Tested up to:        6.3.2
    99 * Requires PHP:        7.4
    1010 * Author:              web83info <[email protected]>
Note: See TracChangeset for help on using the changeset viewer.