How do you add a lib namespace to Wordpress roots sage? - php

Following the documentation written here, I added a file to lib directory containing:
<?php
namespace Theme\URIs;
/**
* Returns theme images directory uri
*/
function get_images_directory_uri() {
return get_template_directory_uri() . '/assets/images';
}
Which I then tried calling from a template file with:
<?php print Theme\URIs\get_images_directory_uri(); ?>
Calling this however, returned a Fatal error: Call to undefined function Theme\URIs\get_images_directory_uri().

The solution is to add the file to the $sage_includes array in functions.php, so it looks like this:
$sage_includes = [
'lib/assets.php', // Scripts and stylesheets
'lib/extras.php', // Custom functions
'lib/setup.php', // Theme setup
'lib/titles.php', // Page titles
'lib/wrapper.php', // Theme wrapper class
'lib/uris.php' // Project URIs
];
This imports the file to be used in the theme.
The present documentation does not explain this. (I assumed it simply imported all files in the libs directory...)

Not the question (I can't comment yet) but unless you've changed the directory structure your function should probably return:
get_template_directory_uri() . '/dist/images';
As the images in /assets/images get optimised and then copied to /dist/images.

Related

What are possible reasons for bonfire Assets::add_module_css to not include file?

I have a primary controller located in secure/application/modules/gps/controllers that has a constructor that looks like this:
public function __construct()
{
parent::__construct();
$this->load->model('gps_model');
Assets::add_module_js('gps', 'gps.js');
Assets::add_module_css('gps','gps.css');
if($this->input->get('clear') != false){
$this->session->sess_destroy();
}
}
My CSS file that I am trying to include is located in the folder secure/application/modules/gps/assets/css. The code executes fine without warning, but the CSS file does not get included for any methods. Is there a configuration setting the may override the assets directory, or is there some other reason it's not being found/added? (The JS file is not being added either. The bonfire base CSS files (screen.css) IS getting loaded fine.
We found the solution to our particular problem.
The assets/cache directory did not exist. Once the server could write (it must exist and be writeable!) to the [document_root]/bonfire/public/assets/cache directory all was good.
This is not a solution, but I have faced the same problem and found help with the below information.
It'll added in your page but can you just look on ctrl+u source where bonfire will auto rename your file.
For example : In my code I have added id_proof_master.css file like below.
public function __construct()
{
parent::__construct();
$this->auth->restrict($this->permissionView);
$this->load->model('id_proof_master/id_proof_master_model');
$this->lang->load('id_proof_master');
$this->form_validation->set_error_delimiters("<span class='error'>", "</span>");
Template::set_block('sub_nav', 'master/_sub_nav');
Assets::add_module_js('id_proof_master', 'id_proof_master.js');
Assets::add_module_css('id_proof_master', 'id_proof_master.css');
}
And it's working fine but when I have check in source view (ctrl+u) it will show file name like "id_proof_master_master_mod.min.css" so can you just check it out in source; maybe it'll show you with some other name like my file.

Can't add new Smarty plugins

Directory structure:
smarty
------plugins
------myplugins
---------------function.myfnc.php
in file function.myfnc.php:
function smarty_function_myfnc($params, &$smarty) {
///code here
}
add plugin:
$smary->addPluginsDir(/path/to/myplugins);
but, when i call in file display.tpl:
{myfnc p="value"}
this is error:
Call to undefined function smarty_function_myfnc()
somebody can help me?
Probably problem is your path to your plugins dir. You should add before your myplugins the same path as you use for requiring Smarty.class.php.
For example I have code:
<?php
require 'smarty/Smarty.class.php';
$smarty = new Smarty;
$smarty->setTemplateDir('templates');
$smarty->addPluginsDir('smarty/myplugins');
var_dump($smarty->getPluginsDir());
echo $smarty->fetch('temp.tpl');
At the beginning I have require 'smarty/Smarty.class.php'; - my path to Smarty.class.php is smarty so when I have folder myplugins which is inside smarty folder as in your case I need to use smarty/myplugins and not only myplugins.
And then I have templates/temp.tpl file with content:
{myfnc p="value"}
and file smarty/myplugins/function.myfnc.php with content:
<?php
function smarty_function_myfnc($params, Smarty_Internal_Template $template)
{
return strtoupper($params['p']);
}
?>
And output for template is VALUE with capital letters as defined in my function.
So as you see it works without a problem.

Import an HTML file from inside a Twig template

I want to import an HTML file from inside a Twig template. The HTML file is located at /var/files/5 (with no extension). And I render the template like this:
$path = $_SERVER['DOCUMENT_ROOT'] . '/../var/files/5';
$content = $this->get('templating')->render('ProConvocationBundle:Default:definitive-view.html.twig', array('path' => $path));
Inside the Twig template I import the HTML file like this:
{% include path %}
but it is not finding the path: Unable to find template "/myDocumentRoot/../app/var/files/5"
I've also tried several relative paths without success. Any idea of how to achieve it?
What causes this exception?
After digging in the Twig code a little, the following seems to cause this exception:
Twig tries to load the file from a known path/namespace, being bundle names (like /var/www/myApplication/src/AcmeBundle/Resources/views) and the app path being myApplication/app/Resources/views). Anyway it doesn't accept absolute paths, since it always tries to add a known path to the beginning of the given file.
<?php
// Twig/Loader/Filesystem.php
class Twig_Loader_Filesystem {
// ...
protected function findTemplate()
{
// ...
foreach ($this->paths[$namespace] as $path) {
if (is_file($path.'/'.$shortname)) {
return $this->cache[$name] = $path.'/'.$shortname;
}
}
throw new Twig_Error_Loader(sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths[$namespace])));
}
So it basically isn't possible to include a file by an absolue path, like in your example.
How to solve this?
You've got a bunch of possibilities to achieve this behaviour:
Add the path to the template loader
See post by #Adam Elsodaney
Move the file
You could simply move your file from app/var/files to app/Resources/views/var/files and use the path var/files/5 to include the file. This is probably not a suitable solution, since you want to keep those files in place.
Write a Twig Extension
You could write your own extension that provides a function named something like include_absolute() that simply returns file_get_contents($yourPath).
More on Twig extensions: http://symfony.com/doc/current/cookbook/templating/twig_extension.html
Be aware that you would might need to add the |raw filter to the output of the Twig function since a lot of stuff gets escaped everywhere.
In Symfony, you should make everything relative to the Kernel root directory which is the app directory.
$uploadedTemplatesDir = $this->get('kernel')->getRootDir() . '/../var/files';
Then add this to your templating loader
$this->get('twig.loader')->addPath($uploadedTemplatesDir);

