init_hooks(); } /** * Initialize WordPress hooks */ private function init_hooks() { // AJAX handlers for profile sharing add_action('wp_ajax_hvac_get_profile_share_data', [$this, 'ajax_get_profile_share_data']); add_action('wp_ajax_nopriv_hvac_get_profile_share_data', [$this, 'ajax_get_profile_share_data']); // URL rewrite rules for direct profile access add_action('init', [$this, 'add_profile_rewrite_rules']); add_filter('query_vars', [$this, 'add_profile_query_vars']); } /** * Generate QR code URL using QR Server API * * @param string $data Data to encode in QR code * @param int $size QR code size in pixels (default: 200) * @param string $error_correction Error correction level (L, M, Q, H) * @return string QR code image URL */ public function generate_qr_url($data, $size = 200, $error_correction = 'M') { // Encode the data for URL $encoded_data = urlencode($data); // QR Server API URL (free and reliable alternative to Google Charts) $qr_url = sprintf( 'https://api.qrserver.com/v1/create-qr-code/?size=%dx%d&data=%s&ecc=%s', $size, $size, $encoded_data, strtoupper($error_correction) ); return $qr_url; } /** * Generate QR code for trainer profile * * @param int $profile_id Trainer profile ID * @param int $size QR code size in pixels * @return string|false QR code URL or false on error */ public function generate_trainer_profile_qr($profile_id, $size = 200) { if (!$profile_id) { return false; } // Generate the profile URL $profile_url = $this->get_trainer_profile_share_url($profile_id); if (!$profile_url) { return false; } return $this->generate_qr_url($profile_url, $size); } /** * Get shareable trainer profile URL * * @param int $profile_id Trainer profile ID * @return string|false Profile URL or false on error */ public function get_trainer_profile_share_url($profile_id) { if (!$profile_id) { return false; } // Get the Find a Trainer page URL $find_trainer_page = get_page_by_path('find-a-trainer'); if (!$find_trainer_page) { return false; } $base_url = get_permalink($find_trainer_page->ID); // Create the profile-specific URL (with trailing slash for WordPress rewrite rules) $profile_url = trailingslashit($base_url) . 'profile/' . $profile_id . '/'; return $profile_url; } /** * Get trainer profile data for sharing * * @param int $profile_id Trainer profile ID * @return array|false Profile data or false on error */ public function get_trainer_share_data($profile_id) { if (!$profile_id) { return false; } // Get the profile post $profile = get_post($profile_id); if (!$profile || $profile->post_type !== 'trainer_profile') { return false; } // Get profile metadata $user_id = get_post_meta($profile_id, 'user_id', true); if (!$user_id) { return false; } // Get user data $user = get_userdata($user_id); if (!$user) { return false; } // Compile share data $share_data = [ 'profile_id' => $profile_id, 'user_id' => $user_id, 'trainer_name' => get_post_meta($profile_id, 'trainer_display_name', true) ?: $user->display_name, 'business_name' => get_user_meta($user_id, 'business_name', true), 'trainer_city' => get_post_meta($profile_id, 'trainer_city', true), 'trainer_state' => get_post_meta($profile_id, 'trainer_state', true), 'certification_type' => get_post_meta($profile_id, 'certification_type', true), 'profile_image' => get_post_meta($profile_id, 'profile_image_url', true), 'share_url' => $this->get_trainer_profile_share_url($profile_id), 'qr_code_url' => $this->generate_trainer_profile_qr($profile_id, 200) ]; return $share_data; } /** * Parse trainer profile ID from URL * * @param string $url URL to parse * @return int|false Profile ID or false if not found */ public function parse_profile_id_from_url($url = null) { // First check if we have a query variable (from rewrite rule) $profile_id = get_query_var('trainer_profile_id'); if ($profile_id) { return intval($profile_id); } // Fallback to URL parsing if (!$url) { $url = $_SERVER['REQUEST_URI']; } // Check if URL matches pattern: /find-a-trainer/profile/{profile_id} if (preg_match('/\/find-a-trainer\/profile\/(\d+)\/?/', $url, $matches)) { return intval($matches[1]); } return false; } /** * Generate profile card HTML for sharing * * @param int $profile_id Trainer profile ID * @param array $options Display options * @return string|false HTML content or false on error */ public function generate_profile_card_html($profile_id, $options = []) { $share_data = $this->get_trainer_share_data($profile_id); if (!$share_data) { return false; } // Default options $options = wp_parse_args($options, [ 'show_qr' => true, 'qr_size' => 150, 'card_width' => 600, 'card_height' => 300 ]); $qr_url = $options['show_qr'] ? $this->generate_trainer_profile_qr($profile_id, $options['qr_size']) : ''; ob_start(); ?>
'Security check failed']); return; } $profile_id = intval($_POST['profile_id']); if (!$profile_id) { wp_send_json_error(['message' => 'Invalid profile ID']); return; } // Get the share data $share_data = $this->get_trainer_share_data($profile_id); if (!$share_data) { wp_send_json_error(['message' => 'Profile not found or not accessible']); return; } wp_send_json_success($share_data); } /** * Add rewrite rules for direct profile access */ public function add_profile_rewrite_rules() { // Add rewrite rule for /find-a-trainer/profile/{profile_id} add_rewrite_rule( '^find-a-trainer/profile/([0-9]+)/?$', 'index.php?pagename=find-a-trainer&trainer_profile_id=$matches[1]', 'top' ); } /** * Add custom query variables */ public function add_profile_query_vars($vars) { $vars[] = 'trainer_profile_id'; return $vars; } } // Initialize HVAC_QR_Generator::instance();