| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace WP_Syntex\Polylang\Options\Business; |
| 7 |
|
| 8 |
use WP_Error; |
| 9 |
use WP_Syntex\Polylang\Options\Abstract_Option; |
| 10 |
use WP_Syntex\Polylang\Options\Options; |
| 11 |
use WP_Syntex\Polylang\Model\Languages; |
| 12 |
|
| 13 |
|
| 14 |
defined( 'ABSPATH' ) || exit; |
| 15 |
|
| 16 |
/** |
| 17 |
* Class defining navigation menus array option. |
| 18 |
* |
| 19 |
* @since 3.7 |
| 20 |
* |
| 21 |
* @phpstan-type NavMenusValue array< |
| 22 |
* non-falsy-string, |
| 23 |
* array< |
| 24 |
* non-falsy-string, |
| 25 |
* array<non-falsy-string, int<0, max>> |
| 26 |
* > |
| 27 |
* > |
| 28 |
*/ |
| 29 |
class Nav_Menus extends Abstract_Option { |
| 30 |
/** |
| 31 |
* Returns option key. |
| 32 |
* |
| 33 |
* @since 3.7 |
| 34 |
* |
| 35 |
* @return string |
| 36 |
* |
| 37 |
* @phpstan-return 'nav_menus' |
| 38 |
*/ |
| 39 |
public static function key(): string { |
| 40 |
return 'nav_menus'; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Adds information to the site health info array. |
| 45 |
* |
| 46 |
* @since 3.8 |
| 47 |
* |
| 48 |
* @param Options $options An instance of the Options class providing additional configuration. |
| 49 |
* |
| 50 |
* @return array The updated site health information. |
| 51 |
*/ |
| 52 |
public function get_site_health_info( Options $options ): array { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 53 |
$current_theme = get_stylesheet(); |
| 54 |
/** @phpstan-var NavMenusValue $nav_menus */ |
| 55 |
$nav_menus = $this->get(); |
| 56 |
if ( empty( $nav_menus[ $current_theme ] ) ) { |
| 57 |
return array(); |
| 58 |
} |
| 59 |
|
| 60 |
$parts = array(); |
| 61 |
foreach ( $nav_menus[ $current_theme ] as $location => $lang ) { |
| 62 |
if ( empty( $lang ) ) { |
| 63 |
$parts[] = sprintf( '%1$s: %2$s', $location, __( 'Not used', 'polylang' ) ); |
| 64 |
} else { |
| 65 |
$parts[] = sprintf( |
| 66 |
'%1$s: %2$s', |
| 67 |
$location, |
| 68 |
$this->format_array_for_site_health_info( $lang ) |
| 69 |
); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
return $this->format_single_value_for_site_health_info( implode( ' | ', $parts ) ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Returns the default value. |
| 78 |
* |
| 79 |
* @since 3.7 |
| 80 |
* |
| 81 |
* @return array |
| 82 |
*/ |
| 83 |
protected function get_default() { |
| 84 |
return array(); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Returns the JSON schema part specific to this option. |
| 89 |
* |
| 90 |
* @since 3.7 |
| 91 |
* |
| 92 |
* @return array Partial schema. |
| 93 |
*/ |
| 94 |
protected function get_data_structure(): array { |
| 95 |
return array( |
| 96 |
'type' => 'object', // Correspond to associative array in PHP, @see{https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#primitive-types}. |
| 97 |
'patternProperties' => array( |
| 98 |
'[^\/:<>\*\?"\|]+' => array( // Excludes invalid directory name characters @see https://developer.wordpress.org/reference/classes/wp_rest_themes_controller/register_routes/ |
| 99 |
'type' => 'object', |
| 100 |
'patternProperties' => array( |
| 101 |
'[\w-]+' => array( // Accepted characters for menu locations @see https://developer.wordpress.org/reference/classes/wp_rest_menu_locations_controller/register_routes/ |
| 102 |
'type' => 'object', |
| 103 |
'patternProperties' => array( |
| 104 |
Languages::SLUG_PATTERN => array( // Language slug as key. |
| 105 |
'type' => 'integer', |
| 106 |
'minimum' => 0, // A post ID. |
| 107 |
), |
| 108 |
), |
| 109 |
'additionalProperties' => false, |
| 110 |
), |
| 111 |
), |
| 112 |
'additionalProperties' => false, |
| 113 |
), |
| 114 |
), |
| 115 |
'additionalProperties' => false, |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Sanitizes option's value. |
| 121 |
* Can populate the `$errors` property with blocking and non-blocking errors: in case of non-blocking errors, |
| 122 |
* the value is sanitized and can be stored. |
| 123 |
* |
| 124 |
* @since 3.7 |
| 125 |
* |
| 126 |
* @param array $value Value to sanitize. |
| 127 |
* @param Options $options All options. |
| 128 |
* @return array|WP_Error The sanitized value. An instance of `WP_Error` in case of blocking error. |
| 129 |
* |
| 130 |
* @phpstan-return NavMenusValue|WP_Error |
| 131 |
*/ |
| 132 |
protected function sanitize( $value, Options $options ) { |
| 133 |
// Sanitize new value. |
| 134 |
$value = parent::sanitize( $value, $options ); |
| 135 |
|
| 136 |
if ( is_wp_error( $value ) ) { |
| 137 |
// Blocking error. |
| 138 |
return $value; |
| 139 |
} |
| 140 |
|
| 141 |
/** @phpstan-var NavMenusValue $value */ |
| 142 |
if ( empty( $value ) ) { |
| 143 |
// Nothing to validate. |
| 144 |
return $value; |
| 145 |
} |
| 146 |
|
| 147 |
$all_langs = array(); |
| 148 |
$language_terms = wp_list_pluck( $this->get_language_terms(), 'slug' ); |
| 149 |
|
| 150 |
foreach ( $value as $theme_slug => $menu_ids_by_location ) { |
| 151 |
foreach ( $menu_ids_by_location as $location => $menu_ids ) { |
| 152 |
// Make sure the language slugs correspond to an existing language. |
| 153 |
$value[ $theme_slug ][ $location ] = array(); |
| 154 |
|
| 155 |
foreach ( $language_terms as $lang_slug ) { |
| 156 |
if ( ! empty( $menu_ids[ $lang_slug ] ) ) { |
| 157 |
$value[ $theme_slug ][ $location ][ $lang_slug ] = $menu_ids[ $lang_slug ]; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
// Detect unknown languages. |
| 162 |
$all_langs = array_merge( $all_langs, $menu_ids ); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
/** @phpstan-var NavMenusValue $value */ |
| 167 |
$unknown_langs = array_diff_key( $all_langs, array_flip( $language_terms ) ); |
| 168 |
|
| 169 |
// Detect invalid language slugs. |
| 170 |
if ( ! empty( $unknown_langs ) ) { |
| 171 |
// Non-blocking error. |
| 172 |
$this->add_unknown_languages_warning( array_keys( $unknown_langs ) ); |
| 173 |
} |
| 174 |
|
| 175 |
return $value; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Returns the description used in the JSON schema. |
| 180 |
* |
| 181 |
* @since 3.7 |
| 182 |
* |
| 183 |
* @return string |
| 184 |
*/ |
| 185 |
protected function get_description(): string { |
| 186 |
return __( 'Translated navigation menus for each theme.', 'polylang' ); |
| 187 |
} |
| 188 |
} |
| 189 |
|