PageRenderTime 33ms CodeModel.GetById 27ms app.highlight 4ms RepoModel.GetById 0ms app.codeStats 0ms

/redux/ReduxCore/inc/class.redux_admin_notices.php

https://gitlab.com/mahbubur204/RECIPES-Landing-page
PHP | 189 lines | 97 code | 29 blank | 63 comment | 23 complexity | a8cbc18d47618e69cdf2efb0947e97aa MD5 | raw file
  1<?php
  2
  3    /**
  4     * Redux Framework Admin Notice Class
  5     * Makes instantiating a Redux object an absolute piece of cake.
  6     *
  7     * @package     Redux_Framework
  8     * @author      Kevin Provance
  9     * @author      Dovy Paukstys
 10     * @subpackage  Core
 11     */
 12
 13    // Exit if accessed directly
 14    if ( ! defined( 'ABSPATH' ) ) {
 15        exit;
 16    }
 17
 18    // Don't duplicate me!
 19    if ( ! class_exists( 'Redux_Admin_Notices' ) ) {
 20
 21        /**
 22         * Redux API Class
 23         * Simple API for Redux Framework
 24         *
 25         * @since       1.0.0
 26         */
 27        class Redux_Admin_Notices {
 28
 29            static public $_parent;
 30
 31            public static function load() {
 32                add_action( 'wp_ajax_redux_hide_admin_notice', array(
 33                    'Redux_Admin_Notices',
 34                    'dismissAdminNoticeAJAX'
 35                ) );
 36            }
 37
 38            /**
 39             * adminNotices - Evaluates user dismiss option for displaying admin notices
 40             *
 41             * @since       3.2.0
 42             * @access      public
 43             * @return      void
 44             */
 45            public static function adminNotices( $notices = array() ) {
 46                global $current_user, $pagenow;
 47
 48                // Check for an active admin notice array
 49                if ( ! empty( $notices ) ) {
 50
 51                    // Enum admin notices
 52                    foreach ( $notices as $notice ) {
 53
 54                        $add_style = '';
 55                        if ( strpos( $notice['type'], 'redux-message' ) != false ) {
 56                            $add_style = 'style="border-left: 4px solid ' . esc_attr( $notice['color'] ) . '!important;"';
 57                        }
 58
 59                        if ( true == $notice['dismiss'] ) {
 60
 61                            // Get user ID
 62                            $userid = $current_user->ID;
 63
 64                            if ( ! get_user_meta( $userid, 'ignore_' . $notice['id'] ) ) {
 65
 66                                // Check if we are on admin.php.  If we are, we have
 67                                // to get the current page slug and tab, so we can
 68                                // feed it back to Wordpress.  Why>  admin.php cannot
 69                                // be accessed without the page parameter.  We add the
 70                                // tab to return the user to the last panel they were
 71                                // on.
 72                                $pageName = '';
 73                                $curTab   = '';
 74                                if ( $pagenow == 'admin.php' || $pagenow == 'themes.php' ) {
 75
 76                                    // Get the current page.  To avoid errors, we'll set
 77                                    // the redux page slug if the GET is empty.
 78                                    $pageName = empty( $_GET['page'] ) ? '&amp;page=' . self::$_parent->args['page_slug'] : '&amp;page=' . esc_attr( $_GET['page'] );
 79
 80                                    // Ditto for the current tab.
 81                                    $curTab = empty( $_GET['tab'] ) ? '&amp;tab=0' : '&amp;tab=' . esc_attr( $_GET['tab'] );
 82                                }
 83
 84                                global $wp_version;
 85                                // Print the notice with the dismiss link
 86                                if ( version_compare( $wp_version, '4.2', '>' ) ) {
 87                                    $output    = "";
 88                                    $css_id    = esc_attr( $notice['id'] ) . $pageName . $curTab;
 89                                    $css_class = esc_attr( $notice['type'] ) . ' redux-notice notice is-dismissible redux-notice';
 90                                    $output .= "<div {$add_style} id='$css_id' class='$css_class'> \n";
 91                                    $nonce = wp_create_nonce( $notice['id'] . $userid . 'nonce' );
 92                                    $output .= "<input type='hidden' class='dismiss_data' id='" . esc_attr( $notice['id'] ) . $pageName . $curTab . "' value='{$nonce}'> \n";
 93                                    $output .= '<p>' . wp_kses_post( $notice['msg'] ) . '</p>';
 94                                    $output .= "</div> \n";
 95                                    echo $output;
 96                                } else {
 97                                    echo '<div ' . $add_style . ' class="' . esc_attr( $notice['type'] ) . ' notice is-dismissable"><p>' . wp_kses_post( $notice['msg'] ) . '&nbsp;&nbsp;<a href="?dismiss=true&amp;id=' . esc_attr( $notice['id'] ) . $pageName . $curTab . '">' . esc_html__( 'Dismiss', 'redux-framework' ) . '</a>.</p></div>';
 98                                }
 99                            }
100                        } else {
101                            // Standard notice
102                            echo '<div ' . $add_style . ' class="' . esc_attr( $notice['type'] ) . ' notice"><p>' . wp_kses_post( $notice['msg'] ) . '</a>.</p></div>';
103                        }
104                        ?>
105                        <script>
106                            jQuery( document ).ready(
107                                function( $ ) {
108                                    $( 'body' ).on(
109                                        'click', '.redux-notice.is-dismissible .notice-dismiss', function( event ) {
110                                            var $data = $( this ).parent().find( '.dismiss_data' );
111                                            $.post(
112                                                ajaxurl, {
113                                                    action: 'redux_hide_admin_notice',
114                                                    id: $data.attr( 'id' ),
115                                                    nonce: $data.val()
116                                                }
117                                            );
118                                        }
119                                    );
120                                }
121                            );
122                        </script>
123                        <?php
124                        /*
125
126                         */
127
128                    }
129
130
131                    // Clear the admin notice array
132                    self::$_parent->admin_notices = array();
133                }
134            }
135
136            /**
137             * dismissAdminNotice - Updates user meta to store dismiss notice preference
138             *
139             * @since       3.2.0
140             * @access      public
141             * @return      void
142             */
143            public static function dismissAdminNotice() {
144                global $current_user;
145
146                // Verify the dismiss and id parameters are present.
147                if ( isset( $_GET['dismiss'] ) && isset( $_GET['id'] ) ) {
148                    if ( 'true' == $_GET['dismiss'] || 'false' == $_GET['dismiss'] ) {
149
150                        // Get the user id
151                        $userid = $current_user->ID;
152
153                        // Get the notice id
154                        $id  = esc_attr( $_GET['id'] );
155                        $val = esc_attr( $_GET['dismiss'] );
156
157                        // Add the dismiss request to the user meta.
158                        update_user_meta( $userid, 'ignore_' . $id, $val );
159                    }
160                }
161            }
162
163            /**
164             * dismissAdminNotice - Updates user meta to store dismiss notice preference
165             *
166             * @since       3.2.0
167             * @access      public
168             * @return      void
169             */
170            public static function dismissAdminNoticeAJAX() {
171                global $current_user;
172
173                // Get the notice id
174                $id = explode( '&', $_POST['id'] );
175                $id = $id[0];
176                // Get the user id
177                $userid = $current_user->ID;
178
179                if ( ! wp_verify_nonce( $_POST['nonce'], $id . $userid . 'nonce' ) ) {
180                    die( 0 );
181                } else {
182                    // Add the dismiss request to the user meta.
183                    update_user_meta( $userid, 'ignore_' . $id, true );
184                }
185            }
186        }
187
188        Redux_Admin_Notices::load();
189    }