Acknowledgify. * It includes a tabbed interface for General, humans.txt, Meta Tags, and Footer Link settings. * * @package Acknowledgify */ // Define a namespace for the Admin_settings namespace Acknowledgify\Admin; use Acknowledgify\Includes\Helper; // Exit if accessed directly. if ( ! defined( 'ABSPATH' ) ) { exit; } // Load constants require_once plugin_dir_path( __FILE__ ) . '../includes/constants.php'; class Settings { /** * Renders the Acknowledgify Settings Page. */ public static function render_settings_page() { // Retrieve saved settings. $options = get_option( 'acknowledgify_settings' ); ?>
__( 'Choose a color', 'acknowledgify' ), 'invalid_color' => __( 'Invalid color selected. Please try again or refresh the page.', 'acknowledgify' ), ] ); // Enqueue the admin settings script wp_enqueue_script( 'admin-settings-script', $plugin_url . 'js/admin-settings.js', [ 'jquery' ], $plugin_version, // Explicit version number true ); // Pass the PHP constants to JavaScript wp_localize_script( 'admin-settings-script', 'acknowledgify_vars', array( // Admin URL 'admin_url' => admin_url('admin-post.php'), // Add humans.txt header text 'HEADER_FIXED' => ACKNOWLEDGIFY_HUMANS_TXT_HEADER, // Add freelancer specific labels 'FREELANCER_HEADER' => ACKNOWLEDGIFY_SECTION_FREELANCER, 'FREELANCER_LABEL' => ACKNOWLEDGIFY_LABEL_FREELANCER_NAME, 'WEBSITE_LABEL' => ACKNOWLEDGIFY_LABEL_FREELANCER_WEBSITE, 'EMAIL_LABEL' => ACKNOWLEDGIFY_LABEL_FREELANCER_EMAIL, 'LOCATION_LABEL' => ACKNOWLEDGIFY_LABEL_FREELANCER_LOCATION, 'PROJECT_CREDITS_HEADER' => ACKNOWLEDGIFY_SECTION_PROJECT_CREDITS, 'PROJECT_LABEL' => ACKNOWLEDGIFY_LABEL_FREELANCER_PROJECT, 'ROLE_LABEL' => ACKNOWLEDGIFY_LABEL_FREELANCER_ROLE, 'THANKS_TO_LABEL' => ACKNOWLEDGIFY_LABEL_FREELANCER_THANKS_TO, // Add agency specific labels 'AGENCY_HEADER' => ACKNOWLEDGIFY_SECTION_TEAM, 'AGENCY_LABEL' => ACKNOWLEDGIFY_LABEL_AGENCY_NAME, 'AGENCY_WEBSITE_LABEL' => ACKNOWLEDGIFY_LABEL_AGENCY_WEBSITE, 'AGENCY_CONTACT_LABEL' => ACKNOWLEDGIFY_LABEL_AGENCY_CONTACT, 'AGENCY_LOCATION_LABEL' => ACKNOWLEDGIFY_LABEL_AGENCY_LOCATION, 'AGENCY_PROJECT_LABEL' => ACKNOWLEDGIFY_LABEL_AGENCY_PROJECT, 'AGENCY_DESIGN_LABEL' => ACKNOWLEDGIFY_LABEL_AGENCY_DESIGN, 'AGENCY_DEVELOPMENT_LABEL' => ACKNOWLEDGIFY_LABEL_AGENCY_DEVELOPMENT, 'AGENCY_SEO_LABEL' => ACKNOWLEDGIFY_LABEL_AGENCY_SEO, 'AGENCY_HOSTING_LABEL' => ACKNOWLEDGIFY_LABEL_AGENCY_HOSTING, 'AGENCY_THANKS_LABEL' => ACKNOWLEDGIFY_LABEL_AGENCY_THANKS, // Alert Message 'MSG_SELECT_FILE' => __('Please select a JSON file to import.', 'acknowledgify'), 'MSG_UPLOAD_VALID_JSON' => __('Please upload a valid JSON file.', 'acknowledgify'), 'MSG_IMPORT_SUCCESS' => __('Import successful!', 'acknowledgify'), 'MSG_IMPORT_FAILED' => __('Import failed. Check the console for details.', 'acknowledgify'), 'MSG_INVALID_JSON' => __('Invalid JSON file. Please check the file format.', 'acknowledgify'), )); } /** * Validate and sanitize settings before saving. */ public static function validate_and_save_settings( $input ) { // Ensure input is an array to prevent errors if ( ! is_array( $input ) ) { return []; } // Verify nonce before processing (only if the request is from an admin page) if ( isset( $_POST['acknowledgify_nonce'] ) ) { $nonce = sanitize_text_field(wp_unslash( $_POST['acknowledgify_nonce'] )); if ( ! wp_verify_nonce( $nonce, 'acknowledgify_settings_nonce' ) ) { wp_die( esc_html__( 'Security check failed.', 'acknowledgify' ) ); } } // Sanitize settings $new_input = array(); // General Settings. $new_input['enable_humans_txt'] = isset( $input['enable_humans_txt'] ) ? $input['enable_humans_txt'] : 0; $new_input['enable_meta_tags'] = isset( $input['enable_meta_tags'] ) ? $input['enable_meta_tags'] : 0; $new_input['enable_footer_link'] = isset( $input['enable_footer_link'] ) ? $input['enable_footer_link'] : 0; // humans.txt Settings. $new_input['humans_template'] = sanitize_text_field( $input['humans_template'] ?? 'freelancer' ); $new_input['humans_custom_content'] = wp_kses_post( $input['humans_custom_content'] ?? '' ); // Freelancer Template Fields. $new_input['freelancer_name'] = sanitize_text_field( $input['freelancer_name'] ?? '' ); $new_input['freelancer_website'] = esc_url_raw( $input['freelancer_website'] ?? '' ); $new_input['freelancer_email'] = sanitize_email( $input['freelancer_email'] ?? '' ); $new_input['freelancer_location'] = sanitize_text_field( $input['freelancer_location'] ?? '' ); $new_input['freelancer_project'] = sanitize_text_field( $input['freelancer_project'] ?? '' ); $new_input['freelancer_role'] = sanitize_text_field( $input['freelancer_role'] ?? '' ); $new_input['freelancer_thanks_to'] = sanitize_text_field( $input['freelancer_thanks_to'] ?? '' ); // Agency Template Fields. $new_input['agency_name'] = sanitize_text_field( $input['agency_name'] ?? '' ); $new_input['agency_website'] = esc_url_raw( $input['agency_website'] ?? '' ); $new_input['agency_contact'] = sanitize_email( $input['agency_contact'] ?? '' ); $new_input['agency_location'] = sanitize_text_field( $input['agency_location'] ?? '' ); $new_input['agency_project'] = sanitize_text_field( $input['agency_project'] ?? '' ); $new_input['agency_design'] = sanitize_text_field( $input['agency_design'] ?? '' ); $new_input['agency_development'] = sanitize_text_field( $input['agency_development'] ?? '' ); $new_input['agency_seo'] = sanitize_text_field( $input['agency_seo'] ?? '' ); $new_input['agency_hosting'] = sanitize_text_field( $input['agency_hosting'] ?? '' ); $new_input['agency_thanks'] = sanitize_text_field( $input['agency_thanks'] ?? '' ); // Meta Tags Settings. $new_input['meta_author'] = sanitize_text_field( $input['meta_author'] ?? '' ); $new_input['meta_designer'] = sanitize_text_field( $input['meta_designer'] ?? '' ); $new_input['meta_developer'] = sanitize_text_field( $input['meta_developer'] ?? '' ); $new_input['meta_copyright'] = sanitize_text_field( $input['meta_copyright'] ?? '' ); $new_input['meta_owner'] = sanitize_text_field( $input['meta_owner'] ?? '' ); $new_input['meta_publisher'] = sanitize_text_field( $input['meta_publisher'] ?? '' ); $new_input['meta_manage'] = sanitize_text_field( $input['meta_manage'] ?? '' ); $new_input['meta_power'] = sanitize_text_field( $input['meta_power'] ?? '' ); $new_input['meta_acknowledgment'] = sanitize_text_field( $input['meta_acknowledgment'] ?? '' ); // Footer Link Settings. $new_input['footer_link_text'] = sanitize_text_field( $input['footer_link_text'] ?? 'Made by Acknowledgify' ); $new_input['footer_link_url'] = esc_url_raw( $input['footer_link_url'] ?? 'https://youragency.com' ); $new_input['footer_text_color'] = sanitize_hex_color( $input['footer_text_color'] ?? '#000000' ); $new_input['footer_font_size'] = sanitize_text_field( $input['footer_font_size'] ?? '16px' ); $new_input['footer_custom_class'] = sanitize_text_field( $input['footer_custom_class'] ?? '' ); $new_input['footer_placement_options'] = sanitize_text_field( $input['footer_placement_options'] ?? 'auto_insert' ); return $new_input; } public static function acknowledgify_export_settings() { // Prevent output buffering issues if (ob_get_length()) { ob_end_clean(); } // Retrieve plugin settings $settings = get_option('acknowledgify_settings', array()); // Convert settings to JSON $json_data = json_encode($settings, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); if ($json_data === false) { wp_die('Error exporting settings.'); } // Set headers for JSON file header('Content-Type: application/json'); header('Content-Disposition: attachment; filename="acknowledgify-settings.json"'); header('Content-Length: ' . strlen($json_data)); header('Pragma: no-cache'); header('Expires: 0'); // Send JSON response // Ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo wp_json_encode($settings, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); exit; } public static function acknowledgify_import_settings() { // Verify nonce before processing (only if the request is from an admin page) if (isset($_POST['acknowledgify_nonce'])) { $nonce = sanitize_text_field(wp_unslash($_POST['acknowledgify_nonce'])); if (!wp_verify_nonce($nonce, 'acknowledgify_settings_nonce')) { wp_die(esc_html__('Security check failed.', 'acknowledgify')); } } // Check if 'settings' exists and is not empty if (isset($_POST['settings']) && !empty($_POST['settings'])) { // Sanitize at the point of assignment (to avoid PHPCS error) $raw_settings = sanitize_textarea_field(wp_unslash($_POST['settings'])); // Decode the JSON string $settings = json_decode($raw_settings, true); // Validate JSON format if (json_last_error() === JSON_ERROR_NONE && is_array($settings)) { // Sanitize each setting deeply $sanitized_array = array_map(function($item) { return is_array($item) ? array_map('sanitize_text_field', $item) : sanitize_text_field($item); }, $settings); // Update WordPress option update_option('acknowledgify_settings', $sanitized_array); // Return success response wp_send_json_success([ 'message' => 'Settings imported and saved successfully.' ]); } else { // Invalid JSON format wp_send_json_error('Invalid JSON format. Please check your file.'); } } else { // No settings provided wp_send_json_error('No settings provided.'); } } }