Skip to content

Commit

Permalink
- make UM forms translateable
Browse files Browse the repository at this point in the history
  • Loading branch information
umdevelopera committed Dec 3, 2023
1 parent 5a94226 commit 75f5a56
Show file tree
Hide file tree
Showing 13 changed files with 717 additions and 252 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@ 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:

`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.

### 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

Expand Down
244 changes: 221 additions & 23 deletions includes/admin/class-admin.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Extends wp-admin features.
* Admin features.
*
* @package um_ext\um_polylang\admin
*/
Expand All @@ -26,45 +26,100 @@ class Admin {
* Admin constructor.
*/
public function __construct() {
add_action( 'in_admin_header', array( $this, 'create_pages_notice' ) );
// Notices.
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_action( 'um_admin_do_action__um_pll_create_pages', array( $this, 'create_pages' ) );
// Create Forms.
add_action( 'um_admin_do_action__um_pll_create_forms', array( $this, 'action_create_forms' ) );

// Create Pages.
add_action( 'um_admin_do_action__um_pll_create_pages', array( $this, 'action_create_pages' ) );

// Translatable post types.
add_filter( 'pll_get_post_types', array( $this, 'pll_get_post_types' ), 10, 2 );

// Settings, Email tab.
$this->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 );
Expand All @@ -76,14 +131,14 @@ public function create_pages_notice() {
<?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 ),
sprintf( __( '%s needs to create required forms for every language to function correctly.', 'um-polylang' ), UM_PLUGIN_NAME ),
UM()->get_allowed_html( 'admin_notice' )
);
?>
</p>
<p>
<a href="<?php echo esc_url( $url ); ?>" class="button button-primary"><?php esc_html_e( 'Create Pages', 'um_polylang' ); ?></a>
<a href="javascript:void(0);" class="button-secondary um_secondary_dismiss"><?php esc_html_e( 'No thanks', 'um_polylang' ); ?></a>
<a href="<?php echo esc_url( $url ); ?>" class="button button-primary"><?php esc_html_e( 'Create Forms', 'um-polylang' ); ?></a>
<a href="javascript:void(0);" class="button-secondary um_secondary_dismiss"><?php esc_html_e( 'No thanks', 'um-polylang' ); ?></a>
</p>

<?php
Expand All @@ -95,7 +150,150 @@ public function create_pages_notice() {
'dismissible' => 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();
?>

<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' )
);
?>
</p>
<p>
<a href="<?php echo esc_url( $url ); ?>" class="button button-primary"><?php esc_html_e( 'Create Pages', 'um-polylang' ); ?></a>
<a href="javascript:void(0);" class="button-secondary um_secondary_dismiss"><?php esc_html_e( 'No thanks', 'um-polylang' ); ?></a>
</p>

<?php
$message = ob_get_clean();

$notice_data = array(
'class' => '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 ) {
?>
<style type="text/css">
@media screen and (max-width: 1200px) {
.um-admin.post-type-um_form tr > * { padding-left: 5px; padding-right: 5px; }
.um-admin.post-type-um_form .manage-column.column-title { width: 180px; }
.um-admin.post-type-um_form .manage-column.column-id { width: 40px; }
.um-admin.post-type-um_form .manage-column.column-mode { width: 60px; }
.um-admin.post-type-um_form .manage-column.column-is_default { width: 50px; }
.um-admin.post-type-um_form .manage-column.column-shortcode { width: 120px; }
}
</style><?php
}
}
}
Expand Down
Loading

0 comments on commit 75f5a56

Please sign in to comment.