Plugin.php
| 1 | <?php |
| 2 | /** |
| 3 | * Plugin hooks |
| 4 | * |
| 5 | * Load local text domain, Add starter notice, Change some links |
| 6 | */ |
| 7 | |
| 8 | namespace WPParsidate\Plugin; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | use WPParsidate\Admin\AdminPages; |
| 13 | use WPParsidate\Settings\Settings; |
| 14 | |
| 15 | class Plugin { |
| 16 | public function __construct() { |
| 17 | add_filter( 'plugin_action_links_' . plugin_basename( WP_PARSI_ROOT ), [ $this, 'pluginActionLink' ] ); |
| 18 | add_filter( 'login_headerurl', [ $this, 'changeLoginLink' ], 10, 2 ); |
| 19 | add_filter( 'wp_parsidate_wp_admin_notice', [ $this, 'adminNotice' ] ); |
| 20 | add_action( 'init', [ $this, 'loadTextDomain' ], - 1 ); |
| 21 | add_filter( 'http_request_args', [ $this, 'limitWpParsiTimeout' ], 10, 2 ); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Limits the timeout for requests to wp-parsi.com to prevent dashboard lag. |
| 26 | * |
| 27 | * @param array $args Request arguments. |
| 28 | * @param string $url Request URL. |
| 29 | * |
| 30 | * @return array Modified request arguments. |
| 31 | */ |
| 32 | public function limitWpParsiTimeout( array $args, string $url ): array { |
| 33 | if ( str_contains( $url, 'wp-parsi.com' ) ) { |
| 34 | $args['timeout'] = 6; |
| 35 | } |
| 36 | |
| 37 | return $args; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Load plugin translations from the local languages directory. |
| 42 | * |
| 43 | * Always loads the local .mo file so that newly added/translated strings |
| 44 | * are available immediately, even before they're published on translate.wordpress.org. |
| 45 | * Uses determine_locale() instead of hardcoding 'fa_IR' for multi-locale flexibility. |
| 46 | */ |
| 47 | public function loadTextDomain(): void { |
| 48 | if ( __( 'WordPress', 'wp-parsidate' ) !== 'وردپرس' || Settings::get( 'local_text_domain', false ) ) { |
| 49 | load_textdomain( |
| 50 | 'wp-parsidate', |
| 51 | WP_PARSI_DIR . '/languages/wp-parsidate-' . determine_locale() . '.mo' |
| 52 | ); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Notice for the activation. |
| 58 | * |
| 59 | * @param array $notices Notice array list |
| 60 | * |
| 61 | * @return array Notice array list |
| 62 | */ |
| 63 | public function adminNotice( array $notices ): array { |
| 64 | if ( ! Settings::get( 'persian_date', false ) ) { |
| 65 | $notices[] = array( |
| 66 | 'id' => 'persian_date_activation', |
| 67 | 'message' => sprintf( |
| 68 | __( 'ParsiDate activated, you may need to configure it to work properly. <a href="%s">Go to configuration page</a>', 'wp-parsidate' ), |
| 69 | esc_url_raw( AdminPages::link( [ 'tab' => 'core' ] ) ) |
| 70 | ), |
| 71 | 'type' => 'warning', |
| 72 | 'dismissible' => true, |
| 73 | 'dismiss_time' => MONTH_IN_SECONDS, |
| 74 | 'not_page' => WP_PARSI_KEY_SLUG |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | return $notices; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Change login header url in wp-login.php & Widget primary link |
| 83 | * |
| 84 | * @return string |
| 85 | */ |
| 86 | public function changeLoginLink(): string { |
| 87 | return 'https://wp-parsi.com'; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Add setting link to admin plugins |
| 92 | * |
| 93 | * @param array $links |
| 94 | * |
| 95 | * @return array |
| 96 | */ |
| 97 | public static function pluginActionLink( $links ): array { |
| 98 | $links[] = '<a href="' . menu_page_url( WP_PARSI_KEY_SLUG, false ) . '">' . |
| 99 | esc_html__( 'Settings', 'wp-parsidate' ) . '</a>'; |
| 100 | |
| 101 | return $links; |
| 102 | } |
| 103 | } |
| 104 |