Skip to content

Instantly share code, notes, and snippets.

Avatar

YJ Tso sepiariver

View GitHub Profile
View classnames.snippet.php
<?php
$input = $modx->getOption('input', $scriptProperties, '', true);
$output = $modx->getOption('options', $scriptProperties, '', true);
$classnames = explode(' ', $input);
$results = [];
foreach ($classnames as $token) {
// Strip out any % encoded octets
$normalized = preg_replace('|%[a-fA-F0-9][a-fA-F0-9]|', '', $token);
// Replace _ with - and lowercase for old times' sake
View test.txt
.box
{
text-align:center;
padding:0px;
box-shadow:none;
}
.thing,.box-header,.box-footer,.box-bit
{
background:#39f;
View keybase.md

Keybase proof

I hereby claim:

  • I am sepiariver on github.
  • I am sepiariver (https://keybase.io/sepiariver) on keybase.
  • I have a public key ASD1X6kMkDJf-qfi4YYBtldsTf2jTVPO46PCj5ur4w6sSQo

To claim this, I am signing this object:

@sepiariver
sepiariver / inSection.snippet.php
Last active Dec 21, 2017
Detects if the current MODX Resource is in a predefined list of "sections", and if so returns a string (e.g. a CSS classname).
View inSection.snippet.php
<?php
// The Resource ID to test
$id = (int) $modx->getOption('id', $scriptProperties, $modx->resource->get('id'), true);
// Option to return early if Current Resource matches $id
$matchCurrent = $modx->getOption('matchCurrent', $scriptProperties, true);
// IDs of Resources deemed as "sections", against which to test
$sectionIds = array_filter(array_map('trim', explode(',', $modx->getOption('sectionIds', $scriptProperties, ''))));
// IDs of parent Resources, the direct children of which will be added to $sectionIds
$sectionParentIds = array_filter(array_map('trim', explode(',', $modx->getOption('sectionParentIds', $scriptProperties, ''))));
// If the test passes, meaning the Resource has a $sectionId as an ancestor, output this
@sepiariver
sepiariver / readtime.snippet.php
Last active Dec 21, 2017
Reading time MODX output filter.
View readtime.snippet.php
<?php
$options = (int) $modx->getOption('options', $scriptProperties, 200, true); // 200 words per minute
$wordCount = (int) str_word_count(strip_tags($input)); // To display word count set options to 0
if ($options === 0) return $wordCount;
return ceil($wordCount / abs($options));
@sepiariver
sepiariver / client_preview.plugin.php
Created Feb 6, 2017
MODX Client Preview Plugin
View client_preview.plugin.php
<?php
if ($modx->context->get('key') === 'mgr') return;
$key = $modx->getOption('key', $scriptProperties, 'my-secret-key');
$val = $modx->getOption('val', $scriptProperties, 'my-secret-value');
switch ($modx->event->name) {
case 'OnHandleRequest':
if (!$modx->getOption('site_status')) {
if ($_GET[$key] === $val) {
$modx->config['site_status'] = 1;
@sepiariver
sepiariver / filterpathsegment.snippet.php
Created Jan 24, 2017
Use the MODX filterPathSegment method to filter a string value into valid URL syntax. Useful for HTML attribute values as well.
View filterpathsegment.snippet.php
<?php
$options = $modx->getOption('options', $scriptProperties, $modx->getOption('friendly_alias_restrict_chars_pattern'), true);
return $modx->filterPathSegment($input, array('friendly_alias_restrict_chars_pattern' => $options));
@sepiariver
sepiariver / quicklist.snippet.php
Last active Dec 21, 2017
Fast snippet for listing MODX Resources. Outputs generic HTML list with link to each Resource. Not template-able except wrapper.
View quicklist.snippet.php
<?php
/**
* quickList
*
* Lists Resourcs super fast. Uses code by Garry Nutting of the MODX Core Team.
*
* @author YJ Tso <yj@modx.com>, Garry Nutting <garry@modx.com>
*
*
* quickList is free software; you can redistribute it and/or modify it
@sepiariver
sepiariver / 0_reuse_code.js
Created Jul 21, 2016
Here are some things you can do with Gists in GistBox.
View 0_reuse_code.js
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
View getResourcesColumns.snippet.php
<?php
/**
* getResourcesColumns
* @author @sepiariver
*
* &parent optional ID of Resource from which to fetch children. Defaults to current resource.
* &fields optional Resource field(s) from which to fetch values. Defaults to 'pagetitle,longtitle,introtext,parent,hidemenu'.
* &columns optional Number of columns to sort Resources into and wrap with colWrapper_n;
* &columnDirection optionalVertical or horizontal column sorting. Defaults to horizontal
* &depth optional Depth to seek children via getChildIds(). Defaults to 1
You can’t perform that action at this time.