| 1 |
<?php |
| 2 |
|
| 3 |
namespace FloatingButton\Dashboard; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
use FloatingButton\WOW_Plugin; |
| 8 |
|
| 9 |
class DashboardInitializer { |
| 10 |
|
| 11 |
public static function init(): void { |
| 12 |
self::header(); |
| 13 |
echo '<div class="wrap wowp-wrap">'; |
| 14 |
self::menu(); |
| 15 |
self::include_pages(); |
| 16 |
echo '</div>'; |
| 17 |
} |
| 18 |
|
| 19 |
public static function header(): void { |
| 20 |
$logo_url = self::logo_url(); |
| 21 |
$add_url = add_query_arg( [ |
| 22 |
'page' => WOW_Plugin::SLUG, |
| 23 |
'tab' => 'settings', |
| 24 |
'action' => 'new' |
| 25 |
], admin_url( 'admin.php' ) ); |
| 26 |
?> |
| 27 |
<div class="wowp-header-wrapper"> |
| 28 |
<div class="wowp-header-border"></div> |
| 29 |
<div class="wowp-header"> |
| 30 |
<div class="wowp-logo"> |
| 31 |
<img src="<?php echo esc_url( $logo_url ); ?>" |
| 32 |
alt="<?php echo esc_attr( WOW_Plugin::info('name') ); ?> logo"> |
| 33 |
</div> |
| 34 |
<h1><?php echo esc_html( WOW_Plugin::info('name') ); ?> <sup |
| 35 |
class="wowp-version"><?php echo esc_html( WOW_Plugin::info('version') ); ?></sup></h1> |
| 36 |
<a href="<?php echo esc_url( $add_url ); ?>" |
| 37 |
class="button"><?php esc_html_e( 'Add New', 'floating-button' ); ?> |
| 38 |
</a> |
| 39 |
<?php do_action( WOW_Plugin::PREFIX . '_admin_header_links' ); ?> |
| 40 |
</div> |
| 41 |
</div> |
| 42 |
|
| 43 |
<?php |
| 44 |
} |
| 45 |
|
| 46 |
public static function logo_url(): string { |
| 47 |
$logo_url = WOW_Plugin::url() . 'assets/img/plugin-logo.png'; |
| 48 |
if ( filter_var( $logo_url, FILTER_VALIDATE_URL ) !== false ) { |
| 49 |
return $logo_url; |
| 50 |
} |
| 51 |
|
| 52 |
return ''; |
| 53 |
} |
| 54 |
|
| 55 |
public static function menu(): void { |
| 56 |
|
| 57 |
$pages = DashboardHelper::get_files( 'pages' ); |
| 58 |
|
| 59 |
$current_page = self::get_current_page(); |
| 60 |
|
| 61 |
$action = ( isset( $_REQUEST["action"] ) ) ? sanitize_text_field( $_REQUEST["action"] ) : ''; |
| 62 |
|
| 63 |
echo '<h2 class="nav-tab-wrapper wowp-nav-tab-wrapper">'; |
| 64 |
foreach ( $pages as $key => $page ) { |
| 65 |
$class = ( $page['file'] === $current_page ) ? ' nav-tab-active' : ''; |
| 66 |
$id = ''; |
| 67 |
|
| 68 |
if ( $action === 'update' && $page['file'] === 'settings' ) { |
| 69 |
$id = ( isset( $_REQUEST["id"] ) ) ? absint( $_REQUEST["id"] ) : ''; |
| 70 |
$page['name'] = __( 'Update', 'floating-button' ) . ' #' . $id; |
| 71 |
} elseif ( $page['file'] === 'settings' && ( $action !== 'new' && $action !== 'duplicate' ) ) { |
| 72 |
continue; |
| 73 |
} |
| 74 |
|
| 75 |
echo '<a class="nav-tab' . esc_attr( $class ) . '" href="' . esc_url( Link::menu( $page['file'], $action, $id ) ) . '">' . esc_html( $page['name'] ) . '</a>'; |
| 76 |
} |
| 77 |
echo '</h2>'; |
| 78 |
|
| 79 |
|
| 80 |
} |
| 81 |
|
| 82 |
public static function include_pages(): void { |
| 83 |
$current_page = self::get_current_page(); |
| 84 |
|
| 85 |
$pages = DashboardHelper::get_files( 'pages' ); |
| 86 |
$default = DashboardHelper::first_file( 'pages' ); |
| 87 |
|
| 88 |
$current = DashboardHelper::search_value( $pages, $current_page ) ? $current_page : $default; |
| 89 |
|
| 90 |
$file = DashboardHelper::get_file( $current, 'pages' ); |
| 91 |
|
| 92 |
|
| 93 |
if ( $file !== false ) { |
| 94 |
$file = apply_filters( WOW_Plugin::PREFIX . '_admin_filter_file', $file, $current ); |
| 95 |
|
| 96 |
$page_path = DashboardHelper::get_folder_path( 'pages' ) . '/' . $file; |
| 97 |
|
| 98 |
if ( file_exists( $page_path ) ) { |
| 99 |
require_once $page_path; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
public static function get_current_page(): string { |
| 107 |
$default = DashboardHelper::first_file( 'pages' ); |
| 108 |
|
| 109 |
return ( isset( $_REQUEST["tab"] ) ) ? sanitize_text_field( $_REQUEST["tab"] ) : $default; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
} |