Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
AsyncAlchemist committed Apr 9, 2024
0 parents commit d0bcf7c
Show file tree
Hide file tree
Showing 10 changed files with 762 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# .gitignore

.env
5 changes: 5 additions & 0 deletions Dockerfile-wordpress
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM wordpress:latest

# Custom PHP configurations
RUN echo "upload_max_filesize = 1024M" >> /usr/local/etc/php/conf.d/uploads.ini
RUN echo "post_max_size = 1024M" >> /usr/local/etc/php/conf.d/uploads.ini
57 changes: 57 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
version: '3.1'

services:
wordpress:
build:
context: .
dockerfile: Dockerfile-wordpress
image: wordpress:latest
platform: linux/arm64 # Changed to ARM64
ports:
- "80:80"
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: ${WORDPRESS_DB_PASSWORD}
WORDPRESS_DB_NAME: wordpress
volumes:
- wce_wordpress_data:/var/www/html
- ./plugin:/var/www/html/wp-content/plugins/cache-everything # Mount the plugin directory
restart: always

db:
image: mysql:5.7
platform: linux/amd64 # Changed to ARM64
volumes:
- wce_db_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: ${WORDPRESS_DB_PASSWORD}
restart: always

phpmyadmin:
image: phpmyadmin/phpmyadmin
platform: linux/amd64 # Changed to ARM64
ports:
- "8080:80"
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
depends_on:
- db
restart: always

cloudflared:
image: cloudflare/cloudflared:latest
platform: linux/arm64 # Check if ARM64 is supported
command: tunnel run a1b67e02-62ff-4bd8-b255-0d87a4810cd2
environment:
TUNNEL_TOKEN: ${TUNNEL_TOKEN}
depends_on:
- wordpress

volumes:
wce_wordpress_data:
wce_db_data:
108 changes: 108 additions & 0 deletions plugin/admin/menu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

function cache_everything_activate() {
add_option('cache_everything_debug_mode', '0'); // Default value set to '0' (debug mode off)
}

/**
* Add an admin menu item for the plugin.
*/
function cache_everything_add_admin_menu() {
// Settings Page
add_menu_page(
'Cache Everything Settings', // Page title
'Cache Everything', // Menu title
'manage_options', // Capability
'cache-everything-settings', // Menu slug for settings
'cache_everything_settings_page', // Function to display the settings page
'dashicons-admin-generic', // Icon URL
6 // Position
);

// Readme Page
add_submenu_page(
'cache-everything-settings', // Parent slug
'Cache Everything Readme', // Page title
'Readme', // Menu title
'manage_options', // Capability
'cache-everything-readme', // Menu slug for readme
'cache_everything_display_readme' // Function to display the readme page
);

add_action('admin_init', 'cache_everything_register_settings');
}

add_action('admin_menu', 'cache_everything_add_admin_menu');

/**
* Register settings for the Cache Everything plugin.
*/
function cache_everything_register_settings() {
// Register a new setting for Cache Everything to store the debug mode option
register_setting('cache_everything_settings', 'cache_everything_debug_mode');

// Add a new section to the Cache Everything admin page
add_settings_section(
'cache_everything_settings_section', // ID
'Cache Everything Settings', // Title
'cache_everything_settings_section_callback', // Callback
'cache-everything-settings' // Page
);

// Add the debug mode option field to the new section
add_settings_field(
'cache_everything_debug_mode', // ID
'Debug Mode', // Title
'cache_everything_debug_mode_callback', // Callback
'cache-everything-settings', // Page
'cache_everything_settings_section' // Section
);
}

/**
* Settings section callback function.
*/
function cache_everything_settings_section_callback() {
echo '<p>Adjust the settings for Cache Everything.</p>';
}

/**
* Debug mode field callback function.
*/
function cache_everything_debug_mode_callback() {
$debug_mode = get_option('cache_everything_debug_mode');
echo '<input type="checkbox" id="cache_everything_debug_mode" name="cache_everything_debug_mode" value="1" ' . checked(1, $debug_mode, false) . '/>';
echo '<label for="cache_everything_debug_mode">Enable Debug Mode</label>';
}

// Adjusted function to display settings
function cache_everything_settings_page() {
echo '<form action="options.php" method="post">';
settings_fields('cache_everything_settings');
do_settings_sections('cache-everything-settings'); // Adjusted to match the settings page slug
submit_button();
echo '</form>';
}

