Siteβ―Builder
Editing:
rfsafe-generate-phone-cache.php
writable 0666
<?php /* Plugin Name: RF Safe generate and update the cache Description: A plugin Function to generate and update the cache file. Version: 1.0 Author: Your Name */ // Function to generate and update the cache file function sar2_generate_cache() { global $wpdb; $table = $wpdb->prefix . 'sar_data'; $results = $wpdb->get_results("SELECT * FROM $table", ARRAY_A); foreach ($results as &$phone) { $pid = $phone['post_id']; $phone['thumbnail_url'] = get_the_post_thumbnail_url($pid, 'thumbnail'); $product_pid = $phone['network']; $product = wc_get_product($product_pid); if ($product) { $regular_price = $product->get_regular_price(); $sale_price = $product->get_sale_price(); $phone['product_price'] = ($regular_price ? $regular_price : '69.99') . '|' . ($sale_price ? $sale_price : 'Check Price'); $phone['product_title'] = $product->get_name(); $phone['product_permalink'] = $product->get_permalink(); } else { $phone['product_price'] = '69.99|Check Price'; $phone['product_title'] = 'QuantaCase'; $phone['product_permalink'] = 'https://www.rfsafe.com/best-phone-case-for-smartphone-radiation-protection/'; } $phonesize2 = get_post_meta($pid, 'aps-attr-group2', true); $phone['dimensions'] = isset($phonesize2['dimensions']) ? $phonesize2['dimensions'] : 'Unknown size'; $phonesize = get_post_meta($pid, 'aps-attr-group4', true); $phone['screensize'] = isset($phonesize['display-size']) ? strtok($phonesize['display-size'], ',') : 'Unknown size'; } $cache_dir = '/home/rfsafe/public_html/cache/'; if (!file_exists($cache_dir)) { mkdir($cache_dir, 0755, true); } $cache_file = $cache_dir . 'sar2_cache.json'; file_put_contents($cache_file, json_encode($results)); // Log cache generation if (file_exists($cache_file)) { error_log('SAR Compare Plugin 2: Cache file created successfully.'); } else { error_log('SAR Compare Plugin 2: Failed to create cache file.'); } } // Shortcode to manually update the cache and download the JSON file function sar2_cache_update_shortcode() { ob_start(); ?> <style> .sar2-cache-container { text-align: center; margin-top: 20px; } .sar2-cache-container button { padding: 10px 20px; margin: 10px; background-color: #0073aa; color: #ffffff; border: none; cursor: pointer; } .sar2-cache-container button:hover { background-color: #005177; } </style> <div class="sar2-cache-container"> <button id="sar2-update-cache">Update Cache</button> <button id="sar2-download-cache" style="display:none;">Download JSON</button> </div> <script> jQuery(document).ready(function($) { $("#sar2-update-cache").click(function() { $.post( '<?php echo admin_url('admin-ajax.php'); ?>', { action: 'sar2_update_cache' }, function(response) { if (response.success) { alert("Cache updated successfully."); $("#sar2-download-cache").show(); } else { alert("Cache update failed."); } } ); }); $("#sar2-download-cache").click(function() { window.location.href = '<?php echo site_url('/?download=sar2_cache'); ?>'; }); }); </script> <?php return ob_get_clean(); } add_shortcode('sar2_cache_update', 'sar2_cache_update_shortcode'); // AJAX handler to update the cache function sar2_update_cache() { sar2_generate_cache(); wp_send_json_success(); } add_action('wp_ajax_sar2_update_cache', 'sar2_update_cache'); add_action('wp_ajax_nopriv_sar2_update_cache', 'sar2_update_cache'); // PHP script to download the JSON file function sar2_download_cache_script() { if (isset($_GET['download']) && $_GET['download'] == 'sar2_cache') { $cache_file = '/home/rfsafe/public_html/cache/sar2_cache.json'; if (file_exists($cache_file)) { header('Content-Description: File Transfer'); header('Content-Type: application/json'); header('Content-Disposition: attachment; filename="sar2_cache.json"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($cache_file)); readfile($cache_file); exit; } else { echo "Cache file not found."; } } } add_action('init', 'sar2_download_cache_script'); // Function to generate phone folders and index.html files function create_phone_folders() { $cache_file = '/home/rfsafe/public_html/cache/sar2_cache.json'; $phones_dir = '/home/rfsafe/public_html/phones/'; if (file_exists($cache_file)) { $data = file_get_contents($cache_file); $phones = json_decode($data, true); foreach ($phones as $phone) { $brand = strtolower(str_replace(' ', '-', $phone['brand'])); $model = strtolower(str_replace(' ', '-', $phone['model'])); $folder_name = $brand . '-' . $model; $phone_folder = $phones_dir . $folder_name; if (!file_exists($phone_folder)) { mkdir($phone_folder, 0755, true); } $index_file = $phone_folder . '/index.html'; $placeholder_text = "{$phone['brand']} {$phone['model']} Phone Specs Coming Soon"; if (!file_exists($index_file)) { file_put_contents($index_file, $placeholder_text); } } return "Phone folders and index.html files created successfully."; } else { return "Cache file not found."; } } // Shortcode to trigger the folder creation function phone_folders_shortcode() { ob_start(); ?> <style> .phone-folder-container { text-align: center; margin-top: 20px; } .phone-folder-container button { padding: 10px 20px; margin: 10px; background-color: #0073aa; color: #ffffff; border: none; cursor: pointer; } .phone-folder-container button:hover { background-color: #005177; } </style> <div class="phone-folder-container"> <button id="create-phone-folders">Create Phone Folders</button> <div id="folder-creation-result" style="margin-top: 10px; font-weight: bold;"></div> </div> <script> jQuery(document).ready(function($) { $("#create-phone-folders").click(function() { $.post( '<?php echo admin_url('admin-ajax.php'); ?>', { action: 'create_phone_folders' }, function(response) { $("#folder-creation-result").text(response.data); } ); }); }); </script> <?php return ob_get_clean(); } add_shortcode('create_phone_folders', 'phone_folders_shortcode'); // AJAX handler to create the phone folders function create_phone_folders_ajax() { $result = create_phone_folders(); wp_send_json_success($result); } add_action('wp_ajax_create_phone_folders', 'create_phone_folders_ajax'); add_action('wp_ajax_nopriv_create_phone_folders', 'create_phone_folders_ajax'); function generate_phone_specs_json() { $cache_file_path = '/home/rfsafe/public_html/cache/sar2_cache.json'; $phones_dir = '/home/rfsafe/public_html/phones/'; if (!file_exists($cache_file_path)) { echo '<p>Cache file not found.</p>'; return; } $phone_data = json_decode(file_get_contents($cache_file_path), true); if (json_last_error() !== JSON_ERROR_NONE) { echo '<p>Error decoding JSON cache file.</p>'; return; } // Retrieve the groups and attributes $groups = get_aps_groups(); $attributes = get_aps_attributes(); foreach ($phone_data as $phone) { $brand = strtolower($phone['brand']); $model = strtolower($phone['model']); $slug = $brand . '-' . str_replace(' ', '-', $model); $phone_dir = $phones_dir . $slug; if (!file_exists($phone_dir)) { mkdir($phone_dir, 0755, true); } $specs = []; foreach ($groups as $key => $group) { if ((strpos(strtolower($group['name']), 'sar') === false || strpos(strtolower($group['name']), 'fcc') !== false) && $group['display'] == 'yes') { foreach ($attributes[$key] as $attr_key => $attr) { $specs[$group['name']][$attr['name']] = get_aps_product_attributes($phone['post_id'], $key)[$attr_key] ?? ''; } } } // Add additional fields under "Phone" group $specs['Phone'] = [ 'thumbnail_url' => $phone['thumbnail_url'], 'product_price' => $phone['product_price'], 'product_title' => $phone['product_title'], 'product_permalink' => $phone['product_permalink'], 'dimensions' => $phone['dimensions'], 'screensize' => $phone['screensize'], 'post_id' => $phone['post_id'], 'network' => $phone['network'], 'brand' => $phone['brand'], 'model' => $phone['model'], 'fccid' => $phone['fccid'], 'fccurl' => $phone['fccurl'], 'rfsafe_phone_score' => $phone['rfsafe_phone_score'], 'post_name' => $phone['post_name'], 'releasedate' => $phone['releasedate'], 'ushead' => $phone['ushead'], 'usbody' => $phone['usbody'], 'euhead' => $phone['euhead'], 'eubody' => $phone['eubody'], 'productspecific' => $phone['productspecific'], 'simulheadus' => $phone['simulheadus'], 'simulbodyus' => $phone['simulbodyus'], 'hotspot' => $phone['hotspot'], 'sarrank' => $phone['sarrank'], 'countrysold' => $phone['countrysold'], 'ushead_rank' => rtrim($phone['ushead_rank'], '.00'), 'ushead_percent' => $phone['ushead_percent'], 'usbody_rank' => rtrim($phone['usbody_rank'], '.00'), 'usbody_percent' => $phone['usbody_percent'], 'simulheadus_rank' => rtrim($phone['simulheadus_rank'], '.00'), 'simulheadus_percent' => $phone['simulheadus_percent'], 'simulbodyus_rank' => rtrim($phone['simulbodyus_rank'], '.00'), 'simulbodyus_percent' => $phone['simulbodyus_percent'], 'simulhotspot_rank' => rtrim($phone['simulhotspot_rank'], '.00'), 'simulhotspot_percent' => $phone['simulhotspot_percent'], 'productspecific_rank' => rtrim($phone['productspecific_rank'], '.00'), 'productspecific_percent' => $phone['productspecific_percent'] ]; // Search for the matching FCC ID in the cache file $fcc_id = $phone['fccid']; $matching_phone = array_filter($phone_data, function($item) use ($fcc_id) { return $item['fccid'] === $fcc_id; }); if (!empty($matching_phone)) { $matching_phone = reset($matching_phone); // Merge the matching phone data into the specs array foreach ($matching_phone as $key => $value) { if ($key !== 'Phone') { $specs[$key] = $value; } else { foreach ($value as $phone_key => $phone_value) { $specs['Phone'][$phone_key] = $phone_value; } } } } // Remove any keys outside the 'Phone' group foreach (array_keys($phone) as $key) { if ($key !== 'Phone' && $key !== 'FCC SAR Levels') { unset($specs[$key]); } } $json_file = $phone_dir . '/' . $slug . '_cache.json'; $result = file_put_contents($json_file, json_encode($specs, JSON_PRETTY_PRINT)); if ($result === false) { echo "<p>Error writing JSON file for $slug.</p>"; } } echo '<p>JSON files generated successfully.</p>'; } function generate_phone_specs_button_shortcode() { ob_start(); ?> <div id="generate-phone-specs-container"> <button id="generate-phone-specs-button">Generate Phone Specs JSON</button> <div id="generate-phone-specs-message"></div> </div> <script> document.addEventListener('DOMContentLoaded', function() { const button = document.getElementById('generate-phone-specs-button'); const messageDiv = document.getElementById('generate-phone-specs-message'); button.addEventListener('click', function() { if (confirm('This operation will generate JSON files for all phones and cannot be undone. Do you want to proceed?')) { messageDiv.innerHTML = '<p>Generating JSON files, please wait...</p>'; fetch('<?php echo admin_url('admin-ajax.php'); ?>', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: 'action=run_generate_phone_specs' }) .then(response => response.text()) .then(data => { messageDiv.innerHTML = data; }) .catch(error => { messageDiv.innerHTML = '<p>Error: ' + error.message + '</p>'; }); } }); }); </script> <?php return ob_get_clean(); } add_shortcode('generate_phone_specs_button', 'generate_phone_specs_button_shortcode'); function run_generate_phone_specs() { generate_phone_specs_json(); wp_die(); } add_action('wp_ajax_run_generate_phone_specs', 'run_generate_phone_specs'); add_action('wp_ajax_nopriv_run_generate_phone_specs', 'run_generate_phone_specs'); function update_phone_index_files() { // Ensure the function only runs when the button is clicked if (!isset($_POST['update_index_files']) || !isset($_POST['update_file'])) { return; } $phones_dir = '/home/rfsafe/public_html/phones/'; $update_files = [ 'index-update.html' => 'index.html', 'quantacase-update.html' => 'quantacase-emf-blocking-anti-radiation-phone-case-rfid-protection.html', 'wifi-off-tip-update.html' => 'phone-radiation-safety-information.html' ]; $selected_update_file = $_POST['update_file']; // Check if the selected update file is valid if (!array_key_exists($selected_update_file, $update_files)) { echo '<p>Invalid update file selected.</p>'; return; } $update_file_path = $phones_dir . $selected_update_file; $destination_file_name = $update_files[$selected_update_file]; // Check if the update file exists if (!file_exists($update_file_path)) { echo '<p>Update file not found.</p>'; return; } // Loop through each directory inside the phones directory foreach (new DirectoryIterator($phones_dir) as $fileInfo) { if ($fileInfo->isDot() || !$fileInfo->isDir()) { continue; } $phone_dir = $fileInfo->getPathname(); $destination_file_path = $phone_dir . '/' . $destination_file_name; $old_destination_file_path = $phone_dir . '/' . str_replace('.html', '.old.html', $destination_file_name); // Delete any existing old destination file if (file_exists($old_destination_file_path)) { unlink($old_destination_file_path); } // Rename the current destination file to destination.old.html if (file_exists($destination_file_path)) { rename($destination_file_path, $old_destination_file_path); } // Copy the update file to the phone directory as the destination file copy($update_file_path, $destination_file_path); } echo '<p>Files updated successfully.</p>'; } function render_update_button() { ob_start(); ?> <form method="post" onsubmit="return confirm('This action cannot be undone. Are you sure you want to update the selected files?');"> <label for="update_file">Select Update File:</label> <select id="update_file" name="update_file" required> <option value="">Select File</option> <option value="index-update.html">Index Update</option> <option value="quantacase-update.html">QuantaCase Update</option> <option value="wifi-off-tip-update.html">WiFi Off Tip Update</option> </select> <button type="submit" name="update_index_files">Update Files</button> </form> <?php return ob_get_clean(); } add_shortcode('update_phone_index_files', 'render_update_button'); add_action('init', 'update_phone_index_files'); // PHP function to handle the AJAX request function update_phone_index_files_ajax() { if (!isset($_POST['update_file'])) { wp_send_json_error(['message' => 'Invalid request.']); return; } $phones_dir = '/home/rfsafe/public_html/phones/'; $update_files = [ 'index-update.html' => 'index.html', 'quantacase-update.html' => 'quantacase-emf-blocking-anti-radiation-phone-case-rfid-protection.html', 'wifi-off-tip-update.html' => 'phone-radiation-safety-information.html', 'cell-phone-radiation-kids-update.html' => 'cell-phone-radiation-kids.html' ]; $selected_update_file = sanitize_text_field($_POST['update_file']); if (!array_key_exists($selected_update_file, $update_files)) { wp_send_json_error(['message' => 'Invalid update file selected.']); return; } $update_file_path = $phones_dir . $selected_update_file; $destination_file_name = $update_files[$selected_update_file]; if (!file_exists($update_file_path)) { wp_send_json_error(['message' => 'Update file not found.']); return; } foreach (new DirectoryIterator($phones_dir) as $fileInfo) { if ($fileInfo->isDot() || !$fileInfo->isDir()) { continue; } $phone_dir = $fileInfo->getPathname(); $destination_file_path = $phone_dir . '/' . $destination_file_name; $old_destination_file_path = $phone_dir . '/' . str_replace('.html', '.old.html', $destination_file_name); if (file_exists($old_destination_file_path)) { unlink($old_destination_file_path); } if (file_exists($destination_file_path)) { rename($destination_file_path, $old_destination_file_path); } copy($update_file_path, $destination_file_path); } wp_send_json_success(['message' => 'Files updated successfully.']); } add_action('wp_ajax_update_phone_index_files_ajax', 'update_phone_index_files_ajax'); add_action('wp_ajax_nopriv_update_phone_index_files_ajax', 'update_phone_index_files_ajax'); // HTML form to trigger the AJAX request function render_update_button_ajax() { ob_start(); ?> <form id="update-form"> <label for="update_file">Select Update File:</label> <select id="update_file" name="update_file" required> <option value="">Select File</option> <option value="index-update.html">Index Update</option> <option value="quantacase-update.html">QuantaCase Update</option> <option value="wifi-off-tip-update.html">WiFi Off Tip Update</option> <option value="cell-phone-radiation-kids-update.html">Cell Phone Radiation Kids Update</option> </select> <button id="update-button">Update Files</button> </form> <script> jQuery(document).ready(function($) { $('#update-button').on('click', function(e) { e.preventDefault(); var updateFile = $('#update_file').val(); if (updateFile === '') { alert('Please select a file to update.'); return; } if (!confirm('This action cannot be undone. Are you sure you want to update the selected files?')) { return; } $.ajax({ url: '<?php echo admin_url('admin-ajax.php'); ?>', type: 'POST', data: { action: 'update_phone_index_files_ajax', update_file: updateFile }, success: function(response) { alert(response.data.message); }, error: function() { alert('An error occurred while updating the files.'); } }); }); }); </script> <?php return ob_get_clean(); } add_shortcode('update_phone_index_files_ajax', 'render_update_button_ajax');
Save changes
Create folder
writable 0777
Create
Cancel