Helper Methods

The Onyx Theme is equipped with several help functions for the development of the project.

These functions are \Onyx\Helpers class static methods, aliased to \Onyx\O.

info

To solve problems with intelisense, always use the alias O for the Helpers class.

use Onyx\Helpers as O;
// Exemplo
\Onyx\O::conf('app')->version;

Methods

O::conf()

Returns environment variables. Use with caution.
The return only works if the file has already been previously loaded by \Onyx\O::load()

\Onyx\O::conf( $name = false )

ParamTypeDescriptionRequired
$namestringConfiguration file*

@return object|false

Configuration variable: any file in the ./core/config/ folder can be the name
Ex: app | assets | hooks | images | mail | support

Check the files to return.

// Example of use
namespace Onyx;
O::conf('app') // All parameters in the `./core/config/app.php` file
O::conf('app')->version; // 1.0
O::conf('app')->dir_uri; // http://site.onyx.local/app/themes/onyx
O::conf('app')->desc; // My Site Description
O::conf('assets')->css; // Objeto|Array loaded styles
O::conf('hooks')->filters['add']; // Objeto|Array added filters

O::load()

Load configuration files. The file must return an array.
Method used to setup the theme by the class \Onyx\Setup

\Onyx\O::load( $file, $obj = true )

ParamTypeDescriptionRequired
$filestringFile Name
$objbooleanReturn as object

@return array|object|false

File Name: the file must be in the ./core/config/ folder

// Example of use
namespace Onyx;
$this->app = O::load( 'app' );
$this->assets = O::load( 'assets' );
$this->hooks = O::load( 'hooks' );
$this->images = O::load( 'images' );
$this->mail = O::load( 'mail' );
$this->sidebars = O::load( 'sidebars' );
$this->support = O::load( 'support' );
$this->cpts = O::load( 'cpts' );
$this->taxs = O::load( 'taxonomies' );

O::set_hierarchy()

Set hierarchy of templates loaded at boot.

O::set_hierarchy( $hierarchy = [] )


O::get_hierarchy()

Capture hierarchy of templates loaded at boot.
Put the suffix Controller in the file name to get the file from the controller.

// Example of use
var_dump( O::get_hierarchy() );

O::array_filter_keys()

Filter multidimensional array recursively by removing the keys passed as a parameter.

O::array_filter_keys( $arr, $filter )

ParamTypeDescriptionRequired
$arrarraySource array
$filterarrayKeys to remove

@return array

// Example of use
namespace Onyx;
$app = [
'version' => '1.0',
'rest' => 'wp-json',
'user' => wp_get_current_user()->user_email,
'devs' => [ 'dev@domain.tld' ],
'uploads' => [
'max_file_size' => 5000,
'unset_types' => [ 'mp4|m4v', 'wmv', 'avi', 'mpeg|mpg|mpe', 'mkv' ],
],
];
$remove = [ 'user', 'devs', 'unset_types' ];
$app_filtered = O::array_filter_keys( $app, $remove );

O::is_amp()

Method for working with the AMP plugin. Checks whether the current page is a AMP page.

@return bool

// Example of use
namespace Onyx;
if (O::is_amp()) {
echo "This is an AMP page";
}

O::valid_url()

Validate a url from a string.

O::valid_url( $uri )

ParamTypeDescriptionRequired
$uristringURL Address

@return bool

// Example of use
namespace Onyx;
if (O::valid_url('https://github.com')) {
echo "This is a valid URL";
}

O::static_path()

Returns the asset url path based on the file location
whether it is a remote or local file. Uses the method static_uri.

O::static_path( $file )

ParamTypeDescriptionRequired
$filestringFile location or a URL

@return string

// Example of use
namespace Onyx;
O::static_path( 'assets/css/home.css' ) // http://site.onyx.local/app/themes/onyx/assets/css/home.css

O::static_uri()

Returns the assets directory according to the ONYX_STATIC environment constant
(defined in the static parameter at ./core/config/app.php).

The subdomain pattern will be: //{$subdomain}.domain.tld/THEME_FOLDER/assets

