| 1 |
<?php |
| 2 |
/** |
| 3 |
* A class to resolve compatibility issues with Translatepress. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Translatepress_Compat |
| 6 |
* @author David Bisset |
| 7 |
* @copyright Copyright (c) 2022, WP Charitable LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.6.48 |
| 10 |
* @version 1.6.48 |
| 11 |
*/ |
| 12 |
|
| 13 |
// Exit if accessed directly. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
if ( ! class_exists( 'Charitable_Translatepress_Compat' ) ) : |
| 19 |
|
| 20 |
/** |
| 21 |
* Charitable_Translatepress_Compat |
| 22 |
* |
| 23 |
* @since 1.6.48 |
| 24 |
*/ |
| 25 |
class Charitable_Translatepress_Compat { |
| 26 |
|
| 27 |
/** |
| 28 |
* Create class object. |
| 29 |
* |
| 30 |
* @since 1.6.48 |
| 31 |
*/ |
| 32 |
public function __construct() { |
| 33 |
/* Load Donation & Campaign field translations late */ |
| 34 |
add_action( 'wp_head', array( $this, 'load_late_translations' ), 200 ); |
| 35 |
|
| 36 |
/** |
| 37 |
* Do something with this class instance. |
| 38 |
* |
| 39 |
* @since 1.6.48 |
| 40 |
* |
| 41 |
* @param Charitable_Translatepress_Compat $helper Translatepress compatibility class instance. |
| 42 |
*/ |
| 43 |
do_action( 'charitable_translatepress_compat', $this ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* TranslatePress shows translateable gettext strings by collecting any gettext calls made |
| 48 |
* after the wp_head hook (priority 100), so here we deliberately run through the list of |
| 49 |
* campaign & donation fields and re-run them through a call to __(), which forces them to |
| 50 |
* be picked up by TranslatePress. |
| 51 |
* |
| 52 |
* @since 1.6.48 |
| 53 |
* |
| 54 |
* @return void |
| 55 |
*/ |
| 56 |
public function load_late_translations() { |
| 57 |
$field_apis = array( |
| 58 |
array( |
| 59 |
'fields' => charitable()->donation_fields(), |
| 60 |
'forms' => array( 'donation_form', 'admin_form' ), |
| 61 |
), |
| 62 |
array( |
| 63 |
'fields' => charitable()->campaign_fields(), |
| 64 |
'forms' => array( 'campaign_form', 'admin_form' ), |
| 65 |
), |
| 66 |
); |
| 67 |
|
| 68 |
$translateable_form_fields = array( 'label', 'placeholder' ); |
| 69 |
|
| 70 |
foreach ( $field_apis as $api ) { |
| 71 |
$fields = $api['fields']; |
| 72 |
|
| 73 |
foreach ( $fields->get_fields() as $field ) { |
| 74 |
|
| 75 |
/* Update the field label. */ |
| 76 |
$field->label = __( $field->label ); |
| 77 |
|
| 78 |
foreach ( $api['forms'] as $form ) { |
| 79 |
$form_settings = $field->$form; |
| 80 |
|
| 81 |
if ( ! is_array( $form_settings ) ) { |
| 82 |
continue; |
| 83 |
} |
| 84 |
|
| 85 |
/* Translate form label and placeholder. */ |
| 86 |
foreach ( $translateable_form_fields as $form_field ) { |
| 87 |
if ( array_key_exists( $form_field, $form_settings ) ) { |
| 88 |
$field->set( $form, $form_field, __( $form_settings[ $form_field ] ) ); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/* Translate options */ |
| 93 |
if ( array_key_exists( 'options', $form_settings ) && is_array( $form_settings['options'] ) ) { |
| 94 |
$options = $form_settings['options']; |
| 95 |
|
| 96 |
foreach ( $options as $key => $value ) { |
| 97 |
$options[ $key ] = __( $value ); |
| 98 |
} |
| 99 |
|
| 100 |
if ( in_array( $field->field, array( 'country', 'state' ) ) ) { |
| 101 |
asort( $options ); |
| 102 |
} |
| 103 |
|
| 104 |
$field->set( $form, 'options', $options ); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
if ( function_exists( 'charitable_get_recipient_types' ) ) { |
| 111 |
foreach ( charitable_get_recipient_types() as $option => $recipient_type ) { |
| 112 |
$recipient_type['label'] = __( $recipient_type['label'], 'charitable-ambassadors' ); |
| 113 |
$recipient_type['description'] = __( $recipient_type['description'], 'charitable-ambassadors' ); |
| 114 |
|
| 115 |
charitable_register_recipient_type( $option, $recipient_type ); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
endif; |
| 122 |
|