config
9 years ago
css
8 years ago
data
9 years ago
images
9 years ago
js
8 years ago
vendor
9 years ago
views
8 years ago
class-tiny-compress-client.php
9 years ago
class-tiny-compress-fopen.php
9 years ago
class-tiny-compress.php
9 years ago
class-tiny-exception.php
9 years ago
class-tiny-image-size.php
9 years ago
class-tiny-image.php
9 years ago
class-tiny-notices.php
9 years ago
class-tiny-php.php
9 years ago
class-tiny-plugin.php
8 years ago
class-tiny-settings.php
8 years ago
class-tiny-wp-base.php
9 years ago
class-tiny-notices.php
142 lines
| 1 | <?php |
| 2 | /* |
| 3 | * Tiny Compress Images - WordPress plugin. |
| 4 | * Copyright (C) 2015-2017 Voormedia B.V. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the Free |
| 8 | * Software Foundation; either version 2 of the License, or (at your option) |
| 9 | * any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 14 | * more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., 51 |
| 18 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | class Tiny_Notices extends Tiny_WP_Base { |
| 22 | private $notices; |
| 23 | private $dismissals; |
| 24 | |
| 25 | private static function get_option_key() { |
| 26 | return self::get_prefixed_name( 'admin_notices' ); |
| 27 | } |
| 28 | |
| 29 | private static function get_user_meta_key() { |
| 30 | return self::get_prefixed_name( 'admin_notice_dismissals' ); |
| 31 | } |
| 32 | |
| 33 | public function admin_init() { |
| 34 | add_action( 'wp_ajax_tiny_dismiss_notice', $this->get_method( 'dismiss' ) ); |
| 35 | if ( current_user_can( 'manage_options' ) ) { |
| 36 | $this->show_stored(); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | private function load_notices() { |
| 41 | if ( is_array( $this->notices ) ) { |
| 42 | return; |
| 43 | } |
| 44 | $option = get_option( self::get_option_key() ); |
| 45 | $this->notices = is_array( $option ) ? $option : array(); |
| 46 | } |
| 47 | |
| 48 | private function save_notices() { |
| 49 | update_option( self::get_option_key(), $this->notices ); |
| 50 | } |
| 51 | |
| 52 | private function load() { |
| 53 | $this->load_notices(); |
| 54 | $this->load_dismissals(); |
| 55 | } |
| 56 | |
| 57 | private function load_dismissals() { |
| 58 | if ( is_array( $this->dismissals ) ) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | $meta = get_user_meta( |
| 63 | $this->get_user_id(), |
| 64 | $this->get_user_meta_key(), |
| 65 | true |
| 66 | ); |
| 67 | |
| 68 | $this->dismissals = is_array( $meta ) ? $meta : array(); |
| 69 | } |
| 70 | |
| 71 | private function save_dismissals() { |
| 72 | update_user_meta( |
| 73 | $this->get_user_id(), |
| 74 | $this->get_user_meta_key(), |
| 75 | $this->dismissals |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | private function show_stored() { |
| 80 | $this->load(); |
| 81 | foreach ( $this->notices as $name => $message ) { |
| 82 | if ( empty( $this->dismissals[ $name ] ) ) { |
| 83 | $this->show( $name, $message ); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | public function add( $name, $message ) { |
| 89 | $this->load_notices(); |
| 90 | $this->notices[ $name ] = $message; |
| 91 | $this->save_notices(); |
| 92 | } |
| 93 | |
| 94 | public function remove( $name ) { |
| 95 | $this->load(); |
| 96 | if ( isset( $this->notices[ $name ] ) ) { |
| 97 | unset( $this->notices[ $name ] ); |
| 98 | $this->save_notices(); |
| 99 | } |
| 100 | if ( isset( $this->dismissals[ $name ] ) ) { |
| 101 | unset( $this->dismissals[ $name ] ); |
| 102 | $this->save_dismissals(); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | public function dismiss() { |
| 107 | if ( empty( $_POST['name'] ) || ! $this->check_ajax_referer() ) { |
| 108 | echo json_encode( false ); |
| 109 | exit(); |
| 110 | } |
| 111 | $this->load_dismissals(); |
| 112 | $this->dismissals[ $_POST['name'] ] = true; |
| 113 | $this->save_dismissals(); |
| 114 | echo json_encode( true ); |
| 115 | exit(); |
| 116 | } |
| 117 | |
| 118 | public function show( $name, $message, $klass = 'error', $dismissible = true ) { |
| 119 | $css = array( $klass, 'notice', 'tiny-notice' ); |
| 120 | if ( ! $dismissible ) { |
| 121 | $add = '</p>'; |
| 122 | } elseif ( self::check_wp_version( 4.2 ) ) { |
| 123 | $add = '</p>'; |
| 124 | $css[] = 'is-dismissible'; |
| 125 | } else { |
| 126 | $add = ' <a href="#" class="tiny-dismiss">' . |
| 127 | esc_html__( 'Dismiss', 'tiny-compress-images' ) . '</a></p>'; |
| 128 | } |
| 129 | |
| 130 | $css = implode( ' ', $css ); |
| 131 | $plugin_name = esc_html__( 'Compress JPEG & PNG images', 'tiny-compress-images' ); |
| 132 | |
| 133 | add_action( 'admin_notices', |
| 134 | create_function( |
| 135 | '', |
| 136 | "echo '<div class=\"$css\" data-name=\"$name\"><p>" . |
| 137 | $plugin_name . ": $message$add</div>';" |
| 138 | ) |
| 139 | ); |
| 140 | } |
| 141 | } |
| 142 |