Sanitizing the code as per wpcs for yoda condition checks - php

i am trying to cleanup the file with phpcs --standard=Wordpress ../filename.php . But it seems phpcbf is unable to perform the action. So, can anyone share some help as how can i manually fix these doc comment and yoda errors. Attached is related chunk of code and the snip of the actual php file and the errors encountered. Any help would be highly appreciated.
<?php
function is_blog() {
return ( is_archive() || is_author() || is_category() || is_home() || is_single() || is_tag() ) && 'post' == get_post_type();
}
?>
<?php do_action( 'generate_before_footer' ); ?>
<div <?php generate_footer_class(); ?>>
<?php
do_action( 'generate_before_footer_content' );
// Get how many widgets to show.
$widgets = generate_get_footer_widgets();
if ( ! empty( $widgets ) && 0 !== $widgets ) :
// Set up the widget width.
$widget_width = '';
if ( $widgets == 1 ) {
$widget_width = '100';
}
if ( $widgets == 2 ) {
$widget_width = '50';
}
if ( $widgets == 3 ) {
$widget_width = '33';
}
if ( $widgets == 4 ) {
$widget_width = '25';
}
if ( $widgets == 5 ) {
$widget_width = '20';
}
?>

Above the is_blog() function, you'll need to add a docblock:
/**
* Function comment here.
*
*/
function is_blog() {
}
Yoda conditions are when you write the value to compare against before the variable you are comparing. So instead of:
if ( $widgets == 1 ) {
$widget_width = '100';
}
It wants you to write:
if ( 1 == $widgets ) {
$widget_width = '100';
}
Changing those things should get the file passing the checks.

Related

Getting this error "Trying to get property of non-object" for a function

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;
?>

PHP Warning: in_array() expects parameter 2 to be array, null given

Please help. I cannot figure this out, and granted I am just learning php
I tried to add code to an existing woocommerce function, to remove a payment gateway if a group of products were in the cart.
I think the 4th line from the bottom "added for auto pay if statement" is the issue here. Thanks in advance.
public function is_available() {
//added for autopay 1 line
global $woocommerce;
$is_available = parent::is_available();
//added for autopay 2 lines
foreach ($woocommerce->cart->cart_contents as $key => $values )
$autopays = array(26108,30619,35613);
// don't show on checkout page
if ( ! $this->is_express_checkout() && is_checkout() && ! $this->show_on_checkout() ) {
$is_available = false;
}
// don't display when order review table is rendered via AJAX
if ( ! $this->is_express_checkout() && defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_POST['action'] ) && $_POST['action'] == 'woocommerce_update_order_review' && ! $this->show_on_checkout() ) {
$is_available = false;
}
// don't show on checkout > pay page
if ( is_checkout_pay_page() ) {
$is_available = false;
}
// don't show if the cart contains a subscription and manual renewals are not enabled
if ( $this->get_plugin()->is_subscriptions_active() && WC_Subscriptions_Cart::cart_contains_subscription() && 'no' === get_option( WC_Subscriptions_Admin::$option_prefix . '_accept_manual_renewals', 'no' ) ) {
$is_available = false;
}
//added for auto pay if statement
if(in_array($values['product_id'],$autopays)){$is_available = false;
}
return $is_available;
}
The syntax for foreach is
foreach (array_expression as $key => $value) {
statement1...
statement2...
}
In your code, you're missing the opening and closing brackets after the foreach statement.

Redux WP Framework. Cannot disable 'dev_mode' with if statment

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
}

Wordpress show only on posts

i have a problem with my WP function. I need it to ONLY be shown on blog posts. Right now it get shown on the other pages such as the frontpage.
Here is my function in functions.php:
function my_sp_function( $content ) {
$include = FALSE;
$post = FALSE;
if ( in_category( array( 3, 4, 5, 6, 91, 133 ) ) ) {
$include = TRUE;
}
if (get_post_type(get_the_ID()) == 'post'){
$post = TRUE;
}
if ( $post = TRUE AND $include == TRUE ) {
return $content;
}
}
Anybody there knows what i do wrong?
I have tried with this code now, it still shows the $content on the front page..
function my_sp_function( $content ) {
$include = FALSE;
$post = FALSE;
if ( in_category( array( 3, 4, 5, 6, 91, 133 ) ) ) {
$include = TRUE;
}
if( is_home() || is_front_page() ) {
$include = FALSE;
}
if ( $post == TRUE AND $include == TRUE ) {
return $content;
}
}
If you would like to include your function only on the home page you have two options - only insert the function into the single.php template in your theme, or the prefered method is to only hook your function to the_content when is_single() is true but is_page() is false.
function my_sp_function( $content ) {
$content .= "Some more stuff for the_content!";
return $content;
}
if ( is_single() && ! is_page() ){
add_filter( 'the_content', 'my_sp_function' );
}

Apply wordpress Template of category to posts

I want a post have the template of the parent category.. Is that possible? if yes, please guide me a bit. Or if any plugin is available, name it.
As of Wordpress 3.0, the logic in wp-includes/template-loader.php for selecting a template looks like this:
if ( defined('WP_USE_THEMES') && WP_USE_THEMES ) :
$template = false;
if ( is_404() && $template = get_404_template() ) :
elseif ( is_search() && $template = get_search_template() ) :
elseif ( is_tax() && $template = get_taxonomy_template() ) :
elseif ( is_front_page() && $template = get_front_page_template() ) :
elseif ( is_home() && $template = get_home_template() ) :
elseif ( is_attachment() && $template = get_attachment_template() ) :
remove_filter('the_content', 'prepend_attachment');
elseif ( is_single() && $template = get_single_template() ) :
elseif ( is_page() && $template = get_page_template() ) :
elseif ( is_category() && $template = get_category_template() ) :
elseif ( is_tag() && $template = get_tag_template() ) :
elseif ( is_author() && $template = get_author_template() ) :
elseif ( is_date() && $template = get_date_template() ) :
elseif ( is_archive() && $template = get_archive_template() ) :
elseif ( is_comments_popup() && $template = get_comments_popup_template() ) :
elseif ( is_paged() && $template = get_paged_template() ) :
else :
$template = get_index_template();
endif;
if ( $template = apply_filters( 'template_include', $template ) )
include( $template );
return;
endif;
Checking get_category_template() in wp-includes/theme.php` we see:
function get_category_template() {
$cat_ID = absint( get_query_var('cat') );
$category = get_category( $cat_ID );
$templates = array();
if ( !is_wp_error($category) )
$templates[] = "category-{$category->slug}.php";
$templates[] = "category-$cat_ID.php";
$templates[] = "category.php";
$template = locate_template($templates);
return apply_filters('category_template', $template);
}
Assuming that your category is Foo, that it's slug is foo, and that the Foo category ID is 17, for a post that belongs to category Foo, Wordpress will check for the following templates in your theme and use the first one it finds:
category-foo.php
category-17.php
category.php
Thus, all you should need to do is to create a template named category-foo.php in your theme directory, and set your post's category to Foo, and that post will be rendered using the category-foo.php template instead of the default post.php template.
This mechanism for selecting templates has been present since Wordpress 1.5, though full list of template types has grown significantly over the years.
The Wordpress documentation for this can be found here.

Categories