Skip to content

Commit

Permalink
- prepare version 1.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
umdevelopera committed Dec 25, 2023
1 parent adb7029 commit 6dd1453
Show file tree
Hide file tree
Showing 11 changed files with 275 additions and 127 deletions.
30 changes: 18 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -12,49 +12,55 @@ 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

Open git bash, navigate to the **plugins** folder and execute this command:

`git clone --branch=main [email protected]: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.

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

Expand Down
41 changes: 27 additions & 14 deletions includes/admin/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
Expand Down Expand Up @@ -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 ) );
Expand All @@ -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 ) );
Expand All @@ -95,6 +96,7 @@ public function notice_create_forms() {
if ( empty( $languages ) ) {
return;
}
$def_lang = pll_default_language();

$args = array(
'fields' => 'ids',
Expand All @@ -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 );
}
}

Expand All @@ -129,10 +133,13 @@ public function notice_create_forms() {

<p>
<?php
// translators: %s: Plugin name.
echo wp_kses(
sprintf( __( '%s needs to create required forms for every language to function correctly.', 'um-polylang' ), UM_PLUGIN_NAME ),
UM()->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 )
)
);
?>
</p>
Expand Down Expand Up @@ -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 ) ) {
Expand All @@ -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 );
}
}

Expand All @@ -198,10 +208,13 @@ public function notice_create_pages() {

<p>
<?php
// translators: %s: Plugin name.
echo wp_kses(
sprintf( __( '%s needs to create required pages for every language to function correctly.', 'um-polylang' ), UM_PLUGIN_NAME ),
UM()->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 )
)
);
?>
</p>
Expand Down
5 changes: 4 additions & 1 deletion includes/core/class-permalinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
*
Expand Down Expand Up @@ -111,6 +113,7 @@ public function add_rewrite_rules( $rules ) {
return array_merge( $newrules, $rules );
}


/**
* Filter the link in the language switcher.
*
Expand Down
161 changes: 161 additions & 0 deletions includes/core/class-posts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php
/**
* Create translated posts and forms
*
* @package um_ext\um_polylang\core
*/

namespace um_ext\um_polylang\core;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Create translated pages and forms
*
* @package um_ext\um_polylang\core
*/
class Posts {

public function __construct() {
add_action( 'pll_save_post', array( $this, 'pll_save_post' ), 20, 3 );
}


/**
* Create pages and forms for languages.
*
* @since 1.1.0
*
* @param array $posts An array of posts to duplicate.
* @param string $post_type Post type.
*
* @return array Information about posts.
*/
public function create_posts( $posts, $post_type ) {

$languages = pll_languages_list();
if ( empty( $languages ) || empty( $posts ) ) {
return;
}
$def_lang = pll_default_language();

$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 );
}
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 );
}
}
}

}
Loading

0 comments on commit 6dd1453

Please sign in to comment.