| 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 |
* Returns the default value. |
| 45 |
* |
| 46 |
* @since 3.7 |
| 47 |
* |
| 48 |
* @return array |
| 49 |
*/ |
| 50 |
protected function get_default() { |
| 51 |
return array(); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Returns the JSON schema part specific to this option. |
| 56 |
* |
| 57 |
* @since 3.7 |
| 58 |
* |
| 59 |
* @return array Partial schema. |
| 60 |
*/ |
| 61 |
protected function get_data_structure(): array { |
| 62 |
return array( |
| 63 |
'type' => 'object', // Correspond to associative array in PHP, @see{https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#primitive-types}. |
| 64 |
'patternProperties' => array( |
| 65 |
'[^\/:<>\*\?"\|]+' => array( // Excludes invalid directory name characters @see https://developer.wordpress.org/reference/classes/wp_rest_themes_controller/register_routes/ |
| 66 |
'type' => 'object', |
| 67 |
'patternProperties' => array( |
| 68 |
'[\w-]+' => array( // Accepted characters for menu locations @see https://developer.wordpress.org/reference/classes/wp_rest_menu_locations_controller/register_routes/ |
| 69 |
'type' => 'object', |
| 70 |
'patternProperties' => array( |
| 71 |
Languages::SLUG_PATTERN => array( // Language slug as key. |
| 72 |
'type' => 'integer', |
| 73 |
'minimum' => 0, // A post ID. |
| 74 |
), |
| 75 |
), |
| 76 |
'additionalProperties' => false, |
| 77 |
), |
| 78 |
), |
| 79 |
'additionalProperties' => false, |
| 80 |
), |
| 81 |
), |
| 82 |
'additionalProperties' => false, |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Sanitizes option's value. |
| 88 |
* Can populate the `$errors` property with blocking and non-blocking errors: in case of non-blocking errors, |
| 89 |
* the value is sanitized and can be stored. |
| 90 |
* |
| 91 |
* @since 3.7 |
| 92 |
* |
| 93 |
* @param array $value Value to sanitize. |
| 94 |
* @param Options $options All options. |
| 95 |
* @return array|WP_Error The sanitized value. An instance of `WP_Error` in case of blocking error. |
| 96 |
* |
| 97 |
* @phpstan-return NavMenusValue|WP_Error |
| 98 |
*/ |
| 99 |
protected function sanitize( $value, Options $options ) { |
| 100 |
// Sanitize new value. |
| 101 |
$value = parent::sanitize( $value, $options ); |
| 102 |
|
| 103 |
if ( is_wp_error( $value ) ) { |
| 104 |
// Blocking error. |
| 105 |
return $value; |
| 106 |
} |
| 107 |
|
| 108 |
/** @phpstan-var NavMenusValue $value */ |
| 109 |
if ( empty( $value ) ) { |
| 110 |
// Nothing to validate. |
| 111 |
return $value; |
| 112 |
} |
| 113 |
|
| 114 |
$all_langs = array(); |
| 115 |
$language_terms = wp_list_pluck( $this->get_language_terms(), 'slug' ); |
| 116 |
|
| 117 |
foreach ( $value as $theme_slug => $menu_ids_by_location ) { |
| 118 |
foreach ( $menu_ids_by_location as $location => $menu_ids ) { |
| 119 |
// Make sure the language slugs correspond to an existing language. |
| 120 |
$value[ $theme_slug ][ $location ] = array(); |
| 121 |
|
| 122 |
foreach ( $language_terms as $lang_slug ) { |
| 123 |
if ( ! empty( $menu_ids[ $lang_slug ] ) ) { |
| 124 |
$value[ $theme_slug ][ $location ][ $lang_slug ] = $menu_ids[ $lang_slug ]; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
// Detect unknown languages. |
| 129 |
$all_langs = array_merge( $all_langs, $menu_ids ); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/** @phpstan-var NavMenusValue $value */ |
| 134 |
$unknown_langs = array_diff_key( $all_langs, array_flip( $language_terms ) ); |
| 135 |
|
| 136 |
// Detect invalid language slugs. |
| 137 |
if ( ! empty( $unknown_langs ) ) { |
| 138 |
// Non-blocking error. |
| 139 |
$this->add_unknown_languages_warning( array_keys( $unknown_langs ) ); |
| 140 |
} |
| 141 |
|
| 142 |
return $value; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Returns the description used in the JSON schema. |
| 147 |
* |
| 148 |
* @since 3.7 |
| 149 |
* |
| 150 |
* @return string |
| 151 |
*/ |
| 152 |
protected function get_description(): string { |
| 153 |
return __( 'Translated navigation menus for each theme.', 'polylang' ); |
| 154 |
} |
| 155 |
} |
| 156 |
|