From 7dce96c8b0d618011a676f8d3873de5f178f59a8 Mon Sep 17 00:00:00 2001 From: umdevelopera Date: Wed, 26 Jul 2023 23:31:17 +0300 Subject: [PATCH 1/5] - fixed Fatal error that occurs is Polylang can not get needed language --- includes/core/class-fields.php | 17 +++++++++-------- includes/core/class-mail.php | 18 +++++------------- includes/core/class-um-polylang.php | 14 +++++++++++++- readme.txt | 6 +++--- um-polylang.php | 8 ++++---- 5 files changed, 34 insertions(+), 29 deletions(-) diff --git a/includes/core/class-fields.php b/includes/core/class-fields.php index f3c7a96..5d59961 100644 --- a/includes/core/class-fields.php +++ b/includes/core/class-fields.php @@ -29,6 +29,7 @@ public function __construct() { add_filter( 'um_profile_bio_key', array( &$this, 'profile_bio_key' ), 20, 2 ); } + /** * Get translated biography key * @@ -41,7 +42,7 @@ public function __construct() { */ public function profile_bio_key( $key, $args ) { if ( 'description' === $key ) { - $curlang_slug = pll_current_language(); + $curlang_slug = UM()->Polylang()->get_current(); $curlang_key = 'description_' . $curlang_slug; if ( um_profile( $curlang_key ) || UM()->fields()->editing ) { $key = $curlang_key; @@ -68,7 +69,7 @@ public function profile_bio_value( $value, $data, $key = null ) { $key = $data['metakey']; } if ( 'description' === $key ) { - $curlang_slug = pll_current_language(); + $curlang_slug = UM()->Polylang()->get_current(); $curlang_key = 'description_' . $curlang_slug; if ( um_profile( $curlang_key ) ) { $value = um_profile( $curlang_key ); @@ -83,22 +84,22 @@ public function profile_bio_value( $value, $data, $key = null ) { /** * Save translated biography * - * @since 2.1.7 - * @hook um_after_user_updated + * @since 1.0.0 + * @hook um_after_user_updated * * @param integer $user_id User ID. * @param array $args Form Data. */ public function profile_bio_update( $user_id, $args ) { - $curlang_slug = pll_current_language(); + $curlang_slug = UM()->Polylang()->get_current(); $curlang_key = 'description_' . $curlang_slug; if ( isset( $args[ $curlang_key ] ) ) { update_user_meta( $user_id, $curlang_key, $args[ $curlang_key ] ); - if ( pll_default_language() === $curlang_slug ) { + if ( UM()->Polylang()->is_default() ) { update_user_meta( $user_id, 'description', $args[ $curlang_key ] ); } - } elseif ( isset( $args[ 'description' ] ) ) { - update_user_meta( $user_id, 'description', $args[ 'description' ] ); + } elseif ( isset( $args['description'] ) ) { + update_user_meta( $user_id, $curlang_key, $args['description'] ); } } diff --git a/includes/core/class-mail.php b/includes/core/class-mail.php index 0e19b1e..edbbcd3 100644 --- a/includes/core/class-mail.php +++ b/includes/core/class-mail.php @@ -119,9 +119,6 @@ public function email_table_columns( $columns ) { $flags_column = ''; foreach ( $languages as $language ) { -// if ( UM()->Polylang()->get_current() === $language ) { -// continue; -// } $language = $polylang->model->get_language( $language ); $flags_column .= '' . $language->flag . ''; } @@ -156,9 +153,6 @@ public function email_table_items( $email_notifications ) { foreach ( $email_notifications as &$email_notification ) { $email_notification['pll_translations'] = ''; foreach ( $languages as $language ) { -// if ( UM()->Polylang()->get_current() === $language ) { -// continue; -// } $email_notification['pll_translations'] .= $this->email_table_cell_pll_translations( $email_notification['key'], $language ); } } @@ -182,11 +176,7 @@ public function email_table_cell_pll_translations( $template, $code ) { $language = $polylang->model->get_language( $code ); $default = pll_default_language(); - - $lang = ''; - if ( $code !== $default ) { - $lang = $language->locale . '/'; - } + $lang = $code === $default ? '' : trailingslashit( $code ); // theme location. $template_path = trailingslashit( get_stylesheet_directory() . '/ultimate-member/email' ) . $lang . $template . '.php'; @@ -203,10 +193,12 @@ public function email_table_cell_pll_translations( $template, $code ) { ) ); + $language_name = is_object( $language ) ? $language->name : $code; + if ( file_exists( $template_path ) ) { // translators: %s - language name. - $hint = sprintf( __( 'Edit the translation in %s', 'polylang' ), $language->name ); + $hint = sprintf( __( 'Edit the translation in %s', 'polylang' ), $language_name ); $icon_html = sprintf( '%3$s', esc_url( $link ), @@ -216,7 +208,7 @@ public function email_table_cell_pll_translations( $template, $code ) { } else { // translators: %s - language name. - $hint = sprintf( __( 'Add a translation in %s', 'polylang' ), $language->name ); + $hint = sprintf( __( 'Add a translation in %s', 'polylang' ), $language_name ); $icon_html = sprintf( '%3$s', esc_url( $link ), diff --git a/includes/core/class-um-polylang.php b/includes/core/class-um-polylang.php index ff0f59f..d7c879b 100644 --- a/includes/core/class-um-polylang.php +++ b/includes/core/class-um-polylang.php @@ -124,7 +124,7 @@ public function get_current( $field = 'slug' ) { } $language = $polylang->model->get_language( $lang ); - return $language->get_prop( $field ); + return is_object( $language ) ? $language->get_prop( $field ) : $lang; } @@ -141,6 +141,18 @@ public function get_default( $field = 'slug' ) { } + /** + * Returns the list of available languages. + * + * @since 1.0.3 + * + * @return array + */ + public function get_languages_list() { + return pll_languages_list(); + } + + /** * Check if Polylang is active. * diff --git a/readme.txt b/readme.txt index b47fa26..1752377 100644 --- a/readme.txt +++ b/readme.txt @@ -4,11 +4,11 @@ Author URI: https://github.com/umdevelopera Plugin URI: https://github.com/umdevelopera/um-polylang Tags: ultimate member, polylang, multilingual Requires at least: 6.0 -Tested up to: 6.2.2 -Stable tag: 1.0.0 +Tested up to: 6.3 +Stable tag: 1.0.2 License: GNU Version 2 or Any Later Version License URI: http://www.gnu.org/licenses/gpl-3.0.txt -Requires UM core at least: 2.5.0 +Requires UM core at least: 2.6.7 == Description == diff --git a/um-polylang.php b/um-polylang.php index b8d7b82..916f490 100644 --- a/um-polylang.php +++ b/um-polylang.php @@ -3,12 +3,12 @@ Plugin Name: Ultimate Member - Polylang Plugin URI: https://github.com/umdevelopera/um-polylang Description: Integrates Ultimate Member with Polylang. - Version: 1.0.2 + Version: 1.0.3-alpha1 Author: umdevelopera Author URI: https://github.com/umdevelopera Text Domain: um-polylang Domain Path: /languages - UM version: 2.6.2 + UM version: 2.6.7 */ if ( ! defined( 'ABSPATH' ) ) { @@ -78,8 +78,8 @@ function um_polylang_init() { UM()->set_class( 'Polylang', true ); } } - add_action( 'plugins_loaded', 'um_polylang_init', 2, 1 ); + add_action( 'plugins_loaded', 'um_polylang_init', 4, 1 ); } } } -add_action( 'plugins_loaded', 'um_polylang_check_dependencies', -20 ); +add_action( 'plugins_loaded', 'um_polylang_check_dependencies', 2 ); From 57eac5e7c8e1e3d0cd18f8b3ee140d25e47b1654 Mon Sep 17 00:00:00 2001 From: umdevelopera Date: Wed, 22 Nov 2023 20:47:08 +0200 Subject: [PATCH 2/5] - fixed the "Class not found" error --- README.md | 16 ++++++++--- includes/core/class-mail.php | 43 +++++++++++++---------------- includes/core/class-permalinks.php | 10 +++---- includes/core/class-um-polylang.php | 17 ++++++------ readme.txt | 20 ++++++++------ um-polylang.php | 39 +++++++++++--------------- 6 files changed, 71 insertions(+), 74 deletions(-) diff --git a/README.md b/README.md index f34e867..eca1bed 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # Ultimate Member - Polylang -Integrates the **Ultimate Member** community plugin with the **Polylang** multilingual plugin. -__Note:__ This is a free extension created for the community. The Ultimate Member team does not provide any support for this extension. +Integrates the **Ultimate Member** community plugin with the **Polylang** multilingual plugin. ## Key features + - Ability to translate email templates. - Ability to translate bio (description) field in profile. - Proper permalinks for the Account and User (profile) pages. @@ -13,6 +13,7 @@ __Note:__ This is a free extension created for the community. The Ultimate Membe __Note:__ This plugin requires the [Ultimate Member](https://wordpress.org/plugins/ultimate-member/) and [Polylang](https://uk.wordpress.org/plugins/polylang/) plugins to be installed first. ### Clone from GitHub + Open git bash, navigate to the **plugins** folder and execute this command: `git clone --branch=main git@github.com:umdevelopera/um-polylang.git um-polylang` @@ -20,9 +21,11 @@ Open git bash, navigate to the **plugins** folder and execute this command: Once the plugin is cloned, enter your site admin dashboard and go to _wp-admin > Plugins > Installed Plugins_. Find the "Ultimate Member - Polylang" plugin and click the "Activate" link. ### Install from ZIP archive + You can install this plugin from the [ZIP archive](https://drive.google.com/file/d/175PVG6tLK7z1wcrAawFfQdTVIC071Eup/view) as any other plugin. Follow [this instruction](https://wordpress.org/support/article/managing-plugins/#upload-via-wordpress-admin). ## How to use + Go to *wp-admin > Ultimate Member > Settings > Email* to translate email templates. Click the "+" icon unter the flag to translate a template for the needed language. The plugin saves translated email templates to locale subfolders in the theme. See details [here](https://docs.ultimatemember.com/article/1335-email-templates). Go to *wp-admin > Pages* to translate pages Account, Login, Members, Password Reset, Register, User. Click the "+" icon unter the flag to translate a page for the needed language. See details [here](https://docs.ultimatemember.com/article/1449-how-to-translate-plugin#forms). @@ -31,7 +34,7 @@ Go to *wp-admin > Settings > Permalinks* and click the "Save Changes" button to __Note:__ The "Post name" permalink structure is recommended. -### Screenshots: +### Screenshots Image - Translate email templates. ![UM Settings, Email (polylang)](https://github.com/umdevelopera/um-polylang/assets/113178913/65d14995-257d-4311-a93a-8f944ea12ba9) @@ -42,7 +45,12 @@ Image - Translate pages. Image - Permalink settings. ![WP Settings, Permalink (default)](https://github.com/umdevelopera/um-polylang/assets/113178913/69be91c9-12dd-490c-9145-b163c5beb26d) -## Related links: +## Support + +This is a free extension created for the community. The Ultimate Member team does not provide any support for this extension. Open new [issue](https://github.com/umdevelopera/um-polylang/issues) if you face a problem. + +## Related links + Ultimate Member home page: https://ultimatemember.com/ Ultimate Member documentation: https://docs.ultimatemember.com/ diff --git a/includes/core/class-mail.php b/includes/core/class-mail.php index edbbcd3..37c4a03 100644 --- a/includes/core/class-mail.php +++ b/includes/core/class-mail.php @@ -40,6 +40,7 @@ public function __construct() { /** * Adding locale suffix to the "Subject Line" field. + * * Example: change 'welcome_email_sub' to 'welcome_email_sub_de_DE' * * @since 1.0.0 @@ -106,28 +107,26 @@ public function create_email_template_file( $settings ) { * * @since 1.0.0 * - * @global object $polylang The Polylang instance. - * * @param array $columns The Email table headers. * @return array */ public function email_table_columns( $columns ) { - global $polylang; - $languages = pll_languages_list(); + $languages = pll_languages_list(); if ( count( $languages ) > 0 ) { - $flags_column = ''; + $flags = ''; foreach ( $languages as $language ) { - $language = $polylang->model->get_language( $language ); - $flags_column .= '' . $language->flag . ''; + $language = PLL()->model->get_language( $language ); + $flag = is_object( $language ) ? $language->flag : $language; + $flags .= '' . $flag . ''; } $new_columns = array(); foreach ( $columns as $column_key => $column_content ) { $new_columns[ $column_key ] = $column_content; if ( 'email' === $column_key && ! isset( $new_columns['pll_translations'] ) ) { - $new_columns['pll_translations'] = $flags_column; + $new_columns['pll_translations'] = $flags; } } @@ -139,7 +138,6 @@ public function email_table_columns( $columns ) { /** - * * Add cell for the column 'translations' in the Email table. * * @since 1.0.0 @@ -153,36 +151,35 @@ public function email_table_items( $email_notifications ) { foreach ( $email_notifications as &$email_notification ) { $email_notification['pll_translations'] = ''; foreach ( $languages as $language ) { - $email_notification['pll_translations'] .= $this->email_table_cell_pll_translations( $email_notification['key'], $language ); + $email_notification['pll_translations'] .= $this->email_table_link( $email_notification['key'], $language ); } } return $email_notifications; } + /** - * Get content for the cell of the column 'translations' in the Email table. + * Get a link to Add/Edit email template for a certain language. * - * @since 2.1.6 - * - * @global object $polylang The Polylang instance. + * @since 1.0.0 + * @version 1.0.3 Use locale instead of the code in the template path. * * @param string $template The email template slug. * @param string $code Slug or locale of the queried language. * @return string */ - public function email_table_cell_pll_translations( $template, $code ) { - global $polylang; + public function email_table_link( $template, $code ) { - $language = $polylang->model->get_language( $code ); + $language = PLL()->model->get_language( $code ); $default = pll_default_language(); - $lang = $code === $default ? '' : trailingslashit( $code ); + $locale = $code === $default ? '' : trailingslashit( $language->get_prop( 'locale' ) ); // theme location. - $template_path = trailingslashit( get_stylesheet_directory() . '/ultimate-member/email' ) . $lang . $template . '.php'; + $template_path = trailingslashit( get_stylesheet_directory() . '/ultimate-member/email/' . $locale ) . $template . '.php'; // plugin location for default language. - if ( empty( $lang ) && ! file_exists( $template_path ) ) { + if ( empty( $locale ) && ! file_exists( $template_path ) ) { $template_path = UM()->mail()->get_template_file( 'plugin', $template ); } @@ -193,12 +190,10 @@ public function email_table_cell_pll_translations( $template, $code ) { ) ); - $language_name = is_object( $language ) ? $language->name : $code; - if ( file_exists( $template_path ) ) { // translators: %s - language name. - $hint = sprintf( __( 'Edit the translation in %s', 'polylang' ), $language_name ); + $hint = sprintf( __( 'Edit the translation in %s', 'polylang' ), $language->get_prop( 'name' ) ); $icon_html = sprintf( '%3$s', esc_url( $link ), @@ -208,7 +203,7 @@ public function email_table_cell_pll_translations( $template, $code ) { } else { // translators: %s - language name. - $hint = sprintf( __( 'Add a translation in %s', 'polylang' ), $language_name ); + $hint = sprintf( __( 'Add a translation in %s', 'polylang' ), $language->get_prop( 'name' ) ); $icon_html = sprintf( '%3$s', esc_url( $link ), diff --git a/includes/core/class-permalinks.php b/includes/core/class-permalinks.php index 82246fd..f0efb39 100644 --- a/includes/core/class-permalinks.php +++ b/includes/core/class-permalinks.php @@ -63,12 +63,10 @@ public function __construct() { * * @since 1.0.0 * - * @global object $polylang The Polylang instance. * @param array $rules Rewrite rules. * @return array */ public function add_rewrite_rules( $rules ) { - global $polylang; $languages = pll_languages_list(); $newrules = array(); @@ -79,7 +77,7 @@ public function add_rewrite_rules( $rules ) { $account = get_post( $account_page_id ); foreach ( $languages as $language ) { - if ( pll_default_language() === $language && $polylang->options['hide_default'] ) { + if ( pll_default_language() === $language && PLL()->options['hide_default'] ) { continue; } $lang_post_id = pll_get_post( $account_page_id, $language ); @@ -88,7 +86,7 @@ public function add_rewrite_rules( $rules ) { if ( isset( $account->post_name ) && isset( $lang_post_obj->post_name ) ) { $lang_page_slug = $lang_post_obj->post_name; - if ( 1 === $polylang->options['force_lang'] ) { + if ( 1 === PLL()->options['force_lang'] ) { $newrules[ $language . '/' . $lang_page_slug . '/([^/]+)/?$' ] = 'index.php?page_id=' . $lang_post_id . '&um_tab=$matches[1]&lang=' . $language; } @@ -103,7 +101,7 @@ public function add_rewrite_rules( $rules ) { $user = get_post( $user_page_id ); foreach ( $languages as $language ) { - if ( pll_default_language() === $language && $polylang->options['hide_default'] ) { + if ( pll_default_language() === $language && PLL()->options['hide_default'] ) { continue; } $lang_post_id = pll_get_post( $user_page_id, $language ); @@ -112,7 +110,7 @@ public function add_rewrite_rules( $rules ) { if ( isset( $user->post_name ) && isset( $lang_post_obj->post_name ) ) { $lang_page_slug = $lang_post_obj->post_name; - if ( 1 === $polylang->options['force_lang'] ) { + if ( 1 === PLL()->options['force_lang'] ) { $newrules[ $language . '/' . $lang_page_slug . '/([^/]+)/?$' ] = 'index.php?page_id=' . $lang_post_id . '&um_user=$matches[1]&lang=' . $language; } diff --git a/includes/core/class-um-polylang.php b/includes/core/class-um-polylang.php index d7c879b..8c1ccc2 100644 --- a/includes/core/class-um-polylang.php +++ b/includes/core/class-um-polylang.php @@ -58,6 +58,7 @@ public function __construct() { */ public function fields() { if ( empty( UM()->classes['um_polylang_fields'] ) ) { + require_once um_polylang_path . 'includes/core/class-fields.php'; UM()->classes['um_polylang_fields'] = new um_ext\um_polylang\core\Fields(); } return UM()->classes['um_polylang_fields']; @@ -71,6 +72,7 @@ public function fields() { */ public function form() { if ( empty( UM()->classes['um_polylang_form'] ) ) { + require_once um_polylang_path . 'includes/core/class-form.php'; UM()->classes['um_polylang_form'] = new um_ext\um_polylang\core\Form(); } return UM()->classes['um_polylang_form']; @@ -84,6 +86,7 @@ public function form() { */ public function mail() { if ( empty( UM()->classes['um_polylang_mail'] ) ) { + require_once um_polylang_path . 'includes/core/class-mail.php'; UM()->classes['um_polylang_mail'] = new um_ext\um_polylang\core\Mail(); } return UM()->classes['um_polylang_mail']; @@ -97,6 +100,7 @@ public function mail() { */ public function permalinks() { if ( empty( UM()->classes['um_polylang_permalinks'] ) ) { + require_once um_polylang_path . 'includes/core/class-permalinks.php'; UM()->classes['um_polylang_permalinks'] = new um_ext\um_polylang\core\Permalinks(); } return UM()->classes['um_polylang_permalinks']; @@ -108,12 +112,10 @@ public function permalinks() { * * @since 1.0.0 * - * @global object $polylang The Polylang instance. * @param string $field Optional, the language field to return (@see PLL_Language), defaults to `'slug'`. * @return string|int|bool|string[]|PLL_Language The requested field or object for the current language, `false` if the field isn't set. */ public function get_current( $field = 'slug' ) { - global $polylang; $lang = pll_current_language(); if ( isset( $_GET['lang'] ) ) { @@ -122,7 +124,7 @@ public function get_current( $field = 'slug' ) { if ( empty( $lang ) || 'all' === $lang ) { $lang = substr( get_locale(), 0, 2 ); } - $language = $polylang->model->get_language( $lang ); + $language = PLL()->model->get_language( $lang ); return is_object( $language ) ? $language->get_prop( $field ) : $lang; } @@ -156,16 +158,13 @@ public function get_languages_list() { /** * Check if Polylang is active. * - * @since 1.0.0 + * @since 1.0.0 + * @version 1.0.3 Check for the PLL function. * * @return boolean */ public function is_active() { - if ( defined( 'POLYLANG_VERSION' ) ) { - global $polylang; - return isset( $polylang ) && is_object( $polylang ); - } - return false; + return defined( 'POLYLANG_VERSION' ) && function_exists( 'PLL' ); } diff --git a/readme.txt b/readme.txt index 1752377..dd8a5ec 100644 --- a/readme.txt +++ b/readme.txt @@ -1,14 +1,16 @@ === Ultimate Member - Polylang === + Author: umdevelopera Author URI: https://github.com/umdevelopera Plugin URI: https://github.com/umdevelopera/um-polylang Tags: ultimate member, polylang, multilingual -Requires at least: 6.0 -Tested up to: 6.3 -Stable tag: 1.0.2 +Requires at least: 5.5 +Tested up to: 6.4 +Requires UM core at least: 2.6.8 +Tested UM core up to: 2.7.0 +Stable tag: 1.0.3 License: GNU Version 2 or Any Later Version License URI: http://www.gnu.org/licenses/gpl-3.0.txt -Requires UM core at least: 2.6.7 == Description == @@ -20,14 +22,16 @@ Integrates the "Ultimate Member" community plugin with the "Polylang" multilingu * Ability to translate bio (description) field in profile. * Proper permalinks for the Account and User (profile) pages. -== Installation == - -You can install this plugin from the ZIP file as any other plugin. Follow this instruction: https://wordpress.org/support/article/managing-plugins/#upload-via-wordpress-admin - = Documentation & Support = This is a free extension created for the community. The Ultimate Member team does not provide any support for this extension. Open new issue in the GitHub repository if you face a problem: https://github.com/umdevelopera/um-polylang/issues +== Installation == + +Download ZIP file from GitHub or Google Drive. You can find download links here: https://github.com/umdevelopera/um-polylang + +You can install this plugin from the ZIP file as any other plugin. Follow this instruction: https://wordpress.org/support/article/managing-plugins/#upload-via-wordpress-admin + == Changelog == = 1.0.2: July 20, 2023 = diff --git a/um-polylang.php b/um-polylang.php index 916f490..432086a 100644 --- a/um-polylang.php +++ b/um-polylang.php @@ -1,14 +1,17 @@ set_class( 'Polylang', true ); - } - } - add_action( 'plugins_loaded', 'um_polylang_init', 4, 1 ); + UM()->set_class( 'Polylang', true ); } } } From 5a94226e28c027f61c1517a1057a1334e6f02cac Mon Sep 17 00:00:00 2001 From: umdevelopera Date: Thu, 23 Nov 2023 00:30:39 +0200 Subject: [PATCH 3/5] Changes: - added the Admin class. This class displays admin notices. - added the Setup class. This class creates pages and updates permalinks. - added `lang` parameter to the account activation link. --- includes/admin/class-admin.php | 103 ++++++++++++++++++++++++++++ includes/core/class-permalinks.php | 50 ++++++++------ includes/core/class-setup.php | 96 ++++++++++++++++++++++++++ includes/core/class-um-polylang.php | 39 ++++++++++- um-polylang.php | 10 ++- 5 files changed, 268 insertions(+), 30 deletions(-) create mode 100644 includes/admin/class-admin.php create mode 100644 includes/core/class-setup.php diff --git a/includes/admin/class-admin.php b/includes/admin/class-admin.php new file mode 100644 index 0000000..afda277 --- /dev/null +++ b/includes/admin/class-admin.php @@ -0,0 +1,103 @@ +Polylang()->setup()->create_pages(); + wp_safe_redirect( admin_url( 'edit.php?post_type=page' ) ); + exit; + } + + + public function create_pages_notice() { + $screen = get_current_screen(); + if ( ! is_object( $screen ) || 'edit-page' !== $screen->id ) { + return; + } + + $languages = pll_languages_list(); + $pages = UM()->config()->permalinks; + if ( empty( $languages ) || empty( $pages ) ) { + return; + } + + $need_translations = array(); + + foreach ( $pages as $page => $page_id ) { + $page_translations = pll_get_post_translations( $page_id ); + if ( array_diff( $languages, array_keys( $page_translations ) ) ) { + $need_translations[] = $page; + } + } + + if ( $need_translations ) { + + $url_params = array( + 'um_adm_action' => 'um_pll_create_pages', + '_wpnonce' => wp_create_nonce( 'um_pll_create_pages' ), + ); + + $url = add_query_arg( $url_params ); + + ob_start(); + ?> + +

+ get_allowed_html( 'admin_notice' ) + ); + ?> +

+

+ + +

+ + 'notice-warning', + 'message' => $message, + 'dismissible' => true, + ); + + UM()->admin()->notices()->add_notice( 'um_pll_create_pages', $notice_data, 20 ); + } + } + } + +} diff --git a/includes/core/class-permalinks.php b/includes/core/class-permalinks.php index f0efb39..98ddec9 100644 --- a/includes/core/class-permalinks.php +++ b/includes/core/class-permalinks.php @@ -14,37 +14,26 @@ /** * Localize permalinks. * + * @version 1.0.3 static method `update_core_pages` removed. + * * @package um_ext\um_polylang\core */ class Permalinks { - /** - * Update meta for translated core pages. - * This is needed because the 'um_is_core_page' filter was removed. - */ - public static function update_core_pages() { - $languages = pll_languages_list(); - foreach ( $languages as $language ) { - foreach ( UM()->config()->permalinks as $page => $page_id ) { - $lang_post_id = pll_get_post( $page_id, $language ); - if ( $lang_post_id && is_numeric( $lang_post_id ) && $lang_post_id !== $page_id ) { - update_post_meta( $lang_post_id, '_um_wpml_' . $page, 1 ); - update_post_meta( $lang_post_id, '_icl_lang_duplicate_of', $page_id ); - } - } - } - } - - /** * Class Permalinks constructor. */ public function __construct() { add_filter( 'rewrite_rules_array', array( &$this, 'add_rewrite_rules' ), 10, 1 ); + // Links in emails. + add_filter( 'um_activate_url', array( &$this, 'localize_activate_url' ), 10, 1 ); + + // Pages. add_filter( 'um_get_core_page_filter', array( &$this, 'localize_core_page_url' ), 10, 3 ); add_filter( 'um_localize_permalink_filter', array( &$this, 'localize_profile_permalink' ), 10, 2 ); + // Buttons. add_filter( 'um_login_form_button_two_url', array( &$this, 'localize_page_url' ), 10, 2 ); add_filter( 'um_register_form_button_two_url', array( &$this, 'localize_page_url' ), 10, 2 ); @@ -119,16 +108,14 @@ public function add_rewrite_rules( $rules ) { } } - // update core pages. - self::update_core_pages(); - return array_merge( $newrules, $rules ); } /** * Filter the link in the language switcher. * - * @since 1.0.1 + * @since 1.0.1 + * @version 1.0.3 static method `update_core_pages` removed. * * @param string|null $url The link, null if no translation was found. * @param string $slug The language code. @@ -243,6 +230,25 @@ public function is_core_page( $is_core_page, $page ) { } + /** + * Filter account activation link. + * + * @hook um_activate_url + * @see um\core\Permalinks::activate_url() + * @since 1.0.3 + * + * @param string $url Account activation link. + * + * @return string Localized account activation link. + */ + public function localize_activate_url( $url ){ + if ( ! UM()->Polylang()->is_default() ) { + $url = add_query_arg( 'lang', UM()->Polylang()->get_current(), $url ); + } + return $url; + } + + /** * Filter core page URL. * diff --git a/includes/core/class-setup.php b/includes/core/class-setup.php new file mode 100644 index 0000000..9d046f8 --- /dev/null +++ b/includes/core/class-setup.php @@ -0,0 +1,96 @@ +config()->permalinks; + if ( empty( $languages ) || empty( $pages ) ) { + return; + } + + $pages_translations = array(); + foreach( $pages as $page => $page_id ) { + $cur_lang = PLL()->model->post->get_language( $page_id ); + if ( false === $cur_lang ) { + PLL()->model->post->set_language( $page_id, PLL()->pref_lang ); + } + + $translations = pll_get_post_translations( $page_id ); + $untranslated = array_diff( $languages, array_keys( $translations ) ); + + if ( $untranslated ) { + $postdata = get_post( $page_id, ARRAY_A ); + + foreach ( $languages as $lang ) { + if ( array_key_exists( $lang, $translations ) ) { + $lang_post_id = $translations[ $lang ]; + } else { + $postarr = $postdata; + $postarr['ID'] = null; + $postarr['post_date'] = null; + $postarr['post_date_gmt'] = null; + $postarr['post_title'] = $postarr['post_title'] . " ($lang)"; + + $_GET['new_lang'] = $lang; + $lang_post_id = wp_insert_post( $postarr ); + unset( $_GET['new_lang'] ); + + update_post_meta( $lang_post_id, '_um_wpml_' . $page, 1 ); + update_post_meta( $lang_post_id, '_icl_lang_duplicate_of', $page_id ); + } + + $translations[ $lang ] = $lang_post_id; + } + PLL()->model->post->save_translations( $page_id, $translations ); + } + + $pages_translations[ $page ] = $translations; + } + + return $pages_translations; + } + + + /** + * Update core pages. + * Removes rewrite rules and then recreate rewrite rules. + */ + public function refresh_rewrite_rules() { + require_once 'class-permalinks.php'; + UM()->classes['um_polylang_permalinks'] = new Permalinks(); + flush_rewrite_rules(); + } + + + /** + * Run on plugin activation. + */ + public function run() { + $this->refresh_rewrite_rules(); + } + +} diff --git a/includes/core/class-um-polylang.php b/includes/core/class-um-polylang.php index 8c1ccc2..66f0f42 100644 --- a/includes/core/class-um-polylang.php +++ b/includes/core/class-um-polylang.php @@ -43,11 +43,32 @@ public static function instance() { */ public function __construct() { if ( $this->is_active() ) { - $this->fields(); - $this->form(); $this->mail(); $this->permalinks(); + + if( UM()->is_ajax() ) { + + } elseif ( UM()->is_request( 'admin' ) ) { + $this->admin(); + } elseif ( UM()->is_request( 'frontend' ) ) { + $this->fields(); + $this->form(); + } + } + } + + + /** + * Subclass that extends wp-admin features. + * + * @return um_ext\um_polylang\admin\Admin() + */ + public function admin() { + if ( empty( UM()->classes['um_polylang_admin'] ) ) { + require_once um_polylang_path . 'includes/admin/class-admin.php'; + UM()->classes['um_polylang_admin'] = new um_ext\um_polylang\admin\Admin(); } + return UM()->classes['um_polylang_admin']; } @@ -107,6 +128,20 @@ public function permalinks() { } + /** + * Subclass that setup pages and forms. + * + * @return um_ext\um_polylang\core\Setup() + */ + public function setup() { + if ( empty( UM()->classes['um_polylang_setup'] ) ) { + require_once um_polylang_path . 'includes/core/class-setup.php'; + UM()->classes['um_polylang_setup'] = new um_ext\um_polylang\core\Setup(); + } + return UM()->classes['um_polylang_setup']; + } + + /** * Returns the current language. * diff --git a/um-polylang.php b/um-polylang.php index 432086a..cf6640d 100644 --- a/um-polylang.php +++ b/um-polylang.php @@ -34,13 +34,11 @@ // Activation script. if ( ! function_exists( 'um_polylang_activation_hook' ) ) { function um_polylang_activation_hook() { - - // update core pages. if ( function_exists( 'UM' ) && function_exists( 'pll_languages_list' ) ) { - require_once 'includes/core/class-permalinks.php'; - if ( class_exists( 'um_ext\um_polylang\core\Permalinks' ) ) { - $permalinks = new um_ext\um_polylang\core\Permalinks(); - flush_rewrite_rules(); + require_once 'includes/core/class-setup.php'; + if ( class_exists( 'um_ext\um_polylang\core\Setup' ) ) { + $setup = new um_ext\um_polylang\core\Setup(); + $setup->run(); } } } From 75f5a565cb0c8972b42bfc9e11f7e2f753d94a8e Mon Sep 17 00:00:00 2001 From: umdevelopera Date: Sat, 2 Dec 2023 14:54:53 +0200 Subject: [PATCH 4/5] - make UM forms translateable --- README.md | 10 +- includes/admin/class-admin.php | 244 +++++++++++++++++++++++++--- includes/admin/class-mail.php | 196 ++++++++++++++++++++++ includes/core/class-form.php | 30 +++- includes/core/class-mail.php | 170 ------------------- includes/core/class-permalinks.php | 6 +- includes/core/class-setup.php | 73 +++++---- includes/core/class-um-polylang.php | 42 +++-- languages/um-polylang-en_US.mo | Bin 0 -> 519 bytes languages/um-polylang-en_US.po | 88 ++++++++++ languages/um-polylang.pot | 89 ++++++++++ readme.txt | 17 +- um-polylang.php | 4 +- 13 files changed, 717 insertions(+), 252 deletions(-) create mode 100644 includes/admin/class-mail.php create mode 100644 languages/um-polylang-en_US.mo create mode 100644 languages/um-polylang-en_US.po create mode 100644 languages/um-polylang.pot diff --git a/README.md b/README.md index eca1bed..bb7b2e8 100644 --- a/README.md +++ b/README.md @@ -4,15 +4,17 @@ Integrates the **Ultimate Member** community plugin with the **Polylang** multil ## Key features +- Localized permalinks for the Account and User (profile) pages. +- Ability to duplicate Ultimate Member forms for all languages in one click. +- Ability to duplicate Ultimate Member pages for all languages in one click. - Ability to translate email templates. - Ability to translate bio (description) field in profile. -- Proper permalinks for the Account and User (profile) pages. ## Installation __Note:__ This plugin requires the [Ultimate Member](https://wordpress.org/plugins/ultimate-member/) and [Polylang](https://uk.wordpress.org/plugins/polylang/) plugins to be installed first. -### Clone from GitHub +### How to install from GitHub Open git bash, navigate to the **plugins** folder and execute this command: @@ -20,9 +22,9 @@ Open git bash, navigate to the **plugins** folder and execute this command: Once the plugin is cloned, enter your site admin dashboard and go to _wp-admin > Plugins > Installed Plugins_. Find the "Ultimate Member - Polylang" plugin and click the "Activate" link. -### Install from ZIP archive +### How to install from ZIP archive -You can install this plugin from the [ZIP archive](https://drive.google.com/file/d/175PVG6tLK7z1wcrAawFfQdTVIC071Eup/view) as any other plugin. Follow [this instruction](https://wordpress.org/support/article/managing-plugins/#upload-via-wordpress-admin). +You can install this plugin from the [ZIP archive](https://drive.google.com/file/d/1Lpgu5b-6CLkjK0Ik24CBB836aIcRcmaj/view) as any other plugin. Follow [this instruction](https://wordpress.org/support/article/managing-plugins/#upload-via-wordpress-admin). ## How to use diff --git a/includes/admin/class-admin.php b/includes/admin/class-admin.php index afda277..4298cd0 100644 --- a/includes/admin/class-admin.php +++ b/includes/admin/class-admin.php @@ -1,6 +1,6 @@ settings_email_tab(); + + // Forms table styles. + add_action( 'admin_footer', array( $this, 'styles' ) ); } - public function create_pages() { - UM()->Polylang()->setup()->create_pages(); - wp_safe_redirect( admin_url( 'edit.php?post_type=page' ) ); - exit; + /** + * The "Create Forms" button handler. + */ + public function action_create_forms() { + $args = array( + 'fields' => 'ids', + 'nopaging' => true, + 'post_status' => 'publish', + 'post_type' => 'um_form', + ); + $posts = get_posts( $args ); + + UM()->Polylang()->setup()->create_posts( $posts, 'um_form' ); + + $url = add_query_arg( 'update', 'um_pll_create_forms', admin_url( 'edit.php?post_type=um_form' ) ); + exit( wp_safe_redirect( $url ) ); } - public function create_pages_notice() { + /** + * The "Create Pages" button handler. + */ + public function action_create_pages() { + $posts = UM()->config()->permalinks; + + UM()->Polylang()->setup()->create_posts( $posts, 'page' ); + + $url = add_query_arg( 'update', 'um_pll_create_pages', admin_url( 'edit.php?post_type=page' ) ); + exit( wp_safe_redirect( $url ) ); + } + + + /** + * Display a notice with the "Create Forms" button. + * + * @return void + */ + public function notice_create_forms() { $screen = get_current_screen(); - if ( ! is_object( $screen ) || 'edit-page' !== $screen->id ) { + if ( ! is_object( $screen ) || 'edit-um_form' !== $screen->id ) { return; } $languages = pll_languages_list(); - $pages = UM()->config()->permalinks; - if ( empty( $languages ) || empty( $pages ) ) { + if ( empty( $languages ) ) { return; } - $need_translations = array(); + $args = array( + 'fields' => 'ids', + 'nopaging' => true, + 'post_status' => 'publish', + 'post_type' => 'um_form', + ); + $posts = get_posts( $args ); + if ( empty( $posts ) ) { + return; + } - foreach ( $pages as $page => $page_id ) { - $page_translations = pll_get_post_translations( $page_id ); - if ( array_diff( $languages, array_keys( $page_translations ) ) ) { - $need_translations[] = $page; + $need_translations = array(); + foreach ( $posts as $post => $post_id ) { + $post_translations = pll_get_post_translations( $post_id ); + if ( array_diff( $languages, array_keys( $post_translations ) ) ) { + $need_translations[] = $post_id; + break; } } if ( $need_translations ) { - $url_params = array( - 'um_adm_action' => 'um_pll_create_pages', - '_wpnonce' => wp_create_nonce( 'um_pll_create_pages' ), + 'um_adm_action' => 'um_pll_create_forms', + '_wpnonce' => wp_create_nonce( 'um_pll_create_forms' ), ); $url = add_query_arg( $url_params ); @@ -76,14 +131,14 @@ public function create_pages_notice() { get_allowed_html( 'admin_notice' ) ); ?>

- - + +

true, ); - UM()->admin()->notices()->add_notice( 'um_pll_create_pages', $notice_data, 20 ); + UM()->admin()->notices()->add_notice( 'um_pll_create_forms', $notice_data, 20 ); + } + } + + + /** + * Display a notice with the "Create Pages" button. + * + * @return void + */ + public function notice_create_pages() { + $screen = get_current_screen(); + if ( ! is_object( $screen ) || 'edit-page' !== $screen->id ) { + return; + } + + $languages = pll_languages_list(); + if ( empty( $languages ) ) { + return; + } + + $posts = UM()->config()->permalinks; + if ( empty( $posts ) ) { + return; + } + + $need_translations = array(); + foreach ( $posts as $post => $post_id ) { + $post_translations = pll_get_post_translations( $post_id ); + if ( array_diff( $languages, array_keys( $post_translations ) ) ) { + $need_translations[] = $post_id; + break; + } + } + + if ( $need_translations ) { + $url_params = array( + 'um_adm_action' => 'um_pll_create_pages', + '_wpnonce' => wp_create_nonce( 'um_pll_create_pages' ), + ); + + $url = add_query_arg( $url_params ); + + ob_start(); + ?> + +

+ get_allowed_html( 'admin_notice' ) + ); + ?> +

+

+ + +

+ + 'notice-warning', + 'message' => $message, + 'dismissible' => true, + ); + + UM()->admin()->notices()->add_notice( 'um_pll_create_pages', $notice_data, 20 ); + } + } + + + /** + * Display a notice after um_adm_action. + * + * @param array $messages Admin notice messages. + * @param string $update Update action key. + * + * @return array + */ + public function notice_update( $messages, $update ) { + + switch ( $update ) { + + case 'um_pll_create_forms': + $messages[0]['content'] = __( 'Forms have been duplicated successfully.', 'um-polylang' ); + break; + + case 'um_pll_create_pages': + $messages[0]['content'] = __( 'Pages have been duplicated successfully.', 'um-polylang' ); + break; + } + + return $messages; + } + + + /** + * Filters the list of post types available for translation. + * + * @param string[] $post_types List of post type names (as array keys and values). + * @param bool $is_settings True when displaying the list of custom post types in Polylang settings. + * + * @return string[] List of post type names. + */ + public function pll_get_post_types( $post_types, $is_settings ) { + $post_types['um_form'] = 'um_form'; + return array_unique( $post_types ); + } + + + /** + * Extend settings Email tab. + * + * @return um_ext\um_polylang\admin\Mail() + */ + public function settings_email_tab() { + if ( empty( UM()->classes['um_polylang_admin_mail'] ) ) { + require_once 'class-mail.php'; + UM()->classes['um_polylang_admin_mail'] = new Mail(); + } + return UM()->classes['um_polylang_admin_mail']; + } + + + /** + * Fix column width in the "Forms" table. + */ + public function styles() { + $screen = get_current_screen(); + if ( ! is_object( $screen ) || 'edit-um_form' === $screen->id ) { + ?> +Polylang()->is_default() ? '' : '_' . UM()->Polylang()->get_current( 'locale' ); + $value = UM()->options()->get( $email_key . '_sub' . $locale ); + $value_default = UM()->options()->get( $email_key . '_sub' ); + + $section_fields[2]['id'] = $email_key . '_sub' . $locale; + $section_fields[2]['value'] = empty( $value ) ? $value_default : $value; + + return $section_fields; + } + + + /** + * Create email template file in the theme folder. + * + * @since 1.1.0 + * + * @param array $settings Input data. + * @return array + */ + public function create_email_template_file( $settings ) { + if ( isset( $settings['um_email_template'] ) ) { + $template = $settings['um_email_template']; + $template_path = UM()->mail()->get_template_file( 'theme', $template ); + + if ( ! file_exists( $template_path ) ) { + $template_dir = dirname( $template_path ); + + if ( wp_mkdir_p( $template_dir ) ) { + file_put_contents( $template_path, '' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents + } + } + } + return $settings; + } + + + /** + * Add header for the column 'translations' in the Email table. + * + * @since 1.1.0 + * + * @param array $columns The Email table headers. + * @return array + */ + public function email_table_columns( $columns ) { + + $languages = pll_languages_list(); + if ( count( $languages ) > 0 ) { + + $flags = ''; + foreach ( $languages as $language ) { + $language = PLL()->model->get_language( $language ); + $flag = is_object( $language ) ? $language->flag : $language; + $flags .= '' . $flag . ''; + } + + $new_columns = array(); + foreach ( $columns as $column_key => $column_content ) { + $new_columns[ $column_key ] = $column_content; + if ( 'email' === $column_key && ! isset( $new_columns['pll_translations'] ) ) { + $new_columns['pll_translations'] = $flags; + } + } + + $columns = $new_columns; + } + + return $columns; + } + + + /** + * Add cell for the column 'translations' in the Email table. + * + * @since 1.1.0 + * + * @param array $email_notifications Email templates data. + * @return string + */ + public function email_table_items( $email_notifications ) { + $languages = pll_languages_list(); + + foreach ( $email_notifications as &$email_notification ) { + $email_notification['pll_translations'] = ''; + foreach ( $languages as $language ) { + $email_notification['pll_translations'] .= $this->email_table_link( $email_notification['key'], $language ); + } + } + + return $email_notifications; + } + + + /** + * Get a link to Add/Edit email template for a certain language. + * + * @since 1.1.0 + * + * @param string $template The email template slug. + * @param string $code Slug or locale of the queried language. + * @return string + */ + public function email_table_link( $template, $code ) { + + $language = PLL()->model->get_language( $code ); + $default = pll_default_language(); + $locale = $code === $default ? '' : trailingslashit( $language->get_prop( 'locale' ) ); + + // theme location. + $template_path = trailingslashit( get_stylesheet_directory() . '/ultimate-member/email/' . $locale ) . $template . '.php'; + + // plugin location for default language. + if ( empty( $locale ) && ! file_exists( $template_path ) ) { + $template_path = UM()->mail()->get_template_file( 'plugin', $template ); + } + + $link = add_query_arg( + array( + 'email' => $template, + 'lang' => $code, + ) + ); + + if ( file_exists( $template_path ) ) { + + // translators: %s - language name. + $hint = sprintf( __( 'Edit the translation in %s', 'polylang' ), $language->get_prop( 'name' ) ); + $icon_html = sprintf( + '%3$s', + esc_url( $link ), + esc_html( $hint ), + esc_html( $hint ) + ); + } else { + + // translators: %s - language name. + $hint = sprintf( __( 'Add a translation in %s', 'polylang' ), $language->get_prop( 'name' ) ); + $icon_html = sprintf( + '%3$s', + esc_url( $link ), + esc_attr( $hint ), + esc_html( $hint ) + ); + } + + return $icon_html; + } + +} diff --git a/includes/core/class-form.php b/includes/core/class-form.php index 8e8a5ba..ebac06d 100644 --- a/includes/core/class-form.php +++ b/includes/core/class-form.php @@ -23,12 +23,38 @@ class Form { * Class Form constructor. */ public function __construct() { + add_filter( 'shortcode_atts_ultimatemember', array( &$this, 'shortcode_atts_ultimatemember' ), 20, 1 ); add_filter( 'um_pre_args_setup', array( &$this, 'pre_args_setup' ), 20, 1 ); } /** - * Get arguments from original form if translated form doesn't have this data. + * Filters shortcode attributes. + * Replaces 'form_id' attribute if translated form exists. + * + * @since 1.1.0 + * + * @link https://developer.wordpress.org/reference/hooks/shortcode_atts_shortcode/ + * + * @param array $args Shortcode arguments. + * + * @return array Shortcode arguments. + */ + public function shortcode_atts_ultimatemember( $args ){ + if ( isset( $args['form_id'] ) ) { + $form_id = absint( $args['form_id'] ); + $translated_form_id = pll_get_post( $form_id, pll_current_language() ); + + if ( $translated_form_id && $translated_form_id !== $form_id ) { + $args['form_id'] = $translated_form_id; + } + } + return $args; + } + + + /** + * Gets data from the original form if the translated form doesn't have this data. * * @hook um_pre_args_setup * @@ -46,7 +72,7 @@ public function pre_args_setup( $args ) { $original_post_data = UM()->query()->post_data( $original_form_id ); foreach ( $original_post_data as $key => $value ) { - if ( ! isset( $args[ $key ] ) ) { + if ( ! array_key_exists( $key, $args ) ) { $args[ $key ] = $value; } } diff --git a/includes/core/class-mail.php b/includes/core/class-mail.php index 37c4a03..484297c 100644 --- a/includes/core/class-mail.php +++ b/includes/core/class-mail.php @@ -23,44 +23,13 @@ class Mail { * Class Mail constructor. */ public function __construct() { - // Email table. - add_filter( 'um_email_templates_columns', array( &$this, 'email_table_columns' ), 10, 1 ); - add_filter( 'um_email_notifications', array( &$this, 'email_table_items' ), 10, 1 ); - // Email settings. - add_filter( 'um_admin_settings_email_section_fields', array( &$this, 'admin_settings_email_section_fields' ), 10, 2 ); add_filter( 'um_email_send_subject', array( &$this, 'localize_email_subject' ), 10, 2 ); - - // Email template file. add_filter( 'um_change_email_template_file', array( &$this, 'change_email_template_file' ), 10, 1 ); - add_filter( 'um_change_settings_before_save', array( &$this, 'create_email_template_file' ), 8, 1 ); add_filter( 'um_locate_email_template', array( &$this, 'locate_email_template' ), 10, 2 ); } - /** - * Adding locale suffix to the "Subject Line" field. - * - * Example: change 'welcome_email_sub' to 'welcome_email_sub_de_DE' - * - * @since 1.0.0 - * - * @param array $section_fields The email template fields. - * @param string $email_key The email template slug. - * @return array - */ - public function admin_settings_email_section_fields( $section_fields, $email_key ) { - $locale = UM()->Polylang()->is_default() ? '' : '_' . UM()->Polylang()->get_current( 'locale' ); - $value = UM()->options()->get( $email_key . '_sub' . $locale ); - $value_default = UM()->options()->get( $email_key . '_sub' ); - - $section_fields[2]['id'] = $email_key . '_sub' . $locale; - $section_fields[2]['value'] = empty( $value ) ? $value_default : $value; - - return $section_fields; - } - - /** * Change email template for searching in the theme folder. * @@ -77,145 +46,6 @@ public function change_email_template_file( $template ) { } - /** - * Create email template file in the theme folder. - * - * @since 1.0.0 - * - * @param array $settings Input data. - * @return array - */ - public function create_email_template_file( $settings ) { - if ( isset( $settings['um_email_template'] ) ) { - $template = $settings['um_email_template']; - $template_path = UM()->mail()->get_template_file( 'theme', $template ); - - if ( ! file_exists( $template_path ) ) { - $template_dir = dirname( $template_path ); - - if ( wp_mkdir_p( $template_dir ) ) { - file_put_contents( $template_path, '' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents - } - } - } - return $settings; - } - - - /** - * Add header for the column 'translations' in the Email table. - * - * @since 1.0.0 - * - * @param array $columns The Email table headers. - * @return array - */ - public function email_table_columns( $columns ) { - - $languages = pll_languages_list(); - if ( count( $languages ) > 0 ) { - - $flags = ''; - foreach ( $languages as $language ) { - $language = PLL()->model->get_language( $language ); - $flag = is_object( $language ) ? $language->flag : $language; - $flags .= '' . $flag . ''; - } - - $new_columns = array(); - foreach ( $columns as $column_key => $column_content ) { - $new_columns[ $column_key ] = $column_content; - if ( 'email' === $column_key && ! isset( $new_columns['pll_translations'] ) ) { - $new_columns['pll_translations'] = $flags; - } - } - - $columns = $new_columns; - } - - return $columns; - } - - - /** - * Add cell for the column 'translations' in the Email table. - * - * @since 1.0.0 - * - * @param array $email_notifications Email templates data. - * @return string - */ - public function email_table_items( $email_notifications ) { - $languages = pll_languages_list(); - - foreach ( $email_notifications as &$email_notification ) { - $email_notification['pll_translations'] = ''; - foreach ( $languages as $language ) { - $email_notification['pll_translations'] .= $this->email_table_link( $email_notification['key'], $language ); - } - } - - return $email_notifications; - } - - - /** - * Get a link to Add/Edit email template for a certain language. - * - * @since 1.0.0 - * @version 1.0.3 Use locale instead of the code in the template path. - * - * @param string $template The email template slug. - * @param string $code Slug or locale of the queried language. - * @return string - */ - public function email_table_link( $template, $code ) { - - $language = PLL()->model->get_language( $code ); - $default = pll_default_language(); - $locale = $code === $default ? '' : trailingslashit( $language->get_prop( 'locale' ) ); - - // theme location. - $template_path = trailingslashit( get_stylesheet_directory() . '/ultimate-member/email/' . $locale ) . $template . '.php'; - - // plugin location for default language. - if ( empty( $locale ) && ! file_exists( $template_path ) ) { - $template_path = UM()->mail()->get_template_file( 'plugin', $template ); - } - - $link = add_query_arg( - array( - 'email' => $template, - 'lang' => $code, - ) - ); - - if ( file_exists( $template_path ) ) { - - // translators: %s - language name. - $hint = sprintf( __( 'Edit the translation in %s', 'polylang' ), $language->get_prop( 'name' ) ); - $icon_html = sprintf( - '%3$s', - esc_url( $link ), - esc_html( $hint ), - esc_html( $hint ) - ); - } else { - - // translators: %s - language name. - $hint = sprintf( __( 'Add a translation in %s', 'polylang' ), $language->get_prop( 'name' ) ); - $icon_html = sprintf( - '%3$s', - esc_url( $link ), - esc_attr( $hint ), - esc_html( $hint ) - ); - } - - return $icon_html; - } - - /** * Replace email Subject with translated value on email send. * Example: change 'welcome_email_sub' to 'welcome_email_sub_de_DE' diff --git a/includes/core/class-permalinks.php b/includes/core/class-permalinks.php index 98ddec9..783a133 100644 --- a/includes/core/class-permalinks.php +++ b/includes/core/class-permalinks.php @@ -14,7 +14,7 @@ /** * Localize permalinks. * - * @version 1.0.3 static method `update_core_pages` removed. + * @version 1.1.0 static method `update_core_pages` removed. * * @package um_ext\um_polylang\core */ @@ -115,7 +115,7 @@ public function add_rewrite_rules( $rules ) { * Filter the link in the language switcher. * * @since 1.0.1 - * @version 1.0.3 static method `update_core_pages` removed. + * @version 1.1.0 static method `update_core_pages` removed. * * @param string|null $url The link, null if no translation was found. * @param string $slug The language code. @@ -235,7 +235,7 @@ public function is_core_page( $is_core_page, $page ) { * * @hook um_activate_url * @see um\core\Permalinks::activate_url() - * @since 1.0.3 + * @since 1.1.0 * * @param string $url Account activation link. * diff --git a/includes/core/class-setup.php b/includes/core/class-setup.php index 9d046f8..60c8ec7 100644 --- a/includes/core/class-setup.php +++ b/includes/core/class-setup.php @@ -20,64 +20,73 @@ class Setup { /** - * Create pages for languages. + * Create posts for languages. * - * @return array Information about pages. + * @since 1.1.0 + * + * @return array Information about posts. */ - public function create_pages() { + public function create_posts( $posts, $post_type ) { $languages = pll_languages_list(); - $pages = UM()->config()->permalinks; - if ( empty( $languages ) || empty( $pages ) ) { + if ( empty( $languages ) || empty( $posts ) ) { return; } - $pages_translations = array(); - foreach( $pages as $page => $page_id ) { - $cur_lang = PLL()->model->post->get_language( $page_id ); + $posts_translations = array(); + foreach( $posts as $post => $post_id ) { + $cur_lang = PLL()->model->post->get_language( $post_id ); if ( false === $cur_lang ) { - PLL()->model->post->set_language( $page_id, PLL()->pref_lang ); + PLL()->model->post->set_language( $post_id, PLL()->pref_lang ); } - $translations = pll_get_post_translations( $page_id ); + $translations = pll_get_post_translations( $post_id ); $untranslated = array_diff( $languages, array_keys( $translations ) ); if ( $untranslated ) { - $postdata = get_post( $page_id, ARRAY_A ); - - foreach ( $languages as $lang ) { - if ( array_key_exists( $lang, $translations ) ) { - $lang_post_id = $translations[ $lang ]; - } else { - $postarr = $postdata; - $postarr['ID'] = null; - $postarr['post_date'] = null; - $postarr['post_date_gmt'] = null; - $postarr['post_title'] = $postarr['post_title'] . " ($lang)"; - - $_GET['new_lang'] = $lang; - $lang_post_id = wp_insert_post( $postarr ); - unset( $_GET['new_lang'] ); - - update_post_meta( $lang_post_id, '_um_wpml_' . $page, 1 ); - update_post_meta( $lang_post_id, '_icl_lang_duplicate_of', $page_id ); + $postdata = get_post( $post_id, ARRAY_A ); + $postmeta = get_post_meta( $post_id ); + + foreach ( $untranslated as $lang ) { + $postarr = $postdata; + $postarr['ID'] = null; + $postarr['post_date'] = null; + $postarr['post_date_gmt'] = null; + $postarr['post_title'] = $postarr['post_title'] . " ($lang)"; + + // Polylang need the 'new_lang' parameter to set a proper language. + $_GET['new_lang'] = $lang; + $lang_post_id = wp_insert_post( $postarr ); + unset( $_GET['new_lang'] ); + + // Duplicate postmeta. + foreach ( $postmeta as $key => $value ) { + if ( '_um_core' === $key ) { + continue; + } + $meta_value = maybe_unserialize( $value[0] ); + update_post_meta( $lang_post_id, $key, $meta_value ); } + update_post_meta( $lang_post_id, '_icl_lang_duplicate_of', $post_id ); $translations[ $lang ] = $lang_post_id; + do_action( 'um_polylang_create_posts', $lang_post_id, $post_id, $lang, $post_type ); } - PLL()->model->post->save_translations( $page_id, $translations ); + PLL()->model->post->save_translations( $post_id, $translations ); } - $pages_translations[ $page ] = $translations; + $posts_translations[ $post ] = $translations; } - return $pages_translations; + return $posts_translations; } /** - * Update core pages. + * Updates core pages. * Removes rewrite rules and then recreate rewrite rules. + * + * @since 1.1.0 */ public function refresh_rewrite_rules() { require_once 'class-permalinks.php'; diff --git a/includes/core/class-um-polylang.php b/includes/core/class-um-polylang.php index 66f0f42..ce5a002 100644 --- a/includes/core/class-um-polylang.php +++ b/includes/core/class-um-polylang.php @@ -42,25 +42,27 @@ public static function instance() { * Class UM_Polylang constructor. */ public function __construct() { - if ( $this->is_active() ) { - $this->mail(); - $this->permalinks(); - - if( UM()->is_ajax() ) { - - } elseif ( UM()->is_request( 'admin' ) ) { - $this->admin(); - } elseif ( UM()->is_request( 'frontend' ) ) { - $this->fields(); - $this->form(); - } + $this->mail(); + $this->permalinks(); + + if( UM()->is_ajax() ) { + + } elseif ( UM()->is_request( 'admin' ) ) { + $this->admin(); + } elseif ( UM()->is_request( 'frontend' ) ) { + $this->fields(); + $this->form(); } + + add_action( 'plugins_loaded', array( $this, 'textdomain' ), 9 ); } /** * Subclass that extends wp-admin features. * + * @since 1.1.0 + * * @return um_ext\um_polylang\admin\Admin() */ public function admin() { @@ -131,6 +133,8 @@ public function permalinks() { /** * Subclass that setup pages and forms. * + * @since 1.1.0 + * * @return um_ext\um_polylang\core\Setup() */ public function setup() { @@ -142,6 +146,16 @@ public function setup() { } + /** + * Loads a plugin's translated strings. + */ + public function textdomain() { + $locale = get_locale() ? get_locale() : 'en_US'; + load_textdomain( um_polylang_textdomain, WP_LANG_DIR . '/plugins/' . um_polylang_textdomain . '-' . $locale . '.mo' ); + load_plugin_textdomain( um_polylang_textdomain, false, dirname( um_polylang_plugin ) . '/languages/' ); + } + + /** * Returns the current language. * @@ -181,7 +195,7 @@ public function get_default( $field = 'slug' ) { /** * Returns the list of available languages. * - * @since 1.0.3 + * @since 1.1.0 * * @return array */ @@ -194,7 +208,7 @@ public function get_languages_list() { * Check if Polylang is active. * * @since 1.0.0 - * @version 1.0.3 Check for the PLL function. + * @version 1.1.0 Check for the PLL function. * * @return boolean */ diff --git a/languages/um-polylang-en_US.mo b/languages/um-polylang-en_US.mo new file mode 100644 index 0000000000000000000000000000000000000000..b1cc1b4b3738e575bf88763bac142621deb2cc7f GIT binary patch literal 519 zcmaJ;O-lnY5LNKBN6#Kc@Sx~ycehd`r3bZDgceKPiZ@BO<2I5_mL&D-fB56P_*U4BsaMeVvM#LKo`L-yq}^zAGa78(CXJKm(wabq z^}W|@G|*dU)mkdkg<>I{r_h_`S9rGJH9nrm;lLDju1qQ5Y^Fl=fY)Tmtql%sVPO$M u?M17lZ6VnO#WVHMVqP=cXvpGD-08w=jh*;3PI=z972g-Itg!z5s;wV9)16fS literal 0 HcmV?d00001 diff --git a/languages/um-polylang-en_US.po b/languages/um-polylang-en_US.po new file mode 100644 index 0000000..6335274 --- /dev/null +++ b/languages/um-polylang-en_US.po @@ -0,0 +1,88 @@ +msgid "" +msgstr "" +"Project-Id-Version: Ultimate Member - Polylang\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2023-12-03 23:01+0000\n" +"PO-Revision-Date: 2023-12-03 23:01+0000\n" +"Last-Translator: DeveloperA UltimateMember\n" +"Language-Team: English (United States)\n" +"Language: en_US\n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"X-Generator: Loco https://localise.biz/\n" +"X-Loco-Version: 2.6.6; wp-6.4.1\n" +"X-Domain: um-polylang" + +#. %s: Plugin name. +#: includes/admin/class-admin.php:134 +#, php-format +msgid "" +"%s needs to create required forms for every language to function correctly." +msgstr "" + +#. %s: Plugin name. +#: includes/admin/class-admin.php:203 +#, php-format +msgid "" +"%s needs to create required pages for every language to function correctly." +msgstr "" + +#: includes/admin/class-admin.php:140 +msgid "Create Forms" +msgstr "" + +#: includes/admin/class-admin.php:209 +msgid "Create Pages" +msgstr "" + +#: includes/admin/class-admin.php:240 +msgid "Forms have been duplicated successfully." +msgstr "" + +#. Author URI of the plugin +msgid "https://github.com/umdevelopera" +msgstr "" + +#. URI of the plugin +msgid "https://github.com/umdevelopera/um-polylang" +msgstr "" + +#. Description of the plugin +msgid "Integrates Ultimate Member with Polylang." +msgstr "" + +#: includes/admin/class-admin.php:141 includes/admin/class-admin.php:210 +msgid "No thanks" +msgstr "" + +#: includes/admin/class-admin.php:244 +msgid "Pages have been duplicated successfully." +msgstr "" + +#. %s - plugin name. +#: um-polylang.php:69 +#, php-format +msgid "" +"The %s extension requires the Polylang plugin to be " +"activated to work properly. You can download it here" +msgstr "" + +#. %s - plugin name. +#: um-polylang.php:60 +#, php-format +msgid "" +"The %s extension requires the Ultimate Member plugin to be " +"activated to work properly. You can download it here" +msgstr "" + +#. Name of the plugin +msgid "Ultimate Member - Polylang" +msgstr "" + +#. Author of the plugin +msgid "umdevelopera" +msgstr "" diff --git a/languages/um-polylang.pot b/languages/um-polylang.pot new file mode 100644 index 0000000..61b58e6 --- /dev/null +++ b/languages/um-polylang.pot @@ -0,0 +1,89 @@ +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: Ultimate Member - Polylang\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2023-12-03 23:01+0000\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: \n" +"Language: \n" +"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"X-Generator: Loco https://localise.biz/\n" +"X-Loco-Version: 2.6.6; wp-6.4.1\n" +"X-Domain: um-polylang" + +#. %s: Plugin name. +#: includes/admin/class-admin.php:134 +#, php-format +msgid "" +"%s needs to create required forms for every language to function correctly." +msgstr "" + +#. %s: Plugin name. +#: includes/admin/class-admin.php:203 +#, php-format +msgid "" +"%s needs to create required pages for every language to function correctly." +msgstr "" + +#: includes/admin/class-admin.php:140 +msgid "Create Forms" +msgstr "" + +#: includes/admin/class-admin.php:209 +msgid "Create Pages" +msgstr "" + +#: includes/admin/class-admin.php:240 +msgid "Forms have been duplicated successfully." +msgstr "" + +#. Author URI of the plugin +msgid "https://github.com/umdevelopera" +msgstr "" + +#. URI of the plugin +msgid "https://github.com/umdevelopera/um-polylang" +msgstr "" + +#. Description of the plugin +msgid "Integrates Ultimate Member with Polylang." +msgstr "" + +#: includes/admin/class-admin.php:141 includes/admin/class-admin.php:210 +msgid "No thanks" +msgstr "" + +#: includes/admin/class-admin.php:244 +msgid "Pages have been duplicated successfully." +msgstr "" + +#. %s - plugin name. +#: um-polylang.php:69 +#, php-format +msgid "" +"The %s extension requires the Polylang plugin to be " +"activated to work properly. You can download it here" +msgstr "" + +#. %s - plugin name. +#: um-polylang.php:60 +#, php-format +msgid "" +"The %s extension requires the Ultimate Member plugin to be " +"activated to work properly. You can download it here" +msgstr "" + +#. Name of the plugin +msgid "Ultimate Member - Polylang" +msgstr "" + +#. Author of the plugin +msgid "umdevelopera" +msgstr "" diff --git a/readme.txt b/readme.txt index dd8a5ec..7d250df 100644 --- a/readme.txt +++ b/readme.txt @@ -5,10 +5,10 @@ Author URI: https://github.com/umdevelopera Plugin URI: https://github.com/umdevelopera/um-polylang Tags: ultimate member, polylang, multilingual Requires at least: 5.5 -Tested up to: 6.4 +Tested up to: 6.4.1 Requires UM core at least: 2.6.8 Tested UM core up to: 2.7.0 -Stable tag: 1.0.3 +Stable tag: 1.1.0 License: GNU Version 2 or Any Later Version License URI: http://www.gnu.org/licenses/gpl-3.0.txt @@ -18,9 +18,11 @@ Integrates the "Ultimate Member" community plugin with the "Polylang" multilingu = Key Features = +* Localized permalinks for the Account and User (profile) pages. +* Ability to duplicate Ultimate Member forms for all languages in one click. +* Ability to duplicate Ultimate Member pages for all languages in one click. * Ability to translate email templates. * Ability to translate bio (description) field in profile. -* Proper permalinks for the Account and User (profile) pages. = Documentation & Support = @@ -34,6 +36,15 @@ You can install this plugin from the ZIP file as any other plugin. Follow this i == Changelog == += 1.1.0: December 4, 2023 = + +* Added: Polylang integration for Ultimate Member forms. +* Added: The "Create Forms" notice and button. +* Added: The "Create Pages" notice and button. +* Added: The `lang` parameter to the account activation link. +* Added: Translation template (.pot file). +* Fixed: Classes autoloader issue: Class "um_ext\um_polylang\core\Fields" not found. + = 1.0.2: July 20, 2023 = * Fixed: Translated description field value diff --git a/um-polylang.php b/um-polylang.php index cf6640d..b686361 100644 --- a/um-polylang.php +++ b/um-polylang.php @@ -8,10 +8,12 @@ * Text Domain: um-polylang * Domain Path: /languages * - * Version: 1.0.3 + * Version: 1.1.0 * UM version: 2.7.0 * Requires at least: 5.5 * Requires PHP: 5.6 + * + * @package UM Extended */ if ( ! defined( 'ABSPATH' ) ) { From 2bc8ce2a319313a17e3146a61b64670ca2c81198 Mon Sep 17 00:00:00 2001 From: umdevelopera <113178913+umdevelopera@users.noreply.github.com> Date: Mon, 4 Dec 2023 01:46:53 +0200 Subject: [PATCH 5/5] Update README.md --- README.md | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index bb7b2e8..6658f3c 100644 --- a/README.md +++ b/README.md @@ -28,28 +28,33 @@ You can install this plugin from the [ZIP archive](https://drive.google.com/file ## How to use -Go to *wp-admin > Ultimate Member > Settings > Email* to translate email templates. Click the "+" icon unter the flag to translate a template for the needed language. The plugin saves translated email templates to locale subfolders in the theme. See details [here](https://docs.ultimatemember.com/article/1335-email-templates). +Go to *wp-admin > Pages* to translate Ultimate Member pages. Click the **Create Pages** button in the notice to duplicate Ultimate Member pages for all languages. Or click the "+" icon unter the flag to duplicate each page manually. -Go to *wp-admin > Pages* to translate pages Account, Login, Members, Password Reset, Register, User. Click the "+" icon unter the flag to translate a page for the needed language. See details [here](https://docs.ultimatemember.com/article/1449-how-to-translate-plugin#forms). +Image - Translate pages. +![Pages](https://github.com/umdevelopera/um-polylang/assets/113178913/40543c1b-d428-4832-9090-d2cc9166b616) -Go to *wp-admin > Settings > Permalinks* and click the "Save Changes" button to update rewrite rules for the Account and Profile permalinks. +Go to *wp-admin > Ultimate Member > Forms* to translate Ultimate Member forms. Click the **Create Forms** button in the notice to duplicate Ultimate Member forms for all languages. Or click the "+" icon unter the flag to duplicate each form manually. -__Note:__ The "Post name" permalink structure is recommended. +Once forms for languages are created you can open these forms and translate fields. You have to translate a **Label** for custom fields. You also can translate **Placeholder** and **Help Text** if needed. -### Screenshots +Image - Translate forms. +![Forms](https://github.com/umdevelopera/um-polylang/assets/113178913/76763122-a774-4778-ab2c-748b3e983779) -Image - Translate email templates. -![UM Settings, Email (polylang)](https://github.com/umdevelopera/um-polylang/assets/113178913/65d14995-257d-4311-a93a-8f944ea12ba9) +Go to *wp-admin > Ultimate Member > Settings > Email* to translate email templates. Click the "+" icon unter the flag to translate a template for the language. The plugin saves translated email templates to locale subfolders in the theme, see [Email Templates](https://docs.ultimatemember.com/article/1335-email-templates). -Image - Translate pages. -![WP Pages (polylang)](https://github.com/umdevelopera/um-polylang/assets/113178913/1329f025-a464-4c52-bf9f-99261fb5e242) +Image - Translate emails. +![Email](https://github.com/umdevelopera/um-polylang/assets/113178913/17167ba5-8564-4fe9-ba33-ef69bfb67f57) + +Go to *wp-admin > Settings > Permalinks* and click the "Save Changes" button if you need to update rewrite rules for the Account and Profile permalinks. Image - Permalink settings. ![WP Settings, Permalink (default)](https://github.com/umdevelopera/um-polylang/assets/113178913/69be91c9-12dd-490c-9145-b163c5beb26d) +__Note:__ The "Post name" permalink structure is recommended. + ## Support -This is a free extension created for the community. The Ultimate Member team does not provide any support for this extension. Open new [issue](https://github.com/umdevelopera/um-polylang/issues) if you face a problem. +This is a free extension created for the community. The Ultimate Member team does not provide support for this extension. Open new [issue](https://github.com/umdevelopera/um-polylang/issues) if you face a problem. ## Related links @@ -59,4 +64,4 @@ Ultimate Member documentation: https://docs.ultimatemember.com/ Ultimate Member on wordpress.org: https://wordpress.org/plugins/ultimate-member/ -Article: [How to translate plugin](https://docs.ultimatemember.com/article/1449-how-to-translate-plugin#switch) +Article: [How to translate plugin](https://docs.ultimatemember.com/article/1449-how-to-translate-plugin#switch).