|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Add task to select the site locale. |
| 4 | + * |
| 5 | + * @package Progress_Planner |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Progress_Planner\Suggested_Tasks\Providers; |
| 9 | + |
| 10 | +/** |
| 11 | + * Add task to select the site locale. |
| 12 | + */ |
| 13 | +class Select_Locale extends Tasks { |
| 14 | + |
| 15 | + /** |
| 16 | + * Whether the task is an onboarding task. |
| 17 | + * |
| 18 | + * @var bool |
| 19 | + */ |
| 20 | + protected const IS_ONBOARDING_TASK = false; |
| 21 | + |
| 22 | + /** |
| 23 | + * The provider ID. |
| 24 | + * |
| 25 | + * @var string |
| 26 | + */ |
| 27 | + protected const PROVIDER_ID = 'select-locale'; |
| 28 | + |
| 29 | + /** |
| 30 | + * Whether the task is dismissable. |
| 31 | + * |
| 32 | + * @var bool |
| 33 | + */ |
| 34 | + protected $is_dismissable = true; |
| 35 | + |
| 36 | + /** |
| 37 | + * Get the task URL. |
| 38 | + * |
| 39 | + * @return string |
| 40 | + */ |
| 41 | + protected function get_url() { |
| 42 | + return \admin_url( 'options-general.php' ); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Get the link setting. |
| 47 | + * |
| 48 | + * @return array |
| 49 | + */ |
| 50 | + public function get_link_setting() { |
| 51 | + return [ |
| 52 | + 'hook' => 'options-general.php', |
| 53 | + 'iconEl' => 'label[for="WPLANG"]', |
| 54 | + ]; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Get the task title. |
| 59 | + * |
| 60 | + * @return string |
| 61 | + */ |
| 62 | + protected function get_title() { |
| 63 | + return \esc_html__( 'Select your site locale', 'progress-planner' ); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Get the task description. |
| 68 | + * |
| 69 | + * @return string |
| 70 | + */ |
| 71 | + protected function get_description() { |
| 72 | + return \esc_html__( 'Select your site locale to ensure your site is displayed correctly in the correct language.', 'progress-planner' ); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Check if the task should be added. |
| 77 | + * |
| 78 | + * @return bool |
| 79 | + */ |
| 80 | + public function should_add_task() { |
| 81 | + $user_lang = $this->get_browser_locale(); |
| 82 | + $wp_lang = \get_locale(); |
| 83 | + |
| 84 | + return $user_lang && ! \str_starts_with( $wp_lang, $user_lang ); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Get the browser locale. |
| 89 | + * |
| 90 | + * @return string |
| 91 | + */ |
| 92 | + protected function get_browser_locale() { |
| 93 | + $lang = \sanitize_text_field( \wp_unslash( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? '' ) ); |
| 94 | + if ( ! $lang ) { |
| 95 | + return ''; |
| 96 | + } |
| 97 | + |
| 98 | + $lang = \strtolower( \substr( $lang, 0, 2 ) ); |
| 99 | + $lang = \explode( '-', $lang )[0]; |
| 100 | + $lang = \explode( '_', $lang )[0]; |
| 101 | + |
| 102 | + return $lang; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Get all locales from the WP API. |
| 107 | + * |
| 108 | + * Not currently used, but could be useful in the future. |
| 109 | + * |
| 110 | + * @return array |
| 111 | + */ |
| 112 | + protected function get_locales() { |
| 113 | + $cache_key = 'all_locales'; |
| 114 | + $cached = \progress_planner()->get_utils__cache()->get( $cache_key ); |
| 115 | + if ( $cached ) { |
| 116 | + return $cached; |
| 117 | + } |
| 118 | + |
| 119 | + $response = \wp_remote_get( 'https://api.wordpress.org/translations/core/1.0/' ); |
| 120 | + if ( \is_wp_error( $response ) ) { |
| 121 | + \progress_planner()->get_utils__cache()->set( $cache_key, [], 5 * MINUTE_IN_SECONDS ); |
| 122 | + return []; |
| 123 | + } |
| 124 | + $body = \wp_remote_retrieve_body( $response ); |
| 125 | + $locales = \json_decode( $body, true ); |
| 126 | + if ( ! \is_array( $locales ) || ! isset( $locales['translations'] ) ) { |
| 127 | + \progress_planner()->get_utils__cache()->set( $cache_key, [], 5 * MINUTE_IN_SECONDS ); |
| 128 | + return []; |
| 129 | + } |
| 130 | + |
| 131 | + // Get the locales. |
| 132 | + $locales = \array_map( |
| 133 | + function ( $locale ) { |
| 134 | + return [ |
| 135 | + 'code' => $locale['language'], |
| 136 | + 'name' => $locale['native_name'], |
| 137 | + ]; |
| 138 | + }, |
| 139 | + $locales['translations'] |
| 140 | + ); |
| 141 | + |
| 142 | + \progress_planner()->get_utils__cache()->set( $cache_key, $locales, MONTH_IN_SECONDS ); |
| 143 | + |
| 144 | + // Return the locales. |
| 145 | + return $locales; |
| 146 | + } |
| 147 | +} |
0 commit comments