// Adjusted function to only display the readme content
function cache_everything_display_readme() {
$readme_path = plugin_dir_path(__FILE__) . '../readme.md';
if (file_exists($readme_path)) {
$readme_content = file_get_contents($readme_path);
// Escape the content for JavaScript
$readme_content_js = json_encode($readme_content);
echo '<div id="readmeContent" class="wce_wrap"></div>'; // Container for the converted HTML
echo "<script src=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/showdown.min.js\"></script>";
echo "<script>
document.addEventListener('DOMContentLoaded', function() {
var converter = new showdown.Converter(),
text = $readme_content_js,
html = converter.makeHtml(text);
document.getElementById('readmeContent').innerHTML = html;
});
</script>";
} else {
// Print out the full path attempted for debugging purposes
echo '<div class="wrap"><h1>Readme File Not Found</h1><p>Attempted path: ' . esc_html($readme_path) . '</p></div>';
}
}
99 changes: 99 additions & 0 deletions plugin/cache-everything.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
/**
* Plugin Name: Cache Everything
* Plugin URI: https://github.com/AsyncAlchemist
* Description: A simple plugin to cache everything in Wordpress.
* Version: 0.1
* Author: Taylor Selden
* Author URI: https://github.com/AsyncAlchemist
*/
define('CACHE_EVERYTHING_JS_URL', 'wp-content/plugins/cache-everything/js');
define('CACHE_EVERYTHING_CSS_URL', 'wp-content/plugins/cache-everything/css');

require_once(plugin_dir_path(__FILE__) . 'handle-js-request.php');
require_once(plugin_dir_path(__FILE__) . 'handle-css-request.php');
require_once(plugin_dir_path(__FILE__) . 'helpers.php');
require_once(plugin_dir_path(__FILE__) . 'admin/menu.php');


/**
* Flushes rewrite rules on plugin activation and deactivation.
*/
function cache_everything_flush_rewrite_rules() {
// Add the rewrite rule and then flush
cache_everything_add_rewrite_rule();
flush_rewrite_rules();
}
register_activation_hook(__FILE__, 'cache_everything_flush_rewrite_rules');
register_activation_hook(__FILE__, 'cache_everything_activate');
register_deactivation_hook(__FILE__, 'cache_everything_flush_rewrite_rules');

/**
* Registers a query variable for handling JavaScript requests.
*
* @param array $vars The array of existing query variables.
* @return array The modified array including the new query variable.
*/
function cache_everything_register_query_var($vars) {
$vars[] = 'cache_everything_js';
$vars[] = 'cache_everything_css';
return $vars;
}
add_filter('query_vars', 'cache_everything_register_query_var');

/**
* Adds a rewrite rule for serving the dynamic JavaScript file without the .js extension,
* simulating a path that includes the plugin directory.
*/
function cache_everything_add_rewrite_rule() {
// This rule simulates the path. Note that this does not actually place the file in that directory.
add_rewrite_rule(CACHE_EVERYTHING_JS_URL . '$', 'index.php?cache_everything_js=1', 'top');
add_rewrite_rule(CACHE_EVERYTHING_CSS_URL . '$', 'index.php?cache_everything_css=1', 'top');
}
add_action('init', 'cache_everything_add_rewrite_rule');
add_action('template_redirect', 'cache_everything_handle_js_request');
add_action('template_redirect', 'cache_everything_handle_css_request');


