class-admin-notice-custom-css.php
1 week ago
class-admin-notices.php
1 week ago
class-admin.php
1 week ago
class-attachment-fields.php
1 week ago
class-columns.php
1 week ago
class-demo-content.php
1 week ago
class-extensions.php
1 week ago
class-gallery-attachment-modal.php
1 week ago
class-gallery-datasources.php
1 week ago
class-gallery-editor.php
1 week ago
class-gallery-metabox-fields.php
1 week ago
class-gallery-metabox-items.php
1 week ago
class-gallery-metabox-settings-helper.php
1 week ago
class-gallery-metabox-settings.php
1 week ago
class-gallery-metabox-template.php
1 week ago
class-gallery-metaboxes.php
1 week ago
class-menu.php
1 week ago
class-pro-promotion.php
1 week ago
class-settings.php
1 week ago
class-silent-installer-skin.php
1 week ago
class-trial-mode.php
1 week ago
demo-content-galleries.php
1 week ago
demo-content-images.php
1 week ago
index.php
1 week ago
pro-features.php
1 week ago
view-features.php
1 week ago
view-help-demos.php
1 week ago
view-help-getting-started.php
1 week ago
view-help-pro.php
1 week ago
view-help.php
1 week ago
view-system-info.php
1 week ago
class-admin-notice-custom-css.php
302 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | /* |
| 8 | * FooGallery Admin Custom CSS Notice class |
| 9 | */ |
| 10 | |
| 11 | if ( ! class_exists( 'FooGallery_Admin_Notice_CustomCSS' ) ) { |
| 12 | |
| 13 | class FooGallery_Admin_Notice_CustomCSS { |
| 14 | |
| 15 | private $option_name = FOOGALLERY_OPTION_CUSTOM_CSS; |
| 16 | private $notice_id = 'customcss'; |
| 17 | private $notice_class = 'notice-info'; |
| 18 | private $css_classes_to_check = array( 'fg-caption' ); |
| 19 | |
| 20 | public function __construct() { |
| 21 | // We only need to check this after v2.5.0 |
| 22 | if ( version_compare( FOOGALLERY_VERSION, '2.5.0', '<' ) ) { |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | // display the notice |
| 27 | add_action( 'admin_notices', array( $this, 'display_notice' ) ); |
| 28 | // ajax handler to dismiss the notice |
| 29 | add_action( 'wp_ajax_foogallery_admin_notice_dismiss-' . $this->notice_id, array( $this, 'dismiss_notice' ) ); |
| 30 | // clear the saved data if a gallery is saved, or settings are updated |
| 31 | add_action( 'foogallery_after_save_gallery', array( $this, 'determine_and_save_option' ) ); |
| 32 | add_action( 'update_option_foogallery', array( $this, 'after_settings_updated' ), 10, 3 ); |
| 33 | // override the settings |
| 34 | add_filter( 'foogallery_admin_settings_override', array( $this, 'override_settings' ) ); |
| 35 | //render the custom css settings |
| 36 | add_action( 'foogallery_admin_settings_custom_type_render_setting', array( $this, 'render_settings' ) ); |
| 37 | |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Override the settings |
| 42 | */ |
| 43 | function override_settings( $settings ) { |
| 44 | $option = $this->get_option(); |
| 45 | |
| 46 | if ( count( $option['galleries'] ) > 0 || $option['setting'] ) { |
| 47 | |
| 48 | $index = foogallery_admin_fields_find_index_of_field( $settings['settings'], 'custom_js' ); |
| 49 | |
| 50 | $link_html = '<a target="_blank" href="' . esc_url( 'https://fooplugins.com/documentation/foogallery/troubleshooting-foogallery/foogallery-v3-0-custom-css-migration-guide/' ) . '">' . __( 'Custom CSS v3 Migration Guide', 'foogallery' ) . '</a>'; |
| 51 | |
| 52 | $new_settings[] = array( |
| 53 | 'id' => 'custom_css_update', |
| 54 | 'title' => __( 'Custom CSS Review!', 'foogallery' ), |
| 55 | 'desc' => __( 'We found custom CSS that needs to be reviewed. Since FooGallery update v3, custom CSS related to captions (specifically when using `fg-caption`), nees to be reivewed and in some cases updated.', 'foogallery' ) . '<br />' . |
| 56 | __( 'Changes are not always needed, so if you review your Custom CSS and all seems correct, you can safely dismiss the admin notice, and ignore this message.', 'foogallery' ) . '<br />' . |
| 57 | /* translators: %s: Value inserted at runtime. */ |
| 58 | sprintf( __( 'For more info, please read our %s.', 'foogallery' ), $link_html ), |
| 59 | 'type' => 'custom_css_update', |
| 60 | 'tab' => 'custom_assets', |
| 61 | ); |
| 62 | |
| 63 | array_splice( $settings['settings'], $index, 0, $new_settings ); |
| 64 | } |
| 65 | |
| 66 | return $settings; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Render any custom setting types to the settings page |
| 71 | */ |
| 72 | function render_settings( $args ) { |
| 73 | if ( 'custom_css_update' === $args['type'] ) { |
| 74 | $option = $this->get_option(); |
| 75 | |
| 76 | if ( is_array( $option ) && count( $option ) > 0 ) { |
| 77 | $galleries = $option['galleries']; |
| 78 | if ( count( $galleries ) > 0 ) { |
| 79 | ?> |
| 80 | <p><?php esc_html_e( 'You will need to update the custom CSS on the following galleries:', 'foogallery' ); ?></p> |
| 81 | <ul class="ul-disc"> |
| 82 | <?php foreach ( $galleries as $gallery_id => $gallery_name ) { ?> |
| 83 | <li><a href="<?php echo esc_url( get_edit_post_link( $gallery_id ) ); ?>"><?php echo esc_html( $gallery_name ); ?></a></li> |
| 84 | <?php } ?> |
| 85 | </ul> |
| 86 | <?php |
| 87 | } |
| 88 | if ( $option['setting'] ) { |
| 89 | ?> |
| 90 | <p><?php esc_html_e( 'You have outdated custom CSS saved in the Custom Stylesheet setting below that needs to be updated.', 'foogallery' ); ?></p> |
| 91 | <?php |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Clear the saved data after settings are updated. |
| 99 | */ |
| 100 | function after_settings_updated( $old_value, $value, $option ) { |
| 101 | if ( !is_admin() ) { |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | if ( !current_user_can( 'manage_options' ) ) { |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | $this->determine_and_save_option(); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Display the admin notice. |
| 114 | */ |
| 115 | function display_notice() { |
| 116 | if ( $this->should_show_notice() ) { |
| 117 | ?> |
| 118 | <script type="text/javascript"> |
| 119 | (function( $ ) { |
| 120 | $( document ).ready( function() { |
| 121 | $( '.foogallery-admin-notice-<?php echo esc_attr( $this->notice_id ); ?>.is-dismissible' ) |
| 122 | .on( 'click', '.notice-dismiss', function( e ) { |
| 123 | e.preventDefault(); |
| 124 | $.post( ajaxurl, { |
| 125 | action: 'foogallery_admin_notice_dismiss-<?php echo esc_js( $this->notice_id ); ?>', |
| 126 | url: "<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>", |
| 127 | _wpnonce: "<?php echo esc_attr( wp_create_nonce( 'foogallery_admin_notice_dismiss-' . $this->notice_id )); ?>" |
| 128 | } ); |
| 129 | } ); |
| 130 | } ); |
| 131 | })( jQuery ); |
| 132 | </script> |
| 133 | <div class="notice <?php echo esc_attr( $this->notice_class ); ?> is-dismissible foogallery-admin-notice-<?php echo esc_attr( $this->notice_id ); ?>"> |
| 134 | <?php $this->display_notice_inner_html(); ?> |
| 135 | </div> |
| 136 | <?php |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Display the inner HTML of the admin notice. |
| 142 | */ |
| 143 | function display_notice_inner_html() { |
| 144 | $option = $this->get_option(); |
| 145 | $url = foogallery_admin_settings_url() . '#custom_assets'; |
| 146 | $link = '<a href="'. $url . '">' . __( 'FooGallery Settings', 'foogallery' ) . '</a>'; |
| 147 | if ( is_array( $option ) && count( $option ) > 0 ) { ?> |
| 148 | <h3><?php esc_html_e( 'FooGallery - Custom CSS Update Required', 'foogallery' ); ?></h3> |
| 149 | <?php |
| 150 | $galleries = $option['galleries']; |
| 151 | if ( count( $galleries ) > 0 ) { |
| 152 | ?> |
| 153 | <?php /* translators: %s: Value inserted at runtime. */ ?> |
| 154 | <p><?php printf( esc_html__( 'We found outdated custom CSS in %s galleries that needs to be updated.', 'foogallery' ), count( $galleries ) ); ?></p> |
| 155 | <?php |
| 156 | } |
| 157 | if ( $option['setting'] ) { |
| 158 | ?> |
| 159 | <p><?php printf( esc_html__( 'You have outdated custom CSS saved in settings that needs to be updated.', 'foogallery' ) ); ?></p> |
| 160 | <?php |
| 161 | } |
| 162 | ?> |
| 163 | <?php /* translators: %s: Value inserted at runtime. */ ?> |
| 164 | <p><?php printf( esc_html__( 'You will need to update your custom CSS! Visit %s to see what needs updating', 'foogallery' ), wp_kses_post( $link ) ); ?></p> |
| 165 | <?php } |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Determine if the notice should be shown. |
| 170 | */ |
| 171 | function should_show_notice() { |
| 172 | $option = $this->get_option( true ); |
| 173 | |
| 174 | if ( !is_array( $option ) ) { |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | if ( count( $option['galleries'] ) === 0 && !$option['setting'] ) { |
| 179 | // we have previously checked and the results were clear. |
| 180 | return false; |
| 181 | } else if ( $option['hide'] ) { |
| 182 | // the user has hidden the notice. Never show! |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | // If we get here, then the notice should be shown. |
| 187 | return true; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Get the option. |
| 192 | */ |
| 193 | function get_option( $determine = false ) { |
| 194 | $option = get_option( $this->option_name ); |
| 195 | |
| 196 | // If false, then the option doesn't exist. |
| 197 | if ( $option === false ) { |
| 198 | if ( $determine ) { |
| 199 | $this->determine_and_save_option(); |
| 200 | $option = get_option( $this->option_name ); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | if ( !is_array( $option ) ) { |
| 205 | // Malformed. |
| 206 | $option = array( |
| 207 | 'galleries' => array(), |
| 208 | 'setting' => false, |
| 209 | 'hide' => false |
| 210 | ); |
| 211 | } |
| 212 | |
| 213 | return $option; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Get the hidden state of the option. |
| 218 | */ |
| 219 | function get_option_hidden() { |
| 220 | $option = get_option( $this->option_name ); |
| 221 | |
| 222 | return isset( $option['hide'] ) ? $option['hide'] : false; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Ajax handler to dismiss the notice. |
| 227 | */ |
| 228 | function dismiss_notice() { |
| 229 | if ( ! check_ajax_referer( 'foogallery_admin_notice_dismiss-' . $this->notice_id, false, false ) ) { |
| 230 | wp_send_json_error( |
| 231 | array( 'message' => __( 'Invalid security token.', 'foogallery' ) ), |
| 232 | 403 |
| 233 | ); |
| 234 | } |
| 235 | |
| 236 | if ( ! current_user_can( 'manage_options' ) ) { |
| 237 | wp_send_json_error( |
| 238 | array( 'message' => __( 'Insufficient permissions.', 'foogallery' ) ), |
| 239 | 403 |
| 240 | ); |
| 241 | } |
| 242 | |
| 243 | // Just update the hide flag. |
| 244 | $option = $this->get_option(); |
| 245 | $option['hide'] = true; |
| 246 | update_option( $this->option_name, $option, false ); |
| 247 | |
| 248 | wp_send_json_success(); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Determine and save the option. |
| 253 | */ |
| 254 | function determine_and_save_option() { |
| 255 | $option = $option = array( |
| 256 | 'galleries' => array(), |
| 257 | 'setting' => false, |
| 258 | 'hide' => $this->get_option_hidden() |
| 259 | ); |
| 260 | |
| 261 | $galleries = foogallery_get_all_galleries(); |
| 262 | |
| 263 | $galleries_with_css = array(); |
| 264 | $custom_css_setting_contains = false; |
| 265 | |
| 266 | if ( ! empty( $galleries ) ) { |
| 267 | foreach ( $galleries as $gallery ) { |
| 268 | $custom_css = get_post_meta( $gallery->ID, FOOGALLERY_META_CUSTOM_CSS, true ); |
| 269 | |
| 270 | if ( $this->does_contain_css_classes( $custom_css ) ) { |
| 271 | $galleries_with_css[$gallery->ID] = $gallery->name; |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | $custom_css_setting = foogallery_get_setting( 'custom_css' ); |
| 277 | |
| 278 | if ( !empty( $custom_css_setting ) ) { |
| 279 | if ( $this->does_contain_css_classes( $custom_css_setting ) ) { |
| 280 | $custom_css_setting_contains = true; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | $option['galleries'] = $galleries_with_css; |
| 285 | $option['setting'] = $custom_css_setting_contains; |
| 286 | update_option( $this->option_name, $option, false ); |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Check if the CSS contains any of the classes we are looking for. |
| 291 | */ |
| 292 | function does_contain_css_classes( $css ) { |
| 293 | foreach ( $this->css_classes_to_check as $class ) { |
| 294 | if ( strpos( $css, $class ) !== false ) { |
| 295 | return true; |
| 296 | } |
| 297 | } |
| 298 | return false; |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 |