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-polylang.php
136 lines
| 1 | <?php |
| 2 | require_once dirname( __FILE__ ).'/class-custom-sidebars-integration.php'; |
| 3 | add_action( 'cs_integrations', array( 'CustomSidebarsIntegrationPolylang', 'instance' ) ); |
| 4 | /** |
| 5 | * Integrate sidebar locations with Polylang |
| 6 | * |
| 7 | * @since 3.1.2 |
| 8 | */ |
| 9 | class CustomSidebarsIntegrationPolylang 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 CustomSidebarsIntegrationPolylang(); |
| 20 | } |
| 21 | return $instance; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Constructor is private -> singleton. |
| 26 | * |
| 27 | * @since 3.1.2 |
| 28 | */ |
| 29 | private function __construct() { |
| 30 | if ( ! defined( 'POLYLANG_VERSION' ) ) { |
| 31 | return; |
| 32 | } |
| 33 | $this->key_name = 'polylang'; |
| 34 | add_filter( 'custom_sidebars_integrations', array( $this, 'prepare' ) ); |
| 35 | add_filter( 'custom_sidebars_get_location', array( $this, 'get_location' ), 10, 2 ); |
| 36 | add_filter( 'custom_sidebars_set_location', array( $this, 'set_location' ), 10, 4 ); |
| 37 | add_filter( 'cs_replace_sidebars', array( $this, 'replace' ), 10, 2 ); |
| 38 | } |
| 39 | |
| 40 | private function check() { |
| 41 | if ( ! function_exists( 'pll_the_languages' ) ) { |
| 42 | return false; |
| 43 | } |
| 44 | if ( ! empty( $this->languages ) ) { |
| 45 | return true; |
| 46 | } |
| 47 | $args = array( |
| 48 | 'raw' => true, |
| 49 | 'hide_if_empty' => false, |
| 50 | ); |
| 51 | $languages = pll_the_languages( $args ); |
| 52 | if ( empty( $languages ) ) { |
| 53 | return false; |
| 54 | } |
| 55 | $this->languages = $languages; |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Save dismiss decision, no more show it. |
| 61 | * |
| 62 | * @since 3.1.2 |
| 63 | */ |
| 64 | public function prepare( $tabs ) { |
| 65 | $tabs[ $this->key_name ] = array( |
| 66 | 'title' => __( 'Polylang', 'custom-sidebars' ), |
| 67 | 'cat_name' => __( 'Language', 'custom-sidebars' ), |
| 68 | ); |
| 69 | return $tabs; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Add languages |
| 74 | * |
| 75 | * @since 3.1.2 |
| 76 | */ |
| 77 | public function get_location( $req, $defaults ) { |
| 78 | $check = $this->check(); |
| 79 | if ( ! $check ) { |
| 80 | return $req; |
| 81 | } |
| 82 | $req->polylang = array(); |
| 83 | foreach ( $this->languages as $key => $lang ) { |
| 84 | $req->polylang[ $key ] = array( |
| 85 | 'name' => $lang['name'], |
| 86 | 'archive' => array(), |
| 87 | ); |
| 88 | if ( |
| 89 | isset( $defaults[ $this->key_name ] ) |
| 90 | && isset( $defaults[ $this->key_name ][ $key ] ) |
| 91 | ) { |
| 92 | $req->polylang[ $key ]['archive'] = $defaults[ $this->key_name ][ $key ]; |
| 93 | } |
| 94 | } |
| 95 | return $req; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Replace sidebar |
| 100 | * |
| 101 | * @since 3.1.2 |
| 102 | */ |
| 103 | public function replace( $replacements, $options ) { |
| 104 | $check = $this->check(); |
| 105 | if ( ! $check ) { |
| 106 | return $replacements; |
| 107 | } |
| 108 | if ( ! isset( $options[ $this->key_name ] ) ) { |
| 109 | return $replacements; |
| 110 | } |
| 111 | if ( ! function_exists( 'pll_current_language' ) ) { |
| 112 | return $replacements; |
| 113 | } |
| 114 | $current_language = pll_current_language(); |
| 115 | if ( empty( $current_language ) ) { |
| 116 | return $replacements; |
| 117 | } |
| 118 | foreach ( $replacements as $sb_id => $replacement ) { |
| 119 | if ( ! empty( $replacement ) ) { |
| 120 | continue; |
| 121 | } |
| 122 | if ( |
| 123 | isset( $options[ $this->key_name ][ $current_language ] ) |
| 124 | && isset( $options[ $this->key_name ][ $current_language ][ $sb_id ] ) |
| 125 | ) { |
| 126 | $replacements[ $sb_id ] = array( |
| 127 | $options[ $this->key_name ][ $current_language ][ $sb_id ], |
| 128 | $this->key_name, |
| 129 | $current_language, |
| 130 | ); |
| 131 | } |
| 132 | } |
| 133 | return $replacements; |
| 134 | } |
| 135 | }; |
| 136 |