| @@ -1,71 +1,139 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - if ( ! defined('WPINC') ) { | |
| 4 | - wp_die(); | |
| 3 | +if (!defined('ABSPATH')) { | |
| 4 | + exit; | |
| 5 | +} | |
| 6 | + | |
| 7 | +global $submenu, $parent_file, $submenu_file, $plugin_page, $pagenow; | |
| 8 | + | |
| 9 | +$parent_slug = 'edit.php?post_type=' . FLRT_FILTERS_SET_POST_TYPE; | |
| 10 | + | |
| 11 | + | |
| 12 | +$current_tab = isset($_GET['tab']) ? sanitize_key($_GET['tab']) : ''; | |
| 13 | +$current_page = isset($_GET['page']) ? sanitize_key($_GET['page']) : ''; | |
| 14 | +$tabs = array(); | |
| 15 | +$this_page_has_import_export_tab = false; | |
| 16 | +if (isset($submenu[$parent_slug])) { | |
| 17 | + foreach ($submenu[$parent_slug] as $sub_item) { | |
| 18 | + if (isset($sub_item[2]) && strpos($sub_item[2], 'tab=import_export') !== false) { | |
| 19 | + $this_page_has_import_export_tab = true; | |
| 20 | + } | |
| 5 | 21 | } |
| 22 | +} | |
| 6 | 23 | |
| 7 | - global $submenu, $parent_file, $submenu_file, $plugin_page, $pagenow; | |
| 24 | +// Submenu entries that must NOT become toolbar tabs (the toolbar mirrors the | |
| 25 | +// sidebar). Empty by default: since 1.9.7 «What's new» is a Settings tab, not | |
| 26 | +// a submenu entry, and Import/Export is back in both. | |
| 27 | +$hidden_in_toolbar = apply_filters('wpc_header_nav_hidden_slugs', array()); | |
| 8 | 28 | |
| 9 | - // Vars. | |
| 10 | - $parent_slug = 'edit.php?post_type=' . FLRT_FILTERS_SET_POST_TYPE; | |
| 29 | +if (isset($submenu[$parent_slug])) { | |
| 30 | + foreach ($submenu[$parent_slug] as $i => $sub_item) { | |
| 11 | 31 | |
| 12 | - // Generate array of navigation items. | |
| 13 | - $tabs = array(); | |
| 14 | - if( isset($submenu[ $parent_slug ]) ) { | |
| 15 | - foreach( $submenu[ $parent_slug ] as $i => $sub_item ) { | |
| 32 | + if (!current_user_can($sub_item[1])) { | |
| 33 | + continue; | |
| 34 | + } | |
| 16 | 35 | |
| 17 | - // Check user can access page. | |
| 18 | - if ( !current_user_can( $sub_item[1] ) ) { | |
| 19 | - continue; | |
| 20 | - } | |
| 36 | + if ($i === 1) { | |
| 37 | + continue; | |
| 38 | + } | |
| 21 | 39 | |
| 22 | - // Ignore "Add New". | |
| 23 | - if( $i === 1 ) { | |
| 24 | - continue; | |
| 40 | + if (in_array($sub_item[2], $hidden_in_toolbar, true)) { | |
| 41 | + continue; | |
| 42 | + } | |
| 43 | + | |
| 44 | + $tab = array( | |
| 45 | + // The sidebar's unread badge (WhatsNew) belongs to the sidebar; the | |
| 46 | + // toolbar shows the plain label | |
| 47 | + 'text' => preg_replace('#\s*<span class="update-plugins[^"]*">.*?</span></span>#s', '', $sub_item[0]), | |
| 48 | + 'url' => $sub_item[2] | |
| 49 | + ); | |
| 50 | + | |
| 51 | + if (!strpos($sub_item[2], '.php')) { | |
| 52 | + $tab['url'] = add_query_arg(array('page' => $sub_item[2]), $parent_slug); | |
| 53 | + } | |
| 54 | + | |
| 55 | + $tab_tab = ''; | |
| 56 | + $tab_url_parts = wp_parse_url($tab['url']); | |
| 57 | + if (isset($tab_url_parts['query'])) { | |
| 58 | + parse_str($tab_url_parts['query'], $tab_qs); | |
| 59 | + if (isset($tab_qs['tab'])) { | |
| 60 | + $tab_tab = sanitize_key($tab_qs['tab']); | |
| 25 | 61 | } |
| 62 | + } | |
| 26 | 63 | |
| 27 | - // Define tab. | |
| 28 | - $tab = array( | |
| 29 | - 'text' => $sub_item[0], | |
| 30 | - 'url' => $sub_item[2] | |
| 31 | - ); | |
| 64 | + $tab['is_active'] = false; | |
| 32 | 65 | |
| 33 | - // Convert submenu slug "test" to "$parent_slug&page=test". | |
| 34 | - if( !strpos($sub_item[2], '.php') ) { | |
| 35 | - $tab['url'] = add_query_arg( array( 'page' => $sub_item[2] ), $parent_slug ); | |
| 36 | - } | |
| 66 | + $tab_matches = false; | |
| 67 | + if (strpos($sub_item[2], '.php') === false) { | |
| 68 | + $current_page = isset($_GET['page']) ? sanitize_key($_GET['page']) : ''; | |
| 69 | + $tab_matches = ($current_page === sanitize_key($sub_item[2])); | |
| 70 | + } else { | |
| 71 | + $tab_matches = ($submenu_file === $sub_item[2] || $plugin_page === $sub_item[2]); | |
| 72 | + } | |
| 73 | + $is_same_submenu = $tab_matches; | |
| 37 | 74 | |
| 38 | - // Detect active state. | |
| 39 | - if( $submenu_file === $sub_item[2] || $plugin_page === $sub_item[2] ) { | |
| 40 | - $tab['is_active'] = true; | |
| 75 | + if ($is_same_submenu) { | |
| 76 | + // Settings tabs: Import/Export has a toolbar tab of its own (PRO deep | |
| 77 | + // link), every other tab keeps «Settings» active | |
| 78 | + if($current_page == 'filters-settings'){ | |
| 79 | + if ($tab_tab) { | |
| 80 | + $tab['is_active'] = ($tab_tab === $current_tab); | |
| 81 | + } else { | |
| 82 | + $tab['is_active'] = ($current_tab !== 'import_export') || ! $this_page_has_import_export_tab; | |
| 83 | + } | |
| 84 | + }elseif ($current_tab) { | |
| 85 | + if ($tab_tab && $tab_tab === $current_tab) { | |
| 86 | + $tab['is_active'] = true; | |
| 87 | + } | |
| 88 | + } else { | |
| 89 | + if (empty($tab_tab)) { | |
| 90 | + $tab['is_active'] = true; | |
| 91 | + } | |
| 41 | 92 | } |
| 93 | + } | |
| 42 | 94 | |
| 43 | - // Special case for "Add New" page. | |
| 44 | - if( $i === 0 && $submenu_file === 'post-new.php?post_type=' . FLRT_FILTERS_SET_POST_TYPE ) { | |
| 45 | - $tab['is_active'] = true; | |
| 46 | - } | |
| 47 | - $tabs[] = $tab; | |
| 95 | + if ($i === 0 && $submenu_file === 'post-new.php?post_type=' . FLRT_FILTERS_SET_POST_TYPE) { | |
| 96 | + $tab['is_active'] = true; | |
| 48 | 97 | } |
| 98 | + $tabs[] = $tab; | |
| 49 | 99 | } |
| 100 | +} | |
| 50 | 101 | |
| 51 | - $tabs = apply_filters( 'wpc_header_nav_tabs', $tabs ); | |
| 102 | +$tabs = apply_filters('wpc_header_nav_tabs', $tabs); | |
| 52 | 103 | |
| 53 | - // Bail early if set to false. | |
| 54 | - if( $tabs === false ) { | |
| 55 | - return; | |
| 56 | - } | |
| 104 | +if ($tabs === false) { | |
| 105 | + return; | |
| 106 | +} | |
| 57 | 107 | ?> |
| 58 | 108 | <div class="wpc-admin-toolbar"> |
| 59 | - <h2><img src="<?php echo esc_attr( flrt_get_icon_svg('#333333') ); ?>" alt="" width="20" /> <?php echo esc_html( flrt_get_plugin_name() ); ?></h2> | |
| 60 | - <?php foreach( $tabs as $tab ) { | |
| 61 | - $is_active = !empty( $tab['is_active'] ) ? ' is-active' : ''; | |
| 62 | - | |
| 109 | + <h2><a class="wpc-toolbar-brand" href="<?php echo esc_url( admin_url( 'edit.php?post_type=' . FLRT_FILTERS_SET_POST_TYPE ) ); ?>"><img src="<?php | |
| 110 | + echo esc_attr(flrt_get_icon_svg('#333333')); | |
| 111 | + ?>" alt="" width="24"/> <?php | |
| 112 | + echo esc_html(flrt_get_plugin_name()); | |
| 113 | + // The running version, always visible on every plugin screen in both builds | |
| 114 | + // (PRO used to print it on the right next to the licence status); the | |
| 115 | + // logo + name link back to the Filter Sets list | |
| 116 | + echo ' <span class="wpc-plugin-version">' . esc_html( FLRT_PLUGIN_VER ) . '</span>'; | |
| 117 | + ?></a></h2> | |
| 118 | + <?php foreach ($tabs as $tab) { | |
| 119 | + $is_active = !empty($tab['is_active']) ? ' is-active' : ''; | |
| 120 | + $is_pro = str_contains($tab['text'], 'wpc-pro-badge') ? ' wpc-pro-badge-text' : ''; | |
| 121 | + $tab_text = str_replace('wpc-pro-badge', 'wpc-pro-badge-transparent', $tab['text']); | |
| 122 | + $extra_class = preg_replace( '/[^a-z]/', '', strtolower( wp_kses($tab_text, [] ) ) ); | |
| 63 | 123 | printf( |
| 64 | - '<a class="wpc-tab%s" href="%s">%s</a>', $is_active, esc_url( $tab['url'] ), esc_html( $tab['text'] ) | |
| 124 | + '<a class="wpc-tab%s%s wpc-%s" href="%s">%s</a>', | |
| 125 | + $is_active, | |
| 126 | + $is_pro, | |
| 127 | + $extra_class, | |
| 128 | + esc_url($tab['url']), | |
| 129 | + wp_kses($tab_text, array( | |
| 130 | + 'span' => array( | |
| 131 | + 'class' => array(), | |
| 132 | + ), | |
| 133 | + )) | |
| 65 | 134 | ); |
| 66 | 135 | } ?> |
| 67 | - | |
| 68 | 136 | <div class="wpc-admin-right"> |
| 69 | 137 | <?php do_action('wpc_admin_toolbar_right'); ?> |
| 70 | 138 | </div> |
| 71 | 139 | </div> |