Skip to content

Instantly share code, notes, and snippets.

@panoslyrakis
Created January 24, 2017 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save panoslyrakis/9731b58f615c38c1cc290a2d10d1138e to your computer and use it in GitHub Desktop.
Save panoslyrakis/9731b58f615c38c1cc290a2d10d1138e to your computer and use it in GitHub Desktop.
Tranaslate confirmation emails of Appointments plugin. Requires to edit file in order to set the translations per language and service id as there is no gui
<?php
/**
* Plugin Name: Appointments confirmation WPML translate
* Plugin URI: https://premium.wpmudev.org/
* Description: Tranaslate confirmation emails of Appointments plugin. Requires to edit file in order to set the translations per language and service id as there is no gui
* Version: 1.0.0
* Author: Panos Lyrakis ( WPMUDEV )
* Author URI: https://premium.wpmudev.org/profile/panoskatws
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
//Can be used as MU Plugin
//Thread: https://premium.wpmudev.org/forums/topic/translation-of-confirmation-emails
if( ! class_exists( 'APP_NOTIFICATION_WPML' ) ){
class APP_NOTIFICATION_WPML{
private static $_instance = null;
public static function get_instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new APP_NOTIFICATION_WPML();
}
return self::$_instance;
}
private function __construct() {
add_filter( 'app_confirmation_message', array( $this, 'confirmation_message' ), 10 ,3 );
}
public function confirmation_message( $body, $app, $app_id ){
$appointments = appointments();
$r = appointments_get_appointment( $app_id );
if ( ! $r ) {
return false;
}
$location = appointments_get_location( $app->location );
$location_address = ( is_object( $location ) && isset( $location->address ) ) ? $location->address : '';
$args = array(
'user' => $app->name,
'service' => $appointments->get_service_name( $app->service ),
'worker' => appointments_get_worker_name( $app->worker ),
'datetime' => $app->start,
'price' => $app->price,
'deposit' => $appointments->get_deposit( $app->price ),
'phone' => $app->phone,
'note' => $app->note,
'address' => $app->address,
'email' => $app->email,
'city' => $app->city,
'location' => $location_address
);
$body = $this->confirmation_message_translate( $app->service, ICL_LANGUAGE_CODE );
$body = $this->replace_placeholders( $body, $args, 'confirmation-body', $app );
$subject = $this->confirmation_subject_translate( $app->service, ICL_LANGUAGE_CODE );
$subject = $this->replace_placeholders( $subject, $args, 'confirmation-subject', $r );
return array(
'subject' => $subject,
'body' => $body
);
}
/**
* Translate confirmation body
*/
public function confirmation_message_translate( $service_id, $lang = 'en' ){
$translations = array();
// For Service 1
$translations[ 1 ][ 'en' ] = 'My English confirmation message for service 1: DATE, LOCATION, DATE_TIME';
$translations[ 1 ][ 'de' ] = 'My German confirmation message for service 1: DATE, LOCATION, DATE_TIME';
// For Service 2
$translations[ 2 ][ 'en' ] = 'My English confirmation message for service 2: DATE, LOCATION, DATE_TIME';
$translations[ 2 ][ 'de' ] = 'My German confirmation message for service 2: DATE, LOCATION, DATE_TIME';
return $translations[ $service_id ][ $lang ];
}
/**
* Translate confirmation subject
*/
public function confirmation_subject_translate( $service_id, $lang = 'en' ){
$translations = array();
// For Service 1
$translations[ 1 ][ 'en' ] = 'My English confirmation subject for service 1';
$translations[ 1 ][ 'de' ] = 'My German confirmation subject for service 1';
// For Service 2
$translations[ 2 ][ 'en' ] = 'My English confirmation subject for service 2';
$translations[ 2 ][ 'de' ] = 'My German confirmation subject for service 2';
return $translations[ $service_id ][ $lang ];
}
function replace_placeholders( $text, $args, $notification_type, $object ) {
$defaults = array(
'user' => '',
'service' => '',
'worker' => '',
'datetime' => '',
'price' => '',
'deposit' => '',
'phone' => '',
'note' => '',
'address' => '',
'email' => '',
'city' => '',
'location' => ''
);
$args = wp_parse_args( $args, $defaults );
if ( ! empty( $args['price'] ) && ! empty( $args['deposit'] ) ) {
$args['balance'] = (float) $args['price'] - (float) $args['deposit'];
} else {
$args['balance'] = ! empty( $args['price'] ) ? $args['price'] : 0.0;
}
$replacement = array(
'/\bSITE_NAME\b/U' => wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ),
'/\bCLIENT\b/U' => $args['user'],
'/\bSERVICE_PROVIDER\b/U' => $args['worker'],
'/\bSERVICE\b/U' => preg_replace( '/\$(\d)/', '\\\$$1', $args['service'] ),
'/\bDATE_TIME\b/U' => mysql2date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $args['datetime'] ),
'/\bDATE\b/U' => mysql2date( get_option( 'date_format' ), $args['datetime'] ),
'/\bPRICE\b/U' => $args['price'],
'/\bDEPOSIT\b/U' => $args['deposit'],
'/\bBALANCE\b/U' => $args['balance'],
'/\bPHONE\b/U' => $args['phone'],
'/\bNOTE\b/U' => $args['note'],
'/\bADDRESS\b/U' => $args['address'],
'/\bEMAIL\b/U' => $args['email'],
'/\bCITY\b/U' => $args['city'],
'/\bLOCATION\b/U' => $args['location'],
);
$replacement = apply_filters( 'appointments_notification_replacements_wpml', $replacement, $notification_type, $text, $object );
foreach ( $replacement as $macro => $repl ) {
$text = preg_replace( $macro, $repl, $text );
}
return $text;
}
}
add_action( 'plugins_loaded', function(){
APP_NOTIFICATION_WPML::get_instance();
} );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment