PHPUnit testing coverage help for WordPress - wordpress

Below function we need to cover,
function get_assets_list() {
static $assets;
if ( ! isset( $assets ) ) {
$assets = load_asset_file( BUILD_PATH . 'asset-manifest.json' );
if ( empty( $assets ) ) {
$assets = array();
}
}
return $assets;
}
But unfortunately we can't cover below lines
if ( ! isset( $assets ) ) {
$assets = load_asset_file( BUILD_PATH . 'asset-manifest.json' );
if ( empty( $assets ) ) {enter code here
$assets = array();
}
}
Because when we execute the this function always getting $assets this variable set. So how we can set this variable unset or undefined. So that we can cover those line.

I am executed below,
public function test_get_assets_list() {
static $assets = null;
// Replace this with some actual testing code.
$output = get_assets_list();
$this->assertArrayHasKey( 'block.js', $output );
}
but its not covered below lines
if ( ! isset( $assets ) ) {
$assets = load_asset_file( BUILD_PATH . 'asset-manifest.json' );
if ( empty( $assets ) ) {
$assets = array();
}
}

Related

Unable to select shipping method after filtering 'woocommerce_package_rates'

I just wrote this filter to disable non-free shipping methods when free shipping is available:
add_filter( 'woocommerce_package_rates', 'disable_paid_shipping', 9999, 2 );
function disable_paid_shipping( $rates, $package ) {
$free_rates = array();
foreach ( $rates as $i => $rate ) {
if ( str_contains( $rate->label, "gratuita" ) OR str_contains( $rate->label, "gratuito" ) ) {
$free_rates[] = $rate;
}
if ( str_contains( $rate->id, "local") ) {
$local = $rate;
}
if ( str_contains( $rate->id, "fermopoint") ) {
$fermopoint = $rate;
}
}
if ( !empty( $free_rates ) ) {
if ( isset($fermopoint) ) {
$fermopoint->cost = 0;
$fermopoint->label .= ' gratuito';
array_unshift( $free_rates, $fermopoint );
}
if ( isset($local) ) {
$free_rates[] = $local;
}
$rates = $free_rates;
}
return $rates;
}
The code works as expected, unless for two unexpected events occurring:
no shipping method is selected by default anymore (both in cart and checkout page)
when I choose one in the cart page, it gets unselected right after (only in cart page)
To solve the 1st problem at checkout, I can work around by forcing the selection through a hook on woocommerce_before_cart (although this looks like a forced trick).
For the 2nd problem I have no idea.
Suggestions?
The problem lies in that the WC $rates array is an associative array:
$rates = Array (
[rate_id_0] => [rate_obj_0]
[rate_id_1] => [rate_obj_1]
...
);
While the newly created $free_rates is an indexed array:
$free_rates = Array (
[0] => [rate_obj_0]
[1] => [rate_obj_1]
...
);
As result, WC is unable to match the new $free_rates array with the user's default rate_id, which is supposed to be used as the array key.
Here's the working code:
add_filter( 'woocommerce_package_rates', 'disable_paid_shipping', 9999, 2 );
function disable_paid_shipping( $rates, $package ) {
$free_rates = array();
foreach ( $rates as $i => $rate ) {
if ( str_contains( $rate->label, "gratuita" ) OR str_contains( $rate->label, "gratuito" ) ) {
$free_rates[$rate->id] = $rate;
}
if ( str_contains( $rate->id, "local") ) {
$local = $rate;
}
if ( str_contains( $rate->id, "fermopoint") ) {
$fermopoint = $rate;
}
}
if ( !empty( $free_rates ) ) {
if ( isset($fermopoint) ) {
$fermopoint->cost = 0;
$fermopoint->label .= ' gratuito';
$free_rates = array($fermopoint->id => $fermopoint) + $free_rates; // to set it as 1st
}
if ( isset($local) ) {
$free_rates[$local->id] = $local;
}
$rates = $free_rates;
}
return $rates;
}

