Admin.php
3 weeks ago
AdminAbout.php
3 weeks ago
AdminAssets.php
3 weeks ago
AdminConvert.php
3 weeks ago
AdminCore.php
3 weeks ago
AdminDashboard.php
3 weeks ago
AdminIntegration.php
3 weeks ago
AdminPages.php
3 weeks ago
AdminSettings.php
3 weeks ago
AdminTools.php
3 weeks ago
AdminPages.php
214 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPParsidate\Admin; |
| 4 | |
| 5 | defined( 'ABSPATH' ) || exit; |
| 6 | |
| 7 | use WPParsidate\Helper\{Assets, Cache, Notice, Param}; |
| 8 | |
| 9 | class AdminPages { |
| 10 | public function __construct() { |
| 11 | add_action( 'admin_init', [ $this, 'init' ] ); |
| 12 | add_action( 'wp_parsidate_admin_init', [ $this, 'checkSubmitForm' ], 15 ); |
| 13 | add_action( 'admin_menu', array( $this, 'adminMenuInit' ), 0 ); |
| 14 | add_action( 'admin_menu', array( $this, 'addMenu' ), PHP_INT_MAX ); |
| 15 | add_action( 'wp_parsidate_notice', [ $this, 'displayNotices' ] ); |
| 16 | add_action( 'wp_parsidate_header', [ $this, 'pageHeader' ] ); |
| 17 | add_action( 'wp_parsidate_content', [ $this, 'pageContent' ] ); |
| 18 | add_action( 'wp_parsidate_footer', [ $this, 'pageFooter' ] ); |
| 19 | //add_action( 'admin_footer', [ $this, 'flushRewriteRules' ] ); |
| 20 | } |
| 21 | |
| 22 | public function flushRewriteRules(): void { |
| 23 | if ( Cache::get( 'settings_saved' ) ) { |
| 24 | flush_rewrite_rules(); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | public function checkSubmitForm(): void { |
| 29 | $tab = self::getActiveTab(); |
| 30 | if ( isset( $_POST['_form_nonce'] ) && check_admin_referer( 'settings_submit_' . $tab, '_form_nonce' ) ) { |
| 31 | do_action( 'wp_parsidate_submit_settings_form', $tab ); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | public function pageHeader( $currentTab ): void { |
| 36 | AdminSettings::headerSettings( $currentTab, AdminSettings::getSettings( $currentTab ) ); |
| 37 | } |
| 38 | |
| 39 | public function pageContent( $currentTab ): void { |
| 40 | $settings = AdminSettings::getSettings( $currentTab ); |
| 41 | |
| 42 | if ( $settings && apply_filters( 'wp_parsidate_display_tab_settings', true, $currentTab ) ) { |
| 43 | AdminSettings::printPage( $currentTab, $settings ); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | public function pageFooter( $currentTab ): void { |
| 48 | $settings = AdminSettings::getSettings( $currentTab ); |
| 49 | if ( empty( $settings ) ) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | $currentSection = AdminSettings::getActiveSection( $settings ); |
| 54 | $currentSection = $currentSection ?: null; |
| 55 | AdminSettings::footerSettings( $currentTab, $currentSection ); |
| 56 | } |
| 57 | |
| 58 | public function init(): void { |
| 59 | if ( self::isSettingPage() ) { |
| 60 | do_action( 'wp_parsidate_admin_init', self::getActiveTab() ); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | public function adminMenuInit(): void { |
| 65 | if ( self::isSettingPage() ) { |
| 66 | do_action( 'wp_parsidate_admin_init_menu', self::getActiveTab() ); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | public function displayNotices( $tab ): void { |
| 71 | if ( apply_filters( 'wp_parsidate_' . $tab . '_tab_display_notice', true ) ) { |
| 72 | Notice::display( '*' ); |
| 73 | Notice::display( $tab ); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | public function addMenu(): void { |
| 78 | $icon = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI0NTAiIGhlaWdodD0iNDUwIj48ZyBjbGFzcz0ibGF5ZXIiPjx0aXRsZT5MYXllciAxPC90aXRsZT48cGF0aCBmaWxsPSIjZmZmIiBkPSJNMjA3IC42Yy0yOC43IDIuNi01Ny4xIDEwLjYtODEuNyAyMi45LTQ1LjggMjIuOS03OC45IDU2LTEwMS45IDEwMS45LTM0IDY4LTMwLjUgMTUwLj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MTAuOC0xMWM2LjktNy4xIDExLjUtMTEgMTIuOS0xMSAyLjggMCAyOS43IDI2LjYgMjkuNyAyOS40IDAgMi40LTI2LjYgMjkuNi0yOSAyOS42LS44IDAtNi41LTUuMS0xMi43LTExLjJMMTMwIDMwMC41bC0xMS4zIDExLjNjLTYuMiA2LjEtMTEuOSAxMS4yLTEyLjggMTEuMi0uOCAwLTcuOS02LjUtMTUuOC0xNC40LTEzLjktMTQuMS0xNC4yLTE0LjUtMTIuOS0xNyAyLjMtNC4zIDI2LTI3LjYgMjguMS0yNy42IDEuMiAwIDYuNSA0LjQgMTMuMiAxMW0yNy4zIDQzLjZjOS44IDkuMyAxNC4yIDE0LjIgMTQuMiAxNS43cy00LjcgNi45LTE0LjUgMTYuN0wxMzEgMzY1LjRsLTEzLjItMTIuOWMtMTcuNS0xNy4xLTE3LjQtMTctMTUuNC0yMCAzLjEtNC43IDI1LjktMjcuNSAyNy41LTI3LjUuOSAwIDggNi4xIDE1LjkgMTMuNiIvPjxwYXRoIGZpbGw9IiNmZmYiIGQ9Ik0zNDQuMiAxNjQuMWMtNy44IDMuOS0xNy40IDEzLjMtMTkuNyAxOS40LTQuOCAxMi4zIDMgMjEuMiAyNC40IDI3LjkgOC4yIDIuNiA1Mi41IDkuNiA1My42IDguNXMtMy43LTE2LjYtNy41LTI0LjNjLTcuOS0xNi4zLTE5LjMtMjcuOS0zMS42LTMyLjUtNy42LTIuOS0xMi0yLjctMTkuMiAxIi8+PC9nPjwvc3ZnPg=='; |
| 79 | |
| 80 | add_menu_page( esc_html__( 'WP Parsi', 'wp-parsidate' ), esc_html__( 'WP Parsi', 'wp-parsidate' ), 'manage_options', |
| 81 | WP_PARSI_KEY_SLUG, |
| 82 | [ $this, 'mainPage' ], $icon ); |
| 83 | } |
| 84 | |
| 85 | public function mainPage(): void { |
| 86 | $logo = Assets::url( 'images/logo.svg' ); |
| 87 | $currentTab = self::getActiveTab(); |
| 88 | $wrapperClass = array( |
| 89 | WP_PARSI_KEY_SLUG . '-wrap', |
| 90 | WP_PARSI_KEY_SLUG . '-wrapper', |
| 91 | WP_PARSI_KEY_SLUG . '-' . esc_html( $currentTab ), |
| 92 | ); |
| 93 | ?> |
| 94 | <div class="wrap"> |
| 95 | <div class="<?php echo esc_attr( implode( ' ', $wrapperClass ) ) ?>"> |
| 96 | <div class="wppd-sidebar" id="wppd-sidebar"> |
| 97 | <div class="wppd-sidebar-head"> |
| 98 | <div class="wppd-logo-wrap"> |
| 99 | <img src="<?php echo esc_url_raw( $logo ) ?>" alt="Logo" class="wppd-logo"> |
| 100 | <div> |
| 101 | <span><?php esc_html_e( 'WordPress', 'wp-parsidate' ); ?></span> |
| 102 | <span><?php esc_html_e( 'Parsi', 'wp-parsidate' ); ?></span> |
| 103 | </div> |
| 104 | </div> |
| 105 | <a href="#" class="wppd-hide-sidebar" id="wppd-hide-sidebar"> |
| 106 | <svg viewBox="0 0 24 24" width="30px" height="30px" fill="none" xmlns="http://www.w3.org/2000/svg"> |
| 107 | <g id="SVGRepo_bgCarrier" stroke-width="0"></g> |
| 108 | <g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g> |
| 109 | <g id="SVGRepo_iconCarrier"> |
| 110 | <g id="Menu / Close_SM"> |
| 111 | <path id="Vector" d="M16 16L12 12M12 12L8 8M12 12L16 8M12 12L8 16" |
| 112 | stroke="#3c3c3c" stroke-width="2" stroke-linecap="round" |
| 113 | stroke-linejoin="round"></path> |
| 114 | </g> |
| 115 | </g> |
| 116 | </svg> |
| 117 | </a> |
| 118 | </div> |
| 119 | <div class="menu-items"> |
| 120 | <?php |
| 121 | do_action( 'wp_parsidate_start_menus' ); |
| 122 | $menus = self::getMenus(); |
| 123 | $addonSep = false; |
| 124 | foreach ( $menus as $tab => $menu ) { |
| 125 | if ( ! $addonSep && ! in_array( $tab, self::defaultTabs(), true ) ) { |
| 126 | echo '<hr>'; |
| 127 | $addonSep = true; |
| 128 | } |
| 129 | |
| 130 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 131 | echo self::menuItem( $tab, $menu ); |
| 132 | } |
| 133 | do_action( 'wp_parsidate_end_menus' ); |
| 134 | ?> |
| 135 | </div> |
| 136 | </div> |
| 137 | <div class="wppd-display-sidebar"> |
| 138 | <a href="#" id="wppd-display-sidebar"> |
| 139 | <svg xmlns="http://www.w3.org/2000/svg" width="30px" height="30px" fill="none" |
| 140 | viewBox="0 0 24 24"> |
| 141 | <path stroke="#3c3c3c" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" |
| 142 | d="M5 17h8m-8-5h14M5 7h8"/> |
| 143 | </svg> |
| 144 | </a> |
| 145 | </div> |
| 146 | <div class="wppd-content wppd-<?php echo esc_html( $currentTab ) ?>-content" id="wppd-content-wrap"> |
| 147 | <?php |
| 148 | // Display tab header |
| 149 | do_action( 'wp_parsidate_' . $currentTab . '_tab_header' ); |
| 150 | do_action( 'wp_parsidate_header', $currentTab ); |
| 151 | |
| 152 | echo '<div class="wppd-content-body">'; |
| 153 | // Display notice |
| 154 | do_action( 'wp_parsidate_notice', $currentTab ); |
| 155 | do_action( 'wp_parsidate_' . $currentTab . '_tab_notice' ); |
| 156 | |
| 157 | // Display tab content |
| 158 | do_action( 'wp_parsidate_' . $currentTab . '_tab_content' ); |
| 159 | do_action( 'wp_parsidate_content', $currentTab ); |
| 160 | echo '</div>'; |
| 161 | |
| 162 | // Display tab footer |
| 163 | do_action( 'wp_parsidate_' . $currentTab . '_tab_footer' ); |
| 164 | do_action( 'wp_parsidate_footer', $currentTab ); |
| 165 | ?> |
| 166 | </div> |
| 167 | </div> |
| 168 | </div> |
| 169 | <?php |
| 170 | } |
| 171 | |
| 172 | public static function getMenus(): array { |
| 173 | return apply_filters( 'wp_parsidate_menus', [] ); |
| 174 | } |
| 175 | |
| 176 | public static function menuItem( $tab, $menu, $link = null ): string { |
| 177 | $current = self::getActiveTab(); |
| 178 | $link = $menu['link'] ?? $link; |
| 179 | $link = empty( $link ) ? self::link( [ 'tab' => $tab ] ) : $link; |
| 180 | |
| 181 | if ( ! is_array( $menu ) || ! isset( $menu['title'] ) ) { |
| 182 | return ''; |
| 183 | } |
| 184 | |
| 185 | $icon = Assets::isSvgImageString( $menu['icon'] ) ? Assets::setSvgDimensions( $menu['icon'], 20 ) : ''; |
| 186 | |
| 187 | return '<a href="' . esc_url_raw( $link ) . '" class="menu-item' . ( $current === $tab ? ' menu-item-current' : '' ) . '">' . $icon . '<span>' . esc_html( $menu['title'] ) . '</span></a>'; |
| 188 | } |
| 189 | |
| 190 | public static function getActiveTab(): string { |
| 191 | $default = 'dashboard'; |
| 192 | $current = strtolower( Param::get( 'tab', $default ) ); |
| 193 | $tabs = array_merge( self::defaultTabs(), array_keys( self::getMenus() ) ); |
| 194 | |
| 195 | return in_array( $current, $tabs, true ) ? $current : $default; |
| 196 | } |
| 197 | |
| 198 | private static function defaultTabs(): array { |
| 199 | return [ 'dashboard', 'core', 'convert', 'tools', 'integration', 'addons', 'about' ]; |
| 200 | } |
| 201 | |
| 202 | public static function isSettingPage(): bool { |
| 203 | return is_admin() && Param::get( 'page' ) === WP_PARSI_KEY_SLUG; |
| 204 | } |
| 205 | |
| 206 | public static function link( $query ): ?string { |
| 207 | $query = is_array( $query ) ? $query : array(); |
| 208 | $data = array_merge( array( 'page' => WP_PARSI_KEY_SLUG ), $query ); |
| 209 | $query = http_build_query( $data ); |
| 210 | |
| 211 | return admin_url( 'admin.php?' . $query ); |
| 212 | } |
| 213 | } |
| 214 |