From 432c0fa01ae8cf8103b02e33401d0d9df4a82166 Mon Sep 17 00:00:00 2001 From: champsupertramp Date: Sun, 20 Mar 2016 20:02:06 +0800 Subject: [PATCH] Fix WPML compatibility --- core/um-filters-language.php | 42 ++++++++++++++++++++++++++++++++++++ core/um-permalinks.php | 20 +---------------- core/um-query.php | 16 ++++++++------ core/um-short-functions.php | 15 +++++++++++++ um-config.php | 34 ++++++++++++++++------------- um-init.php | 1 + 6 files changed, 88 insertions(+), 40 deletions(-) create mode 100644 core/um-filters-language.php diff --git a/core/um-filters-language.php b/core/um-filters-language.php new file mode 100644 index 00000000..a630de4c --- /dev/null +++ b/core/um-filters-language.php @@ -0,0 +1,42 @@ + 0 && get_post_meta( get_the_ID(), '_um_wpml_user', true ) == 1 ) { + $profile_url = get_permalink( get_the_ID() ); + } + } + + // WPML compatibility + if ( function_exists('icl_object_id') ) { + $language_code = ICL_LANGUAGE_CODE; + $lang_post_id = icl_object_id( $page_id , 'page', true, $language_code ); + + if($lang_post_id != 0) { + $profile_url = get_permalink( $lang_post_id ); + }else { + // No page found, it's most likely the homepage + global $sitepress; + $profile_url = $sitepress->language_url( $language_code ); + } + + } + + return $profile_url; + +} + +add_filter('um_core_page_id_filter','um_core_page_id_filter'); +function um_core_page_id_filter( $page_id ){ + +/* if( ! empty( $_GET['lang'] ) && isset( $_GET['lang'] ) ){ + $page_id = $page_id.'_'.$_GET['lang']; + } +*/ + return $page_id; +} \ No newline at end of file diff --git a/core/um-permalinks.php b/core/um-permalinks.php index 2529c16f..8d4b55fb 100644 --- a/core/um-permalinks.php +++ b/core/um-permalinks.php @@ -191,28 +191,10 @@ class UM_Permalinks { $page_id = $this->core['user']; $profile_url = get_permalink( $page_id ); - if ( function_exists('icl_get_current_language') && icl_get_current_language() != icl_get_default_language() ) { - if ( get_the_ID() > 0 && get_post_meta( get_the_ID(), '_um_wpml_user', true ) == 1 ) { - $profile_url = get_permalink( get_the_ID() ); - } - } - // WPML compatibility - if ( function_exists('icl_object_id') ) { + $profile_url = apply_filters('um_localize_permalink_filter', $this->core, $page_id, $profile_url ); - $language_code = ICL_LANGUAGE_CODE; - $lang_post_id = icl_object_id( $page_id , 'page', true, $language_code ); - - if($lang_post_id != 0) { - $profile_url = get_permalink( $lang_post_id ); - }else { - // No page found, it's most likely the homepage - global $sitepress; - $profile_url = $sitepress->language_url( $language ); - } - } - if ( um_get_option('permalink_base') == 'user_login' ) { $user_in_url = um_user('user_login'); diff --git a/core/um-query.php b/core/um-query.php index 1c256de2..a4960d16 100644 --- a/core/um-query.php +++ b/core/um-query.php @@ -12,16 +12,20 @@ class UM_Query { *** @get wp pages ***/ function wp_pages() { - $count_pages = wp_count_posts('page'); + global $wpdb; + $pages = $wpdb->get_results('SELECT * FROM '.$wpdb->posts.' WHERE post_type = "page" AND post_status = "publish" ', OBJECT); + $count_pages = $wpdb->num_rows; - if ( $count_pages->publish > 300 ) - return; + if ( $count_pages > 300 ) + return 'reached_maximum_limit'; - $pages = get_pages(); $array = ''; - foreach ($pages as $page_data) { - $array[ $page_data->ID ] = $page_data->post_title; + if( $wpdb->num_rows > 0 ){ + foreach ($pages as $page_data) { + $array[ $page_data->ID ] = $page_data->post_title; + } } + return $array; } diff --git a/core/um-short-functions.php b/core/um-short-functions.php index 13f317e8..3d81cc3f 100644 --- a/core/um-short-functions.php +++ b/core/um-short-functions.php @@ -1567,3 +1567,18 @@ function um_fetch_user( $user_id ) { function um_search_form() { echo um_get_search_form(); } + + /** + * Get localization + * @return string + */ + function um_get_locale(){ + + $lang_code = get_locale(); + + if( strpos( $lang_code , 'en_' ) > -1 || empty( $lang_code ) || $lang_code == 0 ){ + return 'en'; + } + + return $lang_code; + } diff --git a/um-config.php b/um-config.php index 7df81853..8e7169be 100644 --- a/um-config.php +++ b/um-config.php @@ -23,27 +23,31 @@ foreach( $core_pages as $page_s => $page ) { $have_pages = $ultimatemember->query->wp_pages(); - if( ! empty( $have_pages ) ){ - $page_setup[] = array( - 'id' => 'core_' . $page_s, - 'type' => 'select', - 'select2' => array( 'allowClear' => 0, 'minimumResultsForSearch' => -1 ), - 'title' => $page, - 'default' => ( isset( $ultimatemember->permalinks->core[ $page_s ] ) ) ? $ultimatemember->permalinks->core[ $page_s ] : '' , - 'options' => $ultimatemember->query->wp_pages(), - 'placeholder' => __('Choose a page...','ultimatemember'), - 'compiler' => true, - ); - }else{ - $page_setup[] = array( - 'id' => 'core_' . $page_s, + $page_id = 'core_' . $page_s; + $page_id = apply_filters('um_core_page_id_filter', $page_id ); + + if( 'reached_maximum_limit' == $have_pages ){ + $page_setup[] = array( + 'id' => $page_id, 'type' => 'text', 'title' => $page, 'placeholder' => __('Add page ID','ultimatemember'), - 'default' => ( isset( $ultimatemember->permalinks->core[ $page_s ] ) ) ? $ultimatemember->permalinks->core[ $page_s ] : '', + 'default' => ( isset( $ultimatemember->permalinks->core[ $page_id ] ) ) ? $ultimatemember->permalinks->core[ $page_id ] : '', 'compiler' => true, ); + }else{ + $page_setup[] = array( + 'id' => $page_id, + 'type' => 'select', + 'select2' => array( 'allowClear' => 0, 'minimumResultsForSearch' => -1 ), + 'title' => $page, + 'default' => ( isset( $ultimatemember->permalinks->core[ $page_id ] ) ) ? $ultimatemember->permalinks->core[ $page_id ] : '' , + 'options' => $ultimatemember->query->wp_pages(), + 'placeholder' => __('Choose a page...','ultimatemember'), + 'compiler' => true, + ); } + } $this->sections[] = array( diff --git a/um-init.php b/um-init.php index 5ad179ad..39305320 100644 --- a/um-init.php +++ b/um-init.php @@ -142,6 +142,7 @@ class UM_API { require_once um_path . 'core/um-actions-modal.php'; require_once um_path . 'core/um-actions-misc.php'; + require_once um_path . 'core/um-filters-language.php'; require_once um_path . 'core/um-filters-login.php'; require_once um_path . 'core/um-filters-fields.php'; require_once um_path . 'core/um-filters-files.php';