| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace WP_Syntex\Polylang\Model; |
| 7 |
|
| 8 |
use PLL_Language; |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Class allowing to chain language proxies. |
| 14 |
* |
| 15 |
* @since 3.8 |
| 16 |
*/ |
| 17 |
class Languages_Proxies { |
| 18 |
/** |
| 19 |
* @var Languages |
| 20 |
*/ |
| 21 |
protected $languages; |
| 22 |
|
| 23 |
/** |
| 24 |
* @var Languages_Proxy_Interface[] |
| 25 |
* |
| 26 |
* @phpstan-var array<non-falsy-string, Languages_Proxy_Interface> |
| 27 |
*/ |
| 28 |
private $proxies = array(); |
| 29 |
|
| 30 |
/** |
| 31 |
* @var string[] |
| 32 |
*/ |
| 33 |
protected $stack = array(); |
| 34 |
|
| 35 |
/** |
| 36 |
* Constructor. |
| 37 |
* |
| 38 |
* @since 3.8 |
| 39 |
* |
| 40 |
* @param Languages $languages Languages' model. |
| 41 |
* @param array $proxies List of registered proxies. |
| 42 |
* @param string $parent Key of the first item of the proxies stack to traverse. |
| 43 |
*/ |
| 44 |
public function __construct( Languages $languages, array $proxies, string $parent ) { |
| 45 |
$this->languages = $languages; |
| 46 |
$this->proxies = $proxies; |
| 47 |
$this->stack[] = $parent; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Returns the list of available languages after passing it through proxies. |
| 52 |
* |
| 53 |
* @since 3.8 |
| 54 |
* |
| 55 |
* @param array $args Optional arguments to pass to `Languages::get_list()`. |
| 56 |
* @return array List of `PLL_Language` objects or `PLL_Language` object properties. |
| 57 |
*/ |
| 58 |
public function get_list( array $args = array() ): array { |
| 59 |
$all_args = $args; |
| 60 |
unset( $args['fields'] ); |
| 61 |
|
| 62 |
$languages = $this->languages->get_list( $args ); |
| 63 |
|
| 64 |
foreach ( $this->stack as $key ) { |
| 65 |
if ( ! isset( $this->proxies[ $key ] ) ) { |
| 66 |
continue; |
| 67 |
} |
| 68 |
$languages = $this->proxies[ $key ]->filter( $languages ); |
| 69 |
} |
| 70 |
|
| 71 |
$languages = array_values( $languages ); // Re-index. |
| 72 |
|
| 73 |
return $this->languages->maybe_convert_list( $languages, $all_args ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Stacks a proxy that will filter the list of languages. |
| 78 |
* |
| 79 |
* @since 3.8 |
| 80 |
* |
| 81 |
* @param string $key Proxy's key. |
| 82 |
* @return Languages_Proxies |
| 83 |
*/ |
| 84 |
public function filter( string $key ): Languages_Proxies { |
| 85 |
$this->stack[] = $key; |
| 86 |
return $this; |
| 87 |
} |
| 88 |
} |
| 89 |
|