Completed
Branch dev (d064b2)
by
unknown
24:11 queued 04:10
created

_setInitialOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 8
rs 9.4286
ccs 0
cts 6
cp 0
cc 1
eloc 5
nc 1
nop 2
crap 2
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 {
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...
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() {
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 ) {
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() {        
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
                        ".admin-page-framework-section-tab h4 {
117
                            padding: 6px 16px 8px;
118
                            font-size: 1.2em;
119
                            font-weight: 400;
120
                        }",     
121
                ),
122
                'script'        => array(
123
                    AdminPageFrameworkLoader_Registry::$sDirPath . '/asset/javascript/flip/jquery.m.flip.js',
124
                    "jQuery( document ).ready( function() {
125
                        jQuery( '.apf-badge-image' ).mflip();
126
                    } );",
127
                ),
128
            )
129
        );
130
131
        // Page Settings
132
        $this->setPageHeadingTabsVisibility( false ); // disables the page heading tabs by passing false.
133
        $this->setInPageTabTag( 'h2' ); // sets the tag used for in-page tabs     
134
        $this->setPageTitleVisibility( false ); // disable the page title of a specific page.
135
        $this->setPluginSettingsLinkLabel( '' ); // pass an empty string to disable it.
136
           
137
        // Hook
138
        add_action( "load_" . AdminPageFrameworkLoader_Registry::$aAdminPages['about'], array( $this, 'replyToLoadPage' ) );
139
        
140
    }   
141
        
142
        /**
143
         * Triggered when the page loads.
144
         * 
145
         * Adds tabs.
146
         */
147
        public function replyToLoadPage( $oFactory ) {
148
            
149
            $_sPageSlug = AdminPageFrameworkLoader_Registry::$aAdminPages['about'];
150
            new AdminPageFrameworkLoader_AdminPageWelcome_Welcome( 
151
                $this,              // factory object
152
                $_sPageSlug,        // page slug
153
                array(
154
                    'tab_slug'      => 'welcome',
155
                    // '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...
156
                    'style'         => array(
157
                        AdminPageFrameworkLoader_Registry::$sDirPath . '/asset/css/admin.css',
158
                        AdminPageFrameworkLoader_Registry::$sDirPath . '/asset/css/code.css',
159
                        ' .main-image, .main-image img { text-align: center; max-width: 90%; margin-left: auto; margin-right: auto; } ',
160
                    ),
161
                )                
162
            );        
163
164
            $this->_setPreferences( $oFactory );
165
            
166
        }
167
            /**
168
             * Gets triggered when the page loads.
169
             */
170
            private function _setPreferences( $oFactory ) {
171
172
                $this->oProp->sWrapperClassAttribute = "wrap about-wrap";
173
                
174
                $oFactory->setInPageTabsVisibility( false );
175
                
176
                add_filter( "content_top_{$this->sPageSlug}", array( $this, 'replyToFilterContentTop' ) );
177
                           
178
            } 
179
   
180
        
181
    /**
182
     * Filters the top part of the page content.
183
     * 
184
     * @remark      A callback of the "content_top_{page slug}" filter hook.
185
     */
186
    public function replyToFilterContentTop( $sContent ) {
187
188
        $_sVersion      = '- ' . AdminPageFrameworkLoader_Registry::VERSION;
189
        $_sPluginName   = AdminPageFrameworkLoader_Registry::SHORTNAME . ' ' . $_sVersion;
190
        
191
        $_sBadgeURL     = esc_url( AdminPageFrameworkLoader_Registry::getPluginURL( 'asset/image/icon-128x128.png' ) );
192
        
193
        $_aOutput   = array();
194
        $_aOutput[] = "<h1>" 
195
                . sprintf( __( 'Welcome to %1$s', 'admin-page-framework-loader' ), $_sPluginName )
196
            . "</h1>";
197
        $_aOutput[] = "<div class='about-text'>"
198
                . 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 )
199
            . "</div>";
200
        $_aOutput[] = ''
201
                . "<div class='apf-badge'>"
202
                    . "<div class='apf-badge-image m-flip'>"
203
                        . "<img src='{$_sBadgeURL}' />"
204
                    . "</div>"
205
                    . "<span class='label'>" . sprintf( __( 'Version %1$s', 'admin-page-framework-loader' ), $_sVersion ) . "</span>"
206
                . "</div>";
207
           
208
        return implode( PHP_EOL, $_aOutput )
209
            . $sContent;
210
    }
211
 
212
}