info

To use the constant ONIX_STATIC you will need to configure a cdn or a subdomain pointing to the WordPress themes folder on your server.

caution

Prefer to use O::static_path()
Use this method if you want to customize the return in a specific function. For more information see the static_path() method declaration in the Helpers.php file

// @return string
O::static_uri( $uri, $subdomain = 'static' )

O::get_img()

Return an image from the theme directory

O::get_img( $img, $title = null, $w = null, $h = null )

ParamTypeDescriptionRequired
$imgstringImage Path
$titlestringImage Title
$wstringImage Width
$hstringImage Height

@return string

// Example of use
namespace Onyx;
$img = O::get_img( 'assets/images/logo.jpg' );

O::img()

O::get_img() alias but prints on the screen

O::img( $img, $title = null, $w = null, $h = null )

@echo string


O::css()

Print <link rel="stylesheet" href="{$css}"> tag

O::css( $css, $home = false )

ParamTypeDescriptionRequired
$cssstringCSS path or URL
$homeboolAppear only on Home

@echo string

// Example of use
<head>
\Onyx\O::css( 'assets/css/main.css' );
</head>

O::js()

Print <script src="{$js}"></script> tag

O::js( $js, $home = false )

ParamTypeDescriptionRequired
$jsstringJavascript path or URL
$homeboolAppear only on Home

@echo string

// Example of use
<head>
\Onyx\O::js( 'assets/js/app.js' );
</head>

O::livereload()

Method to call the Live Reload script (see gulpfile.js).

O::livereload( $port = 3010 )

@return void|false


O::gtag()

Add HTML for google analytics script (main method)
Uses gtag.js from Google

O::gtag( $uax, $script = false )

ParamTypeDescriptionRequired
$uaxstringGoogle UA ID
$scriptboolLoad the script https://www.googletagmanager.com/gtag/js?id=$uax

@echo <script />

// Example of use
<footer>
\Onyx\O::gtag( 'UA-XXXXXXX-X', true );
</footer>

O::analytics()

Add HTML for google analytics script (legacy)
Uses analytics.js from Google

O::gtag( $uax, $script = false )

ParamTypeDescriptionRequired
$uaxstringGoogle UA ID
$scriptboolLoad the script https://www.google-analytics.com/analytics.js

@echo <script />

// Example of use
<footer>
\Onyx\O::analytics( 'UA-XXXXXXX-X', true );
</footer>

O::print()

Print an app enviroment parameter (string)

O::print( $key )

@echo string

// Example of use
namespace Onyx;
O::print( 'version' ); // 1.0

O::is_dev()

Checks whether the logged in user is a developer Use with caution, method used only for development/debugging

O::is_dev()

@return bool

// Example of use
namespace Onyx;
if ( O::is_dev() ) {
echo "Is a developer";
}
info

Add developer emails at ./core/config/app.php


WordPress

O::section_title()

Return the title of the current section, depending on the route type of the WordPress section

O::section_title( $echo = true, $prefix = '' )

ParamTypeDescriptionRequired
$echoboolPrint or return
$prefixstringPrefix

@return void|string

/**
* Example of use. Dependendo da área, pode retornar:
* post_type_archive_title()
* single_cat_title()
* single_tag_title()
* get_the_author()
* single_term_title()
*/
<h1>
\Onyx\O::section_title();
</h1>

O::section_type()

Returns the page type in WordPress as string. Ex: is_page, is_home, is_archive etc...

O::section_type()

@return string


O::menu()

Show navigation menu

O::menu( $menu = null )

ParamTypeDescriptionRequired
$menustringMenu Name

@return void

// Example of use
<ul>
\Onyx\O::menu('Top Menu');
</ul>

O::pagenavi()

Onyx Pagenavi. Show posts/pages pagination menu

O::pagenavi( $query = null )

ParamTypeDescriptionRequired
$queryobjectQuery Object

@return void

info

If no query parameters are passed, it will use the global WordPress query

// Example of use
<div class='pagination'>
\Onyx\O::pagenavi();
</div>