get_option, intval, datatype - php

My scripts run if I do:
$config['max'] = 20;
It doesn't run if I use the configuration-value of my plugin like
$config['max'] = get_option('plugin_max');
Also not like
$config['max'] = intval(get_option('plugin_max'));
In every case, if I print $config['max'], it prints
20
I think there must be some stupid datatype-mismatch, but I can't figure out what it could be.
Solution
ok, I have to clear this up and maybe it will help someone later on. The problem was that get_option was undefined because it was called directly in an AJAX-request. I now routed the ajax-requests properly over WPs admin-ajax.php function so that WP-functions are available to the plugin even when called from AJAX.
Strangely, while debugging, the function was available, but this might have been while initialising the plugin, which is of course no AJAX-request but entirely server-side and so inside the scope of WP by default.
Thanks for your support!

Related

Disable Cache in TYPO3 in a USER_INT function

I am working on a TYPO3 project where I have to dynamically disable caching based on a condition. It is a very specific usecase, that will not happen a lot.
I planned to use a USER_INT function, where I would perform the check and disable the cache if necessary. The USER_INT function works flawlessly, it is being called on every page load.
The thing is, I can not disable the cache, or at least I do not know how.
The code, I have right now:
page = PAGE
page {
typeNum = 0
adminPanelStyles = 0
11 = USER_INT
11.userFunc = [COMPANY_NAMESPACE]\PageHandler->checkCache
And in the function I perform the check:
public function checkCache($content,$conf){
global $TSFE;
$id = $TSFE->id;
if($this->checkIfDisableCache($id)){
//$TSFE->set_no_cache(); // <---- first I tried this one
$TSFE->no_cache=true; // <-----after a while I got despoerate and tried to disable it directly
}
}
I also tried to play with the config, it did not work.
The funny thing is, if I set it directly in typoscript:
config.no_cache = 1
it works, but since the check is rather complex, I want to use PHP to determine, if the cache should be disabled.
I know I am doing something wrong, I just don't know what. Any help would be appretiated :)
I don't think either of the previous answers really explain the situation. You have sort of a catch-22 here, in that your USER_INT is executed after the page cache entry has been generated. The way it works internally is everything that can be cached gets rendered first, and every USER_INT then outputs a marker in the HTML source which gets replaced afterwards. This way the cache can contain the version with markers and those can be rendered without having to render the whole page.
So what you need to do in this case if you want the page cache to be disabled only in some conditions, is to use a custom TypoScript condition that is capable of setting config.no_cache = 1 only under special circumstances. That way you prevent generating a cache entry if the condition is met, but preserve full caching and cached output for every other request.
https://docs.typo3.org/typo3cms/TyposcriptSyntaxReference/TypoScriptParserApi/CustomConditions/Index.html
Note that it is still recommended that you instead create the parts of your page that must not be cached, as USER_INT objects. Having a use case where you in some cases need to disable the entire page cache indicates a possible misunderstanding of how the caching framework and/or USER_INT works. Hopefully the above explains those parts a bit.
if you look at the pibase (AbstractPlugin) code you will see that probably setting $conf['useCacheHash']and $conf['no_cache'] should be done.
https://api.typo3.org/typo3cms/current/html/_abstract_plugin_8php_source.html#l00190
If you create this object as USER_INT, it will be rendered non-cached, outside the main page-rendering.
https://docs.typo3.org/typo3cms/TyposcriptReference/ContentObjects/UserAndUserInt/Index.html

Prevent exit() in third-party function from exiting?

I'm calling several functions (which I can't edit) in sequence, but some of the functions redirect the user, so I never get to the next one.
I'm calling a third-party function which has calls to wp_redirect() which I'm able to prevent, but then the next line is exit; which I can't figure out how get around.
I was hoping to get around it with the ob_ functions, but no luck so far.
Any suggestions, hacky or otherwise, will be hugely appreciated!
edit: I have an idea I haven't tried yet - somehow spawning off new processes to perform these tasks - what would be best way to do that, waiting for each to complete before moving on.
I believe is possible to get the source code of the php interpreter ... mess with the exit function and then recompile and install on the web server your new custom version of php ...
I'm afraid you just can't. There is an option to redefind native php function, see runkit_function_redefine.
But in the comments it also says:
language constructs
(eval, die, exit, isset, unset, echo etc.) which might be confused
with functions, cannot be renamed or redefined even with
runkit.internal_override.
You may be able to use runkit_function_redefine. You'll need to make sure you can modify internal functions in your php.ini file in order to be able to change native/internal functions.
I think the php.ini setting you need to ensure is switched on is runkit.internal_override.
I've not tested this.
However, since exit is a language construct I'm not even sure it's possible to get around it even with the above function.
In the end I went with using cURL to make three synchronous requests to a file with different GET params.
Throw an exception in wp_redirect, and wrap the code with try-catch statement, then check if the exception message contains the message you have set and return the response accordingly, I have used this hack with most plugins including woocommerce, ultimatemember etc...
example:
ob_start();
try{
whatever code....
}catch(\Exception $e){
}
echo ob_get_clean();

