Completed
Branch dev (d064b2)
by
unknown
24:08 queued 04:45
created

AdminPageFramework_Message::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
/**
3
 * Admin Page Framework
4
 * 
5
 * http://en.michaeluno.jp/admin-page-framework/
6
 * Copyright (c) 2013-2015 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
17
 * @subpackage  Property
18
 * @internal
19
 * 
20
 * @remark      When adding a new framework translation item,
21
 * Step 1: add a key and the default value to the `$aDefaults` property array.
22
 * Step 2: add a dummy function call in the `__doDummy()` method so that parser programs can catch it.
23
 */
24
class AdminPageFramework_Message {
1 ignored issue
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
25
26
    /**
27
     * Stores the framework's messages.
28
     * 
29
     * @since       2.0.0
30
     * @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.
31
     * @remark      The user may modify this property directly.
32
     */ 
33
    public $aMessages = array();
34
    
35
    /**
36
     * Stores default translated items.
37
     * 
38
     * @remark      These items should be accessed only when its label needs to be displayed.
39
     * So the translation method `__()` only gets executed for one file.
40
     * 
41
     * Consider the difference between the two.
42
     * <code>
43
     * $_aTranslations = array(
44
     *      'foo'  => __( 'Foo', 'admin-page-framework' ),
45
     *      'bar'  => __( 'Bar', 'admin-page-framework' ),
46
     *       ... more 100 items 
47
     * )
48
     * return isset( $_aTranslations[ $sKey ] ) ? $_aTranslations[ $sKey ] : '';
49
     * </code>
50
     * 
51
     * <code>
52
     * $_aTranslations = array(
53
     *      'foo'  => 'Foo',
54
     *      'bar'  => 'Bar', 
55
     *       ... more 100 items 
56
     * )
57
     * return isset( $_aTranslations[ $sKey ] ) 
58
     *      ? __( $_aTranslations[ $sKey ], $sUserSetTextdomain ) 
59
     *      : '';
60
     * </code>
61
     * @since       3.5.3
62
     */
63
    public $aDefaults = array(
64
65
        // AdminPageFramework
66
        'option_updated'                        => 'The options have been updated.',
67
        'option_cleared'                        => 'The options have been cleared.', 
68
        'export'                                => 'Export', 
69
        'export_options'                        => 'Export Options', 
70
        'import_options'                        => 'Import', 
71
        'import_options'                        => 'Import Options', 
72
        'submit'                                => 'Submit', 
73
        'import_error'                          => 'An error occurred while uploading the import file.', 
74
        'uploaded_file_type_not_supported'      => 'The uploaded file type is not supported: %1$s', 
75
        'could_not_load_importing_data'         => 'Could not load the importing data.', 
76
        'imported_data'                         => 'The uploaded file has been imported.', 
77
        'not_imported_data'                     => 'No data could be imported.', 
78
        'upload_image'                          => 'Upload Image', 
79
        'use_this_image'                        => 'Use This Image', 
80
        'insert_from_url'                       => 'Insert from URL', 
81
        'reset_options'                         => 'Are you sure you want to reset the options?', 
82
        'confirm_perform_task'                  => 'Please confirm your action.', 
83
        'specified_option_been_deleted'         => 'The specified options have been deleted.', 
84
        'nonce_verification_failed'             => 'A problem occurred while processing the form data. Please try again.', 
85
        'check_max_input_vars'                  => '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',  // 3.5.11+
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 
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
88
        'email_scheduled'                       => 'The email has been scheduled.', // 3.3.5+
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'                            => 'Powered by', 
103
104
        // AdminPageFramework_Link_Page
105
        'settings'                              => 'Settings', 
106
107
        // AdminPageFramework_Link_PostType
108
        'manage'                                => 'Manage', 
109
110
        // AdminPageFramework_FieldType_{...}
111
        'select_image'                          => 'Select Image', 
112
        'upload_file'                           => 'Upload File', 
113
        'use_this_file'                         => 'Use This File', 
114
        'select_file'                           => 'Select File', 
115
        'remove_value'                          => 'Remove Value',  // 3.2.0+
116
        'select_all'                            => 'Select All',    // 3.3.0+
117
        'select_none'                           => 'Select None',   // 3.3.0+                       
118
        'no_term_found'                         => 'No term found.', // 3.3.2+
119
120
        // AdminPageFramework_Form_View___Script_{...}
121
        'select'                                => 'Select', // 3.4.2+             
122
        'insert'                                => 'Insert',  // 3.4.2+
123
        'use_this'                              => 'Use This', // 3.4.2+
124
        'return_to_library'                     => 'Return to Library', // 3.4.2+
125
            
126
        // AdminPageFramework_PageLoadInfo_Base
127
        'queries_in_seconds'                    => '%1$s queries in %2$s seconds.', 
128
        'out_of_x_memory_used'                  => '%1$s out of %2$s MB (%3$s) memory used.', 
129
        'peak_memory_usage'                     => 'Peak memory usage %1$s MB.', 
130
        'initial_memory_usage'                  => 'Initial memory usage  %1$s MB.', 
131
        
132
        // AdminPageFramework_FormField
133
        'allowed_maximum_number_of_fields'      => 'The allowed maximum number of fields is {0}.', 
134
        'allowed_minimum_number_of_fields'      => 'The allowed minimum number of fields is {0}.', 
135
        'add'                                   => 'Add', 
136
        'remove'                                => 'Remove', 
137
138
        // AdminPageFramework_FormPart_Table
139
        'allowed_maximum_number_of_sections'    => 'The allowed maximum number of sections is {0}', 
140
        'allowed_minimum_number_of_sections'    => 'The allowed minimum number of sections is {0}', 
141
        'add_section'                           => 'Add Section', 
142
        'remove_section'                        => 'Remove Section', 
143
        'toggle_all'                            => 'Toggle All', 
144
        'toggle_all_collapsible_sections'       => 'Toggle all collapsible sections', 
145
            
146
        // AdminPageFramework_FieldType_reset 3.3.0+
147
        'reset'                                 => 'Reset', 
148
            
149
        // AdminPageFramework_FieldType_system 3.5.3+
150
        'yes'                                   => 'Yes', 
151
        'no'                                    => 'No', 
152
        'on'                                    => 'On', 
153
        'off'                                   => 'Off', 
154
        'enabled'                               => 'Enabled', 
155
        'disabled'                              => 'Disabled', 
156
        'supported'                             => 'Supported', 
157
        'not_supported'                         => 'Not Supported', 
158
        'functional'                            => 'Functional', 
159
        'not_functional'                        => 'Not Functional', 
160
        'too_long'                              => 'Too Long', 
161
        'acceptable'                            => 'Acceptable', 
162
        'no_log_found'                          => 'No log found.',
163
        
164
        // DEVVER+ - accessed from `AdminPageFramework_Form`
165
        'method_called_too_early'               => 'The method is called too early.',
166
        
167
        // DEVVER+  - accessed from `AdminPageFramework_Form_View___DebugInfo`
168
        'debug_info'                            => 'Debug Info',
169
        
170
        'click_to_expand'                       => 'Click here to expand to view the contents.',
171
        'click_to_collapse'                     => 'Click here to collapse the contents.',
172
        
173
        // DEVVER+ - displayed while the page laods
174
        'loading'                               => 'Loading...',
175
        'please_enable_javascript'              => 'Please enable JavaScript for better experience.'
176
    );
177
    
178
    /**
179
     * Stores the text domain.
180
     * @since       3.x
181
     * @since       3.5.0       Declared as a default property.
182
     */
183
    protected $_sTextDomain = 'admin-page-framework';
184
    
185
    /**
186
     * Stores the self instance by text domain.
187
     * @internal
188
     * @since       3.2.0
189
     */
190
    static private $_aInstancesByTextDomain = array();
191
   
192
    /**
193
     * Ensures that only one instance of this class object exists. ( no multiple instances of this object ) 
194
     * 
195
     * @since       2.1.6
196
     * @since       3.2.0       Changed it to create an instance per text domain basis.
197
     * @remark      This class should be instantiated via this method.
198
     */
199
    public static function getInstance( $sTextDomain='admin-page-framework' ) {
200
        
201
        $_oInstance = isset( self::$_aInstancesByTextDomain[ $sTextDomain ] ) && ( self::$_aInstancesByTextDomain[ $sTextDomain ] instanceof AdminPageFramework_Message )
202
            ? self::$_aInstancesByTextDomain[ $sTextDomain ]
203
            : new AdminPageFramework_Message( $sTextDomain );
204
        self::$_aInstancesByTextDomain[ $sTextDomain ] = $_oInstance;
205
        return self::$_aInstancesByTextDomain[ $sTextDomain ];
206
        
207
    }    
208
        /**
209
         * Ensures that only one instance of this class object exists. ( no multiple instances of this object ) 
210
         * @deprecated  3.2.0
211
         */
212
        public static function instantiate( $sTextDomain='admin-page-framework' ) {
213
            return self::getInstance( $sTextDomain );
214
        }
215
        
216
    /**
217
     * Sets up properties.
218
     */
219
    public function __construct( $sTextDomain='admin-page-framework' ) {
220
        
221
        $this->_sTextDomain = $sTextDomain;  
222
        
223
        // Fill the $aMessages property with the keys extracted from the $aDefaults property
224
        // with the value of null.  The null is set to let it trigger the __get() method 
225
        // so that each translation item gets processed individually.
226
        $this->aMessages    = array_fill_keys( 
227
            array_keys( $this->aDefaults ),
228
            null        
229
        );
230
        
231
    }
232
    
233
    /**
234
     * Returns the set text domain string.
235
     * 
236
     * This is used from field type and input classes to display deprecated admin errors/
237
     * 
238
     * @since       3.3.3
239
     */
240
    public function getTextDomain() {
241
        return $this->_sTextDomain;
242
    }
243
    
244
    /**
245
     * Sets a message for the given key.
246
     * @since       DEVVER
247
     */
248
    public function set( $sKey, $sValue ) {
249
        $this->aMessages[ $sKey ] = $sValue;
250
    }
251
    
252
    /**
253
     * Returns the framework system message by key.
254
     * 
255
     * @remark      An alias of the __() method.
256
     * @since       3.2.0
257
     * @since       DEVVER      If no key is specified, return the entire mesage array.
258
     * @return      string|array
259
     */
260
    public function get( $sKey='' ) {
261
        if ( ! $sKey ) {
262
            return $this->_getAllMessages();
263
        }
264
        return isset( $this->aMessages[ $sKey ] )
265
            ? __( $this->aMessages[ $sKey ], $this->_sTextDomain )
266
            : __( $this->{$sKey}, $this->_sTextDomain );     // triggers __get()
267
    }
268
        /**
269
         * Returns the all registered messag items.
270
         * By default, no item is set for a performance reason; the message is retuned on the fly. 
271
         * So all the keys must be iterated to get all the values.
272
         * @since       DEVVER
273
         * @return      array
274
         */
275
        private function _getAllMessages() {
276
            $_aMessages = array();
277
            foreach ( $this->aMessages as $_sLabel => $_sTranslation ) {
278
                $_aMessages[ $_sLabel ] = $this->get( $_sLabel );
279
            }
280
            return $_aMessages;
281
        }
282
283
    /**
284
     * Echoes the framework system message by key.
285
     * @remark  An alias of the _e() method.
286
     * @since   3.2.0
287
     */    
288
    public function output( $sKey ) {
289
        echo $this->get( $sKey );
290
    }   
291
        
292
        /**
293
         * Returns the framework system message by key.
294
         * @since       2.x
295
         * @deprecated  3.2.0
296
         */
297
        public function __( $sKey ) {
298
            return $this->get( $sKey );
299
        }       
300
        /**
301
         * Echoes the framework system message by key.
302
         * @since       2.x
303
         * @deprecated  3.2.0
304
         */    
305
        public function _e( $sKey ) {
306
            $this->output( $sKey );
307
        }
308
    
309
    /**
310
     * Responds to a request to an undefined property.
311
     * 
312
     * @since       3.1.3
313
     * @return      string
314
     */
315
    public function __get( $sPropertyName ) {
316
        return isset( $this->aDefaults[ $sPropertyName ] ) ? $this->aDefaults[ $sPropertyName ] : $sPropertyName;
317
    }
318
   
319
        
320
    /**
321
     * A dummy method just lists translation items to be parsed by translation programs such as POEdit.
322
     * 
323
     * @since       3.5.3
324
     */
325
    private function __doDummy() {
326
327
        __( 'The options have been updated.', 'admin-page-framework' ); 
328
        __( 'The options have been cleared.', 'admin-page-framework' );
329
        __( 'Export', 'admin-page-framework' );
330
        __( 'Export Options', 'admin-page-framework' );
331
        __( 'Import', 'admin-page-framework' );
332
        __( 'Import Options', 'admin-page-framework' );
333
        __( 'Submit', 'admin-page-framework' );
334
        __( 'An error occurred while uploading the import file.', 'admin-page-framework' );
335
        __( 'The uploaded file type is not supported: %1$s', 'admin-page-framework' );
336
        __( 'Could not load the importing data.', 'admin-page-framework' );
337
        __( 'The uploaded file has been imported.', 'admin-page-framework' );
338
        __( 'No data could be imported.', 'admin-page-framework' );
339
        __( 'Upload Image', 'admin-page-framework' );
340
        __( 'Use This Image', 'admin-page-framework' );
341
        __( 'Insert from URL', 'admin-page-framework' );
342
        __( 'Are you sure you want to reset the options?', 'admin-page-framework' );
343
        __( 'Please confirm your action.', 'admin-page-framework' );
344
        __( 'The specified options have been deleted.', 'admin-page-framework' );
345
        __( 'A problem occurred while processing the form data. Please try again.', 'admin-page-framework' );
346
        __( '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' );
347
        __( 'Is it okay to send the email?', 'admin-page-framework' );
348
        __( 'The email has been sent.', 'admin-page-framework' );
349
        __( 'The email has been scheduled.', 'admin-page-framework' );                    
350
        __( 'There was a problem sending the email', 'admin-page-framework' );
351
        __( 'Title', 'admin-page-framework' );    
352
        __( 'Author', 'admin-page-framework' );    
353
        __( 'Categories', 'admin-page-framework' );
354
        __( 'Tags', 'admin-page-framework' );
355
        __( 'Comments', 'admin-page-framework' );
356
        __( 'Date', 'admin-page-framework' ); 
357
        __( 'Show All', 'admin-page-framework' );
358
        __( 'Show All Authors', 'admin-page-framework' );
359
        __( 'Powered by', 'admin-page-framework' );
360
        __( 'Settings', 'admin-page-framework' );
361
        __( 'Manage', 'admin-page-framework' );
362
        __( 'Select Image', 'admin-page-framework' );
363
        __( 'Upload File', 'admin-page-framework' );
364
        __( 'Use This File', 'admin-page-framework' );
365
        __( 'Select File', 'admin-page-framework' );
366
        __( 'Remove Value', 'admin-page-framework' );
367
        __( 'Select All', 'admin-page-framework' );
368
        __( 'Select None', 'admin-page-framework' );          
369
        __( 'No term found.', 'admin-page-framework' );
370
        __( 'Select', 'admin-page-framework' );          
371
        __( 'Insert', 'admin-page-framework' );                              
372
        __( 'Use This', 'admin-page-framework' );                   
373
        __( 'Return to Library', 'admin-page-framework' );
374
        __( '%1$s queries in %2$s seconds.', 'admin-page-framework' );
375
        __( '%1$s out of %2$s MB (%3$s) memory used.', 'admin-page-framework' );
376
        __( 'Peak memory usage %1$s MB.', 'admin-page-framework' );
377
        __( 'Initial memory usage  %1$s MB.', 'admin-page-framework' );
378
        __( 'The allowed maximum number of fields is {0}.', 'admin-page-framework' );
379
        __( 'The allowed minimum number of fields is {0}.', 'admin-page-framework' );
380
        __( 'Add', 'admin-page-framework' );
381
        __( 'Remove', 'admin-page-framework' );
382
        __( 'The allowed maximum number of sections is {0}', 'admin-page-framework' );
383
        __( 'The allowed minimum number of sections is {0}', 'admin-page-framework' );
384
        __( 'Add Section', 'admin-page-framework' );
385
        __( 'Remove Section', 'admin-page-framework' );
386
        __( 'Toggle All', 'admin-page-framework' );
387
        __( 'Toggle all collapsible sections', 'admin-page-framework' );
388
        __( 'Reset', 'admin-page-framework' );
389
        __( 'Yes', 'admin-page-framework' );
390
        __( 'No', 'admin-page-framework' );
391
        __( 'On', 'admin-page-framework' );
392
        __( 'Off', 'admin-page-framework' );
393
        __( 'Enabled', 'admin-page-framework' );
394
        __( 'Disabled', 'admin-page-framework' );
395
        __( 'Supported', 'admin-page-framework' );
396
        __( 'Not Supported', 'admin-page-framework' );
397
        __( 'Functional', 'admin-page-framework' );
398
        __( 'Not Functional', 'admin-page-framework' );
399
        __( 'Too Long', 'admin-page-framework' );
400
        __( 'Acceptable', 'admin-page-framework' );
401
        __( 'No log found.', 'admin-page-framework' );        
402
        
403
        __( 'The method is called too early: %1$s', 'admin-page-framework' );
404
        __( 'Debug Info', 'admin-page-framework' );
405
        
406
        __( 'Click here to expand to view the contents.', 'admin-page-framework' );
407
        __( 'Click here to collapse the contents.', 'admin-page-framework' );
408
        
409
        __( 'Loading...', 'admin-page-framework' );
410
        __( 'Please enable JavaScript for better experience.', 'admin-page-framework' );
411
        
412
    }
413
    
414
}