Classload accessing with PHP - is it possible to load the same class from 2 different resources?

I am just wondering, I actually decided to go down a different route.
I create this file A
C:\somefolder\templates\mytemplate\joomlaoverwrites\libraries\joomla\document\html\renderer\head.php
which overwrites a joomla library file:
C:\somefolder\libraries\joomla\document\html\renderer\head.php
I use the overwrite by using
require_once(__DIR__ . '/joomlaoverwrites/libraries/joomla/document/html/renderer/head.php');
in my index.php of my template
This actually works.
What I now want to do is use my file just as a wrapper class, and sneak my changes into it before returning the answer. E.g.:
public function fetchHead($document)
{
$joomlasOriginalHead = callOriginalFunctionFetchHeadFromTheJoomlaImplementation();
//do my changes to joomlas original answer and return that
}
E.g. from that overwrite, I want to call the original file. Is that somehow possible?

include function xy() of a wordpress plugin

I want to build a WordPress admin dashboard widget which should return some information from another plugin.
This dashboard widget should read the functions of this plugin here:
http://plugins.svn.wordpress.org/wp-document-revisions/trunk/wp-document-revisions.php
So my code is:
include (WP_PLUGIN_URL.'/wp-document-revisions/wp-document-revisions.php');
$file_type = get_file_type('3');
But this doesn't work. These are the errors:
Fatal error: Call to undefined function add_action() in
/.../wp-content/plugins/wp-document-revisions/wp-document-revisions.php
on line 26
Fatal error: Call to undefined function get_file_type() in
/.../wp-content/plugins/dashboard-widget/dashboard-widget.php
Can anyone tell me how I have to do this, that I can read the function get_file_type('3')?
I'm assuming you are navigating straight to your PHP file in the wp-content/plugins/ folder rather than using a stub to create a URL. If this is the case, then you probably forgot to include wp-load.php
You should not include the plugin file (since the plugin may not be installed or activated).
Instead of this you should check if the function exists:
if (function_exists('get_file_type')) {
$file_type = get_file_type('3');
// rest of the code of the widget
}
It sounds like you are trying to access the PHP file directly, rather than going through WordPress proper. You should create a plugin, and hook into the Dashboard Widgets API.
As for implementation with WP Document Revisions, you've got two options. As of version 1.2, get_documents() and get_document_revisions() are two global functions that will be accessible anytime after the plugins_loaded hook. The FAQ has a bit more info, but they basically function like the native get_posts() function.
Alternatively, the class should be available globally as $wpdr. So if you wanted, for example, to call get_latest_version( 1 ) it'd be $wpdb->get_latest_version( 1 ).
Both assume the plugin's already activated. If you simply include the file, you're going to get an error that you're trying to redeclare the WP_Document_Revisions class.
If you do create a dashboard, I'd love to include it in future releases of the plugin!

Categories