Completed
Branch dev (a3767f)
by Michael
40:38
created

AdminPageFrameworkLoader_AdminPageWelcome::start()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6
Metric Value
dl 0
loc 9
rs 9.6667
ccs 0
cts 6
cp 0
cc 2
eloc 4
nc 2
nop 0
crap 6
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
/**
13
 * Constructs the Welcome admin page
14
 * 
15
 * @filter      apply       admin_page_framework_loader_filter_admin_welcome_redirect_url        Applies to the redirecting welcome url. Use this filter to disable the redirection upon plugin installation.
16
 * 
17
 * @since       3.5.0
18
 */
19
class AdminPageFrameworkLoader_AdminPageWelcome 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...
20
    
21
    /**
22
     * User constructor.
23
     * 
24
     * @since       3.5.0
25
     */
26
    public function start() {
27
        
28
        if ( ! is_admin() ) {
29
            return;
30
        }
31
        
32
        add_action( 'admin_menu', array( $this, '_replyToHandleRedirects' ) );
33
        
34
    }
35
        /**
36
         * Handles page redirects.
37
         * 
38
         * This is called to prevent the plugin from performing the redirect when the plugin is not activated or intervene the activation process.
39
         * If this is called in the start() method above, it will redirect the user to the page during the activation process 
40
         * and the user gets a page that is not created because the plugin is not activated.
41
         * 
42
         * @callback    action      admin_menu
43
         * @since       3.5.0
44
         * @sicne       3.5.3       Change the hook from `admin_init` as the resetting option results in an error 'You do not have permission to access this page.'
45
         */
46
        public function _replyToHandleRedirects() {
1 ignored issue
show
Coding Style introduced by
Method name "_replyToHandleRedirects" should not be prefixed with an underscore to indicate visibility
Loading history...
47
                
48
            // When newly installed, the 'welcomed' value is not set.
49
            $_oOption = AdminPageFrameworkLoader_Option::getInstance();
50
            if ( ! $_oOption->get( 'welcomed' ) ) {                
51
                $this->_setInitialOptions( $_oOption, AdminPageFrameworkLoader_Registry::VERSION );
52
                $this->_gotToWelcomePage(); // will exit
53
            }
54
            if ( $_oOption->hasUpgraded() ) {
55
                $this->_setInitialOptions( $_oOption, $_oOption->get( 'version_saved' ) );
56
                $this->_gotToWelcomePage(); // will exit
57
            }            
58
            
59
        }
60
            /**
61
             * 
62
             * @return void
63
             */
64
            private function _setInitialOptions( $oOption, $sVersionUpgradedFrom ) {
1 ignored issue
show
Coding Style introduced by
Method name "_setInitialOptions" should not be prefixed with an underscore to indicate visibility
Loading history...
65
                
66
                $oOption->set( 'welcomed', true );
67
                $oOption->set( 'version_upgraded_from', $sVersionUpgradedFrom );
68
                $oOption->set( 'version_saved', AdminPageFrameworkLoader_Registry::VERSION );
69
                $oOption->save();                
70
                
71
            }
72
            
73
        private function _gotToWelcomePage() {        
0 ignored issues
show
Coding Style introduced by
Method name "_gotToWelcomePage" should not be prefixed with an underscore to indicate visibility
Loading history...
74
            $_sWelcomePageURL = apply_filters(
75
                AdminPageFrameworkLoader_Registry::HOOK_SLUG . '_filter_admin_welcome_redirect_url',
76
                add_query_arg( 
77
                    array( 'page' => AdminPageFrameworkLoader_Registry::$aAdminPages['about'] ),
78
                    admin_url( 'index.php' )   // Dashboard
79
                )                
80
            );
81
            
82
            $this->oUtil->goToLocalURL( $_sWelcomePageURL );
83
            
84
        }
85
    
86
    /**
87
     * Sets up admin pages.
88
     * 
89
     * @since       3.5.0
90
     */
91
    public function setUp() {
0 ignored issues
show
Coding Style introduced by
setUp 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...
92
        
93
        $this->sPageSlug  = AdminPageFrameworkLoader_Registry::$aAdminPages['about'];
0 ignored issues
show
Bug introduced by
The property sPageSlug does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
94
        
95
        // Root page
96
        $this->setRootMenuPage( 
97
            'Dashboard'     // menu slug
98
        ); 
99
        
100
        // Sub-pages
101
        $this->addSubMenuItems( 
102
            array(
103
                'title'         => AdminPageFrameworkLoader_Registry::SHORTNAME,
104
                'page_slug'     => AdminPageFrameworkLoader_Registry::$aAdminPages[ 'about' ],    // page slug
105
                'show_in_menu'  => false,
106
                'style'         => array(
107
                    AdminPageFrameworkLoader_Registry::$sDirPath . '/asset/css/about.css', 
108
                    AdminPageFrameworkLoader_Registry::$sDirPath . '/asset/css/column.css', 
109
                    AdminPageFrameworkLoader_Registry::$sDirPath . '/asset/javascript/flip/jquery.m.flip.css',
110
                    version_compare( $GLOBALS['wp_version'], '3.8', '<' )
111
                        ? ".about-wrap .introduction h2 {
112
                                padding: 1em;
113
                            }"
114
                        : "",
115
                ),
116
                'script'        => array(
117
                    AdminPageFrameworkLoader_Registry::$sDirPath . '/asset/javascript/flip/jquery.m.flip.js',
118
                    "jQuery( document ).ready( function() {
119
                        jQuery( '.apf-badge-image' ).mflip();
120
                    } );",
121
                ),
