providers
3 years ago
class-fast-image.php
8 years ago
class-folders-walker.php
7 years ago
class-folders.php
3 years ago
class-frontend.php
3 years ago
class-galleries.php
3 years ago
class-multilang.php
3 years ago
class-remote-library-api.php
3 years ago
class-remote-library.php
3 years ago
class-settings.php
3 years ago
class-tour.php
3 years ago
class-welcome.php
3 years ago
class-widgets.php
3 years ago
functions.php
3 years ago
class-multilang.php
321 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) |
| 3 | exit; |
| 4 | |
| 5 | new Responsive_Lightbox_Multilang(); |
| 6 | |
| 7 | /** |
| 8 | * Responsive Lightbox Multilang class. |
| 9 | * |
| 10 | * @class Responsive_Lightbox_Multilang |
| 11 | */ |
| 12 | class Responsive_Lightbox_Multilang { |
| 13 | public $multilang = false; |
| 14 | public $languages = array(); |
| 15 | public $default_lang = ''; |
| 16 | public $current_lang = ''; |
| 17 | public $active_plugin = ''; |
| 18 | |
| 19 | /** |
| 20 | * Class constructor. |
| 21 | * |
| 22 | * @global object $sitepress |
| 23 | * |
| 24 | * @return void |
| 25 | */ |
| 26 | public function __construct() { |
| 27 | // set instance |
| 28 | Responsive_Lightbox()->multilang = $this; |
| 29 | |
| 30 | // check if WPML or Polylang is active |
| 31 | include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 32 | |
| 33 | // Polylang support |
| 34 | if ( ( is_plugin_active( 'polylang/polylang.php' ) || is_plugin_active( 'polylang-pro/polylang.php' ) ) && function_exists( 'PLL' ) ) { |
| 35 | $this->multilang = true; |
| 36 | $this->active_plugin = 'polylang'; |
| 37 | |
| 38 | // get registered languages |
| 39 | $registered_languages = PLL()->model->get_languages_list(); |
| 40 | |
| 41 | if ( ! empty( $registered_languages ) ) { |
| 42 | foreach ( $registered_languages as $language ) |
| 43 | $this->languages[$language->slug] = $language->name; |
| 44 | } |
| 45 | |
| 46 | // get default language |
| 47 | $this->default_lang = pll_default_language(); |
| 48 | |
| 49 | // filters |
| 50 | add_filter( 'rl_count_attachments', array( $this, 'count_attachments' ), 9 ); |
| 51 | // WPML support |
| 52 | } elseif ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) && class_exists( 'SitePress' ) ) { |
| 53 | $this->multilang = true; |
| 54 | $this->active_plugin = 'wpml'; |
| 55 | |
| 56 | global $sitepress; |
| 57 | |
| 58 | // get registered languages |
| 59 | $registered_languages = icl_get_languages(); |
| 60 | |
| 61 | if ( ! empty( $registered_languages ) ) { |
| 62 | foreach ( $registered_languages as $language ) |
| 63 | $this->languages[$language['code']] = $language['display_name']; |
| 64 | } |
| 65 | |
| 66 | // get default language |
| 67 | $this->default_lang = $sitepress->get_default_language(); |
| 68 | |
| 69 | // actions |
| 70 | add_action( 'admin_init', array( $this, 'hide_thumbnail' ) ); |
| 71 | } |
| 72 | |
| 73 | // multilang? |
| 74 | if ( $this->multilang ) { |
| 75 | // ations |
| 76 | add_action( 'admin_init', array( $this, 'media_url_redirect' ) ); |
| 77 | |
| 78 | // filters |
| 79 | add_filter( 'setup_theme', array( $this, 'get_current_admin_language' ), 11 ); |
| 80 | add_filter( 'rl_root_folder_query_args', array( $this, 'root_folder_query_args' ) ); |
| 81 | add_filter( 'rl_gallery_query_args', array( $this, 'gallery_featured_query_args' ) ); |
| 82 | add_filter( 'rl_folders_query_args', array( $this, 'gallery_folders_query_args' ) ); |
| 83 | add_filter( 'rl_get_gallery_images_attachments', array( $this, 'update_gallery_images_attachments' ) ); |
| 84 | add_filter( 'rl_folders_media_folder_url', array( $this, 'media_folder_url' ) ); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Avoid duplicating hidden internal thumbnail. |
| 90 | * |
| 91 | * @return void |
| 92 | */ |
| 93 | public function hide_thumbnail() { |
| 94 | // get thumbnail |
| 95 | $thumbnail_id = Responsive_Lightbox()->galleries->maybe_generate_thumbnail(); |
| 96 | |
| 97 | add_post_meta( $thumbnail_id, 'wpml_media_processed', 1, true ); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Get current admin language. |
| 102 | * |
| 103 | * @return void |
| 104 | */ |
| 105 | public function get_current_admin_language() { |
| 106 | if ( $this->active_plugin === 'polylang' ) |
| 107 | $this->current_lang = (string) pll_current_language( 'slug' ); |
| 108 | else |
| 109 | $this->current_lang = ICL_LANGUAGE_CODE === 'all' ? '' : ICL_LANGUAGE_CODE; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Update gallery attachments. |
| 114 | * |
| 115 | * @param array $attachments Attachment IDs |
| 116 | * @return array |
| 117 | */ |
| 118 | public function update_gallery_images_attachments( $attachments ) { |
| 119 | $new_attachments = array(); |
| 120 | |
| 121 | foreach ( $attachments as $attachment_id ) { |
| 122 | if ( $this->active_plugin === 'polylang' ) |
| 123 | $new_attachments[] = pll_get_post( $attachment_id, $this->current_lang ); |
| 124 | else |
| 125 | $new_attachments[] = (int) apply_filters( 'wpml_object_id', $attachment_id, 'attachment', true, $this->current_lang ); |
| 126 | } |
| 127 | |
| 128 | return $attachments; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Root folder WP Query arguments. |
| 133 | * |
| 134 | * @param array args |
| 135 | * @return void |
| 136 | */ |
| 137 | public function root_folder_query_args( $args ) { |
| 138 | $args['lang'] = $this->current_lang; |
| 139 | |
| 140 | return $args; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Get the number of attachments per language. |
| 145 | * Based on count_posts function from Polylang plugin (/include/model.php) |
| 146 | * |
| 147 | * @global object $wpdb |
| 148 | * |
| 149 | * @param int number |
| 150 | * @return int |
| 151 | */ |
| 152 | public function count_attachments( $number ) { |
| 153 | // active language? |
| 154 | if ( $this->current_lang !== '' ) { |
| 155 | // remove internal WP counter to avoid unwanted query |
| 156 | remove_filter( 'rl_count_attachments', array( Responsive_Lightbox()->folders, 'count_attachments' ), 10 ); |
| 157 | // if not let internal WP counter do the job |
| 158 | } else |
| 159 | return $number; |
| 160 | |
| 161 | // get taxonomies |
| 162 | $taxonomies = PLL()->model->get_filtered_taxonomies_query_vars(); |
| 163 | |
| 164 | // prepare defaults |
| 165 | $defaults = array( |
| 166 | 'author' => '', |
| 167 | 'author_name' => '', |
| 168 | 'monthnum' => '', |
| 169 | 'day' => '', |
| 170 | 'year' => '', |
| 171 | 'm' => '' |
| 172 | ); |
| 173 | |
| 174 | // add additional taxonomies |
| 175 | foreach ( $taxonomies as $tax ) { |
| 176 | $defaults[$tax] = ''; |
| 177 | } |
| 178 | |
| 179 | // prepare data |
| 180 | $args = array_intersect_key( array_merge( $defaults, wp_unslash( $_REQUEST ) ), $defaults ); |
| 181 | |
| 182 | global $wpdb; |
| 183 | |
| 184 | $select = "SELECT pll_tr.term_taxonomy_id, COUNT( * ) AS count FROM " . $wpdb->posts; |
| 185 | $join = PLL()->model->post->join_clause(); |
| 186 | $where = " WHERE post_status = 'inherit'"; |
| 187 | $where .= " AND " . $wpdb->posts . ".post_type = 'attachment'"; |
| 188 | $where .= PLL()->model->post->where_clause( $this->current_lang ); |
| 189 | $groupby = ' GROUP BY pll_tr.term_taxonomy_id'; |
| 190 | |
| 191 | if ( ! empty( $args['m'] ) ) { |
| 192 | $args['m'] = '' . preg_replace( '|[^0-9]|', '', $args['m'] ); |
| 193 | $where .= $wpdb->prepare( " AND YEAR( " . $wpdb->posts . ".post_date ) = %d", substr( $args['m'], 0, 4 ) ); |
| 194 | |
| 195 | if ( strlen( $args['m'] ) > 5 ) |
| 196 | $where .= $wpdb->prepare( " AND MONTH( " . $wpdb->posts . ".post_date ) = %d", substr( $args['m'], 4, 2 ) ); |
| 197 | |
| 198 | if ( strlen( $args['m'] ) > 7 ) |
| 199 | $where .= $wpdb->prepare( " AND DAYOFMONTH( " . $wpdb->posts . ".post_date ) = %d", substr( $args['m'], 6, 2 ) ); |
| 200 | } |
| 201 | |
| 202 | if ( ! empty( $args['year'] ) ) |
| 203 | $where .= $wpdb->prepare( " AND YEAR( " . $wpdb->posts . ".post_date ) = %d", $args['year'] ); |
| 204 | |
| 205 | if ( ! empty( $args['monthnum'] ) ) |
| 206 | $where .= $wpdb->prepare( " AND MONTH( " . $wpdb->posts . ".post_date ) = %d", $args['monthnum'] ); |
| 207 | |
| 208 | if ( ! empty( $args['day'] ) ) |
| 209 | $where .= $wpdb->prepare( " AND DAYOFMONTH( " . $wpdb->posts . ".post_date ) = %d", $args['day'] ); |
| 210 | |
| 211 | if ( ! empty( $args['author_name'] ) ) { |
| 212 | $author = get_user_by( 'slug', sanitize_title_for_query( $args['author_name'] ) ); |
| 213 | |
| 214 | if ( $author ) |
| 215 | $args['author'] = $author->ID; |
| 216 | } |
| 217 | |
| 218 | if ( ! empty( $args['author'] ) ) |
| 219 | $where .= $wpdb->prepare( " AND " . $wpdb->posts . ".post_author = %d", $args['author'] ); |
| 220 | |
| 221 | // filtered taxonomies ( post_format ) |
| 222 | foreach ( $taxonomies as $tax_qv ) { |
| 223 | if ( ! empty( $args[ $tax_qv ] ) ) { |
| 224 | $join .= " INNER JOIN " . $wpdb->term_relationships . " AS tr ON tr.object_id = " . $wpdb->posts . ".ID"; |
| 225 | $join .= " INNER JOIN " . $wpdb->term_taxonomy . " AS tt ON tt.term_taxonomy_id = tr.term_taxonomy_id"; |
| 226 | $join .= " INNER JOIN " . $wpdb->terms . " AS t ON t.term_id = tt.term_id"; |
| 227 | $where .= $wpdb->prepare( ' AND t.slug = %s', $args[ $tax_qv ] ); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // get result |
| 232 | $result = $wpdb->get_row( $select . $join . $where . $groupby, ARRAY_A ); |
| 233 | |
| 234 | return empty( $result['count'] ) ? 0 : (int) $result['count']; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Featured gallery query arguments. |
| 239 | * |
| 240 | * @param array $args |
| 241 | * @return array |
| 242 | */ |
| 243 | public function gallery_featured_query_args( $args ) { |
| 244 | // set active language |
| 245 | $args['lang'] = $this->current_lang; |
| 246 | |
| 247 | return $args; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Folders gallery query arguments. |
| 252 | * |
| 253 | * @param array $args |
| 254 | * @return array |
| 255 | */ |
| 256 | public function gallery_folders_query_args( $args ) { |
| 257 | // set active language |
| 258 | $args['lang'] = $this->current_lang; |
| 259 | |
| 260 | return $args; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Update media folders URLs. |
| 265 | * |
| 266 | * @param string $url |
| 267 | * @return string |
| 268 | */ |
| 269 | public function media_folder_url( $url ) { |
| 270 | // active language? |
| 271 | if ( $this->current_lang !== '' ) |
| 272 | $url = add_query_arg( 'lang', $this->current_lang, $url ); |
| 273 | |
| 274 | return $url; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Redirect to equivalent media folder in specified language. |
| 279 | * |
| 280 | * @global string $pagenow |
| 281 | * |
| 282 | * @return void |
| 283 | */ |
| 284 | public function media_url_redirect() { |
| 285 | global $pagenow; |
| 286 | |
| 287 | // get main instance |
| 288 | $rl = Responsive_Lightbox(); |
| 289 | |
| 290 | // only for media with selected language |
| 291 | if ( $pagenow === 'upload.php' && $this->current_lang !== '' && $rl->options['folders']['active'] ) { |
| 292 | // get taxonomy |
| 293 | $taxonomy = $rl->options['folders']['media_taxonomy']; |
| 294 | |
| 295 | // parse URL |
| 296 | $params = parse_url( html_entity_decode( urldecode( add_query_arg( '', '' ) ) ) ); |
| 297 | |
| 298 | if ( isset( $params['query'] ) ) { |
| 299 | // parse query string |
| 300 | parse_str( $params['query'], $args ); |
| 301 | |
| 302 | if ( isset( $args['lang'], $args[$taxonomy] ) ) { |
| 303 | // cast term ID |
| 304 | $term_id = (int) $args[$taxonomy]; |
| 305 | |
| 306 | if ( $this->active_plugin === 'polylang' ) |
| 307 | $new_term_id = pll_get_term( $term_id, $args['lang'] ); |
| 308 | else |
| 309 | $new_term_id = apply_filters( 'wpml_object_id', $term_id, $taxonomy, true, $args['lang'] ); |
| 310 | |
| 311 | // different ID? |
| 312 | if ( $term_id !== $new_term_id ) { |
| 313 | wp_safe_redirect( add_query_arg( $taxonomy, $new_term_id ) ); |
| 314 | |
| 315 | exit; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | } |