1

The short story, I'm getting php to prove to me that the file exists and is readable but the script simply seems to stop running once the include statement is hit.

Also you'll note from the data output further down in this question that I'm using an absolute path.

Some notable things that I've tried in resolving this issue is to:

  1. replace include with require and require_once
  2. replace the $include var with a hardcoded string
  3. replace the hardcoded string to an invalid location using require.

In each of these cases the same thing happens, no script is executed beyond the include or require statement and no errors are thrown.

I'm working on a Woocommerce (which is probably a side and irrelevant point, but possibly helpful) system and it appears to be behaving rather oddly. My code is as follows, I've inserted a pile of var_dumps to demonstrate my issue:

/**
 * Output the cart shortcode.
 */
public static function output() {

    var_dump("Hello");

    // Constants
    if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
        define( 'WOOCOMMERCE_CART', true );
    }

    var_dump("After Constants");

    // Update Shipping
    if ( ! empty( $_POST['calc_shipping'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-cart' ) ) {
        self::calculate_shipping();
    }

    var_dump("After Shipping Calc");

    // Check cart items are valid
    do_action( 'woocommerce_check_cart_items' );

    var_dump("After cart items check");

    // Calc totals
    WC()->cart->calculate_totals();

    var_dump("After Calc Totals");

    var_dump(WC()->cart->get_cart());
    var_dump(sizeof( WC()->cart->get_cart() ));

    if ( 0 === sizeof( WC()->cart->get_cart() ) ) {

        var_dump("Size = 0 , Empty");

        wc_get_template( 'cart/cart-empty.php' );
    } else {

        var_dump("Size != 0 , Not Empty");

        wc_get_template( 'cart/cart.php' );
    }
}

Called wc_get_template() Function:

/**
 * Get other templates (e.g. product attributes) passing attributes and including the file.
 *
 * @access public
 * @param string $template_name
 * @param array $args (default: array())
 * @param string $template_path (default: '')
 * @param string $default_path (default: '')
 * @return void
 */
function wc_get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) {

    var_dump("wc_get_template fired. Template name = " . $template_name);

    if ( $args && is_array( $args ) ) {
        extract( $args );
    }

    $located = wc_locate_template( $template_name, $template_path, $default_path );

    var_dump($located);

    if ( ! file_exists( $located ) ) {
        _doing_it_wrong( __FUNCTION__, sprintf( '<code>%s</code> does not exist.', $located ), '2.1' );
        return;
    }

    var_dump(file_exists( $located ), "File exists");

    var_dump(file_get_contents($located));

    // Allow 3rd party plugin filter template file from their plugin
    $located = apply_filters( 'wc_get_template', $located, $template_name, $args, $template_path, $default_path );

    do_action( 'woocommerce_before_template_part', $template_name, $template_path, $located, $args );

    var_dump("After do_action function");

    include $located;

    var_dump("After the file is included");

    do_action( 'woocommerce_after_template_part', $template_name, $template_path, $located, $args );
}

The output:

string 'Hello' (length=5)
string 'After Constants' (length=15)
string 'After Shipping Calc' (length=19)
string 'After cart items check' (length=22)
string 'After Calc Totals' (length=17)
array (size=2)
  'e82c4b19b8151ddc25d4d93baf7b908f' => 
    array (size=10)
      'product_id' => int 2468
      'variation_id' => string '' (length=0)
      'variation' => 
        array (size=0)
          empty
      'quantity' => int 1
      'line_total' => float 27.06
      'line_tax' => float 5.412
      'line_subtotal' => float 27.06
      'line_subtotal_tax' => float 5.412
      'line_tax_data' => 
        array (size=2)
          'total' => 
            array (size=1)
              ...
          'subtotal' => 
            array (size=1)
              ...
      'data' => 
        object(WC_Product_Simple)[101]
          public 'id' => int 2468
          public 'post' => 
            object(WP_Post)[103]
              ...
          public 'product_type' => string 'simple' (length=6)
          protected 'dimensions' => string '' (length=0)
          protected 'shipping_class' => string '' (length=0)
          protected 'shipping_class_id' => int 0
          public 'price' => string '27.06' (length=5)
          public 'manage_stock' => string 'no' (length=2)
          public 'stock_status' => string 'instock' (length=7)
          public 'tax_status' => string 'taxable' (length=7)
          public 'virtual' => string 'no' (length=2)
  '1843e35d41ccf6e63273495ba42df3c1' => 
    array (size=10)
      'product_id' => int 2463
      'variation_id' => string '' (length=0)
      'variation' => 
        array (size=0)
          empty
      'quantity' => int 1
      'line_total' => float 29.7
      'line_tax' => float 5.94
      'line_subtotal' => float 29.7
      'line_subtotal_tax' => float 5.94
      'line_tax_data' => 
        array (size=2)
          'total' => 
            array (size=1)
              ...
          'subtotal' => 
            array (size=1)
              ...
      'data' => 
        object(WC_Product_Simple)[69]
          public 'id' => int 2463
          public 'post' => 
            object(WP_Post)[67]
              ...
          public 'product_type' => string 'simple' (length=6)
          protected 'dimensions' => string '' (length=0)
          protected 'shipping_class' => string '' (length=0)
          protected 'shipping_class_id' => int 0
          public 'price' => string '29.70' (length=5)
          public 'manage_stock' => string 'no' (length=2)
          public 'stock_status' => string 'instock' (length=7)
          public 'virtual' => string 'no' (length=2)
          public 'tax_status' => string 'taxable' (length=7)
