Skip to content

2.x #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014 sleepy-mustache
Copyright (c) 2020 sleepyMUSTACHE

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# HTML Compress Module
# URLClass Module

* Date: March 20, 2018
* Date: March 2, 2020
* Author: Jaime A. Rodriguez <[email protected]>
* Version: 1.1
* Version: 2.p
* License: http://opensource.org/licenses/MIT

Adds a class to the HTML tag to the "{{ urlClass }}" placeholder allowing you to target pages via CSS using the URL. It replaces spaces and forward slashes with dashes to make it URL friendly. It adds "-index" to the end of the class if the page is the default page in that directory.
Expand All @@ -17,6 +17,8 @@ Adds a class to the HTML tag to the "{{ urlClass }}" placeholder allowing you to

## Changelog

### Version 1.1
### Version 2.0
* Updated

### Version 1.1
* Bugfix: Trailing slashes are now properly converted to dashes
114 changes: 70 additions & 44 deletions urlclass.php
Original file line number Diff line number Diff line change
@@ -1,60 +1,86 @@
<?php
namespace Module\URLclass;

/**
* Generates a class based on the URL.
* URLClass Module
*
* The URLClass module adds a class to the placeholder {{ urlclass }} that can be
* used to uniquely identify a page in css.
*
* This way we can target this page with css. Specifically it takes the folder
* and filename and replaces the directory separator with a hyphen, e.g.
* *\user\login\* will translate into *user-login-index*. The index added to the
* end if we are using the default page. *\user\login.php* will translate into
* *user-login*.
* PHP version 7.0.0
*
* @return string The class name
* @internal
* @category Module
* @package Sleepy
* @author Jaime A. Rodriguez <[email protected]>
* @license http://opensource.org/licenses/MIT; MIT
* @link https://sleepymustache.com
*/
function render() {
// Get the current URL
$url = \Sleepy\Hook::addFilter('urlclass_url', $_SERVER['REQUEST_URI']);

// Remove the parameters
if ($parameters = strlen($url) - (strlen($url) - strpos($url, '?'))) {
$url = substr($url, 0, $parameters);
}
namespace Module\UrlClass;

use \Sleepy\Core\Hook;
use \Sleepy\Core\Module;

/**
* The URLClass module
*
* @category Module
* @package Sleepy
* @author Jaime A. Rodriguez <[email protected]>
* @license http://opensource.org/licenses/MIT; MIT
* @link https://sleepymustache.com
*/
class UrlClass extends Module
{

public $hooks = [
'render_placeholder_urlclass' => 'convert'
];

/**
* Convert the placeholder into the URLClass
*
* @param string $url The current URL
*
* @return void
*/
public function convert($url)
{
// Get the current URL
$url = Hook::addFilter('urlclass_url', $_SERVER['REQUEST_URI']);

// Remove first slash
if (strpos($url, '/') == 0) {
$url = substr($url, 1, strlen($url) - 1);
}
// Remove the parameters
if ($parameters = strlen($url) - (strlen($url) - strpos($url, '?'))) {
$url = substr($url, 0, $parameters);
}

// Slashes become dashes
$url = str_replace('/', '-', $url);
// Remove first slash
if (strpos($url, '/') == 0) {
$url = substr($url, 1, strlen($url) - 1);
}

// Slashes become dashes
$url = str_replace('/', '-', $url);

// If it doesn't end in php, then add default page
if (!strpos($url, '.php')) {
// Add trailing slash
if (substr($url, -1) !== '-' && strlen($url)) {
$url .= '-';
}
// If it doesn't end in php, then add default page
if (!strpos($url, '.php')) {
// Add trailing slash
if (substr($url, -1) !== '-' && strlen($url)) {
$url .= '-';
}

$url = $url . \Sleepy\Hook::addFilter('urlclass_default', 'index');
} else {
$url = substr($url, 0, strlen($url) - 4);
}
$url = $url . Hook::addFilter('urlclass_default', 'index');
} else {
$url = substr($url, 0, strlen($url) - 4);
}

if (empty($url)) {
$url = \Sleepy\Hook::addFilter('urlclass_default', 'index');
}
if (empty($url)) {
$url = Hook::addFilter('urlclass_default', 'index');
}

// XSS Prevention
$url = preg_replace('/[^A-Za-z0-9\-]/', '', $url);
// XSS Prevention
$url = preg_replace('/[^A-Za-z0-9\-]/', '', $url);

return \Sleepy\Hook::addFilter('urlclass_class', $url);
return Hook::addFilter('urlclass_class', $url);
}
}

// Apply SM Hooks
\Sleepy\Hook::applyFilter(
'render_placeholder_urlclass',
'\Module\URLclass\render'
);
Hook::register(new UrlClass());