What's wrong with this PHP code on Line 136 - php

I get the following error on my WP Dashboard:
Warning: First parameter must either be an object or the name of an existing class
in /home/content/88/11746388/html/wp-content/plugins/pluginname/pluginname.php on
line 136
Those are my lines from 130 to 140. Any ideas?
function get_latest_version($plugin_file) {
$latest_version = '';
$sr_plugin_info = get_site_transient ( 'update_plugins' );
// if ( property_exists($sr_plugin_info, 'response [$plugin_file]') && property_exists('response [$plugin_file]', 'new_version') ) {
if ( property_exists($sr_plugin_info, 'response [$plugin_file]') ) {
$latest_version = $sr_plugin_info->response [$plugin_file]->new_version;
}
return $latest_version;
}

it seems that you have error in this line :
if ( property_exists($sr_plugin_info, 'response [$plugin_file]') ) {
property_exists — Checks if the object or class has a property .
and 'response [$plugin_file]' it is not a valid property name of class.

To be honest that code is bleeding from several wounds.
a.) get_site_transient is not guaranteed to return an object and property_exists expects one
b.) you try to check if an array key exists in a property with property_exists. I'm fairly certain that property_exists can not do that - you should only check for response property with it and use isset() for the $plugin_file index.

function get_latest_version($plugin_file) {
$latest_version = '';
$sr_plugin_info = get_site_transient ( 'update_plugins' );
// if ( property_exists($sr_plugin_info, response[$plugin_file]) && property_exists(response[$plugin_file], 'new_version') ) {
if ( property_exists($sr_plugin_info, response[$plugin_file]) ) {
$latest_version = $sr_plugin_info->response[$plugin_file]->new_version;
}
return $latest_version;
}

Related

How can I simplify this function - return key value from array?

I have written the following function which returns the value of a key which is the account prefix of a cPanel account.
[prefix] = 'oneclick_'
The function works but I'm only a beginner and I'm sure there's an easier and better way to write this function.
<?php
// Get data from cPanel API
$array = $cp_db_restrictions['cpanelresult']['result']['data'];
//Pass array of $data to function
db_prefix( $array );
//Function accepts array of $data
function db_prefix( $array ) {
if( count( $array ) > 0 ) {
return $array['prefix'];
} else {
return "No prefix";
}
}
?>
<?php echo db_prefix($array); ?>
The function echos "oneclick_"
EDIT:
This is the code which returns the array:
$cp_db_restrictions = $cpanel->uapi(
'Mysql', 'get_restrictions'
);
However, I'm not quite sure how to add that into the function. When I try I get the following error message:
Notice: Undefined variable: array in ...
Fatal error: Uncaught Error: Call to a member function uapi() on null in...
SOLUTION*
So, thanks to Nick's answer here is the solution. You must make sure to declare the cPanel class at the top of the document and within the same PHP tags.
<?php
include("/usr/local/cpanel/php/cpanel.php"); // Instantiate the CPANEL object.
$cpanel = new CPANEL();
function db_prefix($cpanel) {
$cp_db_restrictions = $cpanel->uapi('Mysql', 'get_restrictions');
$array = $cp_db_restrictions['cpanelresult']['result']['data'];
return $array['prefix'] ?? 'No prefix';
} ?>
<?php echo db_prefix($cpanel); ?>
In PHP7 you can just use the null coalescing operator ??:
echo $array['prefix'] ?? 'No prefix';

How do I fix these illegal string offset warnings?

