Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #4 from kimcoleman/dev
Browse files Browse the repository at this point in the history
Translation, new filter and function to generate name, version bump a…
  • Loading branch information
kimcoleman authored Mar 13, 2021
2 parents 056e297 + da89f14 commit 11b20ba
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 23 deletions.
83 changes: 63 additions & 20 deletions force-first-last.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,49 @@
Plugin Name: Force First and Last Name as Display Name
Plugin URI: https://www.strangerstudios.com/wordpress-plugins/force-first-last/
Description: Force the user field display_name to be set as the user's first and last name.
Version: 1.1
Version: 1.2
Author: Stranger Studios
Author URI: https://www.strangerstudios.com
Text Domain: force-first-last
Domain Path: /languages
*/

/*
Copyright 2011 Stranger Studios (email : [email protected])
GPLv2 Full license details in license.txt
*/

define( 'FFL_BASENAME', plugin_basename( __FILE__ ) );

/**
* Load text domain
*/
function ffl_load_plugin_text_domain() {
load_plugin_textdomain( 'force-first-last', false, basename( dirname( __FILE__ ) ) . '/languages' );
}
add_action( 'plugins_loaded', 'ffl_load_plugin_text_domain' );

/**
* Generate the Display Name
*
*/
function ffl_generate_display_name( $first_name, $last_name ) {
if ( empty( $first_name ) || empty( $last_name ) ) {
return;
}

/**
* Adjust the display name order to be first last or last first depending on location.
*
* @param $display_name_order array An array of name parts.
* @return $display_name A formatted display name.
*
*/
$display_name_order = apply_filters( 'ffl_display_name_order', array( $first_name, $last_name ), $first_name, $last_name );
$display_name = trim( $display_name_order[0] . ' ' . $display_name_order[1] );
return $display_name;
}

/**
* Hide Display Name field on profile page.
*
Expand Down Expand Up @@ -44,10 +76,12 @@ function ffl_save_extra_profile_fields( $user_id ) {
}

if ( isset( $_POST['first_name'] ) && isset( $_POST['last_name'] ) ) {
$display_name = trim($_POST['first_name'] . " " . $_POST['last_name']);
$first_name = sanitize_text_field( trim( $_POST['first_name'] ) );
$last_name = sanitize_text_field( trim( $_POST['last_name'] ) );
$display_name = ffl_generate_display_name( $first_name, $last_name );
} else {
$info = get_userdata( $user_id );
$display_name = trim($info->first_name . ' ' . $info->last_name);
$display_name = ffl_generate_display_name( $info->first_name, $info->last_name );
if ( ! $display_name ) {
$display_name = $info->user_login;
}
Expand All @@ -59,7 +93,7 @@ function ffl_save_extra_profile_fields( $user_id ) {
'ID' => $user_id,
'display_name' => $display_name
);
wp_update_user( $args ) ;
wp_update_user( $args );
}
add_action( 'personal_options_update', 'ffl_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'ffl_save_extra_profile_fields' );
Expand All @@ -80,20 +114,18 @@ function ffl_pmpro_member_profile_edit_user_object_fields( $user_fields ) {
*
*/
function ffl_fix_user_display_name( $user_id ) {
//set the display name
// Get the user's first and last name.
$info = get_userdata( $user_id );

$display_name = trim($info->first_name . ' ' . $info->last_name);
$display_name = ffl_generate_display_name( $info->first_name, $info->last_name );
if ( ! $display_name ) {
$display_name = $info->user_login;
}

$args = array(
'ID' => $user_id,
'display_name' => $display_name
);

wp_update_user( $args ) ;
wp_update_user( $args );
}
add_action( 'user_register', 'ffl_fix_user_display_name', 20 );

