Completed
Branch master (6ffb79)
by
unknown
05:36
created

AdminPageFramework::_getCallerPath()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 6
eloc 11
c 1
b 1
f 0
nc 7
nop 1
dl 0
loc 21
rs 8.7624
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
 * The main class of the framework to be extended to create admin pages and forms.
12
 * 
13
 * <p>The user defines their own class by extending this class with the `setUp()` method overridden to define admin pages.</p>
14
 * 
15
 * @abstract
16
 * @since       2.0.0
17
 * @extends     AdminPageFramework_Controller
18
 * @package     AdminPageFramework
19
 * @subpackage  Factory/AdminPage
20
 * @remark      Most of the internal methods are prefixed with the underscore like `_getSomething()` and callback methods are prefixed with `_reply`.
21
 * The methods for the users are public and do not have those prefixes.
22
 */
23
abstract class AdminPageFramework extends AdminPageFramework_Controller {
24
     
25
    /**
26
     * Defines the class object structure type.
27
     * 
28
     * This is used to create a property object as well as to define the form element structure.
29
     * 
30
     * @since       3.0.0
31
     * @since       3.7.0       Changed the name from `$_sFieldsType`.
32
     * @since       3.7.12      Moved from `AdminPageFrmework_Model_Form`. Removed the static scope.
33
     * @internal
34
     */
35
    protected $_sStructureType = 'admin_page';
36
     
37
    /**
38
     * Registers necessary callbacks and sets up internal components including properties.
39
     * 
40
     * <h4>Example</h4>
41
     * <code>
42
     * new MyAdminPageClass( 'my_custom_option_key', __FILE__ );
43
     * </code>
44
     * 
45
     * @access      public
46
     * @since       2.0.0
47
     * @see         http://codex.wordpress.org/Roles_and_Capabilities
48
     * @see         http://codex.wordpress.org/I18n_for_WordPress_Developers#Text_Domains
49
     * @param       array|integer|string    $aisOptionKey    (optional) specifies the option key name to store in the options table. If this is not set, the instantiated class name will be used as default. 
0 ignored issues
show
Documentation introduced by
There is no parameter named $aisOptionKey. Did you maybe mean $isOptionKey?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
50
     * [3.5.9+] If an integer is given, a transient will be used. If an array of option key arguments is given, the argument values will be set to the framework properties.
51
     * - type - either `options_table` or `transient`.
52
     * - key - the option key or the transient key
53
     * - duration  - when the option type is transient, this value will be used for the time span to store the value in the database.
54
     * `
55
     * array(
56
     *      'type' => 'options_table',
57
     *      'key'  => 'my_admin_options',
58
     * )
59
     * `
60
     * `
61
     * array(
62
     *      'type' => 'transient',
63
     *      'key' => $sTransientKeyDefinedSomewhereInYourProgram,
64
     *      'duration' => 60*60*24*2  // two days
65
     * )
66
     * `
67
     * @param       string                  $sCallerPath    (optional) used to retrieve the plugin/theme details to auto-insert the information into the page footer.
68
     * @param       string                  $sCapability    (optional) sets the overall access level to the admin pages created by the framework. The used capabilities are listed <a href="http://codex.wordpress.org/Roles_and_Capabilities">here</a>. The capability can be set per page, tab, setting section, setting field. Default: `manage_options`
69
     * @param       string                  $sTextDomain    (optional) the <a href="http://codex.wordpress.org/I18n_for_WordPress_Developers#Text_Domains" target="_blank">text domain</a> used for the framework's system messages. Default: admin-page-framework.
70
     */
71
    public function __construct( $isOptionKey=null, $sCallerPath=null, $sCapability='manage_options', $sTextDomain='admin-page-framework' ){
72
73
        if ( ! $this->_isInstantiatable() ) {
74
            return;
75
        }
76
                        
77
        parent::__construct( 
78
            $isOptionKey,
79
            $this->_getCallerPath( $sCallerPath ),
80
            $sCapability, 
81
            $sTextDomain 
82
        );
83
84
    }   
85
        /**
86
         * Returns the script caller path.
87
         * @remark      It is important to find the caller script path here when separating the library into multiple files.
88
         * @return      null|string
89
         * @since       3.8.2
90
         */
91
        private function _getCallerPath( $sCallerPath ) {
0 ignored issues
show
Coding Style introduced by
_getCallerPath uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
_getCallerPath uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
92
            
93
            if ( $sCallerPath  ) {
94
                return trim( $sCallerPath );
95
            }
96
            
97
            if ( ! is_admin() ) {
98
                return null;
99
            }
100
            
101
            if ( ! isset( $GLOBALS[ 'pagenow' ] ) ) {
102
                return null;
103
            }
104
            
105
            $_sCalllerPath = in_array( $GLOBALS[ 'pagenow' ], array( 'plugins.php', ) ) || isset( $_GET[ 'page' ] )
106
                ? AdminPageFramework_Utility::getCallerScriptPath( __FILE__ ) // not using $oUtil as this method is caller earlier than the base constructor.
107
                : null;
108
                
109
            return $_sCalllerPath;
110
            
111
        }
112
    
113
}
114