commit 1aa6d717ed08e5a7f23bc4f68fb499755e0c04db
parent 19b91de809d48dd921e0386db32fc636f6b99a7e
Author: Sean Enck <sean@ttypty.com>
Date: Sun, 2 Nov 2025 11:55:37 -0500
init plugin content
Diffstat:
3 files changed, 266 insertions(+), 0 deletions(-)
diff --git a/plugins/hphp/hphp.php b/plugins/hphp/hphp.php
@@ -0,0 +1,222 @@
+<?php
+/*
+Plugin Name: Happy Paws Haven Plugin
+Description: Customized wordpress settings for HPH
+Version: {VERSION}
+Author: Sean Enck
+License: GPL-3.0+
+Text Domain: happy-paws-haven-plugin
+*/
+
+
+// NOTE:
+// this is mostly baselined starting from
+// https://wordpress.com/blog/2025/07/31/introduction-to-wordpress-plugin-development/
+function hphp_register_settings() {
+ register_setting( 'hphp_settings_group', 'hphp_json_configuration', array(
+ 'type' => 'string',
+ 'default' => '{}',
+ ) );
+ register_setting( 'hphp_settings_group', 'hphp_pages_onready', array(
+ 'type' => 'boolean',
+ 'default' => '',
+ ) );
+ register_setting( 'hphp_settings_group', 'hphp_pages_allowed', array(
+ 'type' => 'string',
+ 'sanitize_callback' => 'hphp_sanitize_comma_list',
+ 'default' => '',
+ ) );
+ $counters = hphp_load_counters(get_option("hphp_json_configuration", "{}"));
+ foreach ($counters as $key => $value) {
+ if ($value == "int" ) {
+ register_setting( 'hphp_settings_group', $key, array(
+ 'type' => 'integer',
+ 'sanitize_callback' => 'hphp_sanitize_int',
+ 'default' => 0,
+ ) );
+ } else if ($value == "sum" ) {
+ register_setting( 'hphp_settings_group', $key, array(
+ 'type' => 'string',
+ 'sanitize_callback' => 'hphp_sanitize_sum',
+ 'default' => '',
+ ) );
+ }
+ }
+}
+
+add_action( 'admin_init', 'hphp_register_settings' );
+
+function hphp_sanitize_comma_list( $value ) {
+ return preg_replace('/[^a-z0-9-,]/', '', $value) ?? '';
+}
+
+function hphp_sanitize_sum( $value ) {
+ return preg_replace('/[^a-z_]/', '', $value) ?? '';
+}
+
+function hphp_sanitize_int( $value ) {
+ $value = intval($value);
+ return ( $value > 0 ) ? $value : 0;
+}
+
+function hphp_load_counters( $json ) {
+ $counters = array();
+ $objects = json_decode("$json", $true);
+ if (property_exists($objects, "counters")) {
+ foreach ($objects->counters as $key => $value) {
+ $counters["hphp_counter_" . $key] = $value;
+ }
+ }
+ return $counters;
+}
+
+function hphp_register_settings_page() {
+ add_options_page(
+ 'HPHP',
+ 'HPHP',
+ 'manage_options',
+ 'hphp-settings',
+ 'hphp_render_settings_page'
+ );
+}
+
+function hphp_print_counter_settings( $counters, $type ) {
+ foreach ($counters as $key => $value) {
+ if ($value != "$type") {
+ continue;
+ }
+ echo "<hr />";
+ if ($value == "int") {
+ $numeric = get_option( $key , 0 );
+ echo "<input name='" . $key . "' type='number' id='" . $key . "' value='" . esc_attr( $numeric ) . "' class='small-text' min='1' />";
+ } else if ($value == "sum" ) {
+ $str = get_option( $key, "" );
+ echo "<input name='" . $key . "' type='text' id='" . $key . "' value='" . esc_attr( $str ) . "' />";
+ }
+ echo "<label style='padding-left: 5px' for='" . $key . "'>" . str_replace("_", " ", str_replace("hphp_counter_", "", $key)) . "</label>";
+ echo "<br />";
+ }
+}
+
+add_action( 'admin_menu', 'hphp_register_settings_page' );
+
+// Render the settings page.
+function hphp_render_settings_page() {
+ if ( ! current_user_can( 'manage_options' ) ) {
+ return;
+ }
+ ?>
+ <div class="wrap">
+ <h1><?php esc_html_e( 'Happy Paws Haven Plugin Settings', 'happy-paws-haven' ); ?></h1>
+ <hr />
+ <h3>Counters</h3>
+ <form method="post" action="options.php">
+ <?php
+ settings_fields( 'hphp_settings_group' );
+ do_settings_sections( 'hphp_settings_group' );
+ $json_payload = get_option("hphp_json_configuration", "{}");
+ $counters = hphp_load_counters( $json_payload );
+ ksort($counters);
+ hphp_print_counter_settings($counters, "int");
+ hphp_print_counter_settings($counters, "sum");
+ $pages = get_option("hphp_pages_allowed", "");
+ $onready = get_option("hphp_pages_onready", false);
+ $checked = "";
+ if ($onready) {
+ $checked = "checked";
+ }
+ ?>
+ <hr />
+ <button type="button" onclick="document.getElementById('hphp_advanced_config').style.display = (document.getElementById('hphp_advanced_config').style.display == 'none' ? 'block' : 'none');">Advanced Configuration</button>
+ <div id="hphp_advanced_config" style="display: none; padding: 20px; background-color: #f0f0f0; border: 1px solid #ccc;">
+ This value controls which counters are available and how they are enabled, it is a JSON payload (string).
+ <br />
+ <br />
+ It currently accepts a 'counters' key which is a dictionary of name/value pairs where name is the variable and value is one of 'int' or 'sum'. An 'int' type is a numeric and a 'sum' requires a string to match on.
+ <textarea name="hphp_json_configuration" id="hphp_json_configuration" rows="10" cols="80"><?php echo esc_attr( $json_payload ); ?></textarea>
+ <hr />
+ This value controls whether a page is allowed to process/show counter data ([a-z] strings and comma delimited: ',')
+ <br />
+ <input type="text" name="hphp_pages_allowed" id="hphp_pages_allowed" value='<?php echo esc_attr( $pages ); ?>'/>
+ <hr />
+ Indicates whether document ready should fire an init.
+ <br />
+ <input type="checkbox" name="hphp_pages_onready" id="hphp_pages_onready" <?php echo $checked; ?>/>
+ </div>
+ <hr />
+ <?php submit_button(); ?>
+ </form>
+ </div>
+ <?php
+}
+
+function hphp_enqueue_assets() {
+ wp_enqueue_style(
+ 'hphp-style',
+ plugin_dir_url( __FILE__ ) . 'style.css',
+ array(),
+ '{CSS_VERSION}'
+ );
+ wp_enqueue_script(
+ 'hphp-js',
+ plugin_dir_url( __FILE__ ) . 'plugin.js',
+ array('jquery'),
+ '{JS_VERSION}'
+ );
+ $pages = get_option("hphp_pages_allowed", "");
+ $allowed = true;
+ $page_name = "";
+ $queried = get_queried_object();
+ if ($queried && property_exists($queried, 'post_name')) {
+ $page_name = $queried->post_name;
+ }
+ if (!empty($pages) && $page_name != "") {
+ $filtering = explode(",", $pages);
+ $allowed = false;
+ foreach ($filtering as $filtered) {
+ if (str_contains($page_name, $filtered)) {
+ $allowed = true;
+ break;
+ }
+ }
+
+ }
+ $counter_data = array();
+ if ($allowed) {
+ $sums = array();
+ $json_payload = get_option("hphp_json_configuration", "{}");
+ $counters = hphp_load_counters( $json_payload );
+ foreach ($counters as $key => $value) {
+ if ($value == "int") {
+ $counter_data[$key] = get_option($key, 0);
+ } else if ($value == "sum" ) {
+ $str = get_option( $key, "" );
+ $sums[$key] = $str;
+ }
+ }
+ foreach ($sums as $key => $str) {
+ $value = 0;
+ foreach ($counter_data as $counter => $val) {
+ if (str_contains($counter, $str)) {
+ $value = $value + $val;
+ }
+ }
+ $counter_data[$key] = $value;
+ }
+ }
+ $payload = array();
+ $payload["hphp_page_name"] = $page_name;
+ $triggers = array();
+ if (!get_option("hphp_pages_onready", false)) {
+ array_push($triggers, "onready");
+ }
+ $payload["triggers"] = $triggers;
+ if (!empty($counter_data)) {
+ $payload["counters"] = $counter_data;
+ }
+ $json_output = json_encode($payload);
+ $script = "var hphp_data_payload = " . $json_output . ";";
+
+ wp_add_inline_script('hphp-js', $script, 'before');
+}
+add_action( 'wp_enqueue_scripts', 'hphp_enqueue_assets' );
diff --git a/plugins/hphp/plugin.js b/plugins/hphp/plugin.js
@@ -0,0 +1,44 @@
+function hphpSetCounter(id, value) {
+ const element = document.getElementById(id);
+ if (element) {
+ element.textContent = value.toLocaleString('en-US');
+ return true;
+ }
+ return false;
+}
+
+function hphpInitAndHide(id) {
+ hphpInit()
+ const element = document.getElementById(id);
+ if (element) {
+ element.style.display = 'none';
+ }
+}
+
+function hphpInit() {
+ hphpLoadData(null);
+}
+
+function hphpLoadData(selector) {
+ if (typeof hphp_data_payload === "undefined") {
+ return;
+ }
+ if (selector !== null) {
+ if ("triggers" in hphp_data_payload) {
+ if (hphp_data_payload["triggers"].includes(selector)) {
+ return
+ }
+ }
+ }
+ if ("counters" in hphp_data_payload) {
+ const counters = hphp_data_payload["counters"]
+ for (const key in counters) {
+ const value = Math.floor(counters[key]) || 0;
+ hphpSetCounter(key + "_display", value);
+ }
+ }
+}
+
+jQuery(document).ready(function() {
+ hphpLoadData("onready")
+});
diff --git a/plugins/hphp/style.css b/plugins/hphp/style.css