Expand All @@ -102,7 +134,7 @@ function ffl_fix_user_display_name( $user_id ) {
*
*/
function ffl_settings_menu_item() {
add_options_page('Force First Last', 'Force First Last', 'manage_options', 'ffl_settings', 'ffl_settings_page');
add_options_page('Force First Last', __( 'Force First Last', 'force-first-last' ), 'manage_options', 'ffl_settings', 'ffl_settings_page');
}
add_action( 'admin_menu', 'ffl_settings_menu_item', 20 );

Expand Down Expand Up @@ -149,15 +181,15 @@ function ffl_settings_page() { ?>
</style>
<div class="wrap">
<div class="stranger_studios_banner">
<a class="stranger_studios_logo" title="Stranger Studios" target="_blank" href="https://www.strangerstudios.com/?utm_source=plugin&utm_medium=force-first-last&utm_campaign=homepage"><img src="<?php echo esc_url( plugins_url( 'images/Stranger-Studios.png', FFL_BASENAME ) ); ?>" border="0" alt="Stranger Studios(c) - All Rights Reserved"></a>
<a class="stranger_studios_logo" title="<?php esc_attr_e( 'Stranger Studios', 'force-first-last' ); ?>" target="_blank" href="https://www.strangerstudios.com/?utm_source=plugin&utm_medium=force-first-last&utm_campaign=homepage"><img src="<?php echo esc_url( plugins_url( 'images/Stranger-Studios.png', FFL_BASENAME ) ); ?>" border="0" alt="<?php esc_attr_e( 'Stranger Studios(c) - All Rights Reserved', 'force-first-last' ); ?>"></a>
<div class="stranger_studios_meta">
<a href="https://www.strangerstudios.com/wordpress-plugins/?utm_source=plugin&utm_medium=force-first-last&utm_campaign=wordpress-plugins" target="_blank" title="Stranger Studios WordPress Plugins">Plugins</a>
<a href="https://www.strangerstudios.com/wordpress-themes/?utm_source=plugin&utm_medium=force-first-last&utm_campaign=wordpress-themes" target="_blank" title="Stranger Studios WordPress Themes">Themes</a>
<a href="https://www.strangerstudios.com/blog/?utm_source=plugin&utm_medium=force-first-last&utm_campaign=blog" target="_blank" title="Stranger Studios Blog">Blog</a>
<a href="https://www.strangerstudios.com/wordpress-plugins/?utm_source=plugin&utm_medium=force-first-last&utm_campaign=wordpress-plugins" target="_blank" title="<?php esc_attr_e( 'Stranger Studios WordPress Plugins', 'force-first-last' ); ?>"><?php esc_html_e( 'Plugins', 'force-first-last' ); ?></a>
<a href="https://www.strangerstudios.com/wordpress-themes/?utm_source=plugin&utm_medium=force-first-last&utm_campaign=wordpress-themes" target="_blank" title="<?php esc_attr_e( 'Stranger Studios WordPress Themes', 'force-first-last' ); ?>"><?php esc_html_e( 'Themes', 'force-first-last' ); ?></a>
<a href="https://www.strangerstudios.com/blog/?utm_source=plugin&utm_medium=force-first-last&utm_campaign=blog" target="_blank" title="<?php esc_attr_e( 'Stranger Studios Blog', 'force-first-last' ); ?>"><?php esc_html_e( 'Blog', 'force-first-last' ); ?></a>
</div>
</div>
<div class="ffl_admin">
<h1 class="wp-heading-inline">Force First and Last Name</h1>
<h1 class="wp-heading-inline"><?php esc_html_e( 'Force First and Last Name', 'force-first-last') ;?></h1>
<?php if ( ! empty($_REQUEST['updateusers']) && current_user_can( 'manage_options' ) ) {
global $wpdb;
$user_ids = $wpdb->get_col("SELECT ID FROM $wpdb->users");
Expand All @@ -166,11 +198,22 @@ function ffl_settings_page() { ?>
ffl_fix_user_display_name($user_id);
set_time_limit(30);
} ?>
<div class="notice inline updated"><p><?php echo count($user_ids);?> users updated.</p></div>
<div class="notice inline updated"><p><?php printf( __( '%s users updated', 'force-first-last' ), count( $user_ids ) ); ?></p></div>
<?php } ?>
<p>The <em>Force First and Last Name as Display Name</em> plugin will only fix display names at registration or when a profile is updated. If you just activated this plugin, please click on the button below to update the display names of your existing users.</p>
<p><a href="?page=ffl_settings&updateusers=1" class="button button-hero button-primary">Update Existing Users</a></p>
<p><strong>WARNING:</strong> This process may take a long time for sites with many users or hosted on a slow server. <strong>Running this script may hang up or cause other issues with your site.</strong> Use at your own risk.</p>
<p><?php
$allowed_ffl_admin_text_strings_html = array (
'a' => array (
'href' => array(),
'target' => array(),
'title' => array(),
),
'strong' => array(),
'em' => array(),
);
echo wp_kses( __( 'The <em>Force First and Last Name as Display Name</em> plugin will only fix display names at registration or when a profile is updated. If you just activated this plugin, please click on the button below to update the display names of your existing users.', 'force-first-last' ), $allowed_ffl_admin_text_strings_html ); ?>
</p>
<p><a href="?page=ffl_settings&updateusers=1" class="button button-hero button-primary"><?php esc_html_e( 'Update Existing Users', 'force-first-last' ); ?></a></p>
<p><?php echo wp_kses( __( '<strong>WARNING:</strong> This process may take a long time for sites with many users or hosted on a slow server. <strong>Running this script may hang up or cause other issues with your site.</strong> Use at your own risk.', 'force-first-last' ), $allowed_ffl_admin_text_strings_html ); ?></p>
</div>
</div> <!-- end ffl_admin -->
<?php
Expand Down
82 changes: 82 additions & 0 deletions languages/force-first-last.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Force First and Last Name as Display Name: WordPress plugin.
# Copyright (C) 2011
# This file is distributed under the same license as the PACKAGE package.
# Stranger Studios <[email protected]>, 2011.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: [email protected]\n"
"POT-Creation-Date: 2021-03-12 14:24-0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"

#: force-first-last.php:137
msgid "Force First Last"
msgstr ""

#: force-first-last.php:184
msgid "Stranger Studios"
msgstr ""

#: force-first-last.php:184
msgid "Stranger Studios(c) - All Rights Reserved"
msgstr ""

#: force-first-last.php:186
msgid "Plugins"
msgstr ""

#: force-first-last.php:186
msgid "Stranger Studios WordPress Plugins"
msgstr ""

#: force-first-last.php:187
msgid "Stranger Studios WordPress Themes"
msgstr ""

#: force-first-last.php:187
msgid "Themes"
msgstr ""

#: force-first-last.php:188
msgid "Blog"
msgstr ""

#: force-first-last.php:188
msgid "Stranger Studios Blog"
msgstr ""

#: force-first-last.php:192
msgid "Force First and Last Name"
msgstr ""

#: force-first-last.php:201
#, php-format
msgid "%s users updated"
msgstr ""

#: force-first-last.php:213
msgid ""
"The <em>Force First and Last Name as Display Name</em> plugin will only fix "
"display names at registration or when a profile is updated. If you just "
"activated this plugin, please click on the button below to update the "
"display names of your existing users."
msgstr ""

#: force-first-last.php:215
msgid "Update Existing Users"
msgstr ""

#: force-first-last.php:216
msgid ""
"<strong>WARNING:</strong> This process may take a long time for sites with "
"many users or hosted on a slow server. <strong>Running this script may hang "
"up or cause other issues with your site.</strong> Use at your own risk."
msgstr ""
29 changes: 29 additions & 0 deletions languages/gettext.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#---------------------------
# This script generates a new force-first-last.pot file for use in translations.
# To generate a new force-first-last.pot, cd to the main /force-first-last/ directory,
# then execute `languages/gettext.sh` from the command line.
# then fix the header info (helps to have the old force-first-last.pot open before running script above)
# then execute `cp languages/force-first-last.pot languages/force-first-last.po` to copy the .pot to .po
# then execute `msgfmt languages/force-first-last.po --output-file languages/force-first-last.mo` to generate the .mo
#---------------------------
echo "Updating paid-memberships-pro.pot... "
xgettext -j -o languages/force-first-last.pot \
--default-domain=force-first-last \
--language=PHP \
--keyword=_ \
--keyword=__ \
--keyword=_e \
--keyword=_ex \
--keyword=_n \
--keyword=_x \
--keyword=esc_html__ \
--keyword=esc_html_e \
--keyword=esc_html_x \
--keyword=esc_attr__ \
--keyword=esc_attr_e \
--keyword=esc_attr_x \
--sort-by-file \
--package-version=1.0 \
--msgid-bugs-address="[email protected]" \
$(find . -name "*.php")
echo "Done!"
14 changes: 11 additions & 3 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Contributors: strangerstudios, kimannwall
Tags: display name, user, force, first name, last name
Requires at least: 3.0
Tested up to: 5.5
Stable tag: 1.1
Tested up to: 5.7
Stable tag: 1.2

Force the user field "display_name" to be set as the user's first and last name.

Expand All @@ -29,7 +29,11 @@ This plugin is now compatible with Paid Memberships Pro. The "Display Name" fiel

== Frequently Asked Questions ==

None yet.
= How do I update existing display names on my site? =
Navigate to Settings > Force First Last to run a script that will update all existing users.

= In my location, names are formatted as LAST FIRST. How do I switch the order? =
You will need to use a custom filter to adjust the display order. See this code for an example: https://gist.github.com/kimcoleman/dc3227b9cebec79fa983261827df7485.

== Screenshots ==

Expand All @@ -39,6 +43,10 @@ None yet.

== Changelog ==

= 1.2 - 2021-03-12
* ENHANCEMENT: Added `ffl_display_name_order` filter to allow custom code to change display name to last first.
* ENHANCEMENT: Tested up to WordPress 5.7.

= 1.1 - 2020-08-13 =
* ENHANCEMENT: Adding support for the Paid Memberships Pro frontend member profile edit screen.
* ENHANCEMENT: Improving the settings page appearance.
Expand Down

0 comments on commit 11b20ba

Please sign in to comment.