Other.php
83 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPParsidate\App\Tools; |
| 4 | |
| 5 | use WPParsidate\Helper\Debug; |
| 6 | use WPParsidate\Settings\Settings; |
| 7 | |
| 8 | class Other { |
| 9 | private const sectionID = 'other'; |
| 10 | |
| 11 | public function __construct() { |
| 12 | add_filter( 'wp_parsidate_tools_settings_sections', [ $this, 'addSectionSettings' ] ); |
| 13 | |
| 14 | if ( Settings::get( 'date_in_admin_bar', false ) ) { |
| 15 | add_action( 'admin_bar_menu', [ $this, 'addDateToAdminBar' ], PHP_INT_MAX ); |
| 16 | add_action( 'admin_head', [ $this, 'adminBarDateStyle' ] ); |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Add Jalali date to WordPress admin bar |
| 22 | * |
| 23 | * @param $adminBar |
| 24 | * |
| 25 | * @return void |
| 26 | */ |
| 27 | public function addDateToAdminBar( $adminBar ): void { |
| 28 | $format = Debug::plugin() ? 'l Y/m/d H:i:s' : 'l Y/m/d'; |
| 29 | //$currentDate = parsidate( $format, current_time( 'timestamp' ) ); |
| 30 | $currentDate = wp_date( $format, time() ); |
| 31 | |
| 32 | $args = array( |
| 33 | 'id' => 'wpp_current_date', |
| 34 | 'title' => esc_html__( 'Today: ', 'wp-parsidate' ) . $currentDate, |
| 35 | 'meta' => array( 'class' => 'wpp-admin-bar-date' ), |
| 36 | ); |
| 37 | |
| 38 | $adminBar->add_node( $args ); |
| 39 | } |
| 40 | |
| 41 | public function adminBarDateStyle(): void { |
| 42 | global $_wp_admin_css_colors; |
| 43 | $color = get_user_option( 'admin_color' ); |
| 44 | $bgColor = $_wp_admin_css_colors[ $color ]->colors[2] ?? '#2271b1'; |
| 45 | |
| 46 | echo '<style> |
| 47 | .wpp-admin-bar-date { |
| 48 | color: #fff !important; |
| 49 | background-color: ' . esc_html( $bgColor ) . ' !important; |
| 50 | unicode-bidi: embed !important; |
| 51 | } |
| 52 | </style>'; |
| 53 | } |
| 54 | |
| 55 | public function addSectionSettings( array $sections ): array { |
| 56 | $settings = [ |
| 57 | 'start_grid_admin_bar' => array( |
| 58 | 'title' => esc_html__( 'Admin Bar', 'wp-parsidate' ), |
| 59 | 'type' => 'startGrid', |
| 60 | ), |
| 61 | 'date_in_admin_bar' => array( |
| 62 | 'id' => 'date_in_admin_bar', |
| 63 | 'title' => esc_html__( 'Display date in the admin bar', 'wp-parsidate' ), |
| 64 | 'type' => 'toggle', |
| 65 | 'default' => false, |
| 66 | 'desc' => esc_html__( "Display today's Jalali date in the WordPress admin bar.", 'wp-parsidate' ), |
| 67 | 'sanitize' => 'bool' |
| 68 | ), |
| 69 | 'end_grid_admin_bar' => array( |
| 70 | 'type' => 'endGrid', |
| 71 | ), |
| 72 | ]; |
| 73 | |
| 74 | $sections[ self::sectionID ] = array( |
| 75 | 'title' => esc_html__( 'Other tools', 'wp-parsidate' ), |
| 76 | 'desc' => esc_html__( 'Other tools', 'wp-parsidate' ), |
| 77 | 'settings' => $settings |
| 78 | ); |
| 79 | |
| 80 | return $sections; |
| 81 | } |
| 82 | } |
| 83 |