Completed
Branch dev (1ad2e5)
by
unknown
04:33
created

_enqueueSRCByCondition()   D

Complexity

Conditions 10
Paths 4

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 15
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 30
rs 4.8196

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Admin Page Framework
4
 * 
5
 * http://en.michaeluno.jp/admin-page-framework/
6
 * Copyright (c) 2013-2016 Michael Uno; Licensed MIT
7
 * 
8
 */
9
10
/**
11
 * {@inheritdoc}
12
 * 
13
 * {@inheritdoc}
14
 * 
15
 * This is for generic pages the framework creates.
16
 * 
17
 * @since       2.1.5
18
 * @since       3.3.0       Changed the name from AdminPageFramework_HeadTag_Page.
19
 * @package     AdminPageFramework
20
 * @subpackage  Factory/AdminPage/Resource
21
 * @extends     AdminPageFramework_Resource_Base
22
 * @internal
23
 */
24
class AdminPageFramework_Resource_admin_page extends AdminPageFramework_Resource_Base {
25
    
26
    /**
27
     * Applies page and tab specific filters to inline CSS rules.
28
     * 
29
     * @since       3.5.0
30
     * @return      void
31
     */
32
    protected function _printClassSpecificStyles( $sIDPrefix ) {
33
     
34
        // This method can be called two times in a page to support embedding in the footer. 
35
        static $_bLoaded = false;
36
        if ( $_bLoaded ) {
37
            parent::_printClassSpecificStyles( $sIDPrefix );
38
            return;
39
        }        
40
        $_bLoaded   = true;
41
     
42
        $_oCaller   = $this->oProp->oCaller;     
43
        $_sPageSlug = $this->_getCurrentPageSlugForFilter();
44
        $_sTabSlug  = $this->_getCurrentTabSlugForFilter( $_sPageSlug );
45
        
46
        // tab 
47
        if ( $_sPageSlug && $_sTabSlug ) {
48
            $this->oProp->sStyle     = $this->addAndApplyFilters( 
49
                $_oCaller, 
50
                "style_{$_sPageSlug}_{$_sTabSlug}", 
51
                $this->oProp->sStyle 
52
            );                 
53
        }
54
        
55
        // page
56
        if ( $_sPageSlug ) {
57
            $this->oProp->sStyle     = $this->addAndApplyFilters( 
58
                $_oCaller, 
59
                "style_{$_sPageSlug}", 
60
                $this->oProp->sStyle 
61
            );     
62
        }
63
        
64
        // The parent method should be called after updating the $this->oProp->sStyle property above.
65
        parent::_printClassSpecificStyles( $sIDPrefix );
66
        
67
    }
68
        /**
69
         * Returns the currently loaded page slug to apply resource filters.
70
         * 
71
         * If the page has not been added, an empty value will be returned.
72
         * 
73
         * @since       3.5.3
74
         * @return      string      The page slug if the page has been added.
75
         */
76
        private function _getCurrentPageSlugForFilter() {
77
            $_sPageSlug = $this->oProp->getCurrentPageSlug();
78
            return $this->oProp->isPageAdded( $_sPageSlug )
79
                ? $_sPageSlug
80
                : '';            
81
        }
82
        /**
83
         * Returns the currently loaded tab slug to apply resource filters.
84
         * 
85
         * If the tab has not been added, an empty value will be returned.
86
         * 
87
         * @since       3.5.3
88
         * @return      string      The tab slug if the tab has been added.
89
         */
90
        private function _getCurrentTabSlugForFilter( $sPageSlug ) {
91
            $_sTabSlug  = $this->oProp->getCurrentTabSlug( $sPageSlug ); 
92
            return isset( $this->oProp->aInPageTabs[ $sPageSlug ][ $_sTabSlug ] )
93
                ? $_sTabSlug
94
                : '';          
95
        }
96
        
97
    /**
98
     * Applies page and tab specific filters to inline JaveScript scirpts.
99
     * 
100
     * @since       3.5.0
101
     * @return      void
102
     */
103
    protected function _printClassSpecificScripts( $sIDPrefix ) {
104
       
105
        // This method can be called two times in a page to support embedding in the footer. 
106
        static $_bLoaded = false;
107
        if ( $_bLoaded ) {
108
            parent::_printClassSpecificScripts( $sIDPrefix );
109
            return;
110
        }        
111
        $_bLoaded   = true;
112
       
113
        $_oCaller   = $this->oProp->oCaller;     
114
        $_sPageSlug = $this->_getCurrentPageSlugForFilter();
115
        $_sTabSlug  = $this->_getCurrentTabSlugForFilter( $_sPageSlug );
116
        
117
        // tab 
118
        if ( $_sPageSlug && $_sTabSlug ) {
119
            $this->oProp->sScript     = $this->addAndApplyFilters( 
120
                $_oCaller, 
121
                "script_{$_sPageSlug}_{$_sTabSlug}", 
122
                $this->oProp->sScript 
123
            );                 
124
        }
125
        
126
        // page
127
        if ( $_sPageSlug ) {
128
            $this->oProp->sScript     = $this->addAndApplyFilters( 
129
                $_oCaller, 
130
                "script_{$_sPageSlug}", 
131
                $this->oProp->sScript 
132
            );     
133
        }        
134
        
135
        // The parent method should be called after updating the $this->oProp->sScript property above.
136
        parent::_printClassSpecificScripts( $sIDPrefix );
137
        
138
    }
139
    
140
    /**
141
     * Enqueues styles by page slug and tab slug.
142
     * 
143
     * @since 2.1.5
144
     * @internal
145
     */
146
    public function _enqueueStyles( $aSRCs, $sPageSlug='', $sTabSlug='', $aCustomArgs=array() ) {
147
        
148
        $_aHandleIDs = array();
149
        foreach( ( array ) $aSRCs as $_sSRC ) {
150
            $_aHandleIDs[] = $this->_enqueueStyle( $_sSRC, $sPageSlug, $sTabSlug, $aCustomArgs );
151
        }
152
        return $_aHandleIDs;
153
        
154
    }
155
    /**
156
     * Enqueues a style by page slug and tab slug.
157
     * 
158
     * <h4>Custom Argument Array for the Fourth Parameter</h4>
159
     * <ul>
160
     *     <li><strong>handle_id</strong> - ( optional, string ) The handle ID of the stylesheet.</li>
161
     *     <li><strong>dependencies</strong> - ( optional, array ) The dependency array. For more information, see <a href="http://codex.wordpress.org/Function_Reference/wp_enqueue_style">codex</a>.</li>
162
     *     <li><strong>version</strong> - ( optional, string ) The stylesheet version number.</li>
163
     *     <li><strong>media</strong> - ( optional, string ) the description of the field which is inserted into the after the input field tag.</li>
164
     * </ul>
165
     * 
166
     * @since       2.1.2
167
     * @since       2.1.5   Moved from the main class.
168
     * @see         http://codex.wordpress.org/Function_Reference/wp_enqueue_style
169
     * @param       string  $sSRC           The URL of the stylesheet to enqueue, the absolute file path, or the relative path to the root directory of WordPress. Example: '/css/mystyle.css'.
170
     * @param       string  $sPageSlug      (optional) The page slug that the stylesheet should be added to. If not set, it applies to all the pages created by the framework.
171
     * @param       string  $sTabSlug       (optional) The tab slug that the stylesheet should be added to. If not set, it applies to all the in-page tabs in the page.
172
     * @param       array   $aCustomArgs    (optional) The argument array for more advanced parameters.
173
     * @return      string  The script handle ID. If the passed url is not a valid url string, an empty string will be returned.
174
     * @internal
175
     */    
176
    public function _enqueueStyle( $sSRC, $sPageSlug='', $sTabSlug='', $aCustomArgs=array() ) {
177
        return $this->_enqueueResourceByType( $sSRC, $sPageSlug, $sTabSlug, $aCustomArgs, 'style' );        
178
    }
179
    
180
    /**
181
     * Enqueues scripts by page slug and tab slug.
182
     * 
183
     * @since 2.1.5
184
     */
185
    public function _enqueueScripts( $aSRCs, $sPageSlug='', $sTabSlug='', $aCustomArgs=array() ) {
186
        
187
        $_aHandleIDs = array();
188
        foreach( ( array ) $aSRCs as $_sSRC ) {
189
            $_aHandleIDs[] = $this->_enqueueScript( $_sSRC, $sPageSlug, $sTabSlug, $aCustomArgs );
190
        }
191
        return $_aHandleIDs;
192
        
193
    }    
194
    /**
195
     * Enqueues a script by page slug and tab slug.
196
     * 
197
     * <h4>Custom Argument Array for the Fourth Parameter</h4>
198
     * <ul>
199
     *     <li><strong>handle_id</strong> - ( optional, string ) The handle ID of the script.</li>
200
     *     <li><strong>dependencies</strong> - ( optional, array ) The dependency array. For more information, see <a href="http://codex.wordpress.org/Function_Reference/wp_enqueue_script">codex</a>.</li>
201
     *     <li><strong>version</strong> - ( optional, string ) The stylesheet version number.</li>
202
     *     <li><strong>translation</strong> - ( optional, array ) The translation array. The handle ID will be used for the object name.</li>
203
     *     <li><strong>in_footer</strong> - ( optional, boolean ) Whether to enqueue the script before < / head > or before < / body > Default: <code>false</code>.</li>
204
     * </ul>  
205
     * 
206
     * @since       2.1.2
207
     * @since       2.1.5       Moved from the main class.
208
     * @see         http://codex.wordpress.org/Function_Reference/wp_enqueue_script
209
     * @param       string      $sSRC           The URL of the stylesheet to enqueue, the absolute file path, or the relative path to the root directory of WordPress. Example: '/js/myscript.js'.
210
     * @param       string      $sPageSlug      (optional) The page slug that the script should be added to. If not set, it applies to all the pages created by the framework.
211
     * @param       string      $sTabSlug       (optional) The tab slug that the script should be added to. If not set, it applies to all the in-page tabs in the page.
212
     * @param       array       $aCustomArgs    (optional) The argument array for more advanced parameters.
213
     * @return      string      The script handle ID. If the passed url is not a valid url string, an empty string will be returned.
214
     * @internal
215
     */
216
    public function _enqueueScript( $sSRC, $sPageSlug='', $sTabSlug='', $aCustomArgs=array() ) {
217
        return $this->_enqueueResourceByType( $sSRC, $sPageSlug, $sTabSlug, $aCustomArgs, 'script' );
218
    }
219
        /**
220
         * Enqueues a resouce.
221
         * 
222
         * @since       3.5.3
223
         * @param       string      $sSRC           The source path or url.
224
         * @param       string      $sPageSlug      The page slug that the item gets enqueued.
225
         * @param       string      $sTabSlug       The page slug that the item gets enqueued.
226
         * @param       array       $aCustomArgs    A custom argument array.
227
         * @param       string      $sType          Accepts 'style' or 'script'
228
         */
229
        private function _enqueueResourceByType( $sSRC, $sPageSlug='', $sTabSlug='', $aCustomArgs=array(), $sType='style' ) {
230
            
231
            $sSRC       = trim( $sSRC );
232
            if ( empty( $sSRC ) ) { 
233
                return ''; 
234
            }
235
            $sSRC       = $this->getResolvedSRC( $sSRC );
236
237
            // Get the property name for the type
238
            $_sContainerPropertyName     = $this->_getContainerPropertyNameByType( $sType );
239
            $_sEnqueuedIndexPropertyName = $this->_getEnqueuedIndexPropertyNameByType( $sType );
240
            
241
            // setting the key based on the url prevents duplicate items
242
            $_sSRCHash  = md5( $sSRC );
243
            if ( isset( $this->oProp->{$_sContainerPropertyName}[ $_sSRCHash ] ) ) { 
244
                return ''; 
245
            } 
246
            
247
            $this->oProp->{$_sContainerPropertyName}[ $_sSRCHash ] = array_filter( $this->getAsArray( $aCustomArgs ), array( $this, 'isNotNull' ) )
248
                + array(     
249
                    'sPageSlug' => $sPageSlug,
250
                    'sTabSlug'  => $sTabSlug,
251
                    'sSRC'      => $sSRC,
252
                    'sType'     => $sType,
253
                    'handle_id' => $sType . '_' . $this->oProp->sClassName . '_' .  ( ++$this->oProp->{$_sEnqueuedIndexPropertyName} ),
254
                )
255
                + self::$_aStructure_EnqueuingResources
256
                ;
257
258
            // Store the attributes in another container by url.
259
            $this->oProp->aResourceAttributes[ $this->oProp->{$_sContainerPropertyName}[ $_sSRCHash ]['handle_id'] ] = $this->oProp->{$_sContainerPropertyName}[ $_sSRCHash ]['attributes'];
260
            
261
            return $this->oProp->{$_sContainerPropertyName}[ $_sSRCHash ][ 'handle_id' ];
262
            
263
        }
264
            /**
265
             * Returns the property name that contains the information of resources by type.
266
             * @since       3.5.3
267
             * @return      string      the property name that contains the information of resources by type.
268
             */
269
            private function _getContainerPropertyNameByType( $sType ) {
270
                switch ( $sType ) {
271
                    default:
272
                    case 'style':
273
                        return 'aEnqueuingStyles';
274
                    case 'script':
275
                        return 'aEnqueuingScripts';
276
                }
277
            }
278
            /**
279
             * Returns the property name that contains the added count of resources by type.
280
             * @since       3.5.3
281
             * @return      string      the property name that contains the added count of resources by type.
282
             */            
283
            private function _getEnqueuedIndexPropertyNameByType( $sType ) {
284
                switch ( $sType ) {
285
                    default:
286
                    case 'style':
287
                        return 'iEnqueuedStyleIndex';
288
                    case 'script':
289
                        return 'iEnqueuedScriptIndex';
290
                }
291
            }            
292
    /**
293
     * Enqueues a style source without conditions.
294
     * 
295
     * @remark      Used for inserting the input field head tag elements.
296
     * @since       3.0.0
297
     * @internal
298
     */
299
    public function _forceToEnqueueStyle( $sSRC, $aCustomArgs=array() ) {
300
        return $this->_enqueueStyle( $sSRC, '', '', $aCustomArgs );
301
    }
302
    /**
303
     * Enqueues a script source without conditions.
304
     * 
305
     * @remark      Used for inserting the input field head tag elements.
306
     * @since       3.0.0
307
     * @internal
308
     */    
309
    public function _forceToEnqueueScript( $sSRC, $aCustomArgs=array() ) {
310
        return $this->_enqueueScript( $sSRC, '', '', $aCustomArgs );
311
    }
312
    
313
    
314
    /**
315
     * A helper function for the _replyToEnqueueScripts() and _replyToEnqueueStyle() methods.
316
     * 
317
     * @since       2.1.2
318
     * @since       2.1.5       Moved from the main class. Changed the name from enqueueSRCByPageConditoin.
319
     * @since       3.7.0      Fixed a typo in the method name.
320
     * @internal
321
     */
322
    protected function _enqueueSRCByCondition( $aEnqueueItem ) {
323
324
        $sCurrentPageSlug   = $this->oProp->getCurrentPageSlug();
325
        $sCurrentTabSlug    = $this->oProp->getCurrentTabSlug( $sCurrentPageSlug );
326
        $sPageSlug          = $aEnqueueItem['sPageSlug'];
327
        $sTabSlug           = $aEnqueueItem['sTabSlug'];
328
329
        // If the page slug is not specified and the currently loading page is one of the pages that is added by the framework,
330
        if ( ! $sPageSlug && $this->oProp->isPageAdded( $sCurrentPageSlug ) ) { // means script-global(among pages added by the framework)
331
            return $this->_enqueueSRC( $aEnqueueItem );
332
        }
333
                
334
        // If both tab and page slugs are specified,
335
        if ( 
336
            ( $sPageSlug && $sCurrentPageSlug == $sPageSlug )
337
            && ( $sTabSlug && $sCurrentTabSlug == $sTabSlug )
338
        ) {
339
            return $this->_enqueueSRC( $aEnqueueItem );
340
        }
341
        
342
        // If the tab slug is not specified and the page slug is specified, 
343
        // and if the current loading page slug and the specified one matches,
344
        if ( 
345
            ( $sPageSlug && ! $sTabSlug )
346
            && ( $sCurrentPageSlug == $sPageSlug )
347
        ) {
348
            return $this->_enqueueSRC( $aEnqueueItem );
349
        }
350
351
    }
352
}
353