Loading github project into wordpress plugin - php

I am trying to use the following github project in my wordpress plugin:
PHP Object wrapper for the Google Chart API
I tried the following:
require_once(WPPLUGIN_PATH. "gchart/gChartInit.php");
$piChart = new gPieChart(); // <---- here I get the error
$piChart->addDataSet(array(112,315,66,40));
$piChart->setLabels(array("first", "second", "third","fourth"));
$piChart->setLegend(array("first", "second", "third","fourth"));
$piChart->setColors(array("ff3344", "11ff11", "22aacc", "3333aa"));
echo $piChart->getImgCode();
My problem seems to be that when I am using the above code, it jumps at line $piChart = new gPieChart(); into another wordpress-plugins autoloader and does not load the gPieChart.php properly.
The error message is:
[26-Jul-2016 20:39:59 UTC] PHP Fatal error: Class 'gPieChart' not found in /home/ubuntu/workspace/wp-content/plugins/wp-analytics-mail/sendTestMail.php on line 267
[26-Jul-2016 20:39:59 UTC] PHP Stack trace:
[26-Jul-2016 20:39:59 UTC] PHP 1. {main}() /home/ubuntu/workspace/index.php:0
[26-Jul-2016 20:39:59 UTC] PHP 2. require() /home/ubuntu/workspace/index.php:17
[26-Jul-2016 20:39:59 UTC] PHP 3. require_once() /home/ubuntu/workspace/wp-blog-header.php:19
[26-Jul-2016 20:39:59 UTC] PHP 4. include() /home/ubuntu/workspace/wp-includes/template-loader.php:75
[26-Jul-2016 20:39:59 UTC] PHP 5. include() /home/ubuntu/workspace/wp-content/themes/twentysixteen/page-analytics-analytics-mail.php:138
Any suggestions how to make the above code work?
I appreciate your reply!

Here's what you do.
Install Composer as per the doc or, if you're on Windows/Unix, figure out how to install the thing in your environment.
Create new file called composer.json in the root folder of your plugin (in this case wp-analytics-mail)
Again, as per the doc, paste this into the composer.json
{
"require": {
"gchartphp/gchartphp": "dev-master"
}
}
In the command line, get to the plugin folder and run composer update
Put require 'vendor/autoload.php'; somewhere close to the top of your main plugin file (sendTestMail.php)
Lower down in the same file, initialize and use the library:
$piChart = new gchart\gPieChart();

Related

"UnexpectedValueException" - error while crerate a new project

I wanted to create a new project in Laravel yesterday but I have a problem. If i put "laravel new project", it will display this error
[UnexpectedValueException]
Could not parse version constraint project: Invalid version string
"project"
If I want create a new project via composer "composer create-project --prefer-dist laravel/laravel project", it will return this error
[ErrorException]
"continue" targeting switch is equivalent to "break". Did you mean to
use " continue 2"?
But in this situation project will be create but I have w problem with run it,
I write "php artisan serv" and it return this
PHP Warning: require(/home/michal/project/vendor/autoload.php):
failed to open stream: No such file or directory in
/home/michal/project/artisan on line 18 PHP Stack trace: PHP 1.
{main}() /home/michal/project/artisan:0 PHP Fatal error: require():
Failed opening required '/home/michal/project/vendor/autoload.php'
(include_path='.:/usr/share/php') in /home/michal/project/artisan on
line 18 PHP Stack trace: PHP 1. {main}()
/home/michal/project/artisan:0
How I can solve my problem?
PS. My composer version is 1.5.6

PHP: Issue with class loading when running 'codeception run wpunit'

