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

hasNotice()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 23
rs 8.7972
cc 4
eloc 13
nc 4
nop 1
1
<?php
2
/**
3
 * Admin Page Framework
4
 * 
5
 * http://en.michaeluno.jp/admin-page-framework/
6
 * Copyright (c) 2013-2015 Michael Uno; Licensed MIT
7
 * 
8
 */
9
10
/**
11
 * Provides methods to handle setting notices.
12
 * 
13
 * @package     AdminPageFramework
14
 * @subpackage  Form
15
 * @since       DEVVER
16
 */
17
class AdminPageFramework_Form___SubmitNotice extends AdminPageFramework_WPUtility {
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...
18
    
19
    /**
20
     * Stores form submit notifications.
21
     * 
22
     * At the script termination, these will be saved as a transient in the database.
23
     */
24
    static private $_aNotices = array();
25
    
26
    /**
27
     * Checks if a submit notice has been set.
28
     * 
29
     * This is used in the internal validation callback method to decide whether the system error or update notice should be added or not.
30
     * If this method yields true, the framework discards the system message and displays the user set notification message.
31
     * 
32
     * @param       string      $sType If empty, the method will check if a message exists in all types. Otherwise, it checks the existence of a message of the specified type.
33
     * @return      boolean     True if a setting notice is set; otherwise, false.
34
     */
35
    public function hasNotice( $sType='' ) {
36
                
37
        if ( ! $sType ) {
38
            return ( bool ) count( self::$_aNotices );
39
        }
40
        
41
        // Check if there is a message of the type.
42
        foreach( self::$_aNotices as $_aNotice ) {
43
            $_sClassAttribute = $this->getElement( 
44
                $_aNotice, 
45
                array( 
46
                    'aAttributes', 
47
                    'class' 
48
                ),
49
                ''
50
            );
51
            if ( $_sClassAttribute === $sType ) {
52
                return true;
53
            }
54
        }
55
        return false;
56
        
57
    }
58
    
59
    /**
60
     * Sets the given message to be displayed in the next page load. 
61
     * 
62
     * This is used to inform users about the submitted input data, such as "Updated successfully." or "Problem occurred." etc. 
63
     * and normally used in validation callback methods.
64
     * 
65
     * <h4>Example</h4>
66
     * `
67
     * if ( ! $bVerified ) {
68
     *       $this->setFieldErrors( $aErrors );     
69
     *       $this->setSettingNotice( 'There was an error in your input.' );
70
     *       return $aOldPageOptions;
71
     * }
72
     * `
73
     * @since        DEVVER
74
     * @access       public
75
     * @param        string      $sMessage       the text message to be displayed.
76
     * @param        string      $sType          (optional) the type of the message, either "error" or "updated"  is used.
77
     * @param        array       $asAttributes   (optional) the tag attribute array applied to the message container HTML element. If a string is given, it is used as the ID attribute value.
78
     * @param        boolean     $bOverride      (optional) If true, only one message will be shown in the next page load. false: do not override when there is a message of the same id. true: override the previous one.
79
     * @return       void
80
     */
81
    public function set( $sMessage, $sType='error', $asAttributes=array(), $bOverride=true ) {
82
        
83
        // If the array is empty, shecule the task of saving the array at shutdown.
84
        if ( empty( self::$_aNotices ) ) {
85
            add_action( 'shutdown', array( $this, '_replyToSaveNotices' ) ); // the method is defined in the model class.
86
        }
87
        
88
        $_sID = md5( trim( $sMessage ) );
89
            
90
        // If the override is false and a message is already set, do not add.
91
        if ( ! $bOverride && isset( self::$_aNotices[ $_sID ] ) ) {
92
            return;
93
        }
94
95
        // If the override is true, reset the notice array.
96
        if ( $bOverride ) {
97
            self::$_aNotices = array();
98
        }
99
        
100
        $_aAttributes = $this->getAsArray( $asAttributes );
101
        if ( is_string( $asAttributes ) && ! empty( $asAttributes ) ) {
102
            $_aAttributes[ 'id' ] = $asAttributes;
103
        }
104
        self::$_aNotices[ $_sID ] = array(
105
            'sMessage'      => $sMessage,
106
            'aAttributes'   => $_aAttributes + array(
107
                'class'     => $sType,
108
                'id'        => 'form_submit_notice_' . $_sID,
109
            ),
110
        );
111
112
113
    }       
114
        /**
115
         * Saves the notification array set via the setSettingNotice() method.
116
         * 
117
         * @since       3.0.4 
118
         * @sine        DEVVER      Moved from `AdminPageFramework_Factory_Model`.
119
         * @internal
120
         * @callback    action      shutdown
121
         * @return      void
122
         */
123
        public function _replyToSaveNotices() {
124
            if ( empty( self::$_aNotices ) ) { 
125
                return; 
126
            }
127
            $this->setTransient( 
128
                'apf_notices_' . get_current_user_id(), 
129
                self::$_aNotices
130
            );
131
        }        
132
    
133
    /**
134
     * Outputs the stored submit notices in the database.
135
     * @return      void
136
     */
137
    public function render() {
0 ignored issues
show
Coding Style introduced by
render 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...
138
        
139
        // This will load scripts for the fade-in effect.
140
        new AdminPageFramework_AdminNotice( '' );
141
        
142
        $_iUserID  = get_current_user_id();
143
        $_aNotices = $this->getTransient( "apf_notices_{$_iUserID}" );
144
        if ( false === $_aNotices ) { 
145
            return; 
146
        }
147
        $this->deleteTransient( "apf_notices_{$_iUserID}" );
148
    
149
        // By setting false to the 'settings-notice' key, it's possible to disable the notifications set with the framework.
150
        if ( isset( $_GET[ 'settings-notice' ] ) && ! $_GET[ 'settings-notice' ] ) { 
151
            return; 
152
        }
153
        
154
        $this->_printNotices( $_aNotices );                
155
        
156
    }
157
        /**
158
         * Displays settings notices.
159
         * @since       3.5.3
160
         * @since       DEVVER      Moved from `AdminPageFramework_Factory_View`. Renamed from `_printSettingNotices()`.
161
         * @internal
162
         * @return      void
163
         */
164
        private function _printNotices( $aNotices ) {
165
            
166
            $_aPeventDuplicates = array();
167
            foreach ( array_filter( ( array ) $aNotices, 'is_array' ) as $_aNotice ) {
168
                
169
                $_sNotificationKey = md5( serialize( $_aNotice ) );
170
                if ( isset( $_aPeventDuplicates[ $_sNotificationKey ] ) ) {
171
                    continue;
172
                }
173
                $_aPeventDuplicates[ $_sNotificationKey ] = true;
174
                
175
                new AdminPageFramework_AdminNotice(
176
                    $this->getElement( $_aNotice, 'sMessage' ),
177
                    $this->getElement( $_aNotice, 'aAttributes' )
178
                );              
179
              
180
            }            
181
            
182
        }    
183
        
184
}