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