From 6dd1453cd9de024b84388d25c2cff22059b5a357 Mon Sep 17 00:00:00 2001 From: umdevelopera Date: Sun, 24 Dec 2023 17:41:43 +0200 Subject: [PATCH] - prepare version 1.1.1 --- README.md | 30 +++--- includes/admin/class-admin.php | 41 ++++--- includes/core/class-permalinks.php | 5 +- includes/core/class-posts.php | 161 ++++++++++++++++++++++++++++ includes/core/class-setup.php | 68 +----------- includes/core/class-um-polylang.php | 19 +++- languages/um-polylang-en_US.mo | Bin 519 -> 519 bytes languages/um-polylang-en_US.po | 26 ++--- languages/um-polylang.pot | 26 ++--- readme.txt | 22 ++-- um-polylang.php | 4 +- 11 files changed, 275 insertions(+), 127 deletions(-) create mode 100644 includes/core/class-posts.php diff --git a/README.md b/README.md index 6658f3c..6e788da 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Ultimate Member - Polylang -Integrates the **Ultimate Member** community plugin with the **Polylang** multilingual plugin. +Integrates the **Ultimate Member** plugin with the **Polylang** plugin. Makes Ultimate Member multilingual. ## Key features @@ -12,7 +12,7 @@ Integrates the **Ultimate Member** community plugin with the **Polylang** multil ## 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. +__Note:__ This plugin requires the [Ultimate Member](https://wordpress.org/plugins/ultimate-member/) and [Polylang](https://wordpress.org/plugins/polylang/) plugins to be installed first. ### How to install from GitHub @@ -20,19 +20,29 @@ 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` -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. +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. ### How to install from ZIP archive -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). +You can install this plugin from the [ZIP archive](https://drive.google.com/file/d/17n3Kr05_B_edsGY7An7xu85TZ7H5gbOT/view?usp=sharing) as any other plugin. Follow [this instruction](https://wordpress.org/support/article/managing-plugins/#upload-via-wordpress-admin). ## How to use +### How to translate pages + 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. 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 if you need to update rewrite rules for the Account and User page permalinks. +Note: The "Post name" permalink structure is recommended. + +Image - Permalink settings. +![WP Settings, Permalink (default)](https://github.com/umdevelopera/um-polylang/assets/113178913/69be91c9-12dd-490c-9145-b163c5beb26d) + +### How to translate forms + 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. 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. @@ -40,21 +50,17 @@ Once forms for languages are created you can open these forms and translate fiel Image - Translate forms. ![Forms](https://github.com/umdevelopera/um-polylang/assets/113178913/76763122-a774-4778-ab2c-748b3e983779) +### How to translate E-mails + 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 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 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 are facing a problem or have a suggestion. ## Related links diff --git a/includes/admin/class-admin.php b/includes/admin/class-admin.php index 4298cd0..9803a46 100644 --- a/includes/admin/class-admin.php +++ b/includes/admin/class-admin.php @@ -30,6 +30,7 @@ public function __construct() { add_action( 'in_admin_header', array( $this, 'notice_create_forms' ) ); add_action( 'in_admin_header', array( $this, 'notice_create_pages' ) ); add_filter( 'um_adm_action_custom_notice_update', array( $this, 'notice_update' ), 10, 2 ); + add_filter( 'um_adm_action_custom_update_notice', array( $this, 'notice_update' ), 10, 2 ); // Create Forms. add_action( 'um_admin_do_action__um_pll_create_forms', array( $this, 'action_create_forms' ) ); @@ -60,7 +61,7 @@ public function action_create_forms() { ); $posts = get_posts( $args ); - UM()->Polylang()->setup()->create_posts( $posts, 'um_form' ); + UM()->Polylang()->posts()->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 ) ); @@ -73,7 +74,7 @@ public function action_create_forms() { public function action_create_pages() { $posts = UM()->config()->permalinks; - UM()->Polylang()->setup()->create_posts( $posts, 'page' ); + UM()->Polylang()->posts()->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 ) ); @@ -95,6 +96,7 @@ public function notice_create_forms() { if ( empty( $languages ) ) { return; } + $def_lang = pll_default_language(); $args = array( 'fields' => 'ids', @@ -109,10 +111,12 @@ public function notice_create_forms() { $need_translations = array(); foreach ( $posts as $post => $post_id ) { + if ( $def_lang !== pll_get_post_language( $post_id ) ) { + continue; + } $post_translations = pll_get_post_translations( $post_id ); if ( array_diff( $languages, array_keys( $post_translations ) ) ) { - $need_translations[] = $post_id; - break; + $need_translations[ $post_id ] = get_the_title( $post_id ); } } @@ -129,10 +133,13 @@ public function notice_create_forms() {