/**
* Enqueues the dynamic JavaScript file.
*/
function cache_everything_enqueue_scripts() {
$all_roles = cache_everything_get_role_slugs();

wp_enqueue_script('cache-everything', plugins_url('/public/js/cache-everything.js', __FILE__), array(), null, true);

// Determine if the site's permalink structure uses trailing slashes
$permalink_structure = get_option('permalink_structure');
$use_trailing_slashes = substr($permalink_structure, -1) === '/';

// Ensure the site URL does not end with a slash
$site_url = rtrim(site_url(), '/');

// Combine site URL with the CACHE_EVERYTHING_JS_URL to send a full URL without risking a double slash
// Append a trailing slash if the site uses trailing slashes in the permalink structure
$js_full_url = $site_url . '/' . CACHE_EVERYTHING_JS_URL . ($use_trailing_slashes ? '/' : '');

// Combine site URL with the CACHE_EVERYTHIN_CSS_URL to send a full URL without risking a double slash
// Append a trailing slash if the site uses trailing slashes in the permalink structure
$css_full_url = $site_url . '/' . CACHE_EVERYTHING_CSS_URL . ($use_trailing_slashes ? '/' : '');

// Enqueue the dynamic CSS file
wp_enqueue_style('cache-everything', $css_full_url, array(), null, 'all');

// Localize the script with the roles data, the full JS and CSS URLs, and the site prefix
$site_prefix = get_site_prefix(); // Retrieve the site prefix using the helper function

// Retrieve the debug mode setting from WordPress options
$debug_mode = get_option('cache_everything_debug_mode', '0'); // Default to '0' if not set

wp_localize_script('cache-everything', 'wce_Data', array(
'roles' => $all_roles,
'jsUrl' => $js_full_url,
'cssUrl' => $css_full_url,
'sitePrefix' => $site_prefix, // Add the site prefix to the localized script data
'debugMode' => $debug_mode // Add the debug mode status to the localized script data
));

}
add_action('wp_enqueue_scripts', 'cache_everything_enqueue_scripts');
29 changes: 29 additions & 0 deletions plugin/handle-css-request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

require_once(plugin_dir_path(__FILE__) . 'helpers.php');

/**
* Handles the request for the dynamic CSS file.
*/
function cache_everything_handle_css_request() {
if (get_query_var('cache_everything_css')) {
header("Content-Type: text/css; charset=UTF-8");

// Get all user rules
$all_roles = cache_everything_get_role_slugs();

// Get the site prefix by calling the new function
$site_prefix = get_site_prefix();

// Add 'guest' and 'user' to the array of roles to be hidden
array_push($all_roles, 'guest', 'user');

foreach ($all_roles as $slug) {
// Now $slug correctly represents each role slug
// Added !important to ensure this rule overrides others
echo ".$site_prefix-$slug { display: none !important; }\n";
}

exit;
}
}
44 changes: 44 additions & 0 deletions plugin/handle-js-request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* Handles requests for the dynamic JavaScript file.
*
* Outputs JavaScript code based on the user's login status and roles.
* Ensures the script is never cached by setting appropriate headers.
*/
function cache_everything_handle_js_request() {
$is_js_request = intval(get_query_var('cache_everything_js', 0));
if ($is_js_request === 1) {
header('Content-Type: application/javascript');
// Prevent caching of this script
header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
header('Pragma: no-cache'); // HTTP 1.0.
header('Expires: 0'); // Proxies.

// Initialize the response array
$response = array(
'status' => 'guest', // Default status
'roles' => array() // Default empty array for roles
);

// Dynamic JavaScript generation logic
if (is_user_logged_in()) {
$user = wp_get_current_user();
$response['status'] = 'user';
$response['roles'] = $user->roles;
}

// Convert the PHP array to a JSON string
$jsonResponse = json_encode($response);

// Output JavaScript to dispatch an update event with the new role data
echo <<<JS
(function() {
var newRoles = $jsonResponse;
document.dispatchEvent(new CustomEvent('wce_UserRolesUpdate', { detail: newRoles }));
})();
JS;

exit; // Stop further processing
}
}
35 changes: 35 additions & 0 deletions plugin/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/**
* Retrieves all user roles as slugs.
*
* @return array An array of role slugs.
*/
function cache_everything_get_role_slugs() {
global $wp_roles;
if (!isset($wp_roles)) {
$wp_roles = new WP_Roles();
}
return array_keys($wp_roles->get_names());
}

/**
* Generates and returns the site prefix based on the second to last part of the domain.
*
* @return string The site prefix.
*/
function get_site_prefix() {
$site_url = get_site_url(); // WordPress function to get site URL
$parsed_url = parse_url($site_url);
$host = $parsed_url['host'];

// Split the host into parts
$host_parts = explode('.', $host);
if (count($host_parts) > 2) {
// Return the second to last part of the domain, excluding TLD
return $host_parts[count($host_parts) - 2];
} else {
// If there are not enough parts for a subdomain, return the first part
return $host_parts[0];
}
}
Loading

0 comments on commit d0bcf7c

Please sign in to comment.