int 2
string 'Size != 0 , Not Empty' (length=21)
string 'wc_get_template fired. Template name = cart/cart.php' (length=52)
string '/var/www/public/natural-choice/wp-content/themes/theme524401/woocommerce/cart/cart.php' (length=86)
boolean true
string 'File exists' (length=11)
string '<?php
/**
 * Cart Page
 *
 * @author  WooThemes
 * @package WooCommerce/Templates
 * @version 2.3.8
 */

var_dump("Fired");

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

wc_print_notices();

do_action( 'woocommerce_before_cart' ); ?>

<form action="<?php echo esc_url( WC()->cart->get_cart_url() ); ?>" method="post">

<?php do_action( 'woocommerce_before_cart_table' ); ?>

<table class="shop_table cart" cellspacing="0">
    <thead>
        <tr>
            <th class="product-remove">&nbsp;</th>
            <t'... (length=6908)
string 'After do_action function' (length=24)

You'll also notice that I've edited the file I'm trying to include, so that it will fire another var_dump and print "Fired". But that expected output doesn't happen.

What could the cause of this issue be?

Samuel Hawksby-Robinson
  • 2,652
  • 4
  • 24
  • 25
  • try installing query monitor, its very helpful for debugging. – silver Jan 29 '16 at 16:12
  • try [WP_DEBUG](https://codex.wordpress.org/WP_DEBUG) – Reigel Gallarde Jan 29 '16 at 16:26
  • Thanks for the suggestions guys, I've added implemented a suggestion (http://stackoverflow.com/questions/35088758/php-include-isnt-including-woocommerce/35088889#35088889) and it seems that include and require would stop working was related to error reporting suppression. – Samuel Hawksby-Robinson Jan 29 '16 at 16:29

2 Answers2

1

You should be able to debug this sort of problem by viewing the debug logs. They may be deactivated in your installation.

You can try adding these two lines to the top of the PHP file to enable them. It should help you identify the problem.

error_reporting(E_ALL);
ini_set("display_errors", 1);
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Jason Pate
  • 76
  • 7
  • A good suggestion / comment...but this isn't an answer to the question. – rnevius Jan 29 '16 at 16:09
  • Ah, thank you. Perhaps the fact that I'm working with WooCommerce is relevant after all, it seems that all error reporting for some weird reason is deactivated. I'm getting proper usable feedback now. – Samuel Hawksby-Robinson Jan 29 '16 at 16:10
  • @rnevius Indeed. However, I have seen it many times where an "higher rep'd" than I, submitted answers just like these and were (believe it or not), accepted. *Go figure, eh?!* – Funk Forty Niner Jan 29 '16 at 16:13
  • FWIW, it's OK for an answer to help the OP make progress towards a solution. ["Any answer that gets the asker going in the right direction is helpful..."](http://stackoverflow.com/help/how-to-answer) That said, a simple "try this" with some code isn't a _good answer_. Some motivation and/or explanation will improve such answers. – Mogsdad Jan 31 '16 at 00:47
0

Ah the joys of working with WordPress. It turns out that reason for include and require simply stopping the script execution is because unbeknown to me a number of 3rd party plugins were deactivating and supressing all errors.

After @jason-pate 's suggestion ( PHP Include Isn't Including - Woocommerce ) I got to the bottom of the cause of this problem.

include and required were giving errors they just weren't showing them and weren't simply ceasing the script for no reason, manually activating the errors highlighted that there were syntax issues in the template that was being referenced.

Community
  • 1
  • 1
Samuel Hawksby-Robinson
  • 2,652
  • 4
  • 24
  • 25