I recently switched autoload in composer.json from classlist to psr-4. Then in terminal I ran vendor/bin/composer dump-autoload in the project root, which is a plugin directory.
My autoload settings:
"autoload": {
"psr-4": { "A\\" : "classes/A" }
}
There is only 1 class in ./classes/A so it was a concern when the console said it loaded 545 classes. I double checked that I was in the right (plugin) folder instead of the wordpress root.
It appears composer is recursively loading every wordpress class starting from 3 levels below. I get a list of errors, here are a few of the top ones.
owner#G700:/var/www/html/wptest2/wp-content/plugins/fvc$ vendor/bin/codecept run wpunit
Codeception PHP Testing Framework v2.5.6
Powered by PHPUnit 7.5.10 by Sebastian Bergmann and contributors.
Running with seed:
PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback, class 'Utilities' not found in /var/www/html/wptest2/wp-includes/class-wp-hook.php on line 286
PHP Stack trace:
PHP 1. {main}() /var/www/html/wptest2/wp-content/plugins/fvc/vendor/lucatume/wp-browser/src/includes/isolated-install.php:0
PHP 2. activate_plugin() /var/www/html/wptest2/wp-content/plugins/fvc/vendor/lucatume/wp-browser/src/includes/isolated-install.php:210
PHP 3. do_action() /var/www/html/wptest2/wp-admin/includes/plugin.php:660
PHP 4. WP_Hook->do_action() /var/www/html/wptest2/wp-includes/plugin.php:465
PHP 5. WP_Hook->apply_filters() /var/www/html/wptest2/wp-includes/class-wp-hook.php:310
PHP Warning: require_once(../../vendor/autoload.php): failed to open stream: No such file or directory in /var/www/html/wptest2/wp-content/plugins/fvc/tests/wpunit/T1Test.php on line 2
PHP Stack trace:
PHP 1. {main}() /var/www/html/wptest2/wp-content/plugins/fvc/vendor/codeception/codeception/codecept:0
PHP 2. Codeception\Application->run() /var/www/html/wptest2/wp-content/plugins/fvc/vendor/codeception/codeception/codecept:43
PHP 3. Codeception\Application->run() /var/www/html/wptest2/wp-content/plugins/fvc/vendor/codeception/codeception/src/Codeception/Application.php:108
This is an autoload error right? Any idea how I can fix this?
In case this might be something else I am including the test file I am running. This is the only test file that should run with the command vendor/bin/codecept run wpunit
<?php
require_once("../../vendor/autoload.php");
require_once ('../../classes/A/T1.php');
class T1Test extends \Codeception\TestCase\WPTestCase
{
public function testAdd() {
$t1 = new \T1();
$this->assertEquals(1, 1);
$sum = $t1->add(5, 2);
$this->assertEquals(7, $sum, "didn't sum");
}
}

files not uploading in media library wordpress

I am currently using word press latest version. It was working fine yesterday but when i login again today. And when i upload image in the media library it gives this error that
An error occurred in the upload. Please try again later. wordpress
Also my uploaded images are not showing. I have searched for this. My short tags are also on.
[25-Mar-2016 11:50:46 UTC] PHP 1. {main}() E:\designbee\lifeco\wordpress\index.php:0
[25-Mar-2016 11:50:46 UTC] PHP 2. require() E:\designbee\lifeco\wordpress\index.php:17
[25-Mar-2016 11:50:46 UTC] PHP 3. require_once() E:\designbee\lifeco\wordpress\wp-blog-header.php:12
[25-Mar-2016 11:50:46 UTC] PHP 4. require_once() E:\designbee\lifeco\wordpress\wp-load.php:37
[25-Mar-2016 11:50:46 UTC] PHP 5. require_once() E:\designbee\lifeco\wordpress\wp-config.php:89
[25-Mar-2016 11:50:46 UTC] PHP 6. do_action() E:\designbee\lifeco\wordpress\wp-settings.php:376
[25-Mar-2016 11:50:46 UTC] PHP 7. call_user_func_array: {E:\designbee\lifeco\wordpress\wp-includes\plugin.php:525}() E:\designbee\lifeco\wordpress\wp-includes\plugin.php:525
[25-Mar-2016 11:50:46 UTC] PHP 8. lifeco_setup() E:\designbee\lifeco\wordpress\wp-includes\plugin.php:525
this is my log file (Last portion)
There was a div outside of the function in my custom post type.

How to use / import / require PHP library that loads on php.ini?

