Colon operator in PHP - php

This is part of the wordpress code and I don't understand it:
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;
A colon can replace a curly bracket in PHP. So if I substitute the colons, I get this:
if ( is_404() && $template = get_404_template() ) {
elseif ( is_search() && $template = get_search_template() ) {
elseif ( is_tax() && $template = get_taxonomy_template() ) {
...
}
}
}
else
Makes no sense to me, because each elseif is missing its opening if.

Reggie,
colons in if/else statements in PHP : it's NOT about replacing braces
but a PAIR of braces.
Example :
if ($a) : doThis();
elseif ($b) : doThat();
else : doTheOther();
endif;
would become
if ($a) { doThis(); }
elseif ($b) { doThat(); }
else { doTheOther(); }
OR (since it's just one statement and not a block of statements)
if ($a) doThis();
elseif($b) doThat();
else doTheOther();
Reference : Alternative Syntax for Control Structures
As for this specific piece of code :
if ( is_404() && $template = get_404_template() ) :
elseif ( is_search() && $template = get_search_template() ) :
elseif ( is_tax() && $template = get_taxonomy_template() ) :
it translates to
if ( is_404() && $template = get_404_template() )
{ /* DO NOTHING */ }
elseif ( is_search() && $template = get_search_template() )
{ /* DO NOTHING */ }
Hint : The elseif statement does NOT include the other elseif statements. (like elseif ($a) { elseif($b) {} })

Related

Sanitizing the code as per wpcs for yoda condition checks

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.

Order properties should not be accessed directly - WooCommerce 3.0

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()

Wordpress: where are template pages called?

I am trying to integrate wordpress with my previously installed platform.
I was able to edit most of the system as I wish, but I canĀ“t figure out how to intervene in what wordpress does before loading the template file.
What I want to add is a simple conditional statement that either load a page trough my previous cms, or loads normally wordpress.
However, I was unable to understand which function or file loads the components of the theme.
I understand wp uses template hierarchy, loading different files for different types of pages (posts, category page, and so on).
But what function is making this choices and actually loading the files?
Your question isn't clear enough in what you're actually trying to do but /wp-includes/template-loader.php is going to be of particular interest to you.
This is the file that loads the templates. Just to make it clear I'm not suggesting you modify this file I'm merely pointing out where the magic happens.
As soon as it's loaded it runs the action template_redirect. If you wanted to perform some logic then redirect here's where you'd do it. Here's an example that would go in your theme's functions.php:
function wpse_template_redirect() {
if ( condition_a() == condition_b() ) {
wp_redirect( 'url-to-redirect-to' );
exit;
}
}
add_action( 'template_redirect', 'wpse_template_redirect' );
At the bottom of this file there are conditionals which determine which template file is:
$template = false;
if ( is_404() && $template = get_404_template() ) :
elseif ( is_search() && $template = get_search_template() ) :
elseif ( is_front_page() && $template = get_front_page_template() ) :
elseif ( is_home() && $template = get_home_template() ) :
elseif ( is_post_type_archive() && $template = get_post_type_archive_template() ) :
elseif ( is_tax() && $template = get_taxonomy_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;
/**
* Filter the path of the current template before including it.
*
* #since 3.0.0
*
* #param string $template The path of the template to include.
*/
if ( $template = apply_filters( 'template_include', $template ) )
include( $template );
return;

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.

php troubles with coding variables

I am trying to echo certain values if the variable $cardtype ==
$paymentmethod = if( $cardtype == 'visa' ) echo 'VSA';
elseif ( $cardtype == 'mastercard' ) echo 'MSC';
elseif ( $cardtype == 'mastercard' ) echo 'MSC';
elseif ( $cardtype == 'maestro' ) echo 'MAE';
elseif ( $cardtype== 'amex' ) echo 'AMX';
How would I do this???
$types = array( 'visa' => 'VSA', 'mastercard' => 'MSC',
'maestro' => 'MAE', 'amex' => 'AMX' );
echo ( isset( $types[ $cardtype ] ) ) ? $types[ $cardtype ] : 'Wrong card type';
You could use a function containing a switch statement for this:
function GetPaymentMethod( $cardtype )
{
switch( $cardtype )
{
case 'visa':
return 'VSA';
case 'mastercard':
return 'MSC';
case 'maestro':
return 'MAE';
case 'amex':
return 'AMX';
default:
return '<Invalid card type>';
}
}
Test:
echo GetPaymentMethod( 'visa' ); // VSA
Here is one way to do it:
switch($cardtype) {
case 'visa':
echo 'VSA';
break;
case 'mastercard':
echo 'MSC';
break;
}
And so on
for your own code, you have to just remove strange $paymentmethod = from the beginning.
if( $cardtype == 'visa' ) echo 'VSA';
elseif ( $cardtype == 'mastercard' ) echo 'MSC';
elseif ( $cardtype == 'maestro' ) echo 'MAE';
elseif ( $cardtype== 'amex' ) echo 'AMX';
it will work too.

Categories