elgg_get_context() not working

I have been trying to write a plugin on elgg framework. So I tried to call specific css for a particular page by using elgg_extend_view() function which is not working. For example
if(elgg_get_context()){
elgg_extend_view('css/elgg', 'myplugin/css');
}/*This works*/
But the following doesnt seem to work
if(!elgg_get_context()){ //If there is not elggcontext call no_css file
elgg_extend_view('css/elgg', 'myplugin/no_css');
}/*This is not working */
Can anybody tell me why?
Apparently, you're misinterpreting expected behavior of elgg_get_context function.
Check: http://reference.elgg.org/pageowner_8php.html#a25fe73eb19442b4a4476f18e63abf382 It uses strings as a context name (it has to be pushed first and is expected usually to be set just before pagesetup event)
Conditional extension of elgg/css won't work correctly, due to caching. You probably want to use elgg_register_css and elg_load_css instead.
elgg_get_context() returns STRING or NULL
The exclamation (!) is not good in this case.
Try:
if(null!==elgg_get_context()){ //If there is not elggcontext call no_css file
elgg_extend_view('css/elgg', 'myplugin/no_css');
}
If any problem try to visualize what the function is returning, see:
var_dump(elgg_get_context());
Note: elgg is in version 1.8.18, check if you have updated the code.
The best way to do this is to compare your plugin's context, as elgg will always maintain some context.
so do it like:
if(elgg_get_context() == 'myplugin'){
elgg_extend_view('css/elgg', 'myplugin/css');
}

Monitor a PHP variable for change in value

I am actually trying to monitor a PHP variable (may be as a separate thread but not possible in PHP) and fire a PHP function whenever the value of the variable changes.
eg: lets take a variable $_GLOBALS['foo']=1;
if at any point in the code, the value of $_GLOBALS['foo'] changes to something else, i want to fire a PHP function immediately.
The variable can be anywhere inside a loop or in a function,etc.
Why i want this: I have a variable which stores the last error occured as a text. If the value of the variable changes from "" to something else, i want to trigger an error. My LOGIC may seem a bit strange but this is what i would like to do.
Thanx in advance.
Edit: I tried: How to use monitors in PHP? and How can one use multi threading in PHP applications but does not seem to solve the problem.
The Code (Thought this could solve some of your doubts on my question):
public function addtag($tagarray,$uid,$tagtype="1")
{
$dbobj=new dboperations();
$uiobj=new uifriend();
$tagid=$uiobj->randomstring(30,DB_SOCIAL,SOCIAL_TAG,tag_tagid);
$c=0;
foreach($tagarray as $tags)
{
$c++;
$tagname=$tags["tagname"];
$taguid=$tags["tagid"];
$dbobj->dbinsert("INSERT INTO ".SOCIAL_TAG." (".tag_tagid.",".tag_fuid.",".tag_tuid.",".tag_tagname.",".tag_tagtype.") VALUES
('$tagid','$uid','$taguid','$tagname','$tagtype')",DB_SOCIAL);
}
if($c==0)
{
$lasterror="No tags were added";return "";
}
else
{
return $tagid;
}
}
Here, if i call a error handling function instead of monitoring the variable, it wont be advisable in my case since the error handling function may do any operation like give alert and redirect to a page or any similar operation.
I asked this question cause, i thought what if the script does not reach the line
return "";
It would affect the project's workflow. Thats what i am worried about.
And the variable i was talking about is $lasterror and i have many functions like this where $lasterror is used.
I saw this, so I built this:
https://github.com/leedavis81/vent
Should solve your problem.
There is no built-in way to do this in PHP, and there's no easy way to add it. It doesn't really feel right for the way the language works anyway.
Instead of setting a variable, you could build a custom function that handles the error - or use PHP's built-in error handling functionality using a custom error handler.
Another error handling method which comes close to what you want to do (I think) is exceptions.

Problem with prototype framework in cake php

This question is somewhat related to my previous question (not necessary to go through it)
link text
, although this one is more of an Ajax problem.
I tested my page in firebug and I am getting the following error
Event.Observe is not a function
Event.observe('form908983160', 'submit', function(event) { new Ajax.Updater('comments','/blog/posts/comments/add', {asynchronous:true, evalScripts:true, parameters:Form.serialize('form908983160'), requestHeaders:['X-Update', 'comments']}) }, false);
I am using the prototype framework for developing my cake php application.
How should I fix this problem?
Guys, I really need your help in this one. I am really stuck at this one. [:(]
prototype.js has to be :
loaded in the page (verify it's not in 404, for instance)
loaded in the page before the execution of your script
Which generally means you need to include prototype.js first, and, only after that, you can include your scripts.
As a sidenote : you are not using any other JS Framework in that page, of course...
Remember that any Event.observers need to be used in global
Event.observe(window,'loaded',f);
// or
Event.observe(document,'dom:loaded',f)
otherwise they will not work.
For me it looks like there's problem with linking to Prototype. Open firebug and try to see the contents of your script files - if you see html, that means something went terribly wrong :-)

Categories