AdminPageFramework_Message::__()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Admin Page Framework
4
 *
5
 * http://admin-page-framework.michaeluno.jp/
6
 * Copyright (c) 2013-2022, Michael Uno; Licensed MIT
7
 *
8
 */
9
10
/**
11
 * Provides methods for text messages.
12
 *
13
 * @since    2.0.0
14
 * @since    2.1.6       Multiple instances of this class are disallowed.
15
 * @since    3.2.0       Multiple instances of this class are allowed but the instantiation is restricted to per text domain basis.
16
 * @package  AdminPageFramework/Common/Factory/Property
17
 * @internal
18
 * @remark   When adding a new framework translation item,
19
 * Step 1: add a key and the default value to the `$aDefaults` property array.
20
 * Step 2: add a dummy function call in the `___doDummy()` method so that parser programs can catch it.
21
 */
22
class AdminPageFramework_Message {
23
24
    /**
25
     * Stores the framework's messages.
26
     *
27
     * @since  2.0.0
28
     * @since  3.1.3       No item is defined by default but done on the fly per request. The below array structure is kept for backward compatibility.
29
     * @remark The user may modify this property directly.
30
     */
31
    public $aMessages = array();
32
33
    /**
34
     * Stores default translated items.
35
     *
36
     * @remark      These items should be accessed only when its label needs to be displayed.
37
     * So the translation method `__()` only gets executed for one file.
38
     *
39
     * Consider the difference between the two.
40
     * <code>
41
     * $_aTranslations = array(
42
     *      'foo'  => __( 'Foo', 'admin-page-framework' ),
43
     *      'bar'  => __( 'Bar', 'admin-page-framework' ),
44
     *       ... more 100 items
45
     * )
46
     * return isset( $_aTranslations[ $sKey ] ) ? $_aTranslations[ $sKey ] : '';
47
     * </code>
48
     *
49
     * <code>
50
     * $_aTranslations = array(
51
     *      'foo'  => 'Foo',
52
     *      'bar'  => 'Bar',
53
     *       ... more 100 items
54
     * )
55
     * return isset( $_aTranslations[ $sKey ] )
56
     *      ? __( $_aTranslations[ $sKey ], $sUserSetTextdomain )
57
     *      : '';
58
     * </code>
59
     * @since       3.5.3
60
     */
61
    public $aDefaults = array(
62
63
        // AdminPageFramework
64
        'option_updated'                        => 'The options have been updated.',
65
        'option_cleared'                        => 'The options have been cleared.',
66
        'export'                                => 'Export',
67
        'export_options'                        => 'Export Options',
68
        'import'                                => 'Import',
69
        'import_options'                        => 'Import Options',
70
        'submit'                                => 'Submit',
71
        'import_error'                          => 'An error occurred while uploading the import file.',
72
        'uploaded_file_type_not_supported'      => 'The uploaded file type is not supported: %1$s',
73
        'could_not_load_importing_data'         => 'Could not load the importing data.',
74
        'imported_data'                         => 'The uploaded file has been imported.',
75
        'not_imported_data'                     => 'No data could be imported.',
76
        'upload_image'                          => 'Upload Image',
77
        'use_this_image'                        => 'Use This Image',
78
        'insert_from_url'                       => 'Insert from URL',
79
        'reset_options'                         => 'Are you sure you want to reset the options?',
80
        'confirm_perform_task'                  => 'Please confirm your action.',
81
        'specified_option_been_deleted'         => 'The specified options have been deleted.',
82
        'nonce_verification_failed'             => 'A problem occurred while processing the form data. Please try again.',
83
        'check_max_input_vars'                  => 'Not all form fields could not be sent. '
84
            . 'Please check your server settings of PHP <code>max_input_vars</code> and consult the server administrator to increase the value. '
85
            . '<code>max input vars</code>: %1$s. <code>$_POST</code> count: %2$s',  // 3.5.11+ // sanitization unnecessary as it is just a literal string
86
        'send_email'                            => 'Is it okay to send the email?',     // 3.3.0+
87
        'email_sent'                            => 'The email has been sent.',  // 3.3.0+, 3.3.5+ deprecated, 3.8.32 Re-added
88
        'email_scheduled'                       => 'The email has been scheduled.', // 3.3.5+, 3.8.32 deprecated
89
        'email_could_not_send'                  => 'There was a problem sending the email',     // 3.3.0+
90
91
        // AdminPageFramework_PostType
92
        'title'                                 => 'Title',
93
        'author'                                => 'Author',
94
        'categories'                            => 'Categories',
95
        'tags'                                  => 'Tags',
96
        'comments'                              => 'Comments',
97
        'date'                                  => 'Date',
98
        'show_all'                              => 'Show All',
99
        'show_all_authors'                      => 'Show all Authors', // 3.5.10+
100
101
        // AdminPageFramework_Link_Base
102
        'powered_by'                            => 'Thank you for creating with',
103
        'and'                                   => 'and',
104
105
        // AdminPageFramework_Link_admin_page
106
        'settings'                              => 'Settings',
107
108
        // AdminPageFramework_Link_post_type
109
        'manage'                                => 'Manage',
110
111
        // AdminPageFramework_FieldType_{...}
112
        'select_image'                          => 'Select Image',
113
        'upload_file'                           => 'Upload File',
114
        'use_this_file'                         => 'Use This File',
115
        'select_file'                           => 'Select File',
116
        'remove_value'                          => 'Remove Value',  // 3.2.0+
117
        'select_all'                            => 'Select All',    // 3.3.0+
118
        'select_none'                           => 'Select None',   // 3.3.0+
119
        'no_term_found'                         => 'No term found.', // 3.3.2+
120
121
        // AdminPageFramework_Form_View___Script_{...}
122
        'select'                                => 'Select', // 3.4.2+
123
        'insert'                                => 'Insert',  // 3.4.2+
124
        'use_this'                              => 'Use This', // 3.4.2+
125
        'return_to_library'                     => 'Return to Library', // 3.4.2+
126
127
        // AdminPageFramework_PageLoadInfo_Base
128
        'queries_in_seconds'                    => '%1$s queries in %2$s seconds.',
129
        'out_of_x_memory_used'                  => '%1$s out of %2$s (%3$s) memory used.',
130
        'peak_memory_usage'                     => 'Peak memory usage %1$s.',
131
        'initial_memory_usage'                  => 'Initial memory usage  %1$s.',
132
133
        // Repeatable sections & fields
134
        'repeatable_section_is_disabled'        => 'The ability to repeat sections is disabled.', // 3.8.13+
135
        'repeatable_field_is_disabled'          => 'The ability to repeat fields is disabled.',   // 3.8.13+
136
        'warning_caption'                       => 'Warning',   // 3.8.13+
137
138
        // AdminPageFramework_FormField
139
        'allowed_maximum_number_of_fields'      => 'The allowed maximum number of fields is {0}.',
140
        'allowed_minimum_number_of_fields'      => 'The allowed minimum number of fields is {0}.',
141
        'add'                                   => 'Add',
142
        'remove'                                => 'Remove',
143
144
        // AdminPageFramework_FormPart_Table
145
        'allowed_maximum_number_of_sections'    => 'The allowed maximum number of sections is {0}',
146
        'allowed_minimum_number_of_sections'    => 'The allowed minimum number of sections is {0}',
147
        'add_section'                           => 'Add Section',
148
        'remove_section'                        => 'Remove Section',
149
        'toggle_all'                            => 'Toggle All',
150
        'toggle_all_collapsible_sections'       => 'Toggle all collapsible sections',
151
152
        // AdminPageFramework_FieldType_reset 3.3.0+
153
        'reset'                                 => 'Reset',
154
155
        // AdminPageFramework_FieldType_system 3.5.3+
156
        'yes'                                   => 'Yes',
157
        'no'                                    => 'No',
158
        'on'                                    => 'On',
159
        'off'                                   => 'Off',
160
        'enabled'                               => 'Enabled',
161
        'disabled'                              => 'Disabled',
162
        'supported'                             => 'Supported',
163
        'not_supported'                         => 'Not Supported',
164
        'functional'                            => 'Functional',
165
        'not_functional'                        => 'Not Functional',
166
        'too_long'                              => 'Too Long',
167
        'acceptable'                            => 'Acceptable',
168
        'no_log_found'                          => 'No log found.',
169
170
        // 3.7.0+ - accessed from `AdminPageFramework_Form`
171
        'method_called_too_early'               => 'The method is called too early.',
172
173
        // 3.7.0+  - accessed from `AdminPageFramework_Form_View___DebugInfo`
174
        'debug_info'                            => 'Debug Info',
175
        // 3.8.5+
176
        'debug'                                 => 'Debug',
177
        // 'field_arguments'                       => 'Field Arguments', // @deprecated 3.8.22
178
        'debug_info_will_be_disabled'           => 'This information will be disabled when <code>WP_DEBUG</code> is set to <code>false</code> in <code>wp-config.php</code>.',
179
180
        // 'section_arguments'                     => 'Section Arguments', // 3.8.8+   // @deprecated 3.8.22
181
182
        'click_to_expand'                       => 'Click here to expand to view the contents.',
183
        'click_to_collapse'                     => 'Click here to collapse the contents.',
184
185
        // 3.7.0+ - displayed while the page laods
186
        'loading'                               => 'Loading...',
187
        'please_enable_javascript'              => 'Please enable JavaScript for better user experience.',
188
189
        'submit_confirmation_label'             => 'Submit the form.',
190
        'submit_confirmation_error'             => 'Please check this box if you want to proceed.',
191
        'import_no_file'                        => 'No file is selected.',
192
193
        // 3.9.0
194
        'please_fill_out_this_field'            => 'Please fill out this field.',
195
196
    );
197
198
    /**
199
     * Stores the text domain.
200
     * @since 3.x
201
     * @since 3.5.0       Declared as a default property.
202
     */
203
    protected $_sTextDomain = 'admin-page-framework';
204
205
    /**
206
     * Stores the self instance by text domain.
207
     * @internal
208
     * @since    3.2.0
209
     */
210
    static private $_aInstancesByTextDomain = array();
211
212
    /**
213
     * Ensures that only one instance of this class object exists. ( no multiple instances of this object )
214
     *
215
     * @since       2.1.6
216
     * @since       3.2.0       Changed it to create an instance per text domain basis.
217
     * @param       string      $sTextDomain
218
     * @remark      This class should be instantiated via this method.
219
     * @return      AdminPageFramework_Message
220
     */
221
    public static function getInstance( $sTextDomain='admin-page-framework' ) {
222
223
        $_oInstance = isset( self::$_aInstancesByTextDomain[ $sTextDomain ] ) && ( self::$_aInstancesByTextDomain[ $sTextDomain ] instanceof AdminPageFramework_Message )
224
            ? self::$_aInstancesByTextDomain[ $sTextDomain ]
225
            : new AdminPageFramework_Message( $sTextDomain );
226
        self::$_aInstancesByTextDomain[ $sTextDomain ] = $_oInstance;
227
        return self::$_aInstancesByTextDomain[ $sTextDomain ];
228
229
    }
230
        /**
231
         * Ensures that only one instance of this class object exists. ( no multiple instances of this object )
232
         * @deprecated  3.2.0
233
         */
234
        public static function instantiate( $sTextDomain='admin-page-framework' ) {
235
            return self::getInstance( $sTextDomain );
236
        }
237
238
    /**
239
     * Sets up properties.
240
     * @param string $sTextDomain
241
     */
242
    public function __construct( $sTextDomain='admin-page-framework' ) {
243
244
        $this->_sTextDomain = $sTextDomain;
245
246
        // Fill the $aMessages property with the keys extracted from the $aDefaults property
247
        // with the value of null.  The null is set to let it trigger the __get() method
248
        // so that each translation item gets processed individually.
249
        $this->aMessages    = array_fill_keys(
250
            array_keys( $this->aDefaults ),
251
            null
252
        );
253
254
    }
255
256
    /**
257
     * Returns the set text domain string.
258
     *
259
     * This is used from field type and input classes to display deprecated admin errors/
260
     *
261
     * @since 3.3.3
262
     */
263
    public function getTextDomain() {
264
        return $this->_sTextDomain;
265
    }
266
267
    /**
268
     * Sets a message for the given key.
269
     * @since       3.7.0
270
     */
271
    public function set( $sKey, $sValue ) {
272
        $this->aMessages[ $sKey ] = $sValue;
273
    }
274
275
    /**
276
     * Returns the framework system message by key.
277
     *
278
     * @remark An alias of the __() method.
279
     * @since  3.2.0
280
     * @since  3.7.0        If no key is specified, return the entire mesage array.
281
     * @param  string       $sKey
282
     * @return string|array
283
     */
284
    public function get( $sKey='' ) {
285
        if ( ! $sKey ) {
286
            return $this->_getAllMessages();
287
        }
288
        return isset( $this->aMessages[ $sKey ] )
289
            ? __( $this->aMessages[ $sKey ], $this->_sTextDomain )
290
            : __( $this->{$sKey}, $this->_sTextDomain );     // triggers __get()
291
    }
292
        /**
293
         * Returns the all registered messag items.
294
         * By default, no item is set for a performance reason; the message is retuned on the fly.
295
         * So all the keys must be iterated to get all the values.
296
         * @since       3.7.0
297
         * @return      array
298
         */
299
        private function _getAllMessages() {
300
            $_aMessages = array();
301
            foreach ( $this->aMessages as $_sLabel => $_sTranslation ) {
302
                $_aMessages[ $_sLabel ] = $this->get( $_sLabel );
303
            }
304
            return $_aMessages;
305
        }
306
307
    /**
308
     * Echoes the framework system message by key.
309
     * @remark An alias of the _e() method.
310
     * @since  3.2.0
311
     */
312
    public function output( $sKey ) {
313
        echo $this->get( $sKey );
0 ignored issues
show
Bug introduced by
Are you sure $this->get($sKey) of type array|string can be used in echo? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

313
        echo /** @scrutinizer ignore-type */ $this->get( $sKey );
Loading history...
314
    }
315
316
        /**
317
         * Returns the framework system message by key.
318
         * @since       2.x
319
         * @deprecated  3.2.0
320
         */
321
        public function __( $sKey ) {
322
            return $this->get( $sKey );
323
        }
324
        /**
325
         * Echoes the framework system message by key.
326
         * @since       2.x
327
         * @deprecated  3.2.0
328
         */
329
        public function _e( $sKey ) {
330
            $this->output( $sKey );
331
        }
332
333
    /**
334
     * Responds to a request to an undefined property.
335
     *
336
     * @since  3.1.3
337
     * @return string
338
     */
339
    public function __get( $sPropertyName ) {
340
        return isset( $this->aDefaults[ $sPropertyName ] ) ? $this->aDefaults[ $sPropertyName ] : $sPropertyName;
341
    }
342
343
344
    /**
345
     * A dummy method just lists translation items to be parsed by translation programs such as POEdit.
346
     *
347
     * @since 3.5.3
348
     * @since 3.8.19 Changed the name to avoid false-positives of PHP 7.2 incompatibility by third party tools.
349
     */
350
    private function ___doDummy() {
351
352
        __( 'The options have been updated.', 'admin-page-framework' );
353
        __( 'The options have been cleared.', 'admin-page-framework' );
354
        __( 'Export', 'admin-page-framework' );
355
        __( 'Export Options', 'admin-page-framework' );
356
        __( 'Import', 'admin-page-framework' );
357
        __( 'Import Options', 'admin-page-framework' );
358
        __( 'Submit', 'admin-page-framework' );
359
        __( 'An error occurred while uploading the import file.', 'admin-page-framework' );
360
        /* translators: 1: Uploaded file type */
361
        __( 'The uploaded file type is not supported: %1$s', 'admin-page-framework' );
362
        __( 'Could not load the importing data.', 'admin-page-framework' );
363
        __( 'The uploaded file has been imported.', 'admin-page-framework' );
364
        __( 'No data could be imported.', 'admin-page-framework' );
365
        __( 'Upload Image', 'admin-page-framework' );
366
        __( 'Use This Image', 'admin-page-framework' );
367
        __( 'Insert from URL', 'admin-page-framework' );
368
        __( 'Are you sure you want to reset the options?', 'admin-page-framework' );
369
        __( 'Please confirm your action.', 'admin-page-framework' );
370
        __( 'The specified options have been deleted.', 'admin-page-framework' );
371
        __( 'A problem occurred while processing the form data. Please try again.', 'admin-page-framework' );
372
        /* translators: 1: The value of max_input_vars set by PHP 2: Actual $_POST element count */
373
        __( 'Not all form fields could not be sent. Please check your server settings of PHP <code>max_input_vars</code> and consult the server administrator to increase the value. <code>max input vars</code>: %1$s. <code>$_POST</code> count: %2$s', 'admin-page-framework' ); // sanitization unnecessary as a literal string
374
        __( 'Is it okay to send the email?', 'admin-page-framework' );
375
        __( 'The email has been sent.', 'admin-page-framework' );
376
        __( 'The email has been scheduled.', 'admin-page-framework' );
377
        __( 'There was a problem sending the email', 'admin-page-framework' );
378
        __( 'Title', 'admin-page-framework' );
379
        __( 'Author', 'admin-page-framework' );
380
        __( 'Categories', 'admin-page-framework' );
381
        __( 'Tags', 'admin-page-framework' );
382
        __( 'Comments', 'admin-page-framework' );
383
        __( 'Date', 'admin-page-framework' );
384
        __( 'Show All', 'admin-page-framework' );
385
        __( 'Show All Authors', 'admin-page-framework' );
386
        __( 'Thank you for creating with', 'admin-page-framework' );
387
        __( 'and', 'admin-page-framework' );
388
        __( 'Settings', 'admin-page-framework' );
389
        __( 'Manage', 'admin-page-framework' );
390
        __( 'Select Image', 'admin-page-framework' );
391
        __( 'Upload File', 'admin-page-framework' );
392
        __( 'Use This File', 'admin-page-framework' );
393
        __( 'Select File', 'admin-page-framework' );
394
        __( 'Remove Value', 'admin-page-framework' );
395
        __( 'Select All', 'admin-page-framework' );
396
        __( 'Select None', 'admin-page-framework' );
397
        __( 'No term found.', 'admin-page-framework' );
398
        __( 'Select', 'admin-page-framework' );
399
        __( 'Insert', 'admin-page-framework' );
400
        __( 'Use This', 'admin-page-framework' );
401
        __( 'Return to Library', 'admin-page-framework' );
402
        /* translators: 1: Number of performed database queries 2: Elapsed seconds for page load */
403
        __( '%1$s queries in %2$s seconds.', 'admin-page-framework' );
404
        /* translators: 1: Used memory amount 2: Max memory cap set by WordPress (WP_MEMORY_LIMIT) 3: Percentage of the memory usage */
405
        __( '%1$s out of %2$s MB (%3$s) memory used.', 'admin-page-framework' );
406
        /* translators: 1: Peak memory usage amount */
407
        __( 'Peak memory usage %1$s MB.', 'admin-page-framework' );
408
        /* translators: 1: Initial memory usage amount */
409
        __( 'Initial memory usage  %1$s MB.', 'admin-page-framework' );
410
        __( 'The allowed maximum number of fields is {0}.', 'admin-page-framework' );
411
        __( 'The allowed minimum number of fields is {0}.', 'admin-page-framework' );
412
        __( 'Add', 'admin-page-framework' );
413
        __( 'Remove', 'admin-page-framework' );
414
        __( 'The allowed maximum number of sections is {0}', 'admin-page-framework' );
415
        __( 'The allowed minimum number of sections is {0}', 'admin-page-framework' );
416
        __( 'Add Section', 'admin-page-framework' );
417
        __( 'Remove Section', 'admin-page-framework' );
418
        __( 'Toggle All', 'admin-page-framework' );
419
        __( 'Toggle all collapsible sections', 'admin-page-framework' );
420
        __( 'Reset', 'admin-page-framework' );
421
        __( 'Yes', 'admin-page-framework' );
422
        __( 'No', 'admin-page-framework' );
423
        __( 'On', 'admin-page-framework' );
424
        __( 'Off', 'admin-page-framework' );
425
        __( 'Enabled', 'admin-page-framework' );
426
        __( 'Disabled', 'admin-page-framework' );
427
        __( 'Supported', 'admin-page-framework' );
428
        __( 'Not Supported', 'admin-page-framework' );
429
        __( 'Functional', 'admin-page-framework' );
430
        __( 'Not Functional', 'admin-page-framework' );
431
        __( 'Too Long', 'admin-page-framework' );
432
        __( 'Acceptable', 'admin-page-framework' );
433
        __( 'No log found.', 'admin-page-framework' );
434
435
        /* translators: 1: Method name */
436
        __( 'The method is called too early: %1$s', 'admin-page-framework' );
437
        __( 'Debug Info', 'admin-page-framework' );
438
439
        __( 'Click here to expand to view the contents.', 'admin-page-framework' );
440
        __( 'Click here to collapse the contents.', 'admin-page-framework' );
441
442
        __( 'Loading...', 'admin-page-framework' );
443
        __( 'Please enable JavaScript for better user experience.', 'admin-page-framework' );
444
445
        __( 'Debug', 'admin-page-framework' );
446
        // __( 'Field Arguments', 'admin-page-framework' ); @deprecated 3.8.22
447
        __( 'This information will be disabled when <code>WP_DEBUG</code> is set to <code>false</code> in <code>wp-config.php</code>.', 'admin-page-framework' );
448
449
        // __( 'Section Arguments', 'admin-page-framework' ); // 3.8.8+ @deprecated 3.8.22
450
451
        __( 'The ability to repeat sections is disabled.', 'admin-page-framework' ); // 3.8.13+
452
        __( 'The ability to repeat fields is disabled.', 'admin-page-framework' ); // 3.8.13+
453
        __( 'Warning.', 'admin-page-framework' ); // 3.8.13+
454
455
        __( 'Submit the form.', 'admin-page-framework' ); // 3.8.24
456
        __( 'Please check this box if you want to proceed.', 'admin-page-framework' ); // 3.8.24
457
        __( 'No file is selected.', 'admin-page-framework' ); // 3.8.24
458
459
        __( 'Please fill out this field.', 'admin-page-framework' ); // 3.9.0
460
461
    }
462
463
}