Plugin.php
118 lines
| 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\Helper\Param; |
| 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_action( 'admin_notices', [ $this, 'activationAdminNotice' ] ); |
| 20 | add_action( 'admin_init', [ $this, 'dismissActivationNotice' ] ); |
| 21 | add_action( 'init', [ $this, 'loadTextDomain' ], - 1 ); |
| 22 | add_filter( 'http_request_args', [ $this, 'limitWpParsiTimeout' ], 10, 2 ); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Limits the timeout for requests to wp-parsi.com to prevent dashboard lag. |
| 27 | * |
| 28 | * @param array $args Request arguments. |
| 29 | * @param string $url Request URL. |
| 30 | * |
| 31 | * @return array Modified request arguments. |
| 32 | */ |
| 33 | public function limitWpParsiTimeout( array $args, string $url ): array { |
| 34 | if ( str_contains( $url, 'wp-parsi.com' ) ) { |
| 35 | $args['timeout'] = 3; |
| 36 | } |
| 37 | |
| 38 | return $args; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Load plugin translations from the local languages directory. |
| 43 | * |
| 44 | * Always loads the local .mo file so that newly added/translated strings |
| 45 | * are available immediately, even before they're published on translate.wordpress.org. |
| 46 | * Uses determine_locale() instead of hardcoding 'fa_IR' for multi-locale flexibility. |
| 47 | */ |
| 48 | public function loadTextDomain(): void { |
| 49 | if ( __( 'WordPress', 'wp-parsidate' ) !== 'وردپرس' || Settings::get( 'local_text_domain', false )) { |
| 50 | load_textdomain( |
| 51 | 'wp-parsidate', |
| 52 | WP_PARSI_DIR . 'languages/wp-parsidate-' . determine_locale() . '.mo' |
| 53 | ); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Notice for the activation. |
| 59 | * Added dismiss feature. |
| 60 | * |
| 61 | * @return void |
| 62 | * @author Ehsaan |
| 63 | */ |
| 64 | public function activationAdminNotice(): void { |
| 65 | $dismissed = Settings::get( 'activation_admin_notice_dismissed', false ); |
| 66 | |
| 67 | if ( ! $dismissed && ( ! isset( $_GET['page'] ) || WP_PARSI_KEY_SLUG !== Param::get( 'page' ) ) && |
| 68 | ! Settings::get( 'persian_date', false ) ) { |
| 69 | $dismiss_url = wp_nonce_url( add_query_arg( 'wpp-action', 'dismiss-active-notice' ), 'wpp_dismiss_notice' ); |
| 70 | |
| 71 | /* translators: 1: ParsiDate settings link, 2: Dismiss notice link */ |
| 72 | $message = __( '<div class="updated wpp-message"><p>ParsiDate activated, you may need to configure it to work properly. <a href="%1$s">Go to configuration page</a> – <a href="%2$s">Dismiss</a></p></div>', |
| 73 | 'wp-parsidate' ); |
| 74 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 75 | echo sprintf( $message, |
| 76 | esc_url_raw( menu_page_url( WP_PARSI_KEY_SLUG, false ) ), |
| 77 | esc_url_raw( $dismiss_url ), |
| 78 | ); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Dismiss the notice action |
| 84 | * |
| 85 | * @return void |
| 86 | * @author Ehsaan |
| 87 | */ |
| 88 | public function dismissActivationNotice(): void { |
| 89 | if ( isset( $_GET['wpp-action'] ) && Param::get( 'wpp-action' ) === 'dismiss-active-notice' ) { |
| 90 | check_admin_referer( 'wpp_dismiss_notice' ); |
| 91 | Settings::save( 'activation_admin_notice_dismissed', true ); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Change login header url in wp-login.php & Widget primary link |
| 97 | * |
| 98 | * @return string |
| 99 | */ |
| 100 | public function changeLoginLink(): string { |
| 101 | return 'https://wp-parsi.com'; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Add setting link to admin plugins |
| 106 | * |
| 107 | * @param array $links |
| 108 | * |
| 109 | * @return array |
| 110 | */ |
| 111 | public static function pluginActionLink( $links ): array { |
| 112 | $links[] = '<a href="' . menu_page_url( WP_PARSI_KEY_SLUG, false ) . '">' . |
| 113 | esc_html__( 'Settings', 'wp-parsidate' ) . '</a>'; |
| 114 | |
| 115 | return $links; |
| 116 | } |
| 117 | } |
| 118 |