I'm writing a plugin for WordPress, in procedural PHP.
Anyway, in the plugin, I want to update a users WordPress Session Token, which is stored in the usermeta table.
Digging around, I've found a class which has some methods which I think will help me achieve my goal.
I've written a function, which has the correct data, updates the expiry time, and I'm just attempting to pass the update through WP_Session_Tokens.
But I get the error:
Using $this when not in object context in .../wp-includes/class-wp-session-tokens.php on line 166
My function is so:
function update_auth_cookie($user_login, $user) {
$options = get_option('options_premium');
$cookieTime = $options['cookieTime'];
$sessionToken = wp_get_session_token();
$verifier = hash('sha256', $sessionToken);
$sessions = get_user_meta($user->ID, 'session_tokens', true);
$sessions[$verifier]['expiration'] = $cookieTime;
WP_Session_Tokens::update( $verifier, $sessions[$verifier]);
}
add_action('auth_cookie_valid', 'update_auth_cookie', 10, 2);
Is it possible to access a class through a function like this? If so, is it obvious what I'm doing wrong?
Your question is a bit broad, since the problem you are experiencing is not really related to the issue you are trying to solve (and addressing it would not necessarily give you the solution for what you are trying to do).
But anyway: you are getting this error because you are invoking a method statically, and you should first instantiate WP_Session_Tokens and make the call dynamically.
This are basic OOP concepts you should be aware of before trying to use objects, and are not much more difficult than regular PHP syntax.
Something along the lines of:
$wp_session_token = WP_Session_Tokens::get_instance($user->ID);
$wp_session_token->update( $verifier, $sessions[$verifier]);
Word to the wise: I'm not 100% that the above will work, and I do not have a WP installation handy to test it out, but it is at least syntactically/semantically correct and wont give you the error you are experiencing above.
Related
I've been dealing with the Laravel/Osiset-Shopify framework for some time and even after a long research there are things that I don't understand yet.
For example, if I want to fetch my webhooks, it is relatively cumbersome
Auth::user()->api()->rest('GET', '/admin/api/2021-04/webhooks.json')['body']
There is a function getWebhooks() under \osiset\laravel-shopify\src\ShopifyApp\Services\ApiHelper.php which I would much rather use.
I came across this documentation from Osiset, for which I don't know how to use it.
I tried to load the service use Osiset\ShopifyApp\Services\ApiHelper; and get the output data dd(ApiHelper::getWebhooks([]));. However, I get the error message Non-static method Osiset\ShopifyApp\Services\ApiHelper::getWebhooks() cannot be called statically.
Also
$foo = new ApiHelper;
dd( $foo->getWebhooks() );
did not lead to any result: Call to a member function rest() on null.
Can someone show me how to access Osiset's internal functions and use the documentation properly?
You can use this documentation
and for the webhooks.json, You can use this to get the results $userId will be the user's primary key.
$shop = User::find($userId);
$method = 'GET';
$url = '/admin/api/2021-04/webhooks.json';
return $shop->api()->rest($method, $url, []);
UberCart for Drupal has some difficulties with currencies. However, by overriding "uc_currency_format", you can at least do some background calculation to give you a good estimate of the converted value. However, as it's part of UberCart Core, you can't edit the file, So you risk losing your code after every update. Also, this function does not have a hook!
That means the only that I can think of dealing with this, is having a module that overrides the function. So my question is...
Is there a way to override an existing PHP function? For example, I have:
function uc_currency_format($value, $sign = TRUE, $thou = TRUE, $dec = NULL)
{
// dont do this
}
But when this gets called, I want it to instead execute this
function uc_currency_format_rewrite($value, $sign = TRUE, $thou = TRUE, $dec = NULL)
{
// do this
}
Is that possible?
It seems to be one of those very rare times you need to hack the core code.
When it comes to this, I try to limit the impact at minimum like this:
Rename original function. In your case, you would go with something like 'uc_currency_format_ORIGINAL'
In your custom module, rename your 'uc_currency_format_rewrite' into 'uc_currency_format'
This way, you will have your own code running.
At next update, you will see in your testing environment (always better to test, before applying updates to production sites) a duplicate function name fatal error. If your hook has not been implemented yet, you will rename the original fuction, again.
This method is not defined in the best practice, of course. Use it at your own risk.
No it is not possible to override a function in PHP. Drupal 7 does not use Zend (rename_function(), override_function()) or OOP in modules. So you could only ask the maintainer for a new hook.
Maybe you could write a patch, which provides this hook and ask the maintainer for implementing it.
I am writing fresh code, as part of refactoring an older legacy codebase.
Specifically, I am writing a Device class that will be used to compute various specifications of a device.
Device class depends on device's model number and particle count and I can call it as $device = new Device($modelNumber, $particleCount);
Problem: since this class will go into existing legacy code, I have no direct influence on if this class will be called properly. For Device to work, it needs to have correct model number and correct particle count. If it does not receive the proper configuration data, internally device will not be initialized, and the class will not work. I think that I need to find a way to let the caller know that there was an error, in case an invalid configuration data was supplied. How do I structure this to be in line with object oriented principles?
Or, alternatively, do I need to concern myself with this? I think there is a principle that if you supply garbage, you get garbage back, aka my class only needs to work properly with proper data. If improper data is supplied, it can bake a cake instead, or do nothing (and possibly fail silently). Well, I am not sure if this principle will be great. I do need something to complain if supplied configuration data is bad.
Here is some code of what I am thinking:
$device = new Device($x, $y);
$device->getData();
The above will fail or produce bad or no data if $x or $y are outside of device specs. I don't know how to handle this failure. I also want to assume that $device is valid when I call getData() method, and I can't make that assumption.
or
$device = new Device($x, $y);
if ($device->isValid())
$device->getData();
else
blow_up("invalid device configuration supplied");
The above is better, but the caller has to now they are to call isValid() function. This also "waters down" my class. It has to do two things: 1) create device, 2) verify device configuration is valid.
I can create a DeviceChecker class that deals with configuration vefication. And maybe that's a solution. It bothers me a little that DeviceChecker will have to contain some part of the logic that is already in Device class.
Questions
what problem am I trying to solve here? Am I actually trying to design an error handling system in addition to my "simple class" issue? I think I probably am... Well, I don't have the luxury of doing this at the moment (legacy code base is huge). Is there anything I can do now that is perhaps localized to the pieces of code I touch? That something is what I am looking for with this question.
I think you need to use below code to verify your passed arguments in construct
class Device {
public function __constructor($modelNumber, $particleCount) {
if(!$this->isValid($modelNumber, $particleCount) {
return false; //or return any error
}
}
}
This will check the passed params are valid or not and create object based on that only, otherwise return false or any error.
I've found this script for converting wiki syntax to HTML in php and i've tried to integrate it into Codeigniter. it seems really easy to use. However, it's not working, and instead producing around 8 of these errors:
Message: Use of undefined constant LS_NONE - assumed 'LS_NONE'
I think this is because Codeigniter helpers are not a class but rather functions, and this bit of code is a class, or does this issue lie with something else? I've also tried to use it as a model without success.
It also seems horribly outdated (2007). Could somebody suggest a really simple alternative or maybe give an idea of how to convert this to a simple function if that is possible? It's a very short piece of code. I'm not sure how these constants work in relationship to a function versus a class.
I've given the Text_Wiki from Pear ago, but the use and complexity well exceeds both my requirements and knowledge :)
//Any help would be greatly appreciated
Loaded using:
$row = $query->row();
$content=$row->course_content;
$this->load->helper('wiki');
$content=explode("\n", $content);
$output = WikiTextToHTML::convertWikiTextToHTML($content);
$html=array_merge($output);
$data['contents'][]= $html;
$this->load->view('default/a',$data);
It looks like the script is actually a class. put it in the libraries folder and load it with $this->load->library(). That will allow it to properly initialize and define the constants that it uses.
something like:
$this->load->library('wikitexttohtml');
$this->wikitexttohtml->convertWikiTextToHTML($wiki_text);
In my codeigniter i created a library in library folder.I want to load view pages in that library.How can i do this?
This is my code:
$this->load->view('view_page');
But when iam using this code i get an error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_theme_lib::$load
Filename: libraries/theme_lib.php
Line Number: 9
What is the problem in mycode?
In Line number 9 in library the code is :
$this->load->view('view_page');
To do what you're trying to do you need to get an instance of CI, and use that.
e.g.
$CI =& get_instance();
Within your library functions you could then use this variable to load the view:
$CI->load->view('view_page');
I would question though why you want to call a view, in the form that you have done, within a library. I suspect that you would be better to get the view call to return data (setting the 3rd parameter 'true') and then have your library return the display data to the controller.... Your approach seems messy, but then I have no idea what your library is trying to do.....
I have came across your question for different reason, I seem to have problem passing variables to views instead. Let me explain before I tell you answer to your problem.
Imagine you have an Emailer library to send emails rather than sorting that out in controller.
Emailer than within itself builds email string using views. My problem is that when I make my call from controller something like Emailer::send_mail($data,$template) it passes the variables correctly but when I do it from another library the view fails to register the variables. LOL
So yes Stéphane Bourzeix you do sometimes want to use output from view in a different way than just returning to client browser.
The solution is here.
https://www.codeigniter.com/userguide2/general/views.html
the last section of that page has something like
$string = $this->load->view('myfile', '', true);
but something like
$string = $this->load->view('myfile', $view_data, true);
should work too
in case of doing this from other places than controllers you will need to:
$this->ci = & get_instance();
$string = $this->ci->load->view("myfile",$view_data,true);
it seems like the last argument in the list (true) is the one that tells it not to render to browser but instead just create string with template content
I know it's a bit too late but hope it still helps to some. Good luck with your code.
tomhre
You simply DON'T load pages (aka Views) in a Library.
I don't see any need for doing this.