Mecha CMS

Mecha CMS blog and documentation.

Version 1.2.7

Updated: Sunday, 07 August 2016

Changelog in Mecha 1.2.7.

Constants

Added // prefix on all *_LIBRARY_PATH constants.

define('JS_LIBRARY_PATH', '//example.com/foo.min.js');

Recommended way to load external libraries using the Asset class:

// `<script src="//example.com/foo.min.js"></script>`
echo Asset::javascript(JS_LIBRARY_PATH);
// `<script src="http://example.com/foo.min.js"></script>`
echo Asset::javascript($config->scheme . ':' . JS_LIBRARY_PATH);

Mecha will try to fix broken URL protocol like http://://example.com, http:////example.com and http:example.com that mostly caused by the old format in the previous versions.

Fields

Added options Field Type

Just like option field type, but users can select more than one option with this field type. Field type aliases like t for text, s for summary and b for boolean are now completely removed because of that option and options field type which have the same o prefix.

Added attributes and parser Property for Field

Make it possible to set custom HTML attributes in custom fields, and also to set custom function to validate user input on data submission.

Related: Field

Classes

Config::load()

Added scheme property as alternative to protocol without the :// part.

PropertyValue
schemehttp
protocolhttp://

Image() and Package()

In the previous versions, Image() and Package() class will return a warning message if the extensions required by the class are not installed. Now, this class will simply return false if the current web server does not support GD and Zip extensions:

if($image = Image::take('photo.png')) {
    $image->resize(40, 40)->saveAs('avatar.png');
} else {
    // Abort here…
}
if($package = Package::take('path/to/folder')) {
    $package->pack('my-package.zip');
} else {
    // Abort here…
}

Converter::strEval()

Characters like \n, \r, \s, \t, \v will no longer be translated into their original characters. To disable this behaviour, wrap your string with double quotes to indicates that the string is an encoded JSON string:

var_dump(
    Converter::strEval('foo\nbar\nbaz'),
    Converter::strEval('"foo\nbar\nbaz"')
);

Mecha::O()

Added second parameter to determine whether you want to disable the conversion on arrays with numeric keys or not. Default is true:

$test = array(
    'a' => array(
        'foo' => 1,
        'bar' => 1,
        'baz' => 1,
        'qux' => 1
    ),
    'b' => array('foo', 'bar', 'baz', 'qux'),
    'c' => array('foo', 'bar', 'baz', 'd' => 'qux')
);

$test = Mecha::O($test, true);

var_dump(Text::parse($test, '->encoded_json'));

Result:

{
  "a": {
    "foo": 1,
    "bar": 1,
    "baz": 1,
    "qux": 1
  },
  "b": ["foo", "bar", "baz", "qux"],
  "c": {
    "0": "foo",
    "1": "bar",
    "2": "baz",
    "d": "qux"
  }
}

Shortcodes

Custom Parser Function

Added ability to set custom parser function in the value field to parse the shortcode instead of using normal text value. Prefix your value with a ~ to indicate that the value is a function name:

Default Parser
PatternValue
{{foo}}<span class="foo">foo</span>
{{foo text="%s"}}<span class="foo">$1</span>
Custom Parser
PatternValue
{{foo text="%s"}}~do_shortcode_foo

Then you can create the required function to parse the shortcode in functions.php:

function do_shortcode_foo($data) {
    return $data[1] === '1' ? 'foo is one' : 'foo is ' . $data[1];
}

The $data variable will hold all of the matching pairs generated by shortcode wildcard.

Shortcode Escapes

Escaping shortcodes is now becomes easier because Mecha has added a special hook internally to encode the {{ and }} characters on all shortcodes wrapped in backtick characters. That way you can set custom filter for shortcodes with simple string replace function or with regular expression without having to check whether the shortcode is wrapped in backtick characters or not.

Prior to version 1.2.7:

Filter::add('shortcode', function($content) {
    return preg_replace('#(?<!`)\{\{dump\}\}(?!`)#', '<mark>work!</mark>', $content);
});

In version 1.2.7, you can omit the look-behind and look-ahead parts from the regular expression:

Filter::add('shortcode', function($content) {
    return preg_replace('#\{\{dump\}\}#', '<mark>work!</mark>', $content);
});

Related: Common Problems in Making the Shortcode Parser

Behaviour Changes in Default Shortcodes

The default shortcode data are now moved from lot\workers\repair.state.shortcode.php to a plugin. They are stored in lot\plugins\shortcode\workers\shortcodes.php.

It is also advisable to change your old habits on writing URL shortcodes from <a href="{{url}}foo"> to <a href="{{url:foo}}"> because the first example is considered deprecated.

Related: Behaviour Changes in Default Shortcodes

Others

Added

  • Filter
    • article:input
    • article:output
    • comment:input
    • comment:output
    • page:input
    • page:output
  • Method
    • Asset::css() ← alias for Asset::stylesheet()
    • Asset::gif() ← alias for Asset::image()
    • Asset::jpeg() ← alias for Asset::image()
    • Asset::jpg() ← alias for Asset::image()
    • Asset::js() ← alias for Asset::javascript()
    • Asset::png() ← alias for Asset::image()
    • Cell::apart()
    • Cell::unite() ← alias for Cell::unit()
    • File::hidden()
    • Image::save()
    • Mecha::concat()
    • Mecha::version()
    • Plugin::version()
    • Shield::version()
  • Parser
    • to_unite_entity
  • Checker
    • is_json
    • is_serialize

Removed $connector Parameter

The $connector parameter was introduced in the previous versions which is available as a way to set custom path connector between home URL and post slug. It is available in the Get::post(), Get::response() and its alternative methods:

  • http://mecha-cms.com/2016/post/post-slug ← the connector is /post/
  • http://mecha-cms.com/2016/post-slug ← the connector is /
Get::post($path, $excludes, $folder, $FP, $connector)
Get::postHeader($path, $folder, $FP, $connector)
Get::postAnchor($path, $folder, $FP, $connector)

Get::response($path, $excludes, $folder, $FP, $connector)
Get::responseHeader($path, $folder, $FP, $connector)
Get::responseAnchor($path, $folder, $FP, $connector)

But now the $connector parameter is removed. Starts from version 1.2.7, your path connector will be hold by $config->index->slug value which you can set it dynamically during method creation.

Prior to version 1.2.7:

Get::plug('event', function($path, $excludes = array()) {
    return Get::post($path, $excludes, EVENT, 'event:', '/event/');
});

In version 1.2.7:

Get::plug('event', function($path, $excludes = array()) {
    $c = Config::get('index.slug'); // cache the original connector value
    Config::set('index.slug', 'event'); // set a custom connector value
    $post = Get::post($path, $excludes, EVENT, 'event:');
    Config::set('index.slug', $c); // reset to the previous value
    return $post;
});

Info: Please update your event plugin.

Custom Minifier

You can now override the default HTML, CSS and JavaScript minifier as the way you create a method:

// Custom HTML minifier
function do_custom_html_minifier($input) {
    return preg_replace('#>\s*<#', '><', $input);
}

Converter::plug('detractSkeleton', 'do_custom_html_minifier');

Download Mecha v1.2.7

Donation and Email Subscription