* @return void */ public function __construct() { // Clean up a bit the top of the admin pages when notices exist add_action( 'admin_enqueue_scripts', array( $this, 'clean_administration_notices_classic' ) ); add_action( 'enqueue_block_assets', array( $this, 'clean_administration_notices_gutenberg' ) ); // Deal with the missing media states notices add_action( 'all_admin_notices', array( $this, 'display_missing_channel_notice' ) ); add_action( 'all_admin_notices', array( $this, 'display_missing_media_notice' ) ); // Offer the hybrid rendering mode to the existing sites, and report the widgets cache actions add_action( 'all_admin_notices', array( $this, 'display_hybrid_mode_notice' ) ); add_action( 'all_admin_notices', array( $this, 'display_fragments_result_notice' ) ); add_action( 'all_admin_notices', array( $this, 'display_fragments_backfill_notice' ) ); add_action( 'admin_post_affilizz_enable_hybrid_mode', array( $this, 'enable_hybrid_mode' ) ); add_action( 'admin_post_affilizz_dismiss_hybrid_mode_notice', array( $this, 'dismiss_hybrid_mode_notice' ) ); } /** * Offers the hybrid rendering mode to the sites that do not use it, until it is enabled or the notice dismissed. * @author Affilizz * @since 1.18.0 * @return void */ public function display_hybrid_mode_notice() { if ( ! current_user_can( 'manage_options' ) || get_option( 'affilizz_hybrid_mode_notice_dismissed' ) || 'hybrid' === \Affilizz\Core::get_current_rendering_mode() ) { return; } echo wp_kses( $this->wrap_single_line_notice( 'success', wp_sprintf( /* translators: 1 and 2: strong tags, 3 and 4: enable link tags, 5 and 6: dismiss link tags. */ __( '%1$sAffilizz%2$s : the new %1$shybrid%2$s rendering mode includes your widgets in the HTML of your pages, so that search engines and AI assistants can read them, and displays them without waiting for the script. %3$sEnable the hybrid mode%4$s or %5$skeep the current mode%6$s.', 'affilizz' ), '', '', '', '', '', '' ) ), \Affilizz\Core::get_extended_allowed_tags() ); } /** * Administration action : enables the hybrid rendering mode. * @author Affilizz * @since 1.18.0 * @return void */ public function enable_hybrid_mode() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Sorry, you are not allowed to do that.', 'affilizz' ), 403 ); } check_admin_referer( 'affilizz_enable_hybrid_mode' ); update_option( 'affilizz_rendering_mode', 'hybrid' ); wp_safe_redirect( wp_get_referer() ? wp_get_referer() : admin_url() ); exit; } /** * Administration action : stops offering the hybrid rendering mode. * @author Affilizz * @since 1.18.0 * @return void */ public function dismiss_hybrid_mode_notice() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Sorry, you are not allowed to do that.', 'affilizz' ), 403 ); } check_admin_referer( 'affilizz_dismiss_hybrid_mode_notice' ); update_option( 'affilizz_hybrid_mode_notice_dismissed', 1, false ); wp_safe_redirect( wp_get_referer() ? wp_get_referer() : admin_url() ); exit; } /** * Reports the result of the widgets cache actions (see \Affilizz\Util\Fragments). * @author Affilizz * @since 1.18.0 * @return void */ public function display_fragments_result_notice() { // phpcs:disable WordPress.Security.NonceVerification.Recommended -- Read-only result of an action checked by its own nonce. $result = isset( $_GET['affilizz_fragments'] ) ? sanitize_key( wp_unslash( $_GET['affilizz_fragments'] ) ) : ''; $count = isset( $_GET['affilizz_fragments_count'] ) ? absint( $_GET['affilizz_fragments_count'] ) : 0; // phpcs:enable WordPress.Security.NonceVerification.Recommended if ( 'purged' === $result ) { /* translators: %d: number of deleted widgets. */ $message = sprintf( _n( 'Widgets cache cleared : %d widget deleted. The page cache of the posts concerned has been purged.', 'Widgets cache cleared : %d widgets deleted. The page cache of the posts concerned has been purged.', $count, 'affilizz' ), $count ); } elseif ( 'refreshed' === $result ) { /* translators: %d: number of refreshed widgets. */ $message = sprintf( _n( '%d Affilizz widget refreshed. The page cache of the post has been purged.', '%d Affilizz widgets refreshed. The page cache of the post has been purged.', $count, 'affilizz' ), $count ); } else { return; } echo wp_kses( $this->wrap_single_line_notice( 'success', esc_html( $message ) ), \Affilizz\Core::get_extended_allowed_tags() ); } /** * Offers to prepare the widgets of the posts published before the hybrid mode, then reports the progress. * @author Affilizz * @since 1.18.0 * @return void */ public function display_fragments_backfill_notice() { $state = \Affilizz\Util\Fragments::get_backfill_state(); if ( ! current_user_can( 'manage_options' ) || ! is_array( $state ) ) { return; } if ( 'proposed' === $state['status'] ) { $message = wp_sprintf( /* translators: 1 and 2: strong tags, 3: number of posts, 4 and 5: prepare link tags, 6 and 7: dismiss link tags. */ _n( '%1$sAffilizz%2$s : %3$d published post contains Affilizz widgets. %4$sPrepare its widgets now%5$s so that they are in its HTML from its next view, or %6$slet them be prepared on its first view%7$s.', '%1$sAffilizz%2$s : %3$d published posts contain Affilizz widgets. %4$sPrepare their widgets now%5$s so that they are in their HTML from their next view, or %6$slet each be prepared on its first view%7$s.', $state['total'], 'affilizz' ), '', '', $state['total'], '', '', '', '' ); } elseif ( 'running' === $state['status'] ) { $message = wp_sprintf( /* translators: 1 and 2: strong tags, 3: number of prepared posts, 4: number of posts. */ __( '%1$sAffilizz%2$s : preparing the widgets of your published posts (%3$d / %4$d posts).', 'affilizz' ), '', '', $state['done'], $state['total'] ); } else { return; } echo wp_kses( $this->wrap_single_line_notice( 'success', $message ), \Affilizz\Core::get_extended_allowed_tags() ); } /** * Cleans up the administration notices by adding custom CSS to the page. * @author Affilizz * @since 1.14.0 * @return void */ public function clean_administration_notices_classic() { if ( ! function_exists( 'get_current_screen' ) ) { return; } if ( ! empty( get_current_screen() ) && get_current_screen()->is_block_editor() ) { return; } wp_add_inline_style( 'affilizz-admin', ':not(.notice) + .notice { margin-top: 20px; }' ); } public function clean_administration_notices_gutenberg() { if ( ! is_admin() ) { return; } if ( ! function_exists( 'get_current_screen' ) ) { return; } if ( empty( get_current_screen() ) || ! get_current_screen()->is_block_editor() ) { return; } wp_add_inline_style( 'affilizz-admin', ':not(.notice) + .notice { margin-top: 20px; }' ); } /** * Wraps the given content in a notice container. * @author Romain Carlier * @since 1.14.0 * @param string $content The content to wrap. * @return string The wrapped content. */ public function wrap_single_line_notice( $notice_class = '', $content = '' ) { return wp_kses( '

' . $content . '

' . PHP_EOL, \Affilizz\Core::get_extended_allowed_tags() ); } /** * Displays an error if there is a missing information. * @author Affilizz * @return void */ public function display_missing_channel_notice() { $missing_channel = get_transient( 'affilizz_missing_channel' ); if ( false !== $missing_channel ) { echo wp_kses( $this->wrap_single_line_notice( 'warning', wp_sprintf( /* translators: 1 and 2: strong tags, 3 and 4: wizard link tags. */ __( '%1$sWarning%2$s : Your Affilizz %1$schannel%2$s seems to be inexistant or wrongly defined. Please %3$sre-execute our wizard%4$s to redefine these values.', 'affilizz' ), '', '', '', '' ) ), \Affilizz\Core::get_extended_allowed_tags() ); } } /** * Displays an error if there is a missing information. * @author Affilizz * @return void */ public function display_missing_media_notice() { $missing_media = get_transient( 'affilizz_missing_media' ); if ( false !== $missing_media ) { echo wp_kses( $this->wrap_single_line_notice( 'warning', wp_sprintf( /* translators: 1 and 2: strong tags, 3 and 4: wizard link tags. */ __( '%1$sWarning%2$s : Your Affilizz %1$smedia%2$s seems to be inexistant or wrongly defined. Please %3$sre-execute our wizard%4$s to redefine these values.', 'affilizz' ), '', '', '', '' ) ), \Affilizz\Core::get_extended_allowed_tags() ); } } }