| @@ -1,0 +1,165 @@ | ||
| 1 | +<?php | |
| 2 | +/* | |
| 3 | + * License: GPLv3 | |
| 4 | + * License URI: https://www.gnu.org/licenses/gpl.txt | |
| 5 | + * Copyright 2012-2026 Jean-Sebastien Morisset (https://surniaulula.com/) | |
| 6 | + */ | |
| 7 | + | |
| 8 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 9 | + | |
| 10 | + die( 'These aren\'t the droids you\'re looking for.' ); | |
| 11 | +} | |
| 12 | + | |
| 13 | +if ( ! class_exists( 'SucomUtil' ) ) { | |
| 14 | + | |
| 15 | + require_once dirname( __FILE__ ) . '/util.php'; | |
| 16 | +} | |
| 17 | + | |
| 18 | +if ( ! class_exists( 'SucomUtilMetabox' ) ) { | |
| 19 | + | |
| 20 | + class SucomUtilMetabox extends SucomUtil { | |
| 21 | + | |
| 22 | + public static function get_table_metadata( array $metadata, array $exclude_keys, $obj, $obj_id, $metabox_id, $admin_l10n, array $titles ) { | |
| 23 | + | |
| 24 | + $metabox_id = SucomUtil::sanitize_key( $metabox_id ); // Just in case. | |
| 25 | + $admin_l10n = sanitize_text_field( $admin_l10n ); // Just in case. | |
| 26 | + $obj_id = SucomUtil::sanitize_int( $obj_id ); // Returns integer or null. | |
| 27 | + $md_filtered = apply_filters( $metabox_id . '_metabox_table_metadata', $metadata, $obj ); | |
| 28 | + $exclude_keys = apply_filters( $metabox_id . '_metabox_table_exclude_keys', $exclude_keys, $obj ); | |
| 29 | + $delete_cap = apply_filters( $metabox_id . '_delete_meta_capability', 'manage_options', $obj ); | |
| 30 | + $del_icon_class = apply_filters( $metabox_id . '_delete_meta_icon_class', 'dashicons dashicons-table-row-delete' ); | |
| 31 | + $can_delete = $obj_id && current_user_can( $delete_cap, $obj_id, $obj ) ? true : false; | |
| 32 | + | |
| 33 | + $metabox_html = self::get_table_metadata_css( $metabox_id ); | |
| 34 | + $metabox_html .= '<table><thead>' . "\n"; | |
| 35 | + $metabox_html .= '<tr>'; | |
| 36 | + $metabox_html .= $can_delete ? '<th class="del-column"></th>' : ''; | |
| 37 | + $metabox_html .= '<th class="key-column">' . $titles[ 'key' ] . '</th>'; | |
| 38 | + $metabox_html .= '<th class="value-column">' . $titles[ 'value' ] . '</th>'; | |
| 39 | + $metabox_html .= '</tr>' . "\n"; | |
| 40 | + $metabox_html .= '</thead><tbody>' . "\n"; | |
| 41 | + | |
| 42 | + ksort( $md_filtered ); | |
| 43 | + | |
| 44 | + $row_count = 0; | |
| 45 | + | |
| 46 | + foreach( $md_filtered as $meta_key => $value ) { | |
| 47 | + | |
| 48 | + foreach ( $exclude_keys as $key_preg ) { | |
| 49 | + | |
| 50 | + if ( preg_match( $key_preg, $meta_key ) ) { | |
| 51 | + | |
| 52 | + continue 2; | |
| 53 | + } | |
| 54 | + } | |
| 55 | + | |
| 56 | + $row_count++; | |
| 57 | + | |
| 58 | + $is_added_meta = isset( $metadata[ $meta_key ] ) ? false : true; // Check before sanitizing key. | |
| 59 | + $meta_key = SucomUtil::sanitize_meta_key( $meta_key ); // Decode and remove html/js/css and quotes. | |
| 60 | + $meta_key_esc = esc_html( $meta_key ); | |
| 61 | + $value = is_string( $value ) ? $value : self::array_maybe_unserialize( $value ); | |
| 62 | + $value_esc = esc_html( var_export( $value, true ) ); | |
| 63 | + $table_row_id = SucomUtil::sanitize_key( $metabox_id . '_' . $obj_id . '_' . $meta_key ); | |
| 64 | + $metabox_html .= $is_added_meta ? '<tr class="added-meta">' : '<tr id="' . $table_row_id . '">'; | |
| 65 | + | |
| 66 | + if ( $can_delete ) { | |
| 67 | + | |
| 68 | + $onclick_js = 'sucomDeleteMeta' . | |
| 69 | + '( \'' . $metabox_id . // Already sanitized. | |
| 70 | + '\', \'' . $obj_id . // Already sanitized. | |
| 71 | + '\', \'' . $meta_key . // Already sanitized. | |
| 72 | + '\', \'' . $admin_l10n . // Already sanitized. | |
| 73 | + '\' );'; | |
| 74 | + | |
| 75 | + $metabox_html .= '<td class="del-column">'; | |
| 76 | + $metabox_html .= $is_added_meta ? '' : '<span class="' . $del_icon_class . '" onclick="' . $onclick_js . '"></span>'; | |
| 77 | + $metabox_html .= '</td>'; | |
| 78 | + } | |
| 79 | + | |
| 80 | + $metabox_html .= '<td class="key-column"><div><pre>' . $meta_key_esc . '</pre></div></td>'; | |
| 81 | + $metabox_html .= '<td class="value-column"><div><pre>' . $value_esc . '</pre></div></td>'; | |
| 82 | + $metabox_html .= '</tr>' . "\n"; | |
| 83 | + } | |
| 84 | + | |
| 85 | + if ( ! $row_count ) { | |
| 86 | + | |
| 87 | + $metabox_html .= '<tr>'; | |
| 88 | + $metabox_html .= $can_delete ? '<td class="del-column"></td>' : ''; | |
| 89 | + $metabox_html .= '<td class="key-column"><pre></pre></td>'; | |
| 90 | + $metabox_html .= '<td class="value-column"><pre></pre></td>'; | |
| 91 | + $metabox_html .= '</tr>' . "\n"; | |
| 92 | + } | |
| 93 | + | |
| 94 | + $metabox_html .= '</tbody></table>' . "\n"; | |
| 95 | + | |
| 96 | + return $metabox_html; | |
| 97 | + } | |
| 98 | + | |
| 99 | + public static function get_table_metadata_css( $metabox_id ) { | |
| 100 | + | |
| 101 | + $custom_style_css = ' | |
| 102 | + | |
| 103 | + div#' . $metabox_id . '.postbox table { | |
| 104 | + width:100%; | |
| 105 | + max-width:100%; | |
| 106 | + text-align:left; | |
| 107 | + table-layout:fixed; | |
| 108 | + } | |
| 109 | + | |
| 110 | + div#' . $metabox_id . '.postbox table tr.added-meta td { | |
| 111 | + background-color:#eee; | |
| 112 | + } | |
| 113 | + | |
| 114 | + div#' . $metabox_id . '.postbox table tr.added-meta td.del-column { | |
| 115 | + background-color:inherit; | |
| 116 | + } | |
| 117 | + | |
| 118 | + div#' . $metabox_id . '.postbox table .del-column { /* th and td */ | |
| 119 | + padding-top:15px; | |
| 120 | + padding-left:0; | |
| 121 | + border:none; | |
| 122 | + width:2em; | |
| 123 | + color:red; | |
| 124 | + } | |
| 125 | + | |
| 126 | + div#' . $metabox_id . '.postbox table .del-column span { | |
| 127 | + font-size:1em; | |
| 128 | + width:1em; | |
| 129 | + height:1em; | |
| 130 | + } | |
| 131 | + | |
| 132 | + div#' . $metabox_id . '.postbox table .del-column span:hover { | |
| 133 | + cursor:pointer; | |
| 134 | + } | |
| 135 | + | |
| 136 | + div#' . $metabox_id . '.postbox table .key-column { /* th and td */ | |
| 137 | + width:30%; | |
| 138 | + } | |
| 139 | + | |
| 140 | + div#' . $metabox_id . '.postbox table .value-column { /* th and td */ | |
| 141 | + width:auto; | |
| 142 | + } | |
| 143 | + | |
| 144 | + div#' . $metabox_id . '.postbox table td { | |
| 145 | + padding:10px; | |
| 146 | + vertical-align:top; | |
| 147 | + border:1px dotted #ccc; | |
| 148 | + } | |
| 149 | + | |
| 150 | + div#' . $metabox_id . '.postbox table td div { | |
| 151 | + overflow-x:auto; | |
| 152 | + } | |
| 153 | + | |
| 154 | + div#' . $metabox_id . '.postbox table td div pre { | |
| 155 | + margin:0; | |
| 156 | + padding:0; | |
| 157 | + } | |
| 158 | + '; | |
| 159 | + | |
| 160 | + $custom_style_css = self::minify_css( $custom_style_css, $metabox_id ); | |
| 161 | + | |
| 162 | + return '<style type="text/css">' . $custom_style_css . '</style>'; | |
| 163 | + } | |
| 164 | + } | |
| 165 | +} | |