I'm getting mysqli_get_server_info(): invalid object or resource mysqli error, when running wpdb outside of wordpress in a standalone 'plugin'.
Here's the code that returns the error:
private $use_mysqli = false;
if ( function_exists( 'mysqli_connect' ) ) {
if ( defined( 'WP_USE_EXT_MYSQL' ) ) {
$this->use_mysqli = ! WP_USE_EXT_MYSQL;
} elseif ( version_compare( phpversion(), '5.5', '>=' ) ||
! function_exists( 'mysql_connect' ) ) {
$this->use_mysqli = true;
} elseif ( false !== strpos( $GLOBALS['wp_version'], '-' ) ) {
$this->use_mysqli = true;
}
}
public function db_version() {
if ( $this->use_mysqli ) {
$server_info = mysqli_get_server_info( $this->dbh );
} else {
$server_info = mysql_get_server_info( $this->dbh );
}
return preg_replace( '/[^0-9.].*/', '', $server_info );
}
Why am I getting this error? How should I fix it?
Related
Getting a PHP warning for this section of code, whilst running PHP 8:
function wpsc_body_class( $classes ) {
global $wp_query, $wpsc_query;
$post_id = get_the_ID();
if ( $post_id ) {
$page_url = get_permalink( $post_id );
// If on a product or category page...
if ( get_option( 'product_list_url' ) == $page_url || get_post_type( $post_id ) === 'wpsc-product' ) {
$classes[] = 'wp-e-commerce';
if ( ! is_array( $wpsc_query->query ) ) {
$classes[] = 'wpsc-home';
}
if ( wpsc_is_single_product() ) {
$object = $wp_query->get_queried_object();
$classes[] = 'wpsc-single-product';
if ( absint( $object->ID ) > 0 ) {
$classes[] = 'wpsc-single-product-' . absint( $object->ID );
}
}
if ( wpsc_is_in_category() && ! wpsc_is_single_product() ) {
$classes[] = 'wpsc-category';
$tax_object = $wp_query->get_queried_object();
$classes[] = 'wpsc-category-' . esc_attr( $tax_object->slug );
}
if ( wpsc_is_in_tag() && ! wpsc_is_single_product() ) {
$classes[] = 'wpsc-tag';
$tax_object = $wp_query->get_queried_object();
$classes[] = 'wpsc-tag-' . esc_attr( $tax_object->slug );
}
}
$page_url = set_url_scheme( $page_url, 'relative' );
// If viewing the shopping cart...
if ( set_url_scheme( get_option( 'shopping_cart_url' ), 'relative' ) === $page_url ) {
$classes[] = 'wp-e-commerce';
$classes[] = 'wpsc-shopping-cart';
}
// If viewing the transaction...
if ( set_url_scheme( get_option( 'transact_url' ), 'relative' ) === $page_url ) {
$classes[] = 'wp-e-commerce';
$classes[] = 'wpsc-transaction-details';
}
// If viewing your account...
if ( set_url_scheme( get_option( 'user_account_url' ), 'relative' ) === $page_url ) {
$classes[] = 'wp-e-commerce';
$classes[] = 'wpsc-user-account';
}
}
return $classes;
}
Error pointing to this line of code:
if ( ! is_array( $wpsc_query->query ) ) {
Looking for the best way of resolving this, and changing this code, to overcome the php 8 warning for attempt to read property "query" on null.
You should check if the query method exists in the first place and add that check to your if statement like so:
Edit: updated answer to check for null.
$queryMethodExists = method_exists($wpsc_query, 'query');
if (
$wpsc_query === null
|| ($queryMethodExists && ! is_array( $wpsc_query->query ))
) {
$classes[] = 'wpsc-home';
}
This is a function to removed some banners, I know the error is happening around the if ( ! in_array( $current_screen->post_type, $post_types ) ) but I don't know to properly fix it.
function lsx_tec_disable_lsx_banner( $disabled ) {
global $current_screen;
$post_types = apply_filters( 'tribe_is_post_type_screen_post_types', Tribe__Main::get_post_types() );
if ( ! in_array( $current_screen->post_type, $post_types ) ) {
$disabled = true;
}
if ( is_null( $id ) && false !== strpos( $current_screen->id, 'tribe' ) )
{
$disabled = true;
}
if ( is_single() && tribe_is_event() ) {
$disabled = true;
}
return $disabled;
}
please check below link and change your code.
https://codex.wordpress.org/Function_Reference/get_current_screen
<?php
$current_screen = get_current_screen(); // use this instead of global $current_screen;
?>
I've just upgraded my local WooCommerce website to 3.0. Everything works perfectly as normal, but I've noticed with debugging turned on that I'm getting hundreds of the following notices:
[05-Apr-2017 12:25:00 UTC] PHP Notice: id was called <strong>incorrectly</strong>. Order properties should not be accessed directly. Please see Debugging in WordPress for more information. (This message was added in version 3.0.) in C:\xampp\htdocs\dev\wp-includes\functions.php on line 4137
So it looks like WooCommerce are pulling back being able to directly call order data. One example this code is being triggered by is this function in my functions.php file:
function eden_woocommerce_order_number($original, $order)
{
return 'EDN-' . str_pad($order->id, 10, 0, STR_PAD_LEFT);
}
This function simply adds "EDN" to the start of the order ID and pads it by 10 characters, but WooCommerce doesn't like how I'm calling $order - what would be the best way to rewrite such a function that 3.0 is happy with?
it says "id was called incorrectly. Order properties should not be accessed directly."
Try $order->get_id()
Maybe its helpful for others too. Here's the some stuff regarding to all the functions of directly accessed values through the magic function.
This function is from Woocommerce 3.0
if ( 'completed_date' === $key ) {
return $this->get_date_completed() ? gmdate( 'Y-m-d H:i:s', $this->get_date_completed()->getOffsetTimestamp() ) : '';
} elseif ( 'paid_date' === $key ) {
return $this->get_date_paid() ? gmdate( 'Y-m-d H:i:s', $this->get_date_paid()->getOffsetTimestamp() ) : '';
} elseif ( 'modified_date' === $key ) {
return $this->get_date_modified() ? gmdate( 'Y-m-d H:i:s', $this->get_date_modified()->getOffsetTimestamp() ) : '';
} elseif ( 'order_date' === $key ) {
return $this->get_date_created() ? gmdate( 'Y-m-d H:i:s', $this->get_date_created()->getOffsetTimestamp() ) : '';
} elseif ( 'id' === $key ) {
return $this->get_id();
} elseif ( 'post' === $key ) {
return get_post( $this->get_id() );
} elseif ( 'status' === $key ) {
return $this->get_status();
} elseif ( 'post_status' === $key ) {
return get_post_status( $this->get_id() );
} elseif ( 'customer_message' === $key || 'customer_note' === $key ) {
return $this->get_customer_note();
} elseif ( in_array( $key, array( 'user_id', 'customer_user' ) ) ) {
return $this->get_customer_id();
} elseif ( 'tax_display_cart' === $key ) {
return get_option( 'woocommerce_tax_display_cart' );
} elseif ( 'display_totals_ex_tax' === $key ) {
return 'excl' === get_option( 'woocommerce_tax_display_cart' );
} elseif ( 'display_cart_ex_tax' === $key ) {
return 'excl' === get_option( 'woocommerce_tax_display_cart' );
} elseif ( 'cart_discount' === $key ) {
return $this->get_total_discount();
} elseif ( 'cart_discount_tax' === $key ) {
return $this->get_discount_tax();
} elseif ( 'order_tax' === $key ) {
return $this->get_cart_tax();
} elseif ( 'order_shipping_tax' === $key ) {
return $this->get_shipping_tax();
} elseif ( 'order_shipping' === $key ) {
return $this->get_shipping_total();
} elseif ( 'order_total' === $key ) {
return $this->get_total();
} elseif ( 'order_type' === $key ) {
return $this->get_type();
} elseif ( 'order_currency' === $key ) {
return $this->get_currency();
} elseif ( 'order_version' === $key ) {
return $this->get_version();
} elseif ( is_callable( array( $this, "get_{$key}" ) ) ) {
return $this->{"get_{$key}"}();
} else {
return get_post_meta( $this->get_id(), '_' . $key, true );
}
You should call the woo get function. add get_ ()
For example, change:
$order->status to $order->get_status()
I have a problem.
I want to disable 'dev_mode' if 'opt_name' not redux_demo, using the if statment, but it did not work ... where does the fault ??
I found the code on Here
and I turn it into like this
if ( ! function_exists( 'redux_disable_dev_mode_plugin' ) ) {
function redux_disable_dev_mode_plugin( $redux ) {
if ( $redux->args['opt_name'] != 'redux_demo' ) {
if ( $redux->args['dev_mode'] == true ) {
$redux->args['dev_mode'] = false;
}
} else {
if ( $redux->args['dev_mode'] == false ) {
$redux->args['dev_mode'] = true;
}
}
}
add_action( 'redux/construct', 'redux_disable_dev_mode_plugin' );
}
the above code, I input in config.php
Thanks B4 and sorry my english is not good. :D
You can also filter the args JUST for your opt_name using this:
apply_filters( "redux/args/{$this->args['opt_name']}", $this->args );
if ( ! function_exists( 'redux_disable_dev_mode_plugin' ) ) {
function redux_disable_dev_mode_plugin( $redux ) {
if ( $redux->args['opt_name'] != 'redux_demo' ) {
$redux->args['dev_mode'] = false;
}
}
add_action( 'redux/construct', 'redux_disable_dev_mode_plugin' );
}
In framework.php (for me it's from line 1281). It worked for me after making those two attr false.
// Force dev_mode on WP_DEBUG = true and if it's a local server
if ( Redux_Helpers::isLocalHost() || ( Redux_Helpers::isWpDebug() ) ) {
if ( $this->args['dev_mode'] != true ) {
$this->args['update_notice'] = false;
}
$this->dev_mode_forced = true; // make it false
$this->args['dev_mode'] = true; //make it false
}
I have created this function within a Class, And though it works, I think Its messy and can be simplified easily, What technique can i use to simplify this? Though i can use foreach, Every time it will make new directory in the Cache folder instead of creating directory within parent directory.
Example : $directory = 'page/subpage/homepage'
public static function add_directory( $directory, $cache_path ) {
$all_directories = explode( '/', $directory );
$total_directories = count( $all_directories );
if( $total_directories == 1 ) {
if( ! File::exists( $cache_path.'/'.$all_directories[0] ) ) {
File::makeDirectory( $cache_path.'/'.$all_directories[0] );
}
}
else if( $total_directories == 2 ) {
if( ! File::exists( $cache_path.'/'.$all_directories[0] ) ) {
File::makeDirectory( $cache_path.'/'.$all_directories[0] );
}
if( ! File::exists( $cache_path.'/'.$all_directories[0].'/'.$all_directories[1] ) ) {
File::makeDirectory( $cache_path.'/'.$all_directories[0].'/'.$all_directories[1] );
}
}
else if( $total_directories == 3 ) {
if( ! File::exists( $cache_path.'/'.$all_directories[0] ) ) {
File::makeDirectory( $cache_path.'/'.$all_directories[0] );
}
if( ! File::exists( $cache_path.'/'.$all_directories[0].'/'.$all_directories[1] ) ) {
File::makeDirectory( $cache_path.'/'.$all_directories[0].'/'.$all_directories[1] );
}
if( ! File::exists( $cache_path.'/'.$all_directories[0].'/'.$all_directories[1].'/'.$all_directories[2] ) ) {
File::makeDirectory( $cache_path.'/'.$all_directories[0].'/'.$all_directories[1].'/'.$all_directories[2] );
}
}
else if( $total_directories == 4 ) {
if( ! File::exists( $cache_path.'/'.$all_directories[0] ) ) {
File::makeDirectory( $cache_path.'/'.$all_directories[0] );
}
if( ! File::exists( $cache_path.'/'.$all_directories[0].'/'.$all_directories[1] ) ) {
File::makeDirectory( $cache_path.'/'.$all_directories[0].'/'.$all_directories[1] );
}
if( ! File::exists( $cache_path.'/'.$all_directories[0].'/'.$all_directories[1].'/'.$all_directories[2] ) ) {
File::makeDirectory( $cache_path.'/'.$all_directories[0].'/'.$all_directories[1].'/'.$all_directories[2] );
}
if( ! File::exists( $cache_path.'/'.$all_directories[0].'/'.$all_directories[1].'/'.$all_directories[2].'/'.$all_directories[3] ) ) {
File::makeDirectory( $cache_path.'/'.$all_directories[0].'/'.$all_directories[1].'/'.$all_directories[2].'/'.$all_directories[3] );
}
}
else {}
}
Thanks.
You can specify whether you want to create directories recursively by setting true to your File::makeDirectory()'s third argument, so you can replace your entire code with something like this:
public static function add_directory( $directory, $cache_path ) {
if (!File::exists( $cache_path.'/'.$directory)) {
File::makeDirectory($cache_path . '/' . $directory, 0755, true);
}
}
Reference: Looks like it is not documented but it is in Laravel's source code for filesystem/Filesystem.php:
public function makeDirectory($path, $mode = 0755, $recursive = false, $force = false)
{
if ($force)
{
return #mkdir($path, $mode, $recursive);
}
else
{
return mkdir($path, $mode, $recursive);
}
}
From looking at your code - I dont see why this wouldnt work?
public static function add_directory( $directory, $cache_path ) {
if( ! File::exists( $cache_path.'/'.$directory ) ) {
File::makeDirectory( $cache_path.'/'.$directory );
}
}
There doesnt seem to need to be any need to 'explode' the array then rebuild it? This will build all the sub-directories along with it I believe...