Completed
Branch dev (aae49a)
by
unknown
19:32
created

AdminPageFrameworkLoader_AdminPage   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 3.03%
Metric Value
wmc 20
lcom 1
cbo 7
dl 0
loc 180
rs 10
ccs 3
cts 99
cp 0.0303

6 Methods

Rating   Name   Duplication   Size   Complexity  
B start() 0 42 5
B setUp() 0 44 4
A _doPageSettings() 0 23 1
A _getAdminURLTools() 0 14 2
A _getAdminPageSwitchLink() 0 12 3
B _getDemoSwitcherLink() 0 18 5
1
<?php
2
/**
3
 * One of the abstract class of the plugin admin page class.
4
 * 
5
 * @package      Admin Page Framework Loader
6
 * @copyright    Copyright (c) 2014, Michael Uno
7
 * @author       Michael Uno
8
 * @authorurl    http://michaeluno.jp
9
 * @since        3.5.0
10
 */
11
12
class AdminPageFrameworkLoader_AdminPage extends AdminPageFramework {
2 ignored issues
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...
Coding Style introduced by
As per PSR2, the opening brace for this class should be on a new line.
Loading history...
13
    
14
    /**
15
     * User constructor.
16
     * 
17
     * @since       3.5.0
18
     */
19 1
    public function start() {
0 ignored issues
show
Coding Style introduced by
start 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...
20
  
21 1
        if ( ! $this->oProp->bIsAdmin ) {
22 1
            return;
23
        }
24
      
25
        // Allows the user to switch the menu visibility.
26
        if ( isset( $_GET['enable_apfl_admin_pages'] ) ) {
27
            
28
            // Update the options and reload the page
29
            $_oOption = AdminPageFrameworkLoader_Option::getInstance( AdminPageFrameworkLoader_Registry::$aOptionKeys['main'] );
30
            $_oOption->update( 'enable_admin_pages', $_GET['enable_apfl_admin_pages'] );
31
            
32
            $this->oUtil->goToLocalURL( 
33
                remove_query_arg( 'enable_apfl_admin_pages' ),
34
                array( 'AdminPageFrameworkLoader_Utility', 'replyToShowRedirectError' )
35
            );
36
            
37
        }
38
        
39
        // Enable / disable the demo pages
40
        if ( isset( $_GET['enable_apfl_demo_pages'] ) ) {
41
            
42
            // Update the options and reload the page
43
            $_oOption = AdminPageFrameworkLoader_Option::getInstance( AdminPageFrameworkLoader_Registry::$aOptionKeys['main'] );
44
            $_oOption->update( 'enable_demo', $_GET['enable_apfl_demo_pages'] );            
45
             
46
            if ( $_GET[ 'enable_apfl_demo_pages' ] ) {
47
                $this->setSettingNotice( 
48
                    __( 'Enabled demo!', 'admin-page-framework-loader' ),
49
                    'updated'
50
                );
51
            }
52
              
53
            $this->oUtil->goToLocalURL( 
54
                remove_query_arg( 'enable_apfl_demo_pages' ),
55
                array( 'AdminPageFrameworkLoader_Utility', 'replyToShowRedirectError' )
56
            );        
57
            
58
        }
59
             
60
    }
61
62
    /**
63
     * Sets up admin pages.
64
     * 
65
     * @since       3.5.0
66
     */
67
    public function setUp() {
68
69
        $_aOptions          = $this->oProp->aOptions;
70
        $_bAdminPageEnabled = ! is_array( $_aOptions )  // for the first time of loading, the option is not set and it is not an array. 
71
            || ( isset( $_aOptions['enable_admin_pages'] ) && $_aOptions['enable_admin_pages'] );    
72
    
73
        // Set up pages
74
        if ( $_bAdminPageEnabled ) {
75
            
76
            $this->setRootMenuPage( 
77
                AdminPageFrameworkLoader_Registry::SHORTNAME,     // menu slug
78
                AdminPageFrameworkLoader_Registry::$sDirPath . '/asset/image/wp-logo_16x16.png', // menu icon
79
                4  // menu position
80
            ); 
81
                        
82
            // Add pages
83
            new AdminPageFrameworkLoader_AdminPage_Tool( 
84
                $this,
85
                array(
86
                    'page_slug' => AdminPageFrameworkLoader_Registry::$aAdminPages['tool'],
87
                    'title'     => __( 'Tools', 'admin-page-framework-loader' ),
88
                )
89
            );
90
            new AdminPageFrameworkLoader_AdminPage_Addon(
91
                $this,
92
                array(
93
                    'page_slug' => AdminPageFrameworkLoader_Registry::$aAdminPages['addon'],    // page slug
94
                    'title'     => __( 'Add Ons', 'admin-page-framework-loader' ),
95
                )                
96
            );
97
            new AdminPageFrameworkLoader_AdminPage_Help( 
98
                $this,
99
                array(
100
                    'page_slug' => AdminPageFrameworkLoader_Registry::$aAdminPages['help'],
101
                    'title'     => __( 'Help', 'admin-page-framework-loader' ),
102
                    'order'     => 1000, // to be the last menu item
103
                )                
104
            );
105
            
106
        }
107
              
108
        $this->_doPageSettings( $_bAdminPageEnabled, $_aOptions );
109
        
110
    }
111
        /**
112
         * Does page settings.
113
         */
114
        private function _doPageSettings( $_bAdminPageEnabled, $_aOptions ) {
0 ignored issues
show
Coding Style introduced by
Method name "_doPageSettings" should not be prefixed with an underscore to indicate visibility
Loading history...
115
                
116
            // Page Settings
117
            $this->setPageHeadingTabsVisibility( false ); // disables the page heading tabs by passing false.
118
            $this->setInPageTabTag( 'h2' ); // sets the tag used for in-page tabs     
119
            $this->setPageTitleVisibility( false ); // disable the page title of a specific page.
120
            $this->setPluginSettingsLinkLabel( '' ); // pass an empty string to disable it.
121
       
122
            // Styles
123
            $this->enqueueStyle( AdminPageFrameworkLoader_Registry::$sDirPath . '/asset/css/code.css' );
124
            $this->enqueueStyle( AdminPageFrameworkLoader_Registry::$sDirPath . '/asset/css/admin.css' );
125
           
126
            // Action Links (plugin.php)
127
            $this->addLinkToPluginTitle(
128
                $this->_getAdminURLTools( $_bAdminPageEnabled ),
129
                $this->_getAdminPageSwitchLink( $_bAdminPageEnabled ),
130
                $this->_getDemoSwitcherLink( $_bAdminPageEnabled, $_aOptions )
131
            );
132
            $this->addLinkToPluginDescription(
133
                "<a href='https://wordpress.org/support/plugin/admin-page-framework' target='_blank'>" . __( 'Support', 'admin-page-framework-loader' ) . "</a>"
134
            );            
135
            
136
        }
137
        /**
138
         * Returns the Tools admin page link.
139
         */
140
        private function _getAdminURLTools( $_bAdminPageEnabled ) {
1 ignored issue
show
Coding Style introduced by
Method name "_getAdminURLTools" should not be prefixed with an underscore to indicate visibility
Loading history...
141
            if ( ! $_bAdminPageEnabled ) {
142
                return;
143
            }
144
            $_sLink    = esc_url(
145
                add_query_arg( 
146
                    array( 
147
                        'page' => AdminPageFrameworkLoader_Registry::$aAdminPages['tool'],
148
                    ),
149
                    admin_url( 'admin.php' )
150
                )
151
            );                
152
            return "<a href='{$_sLink}'>" . __( 'Tools', 'admin-page-framework-loader' ) . "</a>";
153
        }
154
        /**
155
         * Returns the Enable /Disable Admin Pages link.
156
         */
157
        private function _getAdminPageSwitchLink( $bEnabled ) {
1 ignored issue
show
Coding Style introduced by
Method name "_getAdminPageSwitchLink" should not be prefixed with an underscore to indicate visibility
Loading history...
158
            $_sLink    = esc_url( 
159
                add_query_arg( 
160
                    array( 
161
                        'enable_apfl_admin_pages' => $bEnabled ? 0 : 1,
162
                    )
163
                )
164
            );            
165
            return $bEnabled
166
                ? "<a href='{$_sLink}'>" . __( 'Disable Admin Pages', 'admin-page-framework-loader' ) . "</a>"
167
                : "<a href='{$_sLink}'>" . __( 'Enable Admin Pages', 'admin-page-framework-loader' ) . "</a>";                     
168
        }
169
        /**
170
         * Returns the switch link of the demo pages.
171
         */
172
        private function _getDemoSwitcherLink( $_bAdminPageEnabled, $mOptions=array() ) {
1 ignored issue
show
Coding Style introduced by
Method name "_getDemoSwitcherLink" should not be prefixed with an underscore to indicate visibility
Loading history...
173
            
174
            if ( ! $_bAdminPageEnabled ) {
175
                return '';
176
            }
177
            $_bEnabled  = isset( $mOptions['enable_demo'] ) && $mOptions['enable_demo'];
178
            $_sLink    = esc_url( 
179
                add_query_arg( 
180
                    array( 
181
                        'enable_apfl_demo_pages' => $_bEnabled ? 0 : 1,
182
                    )
183
                )
184
            );        
185
            return $_bEnabled
186
                ? "<a href='{$_sLink}'>" . __( 'Disable Demo', 'admin-page-framework-loader' ) . "</a>"
187
                : "<a href='{$_sLink}'><strong style='font-size: 1em;'>" . __( 'Enable Demo', 'admin-page-framework-loader' ) . "</strong></a>";
188
            
189
        }            
190
191
}
1 ignored issue
show
Coding Style introduced by
According to PSR2, the closing brace of classes should be placed on the next line directly after the body.

Below you find some examples:

// Incorrect placement according to PSR2
class MyClass
{
    public function foo()
    {

    }
    // This blank line is not allowed.

}

// Correct
class MyClass
{
    public function foo()
    {

    } // No blank lines after this line.
}
Loading history...