In woocommerce how do I retrieve all variations like get_available_variations but including invisible

I want to retrieve all variations in a template file including hidden variations,
like $product->get_available_variations
I have placed the following in my theme function.php to extend the WC_Product_Variable class.
But when I call my new function
$product->get_all_variations();
in my template (single-product\add-to-cart\variable-subscription.php) it gives an undefined method error.
Calling $product->get_available_variations(); works fine.
What have I missed?
add_action( 'init', 'register_custom_variation' );
function register_custom_variation () {
class WC_Product_Variable_Extension extends WC_Product_Variable {
public function __construct( $product ) {
parent::__construct( $product );
}
public function get_all_variations() {
$variation_ids = $this->get_children();
$available_variations = array();
if ( is_callable( '_prime_post_caches' ) ) {
_prime_post_caches( $variation_ids );
}
foreach ( $variation_ids as $variation_id ) {
$variation = wc_get_product( $variation_id );
// Hide out of stock variations if 'Hide out of stock items from the catalog' is checked.
if ( ! $variation || ! $variation->exists() || ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && ! $variation->is_in_stock() ) ) {
continue;
}
$available_variations[] = $this->get_all_variation( $variation );
}
$available_variations = array_values( array_filter( $available_variations ) );
return $available_variations;
}
}
}

How to edit woocommerce functions trought theme's functions?

I'd like to edit public function generate_product_data from woocommerce/includes/class-wc-structured-data.php
Inside this function:
if ( $product->get_sku() ) {
$markup['sku'] = $product->get_sku();
} else {
$markup['sku'] = $product->get_id();
}
I want to change it to:
if ( $product->get_sku() ) {
$markup['sku'] = $product->get_sku();
$markup['mpn'] = 'BG' . $markup['sku'];
} else {
$markup['sku'] = $product->get_id();
$markup['mpn'] = 'BG' . $product->get_id();
}
And i want to add my custom code right before this part:
// Check we have required data.
if ( empty( $markup['aggregateRating'] ) && empty( $markup['offers'] ) && empty( $markup['review'] ) ) {
return;
}
I tried with:
add_filter( 'woocommerce_structured_data_product', 'add_mpn' );
function add_mpn($markup) {
if ( $product->get_sku() ) {
$markup['mpn'] = 'BG' . $markup['sku'];
} else {
$markup['mpn'] = 'BG' . $product->get_id();
}
return $markup;
}
But it doesn't work.
Any help?
You are almost there, you need to change it a bit. The filter gives you two variables to work with, although you need to return only one. This is why I added the priority (50) and the number 2, to accept two variables.
I also recommend to give a more meaningful name to your function. Try this:
add_filter( 'woocommerce_structured_data_product', 'add_mpn', 50, 2 );
function add_mpn($markup, $product) {
if ( $product->get_sku() ) {
$markup['mpn'] = 'BG' . $markup['sku'];
} else {
$markup['mpn'] = 'BG' . $product->get_id();
}
return $markup;
}

override Wordpress core public function

