class-custom-sidebars-integration-polylang.php
8 years ago
class-custom-sidebars-integration-wml.php
7 years ago
class-custom-sidebars-integration-wpml.php
8 years ago
class-custom-sidebars-integration.php
8 years ago
class-custom-sidebars-integration-wml.php
115 lines
| 1 | <?php |
| 2 | require_once dirname( __FILE__ ).'/class-custom-sidebars-integration.php'; |
| 3 | add_action( 'cs_integrations', array( 'CustomSidebarsIntegrationWML', 'instance' ) ); |
| 4 | /** |
| 5 | * Integrate sidebar locations with WML |
| 6 | * |
| 7 | * @since 3.2.0 |
| 8 | */ |
| 9 | class CustomSidebarsIntegrationWML extends CustomSidebarsIntegration { |
| 10 | |
| 11 | /** |
| 12 | * Returns the singleton object. |
| 13 | * |
| 14 | * @since 3.2.0 |
| 15 | */ |
| 16 | public static function instance() { |
| 17 | static $instance = null; |
| 18 | if ( null === $instance ) { |
| 19 | $instance = new CustomSidebarsIntegrationWML(); |
| 20 | } |
| 21 | return $instance; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Constructor is private -> singleton. |
| 26 | * |
| 27 | * @since 3.2.0 |
| 28 | */ |
| 29 | private function __construct() { |
| 30 | if ( ! function_exists( 'wpm_get_languages' ) ) { |
| 31 | return; |
| 32 | } |
| 33 | $languages = wpm_get_languages(); |
| 34 | if ( empty( $languages ) ) { |
| 35 | return; |
| 36 | } |
| 37 | $this->key_name = 'wml'; |
| 38 | $this->languages = $languages; |
| 39 | add_filter( 'custom_sidebars_integrations', array( $this, 'prepare' ) ); |
| 40 | add_filter( 'custom_sidebars_get_location', array( $this, 'get_location' ), 10, 2 ); |
| 41 | add_filter( 'custom_sidebars_set_location', array( $this, 'set_location' ), 10, 4 ); |
| 42 | add_filter( 'cs_replace_sidebars', array( $this, 'replace' ), 10, 2 ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Save dismiss decision, no more show it. |
| 47 | * |
| 48 | * @since 3.2.0 |
| 49 | */ |
| 50 | public function prepare( $tabs ) { |
| 51 | $tabs[ $this->key_name ] = array( |
| 52 | 'title' => __( 'WP Multilang', 'custom-sidebars' ), |
| 53 | 'cat_name' => __( 'Language', 'custom-sidebars' ), |
| 54 | ); |
| 55 | return $tabs; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Add languages |
| 60 | * |
| 61 | * @since 3.2.0 |
| 62 | */ |
| 63 | public function get_location( $req, $defaults ) { |
| 64 | $req->wml = array(); |
| 65 | foreach ( $this->languages as $key => $lang ) { |
| 66 | $req->wml[ $key ] = array( |
| 67 | 'name' => isset( $lang['name'] )? $lang['name'] : '', |
| 68 | 'native' => isset( $lang['name'] )? $lang['name'] : '', |
| 69 | 'archive' => array(), |
| 70 | ); |
| 71 | if ( |
| 72 | isset( $defaults[ $this->key_name ] ) |
| 73 | && isset( $defaults[ $this->key_name ][ $key ] ) |
| 74 | ) { |
| 75 | $req->wml[ $key ]['archive'] = $defaults[ $this->key_name ][ $key ]; |
| 76 | } |
| 77 | } |
| 78 | return $req; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Replace sidebar |
| 83 | * |
| 84 | * @since 3.2.0 |
| 85 | */ |
| 86 | public function replace( $replacements, $options ) { |
| 87 | if ( ! isset( $options[ $this->key_name ] ) ) { |
| 88 | return $replacements; |
| 89 | } |
| 90 | if ( ! function_exists( 'wpm_get_language' ) ) { |
| 91 | return; |
| 92 | } |
| 93 | $current_language = wpm_get_language(); |
| 94 | if ( empty( $current_language ) ) { |
| 95 | return $replacements; |
| 96 | } |
| 97 | foreach ( $replacements as $sb_id => $replacement ) { |
| 98 | if ( ! empty( $replacement ) ) { |
| 99 | continue; |
| 100 | } |
| 101 | if ( |
| 102 | isset( $options[ $this->key_name ][ $current_language ] ) |
| 103 | && isset( $options[ $this->key_name ][ $current_language ][ $sb_id ] ) |
| 104 | ) { |
| 105 | $replacements[ $sb_id ] = array( |
| 106 | $options[ $this->key_name ][ $current_language ][ $sb_id ], |
| 107 | $this->key_name, |
| 108 | $current_language, |
| 109 | ); |
| 110 | } |
| 111 | } |
| 112 | return $replacements; |
| 113 | } |
| 114 | }; |
| 115 |