| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class Dashboard |
| 5 |
* |
| 6 |
* @package WowPlugin |
| 7 |
* @subpackage Admin |
| 8 |
* @author Dmytro Lobov <dev@wow-company.com>, Wow-Company |
| 9 |
* @copyright 2024 Dmytro Lobov |
| 10 |
* @license GPL-2.0+ |
| 11 |
* |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace FloatMenuLite\Admin; |
| 15 |
|
| 16 |
use FloatMenuLite\WOWP_Plugin; |
| 17 |
|
| 18 |
class Dashboard { |
| 19 |
|
| 20 |
public static function init(): void { |
| 21 |
add_filter( 'plugin_action_links', [ __CLASS__, 'settings_link' ], 10, 2 ); |
| 22 |
add_filter( 'admin_footer_text', [ __CLASS__, 'footer_text' ] ); |
| 23 |
add_action( 'admin_enqueue_scripts', [ __CLASS__, 'admin_assets' ] ); |
| 24 |
add_action( 'admin_menu', [ __CLASS__, 'admin_page' ] ); |
| 25 |
AdminNotices::init(); |
| 26 |
} |
| 27 |
|
| 28 |
public static function settings_link( $links, $file ) { |
| 29 |
if ( false === strpos( $file, WOWP_Plugin::basename() ) ) { |
| 30 |
return $links; |
| 31 |
} |
| 32 |
$link = admin_url( 'admin.php?page=' . WOWP_Plugin::SLUG ); |
| 33 |
$text = esc_attr__( 'Settings', 'float-menu' ); |
| 34 |
$settings_link = '<a href="' . esc_url( $link ) . '">' . esc_attr( $text ) . '</a>'; |
| 35 |
array_unshift( $links, $settings_link ); |
| 36 |
|
| 37 |
return $links; |
| 38 |
} |
| 39 |
|
| 40 |
public static function footer_text( $footer_text ) { |
| 41 |
global $pagenow; |
| 42 |
|
| 43 |
if ( $pagenow === 'admin.php' && ( isset( $_GET['page'] ) && $_GET['page'] === WOWP_Plugin::SLUG ) ) { |
| 44 |
$text = sprintf( |
| 45 |
/* translators: 1: Rating link (URL), 2: Plugin name */ |
| 46 |
__( 'Thank you for using <b>%2$s</b>! Please <a href="%1$s" target="_blank">rate us</a>', 'float-menu' ), |
| 47 |
esc_url( WOWP_Plugin::info( 'rating' ) ), |
| 48 |
esc_attr( WOWP_Plugin::info( 'name' ) ) |
| 49 |
); |
| 50 |
|
| 51 |
return str_replace( '</span>', '', $footer_text ) . ' | ' . $text . '</span>'; |
| 52 |
} |
| 53 |
|
| 54 |
return $footer_text; |
| 55 |
} |
| 56 |
|
| 57 |
public static function admin_assets( $hook ): void { |
| 58 |
|
| 59 |
$page = 'wow-plugins_page_' . WOWP_Plugin::SLUG; |
| 60 |
if ( $page !== $hook ) { |
| 61 |
return; |
| 62 |
} |
| 63 |
do_action( WOWP_Plugin::PREFIX . '_admin_load_assets' ); |
| 64 |
|
| 65 |
$slug = WOWP_Plugin::SLUG; |
| 66 |
$version = WOWP_Plugin::info( 'version' ); |
| 67 |
$assets_url = WOWP_Plugin::url() . 'admin/assets/'; |
| 68 |
|
| 69 |
$styles = DashboardHelper::get_files( 'assets/css' ); |
| 70 |
|
| 71 |
if ( ! empty( $styles ) ) { |
| 72 |
foreach ( $styles as $key => $style ) { |
| 73 |
$name = $style['file']; |
| 74 |
$file = $key . '.' . $name; |
| 75 |
wp_enqueue_style( $slug . '-admin-' . $name, $assets_url . 'css/' . $file . '.css', null, $version ); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
$scripts = DashboardHelper::get_files( 'assets/js' ); |
| 80 |
if ( ! empty( $scripts ) ) { |
| 81 |
foreach ( $scripts as $key => $script ) { |
| 82 |
$name = $script['file']; |
| 83 |
$file = $key . '.' . $name; |
| 84 |
wp_enqueue_script( $slug . '-admin-' . $name, $assets_url . 'js/' . $file . '.js', [ 'jquery' ], $version, true ); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
} |
| 90 |
|
| 91 |
public static function admin_page(): void { |
| 92 |
$page_title = WOWP_Plugin::info( 'name' ); |
| 93 |
$menu_title = WOWP_Plugin::info( 'menu_title' ); |
| 94 |
$capability = 'manage_options'; |
| 95 |
$parent_slug = 'wow-company'; |
| 96 |
add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, WOWP_Plugin::SLUG, [ |
| 97 |
__CLASS__, |
| 98 |
'dashboard' |
| 99 |
] ); |
| 100 |
} |
| 101 |
|
| 102 |
public static function dashboard(): void { |
| 103 |
self::header(); |
| 104 |
echo '<div class="wrap wpie-wrap">'; |
| 105 |
self::menu(); |
| 106 |
self::include_pages(); |
| 107 |
echo '</div>'; |
| 108 |
} |
| 109 |
|
| 110 |
public static function header(): void { |
| 111 |
$logo_url = self::logo_url(); |
| 112 |
?> |
| 113 |
<div class="wpie-header-wrapper"> |
| 114 |
<div class="wpie-header-border"></div> |
| 115 |
<div class="wpie-header"> |
| 116 |
<div class="wpie-header__container"> |
| 117 |
<?php if ( ! empty( $logo_url ) ): ?> |
| 118 |
<div class="wpie-logo"> |
| 119 |
<img src="<?php echo esc_url( $logo_url ); ?>" |
| 120 |
alt="<?php echo esc_attr( WOWP_Plugin::info( 'name' ) ); ?> logo"> |
| 121 |
</div> |
| 122 |
<?php endif; ?> |
| 123 |
<h1><?php echo esc_html( WOWP_Plugin::info( 'name' ) ); ?> <sup |
| 124 |
class="wpie-version"><?php echo esc_html( WOWP_Plugin::info( 'version' ) ); ?></sup> |
| 125 |
</h1> |
| 126 |
<a href="<?php echo esc_url( Link::add_new_item() ); ?>" |
| 127 |
class="button button-primary"><?php esc_html_e( 'Add New', 'float-menu' ); ?> |
| 128 |
</a> |
| 129 |
<?php do_action( WOWP_Plugin::PREFIX . '_admin_header_links' ); ?> |
| 130 |
</div> |
| 131 |
</div> |
| 132 |
</div> |
| 133 |
<?php |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
public static function logo_url(): string { |
| 139 |
$logo_url = WOWP_Plugin::url() . 'admin/assets/img/plugin-logo.png'; |
| 140 |
if ( filter_var( $logo_url, FILTER_VALIDATE_URL ) !== false ) { |
| 141 |
return $logo_url; |
| 142 |
} |
| 143 |
|
| 144 |
return ''; |
| 145 |
} |
| 146 |
|
| 147 |
public static function menu(): void { |
| 148 |
$pages = DashboardHelper::get_files( 'pages' ); |
| 149 |
|
| 150 |
$current_page = self::get_current_page(); |
| 151 |
|
| 152 |
$action = ( isset( $_REQUEST["action"] ) ) ? sanitize_text_field( wp_unslash( $_REQUEST["action"] ) ) : ''; |
| 153 |
|
| 154 |
echo '<h2 class="nav-tab-wrapper wpie-nav-tab-wrapper">'; |
| 155 |
foreach ( $pages as $key => $page ) { |
| 156 |
$class = ( $page['file'] === $current_page ) ? ' nav-tab-active' : ''; |
| 157 |
$id = ''; |
| 158 |
|
| 159 |
if ( $action === 'update' && $page['file'] === 'settings' ) { |
| 160 |
$id = ( isset( $_REQUEST["id"] ) ) ? absint( $_REQUEST["id"] ) : ''; |
| 161 |
$page['name'] = __( 'Update', 'float-menu' ) . ' #' . $id; |
| 162 |
} elseif ( $page['file'] === 'settings' && ( $action !== 'new' && $action !== 'duplicate' ) ) { |
| 163 |
continue; |
| 164 |
} |
| 165 |
|
| 166 |
echo '<a class="nav-tab' . esc_attr( $class ) . '" href="' . esc_url( Link::menu( $page['file'], $action, $id ) ) . '">' . esc_html( $page['name'] ) . '</a>'; |
| 167 |
} |
| 168 |
echo '</h2>'; |
| 169 |
} |
| 170 |
|
| 171 |
public static function get_current_page(): string { |
| 172 |
$default = DashboardHelper::first_file( 'pages' ); |
| 173 |
|
| 174 |
return ( isset( $_REQUEST["tab"] ) ) ? sanitize_text_field( wp_unslash( $_REQUEST["tab"] ) ) : $default; |
| 175 |
} |
| 176 |
|
| 177 |
public static function include_pages(): void { |
| 178 |
$current_page = self::get_current_page(); |
| 179 |
|
| 180 |
$pages = DashboardHelper::get_files( 'pages' ); |
| 181 |
$default = DashboardHelper::first_file( 'pages' ); |
| 182 |
|
| 183 |
$current = DashboardHelper::search_value( $pages, $current_page ) ? $current_page : $default; |
| 184 |
|
| 185 |
$file = DashboardHelper::get_file( $current, 'pages' ); |
| 186 |
|
| 187 |
|
| 188 |
if ( $file !== false ) { |
| 189 |
$file = apply_filters( WOWP_Plugin::PREFIX . '_admin_filter_file', $file, $current ); |
| 190 |
|
| 191 |
$page_path = DashboardHelper::get_folder_path( 'pages' ) . '/' . $file; |
| 192 |
|
| 193 |
if ( file_exists( $page_path ) ) { |
| 194 |
require_once $page_path; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
} |
| 199 |
|
| 200 |
} |