replyToLoadPage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 17
rs 9.9332
c 0
b 0
f 0
cc 1
ccs 0
cts 7
cp 0
nc 1
nop 1
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 {
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( 'init', 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      init
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->_goToWelcomePage(); // will exit
53
            }
54
            if ( $_oOption->hasUpgraded() ) {
55
                $this->_setInitialOptions( $_oOption, $_oOption->get( 'version_saved' ) );
56
                $this->_goToWelcomePage(); // 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 _goToWelcomePage() {    
74
        
75
            $_sWelcomePageURL = apply_filters(
76
                AdminPageFrameworkLoader_Registry::HOOK_SLUG . '_filter_admin_welcome_redirect_url',
77
                add_query_arg( 
78
                    array( 'page' => AdminPageFrameworkLoader_Registry::$aAdminPages[ 'about' ] ),
79
                    admin_url( 'index.php' )   // Dashboard
80
                )                
81
            );
82
            
83
            $this->oUtil->goToLocalURL( $_sWelcomePageURL );
84
            
85
        }
86
    
87
    /**
88
     * Sets up admin pages.
89
     * 
90
     * @since       3.5.0
91
     * @callback    action      wp_loaded
92
     */
93
    public function setUp() {
94
  
95
        $_oOption = AdminPageFrameworkLoader_Option::getInstance();
96
        if ( ! $_oOption->get( 'enable_admin_pages' ) ) {
97
            return;
98
        }
99
        
100
        $this->sPageSlug  = AdminPageFrameworkLoader_Registry::$aAdminPages[ 'about' ];
0 ignored issues
show
Bug Best Practice introduced by
The property sPageSlug does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
101
        
102
        // Root page
103
        $this->setRootMenuPage( 
104
            'Dashboard'     // menu slug
105
        ); 
106
107
        // Sub-pages
108
        $this->addSubMenuItems( 
109
            array(
110
                'title'         => AdminPageFrameworkLoader_Registry::SHORTNAME,
111
                'page_slug'     => AdminPageFrameworkLoader_Registry::$aAdminPages[ 'about' ],    // page slug
112
                'show_in_menu'  => false,
113
                'style'         => array(
114
                    AdminPageFrameworkLoader_Registry::$sDirPath . '/asset/css/about.css', 
115
                    AdminPageFrameworkLoader_Registry::$sDirPath . '/asset/css/column.css', 
116
                    AdminPageFrameworkLoader_Registry::$sDirPath . '/asset/javascript/flip/jquery.m.flip.css',
117
                    version_compare( $GLOBALS[ 'wp_version' ], '3.8', '<' )
118
                        ? ".about-wrap .introduction h2 {
119
                                padding: 1em;
120
                            }"
121
                            
122
                        : "",
123
                        ".admin-page-framework-section-tab h4 {
124
                            padding: 6px 16px 8px;
125
                            font-size: 1.2em;
126
                            font-weight: 400;
127
                        }",     
128
                ),
129
                'script'        => array(
130
                    AdminPageFrameworkLoader_Registry::$sDirPath . '/asset/javascript/flip/jquery.m.flip.js',
131
                    "jQuery( document ).ready( function() {
132
                        jQuery( '.apf-badge-image' ).mflip();
133
                    } );",
134
                ),
135
            )
136
        );
137
138
        $this->setPluginSettingsLinkLabel( '' ); // pass an empty string to disable it.
139
        
140
        // Hook
141
        add_action( "load_" . $this->oProp->sClassName, array( $this, 'replyToLoadClassPages' ) );
142
        add_action( "load_" . AdminPageFrameworkLoader_Registry::$aAdminPages[ 'about' ], array( $this, 'replyToLoadPage' ) );
143
144
    }   
145
    
146
    /**
147
     * Set up page settings.
148
     */
149
    public function replyToLoadClassPages( $oFactory ) {
0 ignored issues
show
Unused Code introduced by
The parameter $oFactory is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

149
    public function replyToLoadClassPages( /** @scrutinizer ignore-unused */ $oFactory ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
150
151
        $this->setPageHeadingTabsVisibility( false ); // disables the page heading tabs by passing false.
152
        $this->setInPageTabTag( 'h2' ); // sets the tag used for in-page tabs     
153
        $this->setPageTitleVisibility( false ); // disable the page title of a specific page.
154
    
155
    }
156
        
157
    /**
158
     * Triggered when the page loads.
159
     * 
160
     * Adds tabs.
161
     * @callback    action      load_{page slug}
162
     * @return      void
163
     */
164
    public function replyToLoadPage( $oFactory ) {
165
166
        $_sPageSlug = AdminPageFrameworkLoader_Registry::$aAdminPages[ 'about' ];
167
        new AdminPageFrameworkLoader_AdminPageWelcome_Welcome( 
168
            $this,              // factory object
169
            $_sPageSlug,        // page slug
170
            array(
171
                'tab_slug'      => 'welcome',
172
                // 'title'         => __( "What's New", 'admin-page-framework-loader' ),   // '
173
                'style'         => array(
174
                    AdminPageFrameworkLoader_Registry::$sDirPath . '/asset/css/admin.css',
175
                    AdminPageFrameworkLoader_Registry::$sDirPath . '/asset/css/code.css',
176
                ),
177
            )                
178
        );        
179
180
        $this->_setPreferences( $oFactory );
181
        
182
    }
183
        /**
184
         * Gets triggered when the page loads.
185
         */
186
        private function _setPreferences( $oFactory ) {
187
188
            $this->oProp->sWrapperClassAttribute = "wrap about-wrap";
189
            
190
            $oFactory->setInPageTabsVisibility( false );
191
            
192
            add_filter( "content_top_{$this->sPageSlug}", array( $this, 'replyToFilterContentTop' ) );
193
                       
194
        } 
195
                       
196
    /**
197
     * Filters the top part of the page content.
198
     * 
199
     * @callback    filter      content_top_{page slug}
200
     * @return      string
201
     */
202
    public function replyToFilterContentTop( $sContent ) {
203
204
        $_sVersion      = '- ' . AdminPageFrameworkLoader_Registry::VERSION;
205
        $_sPluginName   = AdminPageFrameworkLoader_Registry::SHORTNAME . ' ' . $_sVersion;
206
        
207
        $_sBadgeURL     = esc_url( AdminPageFrameworkLoader_Registry::getPluginURL( 'asset/image/icon-128x128.png' ) );
208
        
209
        $_aOutput   = array();
210
        $_aOutput[] = "<h1>" 
211
                . sprintf( __( 'Welcome to %1$s', 'admin-page-framework-loader' ), $_sPluginName )
212
            . "</h1>";
213
        $_aOutput[] = "<div class='about-text'>"
214
                . 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 )
215
            . "</div>";
216
        $_aOutput[] = ''
217
                . "<div class='apf-badge'>"
218
                    . "<div class='apf-badge-image m-flip'>"
219
                        . "<img src='{$_sBadgeURL}' />"
220
                    . "</div>"
221
                    . "<span class='label'>" . sprintf( __( 'Version %1$s', 'admin-page-framework-loader' ), $_sVersion ) . "</span>"
222
                . "</div>";
223
           
224
        return implode( PHP_EOL, $_aOutput )
225
            . $sContent;
226
    }
227
 
228
}
229