Sorry that the context is so vague but this problem is very vague. I will try to convey the experience so far.
This is the code I am editing and it is on line 133:
$log->log('info', 'total_data: ' . count( $result->data ) );
The above code worked, when I changed the string to 'total_results' it caused a syntax error:
$log->log('info', 'total_results: ' . count( $result->data ) );
The error randomly changes whenever any characters change. For example the above code made this error
Parse error: syntax error, unexpected ']' in /web/api.php on line 180
FYI: there are only 179 lines in this file.
If I remove one letter from the string:
$log->log('info', 'total_result: ' . count( $result->data ) );
The above error changes to:
Parse error: syntax error, unexpected ']' in /web/api.php on line 181
If I keep removing one letter at a time and refreshing the line number keeps incrementing.
Sometimes if I delete the line, save, refresh, paste it back in, save, refresh it will then work. Sometimes if I add more characters to the string it will work. This must be something on a level beyond syntax errors because the same code will work at a later time. I have tried to restart apache2 but that does not fix anything, it usually displays a different random error. I cannot see any consistency with the error messages. The one consistent thing seems to be that as long as nothing changes the error will stay the same.
Here are other error messages I have seen while trying to find some consistencies:
Parse error: syntax error, unexpected 'DirectMap4k' (T_STRING) in /web/api.php on line 180
Again I deleted one line, saved, refreshed, put that line back, saved, refreshed and then it worked.
logging.php
<?php
class logger {
protected $file = null;
protected $log_level = 3;
protected $_date_fmt = 'Y-m-d H:i:s.u';
protected $_levels = [ 'CRITICAL' => 0,
'ERROR' => 1,
'WARNING' => 2,
'INFO' => 3,
'DEBUG' => 4,
'TRACE' => 5,
'TRACE1' => 6,
'TRACE2' => 7 ];
function __construct( $name = 'Anonymous',
$file = 'application.log' )
{
$this->name = $name;
$this->filename = $file;
// check if file exists before fopen creates the file
$new_file = ! file_exists( $file );
// die if file is not writable
if( ! ( $new_file or is_writable( $file ) )) {
die( "logger class cannot write to file: <b>" . $file . "</b>" );
}
$this->file = fopen( $file, 'a' );
if( $new_file ) {
$this->write( "<"."?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?".">\n\n" );
}
}
public function log( $level, $message )
{
if( $this->_levels[ strtoupper( $level ) ] > $this->log_level ) {
// do not log
return;
}
list( $usec, $sec ) = explode( ' ', microtime() );
$datetime = sprintf( '%s.%03d',
date("Y-m-d H:i:s", $sec),
floor($usec * 1000) );
$write = sprintf( '%s - %8.8s : %s',
$datetime,
strtolower( $level ),
$message );
$this->write( $write . "\n" );
}
private function write( $message )
{
fwrite( $this->file, $message );
}
function __destruct()
{
if( $this->file ) {
fclose( $this->file );
}
}
}
?>
The problem ended up being with Vagrant file synching using HGFS. The files are actually on the host machine and they were not being accurately synchronized. To avoid the random errors, I have resorted to never sharing development code over HGFS.
Related
If I register a script or style (using wp_register_script() or wp_register_style()), is there a way I can get the URL of that script/style?
(If you must know why, I'm trying to put those URL's into another function that generates prefetch link tags so I can prefetch certain scripts/styles for a performance boost in my site.)
Just in case someone is still looking for this:
<?php
function invdr_get_script_uri_by_handler( $handler ){
//Get an instance of WP_Scripts or create new;
$wp_scripts = wp_scripts();
//Get the script by registered handler name
$script = $wp_scripts->registered[ $handler ];
if ( file_exists( ABSPATH . $script->src ) ){
return ABSPATH . $script->src;
}
return false;
}
add_action( 'wp_enqueue_scripts', 'invdr_get_script_uri_by_handler', PHP_INT_MAX );
Tested in wordpress 5.0
You can use wp_scripts() to get the instance of the WP_Scripts class which contains the registered scripts (this class extends WP_Dependencies).
Basically, try looking in:
$wp_scripts = wp_scripts();
var_dump( $wp_scripts->registered );
var_dump( $wp_scripts );
Here's how I've accomplished this in a self-authored plugin to help me enhance dependencies within WordPress:
// Convert relative URL to absolute?
$absolute = true;
$handle = 'your_stylesheet_handle';
$helper = wp_styles();
$object = $helper->registered[ $handle ];
$src = $object->src;
if (
$absolute
&& $helper->in_default_dir( $src )
) {
$src = $helper->base_url . $src;
}
$ver = $object->ver;
if ( ! is_null( $ver ) && empty( $ver ) ) {
$ver = $helper->default_version;
}
if ( isset( $helper->args[ $handle ] ) ) {
$ver = $ver ? $ver . '&' : '';
$ver .= $helper->args[ $handle ];
}
$src = add_query_arg( 'ver', $ver, $src );
$stylesheet_url = urldecode_deep( $src );
Note that the absolute URL conversion is directed towards handling assets registered by WordPress core, as they're typically relative URLs.
One of my projects has intermittent connection problems to gateway.watsonplatform.net. It had been working fine, nothing has changed, but now, 80% of the time, it cannot find hostname in DNS Cache.
I've tried to set CURLOPT_RESOLVE option but it just prepends a line to the output about adding hostname into the cache, but then it's still not found.
I'm trying to run the project locally and I figured out it is network-specific. It works on one access point fine, on the other, it has connection issues.
And another strange hint: curl command in shell works.
There is the output:
Added gateway.watsonplatform.net:80:158.85.132.88 to DNS cache
Hostname was NOT found in DNS cache
Could not resolve host: gateway.watsonplatform.net
UPDATE:
The port should be 443, not 80. After this change, it works. After deleting whole this assignment, it still works. Now, I cannot reproduce the problem.
I had the same problem when I implement one project with IBM Watson inside one Customer. And in this case: You need to release the URL inside your Proxy network.
The reason for cURL and Nodejs calls works is because the server is calling through Firewall, and not passing for your proxy network.
Make sure if the url works when you set the proxy inside your Server: Connect advanced inside your SO (Options internet).
as a little ugly hack to get it working, you could add a hosts file alias, perhaps reinforced with a hourly cronjob to update the real IP - at the cost of the script failing for up to 1 hour every time they actually change IP? (i actually did this a couple of years back when i was facing a similar problem, i don't think i still have the source code though, ill check)
edit: i don't have the original cronjob, but it looked something like this:
<?php
declare(strict_types = 1);
$hosts = '/etc/hosts';
assert ( is_file ( $hosts ) && is_readable ( $hosts ) && is_writable ( $hosts ), 'need access to ' . $hosts );
$host = 'google.com';
$ip = getIP ( $host );
$str = file_get_contents ( $hosts );
$start = strpos ( $str, '#<wtf_cronjob>' );
$end = strpos ( $str, '#</wtf_cronjob>' );
assert ( false !== $start && false !== $end, 'invalid syntax for hosts file!' );
$end += strlen ( '#</wtf_cronjob>' );
$newstr = substr ( $str, 0, $start ) . '#<wtf_cronjob>' . "\n" . $ip . ' ' . $host . ' ' . "\n" . '#</wtf_cronjob>' . substr ( $str, $end );
file_put_contents ( $hosts, $newstr );
function getIP(string $host, string $dns = '8.8.8.8'): string {
exec ( 'host ' . escapeshellarg ( $host ) . ' ' . escapeshellarg ( $dns ) . ' 2>&1', $lines, $ret );
if ($ret !== 0) {
ob_start ();
var_dump ( $lines, $ret );
$debugstr = ob_get_clean ();
throw new \RuntimeException ( 'host failed. debuginfo: ' . $debugstr );
}
foreach ( $lines as $line ) {
if (false === stripos ( $line, 'has address ' )) {
continue;
}
// found it
$ret = trim ( substr ( $line, stripos ( $line, 'has address ' ) + strlen ( 'has address ' ) ) );
if (! filter_var ( $ret, FILTER_VALIDATE_IP )) {
throw new \RuntimeException ( 'the extracted ip address is invalid! (wrong syntax for host?)' );
}
return $ret;
}
throw new \RuntimeException ( 'could not find ip address!' );
}
with the following new lines in the hosts file:
#<wtf_cronjob>
216.58.209.142 google.com
#</wtf_cronjob>
I've tried to assign manually how the domain should be resolved. Since this step, the request is working and after deleting this assignment, it still works.
Months ago, I have placed a 301 redirect rule in my .htaccess file to redirect all the www request to a non-www request.
The problem is two days ago, when I tried to access my example.net site using www.example.net I get the following warnings in the page and website is not loaded.
http://i.stack.imgur.com/nXBMF.png
Here are the corresponding lines:
1. Plugin.php Line 647 = if ( strpos( $file, $realdir ) === 0 ){
Full function:
/**
* Gets the basename of a plugin.
*
* This method extracts the name of a plugin from its filename.
*
* #since 1.5.0
*
* #param string $file The filename of plugin.
* #return string The name of a plugin.
*/
function plugin_basename( $file ) {
global $wp_plugin_paths;
foreach ( $wp_plugin_paths as $dir => $realdir ) {
if ( strpos( $file, $realdir ) === 0 ) { /** LINE 646 */
$file = $dir . substr( $file, strlen( $realdir ) );
}
}
$file = wp_normalize_path( $file );
$plugin_dir = wp_normalize_path( WP_PLUGIN_DIR );
$mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
$file = preg_replace('#^' . preg_quote($plugin_dir, '#') . '/|^' . preg_quote($mu_plugin_dir, '#') . '/#','',$file); // get relative path from plugins dir
$file = trim($file, '/');
return $file;
}
2. Pluggable.php Line 1178 = header("Location: $location", true, $status);
Full file: http://pastebin.com/0zMJZxV0
I use WordPress only to write some articles. My PHP knowledge is very basic and limited only to locate errors.
Please help me figure out the problem with this. As I have read from the Codex FAQ, they say that empty strings may be a culprit for the pluggable.php error. But I have no idea how to locate it and I have attached the file for your reference.
Please provide your suggestions to avoid this error in the future. Thanks in advance.
3. EDIT - wp setting file: (the error line - include_once( $plugin ); )
// Load active plugins.
foreach ( wp_get_active_and_valid_plugins() as $plugin ) {
wp_register_plugin_realpath( $plugin );
include_once( $plugin );
}
unset( $plugin );
The issue with the header information has been discussed in here: Cannot modify header information error in Wordpress. Could you give this a try and see whether this solves this part of your problem?.
On the other issue:
try var_dump ($file) (for instance -- or echo $file ) to see what they actually contain.
Check your configuration path of plugins :
var_dump($wp_plugin_paths);
You've got an error because $realdir is empty.
I have the following php function that renders an array of templates based on an array of paths. In other words, if you provide a set of arrays such as:
$array_template = array(
'carousel' => 'carousel', //type=>name (with out extension).
'mini' => 'mini_feed'
)
$array_paths = array(
'path_one' => 'path/to/one/',
'path_two' => 'path/to/two/'
)
To this function:
protected function _render_templates_array($templates, array $template_name){
foreach($template_name as $type=>$name){
foreach($templates as $template=>$path){
if(file_exists($path . $name . '.phtml')){
require_once($path . $name . '.phtml');
}
}
}
return;
}
It should find and render every file, checking every path for that file.
The problem I am having is, I figured out how to make it stop searching once all files are found, how ever, do I append an else to the if and throw my error? or is there some where else I should throw my error?
Essentially I need:
render all templates, making sure to look in all paths for those templates.
throw an error if the template is not found in any path.
stop processing once all files are loaded.
Thoughts?
Between the two foreach lines, add $found = false;. Inside the if, add $found = true; Between the two "end foreach" }, add if(!$found) throw.....; as needed.
protected function _render_templates_array($templates, array $template_name){
foreach($template_name as $type=>$name){
$found = false;
foreach($templates as $template=>$path){
if(file_exists($path . $name . '.phtml')){
require_once($path . $name . '.phtml');
$found = true;
}
}
if( !$found) throw new .......;
}
return;
}
I have following error:
Error type - 2: preg_replace() [function.preg-replace]: Compilation failed: missing ) at offset 25: in /var/www/hosting/com/galerielaboratorio/scripts/functions.php on line 766
Here is the function:
function getThumbName($photo, $name = 'thumb') {
$ext = preg_replace ("/.*\./", "", $photo);
$photo = preg_replace ("/\.". $ext ."$/", "" , $photo). "." . $name . "." .$ext; // this line causes the error
return $photo;
}
Thanks for help in advance.
Cannot read your expression, but you missed one ). ()-pairs define expression groups. One ( without its corresponding ) is just invalid (and thats what the error message tries to tell you). If you want to have a literal ( you must escape it \(
However, have a look at pathinfo(), explode() and str_replace(). This is not a scenario for regular expressions
$ext = pathinfo($photo, PATHINFO_EXTENSION);
$photo = basename($photo) . '.' . $name . '.' . $ext;
Not sure where it goes wrong; it's not clear from the code, as you're building a dynamic regular expression. Anyway, here's an alternative for you to enjoy, which contains slightly more error checking than your original function.
<?php
function getThumbName($photo, $name = 'thumb') {
if( $ext = getExtension( $photo ) ) {
return basename( $photo, $ext ) . 'thumb.' . $ext;
}
return false;
}
/**
* Returns the file extension if it exists, false otherwise.
* #param string $filename
* #return string|bool
*/
function getExtension( $filename ) {
return pathinfo( $filename, PATHINFO_EXTENSION );
}
echo getThumbName( 'foo.jpg' ) . PHP_EOL; // foo.thumb.jpg
echo getThumbName( 'slightly.more.complex.gif' ) . PHP_EOL; // slightly.more.complex.thumb.gif
echo getThumbname( 'This is where it gets interesting' ) . PHP_EOL; // false.