Initial commit

This commit is contained in:
2025-06-29 15:42:26 -07:00
commit 7cac0343ba
5 changed files with 980 additions and 0 deletions

261
content-warning.php Normal file
View File

@ -0,0 +1,261 @@
<?php
/**
* Plugin Name: Better Content Warning
* Description: Blocks access to content behind a content warning popup with optional trigger warnings.
* Version: 1.0
* Author: Sophia Atkinson
* Author URI: https://sophia.wtf
* Text Domain: better-content-warning
* License: GPLv3
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
* Requires at least: 6.8.1
* Requires PHP: 7.4
* Original idea based on the content-warning-v3 plugin by Jay Wood
*/
// Add metabox
add_action(
'add_meta_boxes', function () {
add_meta_box('cw_meta_box', 'Better Content Warning', 'cw_meta_box_callback', ['post', 'page'], 'side', 'high');
}
);
function cw_meta_box_callback($post)
{
$enabled = get_post_meta($post->ID, '_cw_enabled', true);
$warnings = get_post_meta($post->ID, '_cw_warnings', true);
wp_nonce_field('cw_meta_box', 'cw_meta_box_nonce');
?>
<p>
<label><input type="checkbox" name="cw_enabled" <?php checked($enabled, 'on'); ?>> Enable Content Warning</label>
</p>
<p>
<label for="cw_warnings">Trigger Warnings (comma-separated):</label><br>
<textarea name="cw_warnings" id="cw_warnings" rows="3" style="width:100%;"><?php echo esc_textarea($warnings); ?></textarea>
</p>
<?php
}
// Save meta
add_action(
'save_post', function ($post_id) {
if (!isset($_POST['cw_meta_box_nonce']) || !wp_verify_nonce($_POST['cw_meta_box_nonce'], 'cw_meta_box')) { return;
}
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return;
}
if (!current_user_can('edit_post', $post_id)) { return;
}
update_post_meta($post_id, '_cw_enabled', isset($_POST['cw_enabled']) ? 'on' : '');
update_post_meta($post_id, '_cw_warnings', sanitize_text_field($_POST['cw_warnings']));
}
);
// Show popup before content
add_filter(
'the_content', function ($content) {
if (!is_singular() || !in_the_loop() || !is_main_query()) { return $content;
}
// Skip popup for admins unless the setting is enabled
if (current_user_can('manage_options') && get_option('cw_show_to_admins') !== '1') {
return $content;
}
$enabled = get_post_meta(get_the_ID(), '_cw_enabled', true);
if ($enabled !== 'on') { return $content;
}
$post_id = get_the_ID();
$warnings = trim(get_post_meta($post_id, '_cw_warnings', true));
$prefix = get_option('cw_prefix', 'Content Warning');
$default = get_option('cw_default_message', 'This post contains content that may be triggering to some people, Continue forward at your discretion.');
$button = get_option('cw_button_text', 'Continue');
$cookie_duration = 180;
$exit_button = get_option('cw_exit_button_text', 'Exit');
$header = esc_html($prefix);
$warning = $warnings ? esc_html($warnings) : esc_html($default);
ob_start();
?>
<div id="cw-popup" class="cw-overlay">
<div class="cw-box">
<p><strong><?php echo $header; ?></strong></p>
<p><?php echo $warning; ?></p>
<button title="Lets get this party started /s" id="cw-continue"><?php echo esc_html($button); ?></button>
<br>
<button title="No shame in exiting <3" id="cw-exit" aria-label="<?php echo esc_attr($exit_button); ?>"><?php echo esc_html($exit_button); ?></button>
</div>
</div>
<script>
(function () {
const postId = "<?php echo esc_js($post_id); ?>";
const cookieKey = "cw_seen_" + postId;
const popup = document.getElementById("cw-popup");
const btn = document.getElementById("cw-continue");
const exitBtn = document.getElementById("cw-exit");
function goHome() {
window.history.back();
}
if (document.cookie.includes(cookieKey + "=true")) {
popup.style.display = "none";
return;
}
btn.addEventListener("click", function () {
popup.style.display = "none";
const expiry = new Date();
expiry.setDate(expiry.getDate() + <?php echo $cookie_duration; ?>);
document.cookie = cookieKey + "=true; path=/; expires=" + expiry.toUTCString();
});
exitBtn.addEventListener("click", goHome);
document.addEventListener("keydown", function(e) {
if (e.key === "Escape") goHome();
});
})();
</script>
<?php
return ob_get_clean() . '<div id="cw-content">' . $content . '</div>';
}
);
// Add styles
add_action(
'wp_head', function () {
$bg = esc_attr(get_option('cw_bg_color', '#1E1F22'));
$overlay_bg = esc_attr(get_option('cw_overlay_bg_color', '#2E3035'));
$font = esc_attr(get_option('cw_font_size', '1.1em'));
$btn = esc_attr(get_option('cw_button_color', '#9E93DC'));
$btn_hover = esc_attr(get_option('cw_button_hover_color', '#8179d5'));
echo "
<style>
.cw-overlay {
position: fixed;
z-index: 9999;
top: 0; left: 0;
width: 100%; height: 100%;
background: $overlay_bg;
display: flex;
align-items: center;
justify-content: center;
}
.cw-box {
background: $bg;
color: #fff;
padding: 3.0em;
border-radius: 12px;
max-width: 700px;
text-align: center;
font-size: $font;
}
.cw-box button {
background: $btn;
border: none;
padding: 0.6em 1.2em;
margin-top: 1em;
font-size: 1em;
color: #fff;
border-radius: 5px;
cursor: pointer;
}
.cw-box button:hover {
background: $btn_hover;
transition: background-color 0.3s ease;
}
</style>";
}
);
// Settings menu
add_action(
'admin_menu', function () {
add_options_page('Better Content Warning Settings', 'Better Content Warning', 'manage_options', 'cw-settings', 'cw_settings_page');
}
);
// Add settings link to plugin page
add_filter(
'plugin_action_links_' . plugin_basename(__FILE__), function ($links) {
$settings_link = '<a href="options-general.php?page=cw-settings">Settings</a>';
array_unshift($links, $settings_link);
return $links;
}
);
// Register settings
add_action(
'admin_init', function () {
register_setting('cw_settings_group', 'cw_prefix');
register_setting('cw_settings_group', 'cw_default_message');
register_setting('cw_settings_group', 'cw_button_text');
register_setting('cw_settings_group', 'cw_bg_color');
register_setting('cw_settings_group', 'cw_font_size');
register_setting('cw_settings_group', 'cw_button_color');
register_setting('cw_settings_group', 'cw_overlay_bg_color');
register_setting('cw_settings_group', 'cw_button_hover_color');
register_setting('cw_settings_group', 'cw_show_to_admins');
register_setting('cw_settings_group', 'cw_exit_button_text');
}
);
// Settings page output
function cw_settings_page()
{
?>
<div class="wrap">
<h1>Content Warning Settings</h1>
<form method="post" action="options.php">
<?php settings_fields('cw_settings_group'); do_settings_sections('cw_settings_group'); ?>
<table class="form-table">
<tr>
<th><label for="cw_show_to_admins">Show Popup to Admins</label></th>
<td>
<input name="cw_show_to_admins" type="checkbox" id="cw_show_to_admins" value="1" <?php checked(get_option('cw_show_to_admins'), '1'); ?>>
<label for="cw_show_to_admins">Enable content warnings for administrator accounts</label>
</td>
</tr>
<tr>
<th><label for="cw_prefix">Warning Prefix</label></th>
<td><input name="cw_prefix" type="text" id="cw_prefix" value="<?php echo esc_attr(get_option('cw_prefix', 'Content Warning:')); ?>" class="regular-text"></td>
</tr>
<tr>
<th><label for="cw_default_message">Default Warning Message</label></th>
<td><input name="cw_default_message" type="text" id="cw_default_message" value="<?php echo esc_attr(get_option('cw_default_message', 'This post contains content that may be triggering to some people, Continue forward at your discretion.')); ?>" class="regular-text"></td>
</tr>
<tr>
<th><label for="cw_button_text">Continue Button Text</label></th>
<td><input name="cw_button_text" type="text" id="cw_button_text" value="<?php echo esc_attr(get_option('cw_button_text', 'Continue')); ?>" class="regular-text"></td>
</tr>
<tr>
<th><label for="cw_exit_button_text">Exit Button Text</label></th>
<td><input name="cw_exit_button_text" type="text" id="cw_exit_button_text" value="<?php echo esc_attr(get_option('cw_exit_button_text', 'Exit')); ?>" class="regular-text"></td>
</tr>
<tr>
<th><label for="cw_bg_color">Popup Background Color</label></th>
<td><input name="cw_bg_color" type="text" id="cw_bg_color" value="<?php echo esc_attr(get_option('cw_bg_color', '#1E1F22')); ?>" class="regular-text"></td>
</tr>
<tr>
<th><label for="cw_overlay_bg_color">Overlay Background Color</label></th>
<td><input name="cw_overlay_bg_color" type="text" id="cw_overlay_bg_color" value="<?php echo esc_attr(get_option('cw_overlay_bg_color', '#2E3035')); ?>" class="regular-text"></td>
</tr>
<tr>
<th><label for="cw_font_size">Font Size</label></th>
<td><input name="cw_font_size" type="text" id="cw_font_size" value="<?php echo esc_attr(get_option('cw_font_size', '1.1em')); ?>" class="regular-text"></td>
</tr>
<tr>
<th><label for="cw_button_color">Button Color</label></th>
<td><input name="cw_button_color" type="text" id="cw_button_color" value="<?php echo esc_attr(get_option('cw_button_color', '#e67c73')); ?>" class="regular-text"></td>
</tr>
<tr>
<th><label for="cw_button_hover_color">Button Hover Color</label></th>
<td><input name="cw_button_hover_color" type="text" id="cw_button_hover_color" value="<?php echo esc_attr(get_option('cw_button_hover_color', '#8179d5')); ?>" class="regular-text"></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
</div>
<div style="margin-top:2em; color:#666;">
<p>If you like this plugin, please consider supporting my work on <a href="https://liberapay.com/sophiaxd" target="_blank">Liberapay</a> or <a href="https://ko-fi.com/sophiaatkinson" target="_blank">Ko-fi</a>.</p>
</div>
<?php
}