sprintf( /* translators: %d: Sticker number. */ __( 'Sticker %d', 'aidbadge-country-support-stickers' ), $index ), 'path' => plugins_url( 'images/sticker' . $index . '.png', __FILE__ ), ); } return $stickers; } /** * Get the available animations keyed by animation id. * * @return array Animation labels keyed by animation id. */ function aidbadge_get_animations() { return array( 'none' => __( 'No Animation', 'aidbadge-country-support-stickers' ), 'float' => __( 'Floating', 'aidbadge-country-support-stickers' ), 'pulse' => __( 'Pulse', 'aidbadge-country-support-stickers' ), 'shake' => __( 'Shake', 'aidbadge-country-support-stickers' ), 'spin' => __( 'Spin on Hover', 'aidbadge-country-support-stickers' ), ); } /** * Enqueue admin assets only on the plugin settings page. * * @param string $hook The current admin page hook suffix. */ function aidbadge_admin_assets( $hook ) { if ( 'toplevel_page_aidbadge-sticker-settings' !== $hook ) { return; } wp_enqueue_style( 'aidbadge-admin-style', plugins_url( 'css/sticker-style.css', __FILE__ ), array(), '1.0.0' ); wp_enqueue_script( 'aidbadge-admin-script', plugins_url( 'js/admin-script.js', __FILE__ ), array( 'jquery' ), '1.0.0', true ); } add_action( 'admin_enqueue_scripts', 'aidbadge_admin_assets' ); /** * Enqueue frontend assets on the front page only. */ function aidbadge_frontend_assets() { if ( ! is_front_page() ) { return; } wp_enqueue_style( 'aidbadge-frontend-style', plugins_url( 'css/frontend.css', __FILE__ ), array(), '1.0.0' ); $animation = get_option( 'aidbadge_selected_animation', 'float' ); if ( 'none' !== $animation ) { wp_add_inline_style( 'aidbadge-frontend-style', aidbadge_get_animation_css( $animation ) ); } } add_action( 'wp_enqueue_scripts', 'aidbadge_frontend_assets' ); /** * Get the inline CSS for a given animation. * * @param string $animation Animation key. * @return string Inline CSS rules. Empty string for unknown animations. */ function aidbadge_get_animation_css( $animation ) { $css = ''; switch ( $animation ) { case 'float': $css = '.aidbadge-sticker { animation: float 3s ease-in-out infinite; }'; break; case 'pulse': $css = '.aidbadge-sticker { animation: pulse 2s infinite; }'; break; case 'shake': $css = '.aidbadge-sticker { animation: shake 0.5s infinite; }'; break; case 'spin': $css = '.aidbadge-sticker:hover { animation: spin 2s linear infinite; }'; break; } return $css; } /** * Register the admin menu page. */ function aidbadge_add_admin_menu() { add_menu_page( __( 'Sticker Settings', 'aidbadge-country-support-stickers' ), __( 'AidBadge', 'aidbadge-country-support-stickers' ), 'manage_options', 'aidbadge-sticker-settings', 'aidbadge_render_admin_page', 'dashicons-format-image', 80 ); } add_action( 'admin_menu', 'aidbadge_add_admin_menu' ); /** * Render the admin settings page and handle (secure) form submission. */ function aidbadge_render_admin_page() { // Verify the user has the required capability. if ( ! current_user_can( 'manage_options' ) ) { return; } // Handle form submission. if ( isset( $_POST['aidbadge_save_settings'] ) ) { // Verify the nonce to protect against CSRF. check_admin_referer( 'aidbadge_save_settings', 'aidbadge_settings_nonce' ); // Sanitize and validate the selected sticker. $selected_sticker = isset( $_POST['selected_sticker'] ) ? sanitize_text_field( wp_unslash( $_POST['selected_sticker'] ) ) : ''; if ( ! in_array( $selected_sticker, aidbadge_get_sticker_keys(), true ) ) { $selected_sticker = 'sticker1'; } // Sanitize and validate the selected animation. $selected_animation = isset( $_POST['selected_animation'] ) ? sanitize_text_field( wp_unslash( $_POST['selected_animation'] ) ) : 'none'; if ( ! in_array( $selected_animation, aidbadge_get_animation_keys(), true ) ) { $selected_animation = 'none'; } update_option( 'aidbadge_selected_sticker', $selected_sticker ); update_option( 'aidbadge_selected_animation', $selected_animation ); echo '
' . esc_html__( 'Settings saved.', 'aidbadge-country-support-stickers' ) . '