Changeset 3357930
- Timestamp:
- 09/08/2025 01:17:06 PM (7 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
netscore-connector/tags/1.0.0/netscore-connector.php
r3357921 r3357930 10 10 */ 11 11 12 12 13 14 // Prevent direct access 13 15 if ( ! defined( 'ABSPATH' ) ) { 14 exit; // Exit if accessed directly16 exit; 15 17 } 16 18 19 // Add admin menu page 20 add_action( 'admin_menu', function() { 21 add_menu_page( 22 __( 'NetScore Connector', 'netscore-connector' ), 23 __( 'NetScore Connector', 'netscore-connector' ), 24 'manage_options', 25 'netscore-connector', 26 'netscore_connector_admin_page' 27 ); 28 } ); 29 17 30 /** 18 * Updated Netscore Connector main class - v1.0.3 19 * - Enqueues admin CSS from css/cuf-styles.css 20 * - Uses Settings API for saving options 21 * - Adds capability checks and nonce where relevant 31 * Render admin page and handle form submission 22 32 */ 23 24 class Netscore_Connector { 25 26 private $option_group = 'netscore_connector_group'; 27 28 public function __construct() { 29 add_action( 'admin_menu', array( $this, 'add_admin_menu' ) ); 30 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_css' ) ); 31 add_action( 'admin_init', array( $this, 'register_settings' ) ); 33 function netscore_connector_admin_page() { 34 // Form submission handling 35 if ( isset( $_POST['netscore_connector_submit'] ) ) { 36 37 // Step 1: Safely retrieve and sanitize nonce 38 $nonce = isset( $_POST['netscore_connector_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['netscore_connector_nonce'] ) ) : ''; 39 40 // Step 2: Verify nonce immediately 41 if ( ! wp_verify_nonce( $nonce, 'netscore_connector_action' ) ) { 42 echo '<div class="notice notice-error"><p>' . esc_html__( 'Nonce verification failed.', 'netscore-connector' ) . '</p></div>'; 43 return; // Stop processing if nonce invalid 44 } 45 46 // Step 3: Sanitize all form inputs 47 $name = isset( $_POST['netscore_connector_name'] ) ? sanitize_text_field( wp_unslash( $_POST['netscore_connector_name'] ) ) : ''; 48 $email = isset( $_POST['netscore_connector_email'] ) ? sanitize_email( wp_unslash( $_POST['netscore_connector_email'] ) ) : ''; 49 $comments = isset( $_POST['netscore_connector_comments'] ) ? sanitize_textarea_field( wp_unslash( $_POST['netscore_connector_comments'] ) ) : ''; 50 51 // Step 4: Validate email 52 if ( ! is_email( $email ) ) { 53 echo '<div class="notice notice-error"><p>' . esc_html__( 'Invalid email address.', 'netscore-connector' ) . '</p></div>'; 54 } else { 55 // Step 5: Send email to admin 56 wp_mail( 57 get_option( 'admin_email' ), 58 __( 'New NetScore Connector Submission', 'netscore-connector' ), 59 "Name: $name\nEmail: $email\nComments:\n$comments" 60 ); 61 62 echo '<div class="notice notice-success"><p>' . esc_html__( 'Form submitted successfully!', 'netscore-connector' ) . '</p></div>'; 63 } 32 64 } 33 34 public function add_admin_menu() { 35 add_menu_page( 36 __( 'Netscore Connector', 'netscore-connector' ), 37 __( 'Netscore Connector', 'netscore-connector' ), 38 'manage_options', 39 'netscore-connector', 40 array( $this, 'settings_page' ), 41 'dashicons-networking', 42 56 43 ); 65 66 // Step 6: Display the form 67 ?> 68 <div class="wrap"> 69 <h1><?php esc_html_e( 'NetScore Connector Form', 'netscore-connector' ); ?></h1> 70 <form method="post" action=""> 71 <?php wp_nonce_field( 'netscore_connector_action', 'netscore_connector_nonce' ); ?> 72 <table class="form-table"> 73 <tr> 74 <th><label for="netscore_connector_name"><?php esc_html_e( 'Name', 'netscore-connector' ); ?></label></th> 75 <td><input type="text" id="netscore_connector_name" name="netscore_connector_name" class="regular-text" required></td> 76 </tr> 77 <tr> 78 <th><label for="netscore_connector_email"><?php esc_html_e( 'Email', 'netscore-connector' ); ?></label></th> 79 <td><input type="email" id="netscore_connector_email" name="netscore_connector_email" class="regular-text" required></td> 80 </tr> 81 <tr> 82 <th><label for="netscore_connector_comments"><?php esc_html_e( 'Comments', 'netscore-connector' ); ?></label></th> 83 <td><textarea id="netscore_connector_comments" name="netscore_connector_comments" class="large-text" rows="5"></textarea></td> 84 </tr> 85 </table> 86 <p> 87 <input type="submit" name="netscore_connector_submit" class="button button-primary" value="<?php esc_attr_e( 'Submit', 'netscore-connector' ); ?>"> 88 </p> 89 </form> 90 </div> 91 <?php 92 } 93 94 // Enqueue CSS for admin form page 95 function cuf_enqueue_styles( $hook ) { 96 // Only load CSS on the NetScore Connector page 97 if ( $hook !== 'toplevel_page_netscore-connector' ) { 98 return; 44 99 } 45 46 public function enqueue_admin_css( $hook ) { 47 if ( $hook !== 'toplevel_page_netscore-connector' ) { 48 return; 49 } 50 wp_enqueue_style( 51 'netscore-connector-admin', 52 plugin_dir_url( __FILE__ ) . 'css/cuf-styles.css', 53 array(), 54 '1.0.3' 55 ); 56 } 57 58 public function register_settings() { 59 register_setting( $this->option_group, 'netscore_api_key', array( 60 'type' => 'string', 61 'sanitize_callback' => 'sanitize_text_field', 62 'default' => '' 63 ) ); 64 register_setting( $this->option_group, 'netscore_api_email', array( 65 'type' => 'string', 66 'sanitize_callback' => 'sanitize_email', 67 'default' => '' 68 ) ); 69 } 70 71 public function settings_page() { 72 if ( ! current_user_can( 'manage_options' ) ) { 73 return; 74 } 75 76 if ( isset( $_GET['settings-updated'] ) ) { 77 add_settings_error( 'netscore_messages', 'netscore_message', __( 'Settings Saved', 'netscore-connector' ), 'updated' ); 78 } 79 settings_errors( 'netscore_messages' ); 80 81 $api_key = get_option( 'netscore_api_key', '' ); 82 $api_email = get_option( 'netscore_api_email', '' ); 83 ?> 84 <div class="wrap"> 85 <div class="netscore-header"> 86 <h1><?php esc_html_e( 'Netscore Connector Settings', 'netscore-connector' ); ?></h1> 87 </div> 88 89 <form class="netscore-connector-form" method="post" action="options.php" novalidate> 90 <?php 91 settings_fields( $this->option_group ); 92 do_settings_sections( $this->option_group ); 93 ?> 94 95 <table class="form-table"> 96 <tr> 97 <th scope="row"><label for="netscore_api_key"><?php esc_html_e( 'API Key', 'netscore-connector' ); ?></label></th> 98 <td> 99 <input name="netscore_api_key" type="text" id="netscore_api_key" value="<?php echo esc_attr( $api_key ); ?>" class="regular-text" /> 100 <p class="description"><?php esc_html_e( 'Your NetSuite API key or token.', 'netscore-connector' ); ?></p> 101 </td> 102 </tr> 103 104 <tr> 105 <th scope="row"><label for="netscore_api_email"><?php esc_html_e( 'Contact Email', 'netscore-connector' ); ?></label></th> 106 <td> 107 <input name="netscore_api_email" type="email" id="netscore_api_email" value="<?php echo esc_attr( $api_email ); ?>" class="regular-text" /> 108 <p class="description"><?php esc_html_e( 'Email used for API account or license notifications.', 'netscore-connector' ); ?></p> 109 </td> 110 </tr> 111 </table> 112 113 <?php submit_button( __( 'Save Settings', 'netscore-connector' ) ); ?> 114 </form> 115 </div> 116 <?php 117 } 100 101 wp_enqueue_style( 102 'cuf-styles', 103 plugin_dir_url(__FILE__) . 'css/cuf-styles.css', 104 array(), 105 '1.0.0' 106 ); 118 107 } 119 120 // Initialize plugin 121 new Netscore_Connector(); 108 add_action('admin_enqueue_scripts', 'cuf_enqueue_styles'); 109 110
Note: See TracChangeset
for help on using the changeset viewer.