Plugin Directory

Changeset 2469780


Ignore:
Timestamp:
02/05/2021 08:25:08 PM (5 years ago)
Author:
trasweb
Message:

Display Metadata v0.2

Location:
display-metadata/trunk
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • display-metadata/trunk/Metabox/class-metabox-factory.php

    r2443918 r2469780  
    99
    1010    private const NONE_ID                         = 0;
     11
    1112    private const DEFAULT_METABOX                 = __NAMESPACE__ . '\None';
     13
    1214    private const METABOX_TYPES_BY_SCREEN_VAR_KEY = [
    1315        'post'    => __NAMESPACE__ . '\Post',
    1416        'tag_ID'  => __NAMESPACE__ . '\Term',
    1517        'user_id' => __NAMESPACE__ . '\User',
     18        'c'       => __NAMESPACE__ . '\Comment',
    1619    ];
    1720
     
    2326     * @return Metabox
    2427     */
    25     final public static function get_current_metabox( array $screen_vars ): Metabox {
     28    final public static function get_current_metabox( array $screen_vars ): Metabox
     29    {
    2630        $screen_vars = array_filter( $screen_vars, 'is_numeric' );
    2731
    2832        foreach ( self::METABOX_TYPES_BY_SCREEN_VAR_KEY as $item_id_key => $metabox_type ) {
    29 
    30             if ( empty( $screen_vars[ $item_id_key ] ) || ! is_a( $metabox_type, Metabox::class, $allow_string = true )  ) {
     33            if ( empty( $screen_vars[ $item_id_key ] ) || !is_a( $metabox_type, Metabox::class, $allow_string = true ) ) {
    3134                continue;
    3235            }
    3336
    34             $item_id    = absint( $screen_vars[ $item_id_key ] );
     37            $item_id = absint( $screen_vars[ $item_id_key ] );
    3538
    3639            return $metabox_type::from_item_id( $item_id );
  • display-metadata/trunk/Metabox/class-metabox.php

    r2443918 r2469780  
    44
    55use Trasweb\Plugins\DisplayMetadata\Plugin;
     6
    67use const Trasweb\Plugins\DisplayMetadata\PLUGIN_NAME;
    78
     
    1112abstract class Metabox {
    1213
    13     protected const METABOX_FILE = Plugin::VIEWS_PATH . '/metabox.php';
    14     protected const ASSETS_FILE  = Plugin::VIEWS_PATH . '/assets.php';
    15     protected const HEADER_FILE  = Plugin::VIEWS_PATH . '/header.php';
    16     protected const TITLE        = '';
    17     protected const FOOTER_FILE  = Plugin::VIEWS_PATH . '/footer.php';
     14    protected const METABOX_FILE = Plugin::VIEWS_PATH . '/metabox.php';
    1815
    19     /**
    20      * @var string $item_id ID from user, post or term.
    21      */
    22     protected $item_id;
     16    protected const ASSETS_FILE  = Plugin::VIEWS_PATH . '/assets.php';
    2317
    24     private function __construct() {
    25         //not direct instance, please
    26     }
     18    protected const HEADER_FILE  = Plugin::VIEWS_PATH . '/header.php';
    2719
    28     private function __clone() {
    29         //not cloning please
    30     }
     20    protected const TITLE        = '';
    3121
    32     /**
    33      * Named constructor. Create an instance from an item id.
    34      *
    35      * @param int $item_id
    36      *
    37      * @return static
    38      */
    39     final public static function from_item_id( int $item_id ): self {
    40         $metabox          = new static();
    41         $metabox->item_id = $item_id;
     22    protected const FOOTER_FILE  = Plugin::VIEWS_PATH . '/footer.php';
    4223
    43         return $metabox;
    44     }
     24    /**
     25     * @var string $item_id ID from user, post or term.
     26     */
     27    protected $item_id;
    4528
    46     /**
    47      * Register a metabox in order to display it later.
    48      *
    49      * @return void
    50      */
    51     abstract public function register(): void;
     29    private function __construct()
     30    {
     31        // not direct instance, please
     32    }
    5233
    53     /**
    54      * Check if a metabox can be registered
    55      *
    56      * @return bool
    57      */
    58     abstract protected function can_be_registered(): bool;
     34    private function __clone()
     35    {
     36        // not cloning please
     37    }
    5938
    60     /**
    61      * Retrieve item properties/fields. E.g: ID, post_title, ...
    62      *
    63      * @return array
    64      */
    65     abstract protected function get_item_properties(): array;
     39    /**
     40     * Named constructor. Create an instance from an item id.
     41     *
     42     * @param integer $item_id
     43     *
     44     * @return static
     45     */
     46    final public static function from_item_id( int $item_id ): self
     47    {
     48        $metabox = new static();
     49        $metabox->item_id = $item_id;
    6650
    67     /**
    68      * Retrieve item metaata. E.g:
    69      *
    70      * @return array
    71      */
    72     abstract protected function get_item_metadata(): array;
     51        return $metabox;
     52    }
    7353
    74     /**
    75      * Display metadata metabox.
    76      *
    77      * @return void
    78      */
    79     final public function display(): void {
    80         $metabox_title = __( static::TITLE, PLUGIN_NAME );
     54    /**
     55     * Register a metabox in order to display it later.
     56     *
     57     * @return void
     58     */
     59    abstract public function register(): void;
    8160
    82         $item_properties = $this->get_item_properties();
    83         $item_metadata   = $this->get_item_metadata();
     61    /**
     62     * Check if a metabox can be registered
     63     *
     64     * @return boolean
     65     */
     66    abstract protected function can_be_registered(): bool;
    8467
    85         $item_vars = [
    86             __( 'Properties', PLUGIN_NAME ) => $item_properties,
    87             __( 'Metadata', PLUGIN_NAME )   => $item_metadata,
    88         ];
     68    /**
     69     * Retrieve item properties/fields. E.g: ID, post_title, ...
     70     *
     71     * @return array
     72     */
     73    abstract protected function get_item_properties(): array;
    8974
    90         $metadata_list = Metadata_Iterator::from_vars_list( $item_vars );
     75    /**
     76     * Retrieve item metaata. E.g:
     77     *
     78     * @return array
     79     */
     80    abstract protected function get_item_metadata(): array;
    9181
    92         require static::METABOX_FILE;
    93     }
     82    /**
     83     * Display metadata metabox.
     84     *
     85     * @return void
     86     */
     87    final public function display(): void
     88    {
     89        $metabox_title = __( static::TITLE, PLUGIN_NAME );
     90
     91        $item_properties = $this->get_item_properties();
     92        $item_metadata = $this->get_item_metadata();
     93
     94        $item_vars = [
     95            __( 'Properties', PLUGIN_NAME ) => $item_properties,
     96            __( 'Metadata', PLUGIN_NAME )   => $item_metadata,
     97        ];
     98
     99        $metadata_list = Metadata_Iterator::from_vars_list( $item_vars );
     100
     101        include static::METABOX_FILE;
     102    }
    94103}
  • display-metadata/trunk/Metabox/class-metadata-iterator.php

    r2443918 r2469780  
    1313class Metadata_Iterator extends ArrayIterator {
    1414
    15     protected const META_LIST_VIEW = Plugin::VIEWS_PATH . '/meta_list.php';
     15    protected const META_LIST_VIEW = Plugin::VIEWS_PATH . '/meta_list.php';
    1616
    17     private $depth;
     17    private $depth;
    1818
    19     /**
    20      * Named constructor.
    21      *
    22      * @param array $vars_list List of vars.
    23      * @param int   $depth     Current depth for these vars.
    24      *
    25      * @return static
    26      */
    27     public static function from_vars_list( array $vars_list, int $depth = 1 ): self {
    28         $iterator        = new self( $vars_list, ArrayIterator::STD_PROP_LIST );
    29         $iterator->depth = $depth;
     19    /**
     20     * Named constructor.
     21     *
     22     * @param array   $vars_list List of vars.
     23     * @param integer $depth     Current depth for these vars.
     24     *
     25     * @return static
     26     */
     27    public static function from_vars_list( array $vars_list, int $depth = 1 ): self
     28    {
     29        $iterator = new self( $vars_list, ArrayIterator::STD_PROP_LIST );
     30        $iterator->depth = $depth;
    3031
    31         return $iterator;
    32     }
     32        return $iterator;
     33    }
    3334
    34     /**
    35      * Retrieve current value of current meta.
    36      *
    37      * @return $this|mixed|string
    38      */
    39     public function current() {
    40         $meta_value = maybe_unserialize( parent::current() );
     35    /**
     36     * Retrieve current value of current meta.
     37     *
     38     * @return $this|mixed|string
     39     */
     40    public function current()
     41    {
     42        $meta_value = maybe_unserialize( parent::current() );
    4143
    42         //Sometimes, unserialize returns objects
    43         if ( \is_object( $meta_value ) ) {
    44             $meta_value = \json_decode( \json_encode( $meta_value ), true );
    45         }
     44        // Sometimes, unserialize returns objects
     45        if ( \is_object( $meta_value ) ) {
     46            $meta_value = \json_decode( \json_encode( $meta_value ), true );
     47        }
    4648
    47         if ( is_array( $meta_value ) ) {
    48             ksort( $meta_value );
     49        if ( is_array( $meta_value ) ) {
     50            ksort( $meta_value );
    4951
    50             return Metadata_Iterator::from_vars_list( $meta_value, $this->depth + 1 );
    51         }
     52            return Metadata_Iterator::from_vars_list( $meta_value, $this->depth + 1 );
     53        }
    5254
    53         if ( is_null( $meta_value ) ) {
    54             return 'NULL';
    55         }
     55        if ( is_null( $meta_value ) ) {
     56            return 'NULL';
     57        }
    5658
    57         return htmlentities( (string) $meta_value );
    58     }
     59        return make_clickable( htmlentities( (string)$meta_value ) );
     60    }
    5961
    60     /**
    61      * Current depth.
    62      *
    63      * @return integer
    64      */
    65     public function get_depth(): int {
    66         return (int) $this->depth;
    67     }
     62    public function get_attributes(): string
     63    {
     64        $attrs[] = ( 1 === $this->get_depth() ) ? 'meta_headers' : 'meta_item';
     65        $attrs[] = 'depth_' . $this->get_depth();
     66        $attrs[] = ( is_array( parent::current() ) && empty( parent::current() ) ) ? 'meta_empty_array' : '';
    6867
    69     /**
    70      * Genererate a view for current metadata list.
    71      *
    72      * @return string
    73      */
    74     public function __toString() {
    75         $metadata_list = $this;
     68        if ( '' !== parent::current() ) {
     69            $attrs[] = ( ! is_array( parent::current() ) && ! is_serialized( parent::current() ) ) ? 'meta_scalar' : 'meta_array';
     70        }
    7671
    77         ob_start();
    78         ( static function () use ( $metadata_list ) {
    79             include static::META_LIST_VIEW;
    80         } )();
     72        return implode( ' ', $attrs );
     73    }
    8174
    82         return \ob_get_clean() ?: '';
    83     }
     75    /**
     76     * Current depth.
     77     *
     78     * @return integer
     79     */
     80    private function get_depth(): int
     81    {
     82        return (int)$this->depth;
     83    }
     84
     85    /**
     86     * Genererate a view for current metadata list.
     87     *
     88     * @return string
     89     */
     90    public function __toString()
     91    {
     92        ob_start();
     93        ( static function ( Metadata_Iterator $metadata_list ) {
     94            include static::META_LIST_VIEW;
     95        } )( $this );
     96
     97        return \ob_get_clean() ?: '';
     98    }
    8499}
  • display-metadata/trunk/Metabox/class-none.php

    r2443918 r2469780  
    77 */
    88class None extends Metabox {
     9    /**
     10     * Nothing to do.
     11     */
     12    public function register(): void
     13    {
     14        // None
     15    }
    916
    10     /**
    11      * Nothing to do.
    12      */
    13     public function register(): void {
    14         //None
    15     }
     17    /**
     18     * cannot can se registered
     19     *
     20     * @return array
     21     */
     22    protected function can_be_registered(): bool
     23    {
     24        return false;
     25    }
    1626
    17     /**
    18      * cannot can se registered
    19      *
    20      * @return array
    21      */
    22     protected function can_be_registered(): bool {
    23         return false;
    24     }
     27    /**
     28     * Retrieve none
     29     *
     30     * @return array
     31     */
     32    protected function get_item_properties(): array
     33    {
     34        return [];
     35    }
    2536
    26     /**
    27      * Retrieve none
    28      *
    29      * @return array
    30      */
    31     protected function get_item_properties(): array {
    32         return [];
    33     }
    34 
    35     /**
    36      * Retrieve none
    37      *
    38      * @return array
    39      */
    40     protected function get_item_metadata(): array {
    41         return [];
    42     }
     37    /**
     38     * Retrieve none
     39     *
     40     * @return array
     41     */
     42    protected function get_item_metadata(): array
     43    {
     44        return [];
     45    }
    4346}
  • display-metadata/trunk/Metabox/class-post.php

    r2443918 r2469780  
    44
    55use Trasweb\Plugins\DisplayMetadata\Plugin;
     6
    67use const ARRAY_A;
    78
     
    1112final class Post extends Metabox {
    1213
    13     protected const TITLE       = 'Post information';
    14     protected const HEADER_FILE    = Plugin::VIEWS_PATH . '/nothing.php';
    15     protected const FOOTER_FILE    = Plugin::VIEWS_PATH . '/nothing.php';
     14    protected const TITLE       = 'Post information';
    1615
    17     /**
    18      * Register a metabox in order to display it later.
    19      *
    20      * @return void
    21      */
    22     public function register(): void {
    23         if ( !$this->can_be_registered() ) {
    24             return;
    25         }
     16    protected const HEADER_FILE = Plugin::VIEWS_PATH . '/nothing.php';
    2617
    27         add_meta_box(
    28             'trasweb_metadata_metabox',     // Unique ID
    29             static::TITLE,                // Box title
    30             [ $this, 'display' ],              // Content callback
    31             $this->get_accepted_cpt()          // Post type
    32         );
    33     }
     18    protected const FOOTER_FILE = Plugin::VIEWS_PATH . '/nothing.php';
    3419
    35     /**
    36      * Check if a metabox can be registered
    37      *
    38      * @return bool
    39      */
    40     protected function can_be_registered(): bool {
    41         $current_screen = Plugin::get_current_screen();
     20    /**
     21     * Register a metabox in order to display it later.
     22     *
     23     * @return void
     24     */
     25    public function register(): void
     26    {
     27        if ( ! $this->can_be_registered() ) {
     28            return;
     29        }
    4230
    43         return 'post' === $current_screen->slug;
    44     }
     31        add_meta_box( 'trasweb_metadata_metabox',     // Unique ID
     32            static::TITLE,                            // Box title
     33            [ $this, 'display' ],                     // Content callback
     34            $this->get_accepted_cpt()          // Post type
     35        );
     36    }
    4537
    46     /**
    47      * Helepr: Retrieve list of post type with show-ui. This cpt are where the metabox will be displayed.
    48     *
    49      * @return array
    50     */
    51     protected function get_accepted_cpt(): array {
    52         return array_values( get_post_types( [ 'show_ui' => true ] ) ?: [] );
    53     }
     38    /**
     39     * Check if a metabox can be registered
     40    *
     41     * @return boolean
     42    */
     43    protected function can_be_registered(): bool
     44    {
     45        $current_screen = Plugin::get_current_screen();
    5446
    55     /**
    56      * Retrieve item properties/fields. E.g: ID, post_title, ...
    57      *
    58      * @return array
    59      */
    60     protected function get_item_properties(): array {
    61         return get_post( $this->item_id, ARRAY_A ) ?: [];
    62     }
     47        return 'post' === $current_screen->slug;
     48    }
    6349
    64     /**
    65      * Retrieve item metaata. E.g: _edit_lock
    66      *
    67      * @return array
    68      */
    69     protected function get_item_metadata(): array {
    70         return array_map( 'array_shift', get_post_meta( $this->item_id ) ?: [] );
    71     }
     50    /**
     51     * Helepr: Retrieve list of post type with show-ui. This cpt are where the metabox will be displayed.
     52     *
     53     * @return array
     54     */
     55    protected function get_accepted_cpt(): array
     56    {
     57        return array_values( get_post_types( [ 'show_ui' => true ] ) ?: [] );
     58    }
     59
     60    /**
     61     * Retrieve item properties/fields. E.g: ID, post_title, ...
     62     *
     63     * @return array
     64     */
     65    protected function get_item_properties(): array
     66    {
     67        return get_post( $this->item_id, ARRAY_A ) ?: [];
     68    }
     69
     70    /**
     71     * Retrieve item metaata. E.g: _edit_lock
     72     *
     73     * @return array
     74     */
     75    protected function get_item_metadata(): array
     76    {
     77        return array_map( 'array_shift', get_post_meta( $this->item_id ) ?: [] );
     78    }
    7279}
  • display-metadata/trunk/Metabox/class-term.php

    r2443918 r2469780  
    44
    55use Trasweb\Plugins\DisplayMetadata\Plugin;
     6
    67use const ARRAY_A;
    78
     
    1112final class Term extends Metabox {
    1213
    13     protected const TITLE = 'Term information';
     14    protected const TITLE = 'Term information';
    1415
    15     /**
    16      * Register a metabox in order to display it later.
    17      *
    18      * @return void
    19      */
    20     public function register(): void {
    21         if ( !$this->can_be_registered() ) {
    22             return;
    23         }
     16    /**
     17     * Register a metabox in order to display it later.
     18     *
     19     * @return void
     20     */
     21    public function register(): void
     22    {
     23        if ( ! $this->can_be_registered() ) {
     24            return;
     25        }
    2426
    25         $term = $this->get_item_properties();
     27        $term = $this->get_item_properties();
    2628
    27         if ( empty( $term[ 'term_id' ] ) ) {
    28             return;
    29         }
     29        if ( empty( $term[ 'term_id' ] ) ) {
     30            return;
     31        }
    3032
    31         add_action( $term[ 'taxonomy' ] . '_edit_form', [ $this, 'display' ] );
    32     }
     33        add_action( $term[ 'taxonomy' ] . '_edit_form', [ $this, 'display' ] );
     34    }
    3335
    34     /**
    35      * Check if a metabox can be registered
    36      *
    37      * @return bool
    38      */
    39     protected function can_be_registered(): bool {
    40         $current_screen = Plugin::get_current_screen();
     36    /**
     37     * Check if a metabox can be registered
     38     *
     39     * @return boolean
     40     */
     41    protected function can_be_registered(): bool
     42    {
     43        $current_screen = Plugin::get_current_screen();
    4144
    42         return 'term' === $current_screen->slug;
    43     }
     45        return 'term' === $current_screen->slug;
     46    }
    4447
    45     /**
    46      * Retrieve item properties/fields. E.g: term_id, name, ...
    47      *
    48      * @return array
    49      */
    50     protected function get_item_properties(): array {
    51         return get_term( $this->item_id, '', ARRAY_A ) ?: [];
    52     }
     48    /**
     49     * Retrieve item properties/fields. E.g: term_id, name, ...
     50     *
     51     * @return array
     52     */
     53    protected function get_item_properties(): array
     54    {
     55        return get_term( $this->item_id, '', ARRAY_A ) ?: [];
     56    }
    5357
    54     /**
    55      * Retrieve item metaata.
    56      *
    57      * @return array
    58      */
    59     protected function get_item_metadata(): array {
    60         return array_map( 'array_shift', get_term_meta( $this->item_id ) ?: [] );
    61     }
     58    /**
     59     * Retrieve item metaata.
     60     *
     61     * @return array
     62     */
     63    protected function get_item_metadata(): array
     64    {
     65        return array_map( 'array_shift', get_term_meta( $this->item_id ) ?: [] );
     66    }
    6267}
  • display-metadata/trunk/assets/display_metadata.css

    r2443918 r2469780  
    2929}
    3030
     31#trasweb-metadata-metabox .meta_empty_array table {
     32    border: 1px solid #7e8993;
     33    box-sizing: border-box;
     34    margin: 13px 6px;
     35}
     36
    3137#trasweb-metadata-metabox td.meta_value table {
    3238    box-sizing: border-box;
     
    3642#trasweb-metadata-metabox td, #trasweb-metadata-metabox th {
    3743    border: 1px solid #7e8993;
    38     font-size: 0.95em;
     44    font-size: 1em;
    3945    vertical-align: baseline;
    4046    padding: 4px 5px;
    4147}
    4248
     49#trasweb-metadata-metabox .depth_2:hover td.meta_key, #trasweb-metadata-metabox .depth_2:hover td.meta_value {
     50    background: #dbdbdb;
     51}
     52
     53#trasweb-metadata-metabox .depth_1 > td.meta_key {
     54    font-weight: bold;
     55    font-size: 1.2em;
     56}
     57
    4358#trasweb-metadata-metabox td.meta_key {
    44     background-color: #f3f4f5;
     59    background-color: #f1f1f1;
    4560    font-weight: 600;
     61    color: #000;
     62    letter-spacing: 1px;
     63}
     64
     65#trasweb-metadata-metabox .meta_item > td.meta_key:hover{
     66    position: relative;
     67    background-clip: padding-box; /* fix bug with borders in position relative */
     68}
     69
     70/** label for copy */
     71#trasweb-metadata-metabox .meta_item > td.meta_key:hover:after {
     72    width: 100px;
     73    height: 15px;
     74    display: inline-block;
     75    position: absolute;
     76    content: "Click to copy key";
     77    background-color: #555;
     78    color: #fff;
     79    border-radius: 6px;
     80    padding: 5px;
     81    z-index: 1;
     82    top: -30px;
     83    left: 0;
     84    text-align: center;
     85    font-size: 10px;
    4686}
    4787
     
    5191    word-wrap: break-word;
    5292    width: 100%;
     93    color: #333;
    5394}
     95
     96#trasweb-metadata-metabox .meta_scalar > td.meta_value:hover{
     97    position: relative;
     98    background-clip: padding-box; /* fix bug with borders in position relative */
     99}
     100
     101/** Zebra style */
     102#trasweb-metadata-metabox tr:nth-child(odd) > td.meta_value {
     103    background-color: #f9f9f9;
     104}
     105
     106/** label for copy */
     107#trasweb-metadata-metabox .meta_scalar > td.meta_value:hover:after {
     108    width: 100px;
     109    height: 15px;
     110    display: inline-block;
     111    position: absolute;
     112    content: "Click to copy value";
     113    background-color: #555;
     114    color: #fff;
     115    border-radius: 6px;
     116    padding: 5px;
     117    z-index: 1;
     118    top: -34px;
     119    left: 45%;
     120    text-align: center;
     121    font-size: 10px;
     122}
     123
  • display-metadata/trunk/assets/display_metadata.js

    r2443918 r2469780  
    1 trasweb_fit_metabox_width = function(){
     1trasweb_fit_metabox_width = function () {
    22    /* Avoid overflow */
    3     var metadata_container =  document.getElementById('trasweb-metadata-metabox');
     3    var metadata_container = document.getElementById('trasweb-metadata-metabox');
    44
    5     if( 0 === metadata_container.clientWidth ) {
     5    if (0 === metadata_container.clientWidth) {
    66        /* Fix Gutenberg */
    77        setTimeout('trasweb_fit_metabox_width()', 100);
    8         return ;
     8        return;
    99    }
    1010
    11     for( var depth_i = 1; true; depth_i++ ) {
     11    for (var depth_i = 1; true; depth_i++) {
    1212        var main_tr_list = metadata_container.querySelectorAll('.depth_' + depth_i);
    1313
    14         if( main_tr_list.length <= 0 ) {
     14        if (main_tr_list.length <= 0) {
    1515            break;
    1616        }
    1717
    18         for( var i = 0; i < main_tr_list.length; i++ ) {
    19             var top_container =  main_tr_list[i].parentElement.parentElement;
     18        for (var i = 0; i < main_tr_list.length; i++) {
     19            var top_container = main_tr_list[i].parentElement.parentElement;
    2020            var td_key = main_tr_list[i].children[0];
    21             var td_value  = main_tr_list[i].children[1];
     21            var td_value = main_tr_list[i].children[1];
    2222
    23             if( ( td_value.clientWidth + td_key.clientWidth ) > top_container.clientWidth ) {
    24                 td_value.style.maxWidth = ( top_container.clientWidth - td_key.clientWidth - 15 /* padding */ ) + 'px';
     23            if ((td_value.clientWidth + td_key.clientWidth) > top_container.clientWidth) {
     24                td_value.style.maxWidth = (top_container.clientWidth - td_key.clientWidth - 15 /* padding */) + 'px';
    2525            }
     26
     27            var event_data = { 'td_value': td_value, 'td_key': td_key, 'tr': main_tr_list[i] };
     28            var event = new CustomEvent("meta_element_loaded", {detail: event_data});
     29
     30            metadata_container.dispatchEvent(event);
    2631        }
    2732    }
    2833};
    2934
     35
     36
    3037/**
    3138 * Show/Hide metabox when click in
    3239 */
    33 document.addEventListener('DOMContentLoaded', function() {
     40document.addEventListener('DOMContentLoaded', function () {
    3441    /* non post container */
    35     var container =  document.getElementById('trasweb_metadata_metabox__container');
     42    var container = document.getElementById('trasweb_metadata_metabox__container');
    3643
    3744    /* enable open/close */
    38     if( null != container ) {
    39         container.getElementsByTagName('h2')[0].addEventListener('click', function(event) {
     45    if (null != container) {
     46        container.getElementsByTagName('h2')[0].addEventListener('click', function (event) {
    4047            this.classList.toggle('closed');
    4148        }.bind(container));
    4249    }
    4350
     51    /** enable copy key / value */
     52    if (navigator.clipboard) {
     53        var metadata_container = document.getElementById('trasweb-metadata-metabox');
     54
     55        metadata_container.addEventListener('meta_element_loaded', function (e) {
     56            e.detail.td_key.addEventListener('click', function () {
     57                return navigator.clipboard.writeText(this.innerText);
     58            });
     59
     60            if(e.detail.tr.classList.contains( 'meta_scalar') ) {
     61                e.detail.td_value.addEventListener('click', function () {
     62                    return navigator.clipboard.writeText(this.innerText);
     63                });
     64            }
     65
     66        });
     67    }
     68
     69    /** enable fit width */
    4470    trasweb_fit_metabox_width();
    4571});
  • display-metadata/trunk/class-plugin.php

    r2443918 r2469780  
    44
    55use Trasweb\Plugins\DisplayMetadata\Metabox\Metabox_Factory;
     6
    67use function define;
    78
     
    1112final class Plugin {
    1213
    13     public const PATH                = __DIR__;
    14     public const NAMESPACE            = __NAMESPACE__;
    15     public const VIEWS_PATH          = self::PATH . '/views';
    16     public const ASSETS_PATH          = self::PATH . '/assets';
    17     public const METABOX_FILE_PATTERN = self::PATH . '/Metabox/class-%s.php';
    18     public const NEEDED_CAPABILITY    = 'display_metadata_metabox';
     14    public const PATH = __DIR__;
     15    public const NAMESPACE = __NAMESPACE__;
     16    public const VIEWS_PATH = self::PATH . '/views';
     17    public const ASSETS_PATH = self::PATH . '/assets';
     18    public const METABOX_FILE_PATTERN = self::PATH . '/Metabox/class-%s.php';
     19    public const NEEDED_CAPABILITY = 'display_metadata_metabox';
    1920
    20     private $screen_vars;
     21    private $screen_vars;
    2122
    22     /**
    23      * Plugin constructor.
    24      *
    25      * @param array $screen_vars Params( normally $_GET ) from current screen.
    26      */
    27     public function __construct( array $screen_vars ) {
    28         $this->screen_vars = $screen_vars;
    29     }
     23    /**
     24     * Plugin constructor.
     25     *
     26     * @param array $screen_vars Params( normally $_GET ) from current screen.
     27     */
     28    public function __construct( array $screen_vars )
     29    {
     30        $this->screen_vars = $screen_vars;
     31    }
    3032
    31     /**
    32      * Plugin Starts
    33     *
    34      * @return void
    35     */
    36     public function __invoke(): void {
    37         if ( !current_user_can( 'administrator' ) && !\current_user_can( self::NEEDED_CAPABILITY ) ) {
    38             return;
    39         }
     33    /**
     34     * Retrieve current screen.
     35    *
     36     * @return \WP_Screen
     37    */
     38    final public static function get_current_screen(): \WP_Screen
     39    {
     40        include_once ABSPATH . 'wp-admin/includes/post.php';
     41        include_once ABSPATH . 'wp-admin/includes/class-wp-screen.php';
    4042
    41         $this->bootstrap();
     43        $pagenow = self::get_page_now();
     44        $screen = \WP_Screen::get($pagenow);
    4245
    43         $metabox = Metabox_Factory::get_current_metabox( $this->screen_vars );
    44         $metabox->register();
    45     }
     46        $screen->slug = $screen->base ?: '';
     47        $screen->base = strtok($screen->slug, '-') ?: '';
     48        $screen->place = strtok('') ?: 'main';
    4649
    47     /**
    48      * Define basic of plugin in order to can be loaded.
    49      *
    50      * @return void
    51      */
    52     final private function bootstrap(): void {
    53         define( self::NAMESPACE . '\PLUGIN_NAME', basename( self::PATH ) );
    54         define( self::NAMESPACE . '\PLUGIN_TITLE', __( 'Display metadata', PLUGIN_NAME ) );
     50        return $screen;
     51    }
    5552
    56         //Fix for profile admin pages
    57         if ( defined( 'IS_PROFILE_PAGE' ) && IS_PROFILE_PAGE ) {
    58             $this->screen_vars[ 'user_id' ] = get_current_user_id();
    59         }
     53    /**
     54     * Helper: Retrieve page now from request
     55     *
     56     * @return string
     57     */
     58    final private static function get_page_now(): string
     59    {
     60        global $pagenow;
    6061
    61         $this->register_metabox_autoload();
    62     }
     62        if ( ! $pagenow ) {
     63            unset($pagenow);
    6364
    64     /**
    65      * Register plugin autoload for metabo classes.
    66      *
    67      * @return void
    68      */
    69     final private function register_metabox_autoload(): void {
    70         spl_autoload_register( function ( $class_name ) {
    71             if ( 0 !== strpos( $class_name, self::NAMESPACE ) ) {
    72                 return;
    73             }
     65            $current_base_url = parse_url($_SERVER[ 'REQUEST_URI' ], PHP_URL_PATH) ?: '';
    7466
    75             $file_name = strtolower( str_replace('_', '-', trim( strrchr( $class_name, '\\' ), '\\' ) ) );
    76             require sprintf( self::METABOX_FILE_PATTERN, $file_name );
    77         }, $throw_exception = false );
    78     }
     67            $pagenow = strrchr($current_base_url, '/') ?: $current_base_url;
     68        }
    7969
    80     /**
    81      * Retrieve current screen.
    82      *
    83      * @return \WP_Screen
    84      */
    85     final public static function get_current_screen(): \WP_Screen {
    86         include_once ABSPATH . 'wp-admin/includes/post.php';
    87         include_once ABSPATH . 'wp-admin/includes/class-wp-screen.php';
     70        return basename($pagenow, '.php');
     71    }
    8872
    89         $pagenow = self::get_page_now();
    90         $screen  = \WP_Screen::get( $pagenow );
     73    /**
     74     * Plugin Starts
     75     *
     76     * @return void
     77     */
     78    public function __invoke(): void
     79    {
     80        if ( ! current_user_can('administrator') && ! \current_user_can(self::NEEDED_CAPABILITY) ) {
     81            return;
     82        }
    9183
    92         $screen->slug  = $screen->base ?: '';
    93         $screen->base  = strtok( $screen->slug, '-' ) ?: '';
    94         $screen->place = strtok( '' ) ?: 'main';
     84        $this->bootstrap();
    9585
    96         return $screen;
    97     }
     86        $metabox = Metabox_Factory::get_current_metabox($this->screen_vars);
     87        $metabox->register();
     88    }
    9889
    99     /**
    100      * Helper: Retrieve page now from request
    101      *
    102      * @return string
    103      */
    104     final private static function get_page_now(): string {
    105         global $pagenow;
     90    /**
     91     * Define basic of plugin in order to can be loaded.
     92     *
     93     * @return void
     94     */
     95    final private function bootstrap(): void
     96    {
     97        define(self::NAMESPACE . '\PLUGIN_NAME', basename(self::PATH));
     98        define(self::NAMESPACE . '\PLUGIN_TITLE', __('Display metadata', PLUGIN_NAME));
    10699
    107         if ( !$pagenow ) {
    108             unset( $pagenow );
     100        // Fix for profile admin pages.
     101        if ( defined('IS_PROFILE_PAGE') && IS_PROFILE_PAGE ) {
     102            $this->screen_vars[ 'user_id' ] = get_current_user_id();
     103        }
    109104
    110             $current_base_url = parse_url( $_SERVER[ 'REQUEST_URI' ], PHP_URL_PATH ) ?: '';
     105        $this->register_metabox_autoload();
     106    }
    111107
    112             $pagenow = strrchr( $current_base_url, '/' ) ?: $current_base_url;
    113         }
     108    /**
     109     * Register plugin autoload for metabo classes.
     110     *
     111     * @return void
     112     */
     113    final private function register_metabox_autoload(): void
     114    {
     115        spl_autoload_register(static function ( $class_name ) {
     116            if ( 0 !== strpos($class_name, self::NAMESPACE) ) {
     117                return;
     118            }
    114119
    115         return basename( $pagenow, '.php' );
    116     }
     120            $file_name = strtolower(str_replace('_', '-', trim(strrchr($class_name, '\\'), '\\')));
     121            include sprintf(self::METABOX_FILE_PATTERN, $file_name);
     122        }, $throw_exception = false);
     123    }
    117124}
  • display-metadata/trunk/display-metadata.php

    r2443918 r2469780  
    66 * Plugin Name: Display Metadata
    77 * Plugin URI: https://github.com/trasweb/DisplayMetadata
    8  * Description: Shows metadata( fields, custom fields and protected metas ) in a metabox for posts( any CPT ), terms( any taxonomy ) and users. Metadata are displayed for humans( organized and unserialized ). This metabox only will be displayed per either administrator users or users with `display_metadata_metabox` capability.
    9  * Version: 0.1
     8 * Description: Shows metas in a metabox for posts( any CPT ), terms( any taxonomy ), comments and users. Metadata are displayed for humans( organized and unserialized ). This metabox only will be displayed per either administrator users or users with `display_metadata_metabox` capability.
     9 * Version: 0.2
    1010 * Author: Manuel Canga
    1111 * Author URI: https://manuelcanga.dev
     
    1616*/
    1717
    18 if ( !defined( "ABSPATH" ) ) {
    19     die( "Hello, World!" );
     18if ( ! defined( 'ABSPATH' ) ) {
     19    die( 'Hello, World!' );
    2020}
    2121
    22 if ( !is_admin() ) {
    23     return;
     22if ( ! is_admin() ) {
     23    return;
    2424}
    2525
    2626( static function () {
    27     require_once __DIR__ . '/class-plugin.php';
     27    include_once __DIR__ . '/class-plugin.php';
    2828
    29     ${'display-metadata'} = new Plugin( $_GET ?: [] );
     29    ${'display-metadata'} = new Plugin( $_GET ?: [] );
    3030
    31     add_action( 'admin_init', ${'display-metadata'} );
     31    add_action( 'admin_init', ${'display-metadata'} );
    3232} )();
  • display-metadata/trunk/readme.txt

    r2443918 r2469780  
    55Tested up to: 5.6
    66Requires PHP: 7.2
    7 Stable tag: 0.1
     7Stable tag: 0.2
    88License: GPLv3 or later
    99License URI: http://www.gnu.org/licenses/gpl-3.0.html
    1010
    11 Shows metas( fields, custom fields and protected metas ) in a metabox for posts( any CPT ), terms( any taxonomy ) and users. Metas are displayed for humans( organized and unserialized ). This metabox only will be displayed per either administrator users or users with `display_metadata_metabox` capability.
     11Shows metas( fields, custom fields and protected metas ) in a metabox for posts( any CPT ), terms( any taxonomy ), comments and users. Metas are displayed for humans( organized and unserialized ). This metabox only will be displayed per either administrator users or users with `display_metadata_metabox` capability.
    1212
    1313== Description ==
     
    1515As a developer you normally need know about a value of some meta( or properties ) of a post, term o user.
    1616
    17 As a sysadmin maybe you need know about which meta are created in posts/terms/users in order to remove which are unnecessary.
     17As a sysadmin maybe you need know about which meta are created in posts/terms/comments/users in order to remove which are unnecessary.
    1818
    1919As a plugin author you need check if you plugin is adding  meta rightly.
     
    2222
    2323In all these cases( and others ), you can use Display Meta plugin.
     24
     25== Thanks ==
     26
     27* Codection for [reviewing](https://codection.com/plugin-para-visualizar-metadatos-en-wordpress/) this plugin.
     28
     29* [Javier Esteban – @nobnob](https://profiles.wordpress.org/nobnob/) and [Yordan Soares – @yordansoares](https://profiles.wordpress.org/yordansoares/) for theirs translations.
     30
     31* This plugin is inspired on [David Navia's](https://profiles.wordpress.org/davidnaviaweb/) code, [Post Meta Viewer](https://es.wordpress.org/plugins/post-meta-viewer/) and [JS Morisset's](https://profiles.wordpress.org/jsmoriss/) plugins.
    2432
    2533== Send me bugs or improvements ==
     
    3038
    31391. You can see post( from any CPT ) properties, custom fields and protected meta from a post metabox.
    32 2. Yu can see term( from any taxonoy ) properties, custom fields and protected meta.
     402. You can see term( from any taxonoy ) properties, custom fields and protected meta.
    33413. Finally, You also can see user properties, custom fields and protected meta.
    3442
     
    41491. Click the *Install Now* link for this plugin.
    42501. Click the *Activate Plugin* button.
    43 
     511. **Now, this plugin add a new metabox on editing form of posts( any CPT ), terms( any taxonomy ), comments and users**.
    4452
    4553== Changelog ==
    4654More detailed changes in [Display Metadata GitHub Repository](https://github.com/trasweb/DisplayMetadata/).
    4755
    48 = 0.1 / 2020-01-21 =
     56= 0.2 / 2021-02-05
     57
     58* Support to comment metadata.
     59* Support to copy meta keys / values to clipboard.
     60* Make clickable links.
     61* Allow showing empty array.
     62
     63= 0.1 / 2020-12-21 =
    4964
    5065* Initial release.
     
    5368
    5469= 0.1 =
     70
    5571First version. Install it
  • display-metadata/trunk/views/assets.php

    r2443918 r2469780  
    11<?php
     2
    23use Trasweb\Plugins\DisplayMetadata\Plugin;
     4
    35?>
    46
    57<style>
    6  <?= include Plugin::ASSETS_PATH.'/display_metadata.css' ?>
     8 <?= require Plugin::ASSETS_PATH . '/display_metadata.css' ?>
    79</style>
    810<script>
    9  <?= include Plugin::ASSETS_PATH . '/display_metadata.js' ?>
     11 <?= require Plugin::ASSETS_PATH . '/display_metadata.js' ?>
    1012</script>
  • display-metadata/trunk/views/meta_list.php

    r2443918 r2469780  
    33   <?php
    44   foreach ( $metadata_list as $meta_key => $meta_value ): ?>
    5       <tr class="depth_<?= $metadata_list->get_depth() ?>">
     5      <tr class="<?= $metadata_list->get_attributes() ?>">
    66         <td class="meta_key"><?= $meta_key ?></td>
    77         <td class="meta_value"><?= $meta_value  ?></td>
    88      </tr><?php
    99   endforeach;
    10    ?>
     10    ?>
    1111   </tbody>
    1212</table>
Note: See TracChangeset for help on using the changeset viewer.