After start learning some of PHP I had a doubt that I didn't found the answer on StackOverflow nor Google until now.
I installed a lib on my system and I make it load as an extension on the php.ini file, but I don't know how I can call this extension in my .php files now. I can't show you the lib name at the moment, but I'll call it by "php5fooLib".
I have installed the "php5fooLib" library on /usr/include/php5/ext and have included it on my php.ini with the following line:
extension=php5fooLib.so
And when I try to create a .php file and run it on my Terminal, I got:
PHP Warning: require_once(php5fooLib): failed to open stream: No such file or directory in /home/fernando/Documents/programacao/puro-crypto/main.php on line 4
PHP Stack trace:
PHP 1. {main}() /home/fernando/Documents/programacao/puro-crypto/main.php:0
PHP Fatal error: require_once(): Failed opening required 'php5fooLib' (include_path='“.:/opt/Zend/library”') in /home/fernando/Documents/programacao/puro-crypto/main.php on line 4
PHP Stack trace:
PHP 1. {main}() /home/fernando/Documents/programacao/puro-crypto/main.php:0
The php file I've created is the following:
<?php
require_once("php5fooLib");
class Main {
}
?>
So, given that problem, how can I import / use / require / include a PHP library that's loading on PHP.ini file?
You can only use require and require_once to load .php files.
If your library is already loaded, you can use the functions and classes that it provides you

Class 'Logger' not found in LoggerRoot.php

I'm trying to install log4php in a very simple site (trying to learn how it works).
Apache is installed in /etc/ and vhost is defined as:
DocumentRoot "/home/fferri/workspace/site-tutorial/www"
ServerName tutorial.localhost
php_value auto_prepend_file "/home/fferri/workspace/site-tutorial/www/ConfigureSite.php"
My site is at /home/fferri/workspace/site-tutorial/www/ and log4php folder is at /home/fferri/workspace/site-tutorial/services/lib/log4php-0.9/src/log4php/
Then, I define
define('LOG4PHP_DIR', "/home/fferri/workspace/site-tutorial/services/lib/log4php-0.9/src/log4php/");
into ConfigureSite.php
And, into another file, I try to init log4php:
include(LOG4PHP_DIR."/Logger.php");
$log = Logger::getLogger("main");
$log->info("foo");
$log->warn("bar");
The error is always the same:
Fatal error: Class 'Logger' not found in /home/fferri/workspace/site-tutorial/services/lib/log4php-0.9/src/log4php/LoggerRoot.php on line 37
It's worth to say that LoggerRoot.php and Logger.php are in the same folder. I think I have a problem of paths because my server and my site aren't in the same folder, but I need some hint
Adding the complete stack trace of error:
[22-May-2012 11:34:38] PHP Fatal error: Class 'Logger' not found in /home/fferri/workspace/site-tutorial/services/lib/log4php-0.9/src/log4php/LoggerRoot.php on line 36
[22-May-2012 11:34:38] PHP Stack trace:
[22-May-2012 11:34:38] PHP 1. {main}() /home/fferri/workspace/site-tutorial/www/index.php:0
[22-May-2012 11:34:38] PHP 2. require_once() /home/fferri/workspace/site-tutorial/www/index.php:12
[22-May-2012 11:34:38] PHP 3. require_once() /home/fferri/workspace/site-tutorial/www/greet_at_opening.php:11
[22-May-2012 11:34:38] PHP 4. require_once() /home/fferri/workspace/site-tutorial/services/lib/log4php-0.9/src/log4php/Logger.php:24
[22-May-2012 11:34:38] PHP 5. require_once() /home/fferri/workspace/site-tutorial/services/lib/log4php-0.9/src/log4php/LoggerCategory.php:27
[22-May-2012 11:34:38] PHP 6. require_once() /home/fferri/workspace/site-tutorial/services/lib/log4php-0.9/src/log4php/spi/LoggerLoggingEvent.php:28
[22-May-2012 11:34:38] PHP 7. require_once() /home/fferri/workspace/site-tutorial/services/lib/log4php-0.9/src/log4php/LoggerManager.php:29
[22-May-2012 11:34:38] PHP 8. require_once() /home/fferri/workspace/site-tutorial/services/lib/log4php-0.9/src/log4php/LoggerHierarchy.php:28
So, first, Logger is found but on loading LoggerHierarchy, Logger isn't. Why????
SOLVED: The error is to use log4php-0.9 and php5 together. Downloading the library located in apache web (2.1), the problem is solved.
Thanks!
I think using dirname(__FILE__) will look better.
I also thought it would be the path...
Maybe the Class name defenition then. do you call new Logger in Logger.php?
What about something like:
include(LOG4PHP_DIR."/Logger.php");
$log = new Logger();
$log->info("foo");
The error is to use log4php-0.9 and php5 together. Downloading the library located in apache web (2.1), the problem is solved.

Categories