I have 2 separate wp installs that are sharing the same database. My goal is to use the same post table and post meta table for both websites.
I have achieved that by adding in my config.php of the second site, the code:
define( 'CUSTOM_POST_TABLE', 'site1_posts' );
define( 'CUSTOM_POST_META_TABLE', 'site1_postmeta' );
Also I have added in wp-includes/wp-db.php , public function tables, line 1026-1076 the code
if ( isset( $tables['posts'] ) && defined( 'CUSTOM_POST_TABLE' ) )
$tables['posts'] = CUSTOM_POST_TABLE;
if ( isset( $tables['postmeta'] ) && defined( 'CUSTOM_POST_META_TABLE' ) )
$tables['postmeta'] = CUSTOM_POST_META_TABLE;
Now both websites share the same posts but the issue is that the next time wordpress will be updated, I will loose the changes from wp-includes/wp-db.php
So I tried to override the public function with another function inside the function.php file of second website Override arguments of public function in Wordpress but the website crashed and returns 500 error. Propably I am doing sth wrong.
The code I tried is
add_filter( 'tables', 'my_tables', 11, 2 );
public function my_tables( $scope = 'all', $prefix = true, $blog_id = 0 ) {
if ( isset( $tables['posts'] ) && defined( 'CUSTOM_POST_TABLE' ) )
$tables['posts'] = CUSTOM_POST_TABLE;
if ( isset( $tables['postmeta'] ) && defined( 'CUSTOM_POST_META_TABLE' ) )
$tables['postmeta'] = CUSTOM_POST_META_TABLE;
}
return $tables;
}
The original code inside wp-includes/wp-db.php is
public function tables( $scope = 'all', $prefix = true, $blog_id = 0 ) {
switch ( $scope ) {
case 'all' :
$tables = array_merge( $this->global_tables, $this->tables );
if ( is_multisite() )
$tables = array_merge( $tables, $this->ms_global_tables );
break;
case 'blog' :
$tables = $this->tables;
break;
case 'global' :
$tables = $this->global_tables;
if ( is_multisite() )
$tables = array_merge( $tables, $this->ms_global_tables );
break;
case 'ms_global' :
$tables = $this->ms_global_tables;
break;
case 'old' :
$tables = $this->old_tables;
break;
default :
return array();
}
if ( $prefix ) {
if ( ! $blog_id )
$blog_id = $this->blogid;
$blog_prefix = $this->get_blog_prefix( $blog_id );
$base_prefix = $this->base_prefix;
$global_tables = array_merge( $this->global_tables, $this->ms_global_tables );
foreach ( $tables as $k => $table ) {
if ( in_array( $table, $global_tables ) )
$tables[ $table ] = $base_prefix . $table;
else
$tables[ $table ] = $blog_prefix . $table;
unset( $tables[ $k ] );
}
if ( isset( $tables['users'] ) && defined( 'CUSTOM_USER_TABLE' ) )
$tables['users'] = CUSTOM_USER_TABLE;
if ( isset( $tables['usermeta'] ) && defined( 'CUSTOM_USER_META_TABLE' ) )
$tables['usermeta'] = CUSTOM_USER_META_TABLE;
}
return $tables;
}
How could override it so I won't loose my changes after the next update?

Double filters showing on web view on Woocommerce

