PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.13
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.13
1.8.13 1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 1.7.0.9 All 211 releases
charitable / includes / compat / class-charitable-translatepress-compat.php

class-charitable-translatepress-compat.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.8.13, at includes/compat/class-charitable-translatepress-compat.php

124 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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) 2023, 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 ); // phpcs:ignore
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 ] ) ); // phpcs:ignore
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 ); // phpcs:ignore
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 // Translators: %s is the label of the recipient type.
113 $recipient_type['label'] = sprintf( __( '%s', 'charitable' ), $recipient_type['label'] ); // phpcs:ignore
114 // Translators: %s is the label of the description.
115 $recipient_type['description'] = sprintf( __( '%s', 'charitable' ), $recipient_type['description'] ); // phpcs:ignore
116
117 charitable_register_recipient_type( $option, $recipient_type );
118 }
119 }
120 }
121 }
122
123 endif;
124