get_allowed_html( 'admin_notice' ) + // translators: %1$s - plugin name, %2$s - a list of forms. + echo esc_html( + sprintf( + __( '%1$s needs to create required forms for every language to function correctly. Forms that need translation: %2$s', 'um-polylang' ), + UM_PLUGIN_NAME, + implode( ', ', $need_translations ) + ) ); ?>

@@ -170,6 +177,7 @@ public function notice_create_pages() { if ( empty( $languages ) ) { return; } + $def_lang = pll_default_language(); $posts = UM()->config()->permalinks; if ( empty( $posts ) ) { @@ -178,10 +186,12 @@ public function notice_create_pages() { $need_translations = array(); foreach ( $posts as $post => $post_id ) { + if ( $def_lang !== pll_get_post_language( $post_id ) ) { + continue; + } $post_translations = pll_get_post_translations( $post_id ); if ( array_diff( $languages, array_keys( $post_translations ) ) ) { - $need_translations[] = $post_id; - break; + $need_translations[ $post_id ] = get_the_title( $post_id ); } } @@ -198,10 +208,13 @@ public function notice_create_pages() {

get_allowed_html( 'admin_notice' ) + // translators: %1$s - plugin name, %2$s - a list of pages. + echo esc_html( + sprintf( + __( '%1$s needs to create required pages for every language to function correctly. Pages that need translation: %2$s', 'um-polylang' ), + UM_PLUGIN_NAME, + implode( ', ', $need_translations ) + ) ); ?>

diff --git a/includes/core/class-permalinks.php b/includes/core/class-permalinks.php index 783a133..d3bc874 100644 --- a/includes/core/class-permalinks.php +++ b/includes/core/class-permalinks.php @@ -24,6 +24,8 @@ class Permalinks { * Class Permalinks constructor. */ public function __construct() { + + // Add rewrite rules for the Account and User page. add_filter( 'rewrite_rules_array', array( &$this, 'add_rewrite_rules' ), 10, 1 ); // Links in emails. @@ -46,7 +48,7 @@ public function __construct() { /** - * Add UM rewrite rules for the Account page and Profile page. + * Add rewrite rules for the Account and User page. * * @hook rewrite_rules_array * @@ -111,6 +113,7 @@ public function add_rewrite_rules( $rules ) { return array_merge( $newrules, $rules ); } + /** * Filter the link in the language switcher. * diff --git a/includes/core/class-posts.php b/includes/core/class-posts.php new file mode 100644 index 0000000..d207860 --- /dev/null +++ b/includes/core/class-posts.php @@ -0,0 +1,161 @@ + $post_id ) { + $cur_lang = PLL()->model->post->get_language( $post_id ); + if ( false === $cur_lang ) { + PLL()->model->post->set_language( $post_id, PLL()->pref_lang ); + } + if ( $def_lang !== $cur_lang->get_prop( 'slug' ) ) { + continue; + } + + $translations = pll_get_post_translations( $post_id ); + $untranslated = array_diff( $languages, array_keys( $translations ) ); + + if ( $untranslated ) { + $postdata = get_post( $post_id, ARRAY_A ); + $postmeta = get_post_meta( $post_id ); + + foreach ( $untranslated as $lang ) { + $tr_arr = $postdata; + $tr_arr['ID'] = null; + $tr_arr['post_date'] = null; + $tr_arr['post_date_gmt'] = null; + $tr_arr['post_name'] = $tr_arr['post_name'] . '-' . $lang; + $tr_arr['post_title'] = $tr_arr['post_title'] . " ($lang)"; + + // Polylang need the 'new_lang' parameter to set a proper language. + $_GET['new_lang'] = $lang; + $tr_id = wp_insert_post( $tr_arr ); + 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( $tr_id, $key, $meta_value ); + } + update_post_meta( $tr_id, '_icl_lang_duplicate_of', $post_id ); + + $translations[ $lang ] = $tr_id; + do_action( 'um_polylang_create_posts', $tr_id, $post_id, $lang, $post_type ); + } + PLL()->model->post->save_translations( $post_id, $translations ); + } + + $posts_translations[ $post ] = $translations; + } + UM()->rewrite()->reset_rules(); + + return $posts_translations; + } + + + /** + * Synchronizes post fields in the page or form translation. + * + * Hook `pll_save_post` fires after the post language and translations are saved. + * + * @see \PLL_CRUD_Posts::save_post() + * + * @since 1.1.1 + * + * @param int $tr_id Post id. + * @param WP_Post $post Post object. + * @param int[] $translations Post translations. + * + * @return void + */ + public function pll_save_post( $tr_id, $post, $translations ) { + global $wpdb; + + if ( + is_a( $post, 'WP_Post' ) && 'auto-draft' === $post->post_status + && PLL()->model->post->current_user_can_synchronize( $tr_id ) + && isset( $GLOBALS['pagenow'], $_GET['from_post'], $_GET['new_lang'] ) && 'post-new.php' === $GLOBALS['pagenow'] + ) { + check_admin_referer( 'new-post-translation' ); + + $lang = sanitize_key( $_GET['new_lang'] ); + $post_id = absint( $_GET['from_post'] ); + $original = get_post( $post_id ); + + if ( empty( $original ) ) { + return; + } + + if ( 'um_form' === $original->post_type + || ( 'page' === $original->post_type && in_array( $post_id, UM()->config()->permalinks, true ) ) + ) { + + // Duplicate content. + $wpdb->update( + $wpdb->posts, + array( + 'post_content' => $original->post_content, + 'post_title' => $original->post_title . " ($lang)", + ), + array( + 'ID' => $tr_id, + ) + ); + + // Duplicate postmeta. + $postmeta = get_post_meta( $post_id ); + foreach ( $postmeta as $key => $value ) { + if ( '_um_core' === $key ) { + continue; + } + $meta_value = maybe_unserialize( $value[0] ); + update_post_meta( $tr_id, $key, $meta_value ); + } + update_post_meta( $tr_id, '_icl_lang_duplicate_of', $post_id ); + } + } + } + +} diff --git a/includes/core/class-setup.php b/includes/core/class-setup.php index 60c8ec7..e6f8c6c 100644 --- a/includes/core/class-setup.php +++ b/includes/core/class-setup.php @@ -18,77 +18,13 @@ */ class Setup { - - /** - * Create posts for languages. - * - * @since 1.1.0 - * - * @return array Information about posts. - */ - public function create_posts( $posts, $post_type ) { - - $languages = pll_languages_list(); - if ( empty( $languages ) || empty( $posts ) ) { - return; - } - - $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( $post_id, PLL()->pref_lang ); - } - - $translations = pll_get_post_translations( $post_id ); - $untranslated = array_diff( $languages, array_keys( $translations ) ); - - if ( $untranslated ) { - $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( $post_id, $translations ); - } - - $posts_translations[ $post ] = $translations; - } - - return $posts_translations; - } - - /** * Updates core pages. * Removes rewrite rules and then recreate rewrite rules. * * @since 1.1.0 */ - public function refresh_rewrite_rules() { + public function flush_rewrite_rules() { require_once 'class-permalinks.php'; UM()->classes['um_polylang_permalinks'] = new Permalinks(); flush_rewrite_rules(); @@ -99,7 +35,7 @@ public function refresh_rewrite_rules() { * Run on plugin activation. */ public function run() { - $this->refresh_rewrite_rules(); + $this->flush_rewrite_rules(); } } diff --git a/includes/core/class-um-polylang.php b/includes/core/class-um-polylang.php index ce5a002..39e6537 100644 --- a/includes/core/class-um-polylang.php +++ b/includes/core/class-um-polylang.php @@ -49,6 +49,7 @@ public function __construct() { } elseif ( UM()->is_request( 'admin' ) ) { $this->admin(); + $this->posts(); } elseif ( UM()->is_request( 'frontend' ) ) { $this->fields(); $this->form(); @@ -131,7 +132,23 @@ public function permalinks() { /** - * Subclass that setup pages and forms. + * Subclass that creates translated posts and forms. + * + * @since 1.1.1 + * + * @return um_ext\um_polylang\core\Posts() + */ + public function posts() { + if ( empty( UM()->classes['um_polylang_posts'] ) ) { + require_once um_polylang_path . 'includes/core/class-posts.php'; + UM()->classes['um_polylang_posts'] = new um_ext\um_polylang\core\Posts(); + } + return UM()->classes['um_polylang_posts']; + } + + + /** + * Subclass that do actions on installation. * * @since 1.1.0 * diff --git a/languages/um-polylang-en_US.mo b/languages/um-polylang-en_US.mo index b1cc1b4b3738e575bf88763bac142621deb2cc7f..78bcd5cc05cce4efbca057455ccf08c944753188 100644 GIT binary patch delta 19 acmZo?X=j\n" "Language-Team: \n" @@ -16,29 +16,31 @@ msgstr "" "X-Loco-Version: 2.6.6; wp-6.4.1\n" "X-Domain: um-polylang" -#. %s: Plugin name. -#: includes/admin/class-admin.php:134 +#. %1$s - plugin name, %2$s - a list of forms. +#: includes/admin/class-admin.php:138 #, php-format msgid "" -"%s needs to create required forms for every language to function correctly." +"%1$s needs to create required forms for every language to function correctly." +" Forms that need translation: %2$s" msgstr "" -#. %s: Plugin name. -#: includes/admin/class-admin.php:203 +#. %1$s - plugin name, %2$s - a list of pages. +#: includes/admin/class-admin.php:213 #, php-format msgid "" -"%s needs to create required pages for every language to function correctly." +"%1$s needs to create required pages for every language to function correctly." +" Pages that need translation: %2$s" msgstr "" -#: includes/admin/class-admin.php:140 +#: includes/admin/class-admin.php:146 msgid "Create Forms" msgstr "" -#: includes/admin/class-admin.php:209 +#: includes/admin/class-admin.php:221 msgid "Create Pages" msgstr "" -#: includes/admin/class-admin.php:240 +#: includes/admin/class-admin.php:252 msgid "Forms have been duplicated successfully." msgstr "" @@ -54,11 +56,11 @@ msgstr "" msgid "Integrates Ultimate Member with Polylang." msgstr "" -#: includes/admin/class-admin.php:141 includes/admin/class-admin.php:210 +#: includes/admin/class-admin.php:147 includes/admin/class-admin.php:222 msgid "No thanks" msgstr "" -#: includes/admin/class-admin.php:244 +#: includes/admin/class-admin.php:256 msgid "Pages have been duplicated successfully." msgstr "" diff --git a/readme.txt b/readme.txt index 7d250df..3da2948 100644 --- a/readme.txt +++ b/readme.txt @@ -5,16 +5,16 @@ 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.1 +Tested up to: 6.4.2 Requires UM core at least: 2.6.8 -Tested UM core up to: 2.7.0 -Stable tag: 1.1.0 +Tested UM core up to: 2.8.1 +Stable tag: 1.1.1 License: GNU Version 2 or Any Later Version License URI: http://www.gnu.org/licenses/gpl-3.0.txt == Description == -Integrates the "Ultimate Member" community plugin with the "Polylang" multilingual plugin. +Integrates the "Ultimate Member" plugin with the "Polylang" plugin. Makes UM multilingual. = Key Features = @@ -26,16 +26,24 @@ Integrates the "Ultimate Member" community plugin with the "Polylang" multilingu = 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 +This is a free extension created for the community. The Ultimate Member team does not provide support for this extension. +Open new issue in the GitHub repository if you are facing a problem or have a suggestion: https://github.com/umdevelopera/um-polylang/issues +Documentation is the README section in the GitHub repository: https://github.com/umdevelopera/um-polylang == 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 +Download ZIP file from GitHub or Google Drive. You can find download links here: https://github.com/umdevelopera/um-polylang + == Changelog == += 1.1.1: December 24, 2023 = + +* Tweak: Displays forms/posts that need translation in the notice. +* Tweak: Copy fields to the draft when translating a form manually. +* Tweak: Automatically flush rewrite rules after bulk pages creation. + = 1.1.0: December 4, 2023 = * Added: Polylang integration for Ultimate Member forms. diff --git a/um-polylang.php b/um-polylang.php index b686361..7213c81 100644 --- a/um-polylang.php +++ b/um-polylang.php @@ -8,12 +8,12 @@ * Text Domain: um-polylang * Domain Path: /languages * - * Version: 1.1.0 + * Version: 1.1.1 * UM version: 2.7.0 * Requires at least: 5.5 * Requires PHP: 5.6 * - * @package UM Extended + * @package UM Tools */ if ( ! defined( 'ABSPATH' ) ) {