Context: On one of the sites where my WordPress plugin is installed I'm seeing a series of PHP warnings, but I'm not entirely sure why this is happening. I'm hoping someone here can help me figure out how to solve this warning.
Code Sample:
function my_function( $array ) {
if ( ! isset( $array['where'] ) ) { $array['where'] = 'after'; }
if ( ! isset( $array['echo'] ) ) { $array['echo'] = false; }
if ( ! isset( $array['content'] ) ) { $array['content'] = false; }
$array['shortcode'] = true;
$array['devs'] = true;
return social_warfare( $array );
}
add_shortcode( 'my_shortcode', 'my_function' );
The Warning:
Warning: Illegal string offset 'where' in
/home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php
on line 14
Warning: Illegal string offset 'echo' in
/home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php
on line 15
Warning: Cannot assign an empty string to a string offset in
/home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php
on line 15
Warning: Illegal string offset 'content' in
/home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php
on line 16
Warning: Cannot assign an empty string to a string offset in
/home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php
on line 16
Warning: Illegal string offset 'shortcode' in
/home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php
on line 18
Warning: Illegal string offset 'devs' in
/home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php
on line 19
For some reason, it's throwing a warning every time it encounters one of the indices in the array. How do I fix this? Thanks!
It looks like the function is expecting an array and is getting a string instead.
You can require an array in the function definition.
function my_function(array $array) { ...
Then if you call it with something other than an array, you'll get a TypeError.
Unfortunately, there will still be a problem somewhere else in your code, where something you thought was an array is actually a string.
Setting up your function like this will generate an error earlier, which is good, because it will make it more obvious where the problem is. If you modify your function to ignore the problem instead, it will probably just create more confusing behavior and potentially different errors.
The usage of the function is_array() in the beginning of your function can give you the insurance, that, if someone pass you something else than an array, the variable is reinitialised as an empty array.
Unsetting or nulling it before doing that is useless, because, as of PHP 5.3, PHP does have a garbage collector mechanism.
/**
* #params array $array
*/
function my_function( $array ) {
if ( ! is_array ( $array ) ) { $array = [] };
/**
* Or, if you don't like the short array notation:
* if ( ! is_array ( $array ) ) { $array = array(); };
*/
if ( ! isset( $array['where'] ) ) { $array['where'] = 'after'; }
if ( ! isset( $array['echo'] ) ) { $array['echo'] = false; }
if ( ! isset( $array['content'] ) ) { $array['content'] = false; }
$array['shortcode'] = true;
$array['devs'] = true;
return social_warfare( $array );
}
add_shortcode( 'my_shortcode', 'my_function' );

Laravel : Can't use `Input::get('foobar')` as an action's default parameter value

I'm trying to do the following in my controller:
public function moveMessagesToArchive( $message_ids = Input::get('message_ids') )
{
$json = json_encode( $message_ids);
echo $json;
}
And it keeps throwing the following error:
syntax error, unexpected '(', expecting ')'
at the function signature. What's causing the problem here?
Update
While, I wait for the reason I've written the following work around:
public function moveMessagesToArchive( $message_ids = array() )
{
$ids = array();
if ( ( count($message_ids) !== 0 ) && is_array($message_ids) ) {
$ids = $message_ids;
} else if ( Input::get('message_ids') ) {
$ids = Input::get('message_ids');
} else {
return false;
}
$json = json_encode( $ids );
echo $json;
}
It is not possible in PHP. According to the documentation
The default value must be a constant expression, not (for example) a variable, a class member or a function call.
Reference Example 4
What you are trying to do is not, and never has been supported by php.
Nothing at all to do with Laravel.

Error: Trying to get property of non-object

I'm getting an error:
Trying to get property of non-object on line 30
On the commented line below...
function admin_trim_category_description( $terms, $taxonomies ){
if( 'category' != $taxonomies[0] ) return $terms;
$my_categories = array('item1','item2','item3');
foreach( $terms as $key => $term)
if(in_array(
$terms[$key]->name, //ERROR LINE HERE
$my_categories))
{
unset($terms[$key]);
}
}
What am I doing wrong?
$terms[$key] seems not to be an object. I'd suggest you to check whatever it is an object or not with:
if (is_object($terms[$key])) { /* OK */ }
and/or check whatever the object is an instance of a specific class with:
if ($terms[$key] instanceof MyClass) { /* OK */ }

Php Check If a Static Class is Declared

How can i check to see if a static class has been declared?
ex
Given the class
class bob {
function yippie() {
echo "skippie";
}
}
later in code how do i check:
if(is_a_valid_static_object(bob)) {
bob::yippie();
}
so i don't get:
Fatal error: Class 'bob' not found in file.php on line 3
You can also check for existence of a specific method, even without instantiating the class
echo method_exists( bob, 'yippie' ) ? 'yes' : 'no';
If you want to go one step further and verify that "yippie" is actually static, use the Reflection API (PHP5 only)
try {
$method = new ReflectionMethod( 'bob::yippie' );
if ( $method->isStatic() )
{
// verified that bob::yippie is defined AND static, proceed
}
}
catch ( ReflectionException $e )
{
// method does not exist
echo $e->getMessage();
}
or, you could combine the two approaches
if ( method_exists( bob, 'yippie' ) )
{
$method = new ReflectionMethod( 'bob::yippie' );
if ( $method->isStatic() )
{
// verified that bob::yippie is defined AND static, proceed
}
}
bool class_exists( string $class_name [, bool $autoload ])
This function checks whether or not the given class has been defined.

Categories