I'm having a problem with my woocommerce theme, flatsome, but I think the problem is with the widget. The standard woocommerce product filter.
In the web view it shows normally, but on the mobile view the filter gets doubled;
a filter which gives you the standard mobile view-type of filter that pops on the side when clicked,
and another filter which gives an ugly dropdown.
This is the image on mobile view, the one on top has the right filter I want, the one under gives the ugly dropdown
Here's the code of the filter:
<?php
/*
Plugin Name: WooCommerce Product Filter
Plugin URI: http://www.mihajlovicnenad.com/product-filter
Description: Advanced product filter for any Wordpress template! - mihajlovicnenad.com
Author: Mihajlovic Nenad
Version: 6.3.0
Author URI: https://www.mihajlovicnenad.com
Text Domain: prdctfltr
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( !class_exists( 'PrdctfltrInit' ) ) :
final class PrdctfltrInit {
public static $version = '6.3.0';
protected static $_instance = null;
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
public function __construct() {
do_action( 'prdctfltr_loading' );
$this->includes();
$this->init_hooks();
do_action( 'prdctfltr_loaded' );
}
private function init_hooks() {
register_activation_hook( __FILE__, array( $this, 'activate' ) );
add_action( 'init', array( $this, 'check_version' ), 10 );
add_action( 'init', array( $this, 'init' ), 0 );
}
private function is_request( $type ) {
switch ( $type ) {
case 'admin' :
return is_admin();
case 'ajax' :
return defined( 'DOING_AJAX' );
case 'cron' :
return defined( 'DOING_CRON' );
case 'frontend' :
return ( ! is_admin() || defined( 'DOING_AJAX' ) ) && ! defined( 'DOING_CRON' );
}
}
public function includes() {
include_once( 'lib/pf-characteristics.php' );
include_once( 'lib/pf-widget.php' );
include_once( 'lib/pf-fixoptions.php' );
if ( $this->is_request( 'admin' ) ) {
add_action( 'vc_before_init', array( $this, 'composer' ) );
include_once ( 'lib/pf-settings.php' );
$purchase_code = get_option( 'wc_settings_prdctfltr_purchase_code', '' );
if ( $purchase_code ) {
require 'lib/update/plugin-update-checker.php';
$pf_check = PucFactory::buildUpdateChecker(
'http://mihajlovicnenad.com/envato/verify_json.php?k=' . $purchase_code,
__FILE__
);
}
}
if ( $this->is_request( 'frontend' ) ) {
$this->frontend_includes();
}
}
public function frontend_includes() {
include_once( 'lib/pf-frontend.php' );
include_once( 'lib/pf-shortcode.php' );
}
public function include_template_functions() {
}
public function init() {
do_action( 'before_prdctfltr_init' );
$this->load_plugin_textdomain();
do_action( 'after_prdctfltr_init' );
}
public function load_plugin_textdomain() {
$domain = 'prdctfltr';
$dir = untrailingslashit( WP_LANG_DIR );
$locale = apply_filters( 'plugin_locale', get_locale(), $domain );
if ( $loaded = load_textdomain( $domain, $dir . '/plugins/' . $domain . '-' . $locale . '.mo' ) ) {
return $loaded;
}
else {
load_plugin_textdomain( $domain, FALSE, basename( dirname( __FILE__ ) ) . '/lang/' );
}
}
public function setup_environment() {
}
public function plugin_url() {
return untrailingslashit( plugins_url( '/', __FILE__ ) );
}
public function template_path() {
return apply_filters( 'prdctfltr_template_path', '/templates/' );
}
public function plugin_path() {
return untrailingslashit( plugin_dir_path( __FILE__ ) );
}
public function plugin_basename() {
return untrailingslashit( plugin_basename( __FILE__ ) );
}
public function ajax_url() {
return admin_url( 'admin-ajax.php', 'relative' );
}
public function version() {
return self::$version;
}
public function composer() {
require_once( 'lib/pf-composer.php' );
}
function check_version() {
$version = get_option( 'wc_settings_prdctfltr_version', false );
if ( $version === false ) {
$check = get_option( 'wc_settings_prdctfltr_always_visible', false );
if ( $check === false ) {
update_option( 'wc_settings_prdctfltr_version', self::$version, 'yes' );
return '';
}
else {
$version = get_option( 'wc_settings_prdctfltr_version', '5.8.1' );
}
}
if ( version_compare( '5.8.2', $version, '>' ) ) {
add_action( 'admin_init', array( &$this, 'fix_database_582' ), 100 );
}
if ( version_compare( '6.0.6', $version, '>' ) ) {
add_action( 'init', array( &$this, 'fix_database_606' ), 100 );
}
}
function fix_database_606() {
global $wpdb;
$default = $wpdb->get_results( "SELECT `option_name`, `option_value` FROM `$wpdb->options` WHERE `option_name` LIKE CONVERT( _utf8 'wc_settings_prdctfltr_%'USING utf8mb4 ) COLLATE utf8mb4_unicode_ci LIMIT 99999" );
if ( !empty( $default ) ) {
$fix_default = array();
include_once( 'lib/pf-options-autoload.php' );
foreach( $default as $k => $v ) {
if ( in_array( $v->option_name, $forbidden_std ) ) {
$wpdb->query( "update $wpdb->options set autoload='yes' where option_name = '$v->option_name';" );
}
else if ( in_array( $v->option_name, $dont_autoload_std ) || substr( $v->option_name, 0, 41 ) == 'wc_settings_prdctfltr_term_customization_' || substr( $v->option_name, 0, 43 ) == 'wc_settings_prdctfltr_filter_customization_' ) {
$wpdb->query( "update $wpdb->options set autoload='no' where option_name = '$v->option_name';" );
}
else if ( in_array( $v->option_name, $autoload_std ) ) {
$wpdb->query( "update $wpdb->options set autoload='yes' where option_name = '$v->option_name';" );
}
else if ( strpos( $v->option_name, 'transient' ) ) {
delete_option( $v->option_name );
}
else {
$fix_default[$v->option_name] = get_option( $v->option_name );
$wpdb->query( "update $wpdb->options set autoload='no' where option_name = '$v->option_name';" );
}
}
if ( !empty( $fix_default ) ) {
$fix_default = json_encode( $fix_default );
update_option( 'prdctfltr_wc_default', $fix_default, 'no' );
}
$templates = get_option( 'prdctfltr_templates', array() );
if ( !empty( $templates ) && is_array( $templates ) ) {
update_option( 'prdctfltr_backup_templates', $templates, 'no' );
foreach( $templates as $k1 => $v1 ) {
if ( !empty( $v1 ) && substr( $v1, 0, 1 ) == '{' ) {
update_option( 'prdctfltr_wc_template_' . sanitize_title( $k1 ), $v1, 'no' );
$templates[$k1] = array();
}
}
}
update_option( 'prdctfltr_templates', $templates, 'no' );
}
update_option( 'wc_settings_prdctfltr_version', self::$version, 'yes' );
}
function fix_database_582() {
global $wpdb;
$wpdb->query( "update $wpdb->options set autoload='yes' where option_name like '%prdctfltr%';" );
$wpdb->query( "delete from $wpdb->options where option_name like '_transient_prdctfltr_%';" );
$wpdb->query( "delete from $wpdb->options where option_name like '_transient_%_prdctfltr_%';" );
$wpdb->query( "delete from $wpdb->options where option_name like 'wc_settings_prdctfltr_%_end';" );
$wpdb->query( "delete from $wpdb->options where option_name like 'wc_settings_prdctfltr_%_title' and option_value = '' ;" );
delete_option( 'wc_settings_prdctfltr_force_categories' );
delete_option( 'wc_settings_prdctfltr_force_emptyshop' );
delete_option( 'wc_settings_prdctfltr_force_search' );
delete_option( 'wc_settings_prdctfltr_caching' );
delete_option( 'wc_settings_prdctfltr_selected' );
delete_option( 'wc_settings_prdctfltr_attributes' );
update_option( 'wc_settings_prdctfltr_version', '6.0.5', 'yes' );
}
function activate() {
if ( false !== get_transient( 'prdctfltr_default' ) ) {
delete_transient( 'prdctfltr_default' );
}
$active_presets = get_option( 'prdctfltr_templates', array() );
if ( !empty( $active_presets ) && is_array( $active_presets ) ) {
foreach( $active_presets as $k => $v ) {
if ( false !== ( $transient = get_transient( 'prdctfltr_' . $k ) ) ) {
delete_transient( 'prdctfltr_' . $k );
}
}
}
}
}
function Prdctfltr() {
return PrdctfltrInit::instance();
}
PrdctfltrInit::instance();
endif;
?>
Simple CSS should be able to fix this. Use media queries to hide the filter with the ugly dropdown on small screens.
Something like:
#media (max-width: 768px) {
.filter-dropdown{
display: none;
}
}
Substitute filter-dropdown with the class name of that filter. Substitute 768px with whichever number of pixels is right for your situation.
Without you pasting code we can't be more specific than that.

Resources