| 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 FloatingButton\Admin; |
| 15 |
|
| 16 |
use FloatingButton\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', 'floating-button' ); |
| 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 |
// No nonce verification is required as this is a read-only operation to check the current admin page. |
| 44 |
if ( $pagenow === 'admin.php' && ( isset( $_GET['page'] ) && $_GET['page'] === WOWP_Plugin::SLUG ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 45 |
$text = sprintf( |
| 46 |
/* translators: 1: Rating link (URL), 2: Plugin name */ |
| 47 |
__( 'Thank you for using <b>%2$s</b>! Please <a href="%1$s" target="_blank">rate us</a>', 'floating-button' ), |
| 48 |
esc_url( WOWP_Plugin::info( 'rating' ) ), |
| 49 |
esc_attr( WOWP_Plugin::info( 'name' ) ) |
| 50 |
); |
| 51 |
|
| 52 |
return str_replace( '</span>', '', $footer_text ) . ' | ' . $text . '</span>'; |
| 53 |
} |
| 54 |
|
| 55 |
return $footer_text; |
| 56 |
} |
| 57 |
|
| 58 |
public static function admin_assets( $hook ): void { |
| 59 |
|
| 60 |
$page = 'wow-plugins_page_' . WOWP_Plugin::SLUG; |
| 61 |
if ( $page !== $hook ) { |
| 62 |
return; |
| 63 |
} |
| 64 |
do_action( WOWP_Plugin::PREFIX . '_admin_load_assets' ); |
| 65 |
|
| 66 |
$slug = WOWP_Plugin::SLUG; |
| 67 |
$version = WOWP_Plugin::info( 'version' ); |
| 68 |
$assets_url = WOWP_Plugin::url() . 'admin/assets/'; |
| 69 |
|
| 70 |
$styles = DashboardHelper::get_files( 'assets/css' ); |
| 71 |
|
| 72 |
if ( ! empty( $styles ) ) { |
| 73 |
foreach ( $styles as $key => $style ) { |
| 74 |
$name = $style['file']; |
| 75 |
$file = $key . '.' . $name; |
| 76 |
wp_enqueue_style( $slug . '-admin-' . $name, $assets_url . 'css/' . $file . '.css', null, $version ); |
| 77 |
wp_style_add_data($slug . '-admin-' . $name, 'rtl', 'replace'); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
$scripts = DashboardHelper::get_files( 'assets/js' ); |
| 82 |
if ( ! empty( $scripts ) ) { |
| 83 |
foreach ( $scripts as $key => $script ) { |
| 84 |
$name = $script['file']; |
| 85 |
$file = $key . '.' . $name; |
| 86 |
wp_enqueue_script( $slug . '-admin-' . $name, $assets_url . 'js/' . $file . '.js', [ 'jquery' ], $version, true ); |
| 87 |
if ( $name === 'general' ) { |
| 88 |
wp_localize_script( $slug . '-admin-' . $name, 'wowp_ajax_object', array( |
| 89 |
'url' => admin_url( 'admin-ajax.php' ), |
| 90 |
'security' => wp_create_nonce( WOWP_Plugin::PREFIX . '_settings' ), |
| 91 |
'action' => WOWP_Plugin::PREFIX . '_ajax_settings', |
| 92 |
'prefix' => WOWP_Plugin::PREFIX, |
| 93 |
) ); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
public static function admin_page(): void { |
| 102 |
$page_title = WOWP_Plugin::info( 'name' ); |
| 103 |
$menu_title = WOWP_Plugin::info( 'menu_title' ); |
| 104 |
$capability = 'manage_options'; |
| 105 |
$parent_slug = 'wow-company'; |
| 106 |
add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, WOWP_Plugin::SLUG, [ |
| 107 |
__CLASS__, |
| 108 |
'dashboard' |
| 109 |
] ); |
| 110 |
} |
| 111 |
|
| 112 |
public static function dashboard(): void { |
| 113 |
self::header(); |
| 114 |
echo '<div class="wrap wpie-wrap">'; |
| 115 |
self::menu(); |
| 116 |
self::include_pages(); |
| 117 |
echo '</div>'; |
| 118 |
} |
| 119 |
|
| 120 |
// phpcs:disable PluginCheck.CodeAnalysis.ImageFunctions.NonEnqueuedImage |
| 121 |
public static function header(): void { |
| 122 |
$logo_url = self::logo_url(); |
| 123 |
?> |
| 124 |
<div class="wpie-header-wrapper"> |
| 125 |
<div class="wpie-header-border"></div> |
| 126 |
<div class="wpie-header"> |
| 127 |
<div class="wpie-header__container"> |
| 128 |
<?php if ( ! empty( $logo_url ) ): ?> |
| 129 |
<div class="wpie-logo"> |
| 130 |
<img src="<?php echo esc_url( $logo_url ); ?>" |
| 131 |
alt="<?php echo esc_attr( WOWP_Plugin::info( 'name' ) ); ?> logo"> |
| 132 |
</div> |
| 133 |
<?php endif; ?> |
| 134 |
<h1> |
| 135 |
<?php echo esc_html( WOWP_Plugin::info( 'name' ) ); ?> |
| 136 |
<sup class="wpie-version"><?php echo esc_html( WOWP_Plugin::info( 'version' ) ); ?></sup> |
| 137 |
</h1> |
| 138 |
<a href="<?php echo esc_url( Link::add_new_item() ); ?>" class="button button-primary"> |
| 139 |
<?php esc_html_e( 'Add New', 'floating-button' ); ?> |
| 140 |
</a> |
| 141 |
<?php do_action( WOWP_Plugin::PREFIX . '_admin_after_button' ); ?> |
| 142 |
<?php do_action( WOWP_Plugin::PREFIX . '_admin_header_links' ); ?> |
| 143 |
</div> |
| 144 |
</div> |
| 145 |
</div> |
| 146 |
<?php |
| 147 |
} |
| 148 |
|
| 149 |
// phpcs:enable |
| 150 |
|
| 151 |
|
| 152 |
public static function logo_url(): string { |
| 153 |
$logo_url = WOWP_Plugin::url() . 'admin/assets/img/plugin-logo.png'; |
| 154 |
if ( filter_var( $logo_url, FILTER_VALIDATE_URL ) !== false ) { |
| 155 |
return $logo_url; |
| 156 |
} |
| 157 |
|
| 158 |
return ''; |
| 159 |
} |
| 160 |
|
| 161 |
public static function menu(): void { |
| 162 |
$pages = DashboardHelper::get_files( 'pages' ); |
| 163 |
|
| 164 |
$pages = apply_filters(WOWP_Plugin::PREFIX. '_admin_pages_menu', $pages); |
| 165 |
|
| 166 |
$current_page = self::get_current_page(); |
| 167 |
|
| 168 |
//No nonce checking is required as this is just reading the parameters. |
| 169 |
$action = ( isset( $_REQUEST["action"] ) ) ? sanitize_text_field( wp_unslash( $_REQUEST["action"] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 170 |
|
| 171 |
echo '<h2 class="nav-tab-wrapper wpie-nav-tab-wrapper">'; |
| 172 |
foreach ( $pages as $key => $page ) { |
| 173 |
$class = ( $page['file'] === $current_page ) ? ' nav-tab-active wowp-page-' .$page['file'] : ' wowp-page-' . $page['file']; |
| 174 |
$id = ''; |
| 175 |
|
| 176 |
if ( $action === 'update' && $page['file'] === 'settings' ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 177 |
$id = ( isset( $_REQUEST["id"] ) ) ? absint( $_REQUEST["id"] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 178 |
$page['name'] = __( 'Update', 'floating-button' ) . ' #' . $id; |
| 179 |
} elseif ( $page['file'] === 'settings' && ( $action !== 'new' && $action !== 'duplicate' ) ) { |
| 180 |
continue; |
| 181 |
} |
| 182 |
if($page['file'] === 'pro') { |
| 183 |
echo '<a class="nav-tab' . esc_attr( $class ) . '" href="' . esc_url( Link::menu( $page['file'], $action, $id ) ) . '"><span class="wpie-icon wpie_icon-rocket"></span>' . esc_html( $page['name'] ) . '</a>'; |
| 184 |
} else { |
| 185 |
echo '<a class="nav-tab' . esc_attr( $class ) . '" href="' . esc_url( Link::menu( $page['file'], $action, $id ) ) . '">' . esc_html( $page['name'] ) . '</a>'; |
| 186 |
} |
| 187 |
|
| 188 |
} |
| 189 |
echo '</h2>'; |
| 190 |
} |
| 191 |
|
| 192 |
public static function get_current_page(): string { |
| 193 |
$default = DashboardHelper::first_file( 'pages' ); |
| 194 |
|
| 195 |
// Nonce verification is not required as the “tab” parameter is read-only. |
| 196 |
return ( isset( $_REQUEST["tab"] ) ) ? sanitize_text_field( wp_unslash( $_REQUEST["tab"] ) ) : $default; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 197 |
} |
| 198 |
|
| 199 |
public static function include_pages(): void { |
| 200 |
$current_page = self::get_current_page(); |
| 201 |
|
| 202 |
$pages = DashboardHelper::get_files( 'pages' ); |
| 203 |
$pages = apply_filters( WOWP_Plugin::PREFIX . '_admin_pages_menu', $pages ); |
| 204 |
|
| 205 |
$default = DashboardHelper::first_file( 'pages' ); |
| 206 |
|
| 207 |
$current = DashboardHelper::search_value( $pages, $current_page ) ? $current_page : $default; |
| 208 |
|
| 209 |
$file = DashboardHelper::get_file( $current, 'pages' ); |
| 210 |
|
| 211 |
$file_path = DashboardHelper::get_folder_path( 'pages' ) . '/' . $file; |
| 212 |
|
| 213 |
$page_path = apply_filters( WOWP_Plugin::PREFIX . '_admin_filter_file', $file_path, $file, $current ); |
| 214 |
|
| 215 |
if ( file_exists( $page_path ) ) { |
| 216 |
require_once $page_path; |
| 217 |
} |
| 218 |
|
| 219 |
} |
| 220 |
|
| 221 |
} |