| 1 |
<?php |
| 2 |
/* |
| 3 |
* License: GPLv3 |
| 4 |
* License URI: https://www.gnu.org/licenses/gpl.txt |
| 5 |
* Copyright 2012-2026 Jean-Sebastien Morisset (https://wpsso.com/) |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
|
| 10 |
die( 'These aren\'t the droids you\'re looking for.' ); |
| 11 |
} |
| 12 |
|
| 13 |
if ( ! defined( 'WPSSO_PLUGINDIR' ) ) { |
| 14 |
|
| 15 |
die( 'Do. Or do not. There is no try.' ); |
| 16 |
} |
| 17 |
|
| 18 |
if ( ! class_exists( 'WpssoUtilMetabox' ) ) { |
| 19 |
|
| 20 |
class WpssoUtilMetabox { |
| 21 |
|
| 22 |
private $p; // Wpsso class object. |
| 23 |
|
| 24 |
/* |
| 25 |
* Instantiated by WpssoUtil->__construct(). |
| 26 |
*/ |
| 27 |
public function __construct( &$plugin ) { |
| 28 |
|
| 29 |
$this->p =& $plugin; |
| 30 |
|
| 31 |
if ( $this->p->debug->enabled ) { |
| 32 |
|
| 33 |
$this->p->debug->mark(); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
public static function show_is_hidden_content( $mb, $screen_id = '', $user_id = null ) { |
| 38 |
|
| 39 |
if ( WpssoUser::is_metabox_hidden( $mb[ 'id' ], $screen_id, $user_id ) ) { |
| 40 |
|
| 41 |
echo '<table class="wpsso-dashboard-widget">'; |
| 42 |
echo '<tr><td>'; |
| 43 |
echo '<p class="centered">' . __( 'Reload the page to show this content.', 'wpsso' ) . '</p>'; |
| 44 |
echo '</tr></td></table>'; |
| 45 |
|
| 46 |
return true; |
| 47 |
} |
| 48 |
|
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
public function do_tabbed( $metabox_id = '', $tabs = array(), $table_rows = array(), $args = array() ) { |
| 53 |
|
| 54 |
echo $this->get_tabbed( $metabox_id, $tabs, $table_rows, $args ); |
| 55 |
} |
| 56 |
|
| 57 |
public function get_tabbed( $metabox_id = '', $tabs = array(), $table_rows = array(), $args = array() ) { |
| 58 |
|
| 59 |
$doing_ajax = SucomUtilWP::doing_ajax(); |
| 60 |
$tab_keys = array_keys( $tabs ); |
| 61 |
$default_tab = '_' . reset( $tab_keys ); // Must start with an underscore. |
| 62 |
$mb_tabs_class = 'sucom-metabox-tabs'; |
| 63 |
$mb_tabs_id = ''; |
| 64 |
$tablink_class = 'sucom-tablink'; |
| 65 |
$tabset_class = 'sucom-tabset'; |
| 66 |
|
| 67 |
if ( ! empty( $metabox_id ) ) { |
| 68 |
|
| 69 |
$metabox_id = '_' . trim( $metabox_id, '_' ); // Must start with an underscore. |
| 70 |
$mb_tabs_id = $mb_tabs_class . $metabox_id; |
| 71 |
$mb_tabs_class .= ' ' . $mb_tabs_id; |
| 72 |
} |
| 73 |
|
| 74 |
$tabs_layout = empty( $args[ 'layout' ] ) ? WPSSO_METABOX_TAB_LAYOUT : $args[ 'layout' ]; |
| 75 |
$mb_tabs_class = SucomUtil::sanitize_css_class( $mb_tabs_class . ' ' . $tabs_layout ); |
| 76 |
$mb_tabs_id = SucomUtil::sanitize_css_id( $mb_tabs_id ); |
| 77 |
|
| 78 |
$metabox_html = "\n"; |
| 79 |
$metabox_html .= '<div class="' . $mb_tabs_class . '"' . ( $mb_tabs_id ? ' id="' . $mb_tabs_id . '"' : '' ) . '>' . "\n"; |
| 80 |
$metabox_html .= '<ul class="' . $mb_tabs_class . '">'; |
| 81 |
|
| 82 |
/* |
| 83 |
* Add the settings tab list. |
| 84 |
*/ |
| 85 |
$tab_num = 0; |
| 86 |
|
| 87 |
foreach ( $tabs as $tab => $title_transl ) { |
| 88 |
|
| 89 |
$tab_num++; |
| 90 |
|
| 91 |
$href_key_class = $tabset_class . $metabox_id . '-tab_' . $tab; |
| 92 |
$icon_key_class = $tablink_class . ' ' . $tablink_class . $metabox_id . ' ' . $tablink_class . '-icon ' . $tablink_class . '-href_' . $tab; |
| 93 |
$link_key_class = $tablink_class . ' ' . $tablink_class . $metabox_id . ' ' . $tablink_class . '-text ' . $tablink_class . '-href_' . $tab; |
| 94 |
|
| 95 |
$metabox_html .= '<li' . "\n" . 'class="tab_space' . ( $tab_num === 1 ? ' start_tabs' : '' ) . '"></li>'; |
| 96 |
$metabox_html .= '<li' . "\n" . 'class="' . $href_key_class . ( $tab_num === 1 ? ' first_tab' : '' ) . '">'; |
| 97 |
$metabox_html .= '<a class="' . $icon_key_class . '" href="#' . $href_key_class . '"></a>'; |
| 98 |
$metabox_html .= '<a class="' . $link_key_class . '" href="#' . $href_key_class . '">' . $title_transl . '</a>'; |
| 99 |
$metabox_html .= '</li>'; // Do not add a newline. |
| 100 |
} |
| 101 |
|
| 102 |
$metabox_html .= '<li' . "\n" . 'class="tab_space end_tabs"></li>'; |
| 103 |
$metabox_html .= '</ul><!-- .' . $mb_tabs_class . ' -->' . "\n\n"; |
| 104 |
|
| 105 |
/* |
| 106 |
* Add the settings table for each tab. |
| 107 |
*/ |
| 108 |
foreach ( $tabs as $tab => $title_transl ) { |
| 109 |
|
| 110 |
$href_key_class = $tabset_class . $metabox_id . '-tab_' . $tab; |
| 111 |
|
| 112 |
$metabox_html .= $this->get_table( $table_rows[ $tab ], $href_key_class, |
| 113 |
( empty( $metabox_id ) ? '' : $tabset_class . $metabox_id ), $tabset_class, $title_transl ); |
| 114 |
} |
| 115 |
|
| 116 |
$metabox_html .= '</div><!-- .' . $mb_tabs_class . ' -->' . "\n"; |
| 117 |
|
| 118 |
if ( $doing_ajax ) { |
| 119 |
|
| 120 |
$metabox_html .= '<!-- adding tabs javascript for ajax call -->' . "\n"; |
| 121 |
$metabox_html .= '<script>'; |
| 122 |
$metabox_html .= 'sucomTabs( \'' . $metabox_id . '\', \'' . $default_tab . '\' );'; |
| 123 |
$metabox_html .= '</script>' . "\n"; |
| 124 |
|
| 125 |
} else { |
| 126 |
|
| 127 |
$metabox_html .= '<!-- adding tabs javascript for page load -->' . "\n"; |
| 128 |
$metabox_html .= '<script>'; |
| 129 |
$metabox_html .= 'jQuery( document ).on( \'ready\', function(){ '; |
| 130 |
$metabox_html .= 'sucomTabs( \'' . $metabox_id . '\', \'' . $default_tab . '\' );'; |
| 131 |
$metabox_html .= '});'; |
| 132 |
$metabox_html .= '</script>' . "\n"; |
| 133 |
} |
| 134 |
|
| 135 |
return $metabox_html; |
| 136 |
} |
| 137 |
|
| 138 |
public function do_table( $table_rows, $href_key_class = '', $tabset_mb_class = '', $tabset_class = 'sucom-no_tabset', $title_transl = '' ) { |
| 139 |
|
| 140 |
echo $this->get_table( $table_rows, $href_key_class, $tabset_mb_class, $tabset_class, $title_transl ); |
| 141 |
} |
| 142 |
|
| 143 |
public function get_table( $table_rows, $href_key_class = '', $tabset_mb_class = '', $tabset_class = 'sucom-no_tabset', $title_transl = '' ) { |
| 144 |
|
| 145 |
$metabox_html = ''; |
| 146 |
|
| 147 |
if ( ! is_array( $table_rows ) ) { // Just in case. |
| 148 |
|
| 149 |
return $metabox_html; |
| 150 |
} |
| 151 |
|
| 152 |
$total_rows = count( $table_rows ); |
| 153 |
$count_rows = 0; |
| 154 |
$hidden_opts = 0; |
| 155 |
$hidden_rows = 0; |
| 156 |
$user_show_opts = WpssoUser::show_opts(); |
| 157 |
|
| 158 |
foreach ( $table_rows as $key => $row ) { |
| 159 |
|
| 160 |
if ( ! empty( $key ) ) { // Just in case. |
| 161 |
|
| 162 |
$filter_name = SucomUtil::sanitize_hookname( $href_key_class . '_' . $key . '_row' ); |
| 163 |
|
| 164 |
$row = apply_filters( $filter_name, $row ); |
| 165 |
} |
| 166 |
|
| 167 |
if ( empty( $row ) ) { // Just in case. |
| 168 |
|
| 169 |
continue; |
| 170 |
} |
| 171 |
|
| 172 |
/* |
| 173 |
* Default row class and id attribute values. |
| 174 |
*/ |
| 175 |
$tr = array( |
| 176 |
'class' => 'sucom-alt' . |
| 177 |
( $count_rows % 2 ) . |
| 178 |
( $count_rows === 0 ? ' first-row' : '' ) . |
| 179 |
( $count_rows === ( $total_rows - 1 ) ? ' last-row' : '' ), |
| 180 |
'id' => ( is_int( $key ) ? '' : 'tr_' . $key ) |
| 181 |
); |
| 182 |
|
| 183 |
/* |
| 184 |
* If we don't already have a table row tag, then add one. |
| 185 |
*/ |
| 186 |
if ( strpos( $row, '<tr ' ) === false ) { |
| 187 |
|
| 188 |
$row = '<tr class="' . $tr[ 'class' ] . '"' . ( empty( $tr[ 'id' ] ) ? '' : ' id="' . $tr[ 'id' ] . '"' ) . '>' . $row; |
| 189 |
|
| 190 |
} else { |
| 191 |
|
| 192 |
foreach ( $tr as $att => $val ) { |
| 193 |
|
| 194 |
if ( empty( $tr[ $att ] ) ) { |
| 195 |
|
| 196 |
continue; |
| 197 |
} |
| 198 |
|
| 199 |
/* |
| 200 |
* If we're here, then we have a table row tag already. |
| 201 |
* |
| 202 |
* Count the number of rows and options that are hidden. |
| 203 |
*/ |
| 204 |
if ( $att === 'class' && ! empty( $user_show_opts ) ) { |
| 205 |
|
| 206 |
if ( $matched = preg_match( '/<tr [^>]*class="[^"]*hide(_row)?_in_' . $user_show_opts . '[" ]/', $row, $m ) > 0 ) { |
| 207 |
|
| 208 |
if ( ! isset( $m[ 1 ] ) ) { |
| 209 |
|
| 210 |
$hidden_opts += preg_match_all( '/(<th|<tr[^>]*><td)/', $row, $all_matches ); |
| 211 |
} |
| 212 |
|
| 213 |
$hidden_rows += $matched; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
/* |
| 218 |
* Add the attribute value. |
| 219 |
*/ |
| 220 |
$replace_count = null; |
| 221 |
|
| 222 |
$row = preg_replace( '/(<tr [^>]*' . $att . '=")([^"]*)(")/', '$1$2 ' . $tr[ $att ] . '$3', $row, -1, $replace_count ); |
| 223 |
|
| 224 |
/* |
| 225 |
* If one hasn't been added, then add both the attribute and its value. |
| 226 |
*/ |
| 227 |
if ( $replace_count < 1 ) { |
| 228 |
|
| 229 |
$row = preg_replace( '/(<tr )/', '$1' . $att . '="' . $tr[ $att ] . '" ', $row, -1, $replace_count ); |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
/* |
| 235 |
* Add a closing table row tag if we don't already have one. |
| 236 |
*/ |
| 237 |
if ( strpos( $row, '</tr>' ) === false ) { |
| 238 |
|
| 239 |
$row .= '</tr>' . "\n"; |
| 240 |
} |
| 241 |
|
| 242 |
/* |
| 243 |
* Update the table row array element with the new value. |
| 244 |
*/ |
| 245 |
$table_rows[ $key ] = $row; |
| 246 |
|
| 247 |
$count_rows++; |
| 248 |
} |
| 249 |
|
| 250 |
if ( 0 === $count_rows ) { |
| 251 |
|
| 252 |
$table_rows[ 'no_options' ] = '<tr><td align="center">' . |
| 253 |
'<p class="status-msg">' . __( 'No options available.', 'wpsso' ) . '</p>' . |
| 254 |
'</td></tr>'; |
| 255 |
|
| 256 |
$count_rows++; |
| 257 |
} |
| 258 |
|
| 259 |
$div_class = ( empty( $user_show_opts ) ? '' : 'sucom-show_' . $user_show_opts ) . |
| 260 |
( empty( $tabset_class ) ? '' : ' ' . $tabset_class ) . |
| 261 |
( empty( $tabset_mb_class ) ? '' : ' ' . $tabset_mb_class ) . |
| 262 |
( empty( $href_key_class ) ? '' : ' ' . $href_key_class ); |
| 263 |
|
| 264 |
$table_class = 'sucom-settings ' . $this->p->id . |
| 265 |
( empty( $href_key_class ) ? '' : ' ' . $href_key_class ) . |
| 266 |
( $hidden_rows > 0 && $hidden_rows === $count_rows ? ' hide_in_' . $user_show_opts : '' ); |
| 267 |
|
| 268 |
$metabox_html .= '<div class="' . $div_class . '">' . "\n"; |
| 269 |
$metabox_html .= $title_transl ? '<h3 class="sucom-metabox-tab_title">' . $title_transl . '</h3>' . "\n" : ''; |
| 270 |
$metabox_html .= '<table class="' . $table_class . '">' . "\n"; |
| 271 |
|
| 272 |
foreach ( $table_rows as $row ) { |
| 273 |
|
| 274 |
$metabox_html .= $row; |
| 275 |
} |
| 276 |
|
| 277 |
$metabox_html .= '</table><!-- .' . $table_class . ' --> ' . "\n"; |
| 278 |
$metabox_html .= '</div><!-- .' . $div_class . ' -->' . "\n\n"; |
| 279 |
|
| 280 |
$user_show_opts_label = $this->p->cf[ 'form' ][ 'show_options' ][ $user_show_opts ]; |
| 281 |
|
| 282 |
if ( $hidden_opts > 0 ) { |
| 283 |
|
| 284 |
$metabox_html .= '<div class="hidden_opts_msg ' . $tabset_class . '-msg ' . $tabset_mb_class . '-msg ' . $href_key_class . '-msg">' . |
| 285 |
sprintf( _x( '%1$d additional options not shown in "%2$s" view', 'option comment', 'wpsso' ), |
| 286 |
$hidden_opts, _x( $user_show_opts_label, 'option value', 'wpsso' ) ) . |
| 287 |
' (<a href="javascript:void(0);" onClick="sucomViewUnhideRows( \'' . $href_key_class . '\', \'' . $user_show_opts . |
| 288 |
'\' );">' . _x( 'show these options now', 'option comment', 'wpsso' ) . '</a>)</div>' . "\n"; |
| 289 |
|
| 290 |
} elseif ( $hidden_rows > 0 ) { |
| 291 |
|
| 292 |
$metabox_html .= '<div class="hidden_opts_msg ' . $tabset_class . '-msg ' . $tabset_mb_class . '-msg ' . $href_key_class . '-msg">' . |
| 293 |
sprintf( _x( '%1$d additional rows not shown in "%2$s" view', 'option comment', 'wpsso' ), |
| 294 |
$hidden_rows, _x( $user_show_opts_label, 'option value', 'wpsso' ) ) . |
| 295 |
' (<a href="javascript:void(0);" onClick="sucomViewUnhideRows( \'' . $href_key_class . '\', \'' . $user_show_opts . |
| 296 |
'\', \'hide_row_in\' );">' . _x( 'show these rows now', 'option comment', 'wpsso' ) . '</a>)</div>' . "\n"; |
| 297 |
} |
| 298 |
|
| 299 |
return $metabox_html; |
| 300 |
} |
| 301 |
} |
| 302 |
} |
| 303 |
|