Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
CRAP | n/a |
0 / 0 |
|
| wpcomsh_headstart_get_annotation | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
156 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Utilities for working with Headstart annotations. |
| 4 | * |
| 5 | * @package wpcomsh |
| 6 | */ |
| 7 | |
| 8 | use Automattic\Jetpack\Device_Detection\User_Agent_Info; |
| 9 | |
| 10 | /** |
| 11 | * Retrieves the headstart annotation for a given theme. |
| 12 | * |
| 13 | * @param string $theme_name The name of the theme. |
| 14 | * @param string $locale The preferred locale. |
| 15 | * @param string $fallback_locale The locale that's preferred if $locale is not found. |
| 16 | * |
| 17 | * @return array|bool It will return an associative array that contains the headstart annotation or |
| 18 | * false if it doesn't exist. |
| 19 | */ |
| 20 | function wpcomsh_headstart_get_annotation( $theme_name, $locale, $fallback_locale ) { |
| 21 | if ( ! in_array( $locale, array( 'pt-br', 'zh-cn', 'zh-tw' ), true ) ) { |
| 22 | $locale = strtok( $locale, '-' ); |
| 23 | } |
| 24 | |
| 25 | // 1. Check valid theme. |
| 26 | $theme = wp_get_theme( $theme_name ); |
| 27 | if ( is_wp_error( $theme->errors() ) ) { |
| 28 | return false; |
| 29 | } |
| 30 | |
| 31 | $themes_dir = WP_CONTENT_DIR . '/themes'; |
| 32 | $annotation_dir = 'inc/headstart'; |
| 33 | |
| 34 | // 2. Get annotation file path |
| 35 | $annotation_uri = $themes_dir . "/$theme_name/$annotation_dir/$locale.json"; |
| 36 | // 3. Check annotation URI. |
| 37 | if ( ! is_readable( $annotation_uri ) ) { |
| 38 | // 3.1. Try with fallback locale. |
| 39 | $annotation_uri = $themes_dir . "/$theme_name/$annotation_dir/$fallback_locale.json"; |
| 40 | if ( ! is_readable( $annotation_uri ) ) { |
| 41 | // 3.2 Try with wp-content/lib/headstart/class-headstart-annotations.php?r=6bba545e#171. |
| 42 | $annotation_uri = WP_CONTENT_DIR . '/lib/headstart/annotations/' . $locale . '.json'; |
| 43 | if ( ! is_readable( $annotation_uri ) ) { |
| 44 | return false; |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | // 4. Get annotation content. |
| 49 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 50 | $annotation_string = file_get_contents( $annotation_uri ); |
| 51 | // 5. Check for content. |
| 52 | if ( false === $annotation_string ) { |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | // 6. Decode content. |
| 57 | $annotation = json_decode( $annotation_string, true ); |
| 58 | // 7. Check decoded content. |
| 59 | if ( |
| 60 | empty( $annotation ) || |
| 61 | ! is_array( $annotation ) || |
| 62 | ! array_key_exists( 'content', $annotation ) || |
| 63 | ! is_array( $annotation['content'] ) |
| 64 | ) { |
| 65 | return false; |
| 66 | } |
| 67 | // 8. maybe_filter_annotation_for_non_gutenberg_users in headstart/class-headstart-annotations.php. |
| 68 | $is_gutenberg_user = ! User_Agent_Info::is_mobile_app(); |
| 69 | if ( $is_gutenberg_user ) { |
| 70 | $annotation = apply_filters( 'headstart_gutenberg_annotation_filter', $annotation, $theme_name, false, false, $locale ); |
| 71 | } |
| 72 | return $annotation; |
| 73 | } |