122
            )
123
        );
124
125
        // Page Settings
126
        $this->setPageHeadingTabsVisibility( false ); // disables the page heading tabs by passing false.
127
        $this->setInPageTabTag( 'h2' ); // sets the tag used for in-page tabs     
128
        $this->setPageTitleVisibility( false ); // disable the page title of a specific page.
129
        $this->setPluginSettingsLinkLabel( '' ); // pass an empty string to disable it.
130
           
131
        // Hook
132
        add_action( "load_" . AdminPageFrameworkLoader_Registry::$aAdminPages['about'], array( $this, 'replyToLoadPage' ) );
133
        
134
    }   
135
        
136
        /**
137
         * Triggered when the page loads.
138
         * 
139
         * Adds tabs.
140
         */
141
        public function replyToLoadPage( $oFactory ) {
142
            
143
            $_sPageSlug = AdminPageFrameworkLoader_Registry::$aAdminPages['about'];
144
            new AdminPageFrameworkLoader_AdminPageWelcome_Welcome( 
145
                $this,              // factory object
146
                $_sPageSlug,        // page slug
147
                array(
148
                    'tab_slug'      => 'welcome',
149
                    // 'title'         => __( "What's New", 'admin-page-framework-loader' ),   // '
1 ignored issue
show
Unused Code Comprehensibility introduced by
57% 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...
150
                    'style'         => array(
151
                        AdminPageFrameworkLoader_Registry::$sDirPath . '/asset/css/admin.css',
152
                        AdminPageFrameworkLoader_Registry::$sDirPath . '/asset/css/code.css',
153
                        ' .main-image, .main-image img { text-align: center; max-width: 90%; margin-left: auto; margin-right: auto; } ',
154
                    ),
155
                )                
156
            );        
157
158
            $this->_setPreferences( $oFactory );
159
            
160
        }
161
            /**
162
             * Gets triggered when the page loads.
163
             */
164
            private function _setPreferences( $oFactory ) {
1 ignored issue
show
Coding Style introduced by
Method name "_setPreferences" should not be prefixed with an underscore to indicate visibility
Loading history...
165
166
                $this->oProp->sWrapperClassAttribute = "wrap about-wrap";
167
                
168
                $oFactory->setInPageTabsVisibility( false );
169
                
170
                add_filter( "content_top_{$this->sPageSlug}", array( $this, 'replyToFilterContentTop' ) );
171
                           
172
            } 
173
   
174
        
175
    /**
176
     * Filters the top part of the page content.
177
     * 
178
     * @remark      A callback of the "content_top_{page slug}" filter hook.
179
     */
180
    public function replyToFilterContentTop( $sContent ) {
181
182
        $_sVersion      = '- ' . AdminPageFrameworkLoader_Registry::VERSION;
183
        $_sPluginName   = AdminPageFrameworkLoader_Registry::SHORTNAME . ' ' . $_sVersion;
184
        
185
        $_sBadgeURL     = esc_url( AdminPageFrameworkLoader_Registry::getPluginURL( 'asset/image/icon-128x128.png' ) );
186
        
187
        $_aOutput   = array();
188
        $_aOutput[] = "<h1>" 
189
                . sprintf( __( 'Welcome to %1$s', 'admin-page-framework-loader' ), $_sPluginName )
190
            . "</h1>";
191
        $_aOutput[] = "<div class='about-text'>"
192
                . sprintf( __( 'Thank you for updating to the latest version! %1$s is ready to make your plugin or theme development faster, more organized and better!', 'admin-page-framework-loader' ), $_sPluginName )
193
            . "</div>";
194
        $_aOutput[] = ''
195
                . "<div class='apf-badge'>"
196
                    . "<div class='apf-badge-image m-flip'>"
197
                        . "<img src='{$_sBadgeURL}' />"
198
                    . "</div>"
199
                    . "<span class='label'>" . sprintf( __( 'Version %1$s', 'admin-page-framework-loader' ), $_sVersion ) . "</span>"
200
                . "</div>";
201
           
202
        return implode( PHP_EOL, $_aOutput )
203
            . $sContent;
204
    }
205
 
206
}
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...