MySQL insert error on 1st activation
-
Hi,
Just to report below right after installing…
PHP message: WordPress database error Column ‘get_option’ cannot be null for query INSERT INTO
wp_harrys_gravatar_cache(size,size_get,get_option,cache_time,is_writeable,file_get_contents,fopen,curl,copy) VALUES (1, 1, NULL, NULL, NULL, NULL, NULL, NULL, NULL) made by activate_plugin, do_action(‘activate_harrys-gravatar-cache/harrys-gravatar-cache.php’), harrys_gravatar_cache_activation, get_size_gravatar”Solved by manually inserting according to your screenshots:
INSERT INTOwp_harrys_gravatar_cache(size,size_get,get_option,cache_time,is_writeable,file_get_contents,fopen,curl,copy) VALUES (1, 67, 4, 40320, 1, 1, 1, 1, 1);
-
Correction to the quick fix: INSERT INTO
wp_harrys_gravatar_cache(id,size,size_get,get_option,cache_time,is_writeable,file_get_contents,fopen,curl,copy) VALUES (1, 67, 1, 4, 40320, 1, 1, 1, 1, 1);Hi Nazar78,
thanks for using my plugin:)
The values shouldn’t be “NULL” by activate the plugin.
I couldn’t produce the error in my tests today.Could you produce the error again after reinstall the plugin?
Best regards,
HarryHi Harry Milatz,
Yes it still produce the same error after reinstall. I’m not sure if the PHP7.0.2 version I’m using would cause it? I haven’t got a chance to find out more but;
harrys-gravatar-cache.php:line:137:
if($nothing_set) {
$wpdb->insert($table, array(‘size’ => $avatar_size, ‘size_get’ => $size_get, ‘get_option’ => $get_option, ‘cache_time’ => $cache_time, ‘is_writeable’ => $is_writeable, ‘file_get_contents’ => $file_get_contents, ‘fopen’ => $fopen, ‘curl’ => $curl, ‘copy’ => $copy), array(‘id’ => 1), array(‘%d’, ‘%d’, ‘%d’, ‘%d’, ‘%d’, ‘%d’, ‘%d’, ‘%d’, ‘%d’));
}Above only $avatar_size and $size_get is being set in the function get_size_gravatar. The rest of the values like $get_option onwards are undefined when the table queried as empty ‘$nothing_set’.
Hi Nazar78,
I have not tested the plugin with PHP 7 already. But there should not be this issues with the empty variables, PHP functions such ini_get exists still in PHP 7. Is yout php.ini in anyway “blocked”?
the $nothing_set is true at the first activate(when the table still doesn’t exists) and the $avatar_size and so on are set the function before.
The function to get $get_option and the others are already called from the function in line 59.Hi Harry Milatz,
I’m not sure if this is the global/local scope thingy, but I highly doubt it. My php.ini contents are mostly left default untouched after installing PHP7.0.2-1 FPM Debian build.
In line 59, function harrys_gravatar_cache_activation() called function get_size_gravatar first (containing the DB insert) before function get_copy_options (where $get_option and the rest gets declared) during activation.
May be you could simulate by deleting row#1 then refresh the admin panel? If the row#1 is missing, it will try to reinsert, in my case resulted in the NULLs.
Hi Nazar78,
I deinstall and reinstall it and it works fine on my local installation and on the webserver.
get_size_gravatar try to get the size for the avatars from the templates, if there is no success it is manually set to 67.
The function get_copy_options should found one of the four copy options and should not be empty. PHP copy should work at all.
I will make a fallback for this behaviour in the next update today and give you an info here.
By the way, deleting this ROW and refresh the admin panel would not insert the table. It is only insert on activation if it doesn’t exist.
Deactivating the plugin and then deleting the database table and then activate the plugin insert the database table again on the webserver and on my local install.
Hi Nazar78,
could you deactivate the plugin and replace the code from the harrys-gravatar-cache.php with the code below and then activate the plugin?
<?php /* /** * Plugin Name: Harrys Gravatar Cache * Plugin URI: http://www.all4hardware4u.de * Description: Beschleunigt die Website durch simples und effektives Caching von Gravataren (Globally Recognized Avatars) damit diese vom eigenenem Webserver ausgeliefert werden und nicht vom Gravatar-Server nachgeladen werden müssen. * Version: 1.3.8 * Author: Harry Milatz * Author URI: http://www.all4hardware4u.de * Text Domain: harrys-gravatar-cache * Domain Path: /languages * License: GPL3 Copyright 2015 Harry Milatz (email : [email protected]) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/> */ if (!defined('ABSPATH')) exit; // Verlassen bei direktem Zugriff require_once( ABSPATH . "wp-includes/pluggable.php" ); // Datenbanktabelle global $wpdb; $table=$wpdb->prefix.'harrys_gravatar_cache'; // Pfade festlegen $plugins_dir=str_replace(home_url(), '',plugins_url()); $plugin_dir = basename(dirname(__FILE__)); $path=wp_upload_dir(); $path=$path['basedir']."/gravatar-cache/"; // Load translations load_plugin_textdomain('harrys-gravatar-cache',$plugins_dir.'/'.$plugin_dir.'/languages',$plugin_dir.'/languages'); /* Install */ if( is_admin() ) { add_action( 'admin_menu', 'harry_add_pages' ); add_action( 'admin_init', 'save_settings'); } /* Einstellungsseite den Einstellungen hinzufügen */ function harry_add_pages() { add_options_page( __( 'Harrys Gravatar Cache Settings', 'harrys-gravatar-cache' ), __( 'Harrys Gravatar Cache Settings', 'harrys-gravatar-cache' ), 'manage_options', 'harrys-gravatar-cache-options', 'Einstellungen' ); } // bei Aktivierung des Plugins function harrys_gravatar_cache_activation() { global $wpdb; $table=$wpdb->prefix.'harrys_gravatar_cache'; $path=wp_upload_dir(); $path=$path['basedir']."/gravatar-cache/"; make_folder($path); get_size_gravatar($table); get_copy_options($table, $path); is_writeable_proof($table, $path); $cache_time=40320; $wpdb->update($table, array('cache_time' => $cache_time), array('id' => 1), array('%d')); } register_activation_hook( __FILE__, 'harrys_gravatar_cache_activation' ); // Uninstall function harrys_gravatar_cache_uninstall() { global $wpdb; $table=$wpdb->prefix.'harrys_gravatar_cache'; $wpdb->query( "DROP TABLE <code>{$table}</code> " ); $path=wp_upload_dir(); $path=$path['basedir']."/gravatar-cache/"; empty_cache($path); @rmdir("$path"); } register_uninstall_hook( __FILE__, 'harrys_gravatar_cache_uninstall' ); /* Funktionen für die Einstellungsseite und Install */ // Cache Directory anlegen function make_folder($path) { $path_ok=false; if (!is_dir($path)) { if (@mkdir("$path", 0755, true)) { $path_ok=1; } else { $path_ok=false; } } else { $path_ok=1; } } //Ordnerberechtigungen korrigieren function correct_folder($path) { chmod($path,0755); } // Dateien des Templates nach Avatargrösse durchsuchen function get_size_gravatar($table) { $dateinamen=array(); $dateinamen[]=get_template_directory().'/functions.php'; $dateinamen[]=get_template_directory().'/single.php'; $dateinamen[]=get_template_directory().'/comments.php'; $dateinamen[]=get_template_directory().'/includes/functions/comments.php'; foreach($dateinamen as $dateiname) { $fp=@fopen($dateiname,"r"); if($fp) { $datei_inhalt=@fread($fp,filesize($dateiname)); if($datei_inhalt) { preg_match('/avatar_size=(\d*)/',$datei_inhalt,$size); preg_match('/get_avatar\(\D*?(\d*)\D*?\)/',$datei_inhalt,$size); if(is_numeric($size[1])) { $avatar_size=$size[1]; $size_get=1; break; } } } } if (!is_numeric($avatar_size)) { $avatar_size=67; $size_get=2; } global $wpdb; $wpdb->query ("CREATE TABLE IF NOT EXISTS <code>{$table}</code> ( <code>id</code> int(11) unsigned NOT NULL auto_increment, <code>size</code> int(11) NOT NULL, <code>size_get</code> int(11) NOT NULL, <code>get_option</code> int(11) NOT NULL, <code>cache_time</code> int(11) NOT NULL, <code>is_writeable</code> int(11) NOT NULL, <code>file_get_contents</code> int(11) NOT NULL, <code>fopen</code> int(11) NOT NULL, <code>curl</code> int(11) NOT NULL, <code>copy</code> int(11) NOT NULL, PRIMARY KEY (<code>id</code>) )"); if( !$wpdb->get_var($wpdb->prepare("SELECT size, size_get, get_option, cache_time, is_writeable, file_get_contents, fopen, curl, copy FROM $table WHERE ID = %d", 1) ) ) { $nothing_set = true; } if($nothing_set) { if(empty($avatar_size) || $avatar_size==0){$avatar_size=67;} if(empty($size_get) || $size_get==0){$size_get=2;} if(empty($get_option) || $get_option==0){$get_option=4;} if(empty($cache_time) || $cache_time==0){$cache_time=40320;} if(empty($file_get_contents)){$file_get_contents=0;} if(empty($fopen)){$fopen=0;} if(empty($curl)){$curl=0;} if(empty($copy) || $copy==0){$copy=1;} $wpdb->insert($table, array('size' => $avatar_size, 'size_get' => $size_get, 'get_option' => $get_option, 'cache_time' => $cache_time, 'is_writeable' => $is_writeable, 'file_get_contents' => $file_get_contents, 'fopen' => $fopen, 'curl' => $curl, 'copy' => $copy), array('id' => 1), array('%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')); } $wpdb->update($table, array('size' => $avatar_size, 'size_get' => $size_get), array('id' => 1), array('%d')); return $nothing_set; } if($nothing_set) { get_size_gravatar($table); get_copy_options($table, $path); } // Cache leeren function empty_cache($path) { if ($dh = opendir($path)) { while (($file = readdir($dh)) !== false) { if ($file!="." AND $file !="..") { @unlink("$path$file"); } } closedir($dh); } } // Kopieroption festlegen function get_copy_options($table, $path) { $get_option=0; $file_get_contents=0; $fopen=0; $curl=0; $copy=0; $url=plugin_dir_url('avatar-no.jpg'); $testfile=$path."/test.png"; //file_get_contents if (ini_get('allow_url_fopen') && function_exists('file_get_contents')) { $get_option=1; $file_get_contents=1; } //fopen if (ini_get('allow_url_fopen')) { if (false===$fh=wp_remote_fopen($url,false)) { $fopen=0; } else { $get_option=2; $fopen=1; } } //cURL if (function_exists('curl_init')) { $get_option=3; $curl=1; } //PHP Copy @copy($url, $testfile); if (file_exists($testfile)) { $get_option=4; $copy=1; @unlink($testfile); } global $wpdb; $wpdb->update($table, array('get_option' => $get_option, 'file_get_contents' => $file_get_contents, 'fopen' => $fopen, 'curl' => $curl, 'copy' => $copy), array('id' => 1), array('%d')); } // Cacheordner beschreibbar ? function is_writeable_proof($table, $path) { $is_writeable=0; if(is_writable($path)) { $is_writeable=1; } global $wpdb; $wpdb->update($table, array('is_writeable' => $is_writeable), array('id' => 1), array('%d')); } /* Einstellungsseite */ function Einstellungen() { if ( current_user_can( 'manage_options' ) ) { ?> <div class="wrap"> <h1>Harrys Gravatar Cache</h1> <small><?php _e('Accelerates the site speed by simply and effective caching Gravatar (Globally Recognized Avatars) so that they are delivered from the own web server and do not need to be reloaded from the Gravatar server.','harrys-gravatar-cache')?></small> <h2><?php _e('Settings','harrys-gravatar-cache'); ?></h2> <?php global $wpdb; $table=$wpdb->prefix.'harrys_gravatar_cache'; $size=$wpdb->get_var($wpdb->prepare("SELECT size FROM $table WHERE ID = %d", 1) ); $size_get=$wpdb->get_var($wpdb->prepare("SELECT size_get FROM $table WHERE ID = %d", 1) ); $get_option=$wpdb->get_var($wpdb->prepare("SELECT get_option FROM $table WHERE ID = %d", 1) ); $cache_time=$wpdb->get_var($wpdb->prepare("SELECT cache_time FROM $table WHERE ID = %d", 1) ); $is_writeable=$wpdb->get_var($wpdb->prepare("SELECT is_writeable FROM $table WHERE ID = %d", 1) ); $file_get_contents=$wpdb->get_var($wpdb->prepare("SELECT file_get_contents FROM $table WHERE ID = %d", 1) ); $fopen=$wpdb->get_var($wpdb->prepare("SELECT fopen FROM $table WHERE ID = %d", 1) ); $curl=$wpdb->get_var($wpdb->prepare("SELECT curl FROM $table WHERE ID = %d", 1) ); $copy=$wpdb->get_var($wpdb->prepare("SELECT copy FROM $table WHERE ID = %d", 1) ); $rating = strtolower(get_option('avatar_rating')); if($get_option==1){$copy_option="WordPress Filesystem (file_get_contents)";} if($get_option==2){$copy_option="WordPress Remote Fopen (fopen / cUrl)";} if($get_option==3){$copy_option="WordPress Remote Fopen (fopen / cUrl)";} if($get_option==4){$copy_option="PHP copy";} if($cache_time==10080){$cache_week=1;} if($cache_time==20160){$cache_week=2;} if($cache_time==30240){$cache_week=3;} if($cache_time==40320){$cache_week=4;} if($cache_time==50400){$cache_week=5;} if($cache_time==60480){$cache_week=6;} if($cache_time==70560){$cache_week=7;} if($cache_time==80640){$cache_week=8;} $database_set_wrong=0; if(empty($size) || $size<20 || $size>200 || empty($size_get) || $size_get==0 || ($get_option>4 || $get_option<0) || empty($cache_time) || $cache_time==0 || ($is_writeable!=0 && $is_writeable!=1) ) {$database_set_wrong=1;} $path=wp_upload_dir(); $cache_url=$path['baseurl']."/gravatar-cache/"; $path=$path['basedir']."/gravatar-cache/"; $path_ok=false; $pathperm=substr(sprintf('%o', fileperms($path)),-4); if (is_dir($path)) { $path_ok=1; } if($file_get_contents==1 || $fopen==1 || $curl==1 || $copy==1){$copy_available=1;} ?> <form id="harrys-gravatar-cache" action="" method="post"> <?php if($database_set_wrong==1){?> <table class="form-table"> <tbody> <tr valign="top"> <td><font style="font-size:20px;color:red;"> <?php printf(__('Attention!! The database table "%1$s" could not be filled with the neccessary options for this plugin!!','harrys-gravatar-cache'),$table); ?> </font><br><br> <?php _e('Here is an overview of the options:','harrys-gravatar-cache');?><br> <?php _e('Gravatar Size:','harrys-gravatar-cache');?> <?php if(empty($size) || $size==0){?><font style="color:red"><?php _e('false or empty','harrys-gravatar-cache');?></font>, <?php _e('should be a number between 20 and 200','harrys-gravatar-cache');?><?php } else { ?><font style="color:green">ok</font><?php } ?><br> <?php _e('How to get the Gravatar Size:','harrys-gravatar-cache');?> <?php if(empty($size_get) || $size_get==0){?><font style="color:red"><?php _e('false or empty','harrys-gravatar-cache');?></font>, <?php _e('should be a number between 1 and 3','harrys-gravatar-cache');?><?php } else { ?><font style="color:green">ok</font><?php } ?><br> <?php _e('Copy option:','harrys-gravatar-cache');?> <?php if(empty($get_option)){?><font style="color:red"><?php _e('false or empty','harrys-gravatar-cache');?></font>, <?php _e('should be a number between 1 and 4','harrys-gravatar-cache');?><?php } else { ?><font style="color:green">ok</font><?php } ?><br> <?php _e('Cache time:','harrys-gravatar-cache');?> <?php if(empty($cache_time) || $cache_time==0){?><font style="color:red"><?php _e('false or empty','harrys-gravatar-cache');?></font>, <?php _e('should be a number in seconds between 10080 and 80640','harrys-gravatar-cache');?><?php } else { ?><font style="color:green">ok</font><?php } ?><br> <?php _e('Cache folder writeable:','harrys-gravatar-cache');?> <?php if(empty($is_writeable)){?><font style="color:red"><?php _e('false or empty','harrys-gravatar-cache');?></font>, <?php _e('should be a number 0 or 1','harrys-gravatar-cache');?><?php } else { ?><font style="color:green">ok</font><?php } ?><br> </td> </tr> <tr valign="top"> <td> <?php _e('These error(s) can not be solved with the plugin settings. Try to enable debugging and check the log-files. After you found a reason for the issue and eliminate this, try to deactivate and deinstall and after this reinstall the plugin.','harrys-gravatar-cache'); ?> <br> </td> </tr> <tr valign="top"> <td> <?php printf(__('The database table "%1$s" should look like this:','harrys-gravatar-cache'),$table); ?> <br> <img src="<?php echo plugin_dir_url( __FILE__ );?>database.png" /> </td> </tr> </tbody> </table> <?php } else { ?> <table class="form-table"> <tbody> <?php if(empty($rating)){?> <tr valign="top"> <td><font style="color:orange;"> <?php _e('Attention!! the rating option for Gravatars is empty. It is set to rating "R" for getting the (Gr)Avatars.','harrys-gravatar-cache');?> </font><br> <?php _e('Please check the rating on your <a href="options-discussion.php">"Discussion setting page"</a>.','harrys-gravatar-cache');?> </td> </tr> <?php } ?> <?php if($path_ok!=1){?> <tr valign="top"> <td><font style="font-size:20px;color:red;"> <?php _e('Attention!! the caching folder is not exist!!','harrys-gravatar-cache');?> </font><br> <?php _e('Please press the button to create the caching folder.','harrys-gravatar-cache');?> </td> </tr> <p> <tr valign="top"> <td> <input class="button" type="submit" name="make_folder" value="<?php _e('create folder','harrys-gravatar-cache'); ?>"/> </td> </tr> </p> <?php } ?> <?php if($pathperm!="0755" && $path_ok==1){?> <tr valign="top"> <td><font style="font-size:20px;color:red;"> <?php _e('Attention!! the permissions of the caching folder are not correct!!','harrys-gravatar-cache');?> </font><br> <?php _e('Please press the button to correct the permissions for the caching folder or change manually the permissions to 0755.','harrys-gravatar-cache');?> </td> </tr> <p> <tr valign="top"> <td> <input class="button" type="submit" name="correct_folder" value="<?php _e('correct permissions','harrys-gravatar-cache'); ?>"/> </td> </tr> </p> <?php } ?> <?php if($is_writeable!=1 && $path_ok==1){?> <tr valign="top"> <td><font style="font-size:20px;color:red;"> <?php _e('Attention!! the caching folder is not writeable!!','harrys-gravatar-cache');?> </font><br> <?php _e('Please change the permissions to 0755 for the caching folder "','harrys-gravatar-cache');?> <font style="color:green;"><strong><?php echo $path; ?></strong></font> <?php _e('" and check.','harrys-gravatar-cache');?> </td> </tr> <p> <tr valign="top"> <td> <input class="button" type="submit" name="is_writeable" value="<?php _e('check if the caching folder is writeable','harrys-gravatar-cache'); ?>"/> </td> </tr> </p> <?php } ?> <?php if(($get_option>4 || $get_option<1) && $is_writeable==1 && $path_ok==1){?> <tr valign="top"> <td><font style="font-size:20px;color:red;"> <?php _e('Attention!! NO option for getting the Gravatars is available!!','harrys-gravatar-cache');?> </font><br> <?php _e('Please contact your hoster to make one of these functions available:','harrys-gravatar-cache');?> <br>"file_get_contents"<br>"fopen"<br>"cUrl"<br>"PHP copy"</td> </tr> <p> <tr valign="top"> <td> <input class="button" type="submit" name="get_copy_options" value="<?php _e('check the server for available copy options','harrys-gravatar-cache'); ?>"/> </td> </tr> </p> <?php } ?> </tbody> </table> <hr> <table class="form-table"> <tbody> <tr valign="top"> <th scope="row"><?php _e('current Gravatar size:','harrys-gravatar-cache'); ?></th><td><?php echo $size; ?> px, <?php if($size_get==1){_e('set by template','harrys-gravatar-cache');}else if($size_get==2){_e('set by plugin','harrys-gravatar-cache');}else if($size_get==3){_e('set by you','harrys-gravatar-cache');} ?></td> </tr> <tr valign="top"> <th scope="row"><?php _e('change Gravatar size:','harrys-gravatar-cache'); ?></th> <td><select name="size"> <?php if($size!=40 && $size!=44 && $size!=67 && $size!=80 && $size!=96 && $size!=120) {?><option value="<?php echo $size; ?>" <?php if($size_get!=1) echo "selected=selected";?>><?php echo $size; ?> px, <?php if($size_get==1){_e('set by template','harrys-gravatar-cache');}else if($size_get==2){_e('set by plugin','harrys-gravatar-cache');}else if($size_get==3){_e('set by you','harrys-gravatar-cache');} ?></option><?php } ?> <option value="40" <?php if($size==40) echo "selected=selected";?>>40 px<?php if($size_get==1 && $size==40){ ?>, <?php _e('set by template','harrys-gravatar-cache');}else if($size_get==2 && $size==40){ ?>, <?php _e('set by plugin','harrys-gravatar-cache');}else if($size_get==3 && $size==40){ ?>, <?php _e('set by you','harrys-gravatar-cache');} ?></option> <option value="44" <?php if($size==44) echo "selected=selected";?>>44 px<?php if($size_get==1 && $size==44){ ?>, <?php _e('set by template','harrys-gravatar-cache');}else if($size_get==2 && $size==44){ ?>, <?php _e('set by plugin','harrys-gravatar-cache');}else if($size_get==3 && $size==44){ ?>, <?php _e('set by you','harrys-gravatar-cache');} ?></option> <option value="67" <?php if($size==67) echo "selected=selected";?>>67 px<?php if($size_get==1 && $size==67){ ?>, <?php _e('set by template','harrys-gravatar-cache');}else if($size_get==2 && $size==67){ ?>, <?php _e('set by plugin','harrys-gravatar-cache');}else if($size_get==3 && $size==67){ ?>, <?php _e('set by you','harrys-gravatar-cache');} ?></option> <option value="80" <?php if($size==80) echo "selected=selected";?>>80 px<?php if($size_get==1 && $size==80){ ?>, <?php _e('set by template','harrys-gravatar-cache');}else if($size_get==2 && $size==80){ ?>, <?php _e('set by plugin','harrys-gravatar-cache');}else if($size_get==3 && $size==80){ ?>, <?php _e('set by you','harrys-gravatar-cache');} ?></option> <option value="96" <?php if($size==96) echo "selected=selected";?>>96 px<?php if($size_get==1 && $size==96){ ?>, <?php _e('set by template','harrys-gravatar-cache');}else if($size_get==2 && $size==96){ ?>, <?php _e('set by plugin','harrys-gravatar-cache');}else if($size_get==3 && $size==96){ ?>, <?php _e('set by you','harrys-gravatar-cache');} ?></option> <option value="120" <?php if($size==120) echo "selected=selected";?>>120 px<?php if($size_get==1 && $size==120){ ?>, <?php _e('set by template','harrys-gravatar-cache');}else if($size_get==2 && $size==120){ ?>, <?php _e('set by plugin','harrys-gravatar-cache');}else if($size_get==3 && $size==120){ ?>, <?php _e('set by you','harrys-gravatar-cache');} ?></option> </select></td> </tr> <tr valign="top"> <th scope="row"><?php _e('or enter Gravatar size manually (20-200 px):','harrys-gravatar-cache'); ?></th> <td><input pattern="[0-9]{2,4}" style="width:75px" min="20" max="200" step="1" name="size_man" type="number" value="" /> px </td> </tr> <?php if($size_get!=1){ ?> <p> <tr valign="top"> <td> <input class="button" type="submit" name="get_size_gravatar" value="<?php _e('Try to get the Gravatar size from the template','harrys-gravatar-cache'); ?>"/> </td> </tr> </p> <?php } ?> <?php if($is_writeable==1 && $path_ok==1 && $copy_available==1){?> <tr valign="top"> <th scope="row"><?php _e('your server accepts the following copy options to get the Gravatar:','harrys-gravatar-cache'); ?></th><td><?php if($file_get_contents==1){echo "file_get_contents (<strong>wp_filesystem</strong>)<br>";}if($fopen==1){echo "fopen (<strong>wp_remote_fopen</strong>)<br>";}if($curl==1){echo "cUrl (<strong>wp_remote_fopen</strong>)<br>";}if($copy==1){echo "PHP copy<br>";}?></td> </tr> <tr valign="top"> <th scope="row"><?php _e('current copy option:','harrys-gravatar-cache'); ?></th><td><?php echo $copy_option; ?></td> </tr> <tr valign="top"> <th scope="row"><?php _e('change copy option:','harrys-gravatar-cache'); ?></th> <td><select name="copy_option"> <?php if($file_get_contents==1){?><option value="1" <?php if($get_option==1) echo "selected=selected";?>>WordPress Filesystem (file_get_contents)</option><?php } ?> <?php if($fopen==1 || $curl==1){?><option value="2" <?php if($get_option==2 || $get_option==3) echo "selected=selected";?>>WordPress Remote Fopen (fopen / cUrl)</option><?php } ?> <?php if($copy==1){?><option value="4" <?php if($get_option==4) echo "selected=selected";?>>PHP copy</option><?php } ?> </select></td> </tr> <p> <tr valign="top"> <td> <input class="button" type="submit" name="get_copy_options" value="<?php _e('check the server for available copy options','harrys-gravatar-cache'); ?>"/> </td> </tr> </p> <?php }?> <tr valign="top"> <th scope="row"><?php _e('current Cache time:','harrys-gravatar-cache'); ?></th><td><?php echo $cache_week; if($cache_week<2){_e(' week','harrys-gravatar-cache');}else{_e(' weeks','harrys-gravatar-cache');} ?></td> </tr> <tr valign="top"> <th scope="row"><?php _e('change Cache time:','harrys-gravatar-cache'); ?></th> <td><select name="cache-time"> <option value="10080" <?php if($cache_time==10080) echo "selected=selected";?>>1<?php _e(' week','harrys-gravatar-cache');?></option> <option value="20160" <?php if($cache_time==20160) echo "selected=selected";?>>2<?php _e(' weeks','harrys-gravatar-cache');?></option> <option value="30240" <?php if($cache_time==30240) echo "selected=selected";?>>3<?php _e(' weeks','harrys-gravatar-cache');?></option> <option value="40320" <?php if($cache_time==40320) echo "selected=selected";?>>4<?php _e(' weeks','harrys-gravatar-cache');?></option> <option value="50400" <?php if($cache_time==50400) echo "selected=selected";?>>5<?php _e(' weeks','harrys-gravatar-cache');?></option> <option value="60480" <?php if($cache_time==60480) echo "selected=selected";?>>6<?php _e(' weeks','harrys-gravatar-cache');?></option> <option value="70560" <?php if($cache_time==70560) echo "selected=selected";?>>7<?php _e(' weeks','harrys-gravatar-cache');?></option> <option value="80640" <?php if($cache_time==80640) echo "selected=selected";?>>8<?php _e(' weeks','harrys-gravatar-cache');?></option> </select></td> </tr> </tbody> </table> <p class="submit"> <?php wp_nonce_field( 'harrys_gravatar_cache_options', 'harrys_gravatar_cache_options', false ); ?> <input class="button-primary" type="submit" name="harry_gravatar_save" value="<?php _e('Save changes','harrys-gravatar-cache'); ?>"/> <input class="button" type="submit" name="harry_gravatar_empty_cache" value="<?php _e('Empty Cache','harrys-gravatar-cache'); ?>"/> </p> <hr> <?php if($get_option<5 && $get_option>0 && $is_writeable==1 && $path_ok==1 && $pathperm=="0755") { ?> <h2><?php _e('Statistics','harrys-gravatar-cache'); ?></h2> <table class="form-table"> <tbody> <?php if ($dh = opendir($path)) { $count=0; $filesize=0; while (($file = readdir($dh)) !== false) { if ($file!="." AND $file !="..") { $count++; $filesize=filesize($path.$file)+$filesize; } } closedir($dh); } if($filesize>1024000){ $filesize_show=round($filesize/1024/1024, 2); $filesize_show=$filesize_show." MBytes"; } else if($filesize>10240){ $filesize_show=round($filesize/1024, 2); $filesize_show=$filesize_show." kBytes"; } else { $filesize_show=$filesize." Bytes"; } ?> <tr valign="top"> <th scope="row"><?php _e('Number of files in cache:','harrys-gravatar-cache'); ?></th><td><?php echo $count; ?></td> </tr> <tr valign="top"> <th scope="row"><?php _e('Filesize of cached files:','harrys-gravatar-cache'); ?></th><td><?php echo $filesize_show; ?></td> </tr> <tr valign="top"> <th scope="row"><?php _e('Cache URL:','harrys-gravatar-cache'); ?></th><td><?php echo $cache_url; ?></td> </tr> <tr valign="top"> <th scope="row"><?php _e('Cache Path:','harrys-gravatar-cache'); ?></th><td><?php echo $path; ?></td> </tr> </tbody> </table> <?php } ?> <?php } ?> </form> </div> <?php } else { wp_die( __( 'You do not have sufficient permissions to access this page.', 'harrys-gravatar-cache' ) ); } } /* Einstellungen speichern */ function save_settings() { global $wpdb; $table=$wpdb->prefix.'harrys_gravatar_cache'; $path=wp_upload_dir(); $path=$path['basedir']."/gravatar-cache/"; if(stripos($_SERVER['REQUEST_URI'],'/options-general.php?page=harrys-gravatar-cache-options')!==FALSE) { global $wpdb; if( (isset($_POST['size']) || isset($_POST['size_man']) ) && isset($_POST['harry_gravatar_save']) && !isset($_POST['get_size_gravatar']) && !isset($_POST['harry_gravatar_empty_cache']) && !isset($_POST['get_copy_options']) && !isset($_POST['is_writeable']) && !isset($_POST['make_folder']) && !isset($_POST['correct_folder'])) { check_ajax_referer( 'harrys_gravatar_cache_options', 'harrys_gravatar_cache_options' ); $size=$wpdb->get_var($wpdb->prepare("SELECT size FROM $table WHERE ID = %d", 1) ); if($size!=$_POST['size'] || $size!=$_POST['size_man']) { if($_POST['size_man']!=0 || !empty($_POST['size_man'])) { if($size!=$_POST['size_man']) { $wpdb->update($table, array('size' => $_POST['size_man'], 'size_get' => '3'), array('id' => 1), array('%d', '%d')); } else { $wpdb->update($table, array('size' => $_POST['size_man']), array('id' => 1), array('%d', '%d')); } } else { if($size!=$_POST['size']) { $wpdb->update($table, array('size' => $_POST['size'], 'size_get' => '3'), array('id' => 1), array('%d', '%d')); } else { $wpdb->update($table, array('size' => $_POST['size']), array('id' => 1), array('%d', '%d')); } } empty_cache($path); } } if(isset($_POST['copy_option']) && isset($_POST['harry_gravatar_save']) && !isset($_POST['get_size_gravatar']) && !isset($_POST['harry_gravatar_empty_cache']) && !isset($_POST['get_copy_options']) && !isset($_POST['is_writeable']) && !isset($_POST['make_folder']) && !isset($_POST['correct_folder'])) { check_ajax_referer( 'harrys_gravatar_cache_options', 'harrys_gravatar_cache_options' ); $get_option=$wpdb->get_var($wpdb->prepare("SELECT get_option FROM $table WHERE ID = %d", 1) ); if($get_option!=$_POST['copy_option']){ $wpdb->update($table, array('get_option' => $_POST['copy_option']), array('id' => 1), array('%d')); } } if(isset($_POST['cache-time']) && isset($_POST['harry_gravatar_save']) && !isset($_POST['get_size_gravatar']) && !isset($_POST['harry_gravatar_empty_cache']) && !isset($_POST['get_copy_options']) && !isset($_POST['is_writeable']) && !isset($_POST['make_folder']) && !isset($_POST['correct_folder'])) { check_ajax_referer( 'harrys_gravatar_cache_options', 'harrys_gravatar_cache_options' ); $cache_time=$wpdb->get_var($wpdb->prepare("SELECT cache_time FROM $table WHERE ID = %d", 1) ); if($cache_time!=$_POST['cache_time']){ $wpdb->update($table, array('cache_time' => $_POST['cache-time']), array('id' => 1), array('%d')); } } if(isset($_POST['make_folder'])) { check_ajax_referer( 'harrys_gravatar_cache_options', 'harrys_gravatar_cache_options' ); make_folder($path); is_writeable_proof($table, $path); } if(isset($_POST['correct_folder'])) { check_ajax_referer( 'harrys_gravatar_cache_options', 'harrys_gravatar_cache_options' ); correct_folder($path); is_writeable_proof($table, $path); } if(isset($_POST['is_writeable'])) { check_ajax_referer( 'harrys_gravatar_cache_options', 'harrys_gravatar_cache_options' ); is_writeable_proof($table, $path); } if(isset($_POST['get_size_gravatar'])) { check_ajax_referer( 'harrys_gravatar_cache_options', 'harrys_gravatar_cache_options' ); get_size_gravatar($table); empty_cache($path); } if(isset($_POST['get_copy_options'])) { check_ajax_referer( 'harrys_gravatar_cache_options', 'harrys_gravatar_cache_options' ); get_copy_options($table, $path); } if(isset($_POST['harry_gravatar_empty_cache'])) { check_ajax_referer( 'harrys_gravatar_cache_options', 'harrys_gravatar_cache_options' ); empty_cache($path); } } } /* Caching und return Funktion */ function gravatar_lokal ($avatar, $id_or_email, $size, $default, $alt) { preg_match( '/class=\'(.*?)\'/s', $avatar, $css); $css=$css[1]; preg_match( '/src=\'(.*?)\'/s', $avatar, $src_proof); $src_proof=$src_proof[1]; if(empty($css)){ preg_match( '/class=\"(.*?)\"/s', $avatar, $css); $css=$css[1]; } if(empty($src_proof)){ preg_match( '/src=\"(.*?)\"/s', $avatar, $src_proof); $src_proof=$src_proof[1]; } global $wpdb; $table=$wpdb->prefix.'harrys_gravatar_cache'; $cache_time=$wpdb->get_var($wpdb->prepare("SELECT cache_time FROM $table WHERE ID = %d", 1) ); $path=wp_upload_dir(); $path_file=$path['baseurl']."/gravatar-cache/"; $path=$path['basedir']."/gravatar-cache/"; $mail=get_comment_author_email(); $author=get_comment_author($author); if(empty($alt)){$alt=$author;} $filename=md5( strtolower( $mail ) ); $cachetime = $cache_time * 60; $size=$wpdb->get_var($wpdb->prepare("SELECT size FROM $table WHERE ID = %d", 1) ); $size_srcset=$size*2; $cachefile=$path.$filename.".png"; $user_id = $id_or_email->user_id; # $cachefile_srcset=$path.$filename."_2x.png"; if($mail=="[email protected]" || $user_id==0 || empty($user_id) ){$src_proof="gravatar.com";} $cachefile_srcset=$path.$filename."_2x.png"; if($mail=="[email protected]"){$src_proof="gravatar.com";} if (strpos($src_proof,'gravatar.com')!==false) { if (is_ssl()) { $host = 'https://secure.gravatar.com/avatar'; } else { $host = 'http://www.gravatar.com/avatar'; } $rating = strtolower(get_option('avatar_rating')); if(empty($rating)){$rating=r;} $grav_img = $host."/".$filename."?s=".$size."&d=".$default."&r=".$rating; $srcset_img = $host."/".$filename."?s=".$size_srcset."&d=".$default."&r=".$rating; $css=$css." grav-hashed grav-hijack"; } else { if (is_ssl()) { $host = 'https'; } else { $host = 'http'; } $pos = strpos($src_proof, ":"); $length=strlen ($src_proof); $src_proof = substr($src_proof, $pos, $length); $src_proof=$host.$src_proof; $url_host=parse_url($src_proof, PHP_URL_HOST); $pfad=parse_url($src_proof, PHP_URL_PATH); $query=parse_url($src_proof, PHP_URL_QUERY); $query=str_replace("__","",$query); if(!empty($query)){$query="?".$query;} $src_proof=$host.'://'.$url_host.$pfad.$query; $grav_img = $src_proof; $srcset_img = $src_proof; } #echo "url:".$grav_img; #if (empty($grav_img)){echo "url empty";} if (!file_exists($cachefile) || !file_exists($cachefile_srcset) || (time() - $cachetime > filemtime($cachefile))) { $file_copy=$wpdb->get_var($wpdb->prepare("SELECT get_option FROM $table WHERE ID = %d", 1) ); global $wp_filesystem; if (empty($wp_filesystem)) { require_once ( ABSPATH . "/wp-admin/includes/file.php" ); WP_Filesystem(); } if($file_copy==1) { //file_get_contents $grav_img=$wp_filesystem->get_contents($grav_img); $wp_filesystem->put_contents($cachefile,$grav_img,0644); $fileperm=substr(sprintf('%o', @fileperms($cachefile)),-4); $srcset_img=$wp_filesystem->get_contents($srcset_img); $wp_filesystem->put_contents($cachefile_srcset,$srcset_img,0644); $fileperm=substr(sprintf('%o', @fileperms($cachefile_srcset)),-4); } if($file_copy==2 || $file_copy==3) { //fopen / cUrl $fp = wp_remote_fopen($grav_img); $wp_filesystem->put_contents($cachefile,$fp,0644); $fileperm=substr(sprintf('%o', @fileperms($cachefile)),-4); $fp = wp_remote_fopen($srcset_img); $wp_filesystem->put_contents($cachefile_srcset,$fp,0644); $fileperm=substr(sprintf('%o', @fileperms($cachefile_srcset)),-4); } if($file_copy==4) { //PHP Copy @copy($grav_img, $cachefile); $fileperm=substr(sprintf('%o', @fileperms($cachefile)),-4); @copy($srcset_img, $cachefile_srcset); $fileperm=substr(sprintf('%o', @fileperms($cachefile_srcset)),-4); } if($fileperm!="0644") { @chmod($cachefile,0644); } if($fileperm!="0644") { @chmod($cachefile_srcset,0644); } } $cachefile_png = $path_file.$filename.".png"; $srcset = $path_file.$filename.'_2x.png'; //Fallback falls Caching nicht geklappt hat if(!file_exists($cachefile) || !file_exists($cachefile_srcset)) { return $avatar; } $count=uniqid(); $id='grav-'.$filename.'-'.$count; return "<img id='{$id}' alt='{$alt}' src='{$cachefile_png}' srcset='{$srcset}' class='{$css}' height='{$size}' width='{$size}' />"; } /* vor Funktionsaufruf prüfen ob Cache-Ordner vorhanden und beschreibbar */ $path=wp_upload_dir(); $path=$path['basedir']."/gravatar-cache/"; $path_ok=false; if (is_dir($path)) { $path_ok=1; } $is_writeable=$wpdb->get_var($wpdb->prepare("SELECT is_writeable FROM $table WHERE ID = %d", 1) ); $size=$wpdb->get_var($wpdb->prepare("SELECT size FROM $table WHERE ID = %d", 1) ); $size_get=$wpdb->get_var($wpdb->prepare("SELECT size_get FROM $table WHERE ID = %d", 1) ); $get_option=$wpdb->get_var($wpdb->prepare("SELECT get_option FROM $table WHERE ID = %d", 1) ); $cache_time=$wpdb->get_var($wpdb->prepare("SELECT cache_time FROM $table WHERE ID = %d", 1) ); /* Funktionsaufruf wenn Avatare aktiviert und Cacheordner vorhanden und beschreibbar, size passende Größe, Copyoption verfügbar, Cachetime gesetzt und der User(falls eingeloggt) kein Admin ist */ if(get_option('show_avatars') && $path_ok==1 && $is_writeable==1 && $size>19 && $size<201 && $size_get>0 && $size_get<4 && $get_option>0 && $get_option<5 && $cache_time>10079 && $cache_time<80641 && !current_user_can('manage_options')) { add_filter('get_avatar', 'gravatar_lokal', 16, 5); }Give me a feedback if this will solve the issue.
Best regards,
HarryHi Harry Milatz,
There’s some coding errors but it’s due to this form “`”:
<code>{$table}</code>
and I’ve replaced them in your latest code.Now after reloading opcache, then active the plugin with your latest codes, I see this:
PHP message: WordPress database error Column ‘is_writeable’ cannot be null for query INSERT INTOwp_harrys_gravatar_cache(size,size_get,get_option,cache_time,is_writeable,file_get_contents,fopen,curl,copy) VALUES (1, 1, 1, 1, NULL, 1, 1, 1, 1) made by activate_plugin, do_action(‘activate_harrys-gravatar-cache/harrys-gravatar-cache.php’), harrys_gravatar_cache_activation, get_size_gravatar”I fix it by adding this below to your latest code and it’s working great now (deactivate->activate without errors):
//–insert–
if(empty($copy) || $copy==0){$copy=1;}
//–insert–
if(empty($is_writeable) || $is_writeable==0){$is_writeable=1;}`Thanks for your great support! (-:
I meant I inserted the missing $is_writeable.
Thanks for your feedback, i will add the “$is_writeable condition” ->
if(empty($is_writeable) || $is_writeable==0){$is_writeable=1;}
in line 145 and release the update.
The topic ‘MySQL insert error on 1st activation’ is closed to new replies.