效果图如下
该插件附带了一个可以把产品描述里面的超链接给去掉,以及有的产品图片点击会在地址栏上面显示图片的路径,在该插件可以进行关闭,并且替换成一个模态窗,还有对产品邮费展示进行了处理,到金额到达包邮的时候,别的邮费进行隐藏
下面是该插件源码
目录结构
duola
duola.php
scripts.js
styles.css
duola.php文件
<?php /* Plugin Name: 产品数据内容处理 Description: 产品标题以及描述中,如果有原本网站的数据,使用这个插件进行处理(新版本功能:增加了产品详情,给产品图片新增了一个模态窗,以及产品隐藏其他运费增加了一个快捷按钮和产品描述去超链接) Version: 2.3.4 Author: 朵啦 */ if (!defined('ABSPATH')) { exit; // 如果直接访问,退出 } class ContentReplacer { public function __construct() { // 添加后台菜单 add_action('admin_menu', array($this, 'add_admin_menu')); // 处理表单提交 add_action('admin_post_replace_content', array($this, 'handle_form_submission')); // 处理回滚 add_action('admin_post_rollback_content', array($this, 'handle_rollback')); // 处理免运费隐藏按钮的表单提交 add_action('admin_post_toggle_shipping', array($this, 'handle_toggle_shipping')); // 处理产品模态窗按钮的表单提交 add_action('admin_post_toggle_modal', array($this, 'handle_toggle_modal')); add_action('admin_post_toggle_hyperlink', array($this, 'handle_toggle_hyperlink')); add_action('admin_post_close_hyperlink', array($this, 'handle_close_hyperlink')); // 检查简码状态的AJAX处理 add_action('wp_ajax_check_shortcode_status', array($this, 'check_shortcode_status')); // 添加样式和脚本 add_action('admin_enqueue_scripts', array($this, 'enqueue_styles')); } // 添加后台菜单项 public function add_admin_menu() { add_menu_page( '产品数据内容处理', // 页面标题 '产品数据内容处理', // 菜单标题 'manage_options', // 权限 'content-replacer', // 菜单别名 array($this, 'create_admin_page'), // 页面内容的回调函数 'dashicons-admin-tools' // 图标 ); } public function enqueue_styles() { wp_enqueue_style('content-replacer-styles', plugins_url('styles.css', __FILE__)); wp_enqueue_script('content-replacer-scripts', plugins_url('scripts.js', __FILE__), array('jquery'), null, true); wp_add_inline_style('content-replacer-styles', ' #adminmenu .toplevel_page_content-replacer .wp-menu-image:before { content: ""; display: inline-block; width: 20px; height: 20px; background-image: url(' . plugins_url('img/icon.png', __FILE__) . '); background-size: cover; } '); } // 创建后台页面 public function create_admin_page() { ?> <div class="wrap content-replacer-container"> <h1>产品数据内容处理</h1> <form method="post" action="<?php echo admin_url('admin-post.php'); ?>" id="content-replacer-form"> <input type="hidden" name="action" value="replace_content"> <?php wp_nonce_field('replace_content_nonce', 'replace_content_nonce_field'); ?> <table class="form-table"> <tr valign="top"> <th scope="row"><label for="original_text">原始内容</label></th> <td><input type="text" id="original_text" name="original_text" required /></td> </tr> <tr valign="top"> <th scope="row"><label for="new_text">新内容</label></th> <td><input type="text" id="new_text" name="new_text" required /></td> </tr> </table> <p class="submit"><input type="submit" class="button-primary" value="替换内容" /></p> <div id="loading" class="loading">正在处理中,请稍候...</div> <div class="progress-bar" id="progress-bar"> <div class="progress-bar-inner" id="progress-bar-inner"></div> </div> </form> <h2>替换历史</h2> <ul> <?php $history = get_option('content_replacer_history', array()); if (empty($history)) { echo '<li>还没有进行过替换。</li>'; } else { $history_by_date = []; foreach ($history as $entry) { $date = date('Y-m-d H:i:s', strtotime($entry['date'])); if (!isset($history_by_date[$date])) { $history_by_date[$date] = ['entries' => [], 'count' => 0]; } $history_by_date[$date]['entries'][] = '"' . esc_html($entry['original_text']) . '" 替换为 "' . esc_html($entry['new_text']) . '"'; $history_by_date[$date]['count'] += $entry['replacements']; } foreach ($history_by_date as $date => $data) { $entries = array_unique($data['entries']); // 去除重复项 echo '<li>'; echo '于 ' . esc_html($date) . ' 替换: '; echo implode(', ', $entries); echo ' 共 ' . $data['count'] . ' 处改动 '; echo ' <a href="' . wp_nonce_url(admin_url('admin-post.php?action=rollback_content&date=' . urlencode($date)), 'rollback_content_nonce', 'rollback_content_nonce_field') . '">回滚</a>'; echo '</li>'; } } ?> </ul> </div> <div class="wrap content-replacer-container"> <!-- 新按钮:免运费隐藏 --> <form method="post" action="<?php echo admin_url('admin-post.php'); ?>" id="shipping-toggle-form"> <input type="hidden" name="action" value="toggle_shipping"> <?php wp_nonce_field('toggle_shipping_nonce', 'toggle_shipping_nonce_field'); ?> <p class="submit"><input type="submit" class="button-primary" id="toggle-shipping-button" value="免运费隐藏" /></p> </form> <!-- 新按钮:产品模态窗 --> <form method="post" action="<?php echo admin_url('admin-post.php'); ?>" id="modal-toggle-form"> <input type="hidden" name="action" value="toggle_modal"> <?php wp_nonce_field('toggle_modal_nonce', 'toggle_modal_nonce_field'); ?> <p class="submit"><input type="submit" class="button-primary" id="toggle-modal-button" value="产品模态窗" /></p> </form> <!-- 新按钮:去产品超链接 --> <form method="post" action="<?php echo admin_url('admin-post.php'); ?>" id="hyperlink-toggle-form"> <input type="hidden" name="action" value="toggle_hyperlink"> <?php wp_nonce_field('toggle_hyperlink_nonce', 'toggle_hyperlink_nonce_field'); ?> <p class="submit"><input type="submit" class="button-primary" id="toggle-hyperlink-button" value="检查中..." /></p> </form> <!-- 新按钮:关闭描述去超链接功能 --> <form method="post" action="<?php echo admin_url('admin-post.php'); ?>" id="hyperlink-close-form" style="display:none;"> <input type="hidden" name="action" value="close_hyperlink"> <?php wp_nonce_field('close_hyperlink_nonce', 'close_hyperlink_nonce_field'); ?> <p class="submit"><input type="submit" class="button-primary" id="close-hyperlink-button" value="关闭描述去超链接功能" /></p> </form> </div> <script> jQuery(document).ready(function($) { $('#content-replacer-form').on('submit', function() { $('#loading').hide(); $('#progress-bar').show(); var progressBar = $('#progress-bar-inner'); var width = 0; var interval = setInterval(function() { if (width >= 100) { clearInterval(interval); $('#progress-bar').hide(); } else { width += 1; // 增加进度的步长 progressBar.css('width', width + '%'); progressBar.text(width + '%'); } }, 50); // 模拟进度,逐步增加 }); // 隐藏其他插件提示 $('.notice, .update-nag, .updated, .error, .is-dismissible').not('.content-replacer-container .notice').hide(); // 检查 function.php 中是否有相关简码 $.ajax({ url: ajaxurl, data: { 'action': 'check_shortcode_status' }, success: function(response) { if (response.shipping_hidden) { $('#toggle-shipping-button').val('其他运费已隐藏'); } if (response.modal_enabled) { $('#toggle-modal-button').val('产品图片模态窗已开启'); } if (response.hyperlinks_exist) { $('#toggle-hyperlink-button').val('描述有超链接,点击去掉').css({ 'background-color': '#ff6666', 'color': '#f5f5dc' }).prop('disabled', false); } else { $('#toggle-hyperlink-button').val('产品无链接,无需点击此按钮').css({ 'background-color': '#add8e6', 'color': '#ffffff' }).prop('disabled', true); } } }); // 当去掉超链接按钮被点击 $('#hyperlink-toggle-form').on('submit', function() { $('#toggle-hyperlink-button').val('超链接已去掉,无需点击此按钮').prop('disabled', true); $('#close-hyperlink-button').parent().show(); }); // 当关闭超链接功能按钮被点击 $('#hyperlink-close-form').on('submit', function() { $('#toggle-hyperlink-button').val('描述有超链接,点击去掉').prop('disabled', false); $('#close-hyperlink-button').parent().hide(); }); }); </script> <?php } // 处理表单提交 public function handle_form_submission() { if (!isset($_POST['replace_content_nonce_field']) || !wp_verify_nonce($_POST['replace_content_nonce_field'], 'replace_content_nonce')) { wp_die('Nonce 验证失败'); } if (!current_user_can('manage_options')) { wp_die('无权限'); } $original_text = sanitize_text_field($_POST['original_text']); $new_text = sanitize_text_field($_POST['new_text']); global $wpdb; // 获取所有产品 $products = $wpdb->get_results("SELECT ID, post_title, post_content, post_excerpt FROM {$wpdb->posts} WHERE post_type = 'product'"); // 在替换前保存原始数据 $history = get_option('content_replacer_history', array()); $total_replacements = 0; foreach ($products as $product) { $original_data = array( 'ID' => $product->ID, 'post_title' => $product->post_title, 'post_content' => $product->post_content, 'post_excerpt' => $product->post_excerpt, ); $title_replacements = substr_count($product->post_title, $original_text); $content_replacements = substr_count($product->post_content, $original_text); $excerpt_replacements = substr_count($product->post_excerpt, $original_text); $total_replacements += $title_replacements + $content_replacements + $excerpt_replacements; $history_entry = array( 'date' => current_time('mysql'), 'original_text' => $original_text, 'new_text' => $new_text, 'data' => $original_data, 'replacements' => $total_replacements, ); array_unshift($history, $history_entry); // 添加到数组开头 } update_option('content_replacer_history', $history); // 执行替换操作 foreach ($products as $product) { $updated_post = array( 'ID' => $product->ID, 'post_title' => str_replace($original_text, $new_text, $product->post_title), 'post_content' => str_replace($original_text, $new_text, $product->post_content), 'post_excerpt' => str_replace($original_text, $new_text, $product->post_excerpt), ); $wpdb->update( $wpdb->posts, $updated_post, array('ID' => $product->ID) ); } wp_redirect(admin_url('admin.php?page=content-replacer')); exit; } // 处理回滚操作 public function handle_rollback() { if (!isset($_GET['rollback_content_nonce_field']) || !wp_verify_nonce($_GET['rollback_content_nonce_field'], 'rollback_content_nonce')) { wp_die('Nonce 验证失败'); } if (!current_user_can('manage_options')) { wp_die('无权限'); } $date = urldecode($_GET['date']); $history = get_option('content_replacer_history', array()); // 筛选出指定日期的历史记录 $entries_to_rollback = array_filter($history, function($entry) use ($date) { return date('Y-m-d H:i:s', strtotime($entry['date'])) === $date; }); if (!empty($entries_to_rollback)) { global $wpdb; foreach ($entries_to_rollback as $entry) { $original_data = $entry['data']; $wpdb->update( $wpdb->posts, array( 'post_title' => $original_data['post_title'], 'post_content' => $original_data['post_content'], 'post_excerpt' => $original_data['post_excerpt'], ), array('ID' => $original_data['ID']) ); } // 移除已回滚的历史记录 $history = array_filter($history, function($entry) use ($date) { return date('Y-m-d H:i:s', strtotime($entry['date'])) !== $date; }); update_option('content_replacer_history', $history); wp_redirect(admin_url('admin.php?page=content-replacer')); exit; } else { wp_die('无效的历史记录日期'); } } // 处理免运费隐藏按钮的表单提交 public function handle_toggle_shipping() { if (!isset($_POST['toggle_shipping_nonce_field']) || !wp_verify_nonce($_POST['toggle_shipping_nonce_field'], 'toggle_shipping_nonce')) { wp_die('Nonce 验证失败'); } if (!current_user_can('manage_options')) { wp_die('无权限'); } $function_code = " function js_hide_all_shipping_when_free_is_available( \$shipping_rates ) { foreach ( \$shipping_rates as \$key => \$rate ) { if ( \$rate->get_method_id() == 'free_shipping' ) { \$shipping_rates = array( \$key => \$rate ); } } return \$shipping_rates; } add_filter( 'woocommerce_package_rates', 'js_hide_all_shipping_when_free_is_available' ); "; $functions_path = get_template_directory() . '/functions.php'; $functions_content = file_get_contents($functions_path); if (strpos($functions_content, 'js_hide_all_shipping_when_free_is_available') === false) { file_put_contents($functions_path, $functions_content . PHP_EOL . $function_code); } else { $functions_content = str_replace($function_code, '', $functions_content); file_put_contents($functions_path, $functions_content); } wp_redirect(admin_url('admin.php?page=content-replacer')); exit; } // 处理产品模态窗按钮的表单提交 public function handle_toggle_modal() { if (!isset($_POST['toggle_modal_nonce_field']) || !wp_verify_nonce($_POST['toggle_modal_nonce_field'], 'toggle_modal_nonce')) { wp_die('Nonce 验证失败'); } if (!current_user_can('manage_options')) { wp_die('无权限'); } $function_code = " function theme_enqueue_scripts_and_styles() { wp_enqueue_style('custom-style', get_stylesheet_directory_uri() . '/css/custom-style.css'); wp_enqueue_script('custom-script', get_template_directory_uri() . '/js/custom-script.js', array('jquery'), null, true); \$inline_script = \"jQuery(document).ready(function($) { \$('body').append('<div id=\\\"myModal\\\" class=\\\"modal\\\"><span class=\\\"close\\\">×</span><img class=\\\"modal-content\\\" id=\\\"img01\\\"></div>'); var modal_css = '<style>\ .modal {display: none; position: fixed; z-index: 1000; padding-top: 100px; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgb(0,0,0); background-color: rgba(0,0,0,0.9);}\ .modal-content {margin: auto; display: block; max-width: 40%; transition: transform 0.3s;}\ .close {position: absolute; top: 15px; right: 35px; color: white; font-size: 40px; font-weight: bold; cursor: pointer;}\ </style>'; \$('head').append(modal_css); var isZoomed = false; \$('.woocommerce-product-gallery__image a').click(function(event) { event.preventDefault(); var imgSrc = \$(this).find('img').attr('src'); \$('#img01').attr('src', imgSrc); \$('#myModal').css('display', 'block'); isZoomed = false; \$('#img01').css('transform', 'scale(1)'); }); \$('.close, .modal').on('click', function(event) { if (event.target !== this) return; \$('#myModal').css('display', 'none'); }); \$('#img01').on('dblclick', function() { if (isZoomed) { \$(this).css('transform', 'scale(1)'); isZoomed = false; } else { \$(this).css('transform', 'scale(2)'); isZoomed = true; } }); }); \"; wp_add_inline_script('custom-script', \$inline_script); } add_action('wp_enqueue_scripts', 'theme_enqueue_scripts_and_styles'); "; $functions_path = get_template_directory() . '/functions.php'; $functions_content = file_get_contents($functions_path); if (strpos($functions_content, 'theme_enqueue_scripts_and_styles') === false) { file_put_contents($functions_path, $functions_content . PHP_EOL . $function_code); } else { $functions_content = str_replace($function_code, '', $functions_content); file_put_contents($functions_path, $functions_content); } wp_redirect(admin_url('admin.php?page=content-replacer')); exit; } // 处理去产品超链接按钮的表单提交 public function handle_toggle_hyperlink() { if (!isset($_POST['toggle_hyperlink_nonce_field']) || !wp_verify_nonce($_POST['toggle_hyperlink_nonce_field'], 'toggle_hyperlink_nonce')) { wp_die('Nonce 验证失败'); } if (!current_user_can('manage_options')) { wp_die('无权限'); } $function_code = " function remove_product_hyperlinks(\$content) { if (is_product()) { \$content = preg_replace('/<a href=\".*?\">(.*?)<\/a>/', '\$1', \$content); } return \$content; } add_filter('the_content', 'remove_product_hyperlinks'); "; $functions_path = get_template_directory() . '/functions.php'; $functions_content = file_get_contents($functions_path); if (strpos($functions_content, 'remove_product_hyperlinks') === false) { file_put_contents($functions_path, $functions_content . PHP_EOL . $function_code); } wp_redirect(admin_url('admin.php?page=content-replacer')); exit; } // 处理关闭去超链接功能按钮的表单提交 public function handle_close_hyperlink() { if (!isset($_POST['close_hyperlink_nonce_field']) || !wp_verify_nonce($_POST['close_hyperlink_nonce_field'], 'close_hyperlink_nonce')) { wp_die('Nonce 验证失败'); } if (!current_user_can('manage_options')) { wp_die('无权限'); } $functions_path = get_template_directory() . '/functions.php'; $functions_content = file_get_contents($functions_path); $function_code = " function remove_product_hyperlinks(\$content) { if (is_product()) { \$content = preg_replace('/<a href=\".*?\">(.*?)<\/a>/', '\$1', \$content); } return \$content; } add_filter('the_content', 'remove_product_hyperlinks'); "; if (strpos($functions_content, 'remove_product_hyperlinks') !== false) { $functions_content = str_replace($function_code, '', $functions_content); file_put_contents($functions_path, $functions_content); } wp_redirect(admin_url('admin.php?page=content-replacer')); exit; } // 新增 AJAX 处理函数,检测简码状态 // 新增 AJAX 处理函数,检测产品描述中的超链接状态 public function check_shortcode_status() { $functions_path = get_template_directory() . '/functions.php'; $functions_content = file_get_contents($functions_path); $response = array( 'shipping_hidden' => strpos($functions_content, 'js_hide_all_shipping_when_free_is_available') !== false, 'modal_enabled' => strpos($functions_content, 'theme_enqueue_scripts_and_styles') !== false, 'hyperlinks_exist' => false ); global $wpdb; $products = $wpdb->get_results("SELECT post_content FROM {$wpdb->posts} WHERE post_type = 'product'"); foreach ($products as $product) { if (preg_match('/<a href=\".*?\">/', $product->post_content)) { $response['hyperlinks_exist'] = true; break; } } wp_send_json($response); } } // 实例化插件类 new ContentReplacer();
scripts.js文件
// scripts.js jQuery(document).ready(function($) { $('#content-replacer-form').on('submit', function() { $('#loading').show(); }); // 隐藏其他插件提示 $('.notice, .update-nag, .updated, .error, .is-dismissible').not('.content-replacer-container .notice').hide(); // 提交表单时显示 loading 动画 $('#shipping-toggle-form, #modal-toggle-form').on('submit', function() { $('#loading').show(); }); // 检查 function.php 中是否有相关简码 $.ajax({ url: ajaxurl, data: { 'action': 'check_shortcode_status' }, success: function(response) { if (response.shipping_hidden) { $('#toggle-shipping-button').val('其他运费已隐藏'); } if (response.modal_enabled) { $('#toggle-modal-button').val('产品图片模态窗已开启'); } if (response.hyperlinks_exist) { $('#toggle-hyperlink-button').val('描述有超链接,点击去掉').css({ 'background-color': '#ff6666', 'color': '#f5f5dc' }).prop('disabled', false); } else { $('#toggle-hyperlink-button').val('产品无链接,无需点击此按钮').css({ 'background-color': '#add8e6', 'color': '#ffffff' }).prop('disabled', true); } } }); // 当去掉超链接按钮被点击 $('#hyperlink-toggle-form').on('submit', function() { $('#toggle-hyperlink-button').val('超链接已去掉,无需点击此按钮').prop('disabled', true); $('#close-hyperlink-button').parent().show(); }); // 当关闭超链接功能按钮被点击 $('#hyperlink-close-form').on('submit', function() { $('#toggle-hyperlink-button').val('描述有超链接,点击去掉').prop('disabled', false); $('#close-hyperlink-button').parent().hide(); }); });
styles.css文件
/* styles.css */ /* 渐变背景色 */ .content-replacer-container { background: linear-gradient(135deg, #f9f1e9, #f5e0d3); padding: 20px; border-radius: 10px; color: #333; max-width: 800px; margin: 50px auto; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); text-align: center; } .content-replacer-container h1 { text-align: center; margin-bottom: 20px; font-size: 2em; } .content-replacer-container .form-table { width: 100%; margin: 0 auto; } .content-replacer-container .form-table th, .content-replacer-container .form-table td { padding: 10px; text-align: left; } .content-replacer-container input[type="text"] { width: 100%; padding: 10px; border: 1px solid #ddd; border-radius: 5px; margin-bottom: 10px; box-sizing: border-box; } .content-replacer-container .button-primary { background: #ffba00; border: none; padding: 10px 20px; border-radius: 5px; cursor: pointer; transition: background 0.3s; } .content-replacer-container .button-primary:hover { background: #ff9500; } .content-replacer-container .loading { display: none; text-align: center; padding: 10px; margin-top: 20px; border-radius: 5px; color: #000; /* 将文字颜色改为黑色 */ font-weight: bold; } .content-replacer-container .progress-bar { display: none; width: 100%; background-color: #fff; border: 1px solid #ddd; border-radius: 5px; overflow: hidden; margin-top: 20px; position: relative; } .content-replacer-container .progress-bar-inner { height: 30px; background-color: #00aaff; width: 0; line-height: 30px; color: #fff; text-align: center; white-space: nowrap; transition: width 0.5s ease; /* 平滑的过渡效果 */ } /* 替换历史样式 */ .content-replacer-container ul { list-style: none; padding: 0; } .content-replacer-container ul li { background: rgba(255, 255, 255, 0.2); margin-bottom: 10px; padding: 10px; border-radius: 5px; } .content-replacer-container ul li a { color: #333; text-decoration: underline; cursor: pointer; transition: color 0.3s; } .content-replacer-container ul li a:hover { color: #ffba00; }
duola.php 标签:function,content,functions,replacer,乱码,toggle,导入,WordPress,post From: https://www.cnblogs.com/Boboschen/p/18680805