I would like to get the current template file / line from a plugin function in smarty for profiling/debugging purposes.
Is it possible to do by using the passed $smarty object ?
something like this
function __smarty_add_javascript($params, $smarty){
Profiler::console('Add javascript #[FILE]#[LINE]');
Controller_Template_Base::getInstance()->javascripts[] = array_pop($params);
}
I am able to access the filename by using $smarty->source->filepath however i couldnt find any line information.
I don't think you can access the original template's properties from a plugin in runtime. But you can use the http://php.net/debug_backtrace function to access the caller place's informations. (Of course this information will be based on the compiled file, not the original. But It is better than nothing.)
Related
I editing custom page in wp-admin. Currently in editor i see that
[vc_column_text]Titile1[/vc_column_text]
After each [/vc_column_text] i need to display some info from database. How i can call php function with some parameters in wp-admin editor ?
Like this or so:
[vc_column_text]Titile[/vc_column_text]
[getInforFromDatabase('Titile1')]
This will take quite some doing. You just may not directly call a PHP function from within the WP-Admin Editor but you can create a simple, rudimentary Plugin to do the heavy-lifting for you. And then expose a short-code that you can use from within the WP Admin Editor.
To do this, first create a a Directory in your wp-content/plugins directory. For the purposes of demonstration, we will call this directory: dbInfoSifter but the name is completely up to you. In the end, the path to this directory would be: wp-content/plugins/dbInfoSifter
Now inside of this Folder (dbInfoSifter), create a PHP File with the same name again so that you have: wp-content/plugins/dbInfoSifter/dbInfoSifter.php. Now add the following Code inside the dbInfoSifter.php File:
<?php
/*
Plugin Name: DB Info Sifter
Plugin URI: your-domain.tld
Description: Simple Plugin to sift data From Database.
Author: Your Name
Author URI: http://your-domain.tld
Version: 1.0.0
*/
// THE COMMENTED LINES ABOVE INFORMS WORDPRESS THAT THIS IS A PLUGIN.
// SO IT KNOWS TO ADD IT TO THE PLUGINS LIST...
// WE SHALL REVISIT THIS SOONER THAN LATER...
// IN THIS FILE YOU SHOULD PUT ALL THE LOGIC
// FOR GETTING DATA FROM THE DATABASE OR DOING ANYTHING AT ALL
// HOWEVER, THE MOST IMPORTANT THING IS TO EXPOSE THE SHORT-CODE
// SO THAT WE CAN USE IT INSIDE THE WP-ADMIN EDITOR.
// WE CAN DO THAT WITH THE FOLLOWING LINES OF CODE...
add_shortcode('getInfoFromDatabase', 'dbsGetInfoFromDatabase');
// THE LINE ABOVE EXPOSES THE SHORT-CODE SO THAT YOU CAN CALL IT
// FROM YOUR WP-ADMIN EDITOR... THE ARGUMENTS ARE SPECIFIC:
// THE 1ST ARGUMENT IS THE NAME OF THE SHORT-CODE
// THE 2ND IS THE NAME OF THE FUNCTION TO RUN WHEN THIS SHORT-CODE IS CALLED.
// SO NOW, WE WRITE OUT THE FUNCTION ITSELF:
//THE $atts PARAM IS AN ARRAY OF PARAMETERS PASSED TO THE SHORT-CODE.
function dbsGetInfoFromDatabase($atts){
extract( shortcode_atts( array(
'title1' => "default_value", /*<= SET DEFAULT VALUE*/
'param2' => "default_value", /*<= SET DEFAULT VALUE*/
), $atts ));
// WITH THE extract FUNCTION YOU CAN NOW USE title1 AND param2
// AS NORMAL VARIABLES IN YOUR PROGRAMS LIKE $title1, $param2.
// SO THIS IS WHERE YOU BUILD YOUR LOGIC TO GET DATA FROM THE
// DATABASE & YOU CAN USE THE PARAMETERS TOO...
// YOU ARE NOT LIMITED TO THE NUMBER OF PARAMETERS TO USE
// AS WELL AS THE NAME OF THE PARAMETERS...
// THOSE ARE COMPLETELY UP TO YOU...
/* AND SO; YOUR LOGIC CONTINUES...*/
// IT IS HIGHLY IMPORTANT THAT THIS FUNCTION RETURNS A VALUE.
// MOSTLY LIKELY, THE TYPE WOULD BE A STRING
// (ESPECIALLY IF YOU WANT TO DISPLAY IT AUTOMATICALLY)
return $stringValueResultingFromDBTransactions;
}
That's all... nothing really special to it... But you can also have other Functions within this File that does something, anyways. However, the most important parts of this File (in your case) are: 1.) The function: dbsGetInfoFromDatabase($args) and 2.) The Comments at the Top of the File.
Now, inside the WP-Admin Editor; you can just simply reference this function using the short-code we created like so:
// WP-ADMIN EDITION (BACKEND)
[vc_column_text]Titile[/vc_column_text]
[getInfoFromDatabase title1='Titile1'] //<== CALL YOUR SHORT-CODE
Alternatively, You can do it like so:
//WP-ADMIN EDITION (BACKEND)
[vc_column_text]Titile[/vc_column_text]
[getInfoFromDatabase title1='Titile1'][/getInfoFromDatabase]
Both will achieve the same Result, but the First one seems more concise (to me). Take your pick.
Finally, you need to activate the Plugin at the Backend for this to work. So; navigate to your Plugins Section (at the Backend of Wordpress). You will notice a new Plugin called DB Info Sifter. Simply activate it and you are finally ALL DONE. Your short-code would now work as if you actually called a function and passed it the $title1 parameter.
I hope this helps you a little bit and gives you a head-start...
Good-Luck to you, my Friend...
I'm not so familiar with Smarty. In the code I'm exploring, I have found such construction:
...
Can't understand, how does that url construction work. Looks like it is some custom method (or whatever it's called) in our project. But the project is quite large and I can't find it's definition just by word url.
Where to look for? What can it be?
url - is a Smarty custom plugin, which you can find in smartys' plugin folder. a.category, a.subcategory, a.nice_url: are params that passed to the url.
The following:
{url ...}
url is the custom Smarty Function. You can find it in Smarty plug-in directory or in a file that keeps all functions (if there is one) for your project.
In case you want to find the ends, just search through your whole web-site directory for the following content:
smarty_function_url
It must be found anyway, because it's the only way you can register custom Smarty function.
EDIT 1:
As correctly stated by sofl, if the plug-in is registered dynamically using registerPlugin method:
$smarty->registerPlugin("function","url", ...)
then you would have to search for the following instead:
registerPlugin("function","url"
or
registerPlugin(" function ", " url "
If it still doesn't work just try searching for ->registerPlugin, I think there is no other options left after all and you will find it!
Is it possible to include a typoscript file via php?
Normally I would include typoscript with this:
<INCLUDE_TYPOSCRIPT: source="FILE:fileadmin/templates/typoscript/setup/1.ts">
But I want to do this just with php and not typoscript. Is that possible?
My Purpose: I want to dynamically load typoscript in my page
This can be achieved by invoking accordant functions at an early stage, e.g. in calling or delegating it in ext_localconf.php. For example, the bookstrap package is loading TypoScript in PHP like this:
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addPageTSConfig(
'<INCLUDE_TYPOSCRIPT: source="FILE:EXT:' . $_EXTKEY
. '/Configuration/PageTS/Mod/Wizards/newContentElement.txt">'
);
Please consider, that TypoScript is cached before the actual front-end rendering starts. This means, that you should not modify TypoScript if you're plugin class or controller logic has been called already.
May be you need to return a value from the php function and use typoscript conditions for choosing the typoscript file.
You might try the following (if I get you right):
$typoscriptFile .= file_get_contents($someFile);
$parser = t3lib_div::makeInstance('t3lib_TSparser');
$parser->parse($typoscriptFile);
$tsArray = $parser->setup;
I really don't know how well that will play with anything related to global typoscript though.
If you wanted a complete correct parse, you might be able to pull something like this off if you populated a fresh t3lib_TStemplate instance from $GLOBALS['TSFE']->tmpl and than ran the code above. Might work, never tried.
How can a get a object variable from a remote XML file, for example this one ?
In PHP5 it works fine with simplexml_load_file(), but I need this to work in PHP 4 also. How can I do that?
Or is there any built-in WordPress function that can load xml files? I tried using the WP's SimplePie class, but I get a weird object variable (with incorectly named fields etc).
If you want to use the smipleXML functions in PHP4 include this class ( I have used it and it works like a charm)
http://www.phpclasses.org/package/4484-PHP-Load-XML-files-in-PHP-4-like-SimpleXML-extension.html
I would like to know how to create a php function that can be installed in php
just like the already built in functions like :
rename
copy
The main point I would like to achieve is a simple php function that can be called from ANY php page on the whole host without needing to have a php function within the php page / needing an include.
so simply I would like to create a function that will work like this :
location();
That without a given input string will output the current location of the file via echo etc
Well, there are a couple of options here. One of them is to actually extend the language by writing an extension. You'd have to muck around with the PHP source code, write it in C, and deal with the Zend Engine internally. You probably wouldn't be able to use this on a shared host and it would be quite time consuming and probably not worth it.
What I would do is put all of your functions into a separate PHP file, say helper_functions.php. Now, go into your php.ini and add the directive: auto_prepend_file = helper_functions.php. This file should be in one of the directories specified in your include_path (that's a php.ini directive too).
What this does is basically automatically put include 'helper_functions.php'; on every script. Each and every request will have these functions included, and you can use them globally.
Read more about auto_append_file.
As others have said, there's probably an easier, better way to do most things. But if you want to write an extension, try these links:
http://docstore.mik.ua/orelly/webprog/php/ch14_01.htm
http://www.tuxradar.com/practicalphp/2/3/0
So you want to extend PHP's core language to create a function called location(), written in C, which could be done in PHP by:
echo __FILE__;
Right. Have fun doing that.